[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/includes/ -> class-wp-terms-list-table.php (source)

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


Generated: Mon Jul 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1