PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.6
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / snippet-ops.php

snippet-ops.php in Code Snippets 3.6.6, at php/snippet-ops.php

740 lines 20.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions to perform snippet operations
4 *
5 * @package Code_Snippets
6 */
7
8 namespace Code_Snippets;
9
10 use Code_Snippets\REST_API\Snippets_REST_Controller;
11 use ParseError;
12 use function Code_Snippets\Settings\get_self_option;
13 use function Code_Snippets\Settings\update_self_option;
14
15 /**
16 * Clean the cache where active snippets are stored.
17 *
18 * @param string $table_name Snippets table name.
19 * @param array<string>|false $scopes List of scopes. Optional. If not provided, will flush the cache for all scopes.
20 *
21 * @return void
22 */
23 function clean_active_snippets_cache( string $table_name, $scopes = false ) {
24 $scope_groups = $scopes ? [ $scopes ] : [
25 [ 'head-content', 'footer-content' ],
26 [ 'global', 'single-use', 'front-end' ],
27 [ 'global', 'single-use', 'admin' ],
28 ];
29
30 foreach ( $scope_groups as $scopes ) {
31 wp_cache_delete( sprintf( 'active_snippets_%s_%s', sanitize_key( join( '_', $scopes ) ), $table_name ), CACHE_GROUP );
32 }
33 }
34
35 /**
36 * Flush all snippets caches for a given database table.
37 *
38 * @param string $table_name Snippets table name.
39 *
40 * @return void
41 */
42 function clean_snippets_cache( string $table_name ) {
43 wp_cache_delete( "all_snippet_tags_$table_name", CACHE_GROUP );
44 wp_cache_delete( "all_snippets_$table_name", CACHE_GROUP );
45 clean_active_snippets_cache( $table_name );
46 }
47
48 /**
49 * Retrieve a list of snippets from the database.
50 * Read operation.
51 *
52 * @param array<string> $ids The IDs of the snippets to fetch.
53 * @param bool|null $network Retrieve multisite-wide snippets (true) or site-wide snippets (false).
54 *
55 * @return array<Snippet> List of Snippet objects.
56 *
57 * @since 2.0
58 */
59 function get_snippets( array $ids = array(), ?bool $network = null ): array {
60 global $wpdb;
61
62 // If only one ID has been passed in, defer to the get_snippet() function.
63 $ids_count = count( $ids );
64 if ( 1 === $ids_count ) {
65 return array( get_snippet( $ids[0], $network ) );
66 }
67
68 $network = DB::validate_network_param( $network );
69 $table_name = code_snippets()->db->get_table_name( $network );
70
71 $snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
72
73 // Fetch all snippets from the database if none are cached.
74 if ( ! is_array( $snippets ) ) {
75 $results = $wpdb->get_results( "SELECT * FROM $table_name", ARRAY_A );
76
77 $snippets = $results ?
78 array_map(
79 function ( $snippet_data ) use ( $network ) {
80 $snippet_data['network'] = $network;
81 return new Snippet( $snippet_data );
82 },
83 $results
84 ) :
85 array();
86
87 $snippets = apply_filters( 'code_snippets/get_snippets', $snippets, $network );
88
89 if ( 0 === $ids_count ) {
90 wp_cache_set( "all_snippets_$table_name", $snippets, CACHE_GROUP );
91 }
92 }
93
94 // If a list of IDs are provided, narrow down the snippets list.
95 if ( $ids_count > 0 ) {
96 $ids = array_map( 'intval', $ids );
97 return array_filter(
98 $snippets,
99 function ( Snippet $snippet ) use ( $ids ) {
100 return in_array( $snippet->id, $ids, true );
101 }
102 );
103 }
104
105 return $snippets;
106 }
107
108 /**
109 * Gets all used tags from the database.
110 * Read operation.
111 *
112 * @since 2.0
113 */
114 function get_all_snippet_tags() {
115 global $wpdb;
116 $table_name = code_snippets()->db->get_table_name();
117 $cache_key = "all_snippet_tags_$table_name";
118
119 $tags = wp_cache_get( $cache_key, CACHE_GROUP );
120 if ( $tags ) {
121 return $tags;
122 }
123
124 // Grab all tags from the database.
125 $tags = array();
126 $all_tags = $wpdb->get_col( "SELECT tags FROM $table_name" );
127
128 // Merge all tags into a single array.
129 foreach ( $all_tags as $snippet_tags ) {
130 $snippet_tags = code_snippets_build_tags_array( $snippet_tags );
131 $tags = array_merge( $snippet_tags, $tags );
132 }
133
134 // Remove duplicate tags.
135 $tags = array_values( array_unique( $tags, SORT_REGULAR ) );
136 wp_cache_set( $cache_key, $tags, CACHE_GROUP );
137 return $tags;
138 }
139
140 /**
141 * Make sure that the tags are a valid array.
142 *
143 * @param array|string $tags The tags to convert into an array.
144 *
145 * @return array<string> The converted tags.
146 *
147 * @since 2.0.0
148 */
149 function code_snippets_build_tags_array( $tags ): array {
150
151 /* If there are no tags set, return an empty array. */
152 if ( empty( $tags ) ) {
153 return array();
154 }
155
156 /* If the tags are set as a string, convert them into an array. */
157 if ( is_string( $tags ) ) {
158 $tags = wp_strip_all_tags( $tags );
159 $tags = str_replace( ', ', ',', $tags );
160 $tags = explode( ',', $tags );
161 }
162
163 /* If we still don't have an array, just convert whatever we do have into one. */
164 return (array) $tags;
165 }
166
167 /**
168 * Retrieve a single snippets from the database.
169 * Will return empty snippet object if no snippet ID is specified.
170 * Read operation.
171 *
172 * @param int $id The ID of the snippet to retrieve. 0 to build a new snippet.
173 * @param bool|null $network Retrieve a multisite-wide snippet (true) or site-wide snippet (false).
174 *
175 * @return Snippet A single snippet object.
176 *
177 * @since 2.0.0
178 */
179 function get_snippet( int $id = 0, ?bool $network = null ): Snippet {
180 global $wpdb;
181
182 $id = absint( $id );
183 $network = DB::validate_network_param( $network );
184 $table_name = code_snippets()->db->get_table_name( $network );
185
186 if ( 0 === $id ) {
187 // If an invalid ID is provided, then return an empty snippet object.
188 $snippet = new Snippet();
189
190 } else {
191 $cached_snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
192
193 // Attempt to fetch snippet from the cached list, if it exists.
194 if ( is_array( $cached_snippets ) ) {
195 foreach ( $cached_snippets as $snippet ) {
196 if ( $snippet->id === $id ) {
197 return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network );
198 }
199 }
200 }
201
202 // Otherwise, retrieve the snippet from the database.
203 // phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
204 $snippet_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $id ) );
205 $snippet = new Snippet( $snippet_data );
206 }
207
208 $snippet->network = $network;
209 return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network );
210 }
211
212 /**
213 * Ensure the list of shared network snippets is correct if one has been recently activated or deactivated.
214 * Write operation.
215 *
216 * @access private
217 *
218 * @param Snippet[] $snippets Snippets that was recently updated.
219 *
220 * @return boolean Whether an update was performed.
221 */
222 function update_shared_network_snippets( array $snippets ): bool {
223 $shared_ids = [];
224 $unshared_ids = [];
225
226 if ( ! is_multisite() ) {
227 return false;
228 }
229
230 foreach ( $snippets as $snippet ) {
231 if ( $snippet->network ) {
232 if ( $snippet->shared_network ) {
233 $shared_ids[] = $snippet->id;
234 } else {
235 $unshared_ids[] = $snippet->id;
236 }
237 }
238 }
239
240 if ( ! $shared_ids && ! $unshared_ids ) {
241 return false;
242 }
243
244 $existing_shared_ids = get_site_option( 'shared_network_snippets', [] );
245 $updated_shared_ids = array_values( array_diff( array_merge( $existing_shared_ids, $shared_ids ), $unshared_ids ) );
246
247 if ( $existing_shared_ids === $updated_shared_ids ) {
248 return false;
249 }
250
251 update_site_option( 'shared_network_snippets', $updated_shared_ids );
252
253 // Deactivate the snippet on all sites if necessary.
254 if ( $unshared_ids ) {
255 $sites = get_sites( [ 'fields' => 'ids' ] );
256
257 foreach ( $sites as $site ) {
258 switch_to_blog( $site );
259 $active_shared_ids = get_option( 'active_shared_network_snippets' );
260
261 if ( is_array( $active_shared_ids ) ) {
262 $active_shared_ids = array_diff( $active_shared_ids, $unshared_ids );
263 update_option( 'active_shared_network_snippets', $active_shared_ids );
264 }
265
266 clean_active_snippets_cache( code_snippets()->db->ms_table );
267 }
268
269 restore_current_blog();
270 }
271
272 return true;
273 }
274
275 /**
276 * Activates a snippet.
277 * Write operation.
278 *
279 * @param int $id ID of the snippet to activate.
280 * @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false).
281 *
282 * @return Snippet|string Snippet object on success, error message on failure.
283 * @since 2.0.0
284 */
285 function activate_snippet( int $id, ?bool $network = null ) {
286 global $wpdb;
287 $network = DB::validate_network_param( $network );
288 $table_name = code_snippets()->db->get_table_name( $network );
289
290 // Retrieve the snippet code from the database for validation before activating.
291 $snippet = get_snippet( $id, $network );
292
293 if ( 0 === $snippet->id ) {
294 // translators: %d: snippet identifier.
295 return sprintf( __( 'Could not locate snippet with ID %d.', 'code-snippets' ), $id );
296 }
297
298 $validator = new Validator( $snippet->code );
299 if ( $validator->validate() ) {
300 return __( 'Could not activate snippet: code did not pass validation.', 'code-snippets' );
301 }
302
303 $result = $wpdb->update(
304 $table_name,
305 array( 'active' => '1' ),
306 array( 'id' => $id ),
307 array( '%d' ),
308 array( '%d' )
309 );
310
311 if ( ! $result ) {
312 return __( 'Could not activate snippet.', 'code-snippets' );
313 }
314
315 update_shared_network_snippets( [ $snippet ] );
316 do_action( 'code_snippets/activate_snippet', $snippet );
317 clean_snippets_cache( $table_name );
318 return $snippet;
319 }
320
321 /**
322 * Activates multiple snippets.
323 * Write operation.
324 *
325 * @param array<integer> $ids The IDs of the snippets to activate.
326 * @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false).
327 *
328 * @return Snippet[]|null Snippets which were successfully activated, or null on failure.
329 *
330 * @since 2.0.0
331 */
332 function activate_snippets( array $ids, ?bool $network = null ): ?array {
333 global $wpdb;
334 $network = DB::validate_network_param( $network );
335 $table_name = code_snippets()->db->get_table_name( $network );
336
337 $snippets = get_snippets( $ids, $network );
338
339 if ( ! $snippets ) {
340 return null;
341 }
342
343 // Loop through each snippet code and validate individually.
344 $valid_ids = [];
345 $valid_snippets = [];
346
347 foreach ( $snippets as $snippet ) {
348 $validator = new Validator( $snippet->code );
349 $code_error = $validator->validate();
350
351 if ( ! $code_error ) {
352 $valid_ids[] = $snippet->id;
353 $valid_snippets[] = $snippet;
354 }
355 }
356
357 // If there are no valid snippets, then we're done.
358 if ( ! $valid_ids ) {
359 return null;
360 }
361
362 // Build a SQL query containing all IDs, as wpdb::update does not support OR conditionals.
363 $ids_format = implode( ',', array_fill( 0, count( $valid_ids ), '%d' ) );
364
365 // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
366 $rows_updated = $wpdb->query( $wpdb->prepare( "UPDATE $table_name SET active = 1 WHERE id IN ($ids_format)", $valid_ids ) );
367
368 if ( ! $rows_updated ) {
369 return null;
370 }
371
372 update_shared_network_snippets( $valid_snippets );
373 do_action( 'code_snippets/activate_snippets', $valid_snippets, $table_name );
374 clean_snippets_cache( $table_name );
375 return $valid_ids;
376 }
377
378 /**
379 * Deactivate a snippet.
380 * Write operation.
381 *
382 * @param int $id ID of the snippet to deactivate.
383 * @param bool|null $network Whether the snippets are multisite-wide (true) or site-wide (false).
384 *
385 * @return Snippet|null Snippet that was deactivated on success, or null on failure.
386 *
387 * @since 2.0.0
388 */
389 function deactivate_snippet( int $id, ?bool $network = null ): ?Snippet {
390 global $wpdb;
391 $network = DB::validate_network_param( $network );
392 $table = code_snippets()->db->get_table_name( $network );
393
394 // Set the snippet to active.
395 $result = $wpdb->update(
396 $table,
397 array( 'active' => '0' ),
398 array( 'id' => $id ),
399 array( '%d' ),
400 array( '%d' )
401 );
402
403 if ( ! $result ) {
404 return null;
405 }
406
407 // Update the recently active list.
408 $snippet = get_snippet( $id );
409 $recently_active = [ $id => time() ] + get_self_option( $network, 'recently_activated_snippets', [] );
410 update_self_option( $network, 'recently_activated_snippets', $recently_active );
411
412 update_shared_network_snippets( [ $snippet ] );
413 do_action( 'code_snippets/deactivate_snippet', $id, $network );
414 clean_snippets_cache( $table );
415
416 return $snippet;
417 }
418
419 /**
420 * Deletes a snippet from the database.
421 * Write operation.
422 *
423 * @param int $id ID of the snippet to delete.
424 * @param bool|null $network Delete from network-wide (true) or site-wide (false) table.
425 *
426 * @return bool Whether the operation completed successfully.
427 *
428 * @since 2.0.0
429 */
430 function delete_snippet( int $id, ?bool $network = null ): bool {
431 global $wpdb;
432 $network = DB::validate_network_param( $network );
433 $table = code_snippets()->db->get_table_name( $network );
434
435 $result = $wpdb->delete(
436 $table,
437 array( 'id' => $id ),
438 array( '%d' )
439 );
440
441 if ( $result ) {
442 do_action( 'code_snippets/delete_snippet', $id, $network );
443 clean_snippets_cache( $table );
444 }
445
446 return (bool) $result;
447 }
448
449
450 /**
451 * Test snippet code for errors, augmenting the snippet object.
452 *
453 * @param Snippet $snippet Snippet object.
454 */
455 function test_snippet_code( Snippet $snippet ) {
456 $snippet->code_error = null;
457
458 if ( 'php' !== $snippet->type ) {
459 return;
460 }
461
462 $validator = new Validator( $snippet->code );
463 $result = $validator->validate();
464
465 if ( $result ) {
466 $snippet->code_error = [ $result['message'], $result['line'] ];
467 }
468
469 if ( ! $snippet->code_error && 'single-use' !== $snippet->scope ) {
470 $result = execute_snippet( $snippet->code, $snippet->id, true );
471
472 if ( $result instanceof ParseError ) {
473 $snippet->code_error = [
474 ucfirst( rtrim( $result->getMessage(), '.' ) ) . '.',
475 $result->getLine(),
476 ];
477 }
478 }
479 }
480
481 /**
482 * Saves a snippet to the database.
483 * Write operation.
484 *
485 * @param Snippet|array<string, mixed> $snippet The snippet to add/update to the database.
486 *
487 * @return Snippet|null Updated snippet.
488 *
489 * @since 2.0.0
490 */
491 function save_snippet( $snippet ) {
492 global $wpdb;
493 $table = code_snippets()->db->get_table_name( $snippet->network );
494
495 if ( ! $snippet instanceof Snippet ) {
496 $snippet = new Snippet( $snippet );
497 }
498
499 // Update the last modification date if necessary.
500 $snippet->update_modified();
501 $snippet->increment_revision();
502
503 if ( 'php' === $snippet->type ) {
504 // Remove tags from beginning and end of snippet.
505 $snippet->code = preg_replace( '|^\s*<\?(php)?|', '', $snippet->code );
506 $snippet->code = preg_replace( '|\?>\s*$|', '', $snippet->code );
507
508 // Deactivate snippet if code contains errors.
509 if ( $snippet->active && 'single-use' !== $snippet->scope ) {
510 test_snippet_code( $snippet );
511
512 if ( $snippet->code_error ) {
513 $snippet->active = 0;
514 }
515 }
516 }
517
518 // Shared network snippets are always considered inactive.
519 $snippet->active = $snippet->active && ! $snippet->shared_network;
520
521 // Build the list of data to insert.
522 $data = [
523 'name' => $snippet->name,
524 'description' => $snippet->desc,
525 'code' => $snippet->code,
526 'tags' => $snippet->tags_list,
527 'scope' => $snippet->scope,
528 'priority' => $snippet->priority,
529 'active' => intval( $snippet->active ),
530 'modified' => $snippet->modified,
531 'revision' => $snippet->revision,
532 'cloud_id' => $snippet->cloud_id ? $snippet->cloud_id : null,
533 ];
534
535 // Create a new snippet if the ID is not set.
536 if ( 0 === $snippet->id ) {
537 $result = $wpdb->insert( $table, $data, '%s' );
538 if ( false === $result ) {
539 return null;
540 }
541
542 $snippet->id = $wpdb->insert_id;
543 do_action( 'code_snippets/create_snippet', $snippet, $table );
544 } else {
545
546 // Otherwise, update the snippet data.
547 $result = $wpdb->update( $table, $data, [ 'id' => $snippet->id ], null, [ '%d' ] );
548 if ( false === $result ) {
549 return null;
550 }
551
552 do_action( 'code_snippets/update_snippet', $snippet, $table );
553 }
554
555 update_shared_network_snippets( [ $snippet ] );
556 clean_snippets_cache( $table );
557 return $snippet;
558 }
559
560 /**
561 * Execute a snippet.
562 * Execute operation.
563 *
564 * Code must NOT be escaped, as it will be executed directly.
565 *
566 * @param string $code Snippet code to execute.
567 * @param integer $id Snippet ID.
568 * @param boolean $force Force snippet execution, even if save mode is active.
569 *
570 * @return ParseError|mixed Code error if encountered during execution, or result of snippet execution otherwise.
571 *
572 * @since 2.0.0
573 */
574 function execute_snippet( string $code, int $id = 0, bool $force = false ) {
575 if ( empty( $code ) || ( ! $force && defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) ) {
576 return false;
577 }
578
579 ob_start();
580
581 try {
582 $result = eval( $code );
583 } catch ( ParseError $parse_error ) {
584 $result = $parse_error;
585 }
586
587 ob_end_clean();
588
589 do_action( 'code_snippets/after_execute_snippet', $code, $id, $result );
590 return $result;
591 }
592
593 /**
594 * Run the active snippets.
595 * Read-write-execute operation.
596 *
597 * @return bool true on success, false on failure.
598 *
599 * @since 2.0.0
600 */
601 function execute_active_snippets(): bool {
602 global $wpdb;
603
604 // Bail early if safe mode is active.
605 if ( ( defined( 'CODE_SNIPPETS_SAFE_MODE' ) && CODE_SNIPPETS_SAFE_MODE ) ||
606 ! apply_filters( 'code_snippets/execute_snippets', true ) ) {
607 return false;
608 }
609
610 $db = code_snippets()->db;
611 $scopes = array( 'global', 'single-use', is_admin() ? 'admin' : 'front-end' );
612 $data = $db->fetch_active_snippets( $scopes );
613
614 // Detect if a snippet is currently being edited, and if so, spare it from execution.
615 $edit_id = 0;
616 $edit_table = $db->table;
617
618 if ( wp_is_json_request() && ! empty( $_SERVER['REQUEST_URI'] ) ) {
619 $url = wp_parse_url( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
620
621 if ( isset( $url['path'] ) && false !== strpos( $url['path'], Snippets_REST_Controller::get_prefixed_base_route() ) ) {
622 $path_parts = explode( '/', $url['path'] );
623 $edit_id = intval( end( $path_parts ) );
624
625 if ( ! empty( $url['query'] ) ) {
626 wp_parse_str( $url['query'], $path_params );
627 $edit_table = isset( $path_params['network'] ) && rest_sanitize_boolean( $path_params['network'] ) ?
628 $db->ms_table : $db->table;
629 }
630 }
631 }
632
633 foreach ( $data as $table_name => $active_snippets ) {
634
635 // Loop through the returned snippets and execute the PHP code.
636 foreach ( $active_snippets as $snippet ) {
637 $snippet_id = intval( $snippet['id'] );
638 $code = $snippet['code'];
639
640 // If the snippet is a single-use snippet, deactivate it before execution to ensure that the process always happens.
641 if ( 'single-use' === $snippet['scope'] ) {
642 $active_shared_ids = get_option( 'active_shared_network_snippets', array() );
643
644 if ( $table_name === $db->ms_table && is_array( $active_shared_ids ) && in_array( $snippet_id, $active_shared_ids, true ) ) {
645 unset( $active_shared_ids[ array_search( $snippet_id, $active_shared_ids, true ) ] );
646 $active_shared_ids = array_values( $active_shared_ids );
647 update_option( 'active_shared_network_snippets', $active_shared_ids );
648 clean_active_snippets_cache( $table_name );
649 } else {
650 $wpdb->update(
651 $table_name,
652 array( 'active' => '0' ),
653 array( 'id' => $snippet_id ),
654 array( '%d' ),
655 array( '%d' )
656 );
657 clean_snippets_cache( $table_name );
658 }
659 }
660
661 if ( apply_filters( 'code_snippets/allow_execute_snippet', true, $snippet_id, $table_name ) &&
662 ! ( $edit_id === $snippet_id && $table_name === $edit_table ) ) {
663 execute_snippet( $code, $snippet_id );
664 }
665 }
666 }
667
668 return true;
669 }
670
671 /**
672 * Retrieve a single snippets from the database using its cloud ID.
673 *
674 * Read operation.
675 *
676 * @param string $cloud_id The Cloud ID of the snippet to retrieve.
677 * @param boolean|null $multisite Retrieve a multisite-wide snippet (true) or site-wide snippet (false).
678 *
679 * @return Snippet|null A single snippet object or null if no snippet was found.
680 *
681 * @since 3.5.0
682 */
683 function get_snippet_by_cloud_id( string $cloud_id, ?bool $multisite = null ): ?Snippet {
684 global $wpdb;
685
686 $multisite = DB::validate_network_param( $multisite );
687 $table_name = code_snippets()->db->get_table_name( $multisite );
688
689 $cached_snippets = wp_cache_get( "all_snippets_$table_name", CACHE_GROUP );
690
691 // Attempt to fetch snippet from the cached list, if it exists.
692 if ( is_array( $cached_snippets ) ) {
693 foreach ( $cached_snippets as $snippet ) {
694 if ( $snippet->cloud_id === $cloud_id ) {
695 return apply_filters( 'code_snippets/get_snippet_by_cloud_id', $snippet, $cloud_id, $multisite );
696 }
697 }
698 }
699
700 // Otherwise, search for the snippet from the database.
701 $snippet_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE cloud_id = %s", $cloud_id ) ); // cache pass, db call ok.
702 $snippet = $snippet_data ? new Snippet( $snippet_data ) : null;
703
704 return apply_filters( 'code_snippets/get_snippet_by_cloud_id', $snippet, $cloud_id, $multisite );
705 }
706
707 /**
708 * Update a snippet entry given a list of fields.
709 * Write operation.
710 *
711 * @param int $snippet_id ID of the snippet to update.
712 * @param array<string, mixed> $fields An array of fields mapped to their values.
713 * @param bool|null $network Update in network-wide (true) or site-wide (false) table.
714 */
715 function update_snippet_fields( int $snippet_id, array $fields, ?bool $network = null ) {
716 global $wpdb;
717
718 $table = code_snippets()->db->get_table_name( $network );
719
720 // Build a new snippet object for the validation.
721 $snippet = new Snippet();
722 $snippet->id = $snippet_id;
723
724 // Validate fields through the snippet class and copy them into a clean array.
725 $clean_fields = array();
726
727 foreach ( $fields as $field => $value ) {
728
729 if ( $snippet->set_field( $field, $value ) ) {
730 $clean_fields[ $field ] = $snippet->$field;
731 }
732 }
733
734 // Update the snippet in the database.
735 $wpdb->update( $table, $clean_fields, array( 'id' => $snippet->id ), null, array( '%d' ) );
736
737 do_action( 'code_snippets/update_snippet', $snippet, $table );
738 clean_snippets_cache( $table );
739 }
740