|
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
|
|
|
* Check if the private authentication requirement matches. |
|
55
|
|
|
* |
|
56
|
|
|
* @return boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function matchesPrivateRequirement() { |
|
59
|
|
|
return $this->private && is_user_logged_in(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Check if the public authentication requirement matches. |
|
64
|
|
|
* |
|
65
|
|
|
* @return boolean |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function matchesPublicRequirement() { |
|
68
|
|
|
return $this->public && ! is_user_logged_in(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Check if the ajax action matches the requirement. |
|
73
|
|
|
* |
|
74
|
|
|
* @param RequestInterface $request |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function matchesActionRequirement( RequestInterface $request ) { |
|
78
|
|
|
return $this->action === $request->post( 'action', $request->get( 'action' ) ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritDoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function isSatisfied( RequestInterface $request ) { |
|
85
|
|
|
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return ( $this->matchesPrivateRequirement() || $this->matchesPublicRequirement() ) && $this->matchesActionRequirement( $request ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritDoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getArguments( RequestInterface $request ) { |
|
96
|
|
|
return ['action' => $this->action]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|