| 1 |
<?php |
| 2 |
|
| 3 |
namespace ContentEgg\application; |
| 4 |
|
| 5 |
defined('\ABSPATH') || exit; |
| 6 |
|
| 7 |
use ContentEgg\application\components\Scheduler; |
| 8 |
use ContentEgg\application\components\ContentManager; |
| 9 |
use ContentEgg\application\components\ModuleManager; |
| 10 |
use ContentEgg\application\components\stopwatch\Stopwatch; |
| 11 |
|
| 12 |
/** |
| 13 |
* ModuleUpdateScheduler class file |
| 14 |
* |
| 15 |
* @author keywordrush.com <support@keywordrush.com> |
| 16 |
* @link https://www.keywordrush.com |
| 17 |
* @copyright Copyright © 2026 keywordrush.com |
| 18 |
*/ |
| 19 |
class ModuleUpdateScheduler extends Scheduler |
| 20 |
{ |
| 21 |
const CRON_TAG = 'cegg_module_updater_cron'; |
| 22 |
const BYKEYWORD_UPDATE_LIMIT_FOR_MODULE = 100; |
| 23 |
const ITEMS_UPDATE_LIMIT_FOR_MODULE = 100; |
| 24 |
|
| 25 |
public static function getCronTag() |
| 26 |
{ |
| 27 |
return self::CRON_TAG; |
| 28 |
} |
| 29 |
|
| 30 |
public static function initAction() |
| 31 |
{ |
| 32 |
self::initSchedule(); |
| 33 |
parent::initAction(); |
| 34 |
} |
| 35 |
|
| 36 |
public static function run() |
| 37 |
{ |
| 38 |
@set_time_limit(600); |
| 39 |
|
| 40 |
// 1. By keyword update |
| 41 |
$max_exec_time1 = 270; |
| 42 |
$exec_time1 = self::byKeywordUpdate($max_exec_time1); |
| 43 |
|
| 44 |
// 2. Price update |
| 45 |
$max_exec_time2 = 300 + (300 - $exec_time1) - 30; |
| 46 |
|
| 47 |
self::priceUpdate($max_exec_time2); |
| 48 |
} |
| 49 |
|
| 50 |
public static function byKeywordUpdate($max_execution_time = 300) |
| 51 |
{ |
| 52 |
global $wpdb; |
| 53 |
|
| 54 |
$stopwatch = new Stopwatch(); |
| 55 |
$stopwatch->start(); |
| 56 |
|
| 57 |
$module_ids = ModuleManager::getInstance()->getByKeywordUpdateModuleIds(); |
| 58 |
if (!$module_ids) |
| 59 |
return 0; |
| 60 |
|
| 61 |
$time = time(); |
| 62 |
|
| 63 |
shuffle($module_ids); |
| 64 |
foreach ($module_ids as $module_id) |
| 65 |
{ |
| 66 |
$post_ids = \apply_filters('cegg_keyword_update_posts', array(), $module_id); |
| 67 |
|
| 68 |
if (!$post_ids) |
| 69 |
{ |
| 70 |
$module = ModuleManager::getInstance()->factory($module_id); |
| 71 |
$ttl = $module->config('ttl'); |
| 72 |
$meta_key_keyword = self::addKeywordPrefix($module_id); |
| 73 |
$meta_key_keyword_global = '_cegg_global_autoupdate_keyword'; |
| 74 |
$meta_key_last_bykeyword_update = self::addByKeywordUpdatePrefix($module_id); |
| 75 |
|
| 76 |
$limit = (int) \apply_filters('cegg_update_limit_keyword', self::BYKEYWORD_UPDATE_LIMIT_FOR_MODULE); |
| 77 |
|
| 78 |
$sql = "SELECT last_bykeyword_update.post_id |
| 79 |
FROM {$wpdb->postmeta} last_bykeyword_update |
| 80 |
INNER JOIN {$wpdb->postmeta} keyword |
| 81 |
ON last_bykeyword_update.post_id = keyword.post_id |
| 82 |
AND (keyword.meta_key = %s OR keyword.meta_key = %s) |
| 83 |
WHERE |
| 84 |
{$time} - last_bykeyword_update.meta_value > {$ttl} |
| 85 |
AND last_bykeyword_update.meta_key = %s |
| 86 |
ORDER BY last_bykeyword_update.meta_value ASC |
| 87 |
LIMIT " . $limit; |
| 88 |
|
| 89 |
$query = $wpdb->prepare($sql, $meta_key_keyword, $meta_key_keyword_global, $meta_key_last_bykeyword_update); |
| 90 |
|
| 91 |
$post_ids = $wpdb->get_col($query); |
| 92 |
} |
| 93 |
|
| 94 |
if (!$post_ids) |
| 95 |
continue; |
| 96 |
|
| 97 |
foreach ($post_ids as $post_id) |
| 98 |
{ |
| 99 |
ContentManager::updateByKeyword($post_id, $module_id); |
| 100 |
|
| 101 |
if ($stopwatch->elapsed() >= $max_execution_time) |
| 102 |
return $stopwatch->elapsed(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $stopwatch->elapsed(); |
| 107 |
} |
| 108 |
|
| 109 |
public static function priceUpdate($max_execution_time = 300) |
| 110 |
{ |
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
$stopwatch = new Stopwatch(); |
| 114 |
$stopwatch->start(); |
| 115 |
|
| 116 |
$module_ids = ModuleManager::getInstance()->getItemsUpdateModuleIds(); |
| 117 |
if (!$module_ids) |
| 118 |
return; |
| 119 |
|
| 120 |
$time = time(); |
| 121 |
shuffle($module_ids); |
| 122 |
|
| 123 |
$limit = (int) \apply_filters('cegg_update_module_limit', self::ITEMS_UPDATE_LIMIT_FOR_MODULE); |
| 124 |
|
| 125 |
foreach ($module_ids as $module_id) |
| 126 |
{ |
| 127 |
$module = ModuleManager::getInstance()->factory($module_id); |
| 128 |
$ttl_items = $module->config('ttl_items'); |
| 129 |
$meta_key_last_update = self::addLastItemsUpdatePrefix($module_id); |
| 130 |
|
| 131 |
$sql = "SELECT last_update.post_id |
| 132 |
FROM {$wpdb->postmeta} last_update |
| 133 |
WHERE |
| 134 |
{$time} - last_update.meta_value > {$ttl_items} |
| 135 |
AND last_update.meta_key = %s |
| 136 |
ORDER BY last_update.meta_value ASC |
| 137 |
LIMIT " . $limit; |
| 138 |
|
| 139 |
$query = $wpdb->prepare($sql, $meta_key_last_update); |
| 140 |
$results = $wpdb->get_results($query); |
| 141 |
if (!$results) |
| 142 |
continue; |
| 143 |
|
| 144 |
foreach ($results as $r) |
| 145 |
{ |
| 146 |
ContentManager::updateItems($r->post_id, $module_id); |
| 147 |
|
| 148 |
if ($stopwatch->elapsed() >= $max_execution_time) |
| 149 |
return; |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public static function addByKeywordUpdatePrefix($module_id) |
| 155 |
{ |
| 156 |
return ContentManager::META_PREFIX_LAST_BYKEYWORD_UPDATE . $module_id; |
| 157 |
} |
| 158 |
|
| 159 |
public static function addKeywordPrefix($module_id) |
| 160 |
{ |
| 161 |
return ContentManager::META_PREFIX_KEYWORD . $module_id; |
| 162 |
} |
| 163 |
|
| 164 |
public static function addLastItemsUpdatePrefix($module_id) |
| 165 |
{ |
| 166 |
return ContentManager::META_PREFIX_LAST_ITEMS_UPDATE . $module_id; |
| 167 |
} |
| 168 |
|
| 169 |
public static function initSchedule() |
| 170 |
{ |
| 171 |
\add_filter('cron_schedules', array(__CLASS__, 'addSchedule')); |
| 172 |
} |
| 173 |
|
| 174 |
public static function addSchedule($schedules) |
| 175 |
{ |
| 176 |
$schedules['ten_min'] = array( |
| 177 |
'interval' => 60 * 10, |
| 178 |
'display' => __('Every 10 minutes', 'content-egg'), |
| 179 |
); |
| 180 |
return $schedules; |
| 181 |
} |
| 182 |
} |
| 183 |
|