| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared -- Legacy TaxoPress file: keep behavior unchanged while documenting existing PHPCS exceptions. |
| 4 |
|
| 5 |
class SimpleTags_Client_Schedule |
| 6 |
{ |
| 7 |
public static $instance; |
| 8 |
|
| 9 |
public function __construct() |
| 10 |
{ |
| 11 |
|
| 12 |
add_action('taxopress_cron_autoterms_weekly', [$this, 'taxopress_cron_autoterms_weekly_execution']); |
| 13 |
add_filter('cron_schedules', [$this, 'taxopress_weekly_cron_schedule']); |
| 14 |
add_action('init', [$this, 'schedule_taxopress_cron_events']); |
| 15 |
} |
| 16 |
|
| 17 |
public static function get_instance() |
| 18 |
{ |
| 19 |
if (!isset(self::$instance)) { |
| 20 |
self::$instance = new self(); |
| 21 |
} |
| 22 |
|
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
public function schedule_taxopress_cron_events() |
| 27 |
{ |
| 28 |
|
| 29 |
$autoterms_schedule = taxopress_get_autoterms_schedule_data(); |
| 30 |
$cron_schedule = isset($autoterms_schedule['cron_schedule']) ? $autoterms_schedule['cron_schedule'] : 'disable'; |
| 31 |
|
| 32 |
if ($cron_schedule === 'weekly' && !wp_next_scheduled('taxopress_cron_autoterms_weekly')) { |
| 33 |
wp_schedule_event(time(), 'weekly', 'taxopress_cron_autoterms_weekly'); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function taxopress_weekly_cron_schedule($schedules) |
| 38 |
{ |
| 39 |
if (!isset($schedules['weekly'])) { |
| 40 |
$schedules['weekly'] = array( |
| 41 |
'interval' => 604800, |
| 42 |
'display' => esc_html__('Once Weekly', 'simple-tags') |
| 43 |
); |
| 44 |
} |
| 45 |
return $schedules; |
| 46 |
} |
| 47 |
|
| 48 |
public function taxopress_cron_autoterms_weekly_execution() |
| 49 |
{ |
| 50 |
$this->taxopress_cron_autoterms_execution('weekly'); |
| 51 |
} |
| 52 |
|
| 53 |
public function taxopress_cron_autoterms_execution($frequency = 'weekly') |
| 54 |
{ |
| 55 |
global $wpdb; |
| 56 |
|
| 57 |
$autoterms_schedule = taxopress_get_autoterms_schedule_data(); |
| 58 |
$autoterms = taxopress_get_autoterm_data(); |
| 59 |
$autoterm_schedule_ids = isset($autoterms_schedule['autoterm_id']) ? (array)$autoterms_schedule['autoterm_id'] : []; |
| 60 |
$autoterm_data = []; |
| 61 |
|
| 62 |
foreach ($autoterm_schedule_ids as $autoterm_schedule_id) { |
| 63 |
if (!empty($autoterms[$autoterm_schedule_id])) { |
| 64 |
$autoterm_data[$autoterm_schedule_id] = $autoterms[$autoterm_schedule_id]; |
| 65 |
} |
| 66 |
} |
| 67 |
if (empty($autoterm_data)) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$autoterms_to_run = []; |
| 72 |
foreach ($autoterm_data as $id => $a) { |
| 73 |
if (isset($a['cron_schedule'])) { |
| 74 |
if ($a['cron_schedule'] === $frequency) { |
| 75 |
$autoterms_to_run[$id] = $a; |
| 76 |
} |
| 77 |
continue; |
| 78 |
} |
| 79 |
$global_cron = isset($autoterms_schedule['cron_schedule']) ? $autoterms_schedule['cron_schedule'] : 'disable'; |
| 80 |
if ($global_cron === $frequency) { |
| 81 |
$autoterms_to_run[$id] = $a; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (empty($autoterms_to_run)) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$autoterm_schedule_exclude = isset($autoterms_schedule['autoterm_schedule_exclude']) ? (int)$autoterms_schedule['autoterm_schedule_exclude'] : 0; |
| 90 |
$limit = (isset($autoterms_schedule['schedule_terms_batches']) && (int)$autoterms_schedule['schedule_terms_batches'] > 0) ? (int)$autoterms_schedule['schedule_terms_batches'] : 20; |
| 91 |
$sleep = (isset($autoterms_schedule['schedule_terms_sleep']) && (int)$autoterms_schedule['schedule_terms_sleep'] > 0) ? (int)$autoterms_schedule['schedule_terms_sleep'] : 0; |
| 92 |
$schedule_terms_limit_days = !empty($autoterms_schedule['schedule_terms_limit_days']) ? (int)$autoterms_schedule['schedule_terms_limit_days'] : 0; |
| 93 |
|
| 94 |
$post_types = []; |
| 95 |
$post_status = []; |
| 96 |
foreach ($autoterms_to_run as $a) { |
| 97 |
if (!empty($a['post_types']) && is_array($a['post_types'])) { |
| 98 |
$post_types = array_unique(array_merge($post_types, $a['post_types'])); |
| 99 |
} |
| 100 |
if (!empty($a['post_status']) && is_array($a['post_status'])) { |
| 101 |
$post_status = array_unique(array_merge($post_status, $a['post_status'])); |
| 102 |
} |
| 103 |
} |
| 104 |
if (empty($post_types)) { |
| 105 |
return; |
| 106 |
} |
| 107 |
if (empty($post_status)) { |
| 108 |
$post_status = ['publish']; |
| 109 |
} |
| 110 |
|
| 111 |
$schedule_terms_limit_days_sql = ''; |
| 112 |
if ($schedule_terms_limit_days > 0) { |
| 113 |
$schedule_terms_limit_days_sql = 'AND post_date > "' . gmdate('Y-m-d H:i:s', time() - $schedule_terms_limit_days * 86400) . '"'; |
| 114 |
} |
| 115 |
|
| 116 |
// Use a per-frequency cursor so the schedule walks all eligible posts over time |
| 117 |
$cursor_option_name = 'taxopress_autoterms_schedule_last_id_' . $frequency; |
| 118 |
$last_id = (int) get_option($cursor_option_name, 0); |
| 119 |
|
| 120 |
$post_types = array_map('sanitize_key', (array) $post_types); |
| 121 |
$post_status = array_map('sanitize_key', (array) $post_status); |
| 122 |
$post_types_placeholders = implode(',', array_fill(0, count($post_types), '%s')); |
| 123 |
$post_status_placeholders = implode(',', array_fill(0, count($post_status), '%s')); |
| 124 |
|
| 125 |
if ($autoterm_schedule_exclude > 0) { |
| 126 |
$sql = "SELECT ID, post_title, post_content FROM {$wpdb->posts} "; |
| 127 |
$sql .= "LEFT JOIN {$wpdb->postmeta} ON ( ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_taxopress_autotermed' ) "; |
| 128 |
$sql .= "WHERE post_type IN ({$post_types_placeholders}) "; |
| 129 |
$sql .= "AND {$wpdb->postmeta}.post_id IS NULL "; |
| 130 |
$sql .= "AND post_status IN ({$post_status_placeholders}) "; |
| 131 |
if (!empty($schedule_terms_limit_days_sql)) { |
| 132 |
$sql .= " {$schedule_terms_limit_days_sql} "; |
| 133 |
} |
| 134 |
if ($last_id > 0) { |
| 135 |
$sql .= " AND ID < %d"; |
| 136 |
} |
| 137 |
$sql .= " ORDER BY ID DESC LIMIT %d"; |
| 138 |
|
| 139 |
$prepare_args = array_merge($post_types, $post_status); |
| 140 |
if ($last_id > 0) { |
| 141 |
$prepare_args[] = $last_id; |
| 142 |
} |
| 143 |
$prepare_args[] = $limit; |
| 144 |
|
| 145 |
$objects = (array) $wpdb->get_results($wpdb->prepare($sql, $prepare_args)); |
| 146 |
} else { |
| 147 |
$sql = "SELECT ID, post_title, post_content FROM {$wpdb->posts} "; |
| 148 |
$sql .= "WHERE post_type IN ({$post_types_placeholders}) "; |
| 149 |
$sql .= "AND post_status IN ({$post_status_placeholders}) "; |
| 150 |
if (!empty($schedule_terms_limit_days_sql)) { |
| 151 |
$sql .= " {$schedule_terms_limit_days_sql} "; |
| 152 |
} |
| 153 |
if ($last_id > 0) { |
| 154 |
$sql .= " AND ID < %d"; |
| 155 |
} |
| 156 |
$sql .= " ORDER BY ID DESC LIMIT %d"; |
| 157 |
|
| 158 |
$prepare_args = array_merge($post_types, $post_status); |
| 159 |
if ($last_id > 0) { |
| 160 |
$prepare_args[] = $last_id; |
| 161 |
} |
| 162 |
$prepare_args[] = $limit; |
| 163 |
|
| 164 |
$objects = (array) $wpdb->get_results($wpdb->prepare($sql, $prepare_args)); |
| 165 |
} |
| 166 |
|
| 167 |
if (empty($objects)) { |
| 168 |
delete_option($cursor_option_name); |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
$current_post = 0; |
| 173 |
$min_id_in_batch = null; |
| 174 |
foreach ($objects as $object) { |
| 175 |
$current_post++; |
| 176 |
update_post_meta($object->ID, '_taxopress_autotermed', 1); |
| 177 |
$post_object = get_post($object->ID); |
| 178 |
if ($min_id_in_batch === null || (int) $object->ID < $min_id_in_batch) { |
| 179 |
$min_id_in_batch = (int) $object->ID; |
| 180 |
} |
| 181 |
|
| 182 |
foreach ($autoterms_to_run as $autoterm) { |
| 183 |
if (empty($autoterm['autoterm_for_schedule'])) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
$applied = $autoterm; |
| 188 |
if (isset($autoterm['schedule_terms_limit'])) { |
| 189 |
$applied['terms_limit'] = $autoterm['schedule_terms_limit']; |
| 190 |
} |
| 191 |
if (isset($autoterm['schedule_autoterm_target'])) { |
| 192 |
$applied['autoterm_target'] = $autoterm['schedule_autoterm_target']; |
| 193 |
} |
| 194 |
if (isset($autoterm['schedule_autoterm_word'])) { |
| 195 |
$applied['autoterm_word'] = $autoterm['schedule_autoterm_word']; |
| 196 |
} |
| 197 |
if (isset($autoterm['schedule_autoterm_hash'])) { |
| 198 |
$applied['autoterm_hash'] = $autoterm['schedule_autoterm_hash']; |
| 199 |
} |
| 200 |
if (isset($autoterm['schedule_replace_type'])) { |
| 201 |
$applied['replace_type'] = $autoterm['schedule_replace_type']; |
| 202 |
} |
| 203 |
|
| 204 |
SimpleTags_Client_Autoterms::auto_terms_post( |
| 205 |
$post_object, |
| 206 |
isset($applied['taxonomy']) ? $applied['taxonomy'] : '', |
| 207 |
$applied, |
| 208 |
true, |
| 209 |
$frequency . '_cron_schedule', |
| 210 |
'st_autoterms' |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
unset($object); |
| 215 |
|
| 216 |
if ($sleep > 0 && $current_post % $limit == 0) { |
| 217 |
sleep($sleep); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (!empty($min_id_in_batch)) { |
| 222 |
update_option($cursor_option_name, (int) $min_id_in_batch); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|