Add file from Wordpress server checker plugin and limit 999 files

I am using this wordpress plugin to add files from server and I have 2 problems. Files are not skipped, so I need to modify the code myself to add a validation process, but the problem is that the validation process is very slow for each file. The second problem is that the plugin cannot add more than 999 files at a time and I need to add about 50,000 files to the media library.

The code I modified to check if the file is in the media library and loop through it:

class.add-of-server.php

function handle_imports() { 
    if ( !empty($_POST['files']) && !empty($_POST['cwd']) ) {
        $query_images_args = array(
        'post_name' => trim ( $post_name ), 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
        );

        $query_images = new WP_Query( $query_images_args );
        $images = array();
        foreach ( $query_images->posts as $image) {
            $image_trim = wp_get_attachment_url( $image->ID );
            $image_trim = explode('/', $image_trim);
            $images[] = end($image_trim);
        }
    // $images is the array with the filenames where I stock the media library files
        $files = array_map('stripslashes', $_POST['files']);
        $cwd = trailingslashit(stripslashes($_POST['cwd']));
        $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
        $import_date = isset($_REQUEST['import-date']) ? $_REQUEST['import-date'] : 'file';

        $import_to_gallery = isset($_POST['gallery']) && 'on' == $_POST['gallery'];
        if ( ! $import_to_gallery && !isset($_REQUEST['cwd']) )
        $import_to_gallery = true; // cwd should always be set, if it not, and neither is gallery, this must be the first page load.

        if ( ! $import_to_gallery )
        $post_id = 0;

        flush();
        wp_ob_end_flush_all();

        foreach ( (array)$files as $file ) {
            if (!in_array($file, $images)) {
             // here I ask if the image that I want to add is in the media library or not
                $filename = $cwd . $file;
                $id = $this->handle_import_file($filename, $post_id, $import_date);
                if ( is_wp_error($id) ) {
                    echo '<div class="updated error"><p>' . sprintf(__('<em>%s</em> was <strong>not</strong> imported due to an error: %s', 'add-from-server'), esc_html($file), $id->get_error_message() ) . '</p></div>';
                } else {
                    //increment the gallery count
                    if ( $import_to_gallery )
                    echo "<script type='text/javascript'>jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);</script>";
                    echo '<div class="updated"><p>' . sprintf(__('<em>%s</em> has been added to Media library', 'add-from-server'), esc_html($file)) . '</p></div>';
                }
                flush();
                wp_ob_end_flush_all();
            } else {
                echo '<div class="updated error">File '.$file.' had been skipped because it is already in the media library.</div>';
            }
        }
    }
}

      

So please help 1. How to speed up the verification process, I would like to mention that this code slows down the process (although I have 10,000 images in my media library):

$query_images_args = array(
'post_name' => trim ( $post_name ), 'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $image_trim = wp_get_attachment_url( $image->ID );
    $image_trim = explode('/', $image_trim);
    $images[] = end($image_trim);
}

      

The second problem is with the 999 file limit, how to overcome this limit? I believe it has something to do with the WordPress code, but don't know how to pass it.

+3


source to share


1 answer


Ok, I won't answer your question directly because I don't understand why you are using a plugin to do this, but ... what you are trying to do is simple enough without using a plugin.

First you need to encode the directory and then check if the media exists and if not, add the media to the media library.



function thisismyurl_add_media_to_library() {

    global $wpdb;

    $file_count = 0;

    /* if the user isn't an admin user, don't do anything */
    if ( ! current_user_can( 'manage_options' ) )
        return;




    /* (you'll want to reset this to your path */   
    $file_path = ABSPATH . '/import/path/to/files/';

    /* get a list of all files in a specific directory */
    $files = glob( $file_path . '*.jpg');


    if ( ! empty( $files ) ) {

        /* now we loop the files */
        foreach ( $files as $file ) {
            unset( $post_id );


            /* it likely that a server will time out with too many files so we're going to limit it to 999 new files */
            if ( $file_count < 999 ) {


                $filename = str_replace( $file_path, '', $file );
                /* check to see if the image already exists */
                $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM  $wpdb->posts WHERE post_title = %s", $filename ) );


                /* the file does not exist */
                if ( empty( $post_id ) ) {

                    /* only count new files when checking for the file count */
                    $file_count++;

                    $attachment = array(
                        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                        'post_mime_type' => wp_check_filetype( basename( $file ), null ),
                        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                        'post_content'   => '',
                        'post_status'    => 'inherit'
                    );

                    wp_insert_attachment( $attachment, $filename );


                    /* this is commented out for now, but if you uncomment it, the code will delete each file after it been inserted */
                    /*

                    if ( $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM  $wpdb->posts WHERE post_title = %s", $filename ) ) )
                        unlink( $file );

                    */


                } /* if */
            }

        } /* foreach  */

    } /* if */

}
add_action( 'wp_head', 'thisismyurl_add_media_to_library' );

      

+2


source







All Articles