WordPress 4.0
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-links-list-table.php
1 <?php
2 /**
3  * Links Manager List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_Links_List_Table extends WP_List_Table {
11
12         /**
13          * Constructor.
14          *
15          * @since 3.1.0
16          * @access public
17          *
18          * @see WP_List_Table::__construct() for more information on default arguments.
19          *
20          * @param array $args An associative array of arguments.
21          */
22         public function __construct( $args = array() ) {
23                 parent::__construct( array(
24                         'plural' => 'bookmarks',
25                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
26                 ) );
27         }
28
29         public function ajax_user_can() {
30                 return current_user_can( 'manage_links' );
31         }
32
33         public function prepare_items() {
34                 global $cat_id, $s, $orderby, $order;
35
36                 wp_reset_vars( array( 'action', 'cat_id', 'link_id', 'orderby', 'order', 's' ) );
37
38                 $args = array( 'hide_invisible' => 0, 'hide_empty' => 0 );
39
40                 if ( 'all' != $cat_id )
41                         $args['category'] = $cat_id;
42                 if ( !empty( $s ) )
43                         $args['search'] = $s;
44                 if ( !empty( $orderby ) )
45                         $args['orderby'] = $orderby;
46                 if ( !empty( $order ) )
47                         $args['order'] = $order;
48
49                 $this->items = get_bookmarks( $args );
50         }
51
52         public function no_items() {
53                 _e( 'No links found.' );
54         }
55
56         protected function get_bulk_actions() {
57                 $actions = array();
58                 $actions['delete'] = __( 'Delete' );
59
60                 return $actions;
61         }
62
63         protected function extra_tablenav( $which ) {
64                 global $cat_id;
65
66                 if ( 'top' != $which )
67                         return;
68 ?>
69                 <div class="alignleft actions">
70 <?php
71                         $dropdown_options = array(
72                                 'selected' => $cat_id,
73                                 'name' => 'cat_id',
74                                 'taxonomy' => 'link_category',
75                                 'show_option_all' => __( 'View all categories' ),
76                                 'hide_empty' => true,
77                                 'hierarchical' => 1,
78                                 'show_count' => 0,
79                                 'orderby' => 'name',
80                         );
81                         wp_dropdown_categories( $dropdown_options );
82                         submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
83 ?>
84                 </div>
85 <?php
86         }
87
88         public function get_columns() {
89                 return array(
90                         'cb'         => '<input type="checkbox" />',
91                         'name'       => _x( 'Name', 'link name' ),
92                         'url'        => __( 'URL' ),
93                         'categories' => __( 'Categories' ),
94                         'rel'        => __( 'Relationship' ),
95                         'visible'    => __( 'Visible' ),
96                         'rating'     => __( 'Rating' )
97                 );
98         }
99
100         protected function get_sortable_columns() {
101                 return array(
102                         'name'    => 'name',
103                         'url'     => 'url',
104                         'visible' => 'visible',
105                         'rating'  => 'rating'
106                 );
107         }
108
109         public function display_rows() {
110                 global $cat_id;
111
112                 $alt = 0;
113
114                 foreach ( $this->items as $link ) {
115                         $link = sanitize_bookmark( $link );
116                         $link->link_name = esc_attr( $link->link_name );
117                         $link->link_category = wp_get_link_cats( $link->link_id );
118
119                         $short_url = url_shorten( $link->link_url );
120
121                         $visible = ( $link->link_visible == 'Y' ) ? __( 'Yes' ) : __( 'No' );
122                         $rating  = $link->link_rating;
123                         $style = ( $alt++ % 2 ) ? '' : ' class="alternate"';
124
125                         $edit_link = get_edit_bookmark_link( $link );
126 ?>
127                 <tr id="link-<?php echo $link->link_id; ?>" <?php echo $style; ?>>
128 <?php
129
130                         list( $columns, $hidden ) = $this->get_column_info();
131
132                         foreach ( $columns as $column_name => $column_display_name ) {
133                                 $class = "class='column-$column_name'";
134
135                                 $style = '';
136                                 if ( in_array( $column_name, $hidden ) )
137                                         $style = ' style="display:none;"';
138
139                                 $attributes = $class . $style;
140
141                                 switch ( $column_name ) {
142                                         case 'cb': ?>
143                                                 <th scope="row" class="check-column">
144                                                         <label class="screen-reader-text" for="cb-select-<?php echo $link->link_id; ?>"><?php echo sprintf( __( 'Select %s' ), $link->link_name ); ?></label>
145                                                         <input type="checkbox" name="linkcheck[]" id="cb-select-<?php echo $link->link_id; ?>" value="<?php echo esc_attr( $link->link_id ); ?>" />
146                                                 </th>
147                                                 <?php
148                                                 break;
149
150                                         case 'name':
151                                                 echo "<td $attributes><strong><a class='row-title' href='$edit_link' title='" . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $link->link_name ) ) . "'>$link->link_name</a></strong><br />";
152
153                                                 $actions = array();
154                                                 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
155                                                 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "link.php?action=delete&amp;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>";
156                                                 echo $this->row_actions( $actions );
157
158                                                 echo '</td>';
159                                                 break;
160                                         case 'url':
161                                                 echo "<td $attributes><a href='$link->link_url' title='". esc_attr( sprintf( __( 'Visit %s' ), $link->link_name ) )."'>$short_url</a></td>";
162                                                 break;
163                                         case 'categories':
164                                                 ?><td <?php echo $attributes ?>><?php
165                                                 $cat_names = array();
166                                                 foreach ( $link->link_category as $category ) {
167                                                         $cat = get_term( $category, 'link_category', OBJECT, 'display' );
168                                                         if ( is_wp_error( $cat ) )
169                                                                 echo $cat->get_error_message();
170                                                         $cat_name = $cat->name;
171                                                         if ( $cat_id != $category )
172                                                                 $cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
173                                                         $cat_names[] = $cat_name;
174                                                 }
175                                                 echo implode( ', ', $cat_names );
176                                                 ?></td><?php
177                                                 break;
178                                         case 'rel':
179                                                 ?><td <?php echo $attributes ?>><?php echo empty( $link->link_rel ) ? '<br />' : $link->link_rel; ?></td><?php
180                                                 break;
181                                         case 'visible':
182                                                 ?><td <?php echo $attributes ?>><?php echo $visible; ?></td><?php
183                                                 break;
184                                         case 'rating':
185                                                 ?><td <?php echo $attributes ?>><?php echo $rating; ?></td><?php
186                                                 break;
187                                         default:
188                                                 /**
189                                                  * Fires for each registered custom link column.
190                                                  *
191                                                  * @since 2.1.0
192                                                  *
193                                                  * @param string $column_name Name of the custom column.
194                                                  * @param int    $link_id     Link ID.
195                                                  */
196                                                 ?>
197                                                 <td <?php echo $attributes ?>><?php do_action( 'manage_link_custom_column', $column_name, $link->link_id ); ?></td>
198                                                 <?php
199                                                 break;
200                                 }
201                         }
202 ?>
203                 </tr>
204 <?php
205                 }
206         }
207 }