我正在使用此wordpress plugin以便从服务器添加文件,我有2个问题。文件没有被跳过,因此我需要自己更改代码以添加检查过程,但问题是每个文件的检查过程都非常慢。第二个问题是该插件不能同时添加超过999个文件,我需要向媒体库添加大约50000个文件。
我更改的代码,用于检查文件是否在媒体库中并跳过它:
class.add从 - 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代码有关,但不知道如何通过它。
答案 0 :(得分:2)
好的,我不会直接回答您的问题,因为我不明白您为什么要使用插件来执行此操作,但是......您正在尝试做什么在不使用插件的情况下很容易。
首先,您需要循环目录,然后检查媒体是否存在,如果不存在,则将媒体添加到媒体库。
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' );