PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.50.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.50.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 / terms-functions.php

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

177 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Generate a unique term slug should user decide to copy same term more than once.
5 *
6 * @param string $slug The initial slug to check
7 * @param string $taxonomy The taxonomy to check against
8 * @return string Unique slug
9 */
10 function taxopress_get_unique_term_slug($slug, $taxonomy)
11 {
12 $original_slug = $slug;
13 $count = 1;
14
15 while (term_exists($slug, $taxonomy)) {
16 $slug = $original_slug . '-' . $count;
17 $count++;
18 }
19
20 return $slug;
21 }
22
23 /**
24 * Handle the save and deletion of terms data.
25 */
26 function taxopress_process_terms()
27 {
28
29 if (wp_doing_ajax()) {
30 return;
31 }
32
33 if (!is_admin()) {
34 return;
35 }
36
37 if (!current_user_can('simple_tags')) {
38 return;
39 }
40
41 if (empty($_GET)) {
42 return;
43 }
44
45 if (!isset($_GET['page'])) {
46 return;
47 }
48 if ('st_terms' !== $_GET['page']) {
49 return;
50 }
51
52 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-terms') {
53 $nonce = !empty($_REQUEST['_wpnonce']) ? sanitize_text_field($_REQUEST['_wpnonce']) : '';
54 if (wp_verify_nonce($nonce, 'terms-action-request-nonce') && isset($_REQUEST['taxopress_terms'])) {
55 $term = get_term(sanitize_text_field($_REQUEST['taxopress_terms']));
56 wp_delete_term($term->term_id, $term->taxonomy);
57 }
58 add_action('admin_notices', "taxopress_term_delete_success_admin_notice");
59 add_filter('removable_query_args', 'taxopress_delete_terms_filter_removable_query_args');
60 }
61
62 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-remove-from-posts') {
63 $nonce = !empty($_REQUEST['_wpnonce']) ? sanitize_text_field($_REQUEST['_wpnonce']) : '';
64 if (wp_verify_nonce($nonce, 'terms-action-request-nonce') && isset($_REQUEST['taxopress_terms'])) {
65 $term = get_term(sanitize_text_field($_REQUEST['taxopress_terms']));
66 $args = array(
67 'post_type' => 'any',
68 'posts_per_page' => -1,
69 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Necessary to filter posts by specific term for accurate removal
70 'tax_query' => array(
71 array(
72 'taxonomy' => $term->taxonomy,
73 'field' => 'id',
74 'terms' => $term->term_id
75 )
76 )
77 );
78 $posts = get_posts($args);
79 $counter = 0;
80 foreach ($posts as $post) {
81 $remove = wp_remove_object_terms($post->ID, $term->term_id, $term->taxonomy);
82 if ($remove) {
83 clean_object_term_cache($post->ID, $term->taxonomy);
84 clean_term_cache($term->term_id, $term->taxonomy);
85 $counter++;
86 }
87 }
88 }
89 add_action('admin_notices', "taxopress_term_posts_remov_success_admin_notice");
90 add_filter('removable_query_args', 'taxopress_delete_terms_filter_removable_query_args');
91 }
92
93 if (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-copy-term') {
94 $nonce = !empty($_REQUEST['_wpnonce']) ? sanitize_text_field($_REQUEST['_wpnonce']) : '';
95 if (wp_verify_nonce($nonce, 'terms-action-request-nonce') && isset($_REQUEST['taxopress_terms'])) {
96 $term = get_term(sanitize_text_field($_REQUEST['taxopress_terms']));
97
98 $taxopress_term_name = $term->name . ' Copy';
99 $base_slug = $term->slug . '-copy';
100 $taxopress_term_slug = taxopress_get_unique_term_slug($base_slug, $term->taxonomy);
101 $taxopress_term_data = wp_insert_term($taxopress_term_name, $term->taxonomy, [
102 'slug' => $taxopress_term_slug,
103 'description' => $term->description,
104 ]);
105
106 if (!is_wp_error($taxopress_term_data)) {
107 $taxopress_term_id = $taxopress_term_data['term_id'];
108
109 $args = array(
110 'post_type' => 'any',
111 'posts_per_page' => -1,
112 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Necessary to filter posts by specific term for accurate term copying
113 'tax_query' => array(
114 array(
115 'taxonomy' => $term->taxonomy,
116 'field' => 'id',
117 'terms' => $term->term_id
118 )
119 )
120 );
121 $posts = get_posts($args);
122
123 foreach ($posts as $post) {
124 wp_set_object_terms($post->ID, $taxopress_term_id, $term->taxonomy, true);
125 }
126 }
127 }
128 add_action('admin_notices', "taxopress_term_copy_success_admin_notice");
129 add_filter('removable_query_args', 'taxopress_delete_terms_filter_removable_query_args');
130 }
131 }
132 add_action('admin_init', 'taxopress_process_terms', 8);
133
134 /**
135 * Successful term copy callback.
136 */
137 function taxopress_term_copy_success_admin_notice()
138 {
139 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
140 echo taxopress_admin_notices_helper(esc_html__('Term copied successfully.', 'simple-tags'), true);
141 }
142
143 /**
144 * Successful deleted callback.
145 */
146 function taxopress_term_delete_success_admin_notice()
147 {
148 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
149 echo taxopress_admin_notices_helper(esc_html__('Term deleted successfully.', 'simple-tags'), false);
150 }
151
152 /**
153 * Successful remove term from posts callback.
154 */
155 function taxopress_term_posts_remov_success_admin_notice()
156 {
157 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
158 echo taxopress_admin_notices_helper(esc_html__('Term removed from all posts successfully.', 'simple-tags'), true);
159 }
160
161 /**
162 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
163 *
164 * @link https://core.trac.wordpress.org/ticket/23367
165 *
166 * @param string[] $args Array of removable query arguments.
167 * @return string[] Updated array of removable query arguments.
168 */
169 function taxopress_delete_terms_filter_removable_query_args(array $args)
170 {
171 return array_merge($args, [
172 'action',
173 'taxopress_terms',
174 '_wpnonce',
175 ]);
176 }
177