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 / autoterms-functions.php

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

668 lines 26.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Fetch our TAXOPRESS Autoterms option.
5 *
6 * @return mixed
7 */
8 function taxopress_get_autoterm_data()
9 {
10 return array_filter((array)apply_filters(
11 'taxopress_get_autoterm_data',
12 get_option('taxopress_autoterms', []),
13 get_current_blog_id()
14 ));
15 }
16
17 /**
18 * Fetch our TAXOPRESS Autoterms content option.
19 *
20 * @return mixed
21 */
22 function taxopress_get_autoterms_content_data()
23 {
24 return array_filter((array)apply_filters(
25 'taxopress_get_autoterm_content_data',
26 get_option('taxopress_autoterms_content', []),
27 get_current_blog_id()
28 ));
29 }
30
31 /**
32 * Fetch our TAXOPRESS Autoterms schedule option.
33 *
34 * @return mixed
35 */
36 function taxopress_get_autoterms_schedule_data()
37 {
38 return array_filter((array)apply_filters(
39 'taxopress_get_autoterm_schedule_data',
40 get_option('taxopress_autoterms_schedule', []),
41 get_current_blog_id()
42 ));
43 }
44
45 /**
46 * Get the selected autoterm from the $_POST global.
47 *
48 * @return bool|string False on no result, sanitized autoterm if set.
49 * @internal
50 *
51 */
52 function taxopress_get_current_autoterm()
53 {
54
55 $autoterms = false;
56
57 if (!empty($_GET) && isset($_GET['taxopress_autoterms'])) {
58 $autoterms = sanitize_text_field($_GET['taxopress_autoterms']);
59 } else {
60 $autoterms = taxopress_get_autoterm_data();
61 if (!empty($autoterms)) {
62 // Will return the first array key.
63 $autoterms = key($autoterms);
64 }
65 }
66
67 /**
68 * Filters the current autoterm to edit.
69 *
70 * @param string $autoterms autoterm slug.
71 */
72 return apply_filters('taxopress_current_autoterm', $autoterms);
73 }
74
75 /**
76 * Get auto terms for current post
77 *
78 *
79 * @return mixed
80 */
81 function taxopress_post_type_autoterms()
82 {
83 global $pagenow;
84
85 $allowed_pages = ['post-new.php', 'post.php', 'page.php', 'page-new.php'];
86 if (!in_array($pagenow, $allowed_pages)) {
87 return false;
88 }
89
90 $autoterms = taxopress_get_autoterm_data();
91
92 if (count($autoterms) > 0) {
93 foreach ($autoterms as $autoterm) {
94
95 $post_types = (isset($autoterm['post_types']) && is_array($autoterm['post_types']) && count($autoterm['post_types']) > 0) ? $autoterm['post_types'] : false;
96
97 if (!$post_types) {
98 continue;
99 }
100
101 if (in_array(get_post_type(), $post_types)) {
102 return $autoterm;
103 }
104 }
105 }
106
107 return false;
108 }
109
110 /**
111 * Handle the save and deletion of autoterm data.
112 */
113 function taxopress_process_autoterm()
114 {
115
116 if (wp_doing_ajax()) {
117 return;
118 }
119
120 if (!is_admin()) {
121 return;
122 }
123
124 if (!current_user_can('simple_tags')) {
125 return;
126 }
127
128 if (empty($_GET)) {
129 return;
130 }
131
132 if (!isset($_GET['page'])) {
133 return;
134 }
135 if (!in_array($_GET['page'], ['st_autoterms', 'st_autoterms_schedule'])) {
136 return;
137 }
138
139 if (isset($_GET['new_autoterm'])) {
140 if ((int)$_GET['new_autoterm'] === 1) {
141 add_action('admin_notices', "taxopress_autoterms_update_success_admin_notice");
142 add_filter('removable_query_args', 'taxopress_saved_autoterm_filter_removable_query_args');
143 }
144 }
145
146 if (isset($_GET['deleted_autoterm'])) {
147 if ((int)$_GET['deleted_autoterm'] === 1) {
148 add_action('admin_notices', "taxopress_autoterms_delete_success_admin_notice");
149 add_filter('removable_query_args', 'taxopress_deleted_autoterm_filter_removable_query_args');
150 }
151 }
152
153
154 if (!empty($_POST) && isset($_POST['autoterm_submit'])) {
155 $result = '';
156 if (isset($_POST['autoterm_submit'])) {
157 check_admin_referer(
158 'taxopress_addedit_autoterm_nonce_action',
159 'taxopress_addedit_autoterm_nonce_field'
160 );
161 $result = taxopress_update_autoterm($_POST);
162 }
163
164 if ($result) {
165 wp_safe_redirect(
166 add_query_arg(
167 [
168 'page' => 'st_autoterms',
169 'add' => 'new_item',
170 'action' => 'edit',
171 'taxopress_autoterms' => $result,
172 'new_autoterm' => 1,
173 ],
174 taxopress_admin_url('admin.php')
175 )
176 );
177
178 exit();
179 }
180 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm') {
181 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
182 if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) {
183 taxopress_action_delete_autoterm(sanitize_text_field($_REQUEST['taxopress_autoterms']));
184 add_action('admin_notices', "taxopress_autoterms_delete_autoterm_admin_notice");
185 }
186 add_filter('removable_query_args', 'taxopress_delete_autoterm_filter_removable_query_args');
187 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm-log') {
188 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
189 if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) {
190 wp_delete_post((int)$_REQUEST['taxopress_autoterms_log'], true);
191 add_action('admin_notices', "taxopress_autoterms_delete_autoterm_log_admin_notice");
192 }
193 add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args');
194 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-autoterm-logs') {
195 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
196 if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) {
197 global $wpdb;
198 $result = $wpdb->query(
199 $wpdb->prepare(
200 "
201 DELETE posts, pt, pm
202 FROM {$wpdb->prefix}posts posts
203 LEFT JOIN {$wpdb->prefix}term_relationships pt ON pt.object_id = posts.ID
204 LEFT JOIN {$wpdb->prefix}postmeta pm ON pm.post_id = posts.ID
205 WHERE posts.post_type = %s
206 ",
207 'taxopress_logs'
208 )
209 );
210 add_action('admin_notices', "taxopress_autoterms_delete_autoterm_logs_admin_notice");
211 }
212 add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args');
213 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-enable-autoterm-logs') {
214 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
215 if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) {
216 delete_option('taxopress_autoterms_logs_disabled');
217 add_action('admin_notices', "taxopress_autoterms_enable_log_admin_notice");
218 }
219 add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args');
220 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-disable-autoterm-logs') {
221 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
222 if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) {
223 update_option('taxopress_autoterms_logs_disabled', 1);
224 add_action('admin_notices', "taxopress_autoterms_disable_log_admin_notice");
225 }
226 add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args');
227 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-update-autoterm-limit') {
228 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
229 if (wp_verify_nonce($nonce, 'autoterm-action-request-nonce')) {
230 $limit = (int)$_REQUEST['limit'];
231 if ($limit > 0) {
232 update_option('taxopress_auto_terms_logs_limit', $limit);
233 add_action('admin_notices', "taxopress_autoterms_limit_updated_admin_notice");
234 } else {
235 add_action('admin_notices', "taxopress_autoterms_limit_invalid_admin_notice");
236 }
237 }
238 add_filter('removable_query_args', 'taxopress_delete_autoterm_log_filter_removable_query_args');
239 }
240 }
241
242 add_action('admin_init', 'taxopress_process_autoterm', 8);
243
244
245 /**
246 * Create default autoterm.
247 */
248 function taxopress_create_default_autoterm()
249 {
250
251 if (wp_doing_ajax()) {
252 return;
253 }
254
255 if (!is_admin()) {
256 return;
257 }
258
259 if (!current_user_can('simple_tags')) {
260 return;
261 }
262
263 if ((int)get_option('taxopress_default_autoterms') > 0) {
264 return;
265 }
266
267 if (count(taxopress_get_autoterm_data()) > 0) {
268 return;
269 }
270
271 //create default or export previous
272 $create_default = true;
273
274 $options = get_option(STAGS_OPTIONS_NAME_AUTO);
275 if (is_array($options) && count($options) > 0) {
276 foreach ($options as $options_post_type => $options_taxdata) {
277 if (is_array($options_taxdata) && count($options_taxdata) > 0) {
278 $create_default = false;
279 foreach ($options_taxdata as $options_taxonomy => $options_taxonomy_data) {
280 $taxonomy = get_taxonomy($options_taxonomy);
281 $default = [];
282 $default['taxopress_autoterm']['autoterm_from'] = 'posts';
283 $default['taxopress_autoterm']['title'] = ''. (is_object($taxonomy) ? $taxonomy->labels->name : $options_taxonomy) .' '. ucwords($options_post_type) .' Auto term';
284 $default['taxopress_autoterm']['taxonomy'] = $options_taxonomy;
285 $default['post_types'] = [];
286 $default['post_status'] = ['publish'];
287 $default['taxopress_autoterm']['autoterm_useall'] = isset($options_taxonomy_data['at_all']) ? $options_taxonomy_data['at_all'] : 0;
288 $default['taxopress_autoterm']['autoterm_useonly'] = isset($options_taxonomy_data['at_all_no']) ? $options_taxonomy_data['at_all_no'] : 0;
289 $default['taxopress_autoterm']['autoterm_target'] = isset($options_taxonomy_data['at_empty']) ? $options_taxonomy_data['at_empty'] : 0;
290 $default['taxopress_autoterm']['autoterm_word'] = isset($options_taxonomy_data['only_full_word']) ? $options_taxonomy_data['only_full_word'] : 0;
291 $default['taxopress_autoterm']['autoterm_hash'] = isset($options_taxonomy_data['allow_hashtag_format']) ? $options_taxonomy_data['allow_hashtag_format'] : 0;
292 $default['specific_terms'] = isset($options_taxonomy_data['auto_list']) ? (array) maybe_unserialize($options_taxonomy_data['auto_list']) : [];
293 $default['taxopress_autoterm']['terms_limit'] = '5';
294 $default['taxopress_autoterm']['synonyms_term'] = 1;
295
296 $result = taxopress_update_autoterm($default);
297 }
298 }
299 }
300 }
301
302 if ($create_default) {
303 $default = [];
304 $default['taxopress_autoterm']['title'] = esc_html__('Auto term', 'simple-tags');
305 $default['taxopress_autoterm']['taxonomy'] = 'post_tag';
306 $default['post_types'] = [];
307 $default['post_status'] = ['publish'];
308 $default['taxopress_autoterm']['autoterm_from'] = 'posts';
309 $default['taxopress_autoterm']['autoterm_use_taxonomy'] = '1';
310 $default['taxopress_autoterm']['autoterm_useall'] = '1';
311 $default['taxopress_autoterm']['autoterm_useonly'] = '0';
312 $default['taxopress_autoterm']['autoterm_target'] = '0';
313 $default['taxopress_autoterm']['autoterm_word'] = '0';
314 $default['taxopress_autoterm']['autoterm_hash'] = '0';
315 $default['taxopress_autoterm']['terms_limit'] = '5';
316 $default['taxopress_autoterm']['synonyms_term'] = 1;
317 $default['taxopress_autoterm']['autoterm_for_post'] = '1';
318 $default['taxopress_autoterm']['autoterm_for_schedule'] = '1';
319 $default['taxopress_autoterm']['autoterm_for_existing_content'] = '1';
320 $default['taxopress_autoterm']['autoterm_for_metaboxes'] = '1';
321 $default['taxopress_autoterm']['schedule_terms_limit'] = '5';
322 $default['taxopress_autoterm']['schedule_autoterm_target'] = '0';
323 $default['taxopress_autoterm']['schedule_autoterm_word'] = '0';
324 $default['taxopress_autoterm']['schedule_autoterm_hash'] = '0';
325 $default['taxopress_autoterm']['existing_content_terms_limit'] = '5';
326 $default['specific_terms'] = [];
327 $default['find_in_customs_entries'] = [];
328 $default['find_in_custom_fields_custom_items'] = [];
329 $default['find_in_taxonomies_custom_items'] = [];
330 $result = taxopress_update_autoterm($default);
331 }
332 update_option('taxopress_default_autoterms', $result);
333 }
334
335 add_action('admin_init', 'taxopress_create_default_autoterm', 8);
336
337
338 /**
339 * Add to or update our TAXOPRESS option with new data.
340 *
341 *
342 * @param array $data Array of autoterm data to update. Optional.
343 * @return bool|string False on failure, string on success.
344 * @internal
345 *
346 */
347 function taxopress_update_autoterm($data = [])
348 {
349 $sanitized_data = [];
350 foreach ($data as $key => $value) {
351 if (!is_array($value)) {
352 $sanitized_data[$key] = taxopress_sanitize_text_field($value);
353 } else {
354 $new_value = [];
355 foreach ($data[$key] as $option_key => $option_value) {
356 if ($option_key === 'terms_regex_code') {
357 $regex_pattern = sanitize_text_field($option_value);
358 if (!empty($regex_pattern) && preg_match($regex_pattern, '') !== false) {
359 $new_value[$option_key] = $regex_pattern;
360 } else {
361 $new_value[$option_key] = '';//invalid expression
362 }
363 } elseif ($option_key === 'post_types' || $option_key === 'post_status') {
364 $new_value[$option_key] = taxopress_sanitize_post_type_status($option_value);
365 } else {
366 $new_value[$option_key] = taxopress_sanitize_text_field($option_value);
367 }
368 }
369 $sanitized_data[$key] = $new_value;
370 }
371 }
372 $data = $sanitized_data;
373
374 $autoterms = taxopress_get_autoterm_data();
375
376 $title = $data['taxopress_autoterm']['title'];
377 $title = str_replace('"', '', htmlspecialchars_decode($title));
378 $title = htmlspecialchars($title, ENT_QUOTES);
379 $title = trim($title);
380 $data['taxopress_autoterm']['title'] = stripslashes_deep($title);
381
382
383 //update other post post
384 $data['taxopress_autoterm']['specific_terms'] = isset($data['specific_terms']) ? $data['specific_terms'] : [];
385 $data['taxopress_autoterm']['post_types'] = isset($data['post_types']) ? $data['post_types'] : [];
386 $data['taxopress_autoterm']['post_status'] = isset($data['post_status']) ? $data['post_status'] : [];
387
388 $data['taxopress_autoterm']['html_exclusion'] = isset($data['html_exclusion']) ? $data['html_exclusion'] : [];
389 $data['taxopress_autoterm']['html_exclusion_customs'] = isset($data['html_exclusion_customs']) ? $data['html_exclusion_customs'] : [];
390 $data['taxopress_autoterm']['html_exclusion_customs_entry'] = isset($data['html_exclusion_customs_entry']) ? array_unique($data['html_exclusion_customs_entry']) : [];
391
392 $data['taxopress_autoterm']['find_in_customs_entries'] = isset($data['find_in_customs_entries']) ? $data['find_in_customs_entries'] : [];
393 $data['taxopress_autoterm']['find_in_custom_fields_custom_items'] = isset($data['find_in_custom_fields_custom_items']) ? $data['find_in_custom_fields_custom_items'] : [];
394 $data['taxopress_autoterm']['find_in_taxonomies_custom_items'] = isset($data['find_in_taxonomies_custom_items']) ? $data['find_in_taxonomies_custom_items'] : [];
395
396
397 //update our custom checkbox value if not checked
398 if (!isset($data['taxopress_autoterm']['autoterm_useall'])) {
399 $data['taxopress_autoterm']['autoterm_useall'] = 0;
400 }
401 if (!isset($data['taxopress_autoterm']['autoterm_useonly'])) {
402 $data['taxopress_autoterm']['autoterm_useonly'] = 0;
403 }
404 if (!isset($data['taxopress_autoterm']['autoterm_target'])) {
405 $data['taxopress_autoterm']['autoterm_target'] = 0;
406 }
407 if (!isset($data['taxopress_autoterm']['autoterm_word'])) {
408 $data['taxopress_autoterm']['autoterm_word'] = 0;
409 }
410 if (!isset($data['taxopress_autoterm']['autoterm_hash'])) {
411 $data['taxopress_autoterm']['autoterm_hash'] = 0;
412 }
413 if (!isset($data['taxopress_autoterm']['synonyms_term'])) {
414 $data['taxopress_autoterm']['synonyms_term'] = 0;
415 }
416 if (!isset($data['taxopress_autoterm']['autoterm_for_post'])) {
417 $data['taxopress_autoterm']['autoterm_for_post'] = 0;
418 }
419 if (!isset($data['taxopress_autoterm']['autoterm_for_schedule'])) {
420 $data['taxopress_autoterm']['autoterm_for_schedule'] = 0;
421 }
422 if (!isset($data['taxopress_autoterm']['autoterm_for_existing_content'])) {
423 $data['taxopress_autoterm']['autoterm_for_existing_content'] = 0;
424 }
425 if (!isset($data['taxopress_autoterm']['autoterm_for_metaboxes'])) {
426 $data['taxopress_autoterm']['autoterm_for_metaboxes'] = 0;
427 }
428 if (!isset($data['taxopress_autoterm']['schedule_autoterm_target'])) {
429 $data['taxopress_autoterm']['schedule_autoterm_target'] = 0;
430 }
431 if (!isset($data['taxopress_autoterm']['schedule_autoterm_word'])) {
432 $data['taxopress_autoterm']['schedule_autoterm_word'] = 0;
433 }
434 if (!isset($data['taxopress_autoterm']['schedule_autoterm_hash'])) {
435 $data['taxopress_autoterm']['schedule_autoterm_hash'] = 0;
436 }
437
438 if (isset($data['edited_autoterm'])) {
439 $autoterm_id = $data['edited_autoterm'];
440 $autoterms[$autoterm_id] = $data['taxopress_autoterm'];
441 $success = update_option('taxopress_autoterms', $autoterms);
442 //return 'update_success';
443 } else {
444 $autoterm_id = (int)get_option('taxopress_autoterm_ids_increament') + 1;
445 $data['taxopress_autoterm']['ID'] = $autoterm_id;
446 $autoterms[$autoterm_id] = $data['taxopress_autoterm'];
447 $success = update_option('taxopress_autoterms', $autoterms);
448 $update_id = update_option('taxopress_autoterm_ids_increament', $autoterm_id);
449 //return 'add_success';
450 }
451
452 return $autoterm_id;
453 }
454
455 /**
456 * Successful update callback.
457 */
458 function taxopress_autoterms_update_success_admin_notice()
459 {
460 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
461 echo taxopress_admin_notices_helper(esc_html__('Settings updated successfully.', 'simple-tags'));
462 }
463
464 /**
465 * Successful deleted callback.
466 */
467 function taxopress_autoterms_delete_success_admin_notice()
468 {
469 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
470 echo taxopress_admin_notices_helper(esc_html__('Auto Terms successfully deleted.', 'simple-tags'), false);
471 }
472
473 function taxopress_autoterms_enable_log_admin_notice()
474 {
475 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
476 echo taxopress_admin_notices_helper(esc_html__('Auto Terms logs enabled successfully.', 'simple-tags'));
477 }
478
479 function taxopress_autoterms_disable_log_admin_notice()
480 {
481 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
482 echo taxopress_admin_notices_helper(esc_html__('Auto Terms logs disabled successfully.', 'simple-tags'));
483 }
484
485 function taxopress_autoterms_delete_autoterm_admin_notice()
486 {
487 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
488 echo taxopress_admin_notices_helper(esc_html__('Auto Terms deleted successfully.', 'simple-tags'));
489 }
490
491 function taxopress_autoterms_delete_autoterm_log_admin_notice()
492 {
493 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
494 echo taxopress_admin_notices_helper(esc_html__('Auto Terms log deleted successfully.', 'simple-tags'));
495 }
496
497 function taxopress_autoterms_delete_autoterm_logs_admin_notice()
498 {
499 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
500 echo taxopress_admin_notices_helper(esc_html__('Auto Terms logs deleted successfully.', 'simple-tags'));
501 }
502
503 function taxopress_autoterms_limit_updated_admin_notice()
504 {
505 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
506 echo taxopress_admin_notices_helper(esc_html__('Auto Terms limit updated successfully.', 'simple-tags'));
507 }
508
509 function taxopress_autoterms_limit_invalid_admin_notice()
510 {
511 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
512 echo taxopress_admin_notices_helper(esc_html__('Auto Terms limit must be greater than 0.', 'simple-tags'), false);
513 }
514
515 /**
516 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
517 *
518 * @link https://core.trac.wordpress.org/ticket/23367
519 *
520 * @param string[] $args Array of removable query arguments.
521 * @return string[] Updated array of removable query arguments.
522 */
523 function taxopress_saved_autoterm_filter_removable_query_args(array $args)
524 {
525 return array_merge($args, [
526 'new_autoterm',
527 ]);
528 }
529
530 /**
531 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
532 *
533 * @link https://core.trac.wordpress.org/ticket/23367
534 *
535 * @param string[] $args Array of removable query arguments.
536 * @return string[] Updated array of removable query arguments.
537 */
538 function taxopress_deleted_autoterm_filter_removable_query_args(array $args)
539 {
540 return array_merge($args, [
541 'deleted_autoterm',
542 ]);
543 }
544
545 /**
546 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
547 *
548 * @link https://core.trac.wordpress.org/ticket/23367
549 *
550 * @param string[] $args Array of removable query arguments.
551 * @return string[] Updated array of removable query arguments.
552 */
553 function taxopress_delete_autoterm_filter_removable_query_args(array $args)
554 {
555 return array_merge($args, [
556 'action',
557 'taxopress_autoterms',
558 '_wpnonce',
559 ]);
560 }
561
562 /**
563 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
564 *
565 * @link https://core.trac.wordpress.org/ticket/23367
566 *
567 * @param string[] $args Array of removable query arguments.
568 * @return string[] Updated array of removable query arguments.
569 */
570 function taxopress_delete_autoterm_log_filter_removable_query_args(array $args)
571 {
572 return array_merge($args, [
573 'action',
574 'taxopress_autoterms_log',
575 '_wpnonce',
576 ]);
577 }
578
579 /**
580 * Delete our custom autoterm from the array of autoterms.
581 * @return bool|string False on failure, string on success.
582 */
583 function taxopress_action_delete_autoterm($autoterm_id)
584 {
585 $autoterms = taxopress_get_autoterm_data();
586
587 if (array_key_exists($autoterm_id, $autoterms)) {
588 unset($autoterms[$autoterm_id]);
589 $success = update_option('taxopress_autoterms', $autoterms);
590 }
591
592 if (isset($success)) {
593 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
594 wp_safe_redirect(
595 add_query_arg(
596 [
597 'page' => 'st_autoterms',
598 'deleted_autoterm' => 1,
599 ],
600 taxopress_admin_url('admin.php')
601 )
602 );
603 exit();
604 }
605 }
606
607 /**
608 * Get auto terms logs data
609 *
610 */
611 function taxopress_autoterms_logs_data($per_page = 20, $current_page = 1, $orderby = 'ID', $order = 'desc', $schedule_query = false)
612 {
613
614 $meta_query[] = array('relation' => 'AND');
615 $meta_query[] = array(
616 'key' => '_taxopress_log_component',
617 'value' => 'st_autoterms',
618 );
619
620 /**
621 * Custom filter handler
622 */
623 $custom_filters = [
624 'log_source_filter' => '_taxopress_log_action',
625 'log_filter_post_type' => '_taxopress_log_post_type',
626 'log_filter_taxonomy' => '_taxopress_log_taxonomy',
627 'log_filter_status_message' => '_taxopress_log_status_message',
628 'log_filter_settings' => '_taxopress_log_option_id'
629 ];
630 foreach ($custom_filters as $filter => $option) {
631 if (!empty($_REQUEST[$filter])) {
632 $meta_query[] = array(
633 'key' => sanitize_key($option),
634 'value' => sanitize_text_field($_REQUEST[$filter]),
635 );
636 }
637 }
638
639 if ($schedule_query) {
640 $meta_query[] = array(
641 'key' => '_taxopress_log_action',
642 'value' => ['daily_cron_schedule', 'hourly_cron_schedule', 'weekly_cron_schedule'],
643 'compare' => 'IN'
644 );
645 }
646
647 $logs_arg = array(
648 'post_type' => 'taxopress_logs',
649 'post_status' => 'publish',
650 'paged' => $current_page,
651 'posts_per_page' => $per_page,
652 'meta_query' => $meta_query,
653 'orderby' => $orderby,
654 'order' => $order,
655 );
656
657 /**
658 * Handle search
659 */
660 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
661 $logs_arg['s'] = $search;
662 }
663
664 $logs = new WP_Query($logs_arg);
665
666 return ['posts' => $logs->posts, 'counts' => $logs->found_posts];
667 }
668