wp-auto-upload icon indicating copy to clipboard operation
wp-auto-upload copied to clipboard

Import big images or many images in same time and timeout error

Open airani opened this issue 8 years ago • 4 comments

گزارش شده وقتی تصویر حجم بالایی داره یا تعداد تصاویر زیاد هست خطای تایم اوت رخ میده

airani avatar Mar 19 '17 21:03 airani

@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.

dillix avatar Dec 07 '19 10:12 dillix

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


andyg2 avatar Jan 09 '22 14:01 andyg2

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 🙏🙏🙏

armmiespotter avatar May 10 '23 16:05 armmiespotter

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.

andyg2 avatar May 16 '23 07:05 andyg2