Automatic File Creation

Home Forums Feature Requests Automatic File Creation

This topic contains 6 replies, has 2 voices, and was last updated by  Vincent Mimoun-Prat 9 months, 2 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #306394

    Jacopo Cappelli
    Participant

    Hi,
    is it possible to automate file creation?

    I want that if I upload a file with FTP Customer Area create automatically the file with user assignment (made with txt or with file name)

    Is it possible?

    Thanks,
    Jacopo

    #306941

    Hi

    The current recommandation is to develop that by yourself using our functions. We do not have an add-on for that.

    Regards

    #407127

    Jacopo Cappelli
    Participant

    I made a script that load automatically file on WP Customer Area but I have a question:
    How can i create the id for postmeta?

    I find this:
    $meta[‘id’] = $this->compute_file_id($meta);

    But I see that create value that it’s invalid.

    Can you help me?

    Thanks,
    Jacopo

    #410832

    Jacopo Cappelli
    Participant

    I try with other code:

    function get_base_private_storage_directory()
    {
    $dir = WP_CONTENT_DIR . ‘/customer-area/storage’;
    $dir = apply_filters(‘cuar/core/ownership/base-private-storage-directory’, $dir);
    //if ($create_dirs && !file_exists($dir)){
    // mkdir($dir, 0775, true);
    //}
    return $dir;
    }
    $dir = get_base_private_storage_directory() . ‘/’ . md5(‘wpca-‘ . $postId . md5(-$postId * $postId * $postId));

    But not work!

    Please can you help me?

    Thanks,
    Jacopo

    #412356

    Can you post the code you are using to create the file?

    #413883

    Jacopo Cappelli
    Participant

    I use this one:

    <?php
    require_once(“wp-load.php”);//CHANGEME

    function get_base_private_storage_directory()
    {
    $dir = WP_CONTENT_DIR . ‘/customer-area/storage’;
    $dir = apply_filters(‘cuar/core/ownership/base-private-storage-directory’, $dir);
    //if ($create_dirs && !file_exists($dir)){
    // mkdir($dir, 0775, true);
    //}
    return $dir;
    }
    function compute_file_id($file)
    {
    print_r($file);
    print_r($file[‘file’]);
    $id = md5($file[‘file’]);
    print_r($id);
    return apply_filters(‘cuar/private-content/files/file-id’, $id, $file);
    }

    /**
    Function that returns the private file categoris as an array of strings. The strings
    returned are the categories slugs
    **/
    function getCategories(){
    $terms = get_terms( array ( ‘cuar_private_file_category’), array (“hide_empty” => 0) ) ;
    $ret = array();
    foreach($terms as $t){
    array_push($ret, $t->slug);
    }
    return $ret;
    }

    /**
    Function that returns the users group as an array.
    **/
    function getGroups(){
    $ret = array();
    $groups = get_posts( array ( ‘post_type’ => “cuar_user_group” ) );
    return $groups;
    }

    /**
    Function that returns the ID of a certain group, passed as $val. The ID is seek in $arr.
    **/
    function getGroupID($val, $arr){
    $ret = 0;
    foreach($arr as $a){
    if (get_post_field(‘post_name’,$a )== $val){
    $ret = get_post_field(‘ID’,$a );
    break;
    }

    }
    return $ret;
    }

    function inGroup($array_groups,$value){
    $trovato = false;
    foreach( $array_groups as $el ){
    if( $el->post_name == $value)
    $trovato = true;
    }
    return $trovato;
    }

    function set_html_content_type() {
    return ‘text/html’;
    }

    $sep = “_”;
    $path = “file/”;//CHANGEME
    $files = array_diff(scandir($path), array(‘.’, ‘..’ ));
    $mailFrom = “pippo@gmail.com”;
    $cats = getCategories() ; //get all the file categories
    $groups = getGroups();
    //parse all files
    foreach($files as $val ){
    $fullName = explode(“.”, $val); //separates fileName from the extension
    $parts = explode($sep, $fullName[0]);

    if( count($parts) < 3) {
    echo “Error: not enough fields”;
    mail(“ahboh@pinco.com”, $mailFrom, “Error: not enough fields”);
    }else{

    //if(!array_search($parts[1], array_column($groups, ‘post_name’))){
    if( !inGroup($groups,$parts[1]) ){
    echo “Error: group does not exists “.$parts[1].” – “.$groups;
    //print_r($groups);
    mail(“pippo@pippo.com”, $mailFrom, “Error: group does not exists”);
    }else{

    if(! in_array($parts[2], $cats) ){ //if the category does not exists
    echo “Error: category does not exists”;
    mail(“pippo@pippo.com”, $mailFrom, “Error: category does not exists”);
    }else{
    $group_for_email = $parts[1];
    $author_group_id = getGroupID($parts[1], $groups);
    //search the id of the post group
    $group_name = get_posts ( array (‘name’ => $parts[1],
    ‘post_type’ => ‘cuar_user_group’,
    ‘post_status’ => ‘publish’ ) ) ;
    //search the name of the
    // Create post object
    $my_post = array(
    ‘post_author’ => 1,
    ‘post_title’ => $parts[0],
    ‘post_content’ => ”,
    ‘post_type’ => ‘cuar_private_file’,
    ‘post_status’ => ‘publish’
    );

    // Insert the post into the database
    $postId = wp_insert_post( $my_post );
    $ret = wp_set_object_terms($postId, array($parts[2]), ‘cuar_private_file_category’ ); //set the post category (special file category)
    add_post_meta($postId, “cuar_owner_queryable”, “|grp_$author_group_id|”);
    $group_array = array(
    ‘grp’ => $parts[1]
    );
    update_post_meta( $postId, ‘cuar_owner_displayname’, $group_array);
    add_post_meta($postId, “cuar_owner_sortable_displayname”, “User Group – “.get_post_field(‘post_title’, $group_name[0]));

    if($postId == 0 || is_wp_error($ret)){ // if the insert reports an error
    echo “Error”;
    mail(“pippo@pippo.com”, “pippo@pippo.com”, “Error”);
    } else{

    $nomeFile = $parts[0].’.’.$fullName[1];
    $meta = array(
    ‘id’ => ”,
    ‘source’ => ‘local’,
    ‘post_id’ => $postId,
    ‘file’ => $nomeFile,
    ‘caption’ => $nomeFile,
    ‘extra’ => ”
    );
    $meta[‘id’] = compute_file_id($meta);
    $meta2 = array(
    $meta[‘id’] => $meta
    );
    //print_r(serialize($meta2));
    update_post_meta( $postId, ‘cuar_private_file_file’, $meta2);

    //invio email
    //print_r(“———————“);
    $users = get_post_meta($author_group_id, ‘cuar_group_members’);
    $array_users = explode(‘|’,$users[0]);
    foreach( $array_users as $user ){
    $user_data = get_userdata($user);
    //print_r($user_data->user_email);

    $message = ‘Ciao ‘.$user_data->first_name.’,’;
    $message .= ‘‘.$nomeFile.’‘;
    $message .= “Grazie”;
    add_filter( ‘wp_mail_content_type’, ‘set_html_content_type’ );
    wp_mail($user_data->user_email,”[Pippo] Nuovo File Disponibile”,$message);

    }

    $postId=$meta[‘id’];
    echo $postId . “\n”;
    $dir = get_base_private_storage_directory() . ‘/’ . md5(‘wpca-‘ . $postId . md5(-$postId * $postId * $postId));
    mkdir( “$dir/”, 0775, true);
    rename(“$path/$val”, “$dir/$parts[0].$fullName[1]”); //move the file in another folder

    echo “post created with post_id = $postId \n”;
    }
    }
    }
    }
    }
    ?>

    #414264

    Have a look at our functions to add files:

    https://github.com/marvinlabs/customer-area/blob/master/src/php/functions/functions-private-files.php#L349-L429

    Instead of copy/pasting the code from the addons, you can directly use their functions.

    1. Get the addon: $addon = cuar_addon('the-addon-slug');

    2. Use it: $addon->do_something_for_me();

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.