| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Import\Images |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services\Import; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* This class will import external images added to WordPress posts, |
| 13 |
* using our Extendify library. |
| 14 |
*/ |
| 15 |
|
| 16 |
class ImagesImporter |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Initialize the class and hook into the publish_post and schedule events action. |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
// Get the data directly from the database. |
| 26 |
$partnerData = get_option('extendify_partner_data_v2', []); |
| 27 |
// If the setting is not enabled, we do nothing. |
| 28 |
if (! ($partnerData['enableImageImports-1-14-6'] ?? false)) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
try { |
| 33 |
$this->dailyImageImportCheck(); |
| 34 |
$this->everyTenMinImportCheck(); |
| 35 |
} catch (\Exception $e) {} // phpcs:ignore |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* This checks once a day a bit more thoroughly for images, and |
| 40 |
* will set a signal for the importer to run. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* @throws \Exception Emits Exception in case of an error. |
| 44 |
*/ |
| 45 |
public function dailyImageImportCheck() |
| 46 |
{ |
| 47 |
if (! \wp_next_scheduled('extendify_daily_import_images_check')) { |
| 48 |
\wp_schedule_event( |
| 49 |
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp |
| 50 |
(new \DateTime('tomorrow 03:00', wp_timezone()))->getTimestamp(), |
| 51 |
'daily', |
| 52 |
'extendify_daily_import_images_check' |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
\add_action('extendify_daily_import_images_check', function () { |
| 57 |
// In this case, we will be doing the import soon. |
| 58 |
if (\get_option('extendify_check_for_image_imports')) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if (Post::countPostsNeedingUpdate()->posts_count > 0) { |
| 63 |
\update_option('extendify_check_for_image_imports', true, false); |
| 64 |
} |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* This does a cheap, quick check often for images to import, |
| 70 |
* then if found, imports them here. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function everyTenMinImportCheck() |
| 75 |
{ |
| 76 |
\add_action('init', function () { |
| 77 |
// Create a custom 10 minutes schedule that we use below. |
| 78 |
// phpcs:ignore WordPress.WP.CronInterval -- Verified > 10 min. |
| 79 |
\add_filter('cron_schedules', function ($schedules) { |
| 80 |
$schedules['extendify_every_ten_minutes'] = [ |
| 81 |
'interval' => (10 * MINUTE_IN_SECONDS), |
| 82 |
'display' => __('Every 10 minutes', 'extendify-local'), |
| 83 |
]; |
| 84 |
|
| 85 |
return $schedules; |
| 86 |
}); |
| 87 |
|
| 88 |
if (! \wp_next_scheduled('extendify_images_importer_light')) { |
| 89 |
\wp_schedule_event( |
| 90 |
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp |
| 91 |
time(), |
| 92 |
'extendify_every_ten_minutes', |
| 93 |
'extendify_images_importer_light' |
| 94 |
); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
\add_action('extendify_images_importer_light', function () { |
| 99 |
(new ImagesImporterRunner())->run(); |
| 100 |
}); |
| 101 |
} |
| 102 |
} |
| 103 |
|