@@ -71,7 +71,7 @@ function get_snippets( array $ids = array(), $multisite = null, array $args = ar
7171
7272 /* Build a query containing the specified IDs if there are any */
7373 if ( $ ids_count > 1 ) {
74- $ sql .= sprintf ( ' AND id IN (%s) ' , implode ( ', ' , array_fill ( 0 , $ ids_count , '%d ' ) ) );
74+ $ sql .= sprintf ( ' AND id IN (%s) ' , implode ( ', ' , array_fill ( 0 , $ ids_count , '%d ' ) ) );
7575 $ sql_params = array_merge ( $ sql_params , array_values ( $ ids ) );
7676 }
7777
@@ -260,8 +260,7 @@ function activate_snippets( array $ids, $multisite = null ) {
260260
261261 /* Build a SQL query containing all the provided snippet IDs */
262262 $ ids_format = implode ( ', ' , array_fill ( 0 , count ( $ ids ), '%d ' ) );
263- $ sql = sprintf ( 'SELECT id, code FROM %s WHERE id IN (%s); ' , $ table , $ ids_format );
264- $ rows = $ wpdb ->get_results ( $ wpdb ->prepare ( $ sql , $ ids ) );
263+ $ rows = $ wpdb ->get_results ( $ wpdb ->prepare ( "SELECT id, code FROM $ table WHERE id IN ( $ ids_format) " , $ ids ) );
265264
266265 if ( ! $ rows ) {
267266 return array ();
@@ -286,8 +285,7 @@ function activate_snippets( array $ids, $multisite = null ) {
286285
287286 /* Build a SQL query containing all the valid snippet IDs and activate the valid snippets */
288287 $ ids_format = implode ( ', ' , array_fill ( 0 , count ( $ valid_ids ), '%d ' ) );
289- $ sql = sprintf ( 'UPDATE %s SET active = 1 WHERE id IN (%s); ' , $ table , $ ids_format );
290- $ wpdb ->query ( $ wpdb ->prepare ( $ sql , $ valid_ids ) );
288+ $ wpdb ->query ( $ wpdb ->prepare ( "UPDATE $ table SET active = 1 WHERE id IN ( $ ids_format) " , $ valid_ids ) );
291289
292290 /* Remove snippet from shared network snippet list if it was Network Activated */
293291 if ( $ table === $ db ->ms_table && $ shared_network_snippets = get_site_option ( 'shared_network_snippets ' , false ) ) {
@@ -495,14 +493,17 @@ function execute_active_snippets() {
495493 $ db = code_snippets ()->db ;
496494
497495 $ current_scope = is_admin () ? 'admin ' : 'front-end ' ;
498- $ queries = array ();
499-
500- $ sql_format = "SELECT id, code, scope FROM %s WHERE scope IN ('global', 'single-use', %%s) " ;
501- $ order = 'ORDER BY priority ASC, id ASC ' ;
496+ $ results = array ();
502497
503498 /* Fetch snippets from site table */
504499 if ( $ wpdb ->get_var ( "SHOW TABLES LIKE ' $ db ->table ' " ) === $ db ->table ) {
505- $ queries [ $ db ->table ] = $ wpdb ->prepare ( sprintf ( $ sql_format , $ db ->table ) . 'AND active=1 ' . $ order , $ current_scope );
500+ $ results [ $ db ->table ] = $ wpdb ->get_results ( $ wpdb ->prepare ( "
501+ SELECT id, code, scope
502+ FROM $ db ->table WHERE scope IN ('global', 'single-use', %s)
503+ AND active = 1
504+ ORDER BY priority, id " ,
505+ $ current_scope
506+ ), ARRAY_A );
506507 }
507508
508509 /* Fetch snippets from the network table */
@@ -515,23 +516,31 @@ function execute_active_snippets() {
515516 /* Build a list of "%d, %d, %d ..." for every active network shared snippet we have */
516517 $ active_shared_ids_format = implode ( ', ' , array_fill ( 0 , count ( $ active_shared_ids ), '%d ' ) );
517518
518- /* Include them in the query */
519- $ sql = sprintf ( $ sql_format , $ db ->ms_table ) . " AND (active=1 OR id IN ( $ active_shared_ids_format)) $ order " ;
520-
521519 /* Add the scope number to the IDs array, so that it is the first variable in the query */
522520 array_unshift ( $ active_shared_ids , $ current_scope );
523- $ queries [ $ db ->ms_table ] = $ wpdb ->prepare ( $ sql , $ active_shared_ids );
521+
522+ $ results [ $ db ->ms_table ] = $ wpdb ->get_results ( $ wpdb ->prepare ( "
523+ SELECT id, code, scope
524+ FROM $ db ->ms_table WHERE scope IN ('global', 'single-use', %s)
525+ AND (active = 1 OR id IN ( $ active_shared_ids_format))
526+ ORDER BY priority, id " ,
527+ $ active_shared_ids
528+ ), ARRAY_A );
529+
524530 array_shift ( $ active_shared_ids ); // remove it afterwards as we need this variable later
525531
526532 } else {
527- $ sql = sprintf ( $ sql_format , $ db ->ms_table ) . 'AND active=1 ' . $ order ;
528- $ queries [ $ db ->ms_table ] = $ wpdb ->prepare ( $ sql , $ current_scope );
533+ $ results [ $ db ->ms_table ] = $ wpdb ->get_results ( $ wpdb ->prepare ( "
534+ SELECT id, code, scope
535+ FROM $ db ->ms_table WHERE scope IN ('global', 'single-use', %s)
536+ AND active = 1
537+ ORDER BY priority, id " ,
538+ $ current_scope
539+ ), ARRAY_A );
529540 }
530541 }
531542
532- foreach ( $ queries as $ table_name => $ query ) {
533- $ active_snippets = $ wpdb ->get_results ( $ query , 'ARRAY_A ' );
534-
543+ foreach ( $ results as $ table_name => $ active_snippets ) {
535544 if ( ! is_array ( $ active_snippets ) ) {
536545 continue ;
537546 }
0 commit comments