PluginProbe
Advanced Access Manager – Access Governance for WordPress / 6.9.26
Advanced Access Manager – Access Governance for WordPress v6.9.26
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 / API.php
API.php
404 lines 10.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
12 *
13 * @since 6.9.18 https://github.com/aamplugin/advanced-access-manager/issues/328
14 * @since 6.9.17 https://github.com/aamplugin/advanced-access-manager/issues/319
15 * @since 6.9.5 https://github.com/aamplugin/advanced-access-manager/issues/243
16 * @since 6.7.0 https://github.com/aamplugin/advanced-access-manager/issues/151
17 * @since 6.6.4 https://github.com/aamplugin/advanced-access-manager/issues/142
18 * @since 6.3.1 Fixed bug with setting clearing
19 * @since 6.3.0 Optimized for Multisite setup
20 * @since 6.2.2 Minor refactoring to the clearSettings method
21 * @since 6.0.5 Fixed bug with getOption method where incorrect type could be
22 * returned
23 * @since 6.0.0 Initial implementation of the class
24 *
25 * @package AAM
26 * @version 6.9.18
27 */
28 final class AAM_Core_API
29 {
30
31 /**
32 * Get option from the database
33 *
34 * @param string $option
35 * @param mixed $default
36 * @param int $blog_id
37 *
38 * @return mixed
39 *
40 * @since 6.3.0 Optimized for Multisite setup
41 * @since 6.0.5 Fixed the bug where option may not be returned as proper type
42 * @since 6.0.0 Initial implementation of the method
43 *
44 * @access public
45 * @version 6.3.0
46 */
47 public static function getOption($option, $default = array(), $blog_id = null)
48 {
49 if (is_multisite()) {
50 $result = get_blog_option(
51 ($blog_id ? $blog_id : get_current_blog_id()), $option, $default
52 );
53 } else {
54 $result = get_option($option, $default);
55 }
56
57 return $result;
58 }
59
60 /**
61 * Update option in the DB
62 *
63 * @param string $option
64 * @param mixed $data
65 * @param boolean $autoload
66 * @param int $blog_id
67 *
68 * @return bool
69 *
70 * @since 6.9.18 https://github.com/aamplugin/advanced-access-manager/issues/328
71 * @since 6.7.0 https://github.com/aamplugin/advanced-access-manager/issues/151
72 * @since 6.3.0 Optimized for Multisite setup
73 * @since 6.0.0 Initial implementation of the method
74 *
75 * @access public
76 * @version 6.9.18
77 */
78 public static function updateOption(
79 $option, $data, $autoload = true, $blog_id = null
80 ) {
81 $old_value = self::getOption($option, null, $blog_id);
82
83 if (maybe_serialize($old_value) !== maybe_serialize($data)) {
84 if (is_multisite()) {
85 $result = update_blog_option(
86 ($blog_id ? $blog_id : get_current_blog_id()), $option, $data
87 );
88 } else {
89 $result = update_option($option, $data, $autoload);
90 }
91 } else {
92 $result = true;
93 }
94
95 return $result;
96 }
97
98 /**
99 * Delete option from the DB
100 *
101 * @param string $option
102 * @param int $blog_id
103 *
104 * @return bool
105 *
106 * @since 6.3.0 Optimized for Multisite setup
107 * @since 6.0.0 Initial implementation of the method
108 *
109 * @access public
110 * @version 6.3.0
111 */
112 public static function deleteOption($option, $blog_id = null)
113 {
114 if (is_multisite()) {
115 $result = delete_blog_option(
116 ($blog_id ? $blog_id : get_current_blog_id()), $option
117 );
118 } else {
119 $result = delete_option($option);
120 }
121
122 return $result;
123 }
124
125 /**
126 * Get role list
127 *
128 * @global WP_Roles $wp_roles
129 *
130 * @return WP_Roles
131 *
132 * @access public
133 * @version 6.0.0
134 */
135 public static function getRoles()
136 {
137 global $wp_roles;
138
139 if (function_exists('wp_roles')) {
140 $roles = wp_roles();
141 } elseif (isset($wp_roles)) {
142 $roles = $wp_roles;
143 } else {
144 $roles = new WP_Roles();
145 }
146
147 return $roles;
148 }
149
150 /**
151 * Get main site id
152 *
153 * Compatibility method for sites that are under WP 4.9.0
154 *
155 * @return int
156 *
157 * @access public
158 * @version 6.4.2
159 */
160 public static function getMainSiteId()
161 {
162 if (function_exists('get_main_site_id')) {
163 $id = get_main_site_id();
164 } elseif (is_multisite()) {
165 $network = get_network();
166 $id = ($network ? $network->site_id : 0);
167 } else {
168 $id = get_current_blog_id();
169 }
170
171 return $id;
172 }
173
174 /**
175 * Return max user level
176 *
177 * @param array $caps
178 * @param int $default
179 *
180 * @return int
181 *
182 * @since 6.6.4 https://github.com/aamplugin/advanced-access-manager/issues/142
183 * @since 6.0.0 Initial implementation of the method
184 *
185 * @access public
186 * @version 6.6.4
187 */
188 public static function maxLevel($caps, $default = 0)
189 {
190 $max = $default;
191
192 if (is_array($caps)) { // WP Error Fix bug report
193 foreach ($caps as $cap => $granted) {
194 if (!empty($granted) && (strpos($cap, 'level_') === 0)) {
195 $level = intval(substr($cap, 6));
196 $max = ($max < $level ? $level : $max);
197 }
198 }
199 }
200
201 return intval($max);
202 }
203
204 /**
205 * Get list of all capabilities
206 *
207 * Prepare and return list of all registered in the system capabilities
208 *
209 * @return array
210 *
211 * @access public
212 * @version 6.0.0
213 */
214 public static function getAllCapabilities()
215 {
216 static $caps = array();
217
218 if (empty($caps)) {
219 foreach (self::getRoles()->role_objects as $role) {
220 if (is_array($role->capabilities)) {
221 $caps = array_merge($caps, $role->capabilities);
222 }
223 }
224 }
225
226 return $caps;
227 }
228
229 /**
230 * Check if capability exists
231 *
232 * @param string $cap
233 *
234 * @return boolean
235 *
236 * @access public
237 * @version 6.0.0
238 */
239 public static function capExists($cap)
240 {
241 // Get list of all capabilities registered on the role levels
242 $caps = self::getAllCapabilities();
243
244 // Get list of all capabilities that are assigned on the user level if user
245 // is authenticated
246 if (is_user_logged_in()) {
247 $user = wp_get_current_user();
248 $caps = array_merge($user->caps, $user->allcaps, $caps);
249 }
250
251 return (is_string($cap) && array_key_exists($cap, $caps));
252 }
253
254 /**
255 * Check if AAM capability is allowed
256 *
257 * @param string $cap
258 *
259 * @return boolean
260 *
261 * @access public
262 * @version 6.0.0
263 */
264 public static function isAAMCapabilityAllowed($cap)
265 {
266 return !self::capExists($cap) || current_user_can($cap);
267 }
268
269 /**
270 * Clear all AAM settings
271 *
272 * @return void
273 *
274 * @since 6.9.17 https://github.com/aamplugin/advanced-access-manager/issues/319
275 * @since 6.3.1 https://github.com/aamplugin/advanced-access-manager/issues/48
276 * @since 6.2.2 Refactored the way we iterate over the deleting list of options
277 * @since 6.0.0 Initial implementation of the method
278 *
279 * @access public
280 * @version 6.9.17
281 */
282 public static function clearSettings()
283 {
284 global $wpdb;
285
286 $options = array(
287 AAM_Core_AccessSettings::DB_OPTION,
288 AAM_Core_Config::DB_OPTION,
289 AAM_Core_Cache::DB_OPTION,
290 AAM_Core_ConfigPress::DB_OPTION,
291 AAM_Service_AdminMenu::CACHE_DB_OPTION,
292 AAM_Service_Toolbar::CACHE_DB_OPTION
293 );
294
295 foreach($options as $option) {
296 self::deleteOption($option);
297 }
298
299 // Delete all legacy options
300 $query = "DELETE FROM {$wpdb->options} WHERE (`option_name` LIKE %s) AND ";
301 $query .= "(`option_name` NOT IN ('aam_addons', 'aam_migrations'))";
302 $wpdb->query($wpdb->prepare($query, 'aam%'));
303
304 $wpdb->query($wpdb->prepare(
305 "DELETE FROM {$wpdb->postmeta} WHERE `meta_key` LIKE %s", 'aam-%'
306 ));
307
308 $wpdb->query($wpdb->prepare(
309 "DELETE FROM {$wpdb->usermeta} WHERE `meta_key` LIKE %s", 'aam%'
310 ));
311
312 // Trigger the action to inform other services to clean-up the options
313 do_action('aam_clear_settings_action', $options);
314 }
315
316 /**
317 * Get current post
318 *
319 * @return AAM_Core_Object_Post|null
320 *
321 * @access public
322 * @global WP_Query $wp_query
323 * @global WP_Post $post
324 * @version 6.0.0
325 */
326 public static function getCurrentPost()
327 {
328 global $wp_query, $post;
329
330 $res = $post;
331
332 if (get_the_ID()) {
333 $res = get_post(get_the_ID());
334 } elseif (!empty($wp_query->queried_object)) {
335 $res = $wp_query->queried_object;
336 } elseif (!empty($wp_query->post)) {
337 $res = $wp_query->post;
338 } elseif (!empty($wp_query->query_vars['p'])) {
339 $res = get_post($wp_query->query_vars['p']);
340 } elseif (!empty($wp_query->query_vars['page_id'])) {
341 $res = get_post($wp_query->query_vars['page_id']);
342 } elseif (!empty($wp_query->query['name'])) {
343 //Important! Cover the scenario of NOT LIST but ALLOW READ
344 if (!empty($wp_query->posts)) {
345 foreach ($wp_query->posts as $p) {
346 if ($p->post_name === $wp_query->query['name']) {
347 $res = $p;
348 break;
349 }
350 }
351 } elseif (!empty($wp_query->query['post_type'])) {
352 $res = get_page_by_path(
353 $wp_query->query['name'],
354 OBJECT,
355 $wp_query->query['post_type']
356 );
357 }
358 }
359
360 if (is_a($res, 'WP_Post')) {
361 $result = AAM::getUser()->getObject(
362 AAM_Core_Object_Post::OBJECT_TYPE, $res->ID
363 );
364 } else {
365 $result = null;
366 }
367
368 return $result;
369 }
370
371 /**
372 * Get WP core password hasher
373 *
374 * @return PasswordHash
375 *
376 * @access public
377 * @version 6.0.0
378 */
379 public static function prepareHasher()
380 {
381 require_once ABSPATH . WPINC . '/class-phpass.php';
382
383 return new PasswordHash(8, true);
384 }
385
386 /**
387 * Get AAM API endpoint
388 *
389 * @return string
390 *
391 * @since 6.9.5 https://github.com/aamplugin/advanced-access-manager/issues/243
392 * @since 6.0.0 Initial implementation of the method
393 *
394 * @access public
395 * @version 6.9.5
396 */
397 public static function getAPIEndpoint()
398 {
399 $endpoint = getenv('AAM_ENDPOINT');
400
401 return ($endpoint ? $endpoint : 'https://api.aamportal.com');
402 }
403
404 }