PluginProbe
Advanced Access Manager – Access Governance for WordPress / 6.9.0
Advanced Access Manager – Access Governance for WordPress v6.9.0
7.1.4 7.1.2 7.1.3 6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 All 210 releases
advanced-access-manager / application / Core / Gateway.php
Gateway.php
311 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ======================================================================
5 * LICENSE: This file is subject to the terms and conditions defined in *
6 * file 'license.txt', which is part of this source code package. *
7 * ======================================================================
8 */
9
10 /**
11 * AAM core API gateway
12 *
13 * @since 6.1.0 Significant improvement of the inheritance mechanism
14 * @since 6.0.0 Initial implementation of the class
15 *
16 * @package AAM
17 * @version 6.1.0
18 */
19 final class AAM_Core_Gateway
20 {
21
22 use AAM_Core_Contract_SingletonTrait;
23
24 /**
25 * Prevent from fatal errors
26 *
27 * @param string $name
28 * @param array $arguments
29 *
30 * @return void
31 *
32 * @access public
33 * @version 6.0.0
34 */
35 public function __call($name, $arguments)
36 {
37 _doing_it_wrong(
38 __CLASS__ . '::' . __METHOD__,
39 "The method {$name} is not defined in the AAM API",
40 AAM_VERSION
41 );
42 }
43
44 /**
45 * Get AAM configuration option
46 *
47 * @param string $option
48 * @param mixed $default
49 *
50 * @return mixed
51 *
52 * @access public
53 * @version 6.0.0
54 */
55 public function getConfig($option, $default = null)
56 {
57 return AAM_Core_Config::get($option, $default);
58 }
59
60 /**
61 * Update AAM configuration option
62 *
63 * @param string $option
64 * @param mixed $value
65 *
66 * @return boolean
67 *
68 * @access public
69 * @version 6.0.0
70 */
71 public function updateConfig($option, $value)
72 {
73 return AAM_Core_Config::set($option, $value);
74 }
75
76 /**
77 * Delete AAM configuration option
78 *
79 * @param string $option
80 *
81 * @return boolean
82 *
83 * @access public
84 * @version 6.0.0
85 */
86 public function deleteConfig($option)
87 {
88 return AAM_Core_Config::delete($option);
89 }
90
91 /**
92 * Get user
93 *
94 * If no $id specified, current user will be returned
95 *
96 * @param int $id
97 *
98 * @return AAM_Core_Subject
99 *
100 * @access public
101 * @version 6.0.0
102 */
103 public function getUser($id = null)
104 {
105 if (!empty($id)) {
106 $user = new AAM_Core_Subject_User($id);
107 $user->initialize();
108 } else {
109 $user = AAM::getUser();
110 }
111
112 return $user;
113 }
114
115 /**
116 * Get role subject
117 *
118 * @param string $id
119 *
120 * @return AAM_Core_Subject_Role
121 *
122 * @access public
123 * @version 6.0.0
124 */
125 public function getRole($id)
126 {
127 return new AAM_Core_Subject_Role($id);
128 }
129
130 /**
131 * Get visitor subject
132 *
133 * @return AAM_Core_Subject_Visitor
134 *
135 * @access public
136 * @version 6.0.0
137 */
138 public function getVisitor()
139 {
140 if (is_user_logged_in()) {
141 $visitor = new AAM_Core_Subject_Visitor();
142 } else {
143 $visitor = AAM::getUser();
144 }
145
146 return $visitor;
147 }
148
149 /**
150 * Get default subject
151 *
152 * @return AAM_Core_Subject_Default
153 *
154 * @access public
155 * @version 6.0.0
156 */
157 public function getDefault()
158 {
159 return AAM_Core_Subject_Default::getInstance();
160 }
161
162 /**
163 * Log any critical message
164 *
165 * @param string $message
166 * @param string $markers...
167 *
168 * @access public
169 * @version 6.0.0
170 */
171 public function log()
172 {
173 call_user_func_array('AAM_Core_Console::add', func_get_args());
174 }
175
176 /**
177 * Prepare Access Policy manager but only if service is enabled
178 *
179 * @param AAM_Core_Subject $subject
180 * @param boolean $skipInheritance
181 *
182 * @return AAM_Core_Policy_Manager|null
183 *
184 * @since 6.1.0 Added $skipInheritance flag to insure proper settings inheritance
185 * @since 6.0.0 Initial implementation of the method
186 *
187 * @access public
188 * @version 6.1.0
189 */
190 public function getAccessPolicyManager(
191 AAM_Core_Subject $subject = null, $skipInheritance = false
192 ) {
193 if (is_null($subject)) {
194 $subject = AAM::getUser();
195 }
196
197 if (AAM_Core_Config::get(AAM_Service_AccessPolicy::FEATURE_FLAG, true)) {
198 $manager = AAM_Core_Policy_Factory::get($subject, $skipInheritance);
199 } else {
200 $manager = null;
201 }
202
203 return $manager;
204 }
205
206 /**
207 * Merge two set of access settings into one
208 *
209 * The merging method also takes in consideration the access settings preference
210 * defined in ConfigPress
211 *
212 * @param array $set1
213 * @param array $set2
214 * @param string $objectType
215 * @param string $preference
216 *
217 * @return array
218 *
219 * @access public
220 * @version 6.0.0
221 */
222 public function mergeSettings($set1, $set2, $objectType, $preference = null)
223 {
224 $merged = array();
225
226 // If preference is not explicitly defined, fetch it from the AAM configs
227 if (is_null($preference)) {
228 $preference = $this->getConfig(
229 "core.settings.{$objectType}.merge.preference",
230 'deny'
231 );
232 }
233
234 // first get the complete list of unique keys
235 $keys = array_keys($set1);
236 foreach (array_keys($set2) as $key) {
237 if (!in_array($key, $keys, true)) {
238 $keys[] = $key;
239 }
240 }
241
242 foreach ($keys as $key) {
243 // There can be only two types of preferences: "deny" or "allow". Based
244 // on that, choose access settings that have proper effect as following:
245 //
246 // - If set1 and set2 have two different preferences, get the one that
247 // has correct preference;
248 // - If set1 and set2 have two the same preferences, choose the set2
249 // - If only set1 has access settings, use set1 as-is
250 // - If only set2 has access settings, use set2 as-is
251 // - If set1 and set2 have different effect than preference, choose
252 // set2
253 $effect1 = $this->computeAccessOptionEffect($set1, $key);
254 $effect2 = $this->computeAccessOptionEffect($set2, $key);
255 $effect = ($preference === 'deny');
256
257 // Access Option is either boolean true or array with "enabled" key
258 // set as boolean true
259 if ($effect1 === $effect2) { // both equal
260 $merged[$key] = $set2[$key];
261 } elseif ($effect1 === $effect) { // set1 matches preference
262 $merged[$key] = $set1[$key];
263 } elseif ($effect2 === $effect) { // set2 matches preference
264 $merged[$key] = $set2[$key];
265 } else {
266 if ($preference === 'allow') {
267 $option = isset($set2[$key]) ? $set2[$key] : $set1[$key];
268 if (is_array($option)) {
269 $option['enabled'] = false;
270 } else {
271 $option = false;
272 }
273 $merged[$key] = $option;
274 } elseif (is_null($effect1)) {
275 $merged[$key] = $set2[$key];
276 } elseif (is_null($effect2)) {
277 $merged[$key] = $set1[$key];
278 }
279 }
280 }
281
282 return $merged;
283 }
284
285 /**
286 * Determine correct access option effect
287 *
288 * There can be two possible types of the access settings: straight boolean and
289 * array with "enabled" flag. If provided key is not a part of the access options,
290 * the null is returned, otherwise boolean true of false.
291 *
292 * @param array $opts
293 * @param string $key
294 *
295 * @return null|boolean
296 *
297 * @access protected
298 * @version 6.0.0
299 */
300 protected function computeAccessOptionEffect($opts, $key)
301 {
302 $effect = null; // nothing is defined
303
304 if (isset($opts[$key])) {
305 $effect = is_array($opts[$key]) ? $opts[$key]['enabled'] : $opts[$key];
306 }
307
308 return $effect;
309 }
310
311 }