Skip to content

Commit 2966921

Browse files
committed
fix: refactor snippet execution logic for multisite support by centralizing trashed snippet handling
1 parent 750af73 commit 2966921

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/php/class-db.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,7 @@ public function fetch_active_snippets( array $scopes ): array {
327327
$id = intval( $snippet['id'] );
328328
$active_value = intval( $snippet['active'] );
329329

330-
// Skip if snippet is trashed.
331-
if ( -1 === $active_value ) {
332-
continue;
333-
}
334-
335-
// Skip if snippet is not active and not in the list of active shared snippets.
336-
if ( 1 !== $active_value && ! in_array( $id, $active_shared_ids, true ) ) {
330+
if ( ! self::is_network_snippet_enabled( $active_value, $id, $active_shared_ids ) ) {
337331
continue;
338332
}
339333

@@ -353,4 +347,28 @@ public function fetch_active_snippets( array $scopes ): array {
353347

354348
return $active_snippets;
355349
}
350+
351+
/**
352+
* Determine whether a network snippet should execute on the current site.
353+
*
354+
* Network snippets execute when active=1, or when the snippet is listed as active-shared for the site.
355+
* Trashed snippets (active=-1) never execute.
356+
*
357+
* @param int $active_value Raw active value from the snippet record.
358+
* @param int $snippet_id Snippet ID.
359+
* @param int[] $active_shared_ids Active shared network snippet IDs for the current site.
360+
*
361+
* @return bool
362+
*/
363+
public static function is_network_snippet_enabled( int $active_value, int $snippet_id, array $active_shared_ids ): bool {
364+
if ( -1 === $active_value ) {
365+
return false;
366+
}
367+
368+
if ( 1 === $active_value ) {
369+
return true;
370+
}
371+
372+
return in_array( $snippet_id, $active_shared_ids, true );
373+
}
356374
}

src/php/flat-files/classes/class-snippet-files.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,7 @@ public static function get_active_snippets_from_flat_files(
292292
$id = intval( $snippet['id'] );
293293
$active_value = intval( $snippet['active'] );
294294

295-
// Never execute trashed snippets (active = -1).
296-
if ( -1 === $active_value ) {
297-
continue;
298-
}
299-
300-
if ( 1 !== $active_value && ! in_array( $id, $active_shared_ids, true ) ) {
295+
if ( ! DB::is_network_snippet_enabled( $active_value, $id, $active_shared_ids ) ) {
301296
continue;
302297
}
303298

@@ -378,26 +373,16 @@ private static function load_active_snippets_from_file(
378373
}
379374

380375
$file_snippets = require $snippets_file_path;
376+
$shared_ids = is_array( $active_shared_ids )
377+
? array_map( 'intval', $active_shared_ids )
378+
: [];
381379

382380
$filtered_snippets = array_filter(
383381
$file_snippets,
384-
function ( $snippet ) use ( $scopes, $active_shared_ids ) {
382+
function ( $snippet ) use ( $scopes, $shared_ids ) {
385383
$active_value = isset( $snippet['active'] ) ? intval( $snippet['active'] ) : 0;
386384

387-
// Never treat trashed snippets as active.
388-
if ( -1 === $active_value ) {
389-
return false;
390-
}
391-
392-
$is_active = 1 === $active_value;
393-
394-
if ( null !== $active_shared_ids ) {
395-
$is_active = $is_active || in_array(
396-
intval( $snippet['id'] ),
397-
$active_shared_ids,
398-
true
399-
);
400-
}
385+
$is_active = DB::is_network_snippet_enabled( $active_value, intval( $snippet['id'] ), $shared_ids );
401386

402387
return ( $is_active || 'condition' === $snippet['scope'] ) && in_array( $snippet['scope'], $scopes, true );
403388
}

0 commit comments

Comments
 (0)