PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/julianelve/vendor-wordpress
PHP | 378 lines | 274 code | 85 blank | 19 comment | 57 complexity | 91dcc2208a8045590a1fb7821c8aa4e3 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Terms List Table class.
  4. *
  5. * @package WordPress
  6. * @subpackage List_Table
  7. * @since 3.1.0
  8. * @access private
  9. */
  10. class WP_Terms_List_Table extends WP_List_Table {
  11. var $callback_args;
  12. function __construct( $args = array() ) {
  13. global $post_type, $taxonomy, $action, $tax;
  14. parent::__construct( array(
  15. 'plural' => 'tags',
  16. 'singular' => 'tag',
  17. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  18. ) );
  19. $action = $this->screen->action;
  20. $post_type = $this->screen->post_type;
  21. $taxonomy = $this->screen->taxonomy;
  22. if ( empty( $taxonomy ) )
  23. $taxonomy = 'post_tag';
  24. if ( ! taxonomy_exists( $taxonomy ) )
  25. wp_die( __( 'Invalid taxonomy' ) );
  26. $tax = get_taxonomy( $taxonomy );
  27. // @todo Still needed? Maybe just the show_ui part.
  28. if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) )
  29. $post_type = 'post';
  30. }
  31. function ajax_user_can() {
  32. return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms );
  33. }
  34. function prepare_items() {
  35. $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' );
  36. if ( 'post_tag' == $this->screen->taxonomy ) {
  37. $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
  38. $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page ); // Old filter
  39. } elseif ( 'category' == $this->screen->taxonomy ) {
  40. $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); // Old filter
  41. }
  42. $search = !empty( $_REQUEST['s'] ) ? trim( stripslashes( $_REQUEST['s'] ) ) : '';
  43. $args = array(
  44. 'search' => $search,
  45. 'page' => $this->get_pagenum(),
  46. 'number' => $tags_per_page,
  47. );
  48. if ( !empty( $_REQUEST['orderby'] ) )
  49. $args['orderby'] = trim( stripslashes( $_REQUEST['orderby'] ) );
  50. if ( !empty( $_REQUEST['order'] ) )
  51. $args['order'] = trim( stripslashes( $_REQUEST['order'] ) );
  52. $this->callback_args = $args;
  53. $this->set_pagination_args( array(
  54. 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
  55. 'per_page' => $tags_per_page,
  56. ) );
  57. }
  58. function has_items() {
  59. // todo: populate $this->items in prepare_items()
  60. return true;
  61. }
  62. function get_bulk_actions() {
  63. $actions = array();
  64. $actions['delete'] = __( 'Delete' );
  65. return $actions;
  66. }
  67. function current_action() {
  68. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
  69. return 'bulk-delete';
  70. return parent::current_action();
  71. }
  72. function get_columns() {
  73. $columns = array(
  74. 'cb' => '<input type="checkbox" />',
  75. 'name' => _x( 'Name', 'term name' ),
  76. 'description' => __( 'Description' ),
  77. 'slug' => __( 'Slug' ),
  78. );
  79. if ( 'link_category' == $this->screen->taxonomy ) {
  80. $columns['links'] = __( 'Links' );
  81. } else {
  82. $post_type_object = get_post_type_object( $this->screen->post_type );
  83. $columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' );
  84. }
  85. return $columns;
  86. }
  87. function get_sortable_columns() {
  88. return array(
  89. 'name' => 'name',
  90. 'description' => 'description',
  91. 'slug' => 'slug',
  92. 'posts' => 'count',
  93. 'links' => 'count'
  94. );
  95. }
  96. function display_rows_or_placeholder() {
  97. $taxonomy = $this->screen->taxonomy;
  98. $args = wp_parse_args( $this->callback_args, array(
  99. 'page' => 1,
  100. 'number' => 20,
  101. 'search' => '',
  102. 'hide_empty' => 0
  103. ) );
  104. extract( $args, EXTR_SKIP );
  105. $args['offset'] = $offset = ( $page - 1 ) * $number;
  106. // convert it to table rows
  107. $out = '';
  108. $count = 0;
  109. $terms = array();
  110. if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
  111. // We'll need the full set of terms then.
  112. $args['number'] = $args['offset'] = 0;
  113. $terms = get_terms( $taxonomy, $args );
  114. if ( !empty( $search ) ) // Ignore children on searches.
  115. $children = array();
  116. else
  117. $children = _get_term_hierarchy( $taxonomy );
  118. // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
  119. $out .= $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
  120. } else {
  121. $terms = get_terms( $taxonomy, $args );
  122. foreach ( $terms as $term )
  123. $out .= $this->single_row( $term, 0, $taxonomy );
  124. $count = $number; // Only displaying a single page.
  125. }
  126. if ( empty( $terms ) ) {
  127. list( $columns, $hidden ) = $this->get_column_info();
  128. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
  129. $this->no_items();
  130. echo '</td></tr>';
  131. } else {
  132. echo $out;
  133. }
  134. }
  135. function _rows( $taxonomy, $terms, &$children, $start = 0, $per_page = 20, &$count, $parent = 0, $level = 0 ) {
  136. $end = $start + $per_page;
  137. $output = '';
  138. foreach ( $terms as $key => $term ) {
  139. if ( $count >= $end )
  140. break;
  141. if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
  142. continue;
  143. // If the page starts in a subtree, print the parents.
  144. if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
  145. $my_parents = $parent_ids = array();
  146. $p = $term->parent;
  147. while ( $p ) {
  148. $my_parent = get_term( $p, $taxonomy );
  149. $my_parents[] = $my_parent;
  150. $p = $my_parent->parent;
  151. if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
  152. break;
  153. $parent_ids[] = $p;
  154. }
  155. unset( $parent_ids );
  156. $num_parents = count( $my_parents );
  157. while ( $my_parent = array_pop( $my_parents ) ) {
  158. $output .= "\t" . $this->single_row( $my_parent, $level - $num_parents, $taxonomy );
  159. $num_parents--;
  160. }
  161. }
  162. if ( $count >= $start )
  163. $output .= "\t" . $this->single_row( $term, $level, $taxonomy );
  164. ++$count;
  165. unset( $terms[$key] );
  166. if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
  167. $output .= $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
  168. }
  169. return $output;
  170. }
  171. function single_row( $tag, $level = 0 ) {
  172. static $row_class = '';
  173. $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
  174. $this->level = $level;
  175. echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
  176. echo $this->single_row_columns( $tag );
  177. echo '</tr>';
  178. }
  179. function column_cb( $tag ) {
  180. $default_term = get_option( 'default_' . $this->screen->taxonomy );
  181. if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
  182. return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
  183. . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
  184. return '&nbsp;';
  185. }
  186. function column_name( $tag ) {
  187. $taxonomy = $this->screen->taxonomy;
  188. $tax = get_taxonomy( $taxonomy );
  189. $default_term = get_option( 'default_' . $taxonomy );
  190. $pad = str_repeat( '&#8212; ', max( 0, $this->level ) );
  191. $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
  192. $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
  193. $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
  194. $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $name ) ) . '">' . $name . '</a></strong><br />';
  195. $actions = array();
  196. if ( current_user_can( $tax->cap->edit_terms ) ) {
  197. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  198. $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick&nbsp;Edit' ) . '</a>';
  199. }
  200. if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
  201. $actions['delete'] = "<a class='delete-tag' href='" . wp_nonce_url( "edit-tags.php?action=delete&amp;taxonomy=$taxonomy&amp;tag_ID=$tag->term_id", 'delete-tag_' . $tag->term_id ) . "'>" . __( 'Delete' ) . "</a>";
  202. $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
  203. $actions = apply_filters( 'tag_row_actions', $actions, $tag );
  204. $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
  205. $out .= $this->row_actions( $actions );
  206. $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
  207. $out .= '<div class="name">' . $qe_data->name . '</div>';
  208. $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
  209. $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
  210. return $out;
  211. }
  212. function column_description( $tag ) {
  213. return $tag->description;
  214. }
  215. function column_slug( $tag ) {
  216. return apply_filters( 'editable_slug', $tag->slug );
  217. }
  218. function column_posts( $tag ) {
  219. $count = number_format_i18n( $tag->count );
  220. $tax = get_taxonomy( $this->screen->taxonomy );
  221. $ptype_object = get_post_type_object( $this->screen->post_type );
  222. if ( ! $ptype_object->show_ui )
  223. return $count;
  224. if ( $tax->query_var ) {
  225. $args = array( $tax->query_var => $tag->slug );
  226. } else {
  227. $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
  228. }
  229. if ( 'post' != $this->screen->post_type )
  230. $args['post_type'] = $this->screen->post_type;
  231. if ( 'attachment' == $this->screen->post_type )
  232. return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
  233. return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
  234. }
  235. function column_links( $tag ) {
  236. $count = number_format_i18n( $tag->count );
  237. if ( $count )
  238. $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
  239. return $count;
  240. }
  241. function column_default( $tag, $column_name ) {
  242. return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
  243. }
  244. /**
  245. * Outputs the hidden row displayed when inline editing
  246. *
  247. * @since 3.1.0
  248. */
  249. function inline_edit() {
  250. $tax = get_taxonomy( $this->screen->taxonomy );
  251. if ( ! current_user_can( $tax->cap->edit_terms ) )
  252. return;
  253. ?>
  254. <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
  255. <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
  256. <fieldset><div class="inline-edit-col">
  257. <h4><?php _e( 'Quick Edit' ); ?></h4>
  258. <label>
  259. <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
  260. <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
  261. </label>
  262. <?php if ( !global_terms_enabled() ) { ?>
  263. <label>
  264. <span class="title"><?php _e( 'Slug' ); ?></span>
  265. <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
  266. </label>
  267. <?php } ?>
  268. </div></fieldset>
  269. <?php
  270. $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
  271. list( $columns ) = $this->get_column_info();
  272. foreach ( $columns as $column_name => $column_display_name ) {
  273. if ( isset( $core_columns[$column_name] ) )
  274. continue;
  275. do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
  276. }
  277. ?>
  278. <p class="inline-edit-save submit">
  279. <a accesskey="c" href="#inline-edit" title="<?php esc_attr_e( 'Cancel' ); ?>" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
  280. <?php $update_text = $tax->labels->update_item; ?>
  281. <a accesskey="s" href="#inline-edit" title="<?php echo esc_attr( $update_text ); ?>" class="save button-primary alignright"><?php echo $update_text; ?></a>
  282. <span class="spinner"></span>
  283. <span class="error" style="display:none;"></span>
  284. <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
  285. <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
  286. <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
  287. <br class="clear" />
  288. </p>
  289. </td></tr>
  290. </tbody></table></form>
  291. <?php
  292. }
  293. }