settings = $settings; } /** * Registers the routes. * * @since 0.1.0 * * @return void */ public function register_routes() { register_rest_route( $this->namespace, '/' . $this->rest_base, array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => Permissions::require_capability( Capabilities::ACCESS_APP ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( $this, 'save_settings' ), 'permission_callback' => Permissions::require_capability( Capabilities::MANAGE_SETTINGS ), 'args' => array( 'values' => array( 'description' => __( 'Settings to change, keyed by setting name.', 'hostkhata' ), 'type' => 'object', 'required' => true, ), ), ), ) ); } /** * Returns the settings and their schema. * * @since 0.1.0 * * @return WP_REST_Response */ public function get_settings(): WP_REST_Response { return rest_ensure_response( $this->settings->export_for_app() ); } /** * Saves settings. * * @since 0.1.0 * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function save_settings( WP_REST_Request $request ) { $values = $request->get_param( 'values' ); if ( ! is_array( $values ) ) { return $this->error( 'hostkhata_invalid_settings', __( 'Settings must be an object.', 'hostkhata' ) ); } $result = $this->settings->update( $values ); if ( $result['errors'] ) { return $this->error( 'hostkhata_invalid_settings', __( 'Some settings are not valid.', 'hostkhata' ), 400, array( 'errors' => $result['errors'] ) ); } return rest_ensure_response( $this->settings->export_for_app() ); } }