Completed
Push — dev/5.7.0 ( 2f6e27...2c65c4 )
by Sudar
04:55 queued 01:24
created

BD_Controller   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
dl 0
loc 115
ccs 6
cts 39
cp 0.1538
rs 10
c 0
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A verify_get_request_nonce() 0 6 2
A __construct() 0 6 1
C request_handler() 0 70 13
A increase_timeout() 0 4 2
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 {
19 1
	public function __construct() {
20 1
		add_action( 'admin_init', array( $this, 'request_handler' ) );
21 1
		add_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 );
22 1
		add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 );
23
24 1
		add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 );
25 1
	}
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( $_POST['bd_action'] );
34
			$nonce_valid = false;
35
36
			if ( 'delete_pages_' === substr( $bd_action, 0, strlen( 'delete_pages_' ) )
37
				&& check_admin_referer( 'sm-bulk-delete-pages', 'sm-bulk-delete-pages-nonce' ) ) {
38
				$nonce_valid = true;
39
			}
40
41
			if ( 'delete_posts_' === substr( $bd_action, 0, strlen( 'delete_posts_' ) )
42
				&& check_admin_referer( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' ) ) {
43
				$nonce_valid = true;
44
			}
45
46
			if ( 'delete_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) )
47
				&& check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) {
48
				$nonce_valid = true;
49
			}
50
51
			if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) {
52
				$nonce_valid = true;
53
			}
54
55
			/**
56
			 * Perform nonce check.
57
			 *
58
			 * @since 5.5
59
			 */
60
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
61
				return;
62
			}
63
64
			/**
65
			 * Before performing a bulk action.
66
			 * This hook is for doing actions just before performing any bulk operation.
67
			 *
68
			 * @since 5.4
69
			 */
70
			do_action( 'bd_pre_bulk_action', $bd_action );
71
72
			/**
73
			 * Perform the bulk operation.
74
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
75
			 *
76
			 * @since 5.4
77
			 */
78
			do_action( 'bd_' . $bd_action, $_POST );
79
		}
80
81
		if ( isset( $_GET['bd_action'] ) ) {
82
			$bd_action   = sanitize_text_field( $_GET['bd_action'] );
83
			$nonce_valid = false;
84
85
			/**
86
			 * Perform nonce check.
87
			 *
88
			 * @since 5.5.4
89
			 */
90
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
91
				return;
92
			}
93
94
			/**
95
			 * Perform the bulk operation.
96
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
97
			 *
98
			 * @since 5.5.4
99
			 */
100
			do_action( 'bd_' . $bd_action, $_GET );
101
		}
102
	}
103
104
	/**
105
	 * Verify if GET request has a valid nonce.
106
	 *
107
	 * @since  5.5.4
108
	 *
109
	 * @param bool   $result Whether nonce is valid.
110
	 * @param string $action Action name
111
	 *
112
	 * @return bool True if nonce is valid, otherwise return $result.
113
	 */
114
	public function verify_get_request_nonce( $result, $action ) {
115
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
116
			return true;
117
		}
118
119
		return $result;
120
	}
121
122
	/**
123
	 * Increase PHP timeout.
124
	 *
125
	 * This is to prevent bulk operations from timing out
126
	 *
127
	 * @since 5.5.4
128
	 */
129
	public function increase_timeout() {
130
		// phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved
131
		if ( ! ini_get( 'safe_mode' ) ) {
132
			@set_time_limit( 0 );
133
		}
134
	}
135
}
136