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

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