| 1 |
<?php |
| 2 |
/** |
| 3 |
* Request Handler. |
| 4 |
* |
| 5 |
* @since 5.5.4 |
| 6 |
* |
| 7 |
* @author Sudar |
| 8 |
* |
| 9 |
* @package BulkDelete\Controller |
| 10 |
*/ |
| 11 |
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
| 12 |
|
| 13 |
/** |
| 14 |
* Bulk Delete Controller. |
| 15 |
* |
| 16 |
* @since 5.5.4 |
| 17 |
*/ |
| 18 |
class BD_Controller { //phpcs:ignore |
| 19 |
public function __construct() { |
| 20 |
add_action( 'admin_init', array( $this, 'request_handler' ) ); |
| 21 |
add_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 ); |
| 22 |
add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 ); |
| 23 |
|
| 24 |
add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Handle both POST and GET requests. |
| 29 |
* This method automatically triggers all the actions after checking the nonce. |
| 30 |
*/ |
| 31 |
public function request_handler() { |
| 32 |
if ( isset( $_POST['bd_action'] ) ) { |
| 33 |
$bd_action = sanitize_text_field( wp_unslash($_POST['bd_action']) ); |
| 34 |
$nonce_valid = false; |
| 35 |
|
| 36 |
if ( 'delete_pages_' === substr( $bd_action, 0, strlen( 'delete_pages_' ) ) && check_admin_referer( 'sm-bulk-delete-pages', 'sm-bulk-delete-pages-nonce' ) ) { |
| 37 |
$nonce_valid = true; |
| 38 |
} |
| 39 |
|
| 40 |
if ( 'delete_posts_' === substr( $bd_action, 0, strlen( 'delete_posts_' ) ) && check_admin_referer( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' ) ) { |
| 41 |
$nonce_valid = true; |
| 42 |
} |
| 43 |
|
| 44 |
if ( 'delete_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) ) && check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) { |
| 45 |
$nonce_valid = true; |
| 46 |
} |
| 47 |
|
| 48 |
if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['sm-bulk-delete-misc-nonce'] ?? '')), 'sm-bulk-delete-misc' ) ) { |
| 49 |
$nonce_valid = true; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Perform nonce check. |
| 54 |
* |
| 55 |
* @since 5.5 |
| 56 |
*/ |
| 57 |
if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) { //phpcs:ignore |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Before performing a bulk action. |
| 63 |
* This hook is for doing actions just before performing any bulk operation. |
| 64 |
* |
| 65 |
* @since 5.4 |
| 66 |
*/ |
| 67 |
do_action( 'bd_pre_bulk_action', $bd_action ); //phpcs:ignore |
| 68 |
|
| 69 |
/** |
| 70 |
* Perform the bulk operation. |
| 71 |
* This hook is for doing the bulk operation. Nonce check has already happened by this point. |
| 72 |
* |
| 73 |
* @since 5.4 |
| 74 |
*/ |
| 75 |
do_action( 'bd_' . $bd_action, $_POST ); //phpcs:ignore |
| 76 |
} |
| 77 |
|
| 78 |
if ( isset( $_GET['bd_action'] ) ) { |
| 79 |
$bd_action = sanitize_text_field( wp_unslash($_GET['bd_action']) ); |
| 80 |
$nonce_valid = false; |
| 81 |
|
| 82 |
/** |
| 83 |
* Perform nonce check. |
| 84 |
* |
| 85 |
* @since 5.5.4 |
| 86 |
*/ |
| 87 |
if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) { //phpcs:ignore |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Perform the bulk operation. |
| 93 |
* This hook is for doing the bulk operation. Nonce check has already happened by this point. |
| 94 |
* |
| 95 |
* @since 5.5.4 |
| 96 |
*/ |
| 97 |
do_action( 'bd_' . $bd_action, $_GET ); //phpcs:ignore |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Verify if GET request has a valid nonce. |
| 103 |
* |
| 104 |
* @since 5.5.4 |
| 105 |
* |
| 106 |
* @param bool $result Whether nonce is valid. |
| 107 |
* @param string $action Action name |
| 108 |
* |
| 109 |
* @return bool True if nonce is valid, otherwise return $result. |
| 110 |
*/ |
| 111 |
public function verify_get_request_nonce( $result, $action ) { |
| 112 |
if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return $result; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Increase PHP timeout. |
| 121 |
* |
| 122 |
* This is to prevent bulk operations from timing out |
| 123 |
* |
| 124 |
* @since 5.5.4 |
| 125 |
*/ |
| 126 |
public function increase_timeout() { |
| 127 |
if ( ! ini_get( 'safe_mode' ) ) { |
| 128 |
set_time_limit( 0 ); //phpcs:ignore |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|