PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.53.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.53.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 / class-taxonomies-table.php

class-taxonomies-table.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.53.0, at inc/class-taxonomies-table.php

500 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped,WordPress.Security.NonceVerification.Recommended -- Legacy TaxoPress file: keep behavior unchanged while documenting existing PHPCS exceptions.
4
5 if (!class_exists('WP_List_Table')) {
6 require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
7 }
8
9 class Taxonomy_List extends WP_List_Table
10 {
11 /** Class constructor */
12 public function __construct()
13 {
14
15 parent::__construct([
16 'singular' => __('Taxonomy', 'simple-tags'), //singular name of the listed records
17 'plural' => __('Taxonomies', 'simple-tags'), //plural name of the listed records
18 'ajax' => false //does this table support ajax?
19 ]);
20 }
21
22 /**
23 * Retrieve st_taxonomies data from the database
24 *
25 * @param int $per_page
26 * @param int $page_number
27 *
28 * @return mixed
29 */
30 public static function get_st_taxonomies()
31 {
32 return get_all_taxopress_taxonomies_request();
33 }
34
35 /**
36 * Returns the count of records in the database.
37 *
38 * @return null|string
39 */
40 public static function record_count()
41 {
42 return count(get_all_taxopress_taxonomies_request());
43 }
44
45 /**
46 * Show single row item
47 *
48 * @param array $item
49 */
50 public function single_row($item)
51 {
52 $class = ['st-tax-tr'];
53 $id = 'st-tax-' . md5($item->name);
54 echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class)));
55 $this->single_row_columns($item);
56 echo '</tr>';
57 }
58
59 /**
60 * Associative array of columns
61 *
62 * @return array
63 */
64 public function get_columns()
65 {
66 $columns = [
67 'cb' => '',
68 'name' => __('Name', 'simple-tags'),
69 'registration_key' => __('Registration key', 'simple-tags'),
70 'description' => __('Description', 'simple-tags'),
71 'active' => __('Active', 'simple-tags'),
72 'posttypes' => __('Post Types', 'simple-tags'),
73 'count' => __('Count', 'simple-tags'),
74 'taxopress_order' => __('Order', 'simple-tags'),
75 'edited_with_taxopress' => __('Edited', 'simple-tags'),
76 ];
77
78 return $columns;
79 }
80
81 /**
82 * Render a column when no column specific method exist.
83 *
84 * @param array $item
85 * @param string $column_name
86 *
87 * @return mixed
88 */
89 public function column_default($item, $column_name)
90 {
91 return !empty($item->$column_name) ? $item->$column_name : '&mdash;';
92 }
93
94 /**
95 * The checkbox column
96 *
97 * @param object $item
98 *
99 * @return string|void
100 */
101 protected function column_cb($item)
102 {
103
104 return '';
105 }
106
107 /** Text displayed when no stterm data is available */
108 public function no_items()
109 {
110 _e('No taxonomies found.', 'simple-tags');
111 }
112
113 /**
114 * Displays the search box.
115 *
116 * @param string $text The 'submit' button label.
117 * @param string $input_id ID attribute value for the search input field.
118 *
119 *
120 */
121 public function search_box($text, $input_id)
122 {
123 if (empty($_REQUEST['s']) && !$this->has_items()) {
124 return;
125 }
126
127 $input_id = $input_id . '-search-input';
128
129 if (!empty($_REQUEST['orderby'])) {
130 echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['orderby']))) . '" />';
131 }
132 if (!empty($_REQUEST['order'])) {
133 echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['order']))) . '" />';
134 }
135 if (!empty($_REQUEST['page'])) {
136 echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['page']))) . '" />';
137 }
138 if (!empty($_REQUEST['taxonomy_type'])) {
139 echo '<input type="hidden" name="taxonomy_type" value="' . esc_attr(sanitize_text_field(wp_unslash($_REQUEST['taxonomy_type']))) . '" />';
140 }
141 ?>
142 <p class="search-box">
143 <label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label>
144 <input type="search" id="<?php echo esc_attr($input_id); ?>" name="s"
145 value="<?php _admin_search_query(); ?>"/>
146 <?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?>
147 </p>
148 <?php
149 }
150
151 /**
152 * Sets up the items (roles) to list.
153 */
154 public function prepare_items()
155 {
156
157 $this->_column_headers = $this->get_column_info();
158
159 /**
160 * First, lets decide how many records per page to show
161 */
162 $per_page = $this->get_items_per_page('st_taxonomies_per_page', 20);
163
164 /**
165 * Fetch the data
166 */
167 $data = self::get_st_taxonomies();
168
169 /**
170 * Filter by post type if requested
171 */
172 $selected_post_type = isset($_GET['taxopress_taxonomy_post_type'])
173 ? sanitize_key(wp_unslash($_GET['taxopress_taxonomy_post_type']))
174 : '';
175
176 if (!empty($selected_post_type) && 'all' !== $selected_post_type) {
177 $data_filtered = [];
178 foreach ($data as $item) {
179 if (!empty($item->object_type) && is_array($item->object_type)) {
180 if (in_array($selected_post_type, $item->object_type, true)) {
181 $data_filtered[] = $item;
182 }
183 }
184 }
185 $data = $data_filtered;
186 }
187
188 /**
189 * Handle search
190 */
191 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field(wp_unslash($_REQUEST['s']))) {
192 $data_filtered = [];
193 foreach ($data as $item) {
194 if (
195 $this->str_contains($item->label, $search, false) || $this->str_contains(
196 $item->name,
197 $search,
198 false
199 )
200 ) {
201 $data_filtered[] = $item;
202 }
203 }
204 $data = $data_filtered;
205 }
206
207 /**
208 * This checks for sorting input and sorts the data in our array accordingly.
209 */
210 function usort_reorder($a, $b)
211 {
212 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field(wp_unslash($_REQUEST['orderby'])) : 'label'; //If no sort, default to role
213 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field(wp_unslash($_REQUEST['order'])) : 'asc'; //If no order, default to asc
214 $result = strnatcasecmp(
215 $a->$orderby,
216 $b->$orderby
217 ); //Determine sort order, case insensitive, natural order
218
219 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
220 }
221
222 usort($data, 'usort_reorder');
223
224 /**
225 * Pagination.
226 */
227 $current_page = $this->get_pagenum();
228 $total_items = count($data);
229
230
231 /**
232 * The WP_List_Table class does not handle pagination for us, so we need
233 * to ensure that the data is trimmed to only the current page. We can use
234 * array_slice() to
235 */
236 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
237
238 /**
239 * Now we can add the data to the items property, where it can be used by the rest of the class.
240 */
241 $this->items = $data;
242
243 /**
244 * We also have to register our pagination options & calculations.
245 */
246 $this->set_pagination_args([
247 'total_items' => $total_items, //calculate the total number of items
248 'per_page' => $per_page, //determine how many items to show on a page
249 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
250 ]);
251 }
252
253 /**
254 * Determine if a given string contains a given substring.
255 *
256 * @param string $haystack
257 * @param string|array $needles
258 * @param bool $sensitive Use case sensitive search
259 *
260 * @return bool
261 */
262 public function str_contains($haystack, $needles, $sensitive = true)
263 {
264 foreach ((array)$needles as $needle) {
265 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
266 if ($needle !== '' && $function($haystack, $needle) !== false) {
267 return true;
268 }
269 }
270
271 return false;
272 }
273
274 /**
275 * Columns to make sortable.
276 *
277 * @return array
278 */
279 protected function get_sortable_columns()
280 {
281 $sortable_columns = [
282 'name' => ['label', true],
283 'description' => ['description', true],
284 'registration_key' => ['name', true],
285 ];
286
287 return $sortable_columns;
288 }
289
290 /**
291 * Generates and display row actions links for the list table.
292 *
293 * @param object $item The item being acted upon.
294 * @param string $column_name Current column name.
295 * @param string $primary Primary column name.
296 *
297 * @return string The row actions HTML, or an empty string if the current column is the primary column.
298 */
299 protected function handle_row_actions($item, $column_name, $primary)
300 {
301 //Build row actions
302 $actions = [
303 'edit' => sprintf(
304 '<a href="%s">%s</a>',
305 add_query_arg(
306 [
307 'page' => 'st_taxonomies',
308 'add' => 'taxonomy',
309 'action' => 'edit',
310 'taxopress_taxonomy' => $item->name,
311 ],
312 taxopress_admin_url('admin.php')
313 ),
314 __('Edit', 'simple-tags')
315 ),
316 'view' => sprintf(
317 '<a href="%s">%s</a>',
318 add_query_arg(
319 [
320 'taxonomy' => $item->name,
321 ],
322 taxopress_admin_url('edit-tags.php')
323 ),
324 __('View', 'simple-tags')
325 ),
326 ];
327
328 return $column_name === $primary ? $this->row_actions($actions, false) : '';
329 }
330
331 /**
332 * Method for name column
333 *
334 * @param array $item
335 *
336 * @return string
337 */
338 protected function column_name($item)
339 {
340 $title = sprintf(
341 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
342 add_query_arg(
343 [
344 'page' => 'st_taxonomies',
345 'add' => 'taxonomy',
346 'action' => 'edit',
347 'taxopress_taxonomy' => $item->name,
348 ],
349 taxopress_admin_url('admin.php')
350 ),
351 esc_html($item->label)
352 );
353
354 return $title;
355 }
356
357 /**
358 * Method for registration_key column
359 *
360 * @param array $item
361 *
362 * @return string
363 */
364 protected function column_registration_key($item)
365 {
366 $title = sprintf(
367 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
368 add_query_arg(
369 [
370 'page' => 'st_taxonomies',
371 'add' => 'taxonomy',
372 'action' => 'edit',
373 'taxopress_taxonomy' => $item->name,
374 ],
375 taxopress_admin_url('admin.php')
376 ),
377 esc_html($item->name)
378 );
379
380 return $title;
381 }
382
383 /**
384 * The action column
385 *
386 * @param $item
387 *
388 * @return string
389 */
390 protected function column_description($item)
391 {
392 if (in_array($item->name, ['category', 'post_tag'])) {
393 return __('WordPress core', 'simple-tags');
394 } else {
395 if ($item->name === 'media_tag' || array_key_exists($item->name, taxopress_get_taxonomy_data())) {
396 $alt_description = __('TaxoPress', 'simple-tags');
397 } else {
398 $alt_description = '&mdash;';
399 }
400 return !empty($item->description) ? esc_html($item->description) : $alt_description;
401 }
402 }
403
404 /**
405 * The action column
406 *
407 * @param $item
408 *
409 * @return string
410 */
411 protected function column_active($item)
412 {
413
414 if (in_array($item->name, taxopress_get_deactivated_taxonomy())) {
415 return __('No', 'simple-tags');
416 } else {
417 return __('Yes', 'simple-tags');
418 }
419 }
420
421 /**
422 * The action column
423 *
424 * @param $item
425 *
426 * @return string
427 */
428 protected function column_posttypes($item)
429 {
430 $posttype = '';
431 $sn = 0;
432 foreach ($item->object_type as $objecttype) {
433 $sn++;
434 $post_type_object = get_post_type_object($objecttype);
435 if (is_object($post_type_object)) {
436 $posttype .= $post_type_object->label;
437 if ($sn < count($item->object_type)) {
438 $posttype .= ', ';
439 }
440 }
441 }
442
443 return $posttype;
444 }
445
446 /**
447 * Method for count column
448 *
449 * @param array $item
450 *
451 * @return string
452 */
453 protected function column_count($item)
454 {
455 $terms = get_terms(array('taxonomy' => $item->name, 'hide_empty' => false, 'fields' => 'ids'));
456 $title = sprintf(
457 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
458 add_query_arg(
459 [
460 'page' => 'st_terms',
461 'terms_filter_taxonomy' => $item->name
462 ],
463 taxopress_admin_url('admin.php')
464 ),
465 count($terms)
466 );
467
468 return $title;
469 }
470
471 /**
472 * The edited_with_taxopress column
473 *
474 * @param object $item
475 * @return string
476 */
477 protected function column_edited_with_taxopress($item)
478 {
479 $external_taxonomies = get_option('taxopress_external_taxonomies', array());
480 $taxopress_taxonomies = taxopress_get_taxonomy_data();
481
482 if ($item->name === 'media_tag' || array_key_exists($item->name, $taxopress_taxonomies) || array_key_exists($item->name, $external_taxonomies)) {
483 return '<div class="pp-tooltips-library" data-toggle="tooltip">
484 <span class="dashicons dashicons-yes-alt taxopress-edited-indicator taxopress-edited-yes"></span>
485 <div class="taxopress tooltip-text">This taxonomy has been edited with TaxoPress</div>
486 </div>';
487 } else {
488 return '<div class="pp-tooltips-library" data-toggle="tooltip">
489 <span class="dashicons dashicons-no-alt taxopress-edited-indicator taxopress-edited-no"></span>
490 <div class="taxopress tooltip-text">This taxonomy has not been edited with TaxoPress</div>
491 </div>';
492 }
493 }
494
495 protected function column_taxopress_order($item)
496 {
497 echo apply_filters('taxopress_order_column', $item);
498 }
499 }
500