Skip to content

Commit 675bd45

Browse files
committed
fix: merge shared network snippets in get_items method
1 parent 95d81b2 commit 675bd45

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/php/rest-api/class-snippets-rest-controller.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public function register_routes() {
198198
public function get_items( $request ): WP_REST_Response {
199199
$network = $request->get_param( 'network' );
200200
$all_snippets = get_snippets( [], $network );
201+
$all_snippets = $this->get_network_items( $all_snippets, $network );
201202

202203
// Get collection params (page, per_page).
203204
$collection_params = $this->get_collection_params();
@@ -229,6 +230,36 @@ public function get_items( $request ): WP_REST_Response {
229230
return $response;
230231
}
231232

233+
/**
234+
* Retrieve and merge shared network snippets.
235+
*
236+
* @param array<Snippet> $all_snippets List of snippets to merge with.
237+
* @param bool|null $network Whether fetching network snippets.
238+
*
239+
* @return array<Snippet> Modified list of snippets.
240+
*/
241+
private function get_network_items( array $all_snippets, $network ): array {
242+
if ( ! is_multisite() || $network ) {
243+
return $all_snippets;
244+
}
245+
246+
$shared_ids = get_site_option( 'shared_network_snippets' );
247+
248+
if ( ! $shared_ids || ! is_array( $shared_ids ) ) {
249+
return $all_snippets;
250+
}
251+
252+
$active_shared_snippets = get_option( 'active_shared_network_snippets', array() );
253+
$shared_snippets = get_snippets( $shared_ids, true );
254+
255+
foreach ( $shared_snippets as $snippet ) {
256+
$snippet->shared_network = true;
257+
$snippet->active = in_array( $snippet->id, $active_shared_snippets, true );
258+
}
259+
260+
return array_merge( $all_snippets, $shared_snippets );
261+
}
262+
232263
/**
233264
* Retrieves one item from the collection.
234265
*

0 commit comments

Comments
 (0)