Completed
Push — 137-feature/construct-post-mod... ( 2fadb4...c92423 )
by Maria Daniel Deepak
03:42
created

BD_Controller::request_handler()   C

Complexity

Conditions 13
Paths 67

Size

Total Lines 70
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 24
nc 67
nop 0
dl 0
loc 70
ccs 0
cts 25
cp 0
crap 182
rs 5.6314
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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