PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / htmlburger / wpemerge / src / Routing / Conditions / AjaxCondition.php

AjaxCondition.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/htmlburger/wpemerge/src/Routing/Conditions/AjaxCondition.php

112 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package WPEmerge
4 * @author Atanas Angelov <hi@atanas.dev>
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