Newer
Older
<?php
namespace Automattic\WP\Bulk_Edit_Cron_Offload;
/**
* Prefix for bulk-process hook invoked by request-specific classes
*/
const ACTION = 'a8c_bulk_edit_cron_';
add_action( 'load-edit.php', array( __CLASS__, 'intercept' ) );
}
/**
* Call appropriate handler
*/
if ( ! self::should_intercept_request() ) {
// Validate request
check_admin_referer( 'bulk-posts' );
// Parse request to determine what to do
$vars = self::capture_vars();
$action = self::build_hook( $vars->action );
if ( ! self::bulk_action_allowed( $vars->action ) ) {
return;
}
// Pass request to a class to handle offloading to cron, UX, etc
do_action( $action, $vars );
// Only skip Core's default handling when
if ( has_action( $action ) ) {
self::skip_core_processing();
/**
* Determine if current request is a bulk edit
*/
private static function should_intercept_request() {
Erick Hitter
committed
if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
return true;
} elseif ( isset( $_REQUEST['action'] ) && '-1' !== $_REQUEST['action'] ) {
return true;
} elseif ( isset( $_REQUEST['action2'] ) && '-1' !== $_REQUEST['action2'] ) {
return true;
}
return false;
$vars = (object) array_fill_keys( array( 'user_id', 'action', 'post_type', 'posts', 'tax_input', 'post_author', 'comment_status', 'ping_status', 'post_status', 'post_sticky', 'post_format', ), null );
$vars->user_id = get_current_user_id();
Erick Hitter
committed
if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
$vars->post_status = $_REQUEST['post_status'];
} elseif ( isset( $_REQUEST['action'] ) && '-1' !== $_REQUEST['action'] ) {
$vars->action = $_REQUEST['action'];
} elseif ( isset( $_REQUEST['action2'] ) && '-1' !== $_REQUEST['action2'] ) {
$vars->action = $_REQUEST['action2'];
if ( isset( $_REQUEST['post_type'] ) && ! empty( $_REQUEST['post_type'] ) ) {
$vars->post_type = $_REQUEST['post_type'];
if ( isset( $_REQUEST['post'] ) && is_array( $_REQUEST['post'] ) ) {
$vars->posts = array_map( 'absint', $_REQUEST['post'] );
}
if ( isset( $_REQUEST['tax_input'] ) && is_array( $_REQUEST['tax_input'] ) ) {
$vars->tax_input = $_REQUEST['tax_input'];
if ( isset( $_REQUEST['post_author'] ) && -1 !== (int) $_REQUEST['post_author'] ) {
$vars->post_author = (int) $_REQUEST['post_author'];
}
if ( isset( $_REQUEST['comment_status'] ) && ! empty( $_REQUEST['comment_status'] ) ) {
$vars->comment_status = $_REQUEST['comment_status'];
}
if ( isset( $_REQUEST['ping_status'] ) && ! empty( $_REQUEST['ping_status'] ) ) {
$vars->ping_status = $_REQUEST['ping_status'];
if ( isset( $_REQUEST['_status'] ) && '-1' !== $_REQUEST['_status'] ) {
$vars->post_status = $_REQUEST['_status'];
if ( isset( $_REQUEST['sticky'] ) && '-1' !== $_REQUEST['sticky'] ) {
$vars->post_sticky = $_REQUEST['sticky'];
if ( isset( $_REQUEST['post_format'] ) && '-1' !== $_REQUEST['post_format'] ) {
$vars->post_format = $_REQUEST['post_format'];
// Return captured variables
return $vars;
}
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Validate action
*
* @param string $action Action parsed from request vars
* @return bool
*/
public static function bulk_action_allowed( $action ) {
$allowed_actions = array(
'delete',
'delete_all',
'edit',
'trash',
'untrash',
);
return in_array( $action, $allowed_actions, true );
}
/**
* Build a WP hook specific to a bulk request
*
* @param string $action Bulk action to offload
* @return string
*/
public static function build_hook( $action ) {
return self::ACTION . $action;
}
/**
* Unset flags Core uses to trigger bulk processing
*/
private static function skip_core_processing() {
unset( $_REQUEST['action'] );
unset( $_REQUEST['action2'] );