Completed
Push — 217-feature/delete-posts-by-po... ( 2b6cb8...003725 )
by Sudar
06:50
created

Controller   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 18.18%

Importance

Changes 0
Metric Value
dl 0
loc 109
ccs 6
cts 33
cp 0.1818
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A verify_get_request_nonce() 0 6 2
A increase_timeout() 0 5 2
C request_handler() 0 60 9
1
<?php
2
3
namespace BulkWP\BulkDelete\Core;
4
5 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Bulk Delete Controller.
9
 *
10
 * Handle all requests and automatically perform nonce checks.
11
 *
12
 * @since 5.5.4
13
 * @since 6.0.0 Added namespace.
14
 */
15
class Controller {
16
	/**
17
	 * Controller constructor.
18
	 */
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_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) )
37
				&& check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) {
38
				$nonce_valid = true;
39
			}
40
41
			if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) {
42
				$nonce_valid = true;
43
			}
44
45
			/**
46
			 * Perform nonce check.
47
			 *
48
			 * @since 5.5
49
			 */
50
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
51
				return;
52
			}
53
54
			/**
55
			 * Before performing a bulk action.
56
			 * This hook is for doing actions just before performing any bulk operation.
57
			 *
58
			 * @since 5.4
59
			 */
60
			do_action( 'bd_pre_bulk_action', $bd_action );
61
62
			/**
63
			 * Perform the bulk operation.
64
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
65
			 *
66
			 * @since 5.4
67
			 */
68
			do_action( 'bd_' . $bd_action, $_POST );
69
		}
70
71
		if ( isset( $_GET['bd_action'] ) ) {
72
			$bd_action   = sanitize_text_field( $_GET['bd_action'] );
73
			$nonce_valid = false;
74
75
			/**
76
			 * Perform nonce check.
77
			 *
78
			 * @since 5.5.4
79
			 */
80
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
81
				return;
82
			}
83
84
			/**
85
			 * Perform the bulk operation.
86
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
87
			 *
88
			 * @since 5.5.4
89
			 */
90
			do_action( 'bd_' . $bd_action, $_GET );
91
		}
92
	}
93
94
	/**
95
	 * Verify if GET request has a valid nonce.
96
	 *
97
	 * @since  5.5.4
98
	 *
99
	 * @param bool   $result Whether nonce is valid.
100
	 * @param string $action Action name.
101
	 *
102
	 * @return bool True if nonce is valid, otherwise return $result.
103
	 */
104
	public function verify_get_request_nonce( $result, $action ) {
105
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
106
			return true;
107
		}
108
109
		return $result;
110
	}
111
112
	/**
113
	 * Increase PHP timeout.
114
	 *
115
	 * This is to prevent bulk operations from timing out
116
	 *
117
	 * @since 5.5.4
118
	 */
119
	public function increase_timeout() {
120
		// phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved
121
		if ( ! ini_get( 'safe_mode' ) ) {
122
			// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
123
			@set_time_limit( 0 );
124
		}
125
	}
126
}
127