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 / schedule.php

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

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