PageRenderTime 28ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/haque.mdmanzurul/barongbarong
PHP | 454 lines | 275 code | 89 blank | 90 comment | 59 complexity | 58a4dae784e044a0ee4d66697fd9ae85 MD5 | raw file
  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. /**
  38. * Filter the number of terms displayed per page for the Tags list table.
  39. *
  40. * @since 2.8.0
  41. *
  42. * @param int $tags_per_page Number of tags to be displayed. Default 20.
  43. */
  44. $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page );
  45. /**
  46. * Filter the number of terms displayed per page for the Tags list table.
  47. *
  48. * @since 2.7.0
  49. * @deprecated 2.8.0 Use edit_tags_per_page instead.
  50. *
  51. * @param int $tags_per_page Number of tags to be displayed. Default 20.
  52. */
  53. $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page );
  54. } elseif ( 'category' == $this->screen->taxonomy ) {
  55. /**
  56. * Filter the number of terms displayed per page for the Categories list table.
  57. *
  58. * @since 2.8.0
  59. *
  60. * @param int $tags_per_page Number of categories to be displayed. Default 20.
  61. */
  62. $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page );
  63. }
  64. $search = !empty( $_REQUEST['s'] ) ? trim( wp_unslash( $_REQUEST['s'] ) ) : '';
  65. $args = array(
  66. 'search' => $search,
  67. 'page' => $this->get_pagenum(),
  68. 'number' => $tags_per_page,
  69. );
  70. if ( !empty( $_REQUEST['orderby'] ) )
  71. $args['orderby'] = trim( wp_unslash( $_REQUEST['orderby'] ) );
  72. if ( !empty( $_REQUEST['order'] ) )
  73. $args['order'] = trim( wp_unslash( $_REQUEST['order'] ) );
  74. $this->callback_args = $args;
  75. $this->set_pagination_args( array(
  76. 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ),
  77. 'per_page' => $tags_per_page,
  78. ) );
  79. }
  80. function has_items() {
  81. // todo: populate $this->items in prepare_items()
  82. return true;
  83. }
  84. function get_bulk_actions() {
  85. $actions = array();
  86. $actions['delete'] = __( 'Delete' );
  87. return $actions;
  88. }
  89. function current_action() {
  90. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) )
  91. return 'bulk-delete';
  92. return parent::current_action();
  93. }
  94. function get_columns() {
  95. $columns = array(
  96. 'cb' => '<input type="checkbox" />',
  97. 'name' => _x( 'Name', 'term name' ),
  98. 'description' => __( 'Description' ),
  99. 'slug' => __( 'Slug' ),
  100. );
  101. if ( 'link_category' == $this->screen->taxonomy ) {
  102. $columns['links'] = __( 'Links' );
  103. } else {
  104. $post_type_object = get_post_type_object( $this->screen->post_type );
  105. $columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' );
  106. }
  107. return $columns;
  108. }
  109. function get_sortable_columns() {
  110. return array(
  111. 'name' => 'name',
  112. 'description' => 'description',
  113. 'slug' => 'slug',
  114. 'posts' => 'count',
  115. 'links' => 'count'
  116. );
  117. }
  118. function display_rows_or_placeholder() {
  119. $taxonomy = $this->screen->taxonomy;
  120. $args = wp_parse_args( $this->callback_args, array(
  121. 'page' => 1,
  122. 'number' => 20,
  123. 'search' => '',
  124. 'hide_empty' => 0
  125. ) );
  126. extract( $args, EXTR_SKIP );
  127. $args['offset'] = $offset = ( $page - 1 ) * $number;
  128. // convert it to table rows
  129. $count = 0;
  130. $terms = array();
  131. if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
  132. // We'll need the full set of terms then.
  133. $args['number'] = $args['offset'] = 0;
  134. }
  135. $terms = get_terms( $taxonomy, $args );
  136. if ( empty( $terms ) ) {
  137. list( $columns, $hidden ) = $this->get_column_info();
  138. echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
  139. $this->no_items();
  140. echo '</td></tr>';
  141. return;
  142. }
  143. if ( is_taxonomy_hierarchical( $taxonomy ) && !isset( $orderby ) ) {
  144. if ( !empty( $search ) ) // Ignore children on searches.
  145. $children = array();
  146. else
  147. $children = _get_term_hierarchy( $taxonomy );
  148. // Some funky recursion to get the job done( Paging & parents mainly ) is contained within, Skip it for non-hierarchical taxonomies for performance sake
  149. $this->_rows( $taxonomy, $terms, $children, $offset, $number, $count );
  150. } else {
  151. $terms = get_terms( $taxonomy, $args );
  152. foreach ( $terms as $term )
  153. $this->single_row( $term );
  154. $count = $number; // Only displaying a single page.
  155. }
  156. }
  157. function _rows( $taxonomy, $terms, &$children, $start, $per_page, &$count, $parent = 0, $level = 0 ) {
  158. $end = $start + $per_page;
  159. foreach ( $terms as $key => $term ) {
  160. if ( $count >= $end )
  161. break;
  162. if ( $term->parent != $parent && empty( $_REQUEST['s'] ) )
  163. continue;
  164. // If the page starts in a subtree, print the parents.
  165. if ( $count == $start && $term->parent > 0 && empty( $_REQUEST['s'] ) ) {
  166. $my_parents = $parent_ids = array();
  167. $p = $term->parent;
  168. while ( $p ) {
  169. $my_parent = get_term( $p, $taxonomy );
  170. $my_parents[] = $my_parent;
  171. $p = $my_parent->parent;
  172. if ( in_array( $p, $parent_ids ) ) // Prevent parent loops.
  173. break;
  174. $parent_ids[] = $p;
  175. }
  176. unset( $parent_ids );
  177. $num_parents = count( $my_parents );
  178. while ( $my_parent = array_pop( $my_parents ) ) {
  179. echo "\t";
  180. $this->single_row( $my_parent, $level - $num_parents );
  181. $num_parents--;
  182. }
  183. }
  184. if ( $count >= $start ) {
  185. echo "\t";
  186. $this->single_row( $term, $level );
  187. }
  188. ++$count;
  189. unset( $terms[$key] );
  190. if ( isset( $children[$term->term_id] ) && empty( $_REQUEST['s'] ) )
  191. $this->_rows( $taxonomy, $terms, $children, $start, $per_page, $count, $term->term_id, $level + 1 );
  192. }
  193. }
  194. function single_row( $tag, $level = 0 ) {
  195. static $row_class = '';
  196. $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
  197. $this->level = $level;
  198. echo '<tr id="tag-' . $tag->term_id . '"' . $row_class . '>';
  199. $this->single_row_columns( $tag );
  200. echo '</tr>';
  201. }
  202. function column_cb( $tag ) {
  203. $default_term = get_option( 'default_' . $this->screen->taxonomy );
  204. if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term )
  205. return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>'
  206. . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />';
  207. return '&nbsp;';
  208. }
  209. function column_name( $tag ) {
  210. $taxonomy = $this->screen->taxonomy;
  211. $tax = get_taxonomy( $taxonomy );
  212. $default_term = get_option( 'default_' . $taxonomy );
  213. $pad = str_repeat( '&#8212; ', max( 0, $this->level ) );
  214. /**
  215. * Filter display of the term name in the terms list table.
  216. *
  217. * The default output may include padding due to the term's
  218. * current level in the term hierarchy.
  219. *
  220. * @since 2.5.0
  221. *
  222. * @see WP_Terms_List_Table::column_name()
  223. *
  224. * @param string $pad_tag_name The term name, padded if not top-level.
  225. * @param object $tag Term object.
  226. */
  227. $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag );
  228. $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
  229. $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) );
  230. $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $name ) ) . '">' . $name . '</a></strong><br />';
  231. $actions = array();
  232. if ( current_user_can( $tax->cap->edit_terms ) ) {
  233. $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  234. $actions['inline hide-if-no-js'] = '<a href="#" class="editinline">' . __( 'Quick&nbsp;Edit' ) . '</a>';
  235. }
  236. if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term )
  237. $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>";
  238. if ( $tax->public )
  239. $actions['view'] = '<a href="' . get_term_link( $tag ) . '">' . __( 'View' ) . '</a>';
  240. /**
  241. * Filter the action links displayed for each term in the Tags list table.
  242. *
  243. * @since 2.8.0
  244. * @deprecated 3.0.0 Use {$taxonomy}_row_actions instead.
  245. *
  246. * @param array $actions An array of action links to be displayed. Default
  247. * 'Edit', 'Quick Edit', 'Delete', and 'View'.
  248. * @param object $tag Term object.
  249. */
  250. $actions = apply_filters( 'tag_row_actions', $actions, $tag );
  251. /**
  252. * Filter the action links displayed for each term in the terms list table.
  253. *
  254. * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
  255. *
  256. * @since 3.0.0
  257. *
  258. * @param array $actions An array of action links to be displayed. Default
  259. * 'Edit', 'Quick Edit', 'Delete', and 'View'.
  260. * @param object $tag Term object.
  261. */
  262. $actions = apply_filters( "{$taxonomy}_row_actions", $actions, $tag );
  263. $out .= $this->row_actions( $actions );
  264. $out .= '<div class="hidden" id="inline_' . $qe_data->term_id . '">';
  265. $out .= '<div class="name">' . $qe_data->name . '</div>';
  266. /** This filter is documented in wp-admin/edit-tag-form.php */
  267. $out .= '<div class="slug">' . apply_filters( 'editable_slug', $qe_data->slug ) . '</div>';
  268. $out .= '<div class="parent">' . $qe_data->parent . '</div></div>';
  269. return $out;
  270. }
  271. function column_description( $tag ) {
  272. return $tag->description;
  273. }
  274. function column_slug( $tag ) {
  275. /** This filter is documented in wp-admin/edit-tag-form.php */
  276. return apply_filters( 'editable_slug', $tag->slug );
  277. }
  278. function column_posts( $tag ) {
  279. $count = number_format_i18n( $tag->count );
  280. $tax = get_taxonomy( $this->screen->taxonomy );
  281. $ptype_object = get_post_type_object( $this->screen->post_type );
  282. if ( ! $ptype_object->show_ui )
  283. return $count;
  284. if ( $tax->query_var ) {
  285. $args = array( $tax->query_var => $tag->slug );
  286. } else {
  287. $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug );
  288. }
  289. if ( 'post' != $this->screen->post_type )
  290. $args['post_type'] = $this->screen->post_type;
  291. if ( 'attachment' == $this->screen->post_type )
  292. return "<a href='" . esc_url ( add_query_arg( $args, 'upload.php' ) ) . "'>$count</a>";
  293. return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>";
  294. }
  295. function column_links( $tag ) {
  296. $count = number_format_i18n( $tag->count );
  297. if ( $count )
  298. $count = "<a href='link-manager.php?cat_id=$tag->term_id'>$count</a>";
  299. return $count;
  300. }
  301. function column_default( $tag, $column_name ) {
  302. /**
  303. * Filter the displayed columns in the terms list table.
  304. *
  305. * The dynamic portion of the hook name, $this->screen->taxonomy,
  306. * refers to the slug of the current taxonomy.
  307. *
  308. * @since 2.8.0
  309. *
  310. * @param string $string Blank string.
  311. * @param string $column_name Name of the column.
  312. * @param int $term_id Term ID.
  313. */
  314. return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id );
  315. }
  316. /**
  317. * Outputs the hidden row displayed when inline editing
  318. *
  319. * @since 3.1.0
  320. */
  321. function inline_edit() {
  322. $tax = get_taxonomy( $this->screen->taxonomy );
  323. if ( ! current_user_can( $tax->cap->edit_terms ) )
  324. return;
  325. ?>
  326. <form method="get" action=""><table style="display: none"><tbody id="inlineedit">
  327. <tr id="inline-edit" class="inline-edit-row" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange">
  328. <fieldset><div class="inline-edit-col">
  329. <h4><?php _e( 'Quick Edit' ); ?></h4>
  330. <label>
  331. <span class="title"><?php _ex( 'Name', 'term name' ); ?></span>
  332. <span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
  333. </label>
  334. <?php if ( !global_terms_enabled() ) { ?>
  335. <label>
  336. <span class="title"><?php _e( 'Slug' ); ?></span>
  337. <span class="input-text-wrap"><input type="text" name="slug" class="ptitle" value="" /></span>
  338. </label>
  339. <?php } ?>
  340. </div></fieldset>
  341. <?php
  342. $core_columns = array( 'cb' => true, 'description' => true, 'name' => true, 'slug' => true, 'posts' => true );
  343. list( $columns ) = $this->get_column_info();
  344. foreach ( $columns as $column_name => $column_display_name ) {
  345. if ( isset( $core_columns[$column_name] ) )
  346. continue;
  347. /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
  348. do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy );
  349. }
  350. ?>
  351. <p class="inline-edit-save submit">
  352. <a accesskey="c" href="#inline-edit" class="cancel button-secondary alignleft"><?php _e( 'Cancel' ); ?></a>
  353. <a accesskey="s" href="#inline-edit" class="save button-primary alignright"><?php echo $tax->labels->update_item; ?></a>
  354. <span class="spinner"></span>
  355. <span class="error" style="display:none;"></span>
  356. <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?>
  357. <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" />
  358. <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" />
  359. <br class="clear" />
  360. </p>
  361. </td></tr>
  362. </tbody></table></form>
  363. <?php
  364. }
  365. }