PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/class-wp-links-list-table.php

https://github.com/markjaquith/WordPress
PHP | 349 lines | 174 code | 34 blank | 141 comment | 10 complexity | f8192c90d5c9c8e03a9078756c511fe7 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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_Table
  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">' . get_taxonomy( 'link_category' )->labels->filter_by_item . '</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. * @since 5.9.0 Renamed `$link` to `$item` to match parent class for PHP 8 named parameter support.
  150. *
  151. * @param object $item The current link object.
  152. */
  153. public function column_cb( $item ) {
  154. // Restores the more descriptive, specific name for use within this method.
  155. $link = $item;
  156. ?>
  157. <label class="screen-reader-text" for="cb-select-<?php echo $link->link_id; ?>">
  158. <?php
  159. /* translators: %s: Link name. */
  160. printf( __( 'Select %s' ), $link->link_name );
  161. ?>
  162. </label>
  163. <input type="checkbox" name="linkcheck[]" id="cb-select-<?php echo $link->link_id; ?>" value="<?php echo esc_attr( $link->link_id ); ?>" />
  164. <?php
  165. }
  166. /**
  167. * Handles the link name column output.
  168. *
  169. * @since 4.3.0
  170. *
  171. * @param object $link The current link object.
  172. */
  173. public function column_name( $link ) {
  174. $edit_link = get_edit_bookmark_link( $link );
  175. printf(
  176. '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong>',
  177. $edit_link,
  178. /* translators: %s: Link name. */
  179. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $link->link_name ) ),
  180. $link->link_name
  181. );
  182. }
  183. /**
  184. * Handles the link URL column output.
  185. *
  186. * @since 4.3.0
  187. *
  188. * @param object $link The current link object.
  189. */
  190. public function column_url( $link ) {
  191. $short_url = url_shorten( $link->link_url );
  192. echo "<a href='$link->link_url'>$short_url</a>";
  193. }
  194. /**
  195. * Handles the link categories column output.
  196. *
  197. * @since 4.3.0
  198. *
  199. * @global int $cat_id
  200. *
  201. * @param object $link The current link object.
  202. */
  203. public function column_categories( $link ) {
  204. global $cat_id;
  205. $cat_names = array();
  206. foreach ( $link->link_category as $category ) {
  207. $cat = get_term( $category, 'link_category', OBJECT, 'display' );
  208. if ( is_wp_error( $cat ) ) {
  209. echo $cat->get_error_message();
  210. }
  211. $cat_name = $cat->name;
  212. if ( (int) $cat_id !== $category ) {
  213. $cat_name = "<a href='link-manager.php?cat_id=$category'>$cat_name</a>";
  214. }
  215. $cat_names[] = $cat_name;
  216. }
  217. echo implode( ', ', $cat_names );
  218. }
  219. /**
  220. * Handles the link relation column output.
  221. *
  222. * @since 4.3.0
  223. *
  224. * @param object $link The current link object.
  225. */
  226. public function column_rel( $link ) {
  227. echo empty( $link->link_rel ) ? '<br />' : $link->link_rel;
  228. }
  229. /**
  230. * Handles the link visibility column output.
  231. *
  232. * @since 4.3.0
  233. *
  234. * @param object $link The current link object.
  235. */
  236. public function column_visible( $link ) {
  237. if ( 'Y' === $link->link_visible ) {
  238. _e( 'Yes' );
  239. } else {
  240. _e( 'No' );
  241. }
  242. }
  243. /**
  244. * Handles the link rating column output.
  245. *
  246. * @since 4.3.0
  247. *
  248. * @param object $link The current link object.
  249. */
  250. public function column_rating( $link ) {
  251. echo $link->link_rating;
  252. }
  253. /**
  254. * Handles the default column output.
  255. *
  256. * @since 4.3.0
  257. * @since 5.9.0 Renamed `$link` to `$item` to match parent class for PHP 8 named parameter support.
  258. *
  259. * @param object $item Link object.
  260. * @param string $column_name Current column name.
  261. */
  262. public function column_default( $item, $column_name ) {
  263. /**
  264. * Fires for each registered custom link column.
  265. *
  266. * @since 2.1.0
  267. *
  268. * @param string $column_name Name of the custom column.
  269. * @param int $link_id Link ID.
  270. */
  271. do_action( 'manage_link_custom_column', $column_name, $item->link_id );
  272. }
  273. public function display_rows() {
  274. foreach ( $this->items as $link ) {
  275. $link = sanitize_bookmark( $link );
  276. $link->link_name = esc_attr( $link->link_name );
  277. $link->link_category = wp_get_link_cats( $link->link_id );
  278. ?>
  279. <tr id="link-<?php echo $link->link_id; ?>">
  280. <?php $this->single_row_columns( $link ); ?>
  281. </tr>
  282. <?php
  283. }
  284. }
  285. /**
  286. * Generates and displays row action links.
  287. *
  288. * @since 4.3.0
  289. * @since 5.9.0 Renamed `$link` to `$item` to match parent class for PHP 8 named parameter support.
  290. *
  291. * @param object $item Link being acted upon.
  292. * @param string $column_name Current column name.
  293. * @param string $primary Primary column name.
  294. * @return string Row actions output for links, or an empty string
  295. * if the current column is not the primary column.
  296. */
  297. protected function handle_row_actions( $item, $column_name, $primary ) {
  298. if ( $primary !== $column_name ) {
  299. return '';
  300. }
  301. // Restores the more descriptive, specific name for use within this method.
  302. $link = $item;
  303. $edit_link = get_edit_bookmark_link( $link );
  304. $actions = array();
  305. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  306. $actions['delete'] = sprintf(
  307. '<a class="submitdelete" href="%s" onclick="return confirm( \'%s\' );">%s</a>',
  308. wp_nonce_url( "link.php?action=delete&amp;link_id=$link->link_id", 'delete-bookmark_' . $link->link_id ),
  309. /* translators: %s: Link name. */
  310. esc_js( sprintf( __( "You are about to delete this link '%s'\n 'Cancel' to stop, 'OK' to delete." ), $link->link_name ) ),
  311. __( 'Delete' )
  312. );
  313. return $this->row_actions( $actions );
  314. }
  315. }