airani / wp-auto-upload

Automatically detect external images in the Wordpress post content and import images to your site and adding to the media library and finally replace image urls with new urls
http://wordpress.org/plugins/auto-upload-images/
80 stars 40 forks source link

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

Open airani opened 7 years ago

airani commented 7 years ago

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

dillix commented 4 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.

andyg2 commented 2 years ago

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);
  }
armmiespotter commented 1 year ago

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

andyg2 commented 1 year ago

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.