php - WordPress 从服务器导入大量图像并与帖子相关联
问题描述
我想知道是否可以加快我为 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();
}
首先,我设置为仅生成此图像的缩略图,然后收集所有 product_codes("get_all_ids()") 然后我从 tmp 文件夹中获取所有图像并检查该 img 的产品是否存在,然后获取我拥有图像的产品的所有附件然后我将图像迁移到 wp 上传路径
然后调用“wp_insert_attachment”添加基本图像,然后使用“wp_generate_attachment_metadata”生成缩略图,然后使用“wp_update_attachment_metadata”将该信息附加到附件
收到我添加到产品和产品库的附件 ID
我认为在这个脚本中减慢的部分正在生成一个缩略图是否有可能加快这个过程,或者如果你有任何建议
以目前的速度,添加所有图像大约需要 5 到 10 天
先感谢您
解决方案
问题是 EWWW 图像优化器,因此您需要注意插件之间是否有某种优化器处于活动状态。当我禁用插件(断开挂钩)时插入图像的速度会增加战利品
推荐阅读
- javascript - 如何在表单提交后执行 Javascript
- python-3.x - 用Python根据if条件计算时间差
- memory-management - 如何在没有 MMU 的情况下实现内存管理系统调用
- sql-server - 当一行连接到另一个表时如何删除重复行
- angular - 在代码停止后导航页面时,不应显示网页
- c# - EWS - 按全名获取文件夹
- java - RegEx - 在 HTML-Anchor 元素中的组之间未捕获的任何内容
- python - 我在代码中做错了什么,通过 Flask 应用程序向 MySQL 提交表单值?
- c# - 为什么我的 android xamarin 应用程序在权限被拒绝时会自动关闭?
- lua - 为什么雾的颜色不会根据 Roblox Studio 中的 ClockTime 变化?