Completed
Push — dev/5.5.4 ( b8ed00...5d98fd )
by Sudar
02:05
created

BD_Controller::increase_timeout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
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_action( 'bd_pre_bulk_action', array( $this, 'increase_timeout' ), 9 );
21
		add_action( 'bd_before_scheduler', array( $this, 'increase_timeout' ), 9 );
22
23
		add_filter( 'bd_get_action_nonce_check', array( $this, 'verify_get_request_nonce' ), 10, 2 );
24
	}
25
26
	/**
27
	 * Handle both POST and GET requests.
28
	 * This method automatically triggers all the actions after checking the nonce.
29
	 */
30
	public function request_handler() {
31
		if ( isset( $_POST['bd_action'] ) ) {
32
			$bd_action = sanitize_text_field( $_POST['bd_action'] );
33
			$nonce_valid = false;
34
35
			if ( 'delete_pages_' === substr( $bd_action, 0, strlen( 'delete_pages_' ) )
36
				&& check_admin_referer( 'sm-bulk-delete-pages', 'sm-bulk-delete-pages-nonce' ) ) {
37
				$nonce_valid = true;
38
			}
39
40
			if ( 'delete_posts_' === substr( $bd_action, 0, strlen( 'delete_posts_' ) )
41
				&& check_admin_referer( 'sm-bulk-delete-posts', 'sm-bulk-delete-posts-nonce' ) ) {
42
				$nonce_valid = true;
43
			}
44
45
			if ( 'delete_meta_' === substr( $bd_action, 0, strlen( 'delete_meta_' ) )
46
				&& check_admin_referer( 'sm-bulk-delete-meta', 'sm-bulk-delete-meta-nonce' ) ) {
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 ) ) {
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 ) ) {
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
	 * @param  bool   $result Whether nonce is valid.
104
	 * @param  string $action Action name
105
	 * @return bool           True if nonce is valid, otherwise return $result.
106
	 */
107
	public function verify_get_request_nonce( $result, $action ) {
108
		if ( check_admin_referer( "bd-{$action}", "bd-{$action}-nonce" ) ) {
109
			return true;
110
		}
111
112
		return $result;
113
	}
114
115
	/**
116
	 * Increase PHP timeout.
117
	 *
118
	 * This is to prevent bulk operations from timing out
119
	 *
120
	 * @since 5.5.4
121
	 */
122
	public function increase_timeout() {
123
		if ( ! ini_get( 'safe_mode' ) ) {
124
			@set_time_limit( 0 );
125
		}
126
	}
127
}
128