Mil*_*vić 5 php wordpress import
我想知道是否有可能加快我为wordpress网站创建的现有导入过程?我需要从外部xml文件导入数据,根据该数据创建产品,然后下载图像并将它们分配给创建的产品.问题是还需要通过wp函数生成缩略图并将其与产品相关联.我目前设法每秒插入1到2个图像,使用cron作业每5分钟调用一次脚本,导入限制为120个图像.对于120个图像的块,这相当于大约80到150秒.Tre问题是我需要导入大约10000个产品和200000张图像.
在tmp文件夹中每次约1000张图像
我使用的当前功能是:
function upload_image(){
ini_set("memory_limit","2048M");
add_filter('intermediate_image_sizes', function ($image_sizes){
return array('thumbnail');
},1000 );
if($this->ids===false) {
self::get_all_ids();
}
$upload_dir = wp_upload_dir();
$root = $upload_dir['basedir'].DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR;
$handle = opendir($root);
$files=array();
$propertys=array();
$propertys_images=array();
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$tmp_data=explode('_',$entry);
if(isset($tmp_data[1])) {
$post_id = isset($this->ids[$tmp_data[1]]) ? $this->ids[$tmp_data[1]] : 0;
if (!empty($post_id)) {
$propertys[$post_id] = $post_id;
$files[] = array('url' => $root . $entry, 'post_id' => $post_id, 'name' => $entry);
}else{
unlink( $root . $entry);
}
}else{
unlink( $root . $entry);
}
}
}
if (empty($propertys)){return'';}
global $wpdb;
$results = $wpdb->get_results("
SELECT ID,post_parent, post_title
FROM $wpdb->posts AS posts
WHERE post_type = 'attachment'
AND post_parent IN(".implode(',',$propertys).")
Order by ID asc
",ARRAY_A ) ;
$set_images=array();
foreach ($results as $r){
$set_images[]=$r['post_title'];
$propertys_images[$r['post_parent']][$r['ID']]=$r['ID'];
}
foreach ($files as $k => $d){
if(in_array($d['name'],$set_images)){
unset($files[$k]);
unlink($d['url']);
}
}
require_once( ABSPATH . 'wp-admin/includes/image.php' );
foreach ($files as $id_of_i => $f){
if($id_of_i > 120){continue;}
$image_url = $f['name'];
$post_id=$f['post_id'];
$base_path=$upload_dir['basedir'].DIRECTORY_SEPARATOR.'tmp' .DIRECTORY_SEPARATOR.$image_url;
$filename = basename( $image_url );
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
if(file_exists($base_path)) {
rename($base_path, $file);
//copy($base_path, $file);
}
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_parent' => $post_id,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );
$propertys_images[$post_id][$attach_id]=$attach_id;
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
foreach ($propertys_images as $prop_id => $images){
$images_ids=array_values($images);
set_post_thumbnail($prop_id, $images_ids[0]);
update_post_meta($prop_id, '_thumbnail_id', $images_ids[0]);
update_post_meta($prop_id, 'fave_prop_slider_image', $images_ids[0]);
$num=get_post_meta($prop_id,'numb_of_images',true);
delete_post_meta($prop_id,'fave_property_images');
foreach ($images_ids as $k =>$id){
if($k!='0'){
add_post_meta($prop_id, 'fave_property_images', $id);
}
}
if($num<=count($images_ids)){
update_post_meta($prop_id, 'all_images_imp', 'true');
}
}
exit();
}
Run Code Online (Sandbox Code Playgroud)
首先我设置为仅生成此图像的缩略图,然后我收集所有product_codes("get_all_ids()")然后我从tmp文件夹中获取所有图像并检查该img的产品是否存在,然后获取我的图像的产品的所有附件然后我将图像迁移到wp上传路径
然后调用"wp_insert_attachment"添加基本图像,然后使用"wp_generate_attachment_metadata"生成缩略图,然后使用"wp_update_attachment_metadata"附加到附件的信息
收到的附件ID我添加到产品和产品库中
我认为在这个脚本中,减速部分生成缩略图是否有可能加快此过程,或者如果您有任何建议
使用当前速度,大约需要5到10天才能添加所有图像
先感谢您