PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.52.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.52.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.52.0, at inc/ajax-request.php

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