AccessDeniedRedirect.php
1 year ago
AdminToolbar.php
1 year ago
ApiRoute.php
1 year ago
BackendMenu.php
1 year ago
BackwardCompatibility.php
1 year ago
Capability.php
1 year ago
Configs.php
5 months ago
Content.php
1 year ago
Identity.php
1 year ago
Jwt.php
5 months ago
LoginRedirect.php
1 year ago
LogoutRedirect.php
1 year ago
Metabox.php
1 year ago
Mu.php
1 year ago
NotFoundRedirect.php
1 year ago
Policies.php
1 year ago
Roles.php
1 year ago
SecureLogin.php
1 year ago
SecurityAudit.php
1 year ago
ServiceTrait.php
1 year ago
Settings.php
1 year ago
Urls.php
1 year ago
Users.php
1 year ago
Widgets.php
1 year ago
Settings.php
184 lines
| 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 | * RESTful API for the Settings service |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Restful_Settings |
| 17 | { |
| 18 | |
| 19 | use AAM_Restful_ServiceTrait; |
| 20 | |
| 21 | /** |
| 22 | * Necessary permissions to access endpoint |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const PERMISSIONS = [ |
| 27 | 'aam_manager', |
| 28 | 'aam_manage_settings' |
| 29 | ]; |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * @return void |
| 35 | * @access protected |
| 36 | * |
| 37 | * @version 7.0.0 |
| 38 | */ |
| 39 | protected function __construct() |
| 40 | { |
| 41 | // Register API endpoint |
| 42 | add_action('rest_api_init', function() { |
| 43 | // Get list of all AAM settings that are explicitly defined |
| 44 | $this->_register_route('/settings', [ |
| 45 | 'methods' => WP_REST_Server::READABLE, |
| 46 | 'callback' => array($this, 'get_settings') |
| 47 | ], self::PERMISSIONS); |
| 48 | |
| 49 | // Set settings in bulk |
| 50 | $this->_register_route('/settings', [ |
| 51 | 'methods' => WP_REST_Server::CREATABLE, |
| 52 | 'callback' => array($this, 'set_settings') |
| 53 | ], self::PERMISSIONS); |
| 54 | |
| 55 | // Reset settings |
| 56 | $this->_register_route('/settings', [ |
| 57 | 'methods' => WP_REST_Server::DELETABLE, |
| 58 | 'callback' => array($this, 'reset_settings') |
| 59 | ], self::PERMISSIONS); |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get all defined settings for everyone or specific access level |
| 65 | * |
| 66 | * @param WP_REST_Request $request |
| 67 | * |
| 68 | * @return WP_REST_Response |
| 69 | * @access public |
| 70 | * |
| 71 | * @version 7.0.0 |
| 72 | */ |
| 73 | public function get_settings(WP_REST_Request $request) |
| 74 | { |
| 75 | try { |
| 76 | if ($this->_has_access_level($request)) { |
| 77 | $result = $this->_get_service($request)->get_settings(); |
| 78 | } else { |
| 79 | $result = AAM::api()->db->read( |
| 80 | AAM_Framework_Service_Settings::DB_OPTION, [] |
| 81 | ); |
| 82 | } |
| 83 | } catch (Exception $e) { |
| 84 | $result = $this->_prepare_error_response($e); |
| 85 | } |
| 86 | |
| 87 | return rest_ensure_response($result); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set settings |
| 92 | * |
| 93 | * @param WP_REST_Request $request |
| 94 | * |
| 95 | * @return WP_REST_Response |
| 96 | * @access public |
| 97 | * |
| 98 | * @version 7.0.0 |
| 99 | */ |
| 100 | public function set_settings(WP_REST_Request $request) |
| 101 | { |
| 102 | try { |
| 103 | if ($this->_has_access_level($request)) { |
| 104 | $result = $this->_get_service($request)->set_settings( |
| 105 | $request->get_json_params() |
| 106 | ); |
| 107 | } else { |
| 108 | $result = AAM::api()->db->write( |
| 109 | AAM_Framework_Service_Settings::DB_OPTION, |
| 110 | $request->get_json_params() |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | $result = [ 'success' => $result ]; |
| 115 | } catch (Exception $e) { |
| 116 | $result = $this->_prepare_error_response($e); |
| 117 | } |
| 118 | |
| 119 | return rest_ensure_response($result); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Reset all settings |
| 124 | * |
| 125 | * @param WP_REST_Request $request |
| 126 | * |
| 127 | * @return WP_REST_Response |
| 128 | * @access public |
| 129 | * |
| 130 | * @version 7.0.0 |
| 131 | */ |
| 132 | public function reset_settings(WP_REST_Request $request) |
| 133 | { |
| 134 | try { |
| 135 | if ($this->_has_access_level($request)) { |
| 136 | $result = $this->_get_service($request)->reset(); |
| 137 | } else { |
| 138 | $result = AAM::api()->db->delete( |
| 139 | AAM_Framework_Service_Settings::DB_OPTION |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | $result = [ 'success' => $result ]; |
| 144 | } catch (Exception $e) { |
| 145 | $result = $this->_prepare_error_response($e); |
| 146 | } |
| 147 | |
| 148 | return rest_ensure_response($result); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get service |
| 153 | * |
| 154 | * @param WP_REST_Request $request |
| 155 | * @access private |
| 156 | * |
| 157 | * @version 7.0.0 |
| 158 | */ |
| 159 | private function _get_service(WP_REST_Request $request) |
| 160 | { |
| 161 | return AAM::api()->settings( |
| 162 | $this->_determine_access_level($request), |
| 163 | [ 'error_handling' => 'exception' ] |
| 164 | ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Determine if access level is provided |
| 169 | * |
| 170 | * @param WP_REST_Request $request |
| 171 | * |
| 172 | * @return bool |
| 173 | * @access private |
| 174 | * |
| 175 | * @version 7.0.0 |
| 176 | */ |
| 177 | private function _has_access_level($request) |
| 178 | { |
| 179 | $access_level = $this->_determine_access_level($request); |
| 180 | |
| 181 | return !is_null($access_level); |
| 182 | } |
| 183 | |
| 184 | } |