PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.40.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.40.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.40.0, at inc/class-taxonomies-table.php

473 lines 14.5 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 * Handle search
170 */
171 if ((!empty($_REQUEST['s'])) && $search = sanitize_text_field($_REQUEST['s'])) {
172 $data_filtered = [];
173 foreach ($data as $item) {
174 if ($this->str_contains($item->label, $search, false) || $this->str_contains($item->name, $search,
175 false)) {
176 $data_filtered[] = $item;
177 }
178 }
179 $data = $data_filtered;
180 }
181
182 /**
183 * This checks for sorting input and sorts the data in our array accordingly.
184 */
185 function usort_reorder($a, $b)
186 {
187 $orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'label'; //If no sort, default to role
188 $order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'asc'; //If no order, default to asc
189 $result = strnatcasecmp($a->$orderby,
190 $b->$orderby); //Determine sort order, case insensitive, natural order
191
192 return ($order === 'asc') ? $result : -$result; //Send final sort direction to usort
193 }
194
195 usort($data, 'usort_reorder');
196
197 /**
198 * Pagination.
199 */
200 $current_page = $this->get_pagenum();
201 $total_items = count($data);
202
203
204 /**
205 * The WP_List_Table class does not handle pagination for us, so we need
206 * to ensure that the data is trimmed to only the current page. We can use
207 * array_slice() to
208 */
209 $data = array_slice($data, (($current_page - 1) * $per_page), $per_page);
210
211 /**
212 * Now we can add the data to the items property, where it can be used by the rest of the class.
213 */
214 $this->items = $data;
215
216 /**
217 * We also have to register our pagination options & calculations.
218 */
219 $this->set_pagination_args([
220 'total_items' => $total_items, //calculate the total number of items
221 'per_page' => $per_page, //determine how many items to show on a page
222 'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages
223 ]);
224 }
225
226 /**
227 * Determine if a given string contains a given substring.
228 *
229 * @param string $haystack
230 * @param string|array $needles
231 * @param bool $sensitive Use case sensitive search
232 *
233 * @return bool
234 */
235 public function str_contains($haystack, $needles, $sensitive = true)
236 {
237 foreach ((array)$needles as $needle) {
238 $function = $sensitive ? 'mb_strpos' : 'mb_stripos';
239 if ($needle !== '' && $function($haystack, $needle) !== false) {
240 return true;
241 }
242 }
243
244 return false;
245 }
246
247 /**
248 * Columns to make sortable.
249 *
250 * @return array
251 */
252 protected function get_sortable_columns()
253 {
254 $sortable_columns = [
255 'name' => ['label', true],
256 'description' => ['description', true],
257 'registration_key' => ['name', true],
258 ];
259
260 return $sortable_columns;
261 }
262
263 /**
264 * Generates and display row actions links for the list table.
265 *
266 * @param object $item The item being acted upon.
267 * @param string $column_name Current column name.
268 * @param string $primary Primary column name.
269 *
270 * @return string The row actions HTML, or an empty string if the current column is the primary column.
271 */
272 protected function handle_row_actions($item, $column_name, $primary)
273 {
274 //Build row actions
275 $actions = [
276 'edit' => sprintf(
277 '<a href="%s">%s</a>',
278 add_query_arg(
279 [
280 'page' => 'st_taxonomies',
281 'add' => 'taxonomy',
282 'action' => 'edit',
283 'taxopress_taxonomy' => $item->name,
284 ],
285 taxopress_admin_url('admin.php')
286 ),
287 __('Edit', 'simple-tags')
288 ),
289 'view' => sprintf(
290 '<a href="%s">%s</a>',
291 add_query_arg(
292 [
293 'taxonomy' => $item->name,
294 ],
295 taxopress_admin_url('edit-tags.php')
296 ),
297 __('View', 'simple-tags')
298 ),
299 ];
300
301 return $column_name === $primary ? $this->row_actions($actions, false) : '';
302 }
303
304 /**
305 * Method for name column
306 *
307 * @param array $item
308 *
309 * @return string
310 */
311 protected function column_name($item)
312 {
313 $title = sprintf(
314 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
315 add_query_arg(
316 [
317 'page' => 'st_taxonomies',
318 'add' => 'taxonomy',
319 'action' => 'edit',
320 'taxopress_taxonomy' => $item->name,
321 ],
322 taxopress_admin_url('admin.php')
323 ),
324 esc_html($item->label)
325 );
326
327 return $title;
328 }
329
330 /**
331 * Method for registration_key column
332 *
333 * @param array $item
334 *
335 * @return string
336 */
337 protected function column_registration_key($item)
338 {
339 $title = sprintf(
340 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
341 add_query_arg(
342 [
343 'page' => 'st_taxonomies',
344 'add' => 'taxonomy',
345 'action' => 'edit',
346 'taxopress_taxonomy' => $item->name,
347 ],
348 taxopress_admin_url('admin.php')
349 ),
350 esc_html($item->name)
351 );
352
353 return $title;
354 }
355
356 /**
357 * The action column
358 *
359 * @param $item
360 *
361 * @return string
362 */
363 protected function column_description($item)
364 {
365 if (in_array($item->name, ['category', 'post_tag'])) {
366 return __('WordPress core', 'simple-tags');
367 } else {
368 if( $item->name === 'media_tag' || array_key_exists($item->name, taxopress_get_taxonomy_data())){
369 $alt_description = __('TaxoPress', 'simple-tags');
370 }else{
371 $alt_description = '&mdash;';
372 }
373 return !empty($item->description) ? esc_html($item->description) : $alt_description;
374 }
375 }
376
377 /**
378 * The action column
379 *
380 * @param $item
381 *
382 * @return string
383 */
384 protected function column_active($item)
385 {
386
387 if (in_array($item->name, taxopress_get_deactivated_taxonomy())) {
388 return __('No', 'simple-tags');
389 } else {
390 return __('Yes', 'simple-tags');
391 }
392 }
393
394 /**
395 * The action column
396 *
397 * @param $item
398 *
399 * @return string
400 */
401 protected function column_posttypes($item)
402 {
403 $posttype = '';
404 $sn = 0;
405 foreach ($item->object_type as $objecttype) {
406 $sn++;
407 $post_type_object = get_post_type_object($objecttype);
408 if(is_object($post_type_object)){
409 $posttype .= $post_type_object->label;
410 if ($sn < count($item->object_type)) {
411 $posttype .= ', ';
412 }
413 }
414 }
415
416 return $posttype;
417 }
418
419 /**
420 * Method for count column
421 *
422 * @param array $item
423 *
424 * @return string
425 */
426 protected function column_count($item)
427 {
428 $terms = get_terms($item->name, array('hide_empty' => false, 'fields' => 'ids'));
429 $title = sprintf(
430 '<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>',
431 add_query_arg(
432 [
433 'page' => 'st_terms',
434 'terms_filter_taxonomy' => $item->name
435 ],
436 taxopress_admin_url('admin.php')
437 ),
438 count($terms)
439 );
440
441 return $title;
442 }
443
444 /**
445 * The edited_with_taxopress column
446 *
447 * @param object $item
448 * @return string
449 */
450 protected function column_edited_with_taxopress($item)
451 {
452 $external_taxonomies = get_option('taxopress_external_taxonomies', array());
453 $taxopress_taxonomies = taxopress_get_taxonomy_data();
454
455 if ($item->name === 'media_tag' || array_key_exists($item->name, $taxopress_taxonomies) || array_key_exists($item->name, $external_taxonomies)) {
456 return '<div class="pp-tooltips-library" data-toggle="tooltip">
457 <span class="dashicons dashicons-yes-alt taxopress-edited-indicator taxopress-edited-yes"></span>
458 <div class="taxopress tooltip-text">This taxonomy has been edited with TaxoPress</div>
459 </div>';
460 } else {
461 return '<div class="pp-tooltips-library" data-toggle="tooltip">
462 <span class="dashicons dashicons-no-alt taxopress-edited-indicator taxopress-edited-no"></span>
463 <div class="taxopress tooltip-text">This taxonomy has not been edited with TaxoPress</div>
464 </div>';
465 }
466 }
467
468 protected function column_taxopress_order($item)
469 {
470 echo apply_filters('taxopress_order_column', $item);
471 }
472 }
473