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 / ajax-request.php

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

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