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 / class-taxonomies-table.php

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

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