PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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