Completed
Push — master ( 60de28...d4f927 )
by Sudar
02:13
created

NonceChecker::check_nonce()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 36
rs 8.5806
1
<?php namespace EmailLog\Core\Request;
2
3
use EmailLog\Core\Loadie;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Check nonce for all Email Log requests.
9
 *
10
 * @since 2.0.0
11
 */
12
class NonceChecker implements Loadie {
13
14
	/**
15
	 * Setup hooks.
16
	 *
17
	 * @inheritdoc
18
	 */
19
	public function load() {
20
		add_action( 'admin_init', array( $this, 'check_nonce' ) );
21
	}
22
23
	/**
24
	 * Check nonce for all Email Log Requests.
25
	 * All Email Log Requests will have the `el_` prefix and
26
	 * nonce would be available at `el_{action_name}_nonce`.
27
	 */
28
	public function check_nonce() {
29
		if ( ! isset( $_POST['el-action'] ) ) {
30
			return;
31
		}
32
33
		$action = sanitize_text_field( $_POST['el-action'] );
34
35
		if ( ! isset( $_POST[ $action . '_nonce' ] ) ) {
36
			return;
37
		}
38
39
		if ( ! wp_verify_nonce( $_POST[ $action . '_nonce' ], $action ) ) {
40
			return;
41
		}
42
43
		/**
44
		 * Perform `el` action.
45
		 * Nonce check has already happened at this point.
46
		 *
47
		 * @since 2.0
48
		 *
49
		 * @param string $action Action name.
50
		 * @param array  $_POST  Request data.
51
		 */
52
		do_action( 'el_action', $action, $_POST );
53
54
		/**
55
		 * Perform `el` action.
56
		 * Nonce check has already happened at this point.
57
		 *
58
		 * @since 2.0
59
		 *
60
		 * @param array $_POST Request data.
61
		 */
62
		do_action( $action, $_POST );
63
	}
64
}
65