[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * List Table API: WP_Links_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 links in a list table.
  12   *
  13   * @since 3.1.0
  14   *
  15   * @see WP_List_Table
  16   */
  17  class WP_Links_List_Table extends WP_List_Table {
  18  
  19      /**
  20       * Constructor.
  21       *
  22       * @since 3.1.0
  23       *
  24       * @see WP_List_Table::__construct() for more information on default arguments.
  25       *
  26       * @param array $args An associative array of arguments.
  27       */
  28  	public function __construct( $args = array() ) {
  29          parent::__construct(
  30              array(
  31                  'plural' => 'bookmarks',
  32                  'screen' => $args['screen'] ?? null,
  33              )
  34          );
  35      }
  36  
  37      /**
  38       * @return bool
  39       */
  40  	public function ajax_user_can() {
  41          return current_user_can( 'manage_links' );
  42      }
  43  
  44      /**
  45       * @global int    $cat_id  Link category ID.
  46       * @global string $s       Search string.
  47       * @global string $orderby The field to order the links by.
  48       * @global string $order   The direction to order the links.
  49       */
  50  	public function prepare_items() {
  51          global $cat_id, $s, $orderby, $order;
  52  
  53          $cat_id  = ! empty( $_REQUEST['cat_id'] ) ? absint( $_REQUEST['cat_id'] ) : 0;
  54          $orderby = ! empty( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : '';
  55          $order   = ! empty( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : '';
  56          $s       = ! empty( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '';
  57  
  58          $args = array(
  59              'hide_invisible' => 0,
  60              'hide_empty'     => 0,
  61          );
  62  
  63          if ( 'all' !== $cat_id ) {
  64              $args['category'] = $cat_id;
  65          }
  66          if ( ! empty( $s ) ) {
  67              $args['search'] = $s;
  68          }
  69          if ( ! empty( $orderby ) ) {
  70              $args['orderby'] = $orderby;
  71          }
  72          if ( ! empty( $order ) ) {
  73              $args['order'] = $order;
  74          }
  75  
  76          $this->items = get_bookmarks( $args );
  77      }
  78  
  79      /**
  80       * Displays the message for no items.
  81       */
  82  	public function no_items() {
  83          _e( 'No links found.' );
  84      }
  85  
  86      /**
  87       * Gets the list of bulk actions.
  88       *
  89       * @return array
  90       */
  91  	protected function get_bulk_actions() {
  92          $actions           = array();
  93          $actions['delete'] = __( 'Delete' );
  94  
  95          return $actions;
  96      }
  97  
  98      /**
  99       * @global int $cat_id Link category ID.
 100       * @param string $which The location: 'top' or 'bottom'.
 101       */
 102  	protected function extra_tablenav( $which ) {
 103          global $cat_id;
 104  
 105          if ( 'top' !== $which ) {
 106              return;
 107          }
 108          ?>
 109          <div class="alignleft actions">
 110              <?php
 111              $dropdown_options = array(
 112                  'selected'        => $cat_id,
 113                  'name'            => 'cat_id',
 114                  'taxonomy'        => 'link_category',
 115                  'show_option_all' => get_taxonomy( 'link_category' )->labels->all_items,
 116                  'hide_empty'      => true,
 117                  'hierarchical'    => 1,
 118                  'show_count'      => 0,
 119                  'orderby'         => 'name',
 120              );
 121  
 122              echo '<label class="screen-reader-text" for="cat_id">' . get_taxonomy( 'link_category' )->labels->filter_by_item . '</label>';
 123  
 124              wp_dropdown_categories( $dropdown_options );
 125  
 126              submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
 127              ?>
 128          </div>
 129          <?php
 130      }
 131  
 132      /**
 133       * @return string[] Array of column titles keyed by their column name.
 134       */
 135  	public function get_columns() {
 136          return array(
 137              'cb'         => '<input type="checkbox" />',
 138              'name'       => _x( 'Name', 'link name' ),
 139              'url'        => __( 'URL' ),
 140              'categories' => __( 'Categories' ),
 141              'rel'        => __( 'Relationship' ),
 142              'visible'    => __( 'Visible' ),
 143              'rating'     => __( 'Rating' ),
 144          );
 145      }
 146  
 147      /**
 148       * Gets the list of sortable columns.
 149       *
 150       * @return array
 151       */
 152  	protected function get_sortable_columns() {
 153          return array(
 154              'name'    => array( 'name', false, _x( 'Name', 'link name' ), __( 'Table ordered by Name.' ), 'asc' ),
 155              'url'     => array( 'url', false, __( 'URL' ), __( 'Table ordered by URL.' ) ),
 156              'visible' => array( 'visible', false, __( 'Visible' ), __( 'Table ordered by Visibility.' ) ),
 157              'rating'  => array( 'rating', false, __( 'Rating' ), __( 'Table ordered by Rating.' ) ),
 158          );
 159      }
 160  
 161      /**
 162       * Gets the name of the default primary column.
 163       *
 164       * @since 4.3.0
 165       *
 166       * @return string Name of the default primary column, in this case, 'name'.
 167       */
 168  	protected function get_default_primary_column_name() {
 169          return 'name';
 170      }
 171  
 172      /**
 173       * Handles the checkbox column output.
 174       *
 175       * @since 4.3.0
 176       * @since 5.9.0 Renamed `$link` to `$item` to match parent class for PHP 8 named parameter support.
 177       *
 178       * @param object $item The current link object.
 179       */
 180  	public function column_cb( $item ) {
 181          // Restores the more descriptive, specific name for use within this method.
 182          $link = $item;
 183  
 184          ?>
 185          <input type="checkbox" name="linkcheck[]" id="cb-select-<?php echo $link->link_id; ?>" value="<?php echo esc_attr( $link->link_id ); ?>" />
 186          <label for="cb-select-<?php echo $link->link_id; ?>">
 187              <span class="screen-reader-text">
 188              <?php
 189              /* translators: Hidden accessibility text. %s: Link name. */
 190              printf( __( 'Select %s' ), $link->link_name );
 191              ?>
 192              </span>
 193          </label>
 194          <?php
 195      }
 196  
 197      /**
 198       * Handles the link name column output.
 199       *
 200       * @since 4.3.0
 201       *
 202       * @param object $link The current link object.
 203       */
 204  	public function column_name( $link ) {
 205          $edit_link = get_edit_bookmark_link( $link );
 206          printf(
 207              '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong>',
 208              $edit_link,
 209              /* translators: %s: Link name. */
 210              esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $link->link_name ) ),
 211              $link->link_name
 212          );
 213      }
 214  
 215      /**
 216       * Handles the link URL column output.
 217       *
 218       * @since 4.3.0
 219       *
 220       * @param object $link The current link object.
 221       */
 222  	public function column_url( $link ) {
 223          $short_url = url_shorten( $link->link_url );
 224          echo "<a href='$link->link_url'>$short_url</a>";
 225      }
 226  
 227      /**
 228       * Handles the link categories column output.
 229       *
 230       * @since 4.3.0
 231       *
 232       * @global int $cat_id Link category ID.
 233       *
 234       * @param object $link The current link object.
 235       */
 236  	public function column_categories( $link ) {
 237          global $cat_id;
 238  
 239          $cat_names = array();
 240          foreach ( $link->link_category as $category ) {
 241              $cat = get_term( $category, 'link_category', OBJECT, 'display' );
 242              if ( is_wp_error( $cat ) ) {
 243                  echo $cat->get_error_message();
 244              }
 245              $cat_name = $cat->name;
 246              if ( (int) $cat_id !== $category ) {
 247                  $cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
 248              }
 249              $cat_names[] = $cat_name;
 250          }
 251          echo implode( ', ', $cat_names );
 252      }
 253  
 254      /**
 255       * Handles the link relation column output.
 256       *
 257       * @since 4.3.0
 258       *
 259       * @param object $link The current link object.
 260       */
 261  	public function column_rel( $link ) {
 262          echo empty( $link->link_rel ) ? '<br />' : $link->link_rel;
 263      }
 264  
 265      /**
 266       * Handles the link visibility column output.
 267       *
 268       * @since 4.3.0
 269       *
 270       * @param object $link The current link object.
 271       */
 272  	public function column_visible( $link ) {
 273          if ( 'Y' === $link->link_visible ) {
 274              _e( 'Yes' );
 275          } else {
 276              _e( 'No' );
 277          }
 278      }
 279  
 280      /**
 281       * Handles the link rating column output.
 282       *
 283       * @since 4.3.0
 284       *
 285       * @param object $link The current link object.
 286       */
 287  	public function column_rating( $link ) {
 288          echo $link->link_rating;
 289      }
 290  
 291      /**
 292       * Handles the default column output.
 293       *
 294       * @since 4.3.0
 295       * @since 5.9.0 Renamed `$link` to `$item` to match parent class for PHP 8 named parameter support.
 296       *
 297       * @param object $item        Link object.
 298       * @param string $column_name Current column name.
 299       */
 300  	public function column_default( $item, $column_name ) {
 301          // Restores the more descriptive, specific name for use within this method.
 302          $link = $item;
 303  
 304          /**
 305           * Fires for each registered custom link column.
 306           *
 307           * @since 2.1.0
 308           *
 309           * @param string $column_name Name of the custom column.
 310           * @param int    $link_id     Link ID.
 311           */
 312          do_action( 'manage_link_custom_column', $column_name, $link->link_id );
 313      }
 314  
 315      /**
 316       * Generates the list table rows.
 317       *
 318       * @since 3.1.0
 319       */
 320  	public function display_rows() {
 321          foreach ( $this->items as $link ) {
 322              $link                = sanitize_bookmark( $link );
 323              $link->link_name     = esc_attr( $link->link_name );
 324              $link->link_category = wp_get_link_cats( $link->link_id );
 325              ?>
 326          <tr id="link-<?php echo $link->link_id; ?>">
 327              <?php $this->single_row_columns( $link ); ?>
 328          </tr>
 329              <?php
 330          }
 331      }
 332  
 333      /**
 334       * Generates and displays row action links.
 335       *
 336       * @since 4.3.0
 337       * @since 5.9.0 Renamed `$link` to `$item` to match parent class for PHP 8 named parameter support.
 338       *
 339       * @param object $item        Link being acted upon.
 340       * @param string $column_name Current column name.
 341       * @param string $primary     Primary column name.
 342       * @return string Row actions output for links, or an empty string
 343       *                if the current column is not the primary column.
 344       */
 345  	protected function handle_row_actions( $item, $column_name, $primary ) {
 346          if ( $primary !== $column_name ) {
 347              return '';
 348          }
 349  
 350          // Restores the more descriptive, specific name for use within this method.
 351          $link = $item;
 352  
 353          $edit_link = get_edit_bookmark_link( $link );
 354  
 355          $actions           = array();
 356          $actions['edit']   = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
 357          $actions['delete'] = sprintf(
 358              '<a class="submitdelete" href="%s" onclick="return confirm( \'%s\' );">%s</a>',
 359              wp_nonce_url( "link.php?action=delete&amp;link_id=$link->link_id", 'delete-bookmark_' . $link->link_id ),
 360              /* translators: %s: Link name. */
 361              esc_js( sprintf( __( "You are about to delete this link '%s'\n  'Cancel' to stop, 'OK' to delete." ), $link->link_name ) ),
 362              __( 'Delete' )
 363          );
 364  
 365          return $this->row_actions( $actions );
 366      }
 367  }


Generated : Tue May 5 08:20:14 2026 Cross-referenced by PHPXref