| 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 Taxopress_Terms_List extends WP_List_Table |
| 7 |
{ |
| 8 |
|
| 9 |
/** Class constructor */ |
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
|
| 13 |
parent::__construct([ |
| 14 |
'singular' => esc_html__('Term', 'simple-tags'), //singular name of the listed records |
| 15 |
'plural' => esc_html__('Terms', 'simple-tags'), //plural name of the listed records |
| 16 |
'ajax' => false //does this table support ajax? |
| 17 |
]); |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
public function get_all_terms($count = false){ |
| 22 |
|
| 23 |
$taxonomies = array_keys(get_all_taxopress_taxonomies()); |
| 24 |
$search = (!empty($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : ''; |
| 25 |
|
| 26 |
$orderby = (!empty($_REQUEST['orderby'])) ? sanitize_text_field($_REQUEST['orderby']) : 'ID'; |
| 27 |
$order = (!empty($_REQUEST['order'])) ? sanitize_text_field($_REQUEST['order']) : 'desc'; |
| 28 |
$items_per_page = $this->get_items_per_page('st_Terms_per_page', 20); |
| 29 |
$page = $this->get_pagenum(); |
| 30 |
$offset = ($page - 1) * $items_per_page; |
| 31 |
|
| 32 |
|
| 33 |
$terms_attr = array ( |
| 34 |
'taxonomy' => $taxonomies, |
| 35 |
'orderby' => $orderby, |
| 36 |
'order' => $order, |
| 37 |
'search' => $search, |
| 38 |
'offset' => $offset, |
| 39 |
'hide_empty' => false, |
| 40 |
'include' => 'all', |
| 41 |
'number' => $items_per_page, |
| 42 |
'pad_counts' => true, |
| 43 |
'update_term_meta_cache' => true, |
| 44 |
); |
| 45 |
|
| 46 |
$terms = get_terms($terms_attr); |
| 47 |
|
| 48 |
if(empty($terms) || is_wp_error($terms)){ |
| 49 |
return []; |
| 50 |
} |
| 51 |
|
| 52 |
return $terms; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Retrieve st_Terms data from the database |
| 57 |
* |
| 58 |
* @param int $per_page |
| 59 |
* @param int $page_number |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function get_st_Terms() |
| 64 |
{ |
| 65 |
return $this->get_all_terms(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the count of records in the database. |
| 70 |
* |
| 71 |
* @return null|string |
| 72 |
*/ |
| 73 |
public static function record_count() |
| 74 |
{ |
| 75 |
$taxonomies = array_keys(get_all_taxopress_taxonomies()); |
| 76 |
$search = (!empty($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : ''; |
| 77 |
|
| 78 |
return wp_count_terms(['hide_empty' => false, 'taxonomy' => $taxonomies, 'search' => $search]); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Show single row item |
| 83 |
* |
| 84 |
* @param array $item |
| 85 |
*/ |
| 86 |
public function single_row($item) |
| 87 |
{ |
| 88 |
$class = ['st-terms-tr']; |
| 89 |
$id = 'st-terms-' . md5($item->term_id); |
| 90 |
echo sprintf('<tr id="%s" class="%s">', esc_attr($id), esc_attr(implode(' ', $class))); |
| 91 |
$this->single_row_columns($item); |
| 92 |
echo '</tr>'; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Associative array of columns |
| 97 |
* |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
function get_columns() |
| 101 |
{ |
| 102 |
$columns = [ |
| 103 |
'cb' => '<input type="checkbox" />', |
| 104 |
'name' => esc_html__('Title', 'simple-tags'), |
| 105 |
'slug' => esc_html__('Slug', 'simple-tags'), |
| 106 |
'taxonomy' => esc_html__('Taxonomy', 'simple-tags'), |
| 107 |
'posttypes' => esc_html__('Post Types', 'simple-tags'), |
| 108 |
'count' => esc_html__('Count', 'simple-tags') |
| 109 |
]; |
| 110 |
|
| 111 |
return $columns; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Columns to make sortable. |
| 116 |
* |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
protected function get_sortable_columns() |
| 120 |
{ |
| 121 |
$sortable_columns = [ |
| 122 |
'name' => ['name', true], |
| 123 |
'slug' => ['taxonomy', true], |
| 124 |
'taxonomy' => ['taxonomy', true], |
| 125 |
'count' => ['count', true], |
| 126 |
]; |
| 127 |
|
| 128 |
return $sortable_columns; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Render the bulk edit checkbox |
| 133 |
* |
| 134 |
* @param array $item |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
function column_cb( $item ) { |
| 139 |
return sprintf('<input type="checkbox" name="%1$s[]" value="%2$s" />', 'taxopress_terms', $item->term_id); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get the bulk actions to show in the top page dropdown |
| 144 |
* |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
protected function get_bulk_actions() |
| 148 |
{ |
| 149 |
$actions = [ |
| 150 |
'taxopress-terms-delete-terms' => esc_html__('Delete', 'simple-tags') |
| 151 |
]; |
| 152 |
|
| 153 |
return $actions; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Process bulk actions |
| 158 |
*/ |
| 159 |
public function process_bulk_action() |
| 160 |
{ |
| 161 |
|
| 162 |
$query_arg = '_wpnonce'; |
| 163 |
$action = 'bulk-' . $this->_args['plural']; |
| 164 |
$checked = isset($_REQUEST[$query_arg]) ? wp_verify_nonce(sanitize_key($_REQUEST[$query_arg]), $action) : false; |
| 165 |
|
| 166 |
if (!$checked || !current_user_can('simple_tags')) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
if($this->current_action() === 'taxopress-terms-delete-terms'){ |
| 171 |
$taxopress_terms = array_map('sanitize_text_field', (array)$_REQUEST['taxopress_terms']); |
| 172 |
if (!empty($taxopress_terms)) { |
| 173 |
foreach($taxopress_terms as $taxopress_term){ |
| 174 |
$term = get_term( $taxopress_term ); |
| 175 |
wp_delete_term( $term->term_id, $term->taxonomy ); |
| 176 |
} |
| 177 |
if(count($taxopress_terms) > 1){ |
| 178 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 179 |
echo taxopress_admin_notices_helper(esc_html__('Terms deleted successfully.', 'simple-tags'), false); |
| 180 |
}else{ |
| 181 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 182 |
echo taxopress_admin_notices_helper(esc_html__('Term deleted successfully.', 'simple-tags'), false); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Render a column when no column specific method exist. |
| 191 |
* |
| 192 |
* @param array $item |
| 193 |
* @param string $column_name |
| 194 |
* |
| 195 |
* @return mixed |
| 196 |
*/ |
| 197 |
public function column_default($item, $column_name) |
| 198 |
{ |
| 199 |
return !empty($item->$column_name) ? $item->$column_name : '—'; |
| 200 |
} |
| 201 |
|
| 202 |
/** Text displayed when no stterm data is available */ |
| 203 |
public function no_items() |
| 204 |
{ |
| 205 |
esc_html_e('No item avaliable.', 'simple-tags'); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Displays the search box. |
| 210 |
* |
| 211 |
* @param string $text The 'submit' button label. |
| 212 |
* @param string $input_id ID attribute value for the search input field. |
| 213 |
* |
| 214 |
* |
| 215 |
*/ |
| 216 |
public function search_box($text, $input_id) |
| 217 |
{ |
| 218 |
if (empty($_REQUEST['s']) && !$this->has_items()) { |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
$input_id = $input_id . '-search-input'; |
| 223 |
|
| 224 |
if (!empty($_REQUEST['orderby'])) { |
| 225 |
echo '<input type="hidden" name="orderby" value="' . esc_attr(sanitize_text_field($_REQUEST['orderby'])) . '" />'; |
| 226 |
} |
| 227 |
if (!empty($_REQUEST['order'])) { |
| 228 |
echo '<input type="hidden" name="order" value="' . esc_attr(sanitize_text_field($_REQUEST['order'])) . '" />'; |
| 229 |
} |
| 230 |
if (!empty($_REQUEST['page'])) { |
| 231 |
echo '<input type="hidden" name="page" value="' . esc_attr(sanitize_text_field($_REQUEST['page'])) . '" />'; |
| 232 |
} |
| 233 |
?> |
| 234 |
<p class="search-box"> |
| 235 |
<label class="screen-reader-text" for="<?php echo esc_attr($input_id); ?>"><?php echo esc_html($text); ?>:</label> |
| 236 |
<input type="search" id="<?php echo esc_attr($input_id); ?>" name="s" |
| 237 |
value="<?php _admin_search_query(); ?>"/> |
| 238 |
<?php submit_button($text, '', '', false, ['id' => 'search-submit']); ?> |
| 239 |
</p> |
| 240 |
<?php |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Sets up the items (roles) to list. |
| 245 |
*/ |
| 246 |
public function prepare_items() |
| 247 |
{ |
| 248 |
|
| 249 |
$this->_column_headers = $this->get_column_info(); |
| 250 |
$this->process_bulk_action(); |
| 251 |
|
| 252 |
/** |
| 253 |
* First, lets decide how many records per page to show |
| 254 |
*/ |
| 255 |
$per_page = $this->get_items_per_page('st_Terms_per_page', 20); |
| 256 |
|
| 257 |
/** |
| 258 |
* Fetch the data |
| 259 |
*/ |
| 260 |
$data = $this->get_st_Terms(); |
| 261 |
|
| 262 |
/** |
| 263 |
* Pagination. |
| 264 |
*/ |
| 265 |
$current_page = $this->get_pagenum(); |
| 266 |
$total_items = self::record_count(); |
| 267 |
|
| 268 |
/** |
| 269 |
* Now we can add the data to the items property, where it can be used by the rest of the class. |
| 270 |
*/ |
| 271 |
$this->items = $data; |
| 272 |
|
| 273 |
/** |
| 274 |
* We also have to register our pagination options & calculations. |
| 275 |
*/ |
| 276 |
$this->set_pagination_args([ |
| 277 |
'total_items' => $total_items, //calculate the total number of items |
| 278 |
'per_page' => $per_page, //determine how many items to show on a page |
| 279 |
'total_pages' => ceil($total_items / $per_page) //calculate the total number of pages |
| 280 |
]); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Generates and display row actions links for the list table. |
| 285 |
* |
| 286 |
* @param object $item The item being acted upon. |
| 287 |
* @param string $column_name Current column name. |
| 288 |
* @param string $primary Primary column name. |
| 289 |
* |
| 290 |
* @return string The row actions HTML, or an empty string if the current column is the primary column. |
| 291 |
*/ |
| 292 |
protected function handle_row_actions($item, $column_name, $primary) |
| 293 |
{ |
| 294 |
$taxonomy = get_taxonomy($item->taxonomy); |
| 295 |
|
| 296 |
//Build row actions |
| 297 |
$actions = [ |
| 298 |
'edit' => sprintf( |
| 299 |
'<a href="%s">%s</a>', |
| 300 |
add_query_arg( |
| 301 |
[ |
| 302 |
'taxonomy' => $item->taxonomy, |
| 303 |
'tag_ID' => $item->term_id, |
| 304 |
'post_type' => isset($taxonomy->object_type[0]) ? $taxonomy->object_type[0] : 'post', |
| 305 |
], |
| 306 |
admin_url('term.php') |
| 307 |
), |
| 308 |
esc_html__('Edit', 'simple-tags') |
| 309 |
), |
| 310 |
'delete' => sprintf( |
| 311 |
'<a href="%s" class="delete-terms">%s</a>', |
| 312 |
add_query_arg([ |
| 313 |
'page' => 'st_terms', |
| 314 |
'action' => 'taxopress-delete-terms', |
| 315 |
'taxopress_terms' => esc_attr($item->term_id), |
| 316 |
'_wpnonce' => wp_create_nonce('terms-action-request-nonce') |
| 317 |
], |
| 318 |
admin_url('admin.php')), |
| 319 |
esc_html__('Delete', 'simple-tags') |
| 320 |
), |
| 321 |
]; |
| 322 |
|
| 323 |
return $column_name === $primary ? $this->row_actions($actions, false) : ''; |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Method for name column |
| 328 |
* |
| 329 |
* @param array $item |
| 330 |
* |
| 331 |
* @return string |
| 332 |
*/ |
| 333 |
protected function column_name($item) |
| 334 |
{ |
| 335 |
$taxonomy = get_taxonomy($item->taxonomy); |
| 336 |
|
| 337 |
$title = sprintf( |
| 338 |
'<a href="%1$s"><strong><span class="row-title">%2$s</span></strong></a>', |
| 339 |
add_query_arg( |
| 340 |
[ |
| 341 |
'taxonomy' => $item->taxonomy, |
| 342 |
'tag_ID' => $item->term_id, |
| 343 |
'post_type' => isset($taxonomy->object_type[0]) ? $taxonomy->object_type[0] : 'post', |
| 344 |
], |
| 345 |
admin_url('term.php') |
| 346 |
), |
| 347 |
esc_html($item->name) |
| 348 |
); |
| 349 |
|
| 350 |
return $title; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* The action column |
| 355 |
* |
| 356 |
* @param $item |
| 357 |
* |
| 358 |
* @return string |
| 359 |
*/ |
| 360 |
protected function column_slug($item) |
| 361 |
{ |
| 362 |
return !empty($item->slug) ? $item->slug : '—'; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* The action column |
| 367 |
* |
| 368 |
* @param $item |
| 369 |
* |
| 370 |
* @return string |
| 371 |
*/ |
| 372 |
protected function column_posttypes($item) |
| 373 |
{ |
| 374 |
$posttype = ''; |
| 375 |
$sn = 0; |
| 376 |
$taxonomy = get_taxonomy($item->taxonomy); |
| 377 |
foreach ($taxonomy->object_type as $objecttype) { |
| 378 |
$sn++; |
| 379 |
$post_type_object = get_post_type_object($objecttype); |
| 380 |
if(is_object($post_type_object)){ |
| 381 |
$posttype .= $post_type_object->label; |
| 382 |
if ($sn < count($taxonomy->object_type)) { |
| 383 |
$posttype .= ', '; |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
return $posttype; |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* The action column |
| 393 |
* |
| 394 |
* @param $item |
| 395 |
* |
| 396 |
* @return string |
| 397 |
*/ |
| 398 |
protected function column_count($item) |
| 399 |
{ |
| 400 |
|
| 401 |
$taxonomy = get_taxonomy($item->taxonomy); |
| 402 |
|
| 403 |
if($taxonomy->query_var){ |
| 404 |
return sprintf('<a href="%s" class="">%s</a>', |
| 405 |
add_query_arg( |
| 406 |
[ |
| 407 |
$taxonomy->query_var => esc_attr($item->slug), |
| 408 |
'post_type' => isset($taxonomy->object_type[0]) ? $taxonomy->object_type[0] : 'post', |
| 409 |
], |
| 410 |
admin_url('edit.php') |
| 411 |
), |
| 412 |
number_format_i18n($item->count)); |
| 413 |
}else{ |
| 414 |
return number_format_i18n($item->count); |
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Method for taxonomy column |
| 421 |
* |
| 422 |
* @param array $item |
| 423 |
* |
| 424 |
* @return string |
| 425 |
*/ |
| 426 |
protected function column_taxonomy($item) |
| 427 |
{ |
| 428 |
$taxonomy = get_taxonomy($item->taxonomy); |
| 429 |
|
| 430 |
if($taxonomy){ |
| 431 |
$return = $taxonomy->labels->name; |
| 432 |
}else{ |
| 433 |
$return = '—'; |
| 434 |
} |
| 435 |
|
| 436 |
return $return; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
} |
| 441 |
|