Skip to content

Commit

Permalink
Fix updating the shared on network status of snippets not saving corr…
Browse files Browse the repository at this point in the history
…ectly.
  • Loading branch information
sheabunge committed Nov 25, 2024
1 parent a408f6f commit d3f56f7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }))}
Expand Down
8 changes: 7 additions & 1 deletion src/php/class-data-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ public function set_fields( $data ) {
* @return array<string, mixed> 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;
}

/**
Expand Down
26 changes: 18 additions & 8 deletions src/php/rest-api/class-snippets-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
);
}

/**
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/php/snippet-ops.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d3f56f7

Please sign in to comment.