PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.0
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 All 177 releases
simple-tags / inc / class.client.schedule.php

class.client.schedule.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.44.0, at inc/class.client.schedule.php

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