Completed
Push — dev/5.5.4 ( 155266...c3760c )
by Sudar
02:05
created

BD_Controller::verify_request_nonce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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_get_request_nonce' ), 10, 2 );
21
	}
22
23
	/**
24
	 * Handle both POST and GET requests.
25
	 * This method automatically triggers all the actions after checking the nonce.
26
	 */
27
	public function request_handler() {
28
		if ( isset( $_POST['bd_action'] ) ) {
29
			$bd_action = sanitize_text_field( $_POST['bd_action'] );
30
			$nonce_valid = false;
31
32
			if ( 'delete_pages_' === substr( $bd_action, 0, strlen( 'delete_pages_' ) )
33
				&& check_admin_referer( 'sm-bulk-delete-pages', 'sm-bulk-delete-pages-nonce' ) ) {
34
				$nonce_valid = true;
35
			}
36
37
			if ( 'delete_posts_' === substr( $bd_action, 0, strlen( 'delete_posts_' ) )
38
				&& check_admin_referer( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' ) ) {
39
				$nonce_valid = true;
40
			}
41
42
			if ( 'delete_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) )
43
				&& check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) {
44
				$nonce_valid = true;
45
			}
46
47
			/**
48
			 * Perform nonce check.
49
			 *
50
			 * @since 5.5
51
			 */
52
			if ( ! apply_filters( 'bd_action_nonce_check', $nonce_valid, $bd_action ) ) {
53
				return;
54
			}
55
56
			/**
57
			 * Before performing a bulk action.
58
			 * This hook is for doing actions just before performing any bulk operation
59
			 *
60
			 * @since 5.4
61
			 */
62
			do_action( 'bd_pre_bulk_action', $bd_action );
63
64
			/**
65
			 * Perform the bulk operation.
66
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
67
			 *
68
			 * @since 5.4
69
			 */
70
			do_action( 'bd_' . $bd_action, $_POST );
71
		}
72
73
		if ( isset( $_GET['bd_action'] ) ) {
74
			$bd_action = sanitize_text_field( $_GET['bd_action'] );
75
			$nonce_valid = false;
76
77
			/**
78
			 * Perform nonce check.
79
			 *
80
			 * @since 5.5.4
81
			 */
82
			if ( ! apply_filters( 'bd_get_action_nonce_check', $nonce_valid, $bd_action ) ) {
83
				return;
84
			}
85
86
			/**
87
			 * Perform the bulk operation.
88
			 * This hook is for doing the bulk operation. Nonce check has already happened by this point.
89
			 *
90
			 * @since 5.5.4
91
			 */
92
			do_action( 'bd_' . $bd_action, $_GET );
93
		}
94
	}
95
96
	/**
97
	 * Verify if GET request has a valid nonce.
98
	 *
99
	 * @since  5.5.4
100
	 * @param  bool   $result Whether nonce is valid.
101
	 * @param  string $action Action name
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