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

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