| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Import\Images Runner |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services\Import; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* This class will handle the actual imports |
| 13 |
*/ |
| 14 |
|
| 15 |
class ImagesImporterRunner |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Process posts content to import external images. |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function run() |
| 23 |
{ |
| 24 |
// Return early if conditions are not met for processing images. |
| 25 |
if ( |
| 26 |
!\get_option('extendify_check_for_image_imports') |
| 27 |
|| \get_transient('extendify_import_images_check_delay') |
| 28 |
|| \wp_get_upload_dir()['error'] |
| 29 |
) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
// Try to execute set the limit to something that will work forever. |
| 34 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors, Generic.PHP.NoSilencedErrors.Discouraged |
| 35 |
if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
| 36 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors, Generic.PHP.NoSilencedErrors.Discouraged |
| 37 |
@set_time_limit(0); |
| 38 |
} |
| 39 |
|
| 40 |
// Set a marker in the future so we don't check while working. |
| 41 |
$this->delayProcessing(HOUR_IN_SECONDS); |
| 42 |
|
| 43 |
// Get the posts that we need to update. |
| 44 |
$posts = Post::all(); |
| 45 |
|
| 46 |
if (!$posts) { |
| 47 |
\delete_transient('extendify_import_images_check_delay'); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// loop over the posts. |
| 52 |
foreach ($posts as $post) { |
| 53 |
// If the post is locked we update the marker to run next hour. |
| 54 |
if (Post::isLocked($post->ID)) { |
| 55 |
$this->delayProcessing(15 * MINUTE_IN_SECONDS); |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$updatedBlockContent = (new BlocksUpdater())->getModifiedBlocksInPost($post); |
| 60 |
$status = Post::update($post, $updatedBlockContent); |
| 61 |
|
| 62 |
// If something went wrong, check again (much) later. |
| 63 |
if (is_wp_error($status)) { |
| 64 |
$this->delayProcessing(6 * HOUR_IN_SECONDS); |
| 65 |
} |
| 66 |
}//end foreach |
| 67 |
|
| 68 |
// Delete the signal that says there's something to import. |
| 69 |
if (!Post::countPostsNeedingUpdate()->posts_count) { |
| 70 |
\delete_option('extendify_check_for_image_imports'); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Sets a marker to delay processing until later. |
| 76 |
* |
| 77 |
* @param mixed $expiration Time until expiration in seconds. Default 86400 (one day). |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function delayProcessing($expiration) |
| 82 |
{ |
| 83 |
\set_transient('extendify_import_images_check_delay', time(), $expiration); |
| 84 |
} |
| 85 |
} |
| 86 |
|