WP_REST_Server::READABLE, 'callback' => [ $this, 'get_context' ], 'permission_callback' => [ $this, 'check_permission' ], ], [ 'methods' => WP_REST_Server::EDITABLE, 'callback' => [ $this, 'update_context' ], 'permission_callback' => [ $this, 'check_permission' ], 'args' => [ 'enabled' => [ 'type' => 'boolean', 'required' => false, ], 'instructions' => [ 'type' => 'string', 'required' => false, ], 'sections' => [ 'type' => 'object', 'required' => false, 'additionalProperties' => [ 'type' => 'boolean' ], ], ], ], ] ); } /** * GET /context: everything the screen renders. * * @return WP_REST_Response The full screen payload. * @since 1.4.0 */ public function get_context(): WP_REST_Response { return rest_ensure_response( ContextPayload::build() ); } /** * POST /context: save a partial change and return the new state. * * Only the keys present in the request body change; the screen saves one * control at a time, so a request that carried the whole state would let a * stale tab overwrite a change made in another one. * * @param WP_REST_Request> $request The request. * * @return WP_REST_Response The screen payload, recomputed after the save. * @since 1.4.0 */ public function update_context( WP_REST_Request $request ): WP_REST_Response { $input = []; foreach ( [ 'enabled', 'instructions', 'sections' ] as $key ) { $value = $request->get_param( $key ); if ( $value !== null ) { $input[ $key ] = $value; } } ContextSettings::save( $input ); return rest_ensure_response( ContextPayload::build() ); } }