route( '/settings', array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( Auth::class, 'can_read' ), ), array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'update_settings' ), 'permission_callback' => array( __CLASS__, 'can_manage' ), ), ) ); $this->route( '/preferences', array( array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'update_preferences' ), 'permission_callback' => array( Auth::class, 'can_read' ), ), ) ); } /** * POST /preferences: per-user choices such as the closed guide. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response */ public function update_preferences( \WP_REST_Request $request ) { $body = $request->get_json_params(); if ( empty( $body ) ) { $body = $request->get_body_params(); } if ( isset( $body['guide_dismissed'] ) ) { Settings::set_guide_dismissed( in_array( $body['guide_dismissed'], array( true, 1, '1', 'true', 'yes' ), true ) ); } Shell::save( $body ); return $this->respond( array( 'guide_dismissed' => Settings::guide_dismissed(), 'fullscreen' => Shell::fullscreen(), 'theme' => Shell::theme(), ) ); } /** * Settings changes need the write capability. * * @return bool|\WP_Error */ public static function can_manage() { return Auth::can_change(); } /** * GET /settings. * * @return \WP_REST_Response */ public function get_settings() { return $this->respond( Settings::all() ); } /** * POST /settings. * * @param \WP_REST_Request $request Request. * @return \WP_REST_Response|\WP_Error */ public function update_settings( \WP_REST_Request $request ) { $body = $request->get_json_params(); if ( empty( $body ) ) { $body = $request->get_body_params(); } $result = Settings::update( $body ); if ( is_wp_error( $result ) ) { return $result; } return $this->respond( Settings::all() ); } }