@@ -197,104 +197,20 @@ public static function create_table( string $table_name ): bool {
197197 return $ success ;
198198 }
199199
200- /**
201- * Fetch a list of active snippets from a database table.
202- *
203- * @param string $table_name Name of table to fetch snippets from.
204- * @param array<string> $scopes List of scopes to include in query.
205- * @param boolean $active_only Whether to only fetch active snippets from the table.
206- *
207- * @return array<string, array<string, mixed>>|false List of active snippets, if any could be retrieved.
208- *
209- * @phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
210- */
211- private static function fetch_snippets_from_table ( string $ table_name , array $ scopes , bool $ active_only = true ) {
212- global $ wpdb ;
213-
214- $ cache_key = sprintf ( 'active_snippets_%s_%s ' , sanitize_key ( join ( '_ ' , $ scopes ) ), $ table_name );
215- $ cached_snippets = wp_cache_get ( $ cache_key , CACHE_GROUP );
216-
217- if ( is_array ( $ cached_snippets ) ) {
218- return $ cached_snippets ;
219- }
220-
221- if ( ! self ::table_exists ( $ table_name ) ) {
222- return false ;
223- }
224-
225- $ scopes_format = implode ( ', ' , array_fill ( 0 , count ( $ scopes ), '%s ' ) );
226- $ extra_where = $ active_only ? 'AND active=1 ' : '' ;
227-
228- $ snippets = $ wpdb ->get_results (
229- $ wpdb ->prepare (
230- "
231- SELECT id, code, scope, active, priority
232- FROM $ table_name
233- WHERE scope IN ( $ scopes_format) $ extra_where
234- ORDER BY priority, id " ,
235- $ scopes
236- ),
237- 'ARRAY_A '
238- );
239-
240- // Cache the full list of snippets.
241- if ( is_array ( $ snippets ) ) {
242- wp_cache_set ( $ cache_key , $ snippets , CACHE_GROUP );
243- return $ snippets ;
244- }
245-
246- return false ;
247- }
248-
249- /**
250- * Sort the active snippets by priority, table, and ID.
251- *
252- * @param array $active_snippets List of active snippets to sort.
253- */
254- private function sort_active_snippets ( array &$ active_snippets ): void {
255- $ comparisons = [
256- function ( array $ a , array $ b ) {
257- return $ a ['priority ' ] <=> $ b ['priority ' ];
258- },
259- function ( array $ a , array $ b ) {
260- $ a_table = $ a ['table ' ] === $ this ->ms_table ? 0 : 1 ;
261- $ b_table = $ b ['table ' ] === $ this ->ms_table ? 0 : 1 ;
262- return $ a_table <=> $ b_table ;
263- },
264- function ( array $ a , array $ b ) {
265- return $ a ['id ' ] <=> $ b ['id ' ];
266- },
267- ];
268-
269- usort (
270- $ active_snippets ,
271- static function ( $ a , $ b ) use ( $ comparisons ) {
272- foreach ( $ comparisons as $ comparison ) {
273- $ result = $ comparison ( $ a , $ b );
274- if ( 0 !== $ result ) {
275- return $ result ;
276- }
277- }
278-
279- return 0 ;
280- }
281- );
282- }
283-
284- /**
285- * Generate the SQL for fetching active snippets from the database.
286- *
287- * @param string[] $scopes List of scopes to retrieve in.
288- *
289- * @return array{
290- * id: int,
291- * code: string,
292- * scope: string,
293- * table: string,
294- * network: bool,
295- * priority: int,
296- * } List of active snippets.
297- */
200+ /**
201+ * Generate the SQL for fetching active snippets from the database.
202+ *
203+ * @param string[] $scopes List of scopes to retrieve in.
204+ *
205+ * @return array{
206+ * id: int,
207+ * code: string,
208+ * scope: string,
209+ * table: string,
210+ * network: bool,
211+ * priority: int,
212+ * } List of active snippets.
213+ */
298214 public function fetch_active_snippets ( array $ scopes ): array {
299215 $ active_snippets = [];
300216
@@ -371,4 +287,88 @@ public static function is_network_snippet_enabled( int $active_value, int $snipp
371287
372288 return in_array ( $ snippet_id , $ active_shared_ids , true );
373289 }
290+
291+ /**
292+ * Sort the active snippets by priority, table, and ID.
293+ *
294+ * @param array $active_snippets List of active snippets to sort.
295+ */
296+ private function sort_active_snippets ( array &$ active_snippets ): void {
297+ $ comparisons = [
298+ function ( array $ a , array $ b ) {
299+ return $ a ['priority ' ] <=> $ b ['priority ' ];
300+ },
301+ function ( array $ a , array $ b ) {
302+ $ a_table = $ a ['table ' ] === $ this ->ms_table ? 0 : 1 ;
303+ $ b_table = $ b ['table ' ] === $ this ->ms_table ? 0 : 1 ;
304+ return $ a_table <=> $ b_table ;
305+ },
306+ function ( array $ a , array $ b ) {
307+ return $ a ['id ' ] <=> $ b ['id ' ];
308+ },
309+ ];
310+
311+ usort (
312+ $ active_snippets ,
313+ static function ( $ a , $ b ) use ( $ comparisons ) {
314+ foreach ( $ comparisons as $ comparison ) {
315+ $ result = $ comparison ( $ a , $ b );
316+ if ( 0 !== $ result ) {
317+ return $ result ;
318+ }
319+ }
320+
321+ return 0 ;
322+ }
323+ );
324+ }
325+
326+ /**
327+ * Fetch a list of active snippets from a database table.
328+ *
329+ * @param string $table_name Name of table to fetch snippets from.
330+ * @param array<string> $scopes List of scopes to include in query.
331+ * @param boolean $active_only Whether to only fetch active snippets from the table.
332+ *
333+ * @return array<string, array<string, mixed>>|false List of active snippets, if any could be retrieved.
334+ *
335+ * @phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
336+ */
337+ private static function fetch_snippets_from_table ( string $ table_name , array $ scopes , bool $ active_only = true ) {
338+ global $ wpdb ;
339+
340+ $ cache_key = sprintf ( 'active_snippets_%s_%s ' , sanitize_key ( join ( '_ ' , $ scopes ) ), $ table_name );
341+ $ cached_snippets = wp_cache_get ( $ cache_key , CACHE_GROUP );
342+
343+ if ( is_array ( $ cached_snippets ) ) {
344+ return $ cached_snippets ;
345+ }
346+
347+ if ( ! self ::table_exists ( $ table_name ) ) {
348+ return false ;
349+ }
350+
351+ $ scopes_format = implode ( ', ' , array_fill ( 0 , count ( $ scopes ), '%s ' ) );
352+ $ extra_where = $ active_only ? 'AND active=1 ' : '' ;
353+
354+ $ snippets = $ wpdb ->get_results (
355+ $ wpdb ->prepare (
356+ "
357+ SELECT id, code, scope, active, priority
358+ FROM $ table_name
359+ WHERE scope IN ( $ scopes_format) $ extra_where
360+ ORDER BY priority, id " ,
361+ $ scopes
362+ ),
363+ 'ARRAY_A '
364+ );
365+
366+ // Cache the full list of snippets.
367+ if ( is_array ( $ snippets ) ) {
368+ wp_cache_set ( $ cache_key , $ snippets , CACHE_GROUP );
369+ return $ snippets ;
370+ }
371+
372+ return false ;
373+ }
374374}
0 commit comments