se { $fields = array(); } return $fields; } /** * Register new RESTful route * * The method also applies the `aam_rest_route_args_filter` filter that allows * other processes to change the router definition * * @param string $route * @param array $args * * @return void * * @access private */ private function _register_route($route, $args) { register_rest_route( self::NAMESPACE, $route, apply_filters('aam_rest_route_args_filter', $args, $route, self::NAMESPACE) ); } /** * Validate the input field "fields" * * @param string|null $value Input value * * @return bool|WP_Error * * @access private */ private function _validate_fields_input($value) { $response = true; if (is_string($value) && strlen($value) > 0) { $invalid_fields = []; foreach(explode(',', $value) as $field) { if (strlen(sanitize_key($field)) !== strlen($field)) { $invalid_fields[] = $field; } } if (count($invalid_fields) > 0) { $response = new WP_Error( 'rest_invalid_param', sprintf( __('Invalid fields: %s'), implode(', ', $invalid_fields) ), array('status' => 400) ); } } return $response; } /** * Validate role accessibility * * @param string $slug Role unique slug (aka ID) * * @return bool|WP_Error * * @access private */ private function _validate_role_accessibility($slug) { $response = true; try { AAM_Framework_Manager::roles()->get_role_by_slug($slug); } catch (UnderflowException $_) { $response = new WP_Error( 'rest_not_found', sprintf( __("The role '%s' does not exist or is not editable"), $slug ), array('status' => 404) ); } return $response; } /** * Validate role slug and its uniqueness * * @param string $value Role slug (aka ID) * @param WP_REST_Request $value Current request * * @return bool|WP_Error * * @access private */ private function _validate_role_slug_uniqueness($value, WP_REST_Request $request) { $response = true; if (is_string($value)) { $slug = sanitize_key($value); if ($slug === $request->get_param('slug')) { $response = true; // do nothing, we do not update the slug } elseif (strlen($slug) > 0) { if (AAM_Framework_Manager::roles()->is_role($slug)) { $response = new WP_Error( 'rest_invalid_param', sprintf( __("The role with '%s' slug already exists"), $slug ), array('status' => 400) ); } } else { $response = new WP_Error( 'rest_invalid_param', sprintf( __("Invalid role slug '%s'"), $value ), array('status' => 400) ); } } return $response; } /** * Validate the array of keys * * @param array|null $value Input array of values * * @return bool|WP_Error * * @access private */ private function _validate_keys_array_input($value) { $response = true; if (is_array($value) && count($value) > 0) { $invalid_keys = []; foreach($value as $key) { if (strlen(sanitize_key($key)) !== strlen($key)) { $invalid_keys[] = $key; } } if (count($invalid_keys) > 0) { $response = new WP_Error( 'rest_invalid_param', sprintf( __('Invalid keys: %s'), implode(', ', $invalid_keys) ), array('status' => 400) ); } } return $response; } /** * Prepare the failure response * * @param Exception $ex * @param string $code * @param integer $status * * @return WP_REST_Response * * @access private */ private function _prepare_error_response( $ex, $code = 'rest_unexpected_error', $status = 500 ) { $message = $ex->getMessage(); $data = array('status' => $status); if (defined('WP_DEBUG') && WP_DEBUG) { $data['details'] = array( 'trace' => $ex->getTrace() ); } elseif ($status === 500) { // Mask the real error if debug mode is off $message = __('Unexpected application error', AAM_KEY); } return new WP_REST_Response(new WP_Error($code, $message, $data), $status); } /** * Bootstrap the api * * @return boolean * * @access public */ public static function bootstrap() { if (is_null(self::$_instance)) { self::$_instance = new self; } return self::$_instance; } }