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

https://bitbucket.org/devbctph/futura_wp · PHP · 635 lines · 331 code · 96 blank · 208 comment · 54 complexity · e48f8e3320766e4c1d1ae28551203d3f MD5 · raw file

  1. <?php
  2. /**
  3. * List Table API: WP_Terms_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying terms in a list table.
  11. *
  12. * @since 3.1.0
  13. * @access private
  14. *
  15. * @see WP_List_Table
  16. */
  17. class WP_Terms_List_Table extends WP_List_Table {
  18. public $callback_args;
  19. private $level;
  20. /**
  21. * Constructor.
  22. *
  23. * @since 3.1.0
  24. *
  25. * @see WP_List_Table::__construct() for more information on default arguments.
  26. *
  27. * @global string $post_type
  28. * @global string $taxonomy
  29. * @global string $action
  30. * @global object $tax
  31. *
  32. * @param array $args An associative array of arguments.
  33. */
  34. public function __construct( $args = array() ) {
  35. global $post_type, $taxonomy, $action, $tax;
  36. parent::__construct( array(
  37. 'plural' => 'tags',
  38. 'singular' => 'tag',
  39. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  40. ) );
  41. $action = $this->screen->action;
  42. $post_type = $this->screen->post_type;
  43. $taxonomy = $this->screen->taxonomy;
  44. if ( empty( $taxonomy ) )
  45. $taxonomy = 'post_tag';
  46. if ( ! taxonomy_exists( $taxonomy ) )
  47. wp_die( __( 'Invalid taxonomy.' ) );
  48. $tax = get_taxonomy( $taxonomy );
  49. // @todo Still needed? Maybe just the show_ui part.
  50. if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
  51. $post_type = 'post';
  52. }
  53. /**
  54. *
  55. * @return bool
  56. */
  57. public function ajax_user_can() {
  58. return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
  59. }
  60. /**
  61. */
  62. public function prepare_items() {
  63. $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
  64. if ( 'post_tag' === $this->screen->taxonomy ) {
  65. /**
  66. * Filters the number of terms displayed per page for the Tags list table.
  67. *
  68. * @since 2.8.0
  69. *
  70. * @param int $tags_per_page Number of tags to be displayed. Default 20.
  71. */
  72. $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
  73. /**
  74. * Filters the number of terms displayed per page for the Tags list table.
  75. *
  76. * @since 2.7.0
  77. * @deprecated 2.8.0 Use edit_tags_per_page instead.
  78. *
  79. * @param int $tags_per_page Number of tags to be displayed. Default 20.
  80. */
  81. $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page );
  82. } elseif ( 'category' === $this->screen->taxonomy ) {
  83. /**
  84. * Filters the number of terms displayed per page for the Categories list table.
  85. *
  86. * @since 2.8.0
  87. *
  88. * @param int $tags_per_page Number of categories to be displayed. Default 20.
  89. */
  90. $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
  91. }
  92. $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
  93. $args = array(
  94. 'search' => $search,
  95. 'page' => $this->get_pagenum(),
  96. 'number' => $tags_per_page,
  97. );
  98. if ( !empty( $_REQUEST['orderby'] ) )
  99. $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
  100. if ( !empty( $_REQUEST['order'] ) )
  101. $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
  102. $this->callback_args = $args;
  103. $this->set_pagination_args( array(
  104. 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
  105. 'per_page' => $tags_per_page,
  106. ) );
  107. }
  108. /**
  109. *
  110. * @return bool
  111. */
  112. public function has_items() {
  113. // todo: populate $this->items in prepare_items()
  114. return true;
  115. }
  116. /**
  117. */
  118. public function no_items() {
  119. echo get_taxonomy( $this->screen->taxonomy )->labels->not_found;
  120. }
  121. /**
  122. *
  123. * @return array
  124. */
  125. protected function get_bulk_actions() {
  126. $actions = array();
  127. if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) ) {
  128. $actions['delete'] = __( 'Delete' );
  129. }
  130. return $actions;
  131. }
  132. /**
  133. *
  134. * @return string
  135. */
  136. public function current_action() {
  137. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' === $_REQUEST['action'] || 'delete' === $_REQUEST['action2'] ) )
  138. return 'bulk-delete';
  139. return parent::current_action();
  140. }
  141. /**
  142. *
  143. * @return array
  144. */
  145. public function get_columns() {
  146. $columns = array(
  147. 'cb' => '<input type="checkbox" />',
  148. 'name' => _x( 'Name', 'term name' ),
  149. 'description' => __( 'Description' ),
  150. 'slug' => __( 'Slug' ),
  151. );
  152. if ( 'link_category' === $this->screen->taxonomy ) {
  153. $columns['links'] = __( 'Links' );
  154. } else {
  155. $columns['posts'] = _x( 'Count', 'Number/count of items' );
  156. }
  157. return $columns;
  158. }
  159. /**
  160. *
  161. * @return array
  162. */
  163. protected function get_sortable_columns() {
  164. return array(
  165. 'name' => 'name',
  166. 'description' => 'description',
  167. 'slug' => 'slug',
  168. 'posts' => 'count',
  169. 'links' => 'count'
  170. );
  171. }
  172. /**
  173. */
  174. public function display_rows_or_placeholder() {
  175. $taxonomy = $this->screen->taxonomy;
  176. $args = wp_parse_args( $this->callback_args, array(
  177. 'page' => 1,
  178. 'number' => 20,
  179. 'search' => '',
  180. 'hide_empty' => 0
  181. ) );
  182. $page = $args['page'];
  183. // Set variable because $args['number'] can be subsequently overridden.
  184. $number = $args['number'];
  185. $args['offset'] = $offset = ( $page - 1 ) * $number;
  186. // Convert it to table rows.
  187. $count = 0;
  188. if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
  189. // We'll need the full set of terms then.
  190. $args['number'] = $args['offset'] = 0;
  191. }
  192. $terms = get_terms( $taxonomy, $args );
  193. if ( empty( $terms ) || ! is_array( $terms ) ) {
  194. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
  195. $this->no_items();
  196. echo '</td></tr>';
  197. return;
  198. }
  199. if ( is_taxonomy_hierarchical( $taxonomy ) && ! isset( $args['orderby'] ) ) {
  200. if ( ! empty( $args['search'] ) ) {// Ignore children on searches.
  201. $children = array();
  202. } else {
  203. $children = _get_term_hierarchy( $taxonomy );
  204. }
  205. // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
  206. $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
  207. } else {
  208. foreach ( $terms as $term ) {
  209. $this->single_row( $term );
  210. }
  211. }
  212. }
  213. /**
  214. * @param string $taxonomy
  215. * @param array $terms
  216. * @param array $children
  217. * @param int $start
  218. * @param int $per_page
  219. * @param int $count
  220. * @param int $parent
  221. * @param int $level
  222. */
  223. private function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
  224. $end = $start + $per_page;
  225. foreach ( $terms as $key => $term ) {
  226. if ( $count >= $end )
  227. break;
  228. if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
  229. continue;
  230. // If the page starts in a subtree, print the parents.
  231. if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
  232. $my_parents = $parent_ids = array();
  233. $p = $term->parent;
  234. while ( $p ) {
  235. $my_parent = get_term( $p, $taxonomy );
  236. $my_parents[] = $my_parent;
  237. $p = $my_parent->parent;
  238. if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
  239. break;
  240. $parent_ids[] = $p;
  241. }
  242. unset( $parent_ids );
  243. $num_parents = count( $my_parents );
  244. while ( $my_parent = array_pop( $my_parents ) ) {
  245. echo "\t";
  246. $this->single_row( $my_parent, $level - $num_parents );
  247. $num_parents--;
  248. }
  249. }
  250. if ( $count >= $start ) {
  251. echo "\t";
  252. $this->single_row( $term, $level );
  253. }
  254. ++$count;
  255. unset( $terms[$key] );
  256. if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
  257. $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
  258. }
  259. }
  260. /**
  261. * @global string $taxonomy
  262. * @param WP_Term $tag Term object.
  263. * @param int $level
  264. */
  265. public function single_row( $tag, $level = 0 ) {
  266. global $taxonomy;
  267. $tag = sanitize_term( $tag, $taxonomy );
  268. $this->level = $level;
  269. echo '<tr id="tag-' . $tag->term_id . '">';
  270. $this->single_row_columns( $tag );
  271. echo '</tr>';
  272. }
  273. /**
  274. * @param WP_Term $tag Term object.
  275. * @return string
  276. */
  277. public function column_cb( $tag ) {
  278. if ( current_user_can( 'delete_term', $tag->term_id ) ) {
  279. return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
  280. . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
  281. }
  282. return '&nbsp;';
  283. }
  284. /**
  285. * @param WP_Term $tag Term object.
  286. * @return string
  287. */
  288. public function column_name( $tag ) {
  289. $taxonomy = $this->screen->taxonomy;
  290. $pad = str_repeat( '&#8212; ', max( 0, $this->level ) );
  291. /**
  292. * Filters display of the term name in the terms list table.
  293. *
  294. * The default output may include padding due to the term's
  295. * current level in the term hierarchy.
  296. *
  297. * @since 2.5.0
  298. *
  299. * @see WP_Terms_List_Table::column_name()
  300. *
  301. * @param string $pad_tag_name The term name, padded if not top-level.
  302. * @param WP_Term $tag Term object.
  303. */
  304. $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
  305. $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
  306. $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
  307. $edit_link = add_query_arg(
  308. 'wp_http_referer',
  309. urlencode( wp_unslash( $uri ) ),
  310. get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
  311. );
  312. $out = sprintf(
  313. '<strong><a class="row-title" href="%s" aria-label="%s">%s</a></strong><br />',
  314. esc_url( $edit_link ),
  315. /* translators: %s: taxonomy term name */
  316. esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $tag->name ) ),
  317. $name
  318. );
  319. $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
  320. $out .= '<div class="name">' . $qe_data->name . '</div>';
  321. /** This filter is documented in wp-admin/edit-tag-form.php */
  322. $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug, $qe_data ) . '</div>';
  323. $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
  324. return $out;
  325. }
  326. /**
  327. * Gets the name of the default primary column.
  328. *
  329. * @since 4.3.0
  330. *
  331. * @return string Name of the default primary column, in this case, 'name'.
  332. */
  333. protected function get_default_primary_column_name() {
  334. return 'name';
  335. }
  336. /**
  337. * Generates and displays row action links.
  338. *
  339. * @since 4.3.0
  340. *
  341. * @param WP_Term $tag Tag being acted upon.
  342. * @param string $column_name Current column name.
  343. * @param string $primary Primary column name.
  344. * @return string Row actions output for terms.
  345. */
  346. protected function handle_row_actions( $tag, $column_name, $primary ) {
  347. if ( $primary !== $column_name ) {
  348. return '';
  349. }
  350. $taxonomy = $this->screen->taxonomy;
  351. $tax = get_taxonomy( $taxonomy );
  352. $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
  353. $edit_link = add_query_arg(
  354. 'wp_http_referer',
  355. urlencode( wp_unslash( $uri ) ),
  356. get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
  357. );
  358. $actions = array();
  359. if ( current_user_can( 'edit_term', $tag->term_id ) ) {
  360. $actions['edit'] = sprintf(
  361. '<a href="%s" aria-label="%s">%s</a>',
  362. esc_url( $edit_link ),
  363. /* translators: %s: taxonomy term name */
  364. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $tag->name ) ),
  365. __( 'Edit' )
  366. );
  367. $actions['inline hide-if-no-js'] = sprintf(
  368. '<a href="#" class="editinline aria-button-if-js" aria-label="%s">%s</a>',
  369. /* translators: %s: taxonomy term name */
  370. esc_attr( sprintf( __( 'Quick edit &#8220;%s&#8221; inline' ), $tag->name ) ),
  371. __( 'Quick&nbsp;Edit' )
  372. );
  373. }
  374. if ( current_user_can( 'delete_term', $tag->term_id ) ) {
  375. $actions['delete'] = sprintf(
  376. '<a href="%s" class="delete-tag aria-button-if-js" aria-label="%s">%s</a>',
  377. wp_nonce_url( "edit-tags.php?action=delete&amp;taxonomy=$taxonomy&amp;tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ),
  378. /* translators: %s: taxonomy term name */
  379. esc_attr( sprintf( __( 'Delete &#8220;%s&#8221;' ), $tag->name ) ),
  380. __( 'Delete' )
  381. );
  382. }
  383. if ( $tax->public ) {
  384. $actions['view'] = sprintf(
  385. '<a href="%s" aria-label="%s">%s</a>',
  386. get_term_link( $tag ),
  387. /* translators: %s: taxonomy term name */
  388. esc_attr( sprintf( __( 'View &#8220;%s&#8221; archive' ), $tag->name ) ),
  389. __( 'View' )
  390. );
  391. }
  392. /**
  393. * Filters the action links displayed for each term in the Tags list table.
  394. *
  395. * @since 2.8.0
  396. * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
  397. *
  398. * @param array $actions An array of action links to be displayed. Default
  399. * 'Edit', 'Quick Edit', 'Delete', and 'View'.
  400. * @param WP_Term $tag Term object.
  401. */
  402. $actions = apply_filters( 'tag_row_actions', $actions, $tag );
  403. /**
  404. * Filters the action links displayed for each term in the terms list table.
  405. *
  406. * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
  407. *
  408. * @since 3.0.0
  409. *
  410. * @param array $actions An array of action links to be displayed. Default
  411. * 'Edit', 'Quick Edit', 'Delete', and 'View'.
  412. * @param WP_Term $tag Term object.
  413. */
  414. $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
  415. return $this->row_actions( $actions );
  416. }
  417. /**
  418. * @param WP_Term $tag Term object.
  419. * @return string
  420. */
  421. public function column_description( $tag ) {
  422. if ( $tag->description ) {
  423. return $tag->description;
  424. } else {
  425. return '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . __( 'No description' ) . '</span>';
  426. }
  427. }
  428. /**
  429. * @param WP_Term $tag Term object.
  430. * @return string
  431. */
  432. public function column_slug( $tag ) {
  433. /** This filter is documented in wp-admin/edit-tag-form.php */
  434. return apply_filters( 'editable_slug', $tag->slug, $tag );
  435. }
  436. /**
  437. * @param WP_Term $tag Term object.
  438. * @return string
  439. */
  440. public function column_posts( $tag ) {
  441. $count = number_format_i18n( $tag->count );
  442. $tax = get_taxonomy( $this->screen->taxonomy );
  443. $ptype_object = get_post_type_object( $this->screen->post_type );
  444. if ( ! $ptype_object->show_ui )
  445. return $count;
  446. if ( $tax->query_var ) {
  447. $args = array( $tax->query_var => $tag->slug );
  448. } else {
  449. $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
  450. }
  451. if ( 'post' != $this->screen->post_type )
  452. $args['post_type'] = $this->screen->post_type;
  453. if ( 'attachment' === $this->screen->post_type )
  454. return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
  455. return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
  456. }
  457. /**
  458. * @param WP_Term $tag Term object.
  459. * @return string
  460. */
  461. public function column_links( $tag ) {
  462. $count = number_format_i18n( $tag->count );
  463. if ( $count )
  464. $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
  465. return $count;
  466. }
  467. /**
  468. * @param WP_Term $tag Term object.
  469. * @param string $column_name
  470. * @return string
  471. */
  472. public function column_default( $tag, $column_name ) {
  473. /**
  474. * Filters the displayed columns in the terms list table.
  475. *
  476. * The dynamic portion of the hook name, `$this->screen->taxonomy`,
  477. * refers to the slug of the current taxonomy.
  478. *
  479. * @since 2.8.0
  480. *
  481. * @param string $string Blank string.
  482. * @param string $column_name Name of the column.
  483. * @param int $term_id Term ID.
  484. */
  485. return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
  486. }
  487. /**
  488. * Outputs the hidden row displayed when inline editing
  489. *
  490. * @since 3.1.0
  491. */
  492. public function inline_edit() {
  493. $tax = get_taxonomy( $this->screen->taxonomy );
  494. if ( ! current_user_can( $tax->cap->edit_terms ) )
  495. return;
  496. ?>
  497. <form method="get"><table style="display: none"><tbody id="inlineedit">
  498. <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
  499. <fieldset>
  500. <legend class="inline-edit-legend"><?php _e( 'Quick Edit' ); ?></legend>
  501. <div class="inline-edit-col">
  502. <label>
  503. <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
  504. <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
  505. </label>
  506. <?php if ( !global_terms_enabled() ) { ?>
  507. <label>
  508. <span class="title"><?php _e( 'Slug' ); ?></span>
  509. <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
  510. </label>
  511. <?php } ?>
  512. </div></fieldset>
  513. <?php
  514. $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
  515. list( $columns ) = $this->get_column_info();
  516. foreach ( $columns as $column_name => $column_display_name ) {
  517. if ( isset( $core_columns[$column_name] ) )
  518. continue;
  519. /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
  520. do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
  521. }
  522. ?>
  523. <div class="inline-edit-save submit">
  524. <button type="button" class="cancel button alignleft"><?php _e( 'Cancel' ); ?></button>
  525. <button type="button" class="save button button-primary alignright"><?php echo $tax->labels->update_item; ?></button>
  526. <span class="spinner"></span>
  527. <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
  528. <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
  529. <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
  530. <br class="clear" />
  531. <div class="notice notice-error notice-alt inline hidden">
  532. <p class="error"></p>
  533. </div>
  534. </div>
  535. </td></tr>
  536. </tbody></table></form>
  537. <?php
  538. }
  539. }