From d3f56f7ad7869111fe7562cb9b4ff36943f142ac Mon Sep 17 00:00:00 2001 From: Shea Bunge Date: Mon, 25 Nov 2024 15:24:54 +1100 Subject: [PATCH] Fix updating the shared on network status of snippets not saving correctly. --- .../fields/MultisiteSharingSettings.tsx | 2 +- src/php/class-data-item.php | 8 +++++- .../class-snippets-rest-controller.php | 26 +++++++++++++------ src/php/snippet-ops.php | 4 +-- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/js/components/SnippetForm/fields/MultisiteSharingSettings.tsx b/src/js/components/SnippetForm/fields/MultisiteSharingSettings.tsx index a716fa9a..4754dc0c 100644 --- a/src/js/components/SnippetForm/fields/MultisiteSharingSettings.tsx +++ b/src/js/components/SnippetForm/fields/MultisiteSharingSettings.tsx @@ -13,7 +13,7 @@ export const MultisiteSharingSettings: React.FC = () => { id="snippet_sharing" name="snippet_sharing" type="checkbox" - checked={!!snippet.shared_network} + checked={Boolean(snippet.shared_network)} disabled={isReadOnly} onChange={event => setSnippet(previous => ({ ...previous, shared_network: event.target.checked }))} diff --git a/src/php/class-data-item.php b/src/php/class-data-item.php index 0eab3d47..00758ee8 100644 --- a/src/php/class-data-item.php +++ b/src/php/class-data-item.php @@ -81,7 +81,13 @@ public function set_fields( $data ) { * @return array Field names keyed to current values. */ public function get_fields(): array { - return $this->fields; + $fields = []; + + foreach ( $this->get_allowed_fields() as $field_name ) { + $fields[ $field_name ] = $this->$field_name; + } + + return $fields; } /** diff --git a/src/php/rest-api/class-snippets-rest-controller.php b/src/php/rest-api/class-snippets-rest-controller.php index 46db1d3a..4ae22c88 100644 --- a/src/php/rest-api/class-snippets-rest-controller.php +++ b/src/php/rest-api/class-snippets-rest-controller.php @@ -269,13 +269,16 @@ public function update_item( $request ) { $item = $this->prepare_item_for_database( $request, $snippet ); $result = save_snippet( $item ); - return $result ? - $this->prepare_item_for_response( $result, $request ) : - new WP_Error( - 'rest_cannot_update', - __( 'The snippet could not be updated.', 'code-snippets' ), - [ 'status' => 500 ] - ); + if ( $result ) { + $request->set_param( 'id', $result->id ); + return $this->get_item( $request ); + } + + return new WP_Error( + 'rest_cannot_update', + __( 'The snippet could not be updated.', 'code-snippets' ), + [ 'status' => 500 ] + ); } /** @@ -408,7 +411,14 @@ protected function prepare_item_for_database( $request, ?Snippet $item = null ): * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function prepare_item_for_response( $item, $request ) { - return rest_ensure_response( $item->get_fields() ); + $schema = $this->get_item_schema(); + $response = []; + + foreach ( array_keys( $schema['properties'] ) as $property ) { + $response[ $property ] = $item->$property; + } + + return rest_ensure_response( $response ); } /** diff --git a/src/php/snippet-ops.php b/src/php/snippet-ops.php index 70bad80a..d874d10a 100644 --- a/src/php/snippet-ops.php +++ b/src/php/snippet-ops.php @@ -228,8 +228,8 @@ function update_shared_network_snippets( array $snippets ): bool { } foreach ( $snippets as $snippet ) { - if ( $snippet->shared_network ) { - if ( $snippet->active ) { + if ( $snippet->network ) { + if ( $snippet->shared_network ) { $shared_ids[] = $snippet->id; } else { $unshared_ids[] = $snippet->id;