Failed Conditions
Push — master ( 1d9c07...86291e )
by Atanas
02:02
created

AjaxCondition::toUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2017-2019 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
 * @codeCoverageIgnore
18
 */
19
class AjaxCondition implements ConditionInterface, UrlableInterface {
20
	/**
21
	 * Ajax action to check against.
22
	 *
23
	 * @var string
24
	 */
25
	protected $action = '';
26
27
	/**
28
	 * Flag whether to check against ajax actions which run for authenticated users.
29
	 *
30
	 * @var boolean
31
	 */
32
	protected $private = true;
33
34
	/**
35
	 * Flag whether to check against ajax actions which run for unauthenticated users.
36
	 *
37
	 * @var boolean
38
	 */
39
	protected $public = false;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @codeCoverageIgnore
45
	 * @param string  $action
46
	 * @param boolean $private
47
	 * @param boolean $public
48
	 */
49
	public function __construct( $action, $private = true, $public = false ) {
50
		$this->action = $action;
51
		$this->private = $private;
52
		$this->public = $public;
53
	}
54
55
	/**
56
	 * Check if the private authentication requirement matches.
57
	 *
58
	 * @return boolean
59
	 */
60
	protected function matchesPrivateRequirement() {
61
		return $this->private && is_user_logged_in();
62
	}
63
64
	/**
65
	 * Check if the public authentication requirement matches.
66
	 *
67
	 * @return boolean
68
	 */
69
	protected function matchesPublicRequirement() {
70
		return $this->public && ! is_user_logged_in();
71
	}
72
73
	/**
74
	 * Check if the ajax action matches the requirement.
75
	 *
76
	 * @param  RequestInterface $request
77
	 * @return boolean
78
	 */
79
	protected function matchesActionRequirement( RequestInterface $request ) {
80
		return $this->action === $request->body( 'action', $request->query( 'action' ) );
81
	}
82
83
	/**
84
	 * {@inheritDoc}
85
	 */
86
	public function isSatisfied( RequestInterface $request ) {
87
		if ( ! wp_doing_ajax() ) {
88
			return false;
89
		}
90
91
		if ( ! $this->matchesActionRequirement( $request ) ) {
92
			return false;
93
		}
94
95
		return $this->matchesPrivateRequirement() || $this->matchesPublicRequirement();
96
	}
97
98
	/**
99
	 * {@inheritDoc}
100
	 */
101
	public function getArguments( RequestInterface $request ) {
102
		return ['action' => $this->action];
103
	}
104
105
	/**
106
	 * {@inheritDoc}
107
	 */
108
	public function toUrl( $arguments = [] ) {
109
		return add_query_arg( 'action', $this->action, self_admin_url( 'admin-ajax.php' ) );
110
	}
111
}
112