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.php

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

359 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class SimpleTags_Terms
4 {
5
6 const MENU_SLUG = 'st_options';
7
8 // class instance
9 static $instance;
10
11 // WP_List_Table object
12 public $terms_table;
13
14 /**
15 * Constructor
16 *
17 * @return void
18 * @author Olatechpro
19 */
20 public function __construct()
21 {
22
23 global $hook_suffix; //avoid warning outputs
24 if (!isset($hook_suffix)) {
25 $hook_suffix = '';
26 }
27
28 add_filter('set-screen-option', [__CLASS__, 'set_screen'], 10, 3);
29 // Admin menu
30 add_action('admin_menu', [$this, 'admin_menu']);
31 // Javascript
32 add_action('admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts'], 11);
33 //support post type for terms query
34 add_filter('terms_clauses', [__CLASS__, 'taxopress_terms_clauses'], 10, 3);
35 //inline term edit
36 add_action('wp_ajax_taxopress_terms_inline_save_term', [$this, 'taxopress_terms_inline_save_term_callback']);
37 }
38
39 // Handle the post_type parameter given in get_terms function
40 public static function taxopress_terms_clauses($clauses, $taxonomy, $args)
41 {
42 if (isset($_REQUEST['page']) && $_REQUEST['page'] === 'st_terms' && !empty($args['post_types']) && is_array($args['post_types'])) {
43 global $wpdb;
44
45 $post_types = array();
46
47 foreach ($args['post_types'] as $cpt) {
48 $post_types[] = "'" . $cpt . "'";
49 }
50
51 if (!empty($post_types)) {
52 $clauses['fields'] = 'DISTINCT ' . str_replace('tt.*', 'tt.term_taxonomy_id, tt.term_id, tt.taxonomy, tt.description, tt.parent', $clauses['fields']) . ', COUNT(t.term_id) AS count';
53 $clauses['join'] .= ' INNER JOIN ' . $wpdb->term_relationships . ' AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN ' . $wpdb->posts . ' AS p ON p.ID = r.object_id';
54 $clauses['where'] .= ' AND p.post_type IN (' . implode(',', $post_types) . ')';
55 $clauses['orderby'] = 'GROUP BY t.term_id ' . $clauses['orderby'];
56 }
57 }
58 return $clauses;
59 }
60
61 /**
62 * Init somes JS and CSS need for this feature
63 *
64 * @return void
65 * @author Olatechpro
66 */
67 public static function admin_enqueue_scripts()
68 {
69
70 // add JS for manage click tags
71 if (isset($_GET['page']) && $_GET['page'] == 'st_terms') {
72 wp_enqueue_style('st-taxonomies-css');
73 wp_enqueue_script('admin-tags');
74 wp_enqueue_script('inline-edit-tax');
75 }
76 }
77
78
79 public function taxopress_terms_inline_save_term_callback()
80 {
81 global $wpdb;
82
83 check_ajax_referer('taxinlineeditnonce', '_inline_edit');
84
85 $edit_taxonomy = sanitize_key($_POST['edit_taxonomy']);
86 $taxonomy = sanitize_key($_POST['original_tax']);
87 $tax = get_taxonomy($taxonomy);
88 $edit_tax = get_taxonomy($edit_taxonomy);
89
90 if (!$tax || !$edit_tax) {
91 wp_die(0);
92 }
93
94 if (!isset($_POST['tax_ID']) || !(int) $_POST['tax_ID']) {
95 wp_die(-1);
96 }
97
98 $id = (int) $_POST['tax_ID'];
99
100 if (!current_user_can('edit_term', $id)) {
101 wp_die(-1);
102 }
103
104 $tag = get_term($id, $taxonomy);
105 $_POST['description'] = $tag->description;
106
107 $updated = wp_update_term($id, $taxonomy, $_POST);
108
109 if ($updated && !is_wp_error($updated)) {
110 $tag = get_term($updated['term_id'], $taxonomy);
111 if (!$tag || is_wp_error($tag)) {
112 if (is_wp_error($tag) && $tag->get_error_message()) {
113 wp_die(esc_html($tag->get_error_message()));
114 }
115 wp_die(esc_html__('Item not updated.'));
116 } else {
117 if ($tax !== $edit_tax) {
118 $update_term = $wpdb->update(
119 $wpdb->prefix . 'term_taxonomy',
120 ['taxonomy' => $edit_taxonomy],
121 ['term_taxonomy_id' => $tag->term_taxonomy_id],
122 ['%s'],
123 ['%d']
124 );
125 if ($update_term) {
126 clean_term_cache($tag->term_id);
127 $tag = get_term($tag->term_id, $edit_taxonomy);
128 }
129 }
130 }
131 } else {
132 /*if ( is_wp_error( $updated ) && $updated->get_error_message() ) {
133 wp_die( esc_html($updated->get_error_message()) );
134 }*/
135 wp_die(esc_html__('Error updating term.'));
136 }
137
138 $wp_list_table = new Taxopress_Terms_List();
139
140 $wp_list_table->single_row($tag);
141 wp_die();
142 }
143
144
145 public static function set_screen($status, $option, $value)
146 {
147 return $value;
148 }
149
150 /** Singleton instance */
151 public static function get_instance()
152 {
153 if (!isset(self::$instance)) {
154 self::$instance = new self();
155 }
156
157 return self::$instance;
158 }
159
160 /**
161 * Add WP admin menu for Tags
162 *
163 * @return void
164 * @author Olatechpro
165 */
166 public function admin_menu()
167 {
168 $hook = add_submenu_page(
169 self::MENU_SLUG,
170 esc_html__('Terms', 'simple-tags'),
171 esc_html__('Terms', 'simple-tags'),
172 'simple_tags',
173 'st_terms',
174 [
175 $this,
176 'page_manage_terms',
177 ]
178 );
179
180 if (taxopress_is_screen_main_page() && (!isset($_GET['action']) || $_GET['action'] !== 'new_item')) {
181 add_action("load-$hook", [$this, 'screen_option']);
182 }
183 }
184
185 /**
186 * Screen options
187 */
188 public function screen_option()
189 {
190
191
192 $option = 'per_page';
193 $args = [
194 'label' => esc_html__('Number of items per page', 'simple-tags'),
195 'default' => 20,
196 'option' => 'st_terms_per_page'
197 ];
198
199 add_screen_option($option, $args);
200
201
202 $this->terms_table = new Taxopress_Terms_List();
203 }
204
205 /**
206 * Method for build the page HTML manage tags
207 *
208 * @return void
209 * @author Olatechpro
210 */
211 public function page_manage_terms()
212 {
213 // Default order
214 if (!isset($_GET['orderby'])) {
215 $_GET['orderby'] = 'name';
216 }
217 if (!isset($_GET['order'])) {
218 $_GET['order'] = 'asc';
219 }
220
221 settings_errors(__CLASS__);
222
223 ?>
224 <div class="wrap st_wrap st-manage-taxonomies-page manage-taxopress-terms">
225
226 <div id="">
227
228 <?php
229 if (isset($_GET['action']) && $_GET['action'] === 'new_item') {
230 ?>
231 <div class="taxopress-term-wrap">
232 <div class="taxopress-term-main">
233 <h1 class="wp-heading-inline"><?php esc_html_e('Terms', 'simple-tags'); ?></h1>
234 <div class="taxopress-description">
235 <?php esc_html_e('This screen allows you to search and edit all the terms on your site.', 'simple-tags'); ?>
236 </div>
237 <?php
238 do_action('taxopress_terms_copy_with_metadata_promo');
239 ?>
240 </div>
241 <div class="taxopress-right-sidebar">
242 <?php do_action('taxopress_admin_after_sidebar'); ?>
243 </div>
244 </div>
245 <?php
246 } else {
247 ?>
248 <?php
249
250 $taxonomy_heading = '';
251 $show_order_message = false;
252 if (!empty($_REQUEST['taxopress_terms_taxonomy'])) {
253 $taxonomy_obj = get_taxonomy(sanitize_text_field($_REQUEST['taxopress_terms_taxonomy']));
254 if ($taxonomy_obj) {
255 $taxonomy_heading = $taxonomy_obj->labels->name;
256 $show_order_message = true;
257 }
258 }
259 ?>
260 <h1 class="wp-heading-inline">
261 <?php
262 if ($taxonomy_heading) {
263 echo esc_html($taxonomy_heading);
264 } else {
265 esc_html_e('Terms', 'simple-tags');
266 }
267 ?>
268 </h1>
269 <div class="taxopress-description">
270 <?php
271 if ($show_order_message) {
272 esc_html_e('This screen allows you to order terms via drag and drop.', 'simple-tags');
273 } else {
274 esc_html_e('This screen allows you to search and edit all the terms on your site.', 'simple-tags');
275 }
276 ?>
277 </div>
278
279 <?php
280 if (isset($_REQUEST['s']) && $search = esc_attr(sanitize_text_field(wp_unslash($_REQUEST['s'])))) {
281 /* translators: %s: search keywords */
282 printf(' <span class="subtitle">' . esc_html__(
283 'Search results for &#8220;%s&#8221;',
284 'simple-tags'
285 ) . '</span>', esc_html($search));
286 }
287 ?>
288 <?php
289
290 //the terms table instance
291 $this->terms_table->prepare_items();
292 ?>
293
294
295 <hr class="wp-header-end">
296 <div id="ajax-response"></div>
297 <form class="search-form wp-clearfix st-taxonomies-search-form" method="get">
298 <?php
299 // Hide search box if taxopress_show_all=1
300 $this->terms_table->search_box(esc_html__('Search Terms', 'simple-tags'), 'term');
301
302 ?>
303 </form>
304 <div class="clear"></div>
305
306 <div id="col-container" class="wp-clearfix">
307
308 <div class="col-wrap">
309 <form action="<?php echo esc_url(add_query_arg('', '')); ?>" method="post">
310 <?php
311 // Hide pagination if taxopress_show_all=1
312 // if (!empty($_REQUEST['taxopress_show_all']) && $_REQUEST['taxopress_show_all'] == '1') {
313 // // Remove pagination controls via CSS
314 // echo '<style>
315 // .tablenav.top, .tablenav.bottom { display: none !important; }
316 // </style>';
317 // }
318 $this->terms_table->display(); //Display the table
319 ?>
320 </form>
321 <div class="form-wrap edit-term-notes">
322 <p><?php esc_html__('Description here.', 'simple-tags') ?></p>
323 </div>
324 </div>
325
326
327 </div>
328
329
330 </div>
331 <?php $this->terms_table->inline_edit(); ?>
332
333 <script>
334 (function($) {
335 'use strict';
336
337 $(document).ready(function() {
338 // -------------------------------------------------------------
339 // TaxoPress terms quick edit
340 // -------------------------------------------------------------
341 $(document).on('click', 'a.editinline, button.editinline', function(e) {
342 var term_id = $(this).attr('data-term-id');
343 var taxonomy = $(this).attr('data-taxonomy');
344 $('.inline-edit-row#edit-' + term_id).find('select[name="edit_taxonomy"]').val(taxonomy);
345 });
346 });
347
348 })(jQuery);
349 </script>
350 <?php SimpleTags_Admin::printAdminFooter(); ?>
351 </div>
352 <?php
353 }
354 ?>
355 <?php
356 }
357
358 }
359