PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.43.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.43.0
3.54.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 All 178 releases
simple-tags / inc / class.client.schedule.php

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

172 lines 7.0 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 if ($autoterm_schedule_exclude > 0) {
116 $objects = (array) $wpdb->get_results("SELECT ID, post_title, post_content FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON ( ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_taxopress_autotermed' ) WHERE post_type IN ('" . implode("', '", array_map('esc_sql', $post_types)) . "') AND {$wpdb->postmeta}.post_id IS NULL AND post_status IN ('" . implode("', '", array_map('esc_sql', $post_status)) . "') {$schedule_terms_limit_days_sql} ORDER BY ID DESC LIMIT {$limit}"
117 );
118 } else {
119 $objects = (array) $wpdb->get_results("SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE post_type IN ('" . implode("', '", array_map('esc_sql', $post_types)) . "') AND post_status IN ('" . implode("', '", array_map('esc_sql', $post_status)) . "') {$schedule_terms_limit_days_sql} ORDER BY ID DESC LIMIT {$limit}"
120 );
121 }
122
123 if (empty($objects)) {
124 return;
125 }
126
127 $current_post = 0;
128 foreach ($objects as $object) {
129 $current_post++;
130 update_post_meta($object->ID, '_taxopress_autotermed', 1);
131
132 foreach ($autoterms_to_run as $autoterm) {
133 if (empty($autoterm['autoterm_for_schedule'])) {
134 continue;
135 }
136
137 $applied = $autoterm;
138 if (isset($autoterm['schedule_terms_limit'])) {
139 $applied['terms_limit'] = $autoterm['schedule_terms_limit'];
140 }
141 if (isset($autoterm['schedule_autoterm_target'])) {
142 $applied['autoterm_target'] = $autoterm['schedule_autoterm_target'];
143 }
144 if (isset($autoterm['schedule_autoterm_word'])) {
145 $applied['autoterm_word'] = $autoterm['schedule_autoterm_word'];
146 }
147 if (isset($autoterm['schedule_autoterm_hash'])) {
148 $applied['autoterm_hash'] = $autoterm['schedule_autoterm_hash'];
149 }
150 if (isset($autoterm['schedule_replace_type'])) {
151 $applied['replace_type'] = $autoterm['schedule_replace_type'];
152 }
153
154 SimpleTags_Client_Autoterms::auto_terms_post(
155 $object,
156 isset($applied['taxonomy']) ? $applied['taxonomy'] : '',
157 $applied,
158 true,
159 $frequency . '_cron_schedule',
160 'st_autoterms'
161 );
162 }
163
164 unset($object);
165
166 if ($sleep > 0 && $current_post % $limit == 0) {
167 sleep($sleep);
168 }
169 }
170 }
171 }
172