Completed
Push — 137-feature/refactor-post-modu... ( 580e59 )
by Sudar
07:52
created

BD_Controller::request_handler()   C

Complexity

Conditions 11
Paths 35

Size

Total Lines 65
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 21
nc 35
nop 0
dl 0
loc 65
ccs 0
cts 22
cp 0
crap 132
rs 5.9999
c 2
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_posts_' === substr( $bd_action, 0, strlen( 'delete_posts_' ) )
37
				&& check_admin_referer( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' ) ) {
38
				$nonce_valid = true;
39
			}
40
41
			if ( 'delete_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) )
42
				&& check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) {
43
				$nonce_valid = true;
44
			}
45
46
			if ( 'delete_jetpack_messages' === $bd_action && wp_verify_nonce( $_POST['sm-bulk-delete-misc-nonce'], 'sm-bulk-delete-misc' ) ) {
47
				$nonce_valid = true;
48
			}
49
50
			/**
51
			 * Perform nonce check.
52
			 *
53
			 * @since 5.5
54
			 */
55
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
0 ignored issues
show
Bug introduced by
The call to nonce_check() has too few arguments starting with action. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
			if ( ! /** @scrutinizer ignore-call */ apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
56
				return;
57
			}
58
59
			/**
60
			 * Before performing a bulk action.
61
			 * This hook is for doing actions just before performing any bulk operation.
62
			 *
63
			 * @since 5.4
64
			 */
65
			do_action( 'bd_pre_bulk_action', $bd_action );
66
67
			/**
68
			 * Perform the bulk operation.
69
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
70
			 *
71
			 * @since 5.4
72
			 */
73
			do_action( 'bd_' . $bd_action, $_POST );
74
		}
75
76
		if ( isset( $_GET['bd_action'] ) ) {
77
			$bd_action   = sanitize_text_field( $_GET['bd_action'] );
78
			$nonce_valid = false;
79
80
			/**
81
			 * Perform nonce check.
82
			 *
83
			 * @since 5.5.4
84
			 */
85
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
0 ignored issues
show
Bug introduced by
The call to verify_get_request_nonce() has too few arguments starting with action. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
			if ( ! /** @scrutinizer ignore-call */ apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
86
				return;
87
			}
88
89
			/**
90
			 * Perform the bulk operation.
91
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
92
			 *
93
			 * @since 5.5.4
94
			 */
95
			do_action( 'bd_' . $bd_action, $_GET );
96
		}
97
	}
98
99
	/**
100
	 * Verify if GET request has a valid nonce.
101
	 *
102
	 * @since  5.5.4
103
	 *
104
	 * @param bool   $result Whether nonce is valid.
105
	 * @param string $action Action name
106
	 *
107
	 * @return bool True if nonce is valid, otherwise return $result.
108
	 */
109
	public function verify_get_request_nonce( $result, $action ) {
110
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
111
			return true;
112
		}
113
114
		return $result;
115
	}
116
117
	/**
118
	 * Increase PHP timeout.
119
	 *
120
	 * This is to prevent bulk operations from timing out
121
	 *
122
	 * @since 5.5.4
123
	 */
124
	public function increase_timeout() {
125
		// phpcs:ignore PHPCompatibility.PHP.DeprecatedIniDirectives.safe_modeDeprecatedRemoved
126
		if ( ! ini_get( 'safe_mode' ) ) {
127
			@set_time_limit( 0 );
128
		}
129
	}
130
}
131