1111use function Code_Snippets \Settings \get_self_option ;
1212use function Code_Snippets \Settings \update_self_option ;
1313
14+ /**
15+ * Get the locked status for a snippet from wp_options.
16+ *
17+ * @param int $snippet_id Snippet ID.
18+ * @param bool|null $network Whether the snippet is network-wide (true) or site-wide (false).
19+ *
20+ * @return bool Whether the snippet is locked.
21+ */
22+ function get_snippet_locked ( int $ snippet_id , ?bool $ network = null ): bool {
23+ $ network = DB ::validate_network_param ( $ network );
24+ $ option_name = 'code_snippets_locked ' ;
25+
26+ if ( $ network && is_multisite () ) {
27+ $ locked_snippets = get_site_option ( $ option_name , array () );
28+ } else {
29+ $ locked_snippets = get_option ( $ option_name , array () );
30+ }
31+
32+ return isset ( $ locked_snippets [ $ snippet_id ] ) && $ locked_snippets [ $ snippet_id ];
33+ }
34+
35+ /**
36+ * Set the locked status for a snippet in wp_options.
37+ *
38+ * @param int $snippet_id Snippet ID.
39+ * @param bool $locked Whether the snippet should be locked.
40+ * @param bool|null $network Whether the snippet is network-wide (true) or site-wide (false).
41+ *
42+ * @return void
43+ */
44+ function set_snippet_locked ( int $ snippet_id , bool $ locked , ?bool $ network = null ): void {
45+ $ network = DB ::validate_network_param ( $ network );
46+ $ option_name = 'code_snippets_locked ' ;
47+
48+ if ( $ network && is_multisite () ) {
49+ $ locked_snippets = get_site_option ( $ option_name , array () );
50+ } else {
51+ $ locked_snippets = get_option ( $ option_name , array () );
52+ }
53+
54+ if ( $ locked ) {
55+ $ locked_snippets [ $ snippet_id ] = true ;
56+ } else {
57+ unset( $ locked_snippets [ $ snippet_id ] );
58+ }
59+
60+ if ( $ network && is_multisite () ) {
61+ update_site_option ( $ option_name , $ locked_snippets );
62+ } else {
63+ update_option ( $ option_name , $ locked_snippets );
64+ }
65+ }
66+
1467/**
1568 * Clean the cache where active snippets are stored.
1669 *
@@ -77,7 +130,12 @@ function get_snippets( array $ids = array(), ?bool $network = null ): array {
77130 array_map (
78131 function ( $ snippet_data ) use ( $ network ) {
79132 $ snippet_data ['network ' ] = $ network ;
80- return new Snippet ( $ snippet_data );
133+ $ snippet = new Snippet ( $ snippet_data );
134+ // Load locked from wp_options.
135+ if ( $ snippet ->id > 0 ) {
136+ $ snippet ->locked = get_snippet_locked ( $ snippet ->id , $ network );
137+ }
138+ return $ snippet ;
81139 },
82140 $ results
83141 ) :
@@ -207,6 +265,12 @@ function get_snippet( int $id = 0, ?bool $network = null ): Snippet {
207265 }
208266
209267 $ snippet ->network = $ network ;
268+
269+ // Load locked from wp_options if snippet has an ID.
270+ if ( $ snippet ->id > 0 ) {
271+ $ snippet ->locked = get_snippet_locked ( $ snippet ->id , $ network );
272+ }
273+
210274 return apply_filters ( 'code_snippets/get_snippet ' , $ snippet , $ id , $ network );
211275}
212276
@@ -593,7 +657,7 @@ function save_snippet( $snippet ) {
593657 // Shared network snippets are always considered inactive.
594658 $ snippet ->active = $ snippet ->active && ! $ snippet ->shared_network ;
595659
596- // Build the list of data to insert.
660+ // Build the list of data to insert (excluding locked, which is stored in wp_options) .
597661 $ data = [
598662 'name ' => $ snippet ->name ,
599663 'description ' => $ snippet ->desc ,
@@ -628,6 +692,11 @@ function save_snippet( $snippet ) {
628692 do_action ( 'code_snippets/update_snippet ' , $ snippet , $ table );
629693 }
630694
695+ // Save locked to wp_options.
696+ if ( $ snippet ->id > 0 ) {
697+ set_snippet_locked ( $ snippet ->id , $ snippet ->locked , $ snippet ->network );
698+ }
699+
631700 update_shared_network_snippets ( [ $ snippet ] );
632701 clean_snippets_cache ( $ table );
633702 return $ snippet ;
@@ -704,6 +773,12 @@ function get_snippet_by_cloud_id( string $cloud_id, ?bool $multisite = null ): ?
704773 $ snippet_data = $ wpdb ->get_row ( $ wpdb ->prepare ( "SELECT * FROM $ table_name WHERE cloud_id = %s " , $ cloud_id ) ); // cache pass, db call ok.
705774 $ snippet = $ snippet_data ? new Snippet ( $ snippet_data ) : null ;
706775
776+ // Load locked from wp_options if snippet exists.
777+ if ( $ snippet && $ snippet ->id > 0 ) {
778+ $ snippet ->network = $ multisite ;
779+ $ snippet ->locked = get_snippet_locked ( $ snippet ->id , $ multisite );
780+ }
781+
707782 return apply_filters ( 'code_snippets/get_snippet_by_cloud_id ' , $ snippet , $ cloud_id , $ multisite );
708783}
709784
@@ -726,16 +801,31 @@ function update_snippet_fields( int $snippet_id, array $fields, ?bool $network =
726801
727802 // Validate fields through the snippet class and copy them into a clean array.
728803 $ clean_fields = array ();
804+ $ locked_value = null ;
729805
730806 foreach ( $ fields as $ field => $ value ) {
807+ // Handle locked separately (stored in wp_options).
808+ if ( 'locked ' === $ field ) {
809+ if ( $ snippet ->set_field ( $ field , $ value ) ) {
810+ $ locked_value = $ snippet ->$ field ;
811+ }
812+ continue ;
813+ }
731814
732815 if ( $ snippet ->set_field ( $ field , $ value ) ) {
733816 $ clean_fields [ $ field ] = $ snippet ->$ field ;
734817 }
735818 }
736819
737- // Update the snippet in the database.
738- $ wpdb ->update ( $ table , $ clean_fields , array ( 'id ' => $ snippet ->id ), null , array ( '%d ' ) );
820+ // Update the snippet in the database (excluding locked).
821+ if ( ! empty ( $ clean_fields ) ) {
822+ $ wpdb ->update ( $ table , $ clean_fields , array ( 'id ' => $ snippet ->id ), null , array ( '%d ' ) );
823+ }
824+
825+ // Save locked to wp_options if it was provided.
826+ if ( null !== $ locked_value ) {
827+ set_snippet_locked ( $ snippet ->id , $ locked_value , $ network );
828+ }
739829
740830 do_action ( 'code_snippets/update_snippet ' , $ snippet ->id , $ table );
741831 clean_snippets_cache ( $ table );
0 commit comments