home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / includes / class-wp-links-list-table.php < prev    next >
Encoding:
PHP Script  |  2017-07-26  |  7.4 KB  |  322 lines

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