从服务器添加文件wordpress插件检查和限制999个文件


Add file from server wordpress plugin checking and limit of 999 files

我使用这个wordpress插件,以便从服务器添加文件,我有2个问题。文件没有被跳过,因此我需要自己修改代码,以便添加一个检查过程,但问题是检查过程对于每个文件都非常缓慢。第二个问题是插件不能一次添加超过999个文件,我需要添加大约50000个文件到媒体库。

检查文件是否在媒体库中并跳过它的代码:

class.add-from-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's 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>';
            }
        }
    }
}

所以请帮忙1. 我怎样才能加快检查过程,想要提到的是,这段代码是一个减缓进程(我有10000张图片在媒体库):

$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);
}

第二个问题是999个文件的限制,如何克服这个限制?我相信是与wordpress代码有关,但不知道如何通过它

好吧,我不打算直接回答你的问题,因为我不明白为什么你使用插件来做到这一点,但是…你想做的事情很容易,不需要使用插件。

首先,您需要循环一个目录,然后检查该媒体是否存在,如果不存在,则将该媒体添加到媒体库中。

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's 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's 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' );