class-wp-links-list-table.php 7.4 KB

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