Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

636 linhas
17 KiB

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