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