From 7c52a19a5afcfb90e5f6b0414c52eacb59ca5306 Mon Sep 17 00:00:00 2001 From: Ryan Boren <ryan@git.wordpress.org> Date: Wed, 19 Sep 2012 12:43:31 +0000 Subject: [PATCH] Reduce reliance on global variables in the list tables. Allow passing a screen ID to the list tables so that ajax handlers can set the needed screen. Props nacin fixes #21871 git-svn-id: https://develop.svn.wordpress.org/trunk@21914 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/ajax-actions.php | 35 ++------ .../includes/class-wp-links-list-table.php | 3 +- wp-admin/includes/class-wp-list-table.php | 33 +++---- .../includes/class-wp-media-list-table.php | 5 +- .../includes/class-wp-ms-sites-list-table.php | 3 +- .../class-wp-ms-themes-list-table.php | 14 +-- .../includes/class-wp-plugins-list-table.php | 34 +++---- .../includes/class-wp-posts-list-table.php | 64 ++++++------- .../includes/class-wp-terms-list-table.php | 90 +++++++++---------- .../includes/class-wp-themes-list-table.php | 3 +- .../includes/class-wp-users-list-table.php | 16 ++-- wp-admin/includes/list-table.php | 11 ++- 12 files changed, 137 insertions(+), 174 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 06042da368..69b235a33b 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -29,17 +29,12 @@ function wp_ajax_nopriv_autosave() { * GET-based Ajax handlers. */ function wp_ajax_fetch_list() { - global $current_screen, $wp_list_table; + global $wp_list_table; $list_class = $_GET['list_args']['class']; check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' ); - $current_screen = convert_to_screen( $_GET['list_args']['screen']['id'] ); - - define( 'WP_NETWORK_ADMIN', $current_screen->is_network ); - define( 'WP_USER_ADMIN', $current_screen->is_user ); - - $wp_list_table = _get_list_table( $list_class ); + $wp_list_table = _get_list_table( $list_class, array( 'screen' => $_GET['list_args']['screen']['id'] ) ); if ( ! $wp_list_table ) wp_die( 0 ); @@ -615,9 +610,7 @@ function wp_ajax_add_tag() { $x->send(); } - set_current_screen( $_POST['screen'] ); - - $wp_list_table = _get_list_table('WP_Terms_List_Table'); + $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) ); $level = 0; if ( is_taxonomy_hierarchical($taxonomy) ) { @@ -686,9 +679,7 @@ function wp_ajax_get_comments( $action ) { check_ajax_referer( $action ); - set_current_screen( 'edit-comments' ); - - $wp_list_table = _get_list_table('WP_Post_Comments_List_Table'); + $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); if ( !current_user_can( 'edit_post', $post_id ) ) wp_die( -1 ); @@ -723,8 +714,6 @@ function wp_ajax_replyto_comment( $action ) { check_ajax_referer( $action, '_ajax_nonce-replyto-comment' ); - set_current_screen( 'edit-comments' ); - $comment_post_ID = (int) $_POST['comment_post_ID']; if ( !current_user_can( 'edit_post', $comment_post_ID ) ) wp_die( -1 ); @@ -782,9 +771,9 @@ function wp_ajax_replyto_comment( $action ) { _wp_dashboard_recent_comments_row( $comment ); } else { if ( 'single' == $_REQUEST['mode'] ) { - $wp_list_table = _get_list_table('WP_Post_Comments_List_Table'); + $wp_list_table = _get_list_table('WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); } else { - $wp_list_table = _get_list_table('WP_Comments_List_Table'); + $wp_list_table = _get_list_table('WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); } $wp_list_table->single_row( $comment ); } @@ -811,8 +800,6 @@ function wp_ajax_edit_comment() { check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ); - set_current_screen( 'edit-comments' ); - $comment_id = (int) $_POST['comment_ID']; if ( ! current_user_can( 'edit_comment', $comment_id ) ) wp_die( -1 ); @@ -827,7 +814,7 @@ function wp_ajax_edit_comment() { $comments_status = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : ''; $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0; - $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table' ); + $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); $comment = get_comment( $comment_id ); @@ -1330,8 +1317,6 @@ function wp_ajax_inline_save() { wp_die( __( 'You are not allowed to edit this post.' ) ); } - set_current_screen( $_POST['screen'] ); - if ( $last = wp_check_post_lock( $post_ID ) ) { $last_user = get_userdata( $last ); $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' ); @@ -1367,7 +1352,7 @@ function wp_ajax_inline_save() { // update the post edit_post(); - $wp_list_table = _get_list_table('WP_Posts_List_Table'); + $wp_list_table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => $_POST['screen'] ) ); $mode = $_POST['post_view']; @@ -1399,9 +1384,7 @@ function wp_ajax_inline_save_tax() { if ( ! current_user_can( $tax->cap->edit_terms ) ) wp_die( -1 ); - set_current_screen( 'edit-' . $taxonomy ); - - $wp_list_table = _get_list_table('WP_Terms_List_Table'); + $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) ); if ( ! isset($_POST['tax_ID']) || ! ( $id = (int) $_POST['tax_ID'] ) ) wp_die( -1 ); diff --git a/wp-admin/includes/class-wp-links-list-table.php b/wp-admin/includes/class-wp-links-list-table.php index 34be076aaf..2a897b76ba 100644 --- a/wp-admin/includes/class-wp-links-list-table.php +++ b/wp-admin/includes/class-wp-links-list-table.php @@ -9,9 +9,10 @@ */ class WP_Links_List_Table extends WP_List_Table { - function __construct() { + function __construct( $args = array() ) { parent::__construct( array( 'plural' => 'bookmarks', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, ) ); } diff --git a/wp-admin/includes/class-wp-list-table.php b/wp-admin/includes/class-wp-list-table.php index 60a8a608f7..97489cdb86 100644 --- a/wp-admin/includes/class-wp-list-table.php +++ b/wp-admin/includes/class-wp-list-table.php @@ -81,15 +81,16 @@ class WP_List_Table { $args = wp_parse_args( $args, array( 'plural' => '', 'singular' => '', - 'ajax' => false + 'ajax' => false, + 'screen' => null, ) ); - $screen = get_current_screen(); + $this->screen = convert_to_screen( $args['screen'] ); - add_filter( "manage_{$screen->id}_columns", array( &$this, 'get_columns' ), 0 ); + add_filter( "manage_{$this->screen->id}_columns", array( &$this, 'get_columns' ), 0 ); if ( !$args['plural'] ) - $args['plural'] = $screen->base; + $args['plural'] = $this->screen->base; $args['plural'] = sanitize_key( $args['plural'] ); $args['singular'] = sanitize_key( $args['singular'] ); @@ -242,10 +243,8 @@ class WP_List_Table { * @access public */ function views() { - $screen = get_current_screen(); - $views = $this->get_views(); - $views = apply_filters( 'views_' . $screen->id, $views ); + $views = apply_filters( 'views_' . $this->screen->id, $views ); if ( empty( $views ) ) return; @@ -278,12 +277,10 @@ class WP_List_Table { * @access public */ function bulk_actions() { - $screen = get_current_screen(); - if ( is_null( $this->_actions ) ) { $no_new_actions = $this->_actions = $this->get_bulk_actions(); // This filter can currently only be used to remove actions. - $this->_actions = apply_filters( 'bulk_actions-' . $screen->id, $this->_actions ); + $this->_actions = apply_filters( 'bulk_actions-' . $this->screen->id, $this->_actions ); $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions ); $two = ''; } else { @@ -604,12 +601,10 @@ class WP_List_Table { if ( isset( $this->_column_headers ) ) return $this->_column_headers; - $screen = get_current_screen(); + $columns = get_column_headers( $this->screen ); + $hidden = get_hidden_columns( $this->screen ); - $columns = get_column_headers( $screen ); - $hidden = get_hidden_columns( $screen ); - - $_sortable = apply_filters( "manage_{$screen->id}_sortable_columns", $this->get_sortable_columns() ); + $_sortable = apply_filters( "manage_{$this->screen->id}_sortable_columns", $this->get_sortable_columns() ); $sortable = array(); foreach ( $_sortable as $id => $data ) { @@ -651,8 +646,6 @@ class WP_List_Table { * @param bool $with_id Whether to set the id attribute or not */ function print_column_headers( $with_id = true ) { - $screen = get_current_screen(); - list( $columns, $hidden, $sortable ) = $this->get_column_info(); $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); @@ -914,13 +907,11 @@ class WP_List_Table { * @access private */ function _js_vars() { - $current_screen = get_current_screen(); - $args = array( 'class' => get_class( $this ), 'screen' => array( - 'id' => $current_screen->id, - 'base' => $current_screen->base, + 'id' => $this->screen->id, + 'base' => $this->screen->base, ) ); diff --git a/wp-admin/includes/class-wp-media-list-table.php b/wp-admin/includes/class-wp-media-list-table.php index 77fdda040b..b4126f5ee9 100644 --- a/wp-admin/includes/class-wp-media-list-table.php +++ b/wp-admin/includes/class-wp-media-list-table.php @@ -9,11 +9,12 @@ */ class WP_Media_List_Table extends WP_List_Table { - function __construct() { + function __construct( $args = array() ) { $this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] ); parent::__construct( array( - 'plural' => 'media' + 'plural' => 'media', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, ) ); } diff --git a/wp-admin/includes/class-wp-ms-sites-list-table.php b/wp-admin/includes/class-wp-ms-sites-list-table.php index 1bdc758573..a3e403dc3d 100644 --- a/wp-admin/includes/class-wp-ms-sites-list-table.php +++ b/wp-admin/includes/class-wp-ms-sites-list-table.php @@ -9,9 +9,10 @@ */ class WP_MS_Sites_List_Table extends WP_List_Table { - function __construct() { + function __construct( $args = array() ) { parent::__construct( array( 'plural' => 'sites', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, ) ); } diff --git a/wp-admin/includes/class-wp-ms-themes-list-table.php b/wp-admin/includes/class-wp-ms-themes-list-table.php index 65e9515cc1..4c83b310df 100644 --- a/wp-admin/includes/class-wp-ms-themes-list-table.php +++ b/wp-admin/includes/class-wp-ms-themes-list-table.php @@ -12,24 +12,24 @@ class WP_MS_Themes_List_Table extends WP_List_Table { var $site_id; var $is_site_themes; - function __construct() { + function __construct( $args = array() ) { global $status, $page; + parent::__construct( array( + 'plural' => 'themes', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, + ) ); + $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all'; if ( !in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken' ) ) ) $status = 'all'; $page = $this->get_pagenum(); - $screen = get_current_screen(); - $this->is_site_themes = ( 'site-themes-network' == $screen->id ) ? true : false; + $this->is_site_themes = ( 'site-themes-network' == $this->screen->id ) ? true : false; if ( $this->is_site_themes ) $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; - - parent::__construct( array( - 'plural' => 'themes' - ) ); } function get_table_classes() { diff --git a/wp-admin/includes/class-wp-plugins-list-table.php b/wp-admin/includes/class-wp-plugins-list-table.php index df569f109b..aed80b579c 100644 --- a/wp-admin/includes/class-wp-plugins-list-table.php +++ b/wp-admin/includes/class-wp-plugins-list-table.php @@ -9,9 +9,14 @@ */ class WP_Plugins_List_Table extends WP_List_Table { - function __construct() { + function __construct( $args = array() ) { global $status, $page; + parent::__construct( array( + 'plural' => 'plugins', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, + ) ); + $status = 'all'; if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search' ) ) ) $status = $_REQUEST['plugin_status']; @@ -20,10 +25,6 @@ class WP_Plugins_List_Table extends WP_List_Table { $_SERVER['REQUEST_URI'] = add_query_arg('s', stripslashes($_REQUEST['s']) ); $page = $this->get_pagenum(); - - parent::__construct( array( - 'plural' => 'plugins', - ) ); } function get_table_classes() { @@ -50,7 +51,7 @@ class WP_Plugins_List_Table extends WP_List_Table { 'dropins' => array() ); - $screen = get_current_screen(); + $screen = $this->screen; if ( ! is_multisite() || ( $screen->is_network && current_user_can('manage_network_plugins') ) ) { if ( apply_filters( 'show_advanced_plugins', true, 'mustuse' ) ) @@ -91,7 +92,7 @@ class WP_Plugins_List_Table extends WP_List_Table { || ( $screen->is_network && is_plugin_active_for_network( $plugin_file ) ) ) { $plugins['active'][ $plugin_file ] = $plugin_data; } else { - if ( !$screen->is_network && isset( $recently_activated[ $plugin_file ] ) ) // Was the plugin recently activated? + if ( ! $screen->is_network && isset( $recently_activated[ $plugin_file ] ) ) // Was the plugin recently activated? $plugins['recently_activated'][ $plugin_file ] = $plugin_data; $plugins['inactive'][ $plugin_file ] = $plugin_data; } @@ -236,15 +237,13 @@ class WP_Plugins_List_Table extends WP_List_Table { $actions = array(); - $screen = get_current_screen(); - if ( 'active' != $status ) - $actions['activate-selected'] = $screen->is_network ? __( 'Network Activate' ) : __( 'Activate' ); + $actions['activate-selected'] = $this->screen->is_network ? __( 'Network Activate' ) : __( 'Activate' ); if ( 'inactive' != $status && 'recent' != $status ) - $actions['deactivate-selected'] = $screen->is_network ? __( 'Network Deactivate' ) : __( 'Deactivate' ); + $actions['deactivate-selected'] = $this->screen->is_network ? __( 'Network Deactivate' ) : __( 'Deactivate' ); - if ( !is_multisite() || $screen->is_network ) { + if ( !is_multisite() || $this->screen->is_network ) { if ( current_user_can( 'update_plugins' ) ) $actions['update-selected'] = __( 'Update' ); if ( current_user_can( 'delete_plugins' ) && ( 'active' != $status ) ) @@ -271,9 +270,7 @@ class WP_Plugins_List_Table extends WP_List_Table { echo '<div class="alignleft actions">'; - $screen = get_current_screen(); - - if ( ! $screen->is_network && 'recently_activated' == $status ) + if ( ! $this->screen->is_network && 'recently_activated' == $status ) submit_button( __( 'Clear List' ), 'small', 'clear-recent-list', false ); elseif ( 'top' == $which && 'mustuse' == $status ) echo '<p>' . sprintf( __( 'Files in the <code>%s</code> directory are executed automatically.' ), str_replace( ABSPATH, '/', WPMU_PLUGIN_DIR ) ) . '</p>'; @@ -293,9 +290,7 @@ class WP_Plugins_List_Table extends WP_List_Table { function display_rows() { global $status; - $screen = get_current_screen(); - - if ( is_multisite() && !$screen->is_network && in_array( $status, array( 'mustuse', 'dropins' ) ) ) + if ( is_multisite() && ! $this->screen->is_network && in_array( $status, array( 'mustuse', 'dropins' ) ) ) return; foreach ( $this->items as $plugin_file => $plugin_data ) @@ -306,8 +301,7 @@ class WP_Plugins_List_Table extends WP_List_Table { global $status, $page, $s, $totals; $context = $status; - - $screen = get_current_screen(); + $screen = $this->screen; // preorder $actions = array( diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index c9126ef7ce..aea8f7a54b 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -45,10 +45,15 @@ class WP_Posts_List_Table extends WP_List_Table { */ var $sticky_posts_count = 0; - function __construct() { + function __construct( $args = array() ) { global $post_type_object, $wpdb; - $post_type = get_current_screen()->post_type; + parent::__construct( array( + 'plural' => 'posts', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, + ) ); + + $post_type = $this->screen->post_type; $post_type_object = get_post_type_object( $post_type ); if ( !current_user_can( $post_type_object->cap->edit_others_posts ) ) { @@ -66,28 +71,22 @@ class WP_Posts_List_Table extends WP_List_Table { $sticky_posts = implode( ', ', array_map( 'absint', (array) $sticky_posts ) ); $this->sticky_posts_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( 1 ) FROM $wpdb->posts WHERE post_type = %s AND post_status != 'trash' AND ID IN ($sticky_posts)", $post_type ) ); } - - parent::__construct( array( - 'plural' => 'posts', - ) ); } function ajax_user_can() { - global $post_type_object; - - return current_user_can( $post_type_object->cap->edit_posts ); + return current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_posts ); } function prepare_items() { - global $post_type_object, $avail_post_stati, $wp_query, $per_page, $mode; + global $avail_post_stati, $wp_query, $per_page, $mode; $avail_post_stati = wp_edit_posts_query(); - $this->hierarchical_display = ( $post_type_object->hierarchical && 'menu_order title' == $wp_query->query['orderby'] ); + $this->hierarchical_display = ( is_post_type_hierarchical( $this->screen->post_type ) && 'menu_order title' == $wp_query->query['orderby'] ); $total_items = $this->hierarchical_display ? $wp_query->post_count : $wp_query->found_posts; - $post_type = $post_type_object->name; + $post_type = $this->screen->post_type; $per_page = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' ); $per_page = apply_filters( 'edit_posts_per_page', $per_page, $post_type ); @@ -112,18 +111,16 @@ class WP_Posts_List_Table extends WP_List_Table { } function no_items() { - global $post_type_object; - if ( isset( $_REQUEST['post_status'] ) && 'trash' == $_REQUEST['post_status'] ) - echo $post_type_object->labels->not_found_in_trash; + echo get_post_type_object( $this->screen->post_type )->labels->not_found_in_trash; else - echo $post_type_object->labels->not_found; + echo get_post_type_object( $this->screen->post_type )->labels->not_found; } function get_views() { - global $post_type_object, $locked_post_status, $avail_post_stati; + global $locked_post_status, $avail_post_stati; - $post_type = $post_type_object->name; + $post_type = $this->screen->post_type; if ( !empty($locked_post_status) ) return array(); @@ -198,15 +195,15 @@ class WP_Posts_List_Table extends WP_List_Table { } function extra_tablenav( $which ) { - global $post_type_object, $cat; + global $cat; ?> <div class="alignleft actions"> <?php if ( 'top' == $which && !is_singular() ) { - $this->months_dropdown( $post_type_object->name ); + $this->months_dropdown( $this->screen->post_type ); - if ( is_object_in_taxonomy( $post_type_object->name, 'category' ) ) { + if ( is_object_in_taxonomy( $this->screen->post_type, 'category' ) ) { $dropdown_options = array( 'show_option_all' => __( 'View all categories' ), 'hide_empty' => 0, @@ -221,7 +218,7 @@ class WP_Posts_List_Table extends WP_List_Table { submit_button( __( 'Filter' ), 'small', false, false, array( 'id' => 'post-query-submit' ) ); } - if ( $this->is_trash && current_user_can( $post_type_object->cap->edit_others_posts ) ) { + if ( $this->is_trash && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts ) ) { submit_button( __( 'Empty Trash' ), 'small apply', 'delete_all', false ); } ?> @@ -237,27 +234,20 @@ class WP_Posts_List_Table extends WP_List_Table { } function pagination( $which ) { - global $post_type_object, $mode; + global $mode; parent::pagination( $which ); - if ( 'top' == $which && !$post_type_object->hierarchical ) + if ( 'top' == $which && ! is_post_type_hierarchical( $this->screen->post_type ) ) $this->view_switcher( $mode ); } function get_table_classes() { - global $post_type_object; - - return array( 'widefat', 'fixed', $post_type_object->hierarchical ? 'pages' : 'posts' ); + return array( 'widefat', 'fixed', is_post_type_hierarchical( $this->screen->post_type ) ? 'pages' : 'posts' ); } function get_columns() { - $screen = get_current_screen(); - - if ( empty( $screen ) ) - $post_type = 'post'; - else - $post_type = $screen->post_type; + $post_type = $this->screen->post_type; $posts_columns = array(); @@ -313,7 +303,7 @@ class WP_Posts_List_Table extends WP_List_Table { } function display_rows( $posts = array(), $level = 0 ) { - global $wp_query, $post_type_object, $per_page; + global $wp_query, $per_page; if ( empty( $posts ) ) $posts = $wp_query->posts; @@ -701,7 +691,7 @@ class WP_Posts_List_Table extends WP_List_Table { function inline_edit() { global $mode; - $screen = get_current_screen(); + $screen = $this->screen; $post = get_default_post_to_edit( $screen->post_type ); $post_type_object = get_post_type_object( $screen->post_type ); @@ -733,8 +723,8 @@ class WP_Posts_List_Table extends WP_List_Table { $bulk = 0; while ( $bulk < 2 ) { ?> - <tr id="<?php echo $bulk ? 'bulk-edit' : 'inline-edit'; ?>" class="inline-edit-row inline-edit-row-<?php echo "$hclass inline-edit-$screen->post_type "; - echo $bulk ? "bulk-edit-row bulk-edit-row-$hclass bulk-edit-$screen->post_type" : "quick-edit-row quick-edit-row-$hclass inline-edit-$screen->post_type"; + <tr id="<?php echo $bulk ? 'bulk-edit' : 'inline-edit'; ?>" class="inline-edit-row inline-edit-row-<?php echo "$hclass inline-edit-" . $screen->post_type; + echo $bulk ? "bulk-edit-row bulk-edit-row-$hclass bulk-edit-{$screen->post_type}" : "quick-edit-row quick-edit-row-$hclass inline-edit-{$screen->post_type}"; ?>" style="display: none"><td colspan="<?php echo $this->get_column_count(); ?>" class="colspanchange"> <fieldset class="inline-edit-col-left"><div class="inline-edit-col"> diff --git a/wp-admin/includes/class-wp-terms-list-table.php b/wp-admin/includes/class-wp-terms-list-table.php index d03b096547..52e7f9c461 100644 --- a/wp-admin/includes/class-wp-terms-list-table.php +++ b/wp-admin/includes/class-wp-terms-list-table.php @@ -11,43 +11,44 @@ class WP_Terms_List_Table extends WP_List_Table { var $callback_args; - function __construct() { - global $post_type, $taxonomy, $tax; - - wp_reset_vars( array( 'action', 'taxonomy', 'post_type' ) ); - - if ( empty( $taxonomy ) ) - $taxonomy = 'post_tag'; - - if ( !taxonomy_exists( $taxonomy ) ) - wp_die( __( 'Invalid taxonomy' ) ); - - $tax = get_taxonomy( $taxonomy ); - - if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) ) - $post_type = 'post'; + function __construct( $args = array() ) { + global $post_type, $taxonomy, $action, $tax; parent::__construct( array( 'plural' => 'tags', 'singular' => 'tag', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, ) ); + + $action = $this->screen->action; + $post_type = $this->screen->post_type; + $taxonomy = $this->screen->taxonomy; + + if ( empty( $taxonomy ) ) + $taxonomy = 'post_tag'; + + if ( ! taxonomy_exists( $taxonomy ) ) + wp_die( __( 'Invalid taxonomy' ) ); + + $tax = get_taxonomy( $taxonomy ); + + // @todo Still needed? Maybe just the show_ui part. + if ( empty( $post_type ) || !in_array( $post_type, get_post_types( array( 'show_ui' => true ) ) ) ) + $post_type = 'post'; + } function ajax_user_can() { - global $tax; - - return current_user_can( $tax->cap->manage_terms ); + return current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->manage_terms ); } function prepare_items() { - global $taxonomy; + $tags_per_page = $this->get_items_per_page( 'edit_' . $this->screen->taxonomy . '_per_page' ); - $tags_per_page = $this->get_items_per_page( 'edit_' . $taxonomy . '_per_page' ); - - if ( 'post_tag' == $taxonomy ) { + if ( 'post_tag' == $this->screen->taxonomy ) { $tags_per_page = apply_filters( 'edit_tags_per_page', $tags_per_page ); $tags_per_page = apply_filters( 'tagsperpage', $tags_per_page ); // Old filter - } elseif ( 'category' == $taxonomy ) { + } elseif ( 'category' == $this->screen->taxonomy ) { $tags_per_page = apply_filters( 'edit_categories_per_page', $tags_per_page ); // Old filter } @@ -68,7 +69,7 @@ class WP_Terms_List_Table extends WP_List_Table { $this->callback_args = $args; $this->set_pagination_args( array( - 'total_items' => wp_count_terms( $taxonomy, compact( 'search' ) ), + 'total_items' => wp_count_terms( $this->screen->taxonomy, compact( 'search' ) ), 'per_page' => $tags_per_page, ) ); } @@ -93,8 +94,6 @@ class WP_Terms_List_Table extends WP_List_Table { } function get_columns() { - global $taxonomy, $post_type; - $columns = array( 'cb' => '<input type="checkbox" />', 'name' => _x( 'Name', 'term name' ), @@ -102,10 +101,10 @@ class WP_Terms_List_Table extends WP_List_Table { 'slug' => __( 'Slug' ), ); - if ( 'link_category' == $taxonomy ) { + if ( 'link_category' == $this->screen->taxonomy ) { $columns['links'] = __( 'Links' ); } else { - $post_type_object = get_post_type_object( $post_type ); + $post_type_object = get_post_type_object( $this->screen->post_type ); $columns['posts'] = $post_type_object ? $post_type_object->labels->name : __( 'Posts' ); } @@ -123,7 +122,7 @@ class WP_Terms_List_Table extends WP_List_Table { } function display_rows_or_placeholder() { - global $taxonomy; + $taxonomy = $this->screen->taxonomy; $args = wp_parse_args( $this->callback_args, array( 'page' => 1, @@ -231,11 +230,9 @@ class WP_Terms_List_Table extends WP_List_Table { } function column_cb( $tag ) { - global $taxonomy, $tax; + $default_term = get_option( 'default_' . $this->screen->taxonomy ); - $default_term = get_option( 'default_' . $taxonomy ); - - if ( current_user_can( $tax->cap->delete_terms ) && $tag->term_id != $default_term ) + if ( current_user_can( get_taxonomy( $this->screen->taxonomy )->cap->delete_terms ) && $tag->term_id != $default_term ) return '<label class="screen-reader-text" for="cb-select-' . $tag->term_id . '">' . sprintf( __( 'Select %s' ), $tag->name ) . '</label>' . '<input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" id="cb-select-' . $tag->term_id . '" />'; @@ -243,14 +240,15 @@ class WP_Terms_List_Table extends WP_List_Table { } function column_name( $tag ) { - global $taxonomy, $tax, $post_type; + $taxonomy = $this->screen->taxonomy; + $tax = get_taxonomy( $taxonomy ); $default_term = get_option( 'default_' . $taxonomy ); $pad = str_repeat( '— ', max( 0, $this->level ) ); $name = apply_filters( 'term_name', $pad . ' ' . $tag->name, $tag ); $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' ); - $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $post_type ) ); + $edit_link = esc_url( get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) ); $out = '<strong><a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit “%s”' ), $name ) ) . '">' . $name . '</a></strong><br />'; @@ -284,13 +282,11 @@ class WP_Terms_List_Table extends WP_List_Table { } function column_posts( $tag ) { - global $taxonomy, $post_type; - $count = number_format_i18n( $tag->count ); - $tax = get_taxonomy( $taxonomy ); + $tax = get_taxonomy( $this->screen->taxonomy ); - $ptype_object = get_post_type_object( $post_type ); + $ptype_object = get_post_type_object( $this->screen->post_type ); if ( ! $ptype_object->show_ui ) return $count; @@ -300,8 +296,8 @@ class WP_Terms_List_Table extends WP_List_Table { $args = array( 'taxonomy' => $tax->name, 'term' => $tag->slug ); } - if ( 'post' != $post_type ) - $args['post_type'] = $post_type; + if ( 'post' != $this->screen->post_type ) + $args['post_type'] = $this->screen->post_type; return "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>$count</a>"; } @@ -314,9 +310,7 @@ class WP_Terms_List_Table extends WP_List_Table { } function column_default( $tag, $column_name ) { - $screen = get_current_screen(); - - return apply_filters( "manage_{$screen->taxonomy}_custom_column", '', $column_name, $tag->term_id ); + return apply_filters( "manage_{$this->screen->taxonomy}_custom_column", '', $column_name, $tag->term_id ); } /** @@ -325,7 +319,7 @@ class WP_Terms_List_Table extends WP_List_Table { * @since 3.1.0 */ function inline_edit() { - global $post_type, $tax; + $tax = get_taxonomy( $this->screen->taxonomy ); if ( ! current_user_can( $tax->cap->edit_terms ) ) return; @@ -358,7 +352,7 @@ class WP_Terms_List_Table extends WP_List_Table { if ( isset( $core_columns[$column_name] ) ) continue; - do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $tax->name ); + do_action( 'quick_edit_custom_box', $column_name, 'edit-tags', $this->screen->taxonomy ); } ?> @@ -370,8 +364,8 @@ class WP_Terms_List_Table extends WP_List_Table { <img class="waiting" style="display:none;" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" /> <span class="error" style="display:none;"></span> <?php wp_nonce_field( 'taxinlineeditnonce', '_inline_edit', false ); ?> - <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $tax->name ); ?>" /> - <input type="hidden" name="post_type" value="<?php echo esc_attr( $post_type ); ?>" /> + <input type="hidden" name="taxonomy" value="<?php echo esc_attr( $this->screen->taxonomy ); ?>" /> + <input type="hidden" name="post_type" value="<?php echo esc_attr( $this->screen->post_type ); ?>" /> <br class="clear" /> </p> </td></tr> diff --git a/wp-admin/includes/class-wp-themes-list-table.php b/wp-admin/includes/class-wp-themes-list-table.php index 5bfed52714..7a89fc889b 100644 --- a/wp-admin/includes/class-wp-themes-list-table.php +++ b/wp-admin/includes/class-wp-themes-list-table.php @@ -12,9 +12,10 @@ class WP_Themes_List_Table extends WP_List_Table { protected $search_terms = array(); var $features = array(); - function __construct() { + function __construct( $args = array() ) { parent::__construct( array( 'ajax' => true, + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, ) ); } diff --git a/wp-admin/includes/class-wp-users-list-table.php b/wp-admin/includes/class-wp-users-list-table.php index 4ebef6644a..7039721418 100644 --- a/wp-admin/includes/class-wp-users-list-table.php +++ b/wp-admin/includes/class-wp-users-list-table.php @@ -12,17 +12,17 @@ class WP_Users_List_Table extends WP_List_Table { var $site_id; var $is_site_users; - function __construct() { - $screen = get_current_screen(); - $this->is_site_users = 'site-users-network' == $screen->id; + function __construct( $args = array() ) { + parent::__construct( array( + 'singular' => 'user', + 'plural' => 'users', + 'screen' => isset( $args['screen'] ) ? $args['screen'] : null, + ) ); + + $this->is_site_users = 'site-users-network' == $this->screen->id; if ( $this->is_site_users ) $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; - - parent::__construct( array( - 'singular' => 'user', - 'plural' => 'users' - ) ); } function ajax_user_can() { diff --git a/wp-admin/includes/list-table.php b/wp-admin/includes/list-table.php index 2dff578e0d..bccf0cab33 100644 --- a/wp-admin/includes/list-table.php +++ b/wp-admin/includes/list-table.php @@ -14,9 +14,10 @@ * @since 3.1.0 * * @param string $class The type of the list table, which is the class name. + * @param array $args Optional. Arguments to pass to the class. Accepts 'screen'. * @return object|bool Object on success, false if the class does not exist. */ -function _get_list_table( $class ) { +function _get_list_table( $class, $args = array() ) { $core_classes = array( //Site Admin 'WP_Posts_List_Table' => 'posts', @@ -39,7 +40,13 @@ function _get_list_table( $class ) { if ( isset( $core_classes[ $class ] ) ) { foreach ( (array) $core_classes[ $class ] as $required ) require_once( ABSPATH . 'wp-admin/includes/class-wp-' . $required . '-list-table.php' ); - return new $class; + + if ( isset( $args['screen'] ) ) + $args['screen'] = convert_to_screen( $args['screen'] ); + else + $args['screen'] = get_current_screen(); + + return new $class( $args ); } return false;