PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.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 3.6.2 All 177 releases
simple-tags / inc / terms-functions.php

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

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