From 728285edbd1ed9baac3070f986d8decb09f21b3e Mon Sep 17 00:00:00 2001 From: scribu Date: Wed, 22 Sep 2010 00:10:39 +0000 Subject: [PATCH] Introduce WP_List_Table::current_action() and use throughout admin list screens. See #14579 git-svn-id: https://develop.svn.wordpress.org/trunk@15642 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/edit-comments.php | 11 +- wp-admin/edit-tags.php | 5 +- wp-admin/edit.php | 23 ++-- wp-admin/includes/default-list-tables.php | 48 +++++++++ wp-admin/includes/list-table.php | 18 ++++ wp-admin/link-manager.php | 6 +- wp-admin/plugins.php | 13 +-- wp-admin/upload.php | 123 +++++++++++----------- wp-admin/users.php | 13 +-- 9 files changed, 155 insertions(+), 105 deletions(-) diff --git a/wp-admin/edit-comments.php b/wp-admin/edit-comments.php index 3a27605348..8b349a764f 100644 --- a/wp-admin/edit-comments.php +++ b/wp-admin/edit-comments.php @@ -12,20 +12,21 @@ require_once('./admin.php'); $wp_list_table = get_list_table('comments'); $wp_list_table->check_permissions(); -if ( isset( $_REQUEST['doaction'] ) || isset( $_REQUEST['doaction2'] ) || isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) { +$doaction = $wp_list_table->current_action(); + +if ( $doaction ) { check_admin_referer( 'bulk-comments' ); - if ( ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) && !empty( $_REQUEST['pagegen_timestamp'] ) ) { + if ( 'delete_all' == $do_action && !empty( $_REQUEST['pagegen_timestamp'] ) ) { $comment_status = $wpdb->escape( $_REQUEST['comment_status'] ); $delete_time = $wpdb->escape( $_REQUEST['pagegen_timestamp'] ); $comment_ids = $wpdb->get_col( "SELECT comment_ID FROM $wpdb->comments WHERE comment_approved = '$comment_status' AND '$delete_time' > comment_date_gmt" ); $doaction = 'delete'; - } elseif ( ( $_REQUEST['action'] != -1 || $_REQUEST['action2'] != -1 ) && isset( $_REQUEST['delete_comments'] ) ) { + } elseif ( isset( $_REQUEST['delete_comments'] ) ) { $comment_ids = $_REQUEST['delete_comments']; $doaction = ( $_REQUEST['action'] != -1 ) ? $_REQUEST['action'] : $_REQUEST['action2']; - } elseif ( $_REQUEST['doaction'] == 'undo' && isset( $_REQUEST['ids'] ) ) { + } elseif ( isset( $_REQUEST['ids'] ) ) { $comment_ids = array_map( 'absint', explode( ',', $_REQUEST['ids'] ) ); - $doaction = $_REQUEST['action']; } else { wp_redirect( wp_get_referer() ); } diff --git a/wp-admin/edit-tags.php b/wp-admin/edit-tags.php index 73ad049b30..e106efd019 100644 --- a/wp-admin/edit-tags.php +++ b/wp-admin/edit-tags.php @@ -22,10 +22,7 @@ if ( 'post' != $post_type ) { $submenu_file = "edit-tags.php?taxonomy=$taxonomy"; } -if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['delete_tags'] ) && ( 'delete' == $_REQUEST['action'] || 'delete' == $_REQUEST['action2'] ) ) - $action = 'bulk-delete'; - -switch ( $action ) { +switch ( $wp_list_table->current_action() ) { case 'add-tag': diff --git a/wp-admin/edit.php b/wp-admin/edit.php index 5d7ef6dc7b..215805e7c5 100644 --- a/wp-admin/edit.php +++ b/wp-admin/edit.php @@ -21,21 +21,25 @@ if ( $_redirect = intval( max( @$_REQUEST['p'], @$_REQUEST['attachment_id'], @$_ } // Handle bulk actions -if ( isset($_REQUEST['doaction']) || isset($_REQUEST['doaction2']) || isset($_REQUEST['delete_all']) || isset($_REQUEST['delete_all2']) || isset($_REQUEST['bulk_edit']) ) { +$doaction = $wp_list_table->current_action(); + +if ( $doaction ) { check_admin_referer('bulk-posts'); $sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() ); if ( strpos($sendback, 'post.php') !== false ) $sendback = admin_url($post_new_file); - if ( isset($_REQUEST['delete_all']) || isset($_REQUEST['delete_all2']) ) { - $post_status = preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['post_status']); - $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type=%s AND post_status = %s", $post_type, $post_status ) ); + if ( 'delete_all' == $doaction ) { + $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" ); $doaction = 'delete'; - } elseif ( ( $_REQUEST['action'] != -1 || $_REQUEST['action2'] != -1 ) && ( isset($_REQUEST['post']) || isset($_REQUEST['ids']) ) ) { - $post_ids = isset($_REQUEST['post']) ? array_map( 'intval', (array) $_REQUEST['post'] ) : explode(',', $_REQUEST['ids']); - $doaction = ($_REQUEST['action'] != -1) ? $_REQUEST['action'] : $_REQUEST['action2']; - } else { + } elseif ( isset( $_REQUEST['media'] ) ) { + $post_ids = $_REQUEST['media']; + } elseif ( isset( $_REQUEST['ids'] ) ) { + $post_ids = explode( ',', $_REQUEST['ids'] ); + } + + if ( !isset( $post_ids ) ) { wp_redirect( admin_url("edit.php?post_type=$post_type") ); } @@ -97,8 +101,7 @@ if ( isset($_REQUEST['doaction']) || isset($_REQUEST['doaction2']) || isset($_RE break; } - if ( isset($_REQUEST['action']) ) - $sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback ); + $sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback ); wp_redirect($sendback); exit(); diff --git a/wp-admin/includes/default-list-tables.php b/wp-admin/includes/default-list-tables.php index f377659cf9..a31ea13ad2 100644 --- a/wp-admin/includes/default-list-tables.php +++ b/wp-admin/includes/default-list-tables.php @@ -215,6 +215,13 @@ class WP_Posts_Table extends WP_List_Table { } } + function current_action() { + if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) + return 'delete_all'; + + return parent::current_action(); + } + function pagination( $which ) { global $post_type_object, $mode; @@ -1080,6 +1087,19 @@ class WP_Media_Table extends WP_List_Table { '; } + function current_action() { + if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) + return 'delete_all'; + + return parent::current_action(); + } + function get_columns() { global $mode; @@ -3458,6 +3499,13 @@ class WP_Plugins_Table extends WP_List_Table { \n"; } + /** + * Get the current action selected from the bulk actions dropdown. + * + * @since 3.1.0 + * @access public + * + * @return string|bool The action name or False if no action was selected + */ + function current_action() { + if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) + return $_REQUEST['action']; + + if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] ) + return $_REQUEST['action2']; + + return false; + } + /** * Generate row actions div * diff --git a/wp-admin/link-manager.php b/wp-admin/link-manager.php index bc118e25ef..84ed71f084 100644 --- a/wp-admin/link-manager.php +++ b/wp-admin/link-manager.php @@ -13,10 +13,10 @@ $wp_list_table = get_list_table('links'); $wp_list_table->check_permissions(); // Handle bulk deletes -if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['linkcheck'] ) ) { - check_admin_referer( 'bulk-bookmarks' ); +$doaction = $wp_list_table->current_action(); - $doaction = $_REQUEST['action'] ? $_REQUEST['action'] : $_REQUEST['action2']; +if ( $doaction && isset( $_REQUEST['linkcheck'] ) ) { + check_admin_referer( 'bulk-bookmarks' ); if ( 'delete' == $doaction ) { $bulklinks = (array) $_REQUEST['linkcheck']; diff --git a/wp-admin/plugins.php b/wp-admin/plugins.php index 095079ca89..bf64b9ae0b 100644 --- a/wp-admin/plugins.php +++ b/wp-admin/plugins.php @@ -12,21 +12,14 @@ require_once('./admin.php'); $wp_list_table = get_list_table('plugins'); $wp_list_table->check_permissions(); -if ( isset($_POST['clear-recent-list']) ) - $action = 'clear-recent-list'; -elseif ( !empty($_REQUEST['action']) ) - $action = $_REQUEST['action']; -elseif ( !empty($_REQUEST['action2']) ) - $action = $_REQUEST['action2']; -else - $action = false; +$action = $wp_list_table->current_action(); $plugin = isset($_REQUEST['plugin']) ? $_REQUEST['plugin'] : ''; -//Clean up request URI from temporary args for screen options/paging uri's to work as expected. +// Clean up request URI from temporary args for screen options/paging uri's to work as expected. $_SERVER['REQUEST_URI'] = remove_query_arg(array('error', 'deleted', 'activate', 'activate-multi', 'deactivate', 'deactivate-multi', '_error_nonce'), $_SERVER['REQUEST_URI']); -if ( !empty($action) ) { +if ( $action ) { $network_wide = false; if ( ( isset( $_GET['networkwide'] ) || 'network-activate-selected' == $action ) && is_multisite() && current_user_can( 'manage_network_plugins' ) ) $network_wide = true; diff --git a/wp-admin/upload.php b/wp-admin/upload.php index 3c1840c7bd..4d864eb9a2 100644 --- a/wp-admin/upload.php +++ b/wp-admin/upload.php @@ -13,73 +13,18 @@ $wp_list_table = get_list_table('media'); $wp_list_table->check_permissions(); // Handle bulk actions -if ( isset($_REQUEST['find_detached']) ) { +$doaction = $wp_list_table->current_action(); + +if ( $doaction ) { check_admin_referer('bulk-media'); - if ( !current_user_can('edit_posts') ) - wp_die( __('You are not allowed to scan for lost attachments.') ); - - $lost = $wpdb->get_col( " - SELECT ID FROM $wpdb->posts - WHERE post_type = 'attachment' AND post_parent > '0' - AND post_parent NOT IN ( - SELECT ID FROM $wpdb->posts - WHERE post_type NOT IN ( 'attachment', '" . join( "', '", get_post_types( array( 'public' => false ) ) ) . "' ) - ) - " ); - - $_REQUEST['detached'] = 1; - -} elseif ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) ) { - check_admin_referer( 'bulk-media' ); - - $parent_id = (int) $_REQUEST['found_post_id']; - if ( !$parent_id ) - return; - - $parent = &get_post( $parent_id ); - if ( !current_user_can( 'edit_post', $parent_id ) ) - wp_die( __( 'You are not allowed to edit this post.' ) ); - - $attach = array(); - foreach ( (array) $_REQUEST['media'] as $att_id ) { - $att_id = (int) $att_id; - - if ( !current_user_can( 'edit_post', $att_id ) ) - continue; - - $attach[] = $att_id; - clean_attachment_cache( $att_id ); - } - - if ( ! empty( $attach ) ) { - $attach = implode( ',', $attach ); - $attached = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $attach )", $parent_id ) ); - } - - if ( isset( $attached ) ) { - $location = 'upload.php'; - if ( $referer = wp_get_referer() ) { - if ( false !== strpos( $referer, 'upload.php' ) ) - $location = $referer; - } - - $location = add_query_arg( array( 'attached' => $attached ) , $location ); - wp_redirect( $location ); - exit; - } - -} elseif ( isset( $_REQUEST['doaction'] ) || isset( $_REQUEST['doaction2'] ) || isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) { - check_admin_referer( 'bulk-media' ); - - if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) { + if ( 'delete_all' == $doaction ) { $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" ); $doaction = 'delete'; - } elseif ( ( $_REQUEST['action'] != -1 || $_REQUEST['action2'] != -1 ) && ( isset( $_REQUEST['media'] ) || isset( $_REQUEST['ids'] ) ) ) { - $post_ids = isset( $_REQUEST['media'] ) ? $_REQUEST['media'] : explode( ',', $_REQUEST['ids'] ); - $doaction = ( $_REQUEST['action'] != -1 ) ? $_REQUEST['action'] : $_REQUEST['action2']; - } else { - wp_redirect( $_SERVER['HTTP_REFERER'] ); + } elseif ( isset( $_REQUEST['media'] ) ) { + $post_ids = $_REQUEST['media']; + } elseif ( isset( $_REQUEST['ids'] ) ) { + $post_ids = explode( ',', $_REQUEST['ids'] ); } $location = 'upload.php'; @@ -89,6 +34,58 @@ if ( isset($_REQUEST['find_detached']) ) { } switch ( $doaction ) { + case 'find_detached': + if ( !current_user_can('edit_posts') ) + wp_die( __('You are not allowed to scan for lost attachments.') ); + + $lost = $wpdb->get_col( " + SELECT ID FROM $wpdb->posts + WHERE post_type = 'attachment' AND post_parent > '0' + AND post_parent NOT IN ( + SELECT ID FROM $wpdb->posts + WHERE post_type NOT IN ( 'attachment', '" . join( "', '", get_post_types( array( 'public' => false ) ) ) . "' ) + ) + " ); + + $_REQUEST['detached'] = 1; + break; + case 'attach': + $parent_id = (int) $_REQUEST['found_post_id']; + if ( !$parent_id ) + return; + + $parent = &get_post( $parent_id ); + if ( !current_user_can( 'edit_post', $parent_id ) ) + wp_die( __( 'You are not allowed to edit this post.' ) ); + + $attach = array(); + foreach ( (array) $_REQUEST['media'] as $att_id ) { + $att_id = (int) $att_id; + + if ( !current_user_can( 'edit_post', $att_id ) ) + continue; + + $attach[] = $att_id; + clean_attachment_cache( $att_id ); + } + + if ( ! empty( $attach ) ) { + $attach = implode( ',', $attach ); + $attached = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $attach )", $parent_id ) ); + } + + if ( isset( $attached ) ) { + $location = 'upload.php'; + if ( $referer = wp_get_referer() ) { + if ( false !== strpos( $referer, 'upload.php' ) ) + $location = $referer; + } + + $location = add_query_arg( array( 'attached' => $attached ) , $location ); + wp_redirect( $location ); + exit; + } + break; case 'trash': foreach ( (array) $post_ids as $post_id ) { if ( !current_user_can( 'delete_post', $post_id ) ) diff --git a/wp-admin/users.php b/wp-admin/users.php index 55e6751ce8..d1fbc25de9 100644 --- a/wp-admin/users.php +++ b/wp-admin/users.php @@ -29,15 +29,6 @@ add_contextual_help($current_screen, '

' . __('Support Forums') . '

' ); -$update = $doaction = ''; -if ( isset($_REQUEST['action']) ) - $doaction = $_REQUEST['action'] ? $_REQUEST['action'] : $_REQUEST['action2']; - -if ( "-1" == $doaction ) { - if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) ) - $doaction = 'promote'; -} - if ( empty($_REQUEST) ) { $referer = ''; } elseif ( isset($_REQUEST['wp_http_referer']) ) { @@ -48,7 +39,9 @@ if ( empty($_REQUEST) ) { $referer = ''; } -switch ($doaction) { +$update = ''; + +switch ( $wp_list_table->current_action() ) { /* Bulk Dropdown menu Role changes */ case 'promote':