PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.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 / ajax-request.php

ajax-request.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.51.0, at inc/ajax-request.php

293 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 //Taxopress auto terms => Auto terms all content ajax callback
4 add_action('wp_ajax_taxopress_autoterms_content_by_ajax', 'taxopress_autoterms_content_by_ajax');
5 function taxopress_autoterms_content_by_ajax()
6 {
7 global $wpdb, $added_post_term, $empty_term_messages;
8
9 $added_post_term = [];
10 $empty_term_messages = [];
11
12 // run a quick security check
13 check_ajax_referer('st-admin-js', 'security');
14
15 //instantiate response default value
16 $response['status'] = 'error';
17 $response['message'] = '';
18 $response['content'] = '';
19 $response['total'] = 0;
20 $response['next_start'] = 0;
21 $response['percentage'] = '';
22
23 $autoterms = taxopress_get_autoterm_data();
24 $auto_term_id = !empty($_POST['taxopress_autoterm_content']['autoterm_id']) ? (int)$_POST['taxopress_autoterm_content']['autoterm_id'] : 0;
25
26 $existing_terms_batches = !empty($_POST['taxopress_autoterm_content']['existing_terms_batches']) ? (int)$_POST['taxopress_autoterm_content']['existing_terms_batches'] : 0;
27 $existing_terms_sleep = !empty($_POST['taxopress_autoterm_content']['existing_terms_sleep']) ? (int)$_POST['taxopress_autoterm_content']['existing_terms_sleep'] : 0;
28 $limit_days = !empty($_POST['taxopress_autoterm_content']['limit_days']) ? (int)$_POST['taxopress_autoterm_content']['limit_days'] : 0;
29 $autoterm_existing_content_exclude = !empty($_POST['taxopress_autoterm_content']['autoterm_existing_content_exclude']) ? (int)$_POST['taxopress_autoterm_content']['autoterm_existing_content_exclude'] : 0;
30
31 $start_from = isset($_POST['start_from']) && (int)$_POST['start_from'] > 0 ? (int)$_POST['start_from'] : 0;
32 $offset_start_from = max(0, $start_from);
33
34 if (!current_user_can('simple_tags')) {
35 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Permission denied.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
36 wp_send_json($response);
37 }
38
39 if ($start_from === 0) {
40 delete_option('tmp_auto_terms_st');
41 }
42
43 $auto_term_settings = [
44 'autoterm_id' => $auto_term_id,
45 'existing_terms_batches' => $existing_terms_batches,
46 'existing_terms_sleep' => $existing_terms_sleep,
47 'limit_days' => $limit_days,
48 'autoterm_existing_content_exclude' => $autoterm_existing_content_exclude,
49 ];
50 update_option('taxopress_autoterms_content', $auto_term_settings);
51
52 if (empty($auto_term_id)) {
53 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Auto Term is required, kindly add an Auto Term from Auto Term menu.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
54 wp_send_json($response);
55 } elseif (empty($existing_terms_batches)) {
56 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Limit per batches is required.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
57 wp_send_json($response);
58 } elseif (empty($existing_terms_sleep)) {
59 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Batches wait time is required.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
60 wp_send_json($response);
61 }
62
63 if ($auto_term_id && array_key_exists($auto_term_id, $autoterms)) {
64 $autoterm_data = $autoterms[$auto_term_id];
65 $autoterm_data['existing_terms_batches'] = $existing_terms_batches;
66 $autoterm_data['existing_terms_sleep'] = $existing_terms_sleep;
67 $autoterm_data['limit_days'] = $limit_days;
68 $autoterm_data['autoterm_exclude'] = $autoterm_existing_content_exclude;
69 $autoterm_data['replace_type'] = isset($autoterm_data['existing_content_replace_type']) ? $autoterm_data['existing_content_replace_type'] : '';
70 $autoterm_data['terms_limit'] = !empty($autoterm_data['existing_content_terms_limit']) ? $autoterm_data['existing_content_terms_limit'] : '';
71 } else {
72 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('Auto term settings not found', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
73 wp_send_json($response);
74 }
75
76 if (empty($autoterm_data['post_types'])) {
77 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('The selected Auto Term setting is not enabled for any post type. Please select at least one post type in the Auto Term settings.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
78 wp_send_json($response);
79 }
80
81 if (empty($autoterm_data['autoterm_for_existing_content'])) {
82 $response['message'] = '<div class="taxopress-response-css red"><p>'. esc_html__('The selected Auto Term is not enabled for existing content. Please enable it in Auto Term settings.', 'simple-tags') .'</p><button type="button" class="notice-dismiss"></button></div>';
83 wp_send_json($response);
84 }
85
86 $limit = (isset($autoterm_data['existing_terms_batches']) && (int)$autoterm_data['existing_terms_batches'] > 0) ? (int)$autoterm_data['existing_terms_batches'] : 2;
87
88 $sleep = (isset($autoterm_data['existing_terms_sleep']) && (int)$autoterm_data['existing_terms_sleep'] > 0) ? (int)$autoterm_data['existing_terms_sleep'] : 0;
89
90 if ($sleep > 0 && $start_from > 0) {
91 sleep($sleep);
92 }
93
94 $limit_days = (int) $autoterm_data['limit_days'];
95 $limit_days_sql = '';
96 if ($limit_days > 0) {
97 $limit_days_sql = 'AND post_date > "' . date('Y-m-d H:i:s', time() - $limit_days * 86400) . '"';
98 }
99
100 $post_types = $autoterm_data['post_types'];
101 $post_status = isset($autoterm_data['post_status']) && is_array($autoterm_data['post_status']) ? $autoterm_data['post_status'] : ['publish'];
102
103 $total = isset($_POST['total']) ? (int)$_POST['total'] : 0;
104
105 $post_types = array_map('sanitize_key', (array) $post_types);
106 $post_status = array_map('sanitize_key', (array) $post_status);
107
108 $post_types_placeholders = implode(',', array_fill(0, count($post_types), '%s'));
109 $post_status_placeholders = implode(',', array_fill(0, count($post_status), '%s'));
110
111 if ($autoterm_existing_content_exclude > 0) {
112 $sql = "SELECT ID, post_title, post_content FROM {$wpdb->posts}
113 LEFT JOIN {$wpdb->postmeta} ON ( ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_taxopress_autotermed' )
114 WHERE post_type IN ({$post_types_placeholders})
115 AND {$wpdb->postmeta}.post_id IS NULL
116 AND post_status IN ({$post_status_placeholders})
117 {$limit_days_sql}
118 ORDER BY ID DESC
119 LIMIT %d OFFSET %d";
120
121 $prepare_args = array_merge($post_types, $post_status, [$limit, $offset_start_from]);
122 $objects = (array) $wpdb->get_results($wpdb->prepare($sql, $prepare_args));
123 } else {
124 $sql = "SELECT ID, post_title, post_content FROM {$wpdb->posts}
125 WHERE post_type IN ({$post_types_placeholders})
126 AND post_status IN ({$post_status_placeholders})
127 {$limit_days_sql}
128 ORDER BY ID DESC
129 LIMIT %d OFFSET %d";
130
131 $prepare_args = array_merge($post_types, $post_status, [$limit, $offset_start_from]);
132 $objects = (array) $wpdb->get_results($wpdb->prepare($sql, $prepare_args));
133 }
134
135 $response_content = '';
136 if (!empty($objects)) {
137 foreach ($objects as $object) {
138 update_post_meta($object->ID, '_taxopress_autotermed', 1);
139 $post_object = get_post($object->ID);
140 SimpleTags_Client_Autoterms::auto_terms_post($post_object, $autoterm_data['taxonomy'], $autoterm_data, true, 'existing_content', 'st_autoterms');
141 $log_messages = !empty($empty_term_messages[$object->ID]['message']) ? $empty_term_messages[$object->ID]['message'] : [];
142 $log_message_html = '';
143 if (!empty($log_messages)) {
144 $log_message_html .= '<div class="log-message-show-button"><a href="#" class="msg-show">' . sprintf(esc_html__('Show Log Messages (%1s)', 'simple-tags'), '<strong>' . count($log_messages) . '</strong>') . '</a></div>';
145 $log_message_html .= '<div class="autoterm-log-message" style="display: none;"><ul>';
146 $log_msg_lists = array_map(function ($item) {
147 return '<li class="log-message-list">' . $item . '</li>';
148 }, $log_messages);
149 $log_message_html .= join('', $log_msg_lists);
150 $log_message_html .= '</ul></div>';
151 $log_message_html .= '<div class="log-message-hide-button"><a href="#" class="msg-hide" style="display: none;">' . sprintf(esc_html__('Hide Log Messages (%1s)', 'simple-tags'), '<strong>' . count($log_messages) . '</strong>') . '</a></div>';
152 }
153 if (!empty($added_post_term[$object->ID])) {
154 $tag_lists = array_map(function ($item) {
155 return '<span class="taxopress-term"><span class="term-name">' . $item . '</span></span>';
156 }, $added_post_term[$object->ID]);
157 $added_terms_html = join('', $tag_lists) . $log_message_html;
158 } else {
159 $added_terms_html = esc_html__('No terms added.', 'simple-tags') . $log_message_html;
160 ;
161 }
162 $response_content .= '<li class="result-item">
163 <fieldset>
164 <legend>
165 <span class="result-title">
166 <a target="_blank" href="'. esc_url(get_edit_post_link($object->ID)) . '">' . esc_html($object->post_title) . '</a>
167 </span>
168 </legend>
169 <div class="result-content">'. $added_terms_html .'</div>
170 </fieldset></li>';
171 unset($object);
172 }
173
174 $response['status'] = 'progress';
175 $response['content'] = $response_content;
176 $response['done'] = ($start_from + count($objects));
177 $percentage = 100;
178 $response['notice'] = '<div class="taxopress-response-css yellow"><p>'. sprintf(esc_html__('Please leave this screen running to continue the scan. To stop the scan, close this screen or click here: %1sStop%2s | %3sPause%4s', 'simple-tags'), '<a href="#" class="terminate-autoterm-scan">', '</a>', '<a href="#" class="pause-autoterm-scan" data-pause="0" data-pause-text="'. esc_html__('Pause', 'simple-tags') .'" data-resume-text="'. esc_html__('Resume', 'simple-tags') .'">', '</a>') .'</p></div>';
179 $progress_message = '<div class="taxopress-response-css yellow"><p>'. sprintf(esc_html__('Progress Report: %s posts checked.', 'simple-tags'), '<strong>' . ($start_from + count($objects)) . '</strong>') .'</p></div>';
180
181 } else {
182
183 $counter = (int)get_option('tmp_auto_terms_st');
184 delete_option('tmp_auto_terms_st');
185 $percentage = 100;
186 $response['status'] = 'sucess';
187 $response['done'] = $total;
188 $progress_message = '<div class="taxopress-response-css green"><p>'. sprintf(esc_html__('Completed: %s terms added from %s posts checked.', 'simple-tags'), $counter, ($start_from + count($objects))) .'</p><button type="button" class="notice-dismiss"></button></div>';
189 $response['message'] = $progress_message;
190 }
191 $response['percentage'] = $progress_message;
192
193 wp_send_json($response);
194
195 }
196
197
198 //Taxopress search post call back
199 add_action('wp_ajax_taxopress_post_search', 'taxopress_post_search_callback');
200
201 function taxopress_post_search_callback()
202 {
203 header('Content-Type: application/javascript');
204
205 if (
206 empty($_GET['nonce'])
207 || !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-post-search')
208 ) {
209 wp_send_json_error(null, 403);
210 }
211
212 if (!current_user_can('simple_tags')) {
213 wp_send_json_error(null, 403);
214 }
215
216 $search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
217 $post_type = !empty($_GET['post_types']) ? array_map('sanitize_text_field', $_GET['post_types']) : 'any';
218
219 $post_args = [
220 'post_type' => $post_type,
221 'post_status' => 'publish',
222 'posts_per_page' => apply_filters('taxopress_filter_posts_search_result_limit', 20),
223 ];
224
225 if (!empty($search)) {
226 $post_args['s'] = $search;
227 }
228
229 $posts = get_posts($post_args);
230 $results = [];
231
232 foreach ($posts as $post) {
233 $results[] = [
234 'id' => $post->ID,
235 'text' => $post->post_title,
236 ];
237 }
238
239 $response = [
240 'results' => $results,
241 ];
242 echo wp_json_encode($response);
243 exit;
244 }
245
246
247 //Taxopress search field call back
248 add_action('wp_ajax_taxopress_custom_fields_search', 'taxopress_custom_fields_search_callback');
249
250 function taxopress_custom_fields_search_callback()
251 {
252 global $wpdb;
253
254 header('Content-Type: application/javascript');
255
256 if (
257 empty($_GET['nonce'])
258 || !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-custom-fields-search')
259 ) {
260 wp_send_json_error(null, 403);
261 }
262
263 if (!current_user_can('simple_tags')) {
264 wp_send_json_error(null, 403);
265 }
266
267 $search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
268
269 $whereClause = '';
270 if (!empty($search)) {
271 $like = '%' . $wpdb->esc_like($search) . '%';
272 $whereClause = $wpdb->prepare("AND meta_key LIKE %s", $like);
273 }
274
275 $queryResults = $wpdb->get_col("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE 1=1 $whereClause ORDER BY meta_key ASC LIMIT 20");
276
277 $results = [];
278 if (!empty($queryResults)) {
279 foreach ($queryResults as $queryResult) {
280 $results[] = [
281 'id' => $queryResult,
282 'text' => $queryResult,
283 ];
284 }
285 }
286
287 $response = [
288 'results' => $results,
289 ];
290 echo wp_json_encode($response);
291 exit;
292 }
293