Completed
Push — dev/5.5.4 ( 9021cc...a5c743 )
by Sudar
02:38
created

BD_Controller::request_handler()   C

Complexity

Conditions 11
Paths 35

Size

Total Lines 68
Code Lines 23

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 68
rs 5.8371
cc 11
eloc 23
nc 35
nop 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
 * @author     Sudar
7
 * @package    BulkDelete\Controller
8
 */
9
10
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
11
12
/**
13
 * Bulk Delete Controller.
14
 *
15
 * @since 5.5.4
16
 */
17
class BD_Controller {
18
	public function __construct() {
19
		add_action( 'admin_init', array( $this, 'request_handler' ) );
20
		add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_request_nonce' ), 10, 2 );
21
		add_filter( 'bd_action_nonce_check', array( $this, 'verify_request_nonce' ), 10, 2 );
22
	}
23
24
	/**
25
	 * Handle both POST and GET requests.
26
	 * This method automatically triggers all the actions after checking the nonce.
27
	 */
28
	public function request_handler() {
29
		if ( isset( $_POST['bd_action'] ) ) {
30
			$bd_action = sanitize_text_field( $_POST['bd_action'] );
31
			$nonce_valid = false;
32
33
			if ( 'delete_pages_' === substr( $bd_action, 0, strlen( 'delete_pages_' ) )
34
				&& check_admin_referer( 'sm-bulk-delete-pages', 'sm-bulk-delete-pages-nonce' ) ) {
35
				$nonce_valid = true;
36
			}
37
38
			if ( 'delete_posts_' === substr( $bd_action, 0, strlen( 'delete_posts_' ) )
39
				&& check_admin_referer( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' ) ) {
40
				$nonce_valid = true;
41
			}
42
43
			if ( 'delete_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) )
44
				&& check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) {
45
				$nonce_valid = true;
46
			}
47
48
			/**
49
			 * Perform nonce check.
50
			 *
51
			 * @since 5.5
52
			 */
53
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
54
				return;
55
			}
56
57
			/**
58
			 * Before performing a bulk action.
59
			 * This hook is for doing actions just before performing any bulk operation
60
			 *
61
			 * @since 5.4
62
			 */
63
			do_action( 'bd_pre_bulk_action', $bd_action );
64
65
			/**
66
			 * Perform the bulk operation.
67
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
68
			 *
69
			 * @since 5.4
70
			 */
71
			do_action( 'bd_' . $bd_action, $_POST );
72
		}
73
74
		if ( isset( $_GET['bd_action'] ) ) {
75
			$bd_action = sanitize_text_field( $_GET['bd_action'] );
76
			$nonce_valid = false;
77
78
			/**
79
			 * Perform nonce check.
80
			 *
81
			 * @since 5.5.4
82
			 */
83
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
84
				return;
85
			}
86
87
			/**
88
			 * Perform the bulk operation.
89
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
90
			 *
91
			 * @since 5.5.4
92
			 */
93
			do_action( 'bd_' . $bd_action, $_GET );
94
		}
95
	}
96
97
	/**
98
	 * Verify if request has a valid nonce.
99
	 *
100
	 * @since  5.5.4
101
	 * @param  bool   $result Whether nonce is valid.
102
	 * @param  string $action Action name
103
	 * @return bool           True if nonce is valid, otherwise return $result.
104
	 */
105
	public function verify_request_nonce( $result, $action ) {
106
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $action instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
107
			return true;
108
		}
109
110
		return $result;
111
	}
112
}
113