WP_REST_Server::READABLE, 'callback' => array( self::class, 'get_preferences' ), 'permission_callback' => array( self::class, 'can_edit' ), ), array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( self::class, 'update_preferences' ), 'permission_callback' => array( self::class, 'can_edit' ), 'args' => self::args(), ), ) ); } /** * Only users who can use the page builder have clipboard preferences. */ public static function can_edit(): bool { return current_user_can( 'edit_posts' ); } /** * Returns the current user's preferences. */ public static function get_preferences(): WP_REST_Response { return new WP_REST_Response( Preferences::get( get_current_user_id() ) ); } /** * Saves the preferences sent in the request (all optional). * * @param WP_REST_Request $request Request; values already validated against the schema. */ public static function update_preferences( WP_REST_Request $request ): WP_REST_Response { $changes = array(); foreach ( array_keys( Preferences::DEFAULTS ) as $key ) { if ( $request->has_param( $key ) ) { $changes[ $key ] = $request->get_param( $key ); } } return new WP_REST_Response( Preferences::update( get_current_user_id(), $changes ) ); } /** * Route arguments, validated and sanitized by the REST server. * * @return array> */ private static function args(): array { $args = array(); foreach ( Preferences::schema() as $key => $schema ) { $args[ $key ] = array_merge( $schema, array( 'required' => false, 'validate_callback' => 'rest_validate_request_arg', 'sanitize_callback' => 'rest_sanitize_request_arg', ) ); } return $args; } }