Skip to content

Commit 70d44b4

Browse files
committed
Don't attempt to combine queries for fetching local and multisite snippets
1 parent a8e6989 commit 70d44b4

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

php/snippet-ops.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,11 @@ function execute_active_snippets() {
448448
global $wpdb;
449449

450450
$current_scope = is_admin() ? 1 : 2;
451-
452-
$collate = "COLLATE {$wpdb->collate}";
451+
$queries = array();
453452

454453
/* Fetch snippets from site table */
455454
if ( $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->snippets'" ) === $wpdb->snippets ) {
456-
$sql = $wpdb->prepare( "SELECT id, code {$collate} FROM {$wpdb->snippets} WHERE (scope=0 OR scope=%d) AND active=1", $current_scope );
455+
$queries[] = $wpdb->prepare( "SELECT id, code FROM {$wpdb->snippets} WHERE (scope=0 OR scope=%d) AND active=1", $current_scope );
457456
}
458457

459458
/* Fetch snippets from the network table */
@@ -467,34 +466,27 @@ function execute_active_snippets() {
467466
$active_shared_ids_format = implode( ',', array_fill( 0, count( $active_shared_ids ), '%d' ) );
468467

469468
/* Include them in the query */
470-
$mu_sql = "SELECT id, code {$collate} FROM {$wpdb->snippets} WHERE (scope=0 OR scope=%d) AND (active=1 OR id IN ($active_shared_ids_format))";
469+
$sql = "SELECT id, code FROM {$wpdb->snippets} WHERE (scope=0 OR scope=%d) AND (active=1 OR id IN ($active_shared_ids_format))";
471470

472471
/* Add the scope number to the IDs array, so that it is the first variable in the query */
473472
array_unshift( $active_shared_ids, $current_scope );
474-
$mu_sql = $wpdb->prepare( $mu_sql, $active_shared_ids );
473+
$queries[] = $wpdb->prepare( $sql, $active_shared_ids );
475474

476475
} else {
477-
$mu_sql = $wpdb->prepare( "SELECT id, code {$collate} FROM {$wpdb->ms_snippets} WHERE (scope=0 OR scope=%d) AND active=1", $current_scope );
476+
$queries[] = $wpdb->prepare( "SELECT id, code FROM {$wpdb->ms_snippets} WHERE (scope=0 OR scope=%d) AND active=1", $current_scope );
478477
}
479-
480-
/* Join the two SQL queries together */
481-
$sql = ( empty( $sql ) ? '' : $sql . ' UNION ALL ' ) . $mu_sql;
482-
}
483-
484-
/* Return false if there is no query */
485-
if ( empty( $sql ) ) {
486-
return false;
487478
}
488479

489-
/* Grab the snippets from the database */
490-
$active_snippets = $wpdb->get_results( $sql, ARRAY_A );
480+
foreach ( $queries as $query ) {
481+
$active_snippets = $wpdb->get_results( $query, ARRAY_A );
491482

492-
/* Loop through the returned snippets and execute the PHP code */
493-
foreach ( $active_snippets as $snippet_id => $snippet ) {
494-
$code = $collate ? $snippet[ "code $collate" ] : $snippet['code'];
483+
/* Loop through the returned snippets and execute the PHP code */
484+
foreach ( $active_snippets as $snippet_id => $snippet ) {
485+
$code = $snippet['code'];
495486

496-
if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id ) ) {
497-
execute_snippet( $code, $snippet_id );
487+
if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id ) ) {
488+
execute_snippet( $code, $snippet_id );
489+
}
498490
}
499491
}
500492

0 commit comments

Comments
 (0)