PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / trunk
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms vtrunk
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 / schedule.php

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

513 lines 32.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!class_exists('SimpleTags_Autoterms_Schedule')) {
4 class SimpleTags_Autoterms_Schedule
5 {
6 public const MENU_SLUG = 'st_options';
7
8 public static $instance;
9
10 public $logs_table;
11
12 public function __construct()
13 {
14
15 add_action('admin_menu', [$this, 'admin_menu']);
16 add_action('admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts'], 11);
17 }
18
19 public static function get_instance()
20 {
21 if (!isset(self::$instance)) {
22 self::$instance = new self();
23 }
24
25 return self::$instance;
26 }
27
28 public static function admin_enqueue_scripts()
29 {
30 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter to check page, no state change
31 if (isset($_GET['page']) && $_GET['page'] == 'st_autoterms_schedule') {
32 wp_enqueue_style('st-taxonomies-css');
33 }
34 }
35
36 public function admin_menu()
37 {
38 $hook = add_submenu_page(
39 self::MENU_SLUG,
40 esc_html__('Auto Terms Schedule', 'simple-tags'),
41 esc_html__('Auto Terms Schedule', 'simple-tags'),
42 'simple_tags',
43 'st_autoterms_schedule',
44 [
45 $this,
46 'page_manage_autoterms_schedule',
47 ]
48 );
49 add_action("load-$hook", [$this, 'save_autoterms_schedule_settings']);
50 add_action("load-$hook", [$this, 'screen_option']);
51 }
52
53 public function screen_option()
54 {
55 $option = 'per_page';
56
57 $args = [
58 'label' => esc_html__('Number of items per page', 'simple-tags'),
59 'default' => 20,
60 'option' => 'st_autoterms_schedule_logs_per_page'
61 ];
62 $this->logs_table = new SimpleTags_Schedule_Logs();
63
64 add_screen_option($option, $args);
65 }
66
67 public function save_autoterms_schedule_settings()
68 {
69
70 if (
71 !empty($_POST['taxopress_autoterm_schedule_test_run'])
72 && !empty($_POST['_nonce'])
73 && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_nonce'])), 'taxopress_autoterm_schedule_nonce')
74 && current_user_can('simple_tags')
75 ) {
76 $autoterms_schedule = taxopress_get_autoterms_schedule_data();
77 $cron_schedule = isset($autoterms_schedule['cron_schedule']) ? $autoterms_schedule['cron_schedule'] : 'disable';
78
79 if ($cron_schedule === 'disable') {
80 add_action('admin_notices', function () {
81 echo wp_kses_post(
82 taxopress_admin_notices_helper(
83 esc_html__('Schedule is disabled. Please enable a schedule frequency to run a test.', 'simple-tags'),
84 false
85 )
86 );
87 });
88 return;
89 }
90
91 $hook_name = 'taxopress_cron_autoterms_' . $cron_schedule;
92 wp_schedule_single_event(time(), $hook_name);
93 spawn_cron();
94
95 add_action('admin_notices', function () use ($hook_name) {
96 echo wp_kses_post(
97 taxopress_admin_notices_helper(
98 sprintf(
99 esc_html__('Scheduled the cron event %s to run now. The original event will not be affected.', 'simple-tags'),
100 '<code>' . esc_html($hook_name) . '</code>'
101 ),
102 true
103 )
104 );
105 });
106 return;
107 }
108
109 if (
110 !empty($_POST['taxopress_autoterm_schedule_submit'])
111 && !empty($_POST['_nonce'])
112 && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_nonce'])), 'taxopress_autoterm_schedule_nonce')
113 && current_user_can('simple_tags')
114 ) {
115 $auto_term_ids = !empty($_POST['taxopress_autoterm_schedule']['autoterm_id']) ? array_map('intval', (array)$_POST['taxopress_autoterm_schedule']['autoterm_id']) : [];
116
117 $cron_schedule = !empty($_POST['taxopress_autoterm_schedule']['cron_schedule']) ? sanitize_text_field(wp_unslash($_POST['taxopress_autoterm_schedule']['cron_schedule'])) : 'disable';
118 $schedule_terms_batches = !empty($_POST['taxopress_autoterm_schedule']['schedule_terms_batches']) ? (int)$_POST['taxopress_autoterm_schedule']['schedule_terms_batches'] : '';
119 $schedule_terms_sleep = !empty($_POST['taxopress_autoterm_schedule']['schedule_terms_sleep']) ? (int)$_POST['taxopress_autoterm_schedule']['schedule_terms_sleep'] : '';
120 $schedule_terms_limit_days = !empty($_POST['taxopress_autoterm_schedule']['schedule_terms_limit_days']) ? sanitize_text_field(wp_unslash($_POST['taxopress_autoterm_schedule']['schedule_terms_limit_days'])) : '';
121 $autoterm_schedule_exclude = !empty($_POST['taxopress_autoterm_schedule']['autoterm_schedule_exclude']) ? (int)$_POST['taxopress_autoterm_schedule']['autoterm_schedule_exclude'] : '';
122
123 $response_message = esc_html__('An error occured.', 'simple-tags');
124 $response_sucess = false;
125 if (empty($schedule_terms_batches)) {
126 $response_message = esc_html__('Limit per batches is required.', 'simple-tags');
127 } elseif (empty($schedule_terms_sleep)) {
128 $response_message = esc_html__('Batches wait time is required.', 'simple-tags');
129 } else {
130 $auto_term_schedule_settings = [
131 'autoterm_id' => $auto_term_ids,
132 'cron_schedule' => $cron_schedule,
133 'schedule_terms_batches' => $schedule_terms_batches,
134 'schedule_terms_sleep' => $schedule_terms_sleep,
135 'schedule_terms_limit_days' => $schedule_terms_limit_days,
136 'autoterm_schedule_exclude' => $autoterm_schedule_exclude,
137 ];
138 update_option('taxopress_autoterms_schedule', $auto_term_schedule_settings);
139
140 wp_clear_scheduled_hook('taxopress_cron_autoterms_weekly');
141 do_action('taxopress_clear_schedule_cron_hooks');
142
143 if ($cron_schedule == 'weekly') {
144 wp_schedule_event(time(), 'weekly', 'taxopress_cron_autoterms_weekly');
145 }
146
147 do_action('taxopress_schedule_cron_events', $cron_schedule);
148
149 $autoterm_data = taxopress_get_autoterm_data();
150 $autoterm_data_selected = [];
151
152 if (!empty($auto_term_ids)) {
153 foreach ($auto_term_ids as $term_id) {
154 if (isset($autoterm_data[$term_id])) {
155 $autoterm_data_selected[$term_id] = $autoterm_data[$term_id];
156 }
157 }
158 }
159 $autoterm_data = $autoterm_data_selected;
160
161 $schedule_enabled = false;
162 foreach ($autoterm_data_selected as $term_data) {
163 if (!empty($term_data['autoterm_for_schedule'])) {
164 $schedule_enabled = true;
165 break;
166 }
167 }
168
169 if (!$schedule_enabled) {
170 $response_message = esc_html__('Schedule is not enabled for selected Auto Terms.', 'simple-tags');
171 $response_sucess = false;
172 } else {
173 $response_message = esc_html__('Settings updated successfully.', 'simple-tags');
174 $response_sucess = true;
175 }
176 }
177
178 add_action('admin_notices', function () use ($response_message, $response_sucess) {
179 echo wp_kses_post(taxopress_admin_notices_helper($response_message, $response_sucess));
180 });
181 }
182 }
183
184 public function page_manage_autoterms_schedule()
185 {
186 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter to set default sort, no state change
187 if (!isset($_GET['order'])) {
188 $_GET['order'] = 'name-asc';
189 }
190
191 settings_errors(__CLASS__);
192 ?>
193 <?php
194
195 $ui = new taxopress_admin_ui();
196
197 $autoterms_schedule = taxopress_get_autoterms_schedule_data();
198
199 ?>
200 <div class="wrap taxopress-split-wrap taxopress-autoterm-schedule">
201 <h1><?php echo esc_html__('Auto Terms Schedule', 'simple-tags'); ?> </h1>
202 <div class="taxopress-description">
203 <?php esc_html_e('This feature allows you to run the Auto Terms feature on a schedule. This is helpful if you regularly import content into WordPress. TaxoPress can run on a schedule and add terms to your imported content.', 'simple-tags'); ?>
204 </div>
205 <div class="wp-clearfix"></div>
206 <form method="post" id="auto_term_schedule_form" action="">
207 <div id="poststuff">
208 <div id="post-body" class="taxopress-section metabox-holder columns-2">
209 <div class="tp-flex-item">
210 <div id="post-body-content" class="right-body-content" style="position: relative;">
211 <div class="postbox-header">
212 <h2 class="hndle ui-sortable-handle">
213 <?php echo esc_html__('Settings', 'simple-tags'); ?>
214 </h2>
215 </div>
216 <div class="main">
217 <table class="form-table taxopress-table autoterm_schedule">
218 <?php
219 $autoterm_data = taxopress_get_autoterm_data();
220 $selected_autoterm = !empty($autoterms_schedule['autoterm_id']) ? array_map('intval', (array)$autoterms_schedule['autoterm_id']) : [];
221 if (empty($autoterm_data)) :
222 $auto_term_opionts = [
223 [
224 'attr' => '',
225 'text' => __('Select an option...', 'simple-tags')
226 ]
227 ];
228 else :
229 $auto_term_opionts = [];
230 foreach ($autoterm_data as $autoterm_settings) {
231 $current_option = [];
232 $current_option['attr'] = $autoterm_settings['ID'];
233 $current_option['text'] = $autoterm_settings['title'];
234 if (in_array($autoterm_settings['ID'], $selected_autoterm)) {
235 $current_option['default'] = 'true';
236 }
237 $auto_term_opionts[] = $current_option;
238 }
239 endif;
240 $select = [];
241 $select['options'] = $auto_term_opionts;
242 $select['selected'] = '';
243
244 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
245 echo $ui->get_select_checkbox_input_main([
246 'namearray' => 'taxopress_autoterm_schedule',
247 'name' => 'autoterm_id',
248 'class' => 'taxopress-multi-select2',
249 'labeltext' => esc_html__(
250 'Auto Terms setting',
251 'simple-tags'
252 ),
253 'aftertext' => esc_html__('Select Auto Terms settings to use when running the "Schedule" feature.', 'simple-tags') . ' ',
254 'selections' => $select, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
255 'multiple' => true,
256 ]);
257
258 $cron_options = [
259 'disable' => __('Do not run on a schedule', 'simple-tags'),
260 'weekly' => __('Weekly', 'simple-tags'),
261 ];
262 ?>
263 <tr valign="top">
264 <th scope="row"><label><?php echo esc_html__('Schedule Frequency', 'simple-tags'); ?></label></th>
265
266 <td>
267 <?php
268 $cron_schedule = (!empty($autoterms_schedule['cron_schedule'])) ? $autoterms_schedule['cron_schedule'] : 'disable';
269
270 ?>
271 <input type="hidden"
272 id="cron_schedule_value"
273 name="taxopress_autoterm_schedule[cron_schedule]"
274 value="<?php echo esc_attr($cron_schedule); ?>" />
275
276 <?php
277 $disable_checked = $cron_schedule === 'disable' ? 'checked' : '';
278 ?>
279 <label class="autoterm_cron_disable_label">
280 <input
281 type="checkbox"
282 id="autoterm_cron_disable"
283 class="autoterm_cron_disable"
284 <?php echo esc_html($disable_checked); ?>
285 />
286 <?php echo esc_html($cron_options['disable']); ?>
287 </label>
288
289 <p class="description autoterm_cron_help <?php echo $cron_schedule === 'disable' ? '' : 'st-hide-content'; ?>">
290 <?php echo esc_html__("Disable 'Do not run on a schedule' to select schedule frequency.", 'simple-tags'); ?>
291 </p>
292
293 <div class="autoterm_cron_frequency <?php echo $cron_schedule === 'disable' ? 'st-hide-content' : ''; ?>">
294 <?php
295 unset($cron_options['disable']);
296 foreach ($cron_options as $option => $label) {
297 $checked_status = ($cron_schedule === $option) ? 'checked' : '';
298 ?>
299 <label>
300 <input
301 type="radio"
302 class="autoterm_cron_radio"
303 id="autoterm_cron_<?php echo esc_attr($option); ?>"
304 name="taxopress_autoterm_schedule[cron_schedule_choice]"
305 value="<?php echo esc_attr($option); ?>"
306 <?php echo esc_html($checked_status); ?>
307 />
308 <?php echo esc_html($label); ?>
309 </label>
310 <br /><br />
311 <?php } ?>
312 <?php do_action('taxopress_schedule_frequency_fields', $cron_schedule); ?>
313 </div>
314 </td>
315 </tr>
316 <?php
317
318 $select = [
319 'options' => [
320 [
321 'attr' => '0',
322 'text' => esc_attr__('False', 'simple-tags'),
323 'default' => 'true',
324 ],
325 [
326 'attr' => '1',
327 'text' => esc_attr__('True', 'simple-tags'),
328 ],
329 ],
330 ];
331 $selected = (isset($autoterms_schedule) && isset($autoterms_schedule['autoterm_schedule_exclude'])) ? taxopress_disp_boolean($autoterms_schedule['autoterm_schedule_exclude']) : '';
332 $select['selected'] = !empty($selected) ? $autoterms_schedule['autoterm_schedule_exclude'] : '';
333 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
334 echo $ui->get_select_checkbox_input([
335 'namearray' => 'taxopress_autoterm_schedule',
336 'name' => 'autoterm_schedule_exclude',
337 'class' => '',
338 'labeltext' => esc_html__('Exclude previously analyzed content', 'simple-tags'),
339 'aftertext' => esc_html__('This enables you to skip posts that have already been analyzed by the Schedule feature.', 'simple-tags'),
340 'selections' => $select, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
341 ]);
342 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
343 echo $ui->get_number_input([
344 'namearray' => 'taxopress_autoterm_schedule',
345 'name' => 'schedule_terms_batches',
346 'textvalue' => isset($autoterms_schedule['schedule_terms_batches']) ? esc_attr($autoterms_schedule['schedule_terms_batches']) : '20',
347 'labeltext' => esc_html__(
348 'Limit per batches',
349 'simple-tags'
350 ),
351 'helptext' => esc_html__('This enables your scheduled Auto Terms to run in batches. If you have a lot of content, set this to a lower number to avoid timeouts.', 'simple-tags'),
352 'min' => '1',
353 'required' => true,
354 ]);
355
356 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
357 echo $ui->get_number_input([
358 'namearray' => 'taxopress_autoterm_schedule',
359 'name' => 'schedule_terms_sleep',
360 'textvalue' => isset($autoterms_schedule['schedule_terms_sleep']) ? esc_attr($autoterms_schedule['schedule_terms_sleep']) : '10',
361 'labeltext' => esc_html__('Batches wait time', 'simple-tags'),
362 'helptext' => esc_html__('This is the wait time (in seconds) between processing batches of Auto Terms. If you have a lot of existing content, set this to a higher number to avoid timeouts.', 'simple-tags'),
363 'min' => '0',
364 'required' => true,
365 ]);
366
367 $select = [
368 'options' => [
369 [
370 'attr' => '1',
371 'text' => esc_attr__('24 hours ago', 'simple-tags')
372 ],
373 [
374 'attr' => '7',
375 'text' => esc_attr__('7 days ago', 'simple-tags')
376 ],
377 [
378 'attr' => '14',
379 'text' => esc_attr__('2 weeks ago', 'simple-tags')
380 ],
381 [
382 'attr' => '30',
383 'text' => esc_attr__('1 month ago', 'simple-tags'),
384 'default' => 'true'
385 ],
386 [
387 'attr' => '180',
388 'text' => esc_attr__('6 months ago', 'simple-tags')
389 ],
390 [
391 'attr' => '365',
392 'text' => esc_attr__('1 year ago', 'simple-tags')
393 ],
394 [
395 'attr' => '0',
396 'text' => esc_attr__('No limit', 'simple-tags')
397 ],
398 ],
399 ];
400
401 if (isset($autoterms_schedule) && is_array($autoterms_schedule)) {
402 $select = [
403 'options' => [
404 [
405 'attr' => '1',
406 'text' => esc_attr__('24 hours ago', 'simple-tags')
407 ],
408 [
409 'attr' => '7',
410 'text' => esc_attr__('7 days ago', 'simple-tags')
411 ],
412 [
413 'attr' => '14',
414 'text' => esc_attr__('2 weeks ago', 'simple-tags')
415 ],
416 [
417 'attr' => '30',
418 'text' => esc_attr__('1 month ago', 'simple-tags'),
419 ],
420 [
421 'attr' => '180',
422 'text' => esc_attr__('6 months ago', 'simple-tags')
423 ],
424 [
425 'attr' => '365',
426 'text' => esc_attr__('1 year ago', 'simple-tags')
427 ],
428 [
429 'attr' => '0',
430 'text' => esc_attr__('No limit', 'simple-tags'),
431 'default' => 'true'
432 ],
433 ],
434 ];
435 }
436
437 $selected = (isset($autoterms_schedule) && isset($autoterms_schedule['schedule_terms_limit_days'])) ? taxopress_disp_boolean($autoterms_schedule['schedule_terms_limit_days']) : '';
438 $select['selected'] = !empty($selected) ? $autoterms_schedule['schedule_terms_limit_days'] : '';
439 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
440 echo $ui->get_select_number_select([
441 'namearray' => 'taxopress_autoterm_schedule',
442 'name' => 'schedule_terms_limit_days',
443 'labeltext' => esc_html__(
444 'Limit Auto Terms, based on published date',
445 'simple-tags'
446 ),
447 'aftertext' => esc_html__('This setting can limit your scheduled Auto Terms query to only recent content. We recommend using this feature to avoid timeouts on large sites.', 'simple-tags'),
448 'selections' => $select, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
449 ]);
450
451 ?>
452 </table>
453 </div>
454 </div>
455 <div class="tp-submit-div">
456 <?php wp_nonce_field('taxopress_autoterm_schedule_nonce', '_nonce'); ?>
457 <input type="submit" class="button-primary taxopress-taxonomy-submit taxopress-autoterm-schedule-submit" name="taxopress_autoterm_schedule_submit" value="<?php echo esc_attr__('Save Settings', 'simple-tags'); ?>">
458 <input type="submit" class="button-secondary taxopress-taxonomy-submit taxopress-autoterm-schedule-test-run" name="taxopress_autoterm_schedule_test_run" value="<?php echo esc_attr__('Test Run', 'simple-tags'); ?>">
459 </div>
460 </div>
461
462 <div id="postbox-container-1" class="postbox-container tp-flex-item">
463 <div id="side-sortables" class="meta-box-sortables ui-sortable" style="">
464 <div id="submitdiv" class="postbox">
465 <div class="postbox-header">
466 <h2 class="hndle ui-sortable-handle post_terms_icon preview-title">
467 <?php esc_html_e('Recent Schedule Runs', 'simple-tags'); ?>
468 </h2>
469 </div>
470 <div class="inside">
471 <div id="minor-publishing">
472 <div class="sidebar-body-wrap">
473 <p class="description"><?php echo sprintf(esc_html__('You can see full log details on the %1s screen', 'simple-tags'), '<a target="_blank" href="' . esc_url(admin_url('admin.php?page=st_autoterms&tab=logs')) . '">' . esc_html__('Auto Terms Logs', 'simple-tags') . '</a>'); ?></p>
474 <?php
475 $this->logs_table->prepare_items();
476 ?>
477
478 <div id="col-container" class="wp-clearfix">
479
480 <div class="col-wrap">
481 <form action="<?php echo esc_url(add_query_arg('', '')); ?>" method="post">
482 <?php $this->logs_table->display(); ?>
483 </form>
484 <div class="form-wrap edit-term-notes">
485 <p><?php esc_html__('Description here.', 'simple-tags') ?></p>
486 </div>
487 </div>
488 </div>
489 </div>
490 </div>
491 </div>
492 </div>
493 </div>
494
495 </div>
496 <br class="clear">
497 </div>
498 </div>
499 </form>
500 </div>
501 <?php SimpleTags_Admin::printAdminFooter(); ?>
502 <?php
503 }
504
505 public function autoterms_logs_count()
506 {
507
508 $count = taxopress_autoterms_logs_data(1)['counts'];
509 return '(' . number_format_i18n($count) . ')';
510 }
511 }
512 }
513