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