Failed Conditions
Branch refactor/kernels (2fb3f9)
by Atanas
01:46
created

AjaxCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\Routing\Conditions;
11
12
use WPEmerge\Requests\RequestInterface;
13
14
/**
15
 * Check against the current ajax action.
16
 */
17
class AjaxCondition implements ConditionInterface {
18
	/**
19
	 * Ajax action to check against.
20
	 *
21
	 * @var string
22
	 */
23
	protected $action = '';
24
25
	/**
26
	 * Flag whether to check against ajax actions which run for authenticated users.
27
	 *
28
	 * @var boolean
29
	 */
30
	protected $private = true;
31
32
	/**
33
	 * Flag whether to check against ajax actions which run for unauthenticated users.
34
	 *
35
	 * @var boolean
36
	 */
37
	protected $public = false;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @codeCoverageIgnore
43
	 * @param string  $action
44
	 * @param boolean $private
45
	 * @param boolean $public
46
	 */
47
	public function __construct( $action, $private = true, $public = false ) {
48
		$this->action = $action;
49
		$this->private = $private;
50
		$this->public = $public;
51
	}
52
53
	/**
54
	 * {@inheritDoc}
55
	 */
56
	public function isSatisfied( RequestInterface $request ) {
57
		if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
58
			return false;
59
		}
60
61
		$authenticated = is_user_logged_in();
62
		$private_match = $this->private && $authenticated;
63
		$public_match = $this->public && !$authenticated;
64
		$action_match = $this->action === $request->post( 'action', $request->get( 'action' ) );
65
66
		return ($private_match || $public_match) && $action_match;
67
	}
68
69
	/**
70
	 * {@inheritDoc}
71
	 */
72
	public function getArguments( RequestInterface $request ) {
73
		return ['action' => $this->action];
74
	}
75
}
76