class-wp-terms-list-table.php (19287B)
1 <?php 2 /** 3 * List Table API: WP_Terms_List_Table class 4 * 5 * @package WordPress 6 * @subpackage Administration 7 * @since 3.1.0 8 */ 9 10 /** 11 * Core class used to implement displaying terms in a list table. 12 * 13 * @since 3.1.0 14 * @access private 15 * 16 * @see WP_List_Table 17 */ 18 class WP_Terms_List_Table extends WP_List_Table { 19 20 public $callback_args; 21 22 private $level; 23 24 /** 25 * Constructor. 26 * 27 * @since 3.1.0 28 * 29 * @see WP_List_Table::__construct() for more information on default arguments. 30 * 31 * @global string $post_type 32 * @global string $taxonomy 33 * @global string $action 34 * @global object $tax 35 * 36 * @param array $args An associative array of arguments. 37 */ 38 public function __construct( $args = array() ) { 39 global $post_type, $taxonomy, $action, $tax; 40 41 parent::__construct( 42 array( 43 'plural' => 'tags', 44 'singular' => 'tag', 45 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, 46 ) 47 ); 48 49 $action = $this->screen->action; 50 $post_type = $this->screen->post_type; 51 $taxonomy = $this->screen->taxonomy; 52 53 if ( empty( $taxonomy ) ) { 54 $taxonomy = 'post_tag'; 55 } 56 57 if ( ! taxonomy_exists( $taxonomy ) ) { 58 wp_die( __( 'Invalid taxonomy.' ) ); 59 } 60 61 $tax = get_taxonomy( $taxonomy ); 62 63 // @todo Still needed? Maybe just the show_ui part. 64 if ( empty( $post_type ) || ! in_array( $post_type, get_post_types( array( 'show_ui' => true ) ), true ) ) { 65 $post_type = 'post'; 66 } 67 68 } 69 70 /** 71 * @return bool 72 */ 73 public function ajax_user_can() { 74 return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms ); 75 } 76 77 /** 78 */ 79 public function prepare_items() { 80 $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' ); 81 82 if ( 'post_tag' === $this->screen->taxonomy ) { 83 /** 84 * Filters the number of terms displayed per page for the Tags list table. 85 * 86 * @since 2.8.0 87 * 88 * @param int $tags_per_page Number of tags to be displayed. Default 20. 89 */ 90 $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page ); 91 92 /** 93 * Filters the number of terms displayed per page for the Tags list table. 94 * 95 * @since 2.7.0 96 * @deprecated 2.8.0 Use {@see 'edit_tags_per_page'} instead. 97 * 98 * @param int $tags_per_page Number of tags to be displayed. Default 20. 99 */ 100 $tags_per_page = apply_filters_deprecated( 'tagsperpage', array( $tags_per_page ), '2.8.0', 'edit_tags_per_page' ); 101 } elseif ( 'category' === $this->screen->taxonomy ) { 102 /** 103 * Filters the number of terms displayed per page for the Categories list table. 104 * 105 * @since 2.8.0 106 * 107 * @param int $tags_per_page Number of categories to be displayed. Default 20. 108 */ 109 $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); 110 } 111 112 $search = ! empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : ''; 113 114 $args = array( 115 'search' => $search, 116 'page' => $this->get_pagenum(), 117 'number' => $tags_per_page, 118 ); 119 120 if ( ! empty( $_REQUEST['orderby'] ) ) { 121 $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) ); 122 } 123 124 if ( ! empty( $_REQUEST['order'] ) ) { 125 $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) ); 126 } 127 128 $this->callback_args = $args; 129 130 $this->set_pagination_args( 131 array( 132 'total_items' => wp_count_terms( 133 array( 134 'taxonomy' => $this->screen->taxonomy, 135 'search' => $search, 136 ) 137 ), 138 'per_page' => $tags_per_page, 139 ) 140 ); 141 } 142 143 /** 144 * @return bool 145 */ 146 public function has_items() { 147 // @todo Populate $this->items in prepare_items(). 148 return true; 149 } 150 151 /** 152 */ 153 public function no_items() { 154 echo get_taxonomy( $this->screen->taxonomy )->labels->not_found; 155 } 156 157 /** 158 * @return array 159 */ 160 protected function get_bulk_actions() { 161 $actions = array(); 162 163 if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) ) { 164 $actions['delete'] = __( 'Delete' ); 165 } 166 167 return $actions; 168 } 169 170 /** 171 * @return string 172 */ 173 public function current_action() { 174 if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && 'delete' === $_REQUEST['action'] ) { 175 return 'bulk-delete'; 176 } 177 178 return parent::current_action(); 179 } 180 181 /** 182 * @return array 183 */ 184 public function get_columns() { 185 $columns = array( 186 'cb' => '<input type="checkbox" />', 187 'name' => _x( 'Name', 'term name' ), 188 'description' => __( 'Description' ), 189 'slug' => __( 'Slug' ), 190 ); 191 192 if ( 'link_category' === $this->screen->taxonomy ) { 193 $columns['links'] = __( 'Links' ); 194 } else { 195 $columns['posts'] = _x( 'Count', 'Number/count of items' ); 196 } 197 198 return $columns; 199 } 200 201 /** 202 * @return array 203 */ 204 protected function get_sortable_columns() { 205 return array( 206 'name' => 'name', 207 'description' => 'description', 208 'slug' => 'slug', 209 'posts' => 'count', 210 'links' => 'count', 211 ); 212 } 213 214 /** 215 */ 216 public function display_rows_or_placeholder() { 217 $taxonomy = $this->screen->taxonomy; 218 219 $args = wp_parse_args( 220 $this->callback_args, 221 array( 222 'taxonomy' => $taxonomy, 223 'page' => 1, 224 'number' => 20, 225 'search' => '', 226 'hide_empty' => 0, 227 ) 228 ); 229 230 $page = $args['page']; 231 232 // Set variable because $args['number'] can be subsequently overridden. 233 $number = $args['number']; 234 235 $offset = ( $page - 1 ) * $number; 236 $args['offset'] = $offset; 237 238 // Convert it to table rows. 239 $count = 0; 240 241 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { 242 // We'll need the full set of terms then. 243 $args['number'] = 0; 244 $args['offset'] = $args['number']; 245 } 246 247 $terms = get_terms( $args ); 248 249 if ( empty( $terms ) || ! is_array( $terms ) ) { 250 echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">'; 251 $this->no_items(); 252 echo '</td></tr>'; 253 return; 254 } 255 256 if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) { 257 if ( ! empty( $args['search'] ) ) {// Ignore children on searches. 258 $children = array(); 259 } else { 260 $children = _get_term_hierarchy( $taxonomy ); 261 } 262 263 /* 264 * Some funky recursion to get the job done (paging & parents mainly) is contained within. 265 * Skip it for non-hierarchical taxonomies for performance sake. 266 */ 267 $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count ); 268 } else { 269 foreach ( $terms as $term ) { 270 $this->single_row( $term ); 271 } 272 } 273 } 274 275 /** 276 * @param string $taxonomy 277 * @param array $terms 278 * @param array $children 279 * @param int $start 280 * @param int $per_page 281 * @param int $count 282 * @param int $parent 283 * @param int $level 284 */ 285 private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) { 286 287 $end = $start + $per_page; 288 289 foreach ( $terms as $key => $term ) { 290 291 if ( $count >= $end ) { 292 break; 293 } 294 295 if ( $term->parent !== $parent && empty( $_REQUEST['s'] ) ) { 296 continue; 297 } 298 299 // If the page starts in a subtree, print the parents. 300 if ( $count === $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) { 301 $my_parents = array(); 302 $parent_ids = array(); 303 $p = $term->parent; 304 305 while ( $p ) { 306 $my_parent = get_term( $p, $taxonomy ); 307 $my_parents[] = $my_parent; 308 $p = $my_parent->parent; 309 310 if ( in_array( $p, $parent_ids, true ) ) { // Prevent parent loops. 311 break; 312 } 313 314 $parent_ids[] = $p; 315 } 316 317 unset( $parent_ids ); 318 319 $num_parents = count( $my_parents ); 320 321 while ( $my_parent = array_pop( $my_parents ) ) { 322 echo "\t"; 323 $this->single_row( $my_parent, $level - $num_parents ); 324 $num_parents--; 325 } 326 } 327 328 if ( $count >= $start ) { 329 echo "\t"; 330 $this->single_row( $term, $level ); 331 } 332 333 ++$count; 334 335 unset( $terms[ $key ] ); 336 337 if ( isset( $children[ $term->term_id ] ) && empty( $_REQUEST['s'] ) ) { 338 $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 ); 339 } 340 } 341 } 342 343 /** 344 * @global string $taxonomy 345 * @param WP_Term $tag Term object. 346 * @param int $level 347 */ 348 public function single_row( $tag, $level = 0 ) { 349 global $taxonomy; 350 $tag = sanitize_term( $tag, $taxonomy ); 351 352 $this->level = $level; 353 354 if ( $tag->parent ) { 355 $count = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) ); 356 $level = 'level-' . $count; 357 } else { 358 $level = 'level-0'; 359 } 360 361 echo '<tr id="tag-' . $tag->term_id . '" class="' . $level . '">'; 362 $this->single_row_columns( $tag ); 363 echo '</tr>'; 364 } 365 366 /** 367 * @param WP_Term $tag Term object. 368 * @return string 369 */ 370 public function column_cb( $tag ) { 371 if ( current_user_can( 'delete_term', $tag->term_id ) ) { 372 return sprintf( 373 '<label class="screen-reader-text" for="cb-select-%1$s">%2$s</label>' . 374 '<input type="checkbox" name="delete_tags[]" value="%1$s" id="cb-select-%1$s" />', 375 $tag->term_id, 376 /* translators: %s: Taxonomy term name. */ 377 sprintf( __( 'Select %s' ), $tag->name ) 378 ); 379 } 380 381 return ' '; 382 } 383 384 /** 385 * @param WP_Term $tag Term object. 386 * @return string 387 */ 388 public function column_name( $tag ) { 389 $taxonomy = $this->screen->taxonomy; 390 391 $pad = str_repeat( '— ', max( 0, $this->level ) ); 392 393 /** 394 * Filters display of the term name in the terms list table. 395 * 396 * The default output may include padding due to the term's 397 * current level in the term hierarchy. 398 * 399 * @since 2.5.0 400 * 401 * @see WP_Terms_List_Table::column_name() 402 * 403 * @param string $pad_tag_name The term name, padded if not top-level. 404 * @param WP_Term $tag Term object. 405 */ 406 $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag ); 407 408 $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' ); 409 410 $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI']; 411 412 $edit_link = get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ); 413 414 if ( $edit_link ) { 415 $edit_link = add_query_arg( 416 'wp_http_referer', 417 urlencode( wp_unslash( $uri ) ), 418 $edit_link 419 ); 420 $name = sprintf( 421 '<a class="row-title" href="%s" aria-label="%s">%s</a>', 422 esc_url( $edit_link ), 423 /* translators: %s: Taxonomy term name. */ 424 esc_attr( sprintf( __( '“%s” (Edit)' ), $tag->name ) ), 425 $name 426 ); 427 } 428 429 $out = sprintf( 430 '<strong>%s</strong><br />', 431 $name 432 ); 433 434 $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">'; 435 $out .= '<div class="name">' . $qe_data->name . '</div>'; 436 437 /** This filter is documented in wp-admin/edit-tag-form.php */ 438 $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '</div>'; 439 $out .= '<div class="parent">' . $qe_data->parent . '</div></div>'; 440 441 return $out; 442 } 443 444 /** 445 * Gets the name of the default primary column. 446 * 447 * @since 4.3.0 448 * 449 * @return string Name of the default primary column, in this case, 'name'. 450 */ 451 protected function get_default_primary_column_name() { 452 return 'name'; 453 } 454 455 /** 456 * Generates and displays row action links. 457 * 458 * @since 4.3.0 459 * 460 * @param WP_Term $tag Tag being acted upon. 461 * @param string $column_name Current column name. 462 * @param string $primary Primary column name. 463 * @return string Row actions output for terms, or an empty string 464 * if the current column is not the primary column. 465 */ 466 protected function handle_row_actions( $tag, $column_name, $primary ) { 467 if ( $primary !== $column_name ) { 468 return ''; 469 } 470 471 $taxonomy = $this->screen->taxonomy; 472 $tax = get_taxonomy( $taxonomy ); 473 $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI']; 474 475 $edit_link = add_query_arg( 476 'wp_http_referer', 477 urlencode( wp_unslash( $uri ) ), 478 get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) 479 ); 480 481 $actions = array(); 482 483 if ( current_user_can( 'edit_term', $tag->term_id ) ) { 484 $actions['edit'] = sprintf( 485 '<a href="%s" aria-label="%s">%s</a>', 486 esc_url( $edit_link ), 487 /* translators: %s: Taxonomy term name. */ 488 esc_attr( sprintf( __( 'Edit “%s”' ), $tag->name ) ), 489 __( 'Edit' ) 490 ); 491 $actions['inline hide-if-no-js'] = sprintf( 492 '<button type="button" class="button-link editinline" aria-label="%s" aria-expanded="false">%s</button>', 493 /* translators: %s: Taxonomy term name. */ 494 esc_attr( sprintf( __( 'Quick edit “%s” inline' ), $tag->name ) ), 495 __( 'Quick Edit' ) 496 ); 497 } 498 499 if ( current_user_can( 'delete_term', $tag->term_id ) ) { 500 $actions['delete'] = sprintf( 501 '<a href="%s" class="delete-tag aria-button-if-js" aria-label="%s">%s</a>', 502 wp_nonce_url( "edit-tags.php?action=delete&taxonomy=$taxonomy&tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ), 503 /* translators: %s: Taxonomy term name. */ 504 esc_attr( sprintf( __( 'Delete “%s”' ), $tag->name ) ), 505 __( 'Delete' ) 506 ); 507 } 508 509 if ( is_taxonomy_viewable( $tax ) ) { 510 $actions['view'] = sprintf( 511 '<a href="%s" aria-label="%s">%s</a>', 512 get_term_link( $tag ), 513 /* translators: %s: Taxonomy term name. */ 514 esc_attr( sprintf( __( 'View “%s” archive' ), $tag->name ) ), 515 __( 'View' ) 516 ); 517 } 518 519 /** 520 * Filters the action links displayed for each term in the Tags list table. 521 * 522 * @since 2.8.0 523 * @since 3.0.0 Deprecated in favor of {@see '{$taxonomy}_row_actions'} filter. 524 * @since 5.4.2 Restored (un-deprecated). 525 * 526 * @param string[] $actions An array of action links to be displayed. Default 527 * 'Edit', 'Quick Edit', 'Delete', and 'View'. 528 * @param WP_Term $tag Term object. 529 */ 530 $actions = apply_filters( 'tag_row_actions', $actions, $tag ); 531 532 /** 533 * Filters the action links displayed for each term in the terms list table. 534 * 535 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. 536 * 537 * Possible hook names include: 538 * 539 * - `category_row_actions` 540 * - `post_tag_row_actions` 541 * 542 * @since 3.0.0 543 * 544 * @param string[] $actions An array of action links to be displayed. Default 545 * 'Edit', 'Quick Edit', 'Delete', and 'View'. 546 * @param WP_Term $tag Term object. 547 */ 548 $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag ); 549 550 return $this->row_actions( $actions ); 551 } 552 553 /** 554 * @param WP_Term $tag Term object. 555 * @return string 556 */ 557 public function column_description( $tag ) { 558 if ( $tag->description ) { 559 return $tag->description; 560 } else { 561 return '<span aria-hidden="true">—</span><span class="screen-reader-text">' . __( 'No description' ) . '</span>'; 562 } 563 } 564 565 /** 566 * @param WP_Term $tag Term object. 567 * @return string 568 */ 569 public function column_slug( $tag ) { 570 /** This filter is documented in wp-admin/edit-tag-form.php */ 571 return apply_filters( 'editable_slug', $tag->slug, $tag ); 572 } 573 574 /** 575 * @param WP_Term $tag Term object. 576 * @return string 577 */ 578 public function column_posts( $tag ) { 579 $count = number_format_i18n( $tag->count ); 580 581 $tax = get_taxonomy( $this->screen->taxonomy ); 582 583 $ptype_object = get_post_type_object( $this->screen->post_type ); 584 if ( ! $ptype_object->show_ui ) { 585 return $count; 586 } 587 588 if ( $tax->query_var ) { 589 $args = array( $tax->query_var => $tag->slug ); 590 } else { 591 $args = array( 592 'taxonomy' => $tax->name, 593 'term' => $tag->slug, 594 ); 595 } 596 597 if ( 'post' !== $this->screen->post_type ) { 598 $args['post_type'] = $this->screen->post_type; 599 } 600 601 if ( 'attachment' === $this->screen->post_type ) { 602 return "<a href='" . esc_url( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>"; 603 } 604 605 return "<a href='" . esc_url( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>"; 606 } 607 608 /** 609 * @param WP_Term $tag Term object. 610 * @return string 611 */ 612 public function column_links( $tag ) { 613 $count = number_format_i18n( $tag->count ); 614 615 if ( $count ) { 616 $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>"; 617 } 618 619 return $count; 620 } 621 622 /** 623 * @param WP_Term $tag Term object. 624 * @param string $column_name Name of the column. 625 * @return string 626 */ 627 public function column_default( $tag, $column_name ) { 628 /** 629 * Filters the displayed columns in the terms list table. 630 * 631 * The dynamic portion of the hook name, `$this->screen->taxonomy`, 632 * refers to the slug of the current taxonomy. 633 * 634 * Possible hook names include: 635 * 636 * - `manage_category_custom_column` 637 * - `manage_post_tag_custom_column` 638 * 639 * @since 2.8.0 640 * 641 * @param string $string Blank string. 642 * @param string $column_name Name of the column. 643 * @param int $term_id Term ID. 644 */ 645 return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id ); 646 } 647 648 /** 649 * Outputs the hidden row displayed when inline editing 650 * 651 * @since 3.1.0 652 */ 653 public function inline_edit() { 654 $tax = get_taxonomy( $this->screen->taxonomy ); 655 656 if ( ! current_user_can( $tax->cap->edit_terms ) ) { 657 return; 658 } 659 ?> 660 661 <form method="get"> 662 <table style="display: none"><tbody id="inlineedit"> 663 664 <tr id="inline-edit" class="inline-edit-row" style="display: none"> 665 <td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange"> 666 667 <fieldset> 668 <legend class="inline-edit-legend"><?php _e( 'Quick Edit' ); ?></legend> 669 <div class="inline-edit-col"> 670 <label> 671 <span class="title"><?php _ex( 'Name', 'term name' ); ?></span> 672 <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span> 673 </label> 674 675 <?php if ( ! global_terms_enabled() ) : ?> 676 <label> 677 <span class="title"><?php _e( 'Slug' ); ?></span> 678 <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span> 679 </label> 680 <?php endif; ?> 681 </div> 682 </fieldset> 683 684 <?php 685 $core_columns = array( 686 'cb' => true, 687 'description' => true, 688 'name' => true, 689 'slug' => true, 690 'posts' => true, 691 ); 692 693 list( $columns ) = $this->get_column_info(); 694 695 foreach ( $columns as $column_name => $column_display_name ) { 696 if ( isset( $core_columns[ $column_name ] ) ) { 697 continue; 698 } 699 700 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */ 701 do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy ); 702 } 703 ?> 704 705 <div class="inline-edit-save submit"> 706 <button type="button" class="cancel button alignleft"><?php _e( 'Cancel' ); ?></button> 707 <button type="button" class="save button button-primary alignright"><?php echo $tax->labels->update_item; ?></button> 708 <span class="spinner"></span> 709 710 <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?> 711 <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" /> 712 <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" /> 713 <br class="clear" /> 714 715 <div class="notice notice-error notice-alt inline hidden"> 716 <p class="error"></p> 717 </div> 718 </div> 719 720 </td></tr> 721 722 </tbody></table> 723 </form> 724 <?php 725 } 726 }