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

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

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