Open airani opened 7 years ago
@airani I think we should use bulk image upload feature like plugin ewww image optimizer does. Or you can add feature to import from wp cli command.
How about save external images until say 10 seconds has expired, then add a single cron job to trigger the save post image handler save
.
Untested edit
public function save($postarr) {
$excludePostTypes = self::getOption('exclude_post_types');
if (is_array($excludePostTypes) && in_array($postarr['post_type'], $excludePostTypes, true)) {
return false;
}
$content = $postarr['post_content'];
$images = $this->findAllImageUrls(stripslashes($content));
if (count($images) == 0) {
return false;
}
$startTime = time();
$limit = 10;
foreach ($images as $image) {
if (time() <= $startTime + $limit) {
$uploader = new ImageUploader($image['url'], $image['alt'], $postarr);
if ($uploadedImage = $uploader->save()) {
$urlParts = parse_url($uploadedImage['url']);
$base_url = $uploader::getHostUrl(null, true, true);
$image_url = $base_url . $urlParts['path'];
$content = preg_replace('/' . preg_quote($image['url'], '/') . '/', $image_url, $content);
$content = preg_replace('/alt=["\']' . preg_quote($image['alt'], '/') . '["\']/', "alt='{$uploader->getAlt()}'", $content);
}
} else {
$this->finishLater($postarr['ID']);
}
}
return $content;
}
public function finishLater($post_id) {
$when = time() + 10; // in 10 seconds
$post = get_post($post_id);
wp_schedule_single_event($when, [$this, 'save'], $post);
}
How about save external images until say 10 seconds has expired, then add a single cron job to trigger the save post image handler
save
.Untested edit
public function save($postarr) { $excludePostTypes = self::getOption('exclude_post_types'); if (is_array($excludePostTypes) && in_array($postarr['post_type'], $excludePostTypes, true)) { return false; } $content = $postarr['post_content']; $images = $this->findAllImageUrls(stripslashes($content)); if (count($images) == 0) { return false; } $startTime = time(); $limit = 10; foreach ($images as $image) { if (time() <= $startTime + $limit) { $uploader = new ImageUploader($image['url'], $image['alt'], $postarr); if ($uploadedImage = $uploader->save()) { $urlParts = parse_url($uploadedImage['url']); $base_url = $uploader::getHostUrl(null, true, true); $image_url = $base_url . $urlParts['path']; $content = preg_replace('/' . preg_quote($image['url'], '/') . '/', $image_url, $content); $content = preg_replace('/alt=["\']' . preg_quote($image['alt'], '/') . '["\']/', "alt='{$uploader->getAlt()}'", $content); } } else { $this->finishLater($postarr['ID']); } } return $content; } public function finishLater($post_id) { $when = time() + 10; // in 10 seconds $post = get_post($post_id); wp_schedule_single_event($when, [$this, 'save'], $post); }
hello sir can you help me to make it something like cronjob, pleaseeeeeeeeee 🙏🙏🙏
How about save external images until say 10 seconds has expired, then add a single cron job to trigger the save post image handler
save
.hello sir can you help me to make it something like cronjob, pleaseeeeeeeeee 🙏🙏🙏
WordPress has an internal cronjob system which is called by wp_schedule_single_event
function. See: https://developer.wordpress.org/reference/functions/wp_schedule_single_event/
So there's no need to add any additional cronjob.
Essentially this will allow the main script to continue without having to wait for the images to be processed, and then 10 seconds later (but in the background) another process will do the slower processes. This should stop the timeout when processing large or lots of images.
گزارش شده وقتی تصویر حجم بالایی داره یا تعداد تصاویر زیاد هست خطای تایم اوت رخ میده