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

265 lines 13.8 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 if($autoterm_existing_content_exclude > 0){
105 $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("', '", $post_types) . "') AND {$wpdb->postmeta}.post_id IS NULL AND post_status IN ('" . implode("', '", $post_status) . "') {$limit_days_sql} ORDER BY ID DESC LIMIT {$limit} OFFSET {$offset_start_from}");
106 }else{
107 $objects = (array) $wpdb->get_results("SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE post_type IN ('" . implode("', '", $post_types) . "') AND post_status IN ('" . implode("', '", $post_status) . "') {$limit_days_sql} ORDER BY ID DESC LIMIT {$limit} OFFSET {$offset_start_from}");
108 }
109
110 $response_content = '';
111 if (!empty($objects)) {
112 foreach ($objects as $object) {
113 update_post_meta($object->ID, '_taxopress_autotermed', 1);
114 SimpleTags_Client_Autoterms::auto_terms_post( $object, $autoterm_data['taxonomy'], $autoterm_data, true, 'existing_content', 'st_autoterms' );
115 $log_messages = !empty($empty_term_messages[$object->ID]['message']) ? $empty_term_messages[$object->ID]['message'] : [];
116 $log_message_html = '';
117 if (!empty($log_messages)) {
118 $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>';
119 $log_message_html .= '<div class="autoterm-log-message" style="display: none;"><ul>';
120 $log_msg_lists = array_map(function($item) {
121 return '<li class="log-message-list">' . $item . '</li>';
122 }, $log_messages);
123 $log_message_html .= join('', $log_msg_lists);
124 $log_message_html .= '</ul></div>';
125 $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>';
126 }
127 if (!empty($added_post_term[$object->ID])) {
128 $tag_lists = array_map(function($item) {
129 return '<span class="taxopress-term"><span class="term-name">' . $item . '</span></span>';
130 }, $added_post_term[$object->ID]);
131 $added_terms_html = join('', $tag_lists) . $log_message_html;
132 } else {
133 $added_terms_html = esc_html__('No terms added.', 'simple-tags') . $log_message_html;;
134 }
135 $response_content .= '<li class="result-item">
136 <fieldset>
137 <legend>
138 <span class="result-title">
139 <a target="_blank" href="'. esc_url(get_edit_post_link($object->ID)) . '">' . $object->post_title . '</a>
140 </span>
141 </legend>
142 <div class="result-content">'. $added_terms_html .'</div>
143 </fieldset></li>';
144 unset($object);
145 }
146
147 $response['status'] = 'progress';
148 $response['content'] = $response_content;
149 $response['done'] = ($start_from + count($objects));
150 $percentage = 100;
151 $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>';
152 $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>';
153
154 } else {
155
156 $counter = (int)get_option('tmp_auto_terms_st');
157 delete_option('tmp_auto_terms_st');
158 $percentage = 100;
159 $response['status'] = 'sucess';
160 $response['done'] = $total;
161 $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>';
162 $response['message'] = $progress_message;
163 }
164 $response['percentage'] = $progress_message;
165
166 wp_send_json($response);
167
168 }
169
170
171 //Taxopress search post call back
172 add_action('wp_ajax_taxopress_post_search', 'taxopress_post_search_callback');
173
174 function taxopress_post_search_callback()
175 {
176 header('Content-Type: application/javascript');
177
178 if (
179 empty($_GET['nonce'])
180 || !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-post-search')
181 ) {
182 wp_send_json_error(null, 403);
183 }
184
185 if (!current_user_can('simple_tags')) {
186 wp_send_json_error(null, 403);
187 }
188
189 $search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
190 $post_type = !empty($_GET['post_types']) ? array_map('sanitize_text_field', $_GET['post_types']) : 'any';
191
192 $post_args = [
193 'post_type' => $post_type,
194 'post_status' => 'publish',
195 'posts_per_page' => apply_filters('taxopress_filter_posts_search_result_limit', 20),
196 ];
197
198 if (!empty($search)) {
199 $post_args['s'] = $search;
200 }
201
202 $posts = get_posts($post_args);
203 $results = [];
204
205 foreach ($posts as $post) {
206 $results[] = [
207 'id' => $post->ID,
208 'text' => $post->post_title,
209 ];
210 }
211
212 $response = [
213 'results' => $results,
214 ];
215 echo wp_json_encode($response);
216 exit;
217 }
218
219
220 //Taxopress search field call back
221 add_action('wp_ajax_taxopress_custom_fields_search', 'taxopress_custom_fields_search_callback');
222
223 function taxopress_custom_fields_search_callback()
224 {
225 global $wpdb;
226
227 header('Content-Type: application/javascript');
228
229 if (
230 empty($_GET['nonce'])
231 || !wp_verify_nonce(sanitize_key($_GET['nonce']), 'taxopress-custom-fields-search')
232 ) {
233 wp_send_json_error(null, 403);
234 }
235
236 if (!current_user_can('simple_tags')) {
237 wp_send_json_error(null, 403);
238 }
239
240 $search = !empty($_GET['q']) ? sanitize_text_field($_GET['q']) : '';
241
242 $whereClause = '';
243 if (!empty($search)) {
244 $like = '%' . $wpdb->esc_like($search) . '%';
245 $whereClause = $wpdb->prepare("AND meta_key LIKE %s", $like);
246 }
247
248 $queryResults = $wpdb->get_col("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE 1=1 $whereClause ORDER BY meta_key ASC LIMIT 20");
249
250 $results = [];
251 if (!empty($queryResults)) {
252 foreach ($queryResults as $queryResult) {
253 $results[] = [
254 'id' => $queryResult,
255 'text' => $queryResult,
256 ];
257 }
258 }
259
260 $response = [
261 'results' => $results,
262 ];
263 echo wp_json_encode($response);
264 exit;
265 }