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 / settings / settings.php

settings.php in Code Snippets 3.6.6, at php/settings/settings.php

336 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file registers the settings
4 *
5 * @package Code_Snippets
6 * @subpackage Settings
7 */
8
9 namespace Code_Snippets\Settings;
10
11 use Code_Snippets\Welcome_API;
12 use Code_Snippets\Welcome_Menu;
13 use function Code_Snippets\clean_snippets_cache;
14 use function Code_Snippets\code_snippets;
15
16 const CACHE_KEY = 'code_snippets_settings';
17 const OPTION_GROUP = 'code-snippets';
18 const OPTION_NAME = 'code_snippets_settings';
19
20 /**
21 * Add a new option for either the current site or the current network
22 *
23 * @param bool $network Whether to add a network-wide option.
24 * @param string $option Name of option to add. Expected to not be SQL-escaped.
25 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
26 *
27 * @return bool False if the option was not added. True if the option was added.
28 */
29 function add_self_option( bool $network, string $option, $value ): bool {
30 return $network ? add_site_option( $option, $value ) : add_option( $option, $value );
31 }
32
33 /**
34 * Retrieves an option value based on an option name from either the current site or the current network
35 *
36 * @param bool $network Whether to get a network-wide option.
37 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
38 * @param mixed $default_value Optional value to return if option doesn't exist. Default false.
39 *
40 * @return mixed Value set for the option.
41 */
42 function get_self_option( bool $network, string $option, $default_value = false ) {
43 return $network ? get_site_option( $option, $default_value ) : get_option( $option, $default_value );
44 }
45
46 /**
47 * Update the value of an option that was already added on the current site or the current network
48 *
49 * @param bool $network Whether to update a network-wide option.
50 * @param string $option Name of option. Expected to not be SQL-escaped.
51 * @param mixed $value Option value. Expected to not be SQL-escaped.
52 *
53 * @return bool False if value was not updated. True if value was updated.
54 */
55 function update_self_option( bool $network, string $option, $value ): bool {
56 return $network ? update_site_option( $option, $value ) : update_option( $option, $value );
57 }
58
59 /**
60 * Returns 'true' if plugin settings are unified on a multisite installation
61 * under the Network Admin settings menu
62 *
63 * This option is controlled by the "Enable administration menus" setting on the Network Settings menu
64 *
65 * @return bool
66 */
67 function are_settings_unified(): bool {
68 if ( ! is_multisite() ) {
69 return false;
70 }
71
72 $menu_perms = get_site_option( 'menu_items', array() );
73 return empty( $menu_perms['snippets_settings'] );
74 }
75
76 /**
77 * Retrieve the setting values from the database.
78 * If a setting does not exist in the database, the default value will be returned.
79 *
80 * @return array<string, array<string, mixed>>
81 */
82 function get_settings_values(): array {
83 $settings = wp_cache_get( CACHE_KEY );
84 if ( $settings ) {
85 return $settings;
86 }
87
88 $settings = get_default_settings();
89 $saved = get_self_option( are_settings_unified(), OPTION_NAME, array() );
90
91 foreach ( $settings as $section => $fields ) {
92 if ( isset( $saved[ $section ] ) ) {
93 $settings[ $section ] = array_replace( $fields, $saved[ $section ] );
94 }
95 }
96
97 wp_cache_set( CACHE_KEY, $settings );
98 return $settings;
99 }
100
101 /**
102 * Retrieve an individual setting field value
103 *
104 * @param string $section ID of the section the setting belongs to.
105 * @param string $field ID of the setting field.
106 *
107 * @return mixed
108 */
109 function get_setting( string $section, string $field ) {
110 $settings = get_settings_values();
111
112 return $settings[ $section ][ $field ] ?? null;
113 }
114
115 /**
116 * Update a single setting to a new value.
117 *
118 * @param string $section ID of the section the setting belongs to.
119 * @param string $field ID of the setting field.
120 * @param mixed $new_value Setting value. Expected to not be SQL-escaped.
121 *
122 * @return bool False if value was not updated. True if value was updated.
123 */
124 function update_setting( string $section, string $field, $new_value ): bool {
125 $settings = get_settings_values();
126
127 $settings[ $section ][ $field ] = $new_value;
128
129 wp_cache_set( CACHE_KEY, $settings );
130 return update_self_option( are_settings_unified(), OPTION_NAME, $settings );
131 }
132
133 /**
134 * Retrieve the settings sections
135 *
136 * @return array<string, string> Settings sections.
137 */
138 function get_settings_sections(): array {
139 $sections = array(
140 'general' => __( 'General', 'code-snippets' ),
141 'editor' => __( 'Code Editor', 'code-snippets' ),
142 'debug' => __( 'Debug', 'code-snippets' ),
143 );
144
145 return apply_filters( 'code_snippets_settings_sections', $sections );
146 }
147
148 /**
149 * Register settings sections, fields, etc
150 */
151 function register_plugin_settings() {
152 if ( are_settings_unified() ) {
153 if ( ! get_site_option( OPTION_NAME ) ) {
154 add_site_option( OPTION_NAME, get_default_settings() );
155 }
156 } elseif ( ! get_option( OPTION_NAME ) ) {
157 add_option( OPTION_NAME, get_default_settings() );
158 }
159
160 // Register the setting.
161 register_setting(
162 OPTION_GROUP,
163 OPTION_NAME,
164 [ 'sanitize_callback' => __NAMESPACE__ . '\\sanitize_settings' ]
165 );
166
167 // Register settings sections.
168 foreach ( get_settings_sections() as $section_id => $section_name ) {
169 add_settings_section( $section_id, $section_name, '__return_empty_string', 'code-snippets' );
170 }
171
172 // Register settings fields.
173 foreach ( get_settings_fields() as $section_id => $fields ) {
174 foreach ( $fields as $field_id => $field ) {
175 $field_object = new Setting_Field( $section_id, $field_id, $field );
176 add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id );
177 }
178 }
179
180 // Add editor preview as a field.
181 add_settings_field(
182 'editor_preview',
183 __( 'Editor Preview', 'code-snippets' ),
184 __NAMESPACE__ . '\\render_editor_preview',
185 'code-snippets',
186 'editor'
187 );
188 }
189
190 add_action( 'admin_init', __NAMESPACE__ . '\\register_plugin_settings' );
191
192 /**
193 * Sanitize a single setting value.
194 *
195 * @param array<string, mixed> $field Setting field information.
196 * @param mixed $input_value User input setting value, or null if missing.
197 *
198 * @return mixed Sanitized setting value, or null if unset.
199 */
200 function sanitize_setting_value( array $field, $input_value ) {
201 switch ( $field['type'] ) {
202
203 case 'checkbox':
204 return 'on' === $input_value;
205
206 case 'number':
207 return intval( $input_value );
208
209 case 'select':
210 $select_options = array_map( 'strval', array_keys( $field['options'] ) );
211 return in_array( strval( $input_value ), $select_options, true ) ? $input_value : null;
212
213 case 'checkboxes':
214 $results = [];
215
216 if ( ! empty( $input_value ) ) {
217 foreach ( $field['options'] as $option_id => $option_label ) {
218 if ( isset( $input_value[ $option_id ] ) && 'on' === $input_value[ $option_id ] ) {
219 $results[] = $option_id;
220 }
221 }
222 }
223
224 return $results;
225
226 case 'text':
227 return trim( sanitize_text_field( $input_value ) );
228
229 case 'callback':
230 return isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ?
231 call_user_func( $field['sanitize_callback'], $input_value ) :
232 null;
233
234 default:
235 return null;
236 }
237 }
238
239 /**
240 * Process settings actions.
241 *
242 * @param array $input Provided settings input.
243 *
244 * @return array|null New $input value to return, or null to continue with settings update process.
245 */
246 function process_settings_actions( array $input ): ?array {
247
248 if ( isset( $input['reset_settings'] ) ) {
249 add_settings_error(
250 OPTION_NAME,
251 'settings_reset',
252 __( 'All settings have been reset to their defaults.', 'code-snippets' ),
253 'updated'
254 );
255
256 return [];
257 }
258
259 if ( isset( $input['debug']['database_update'] ) ) {
260 code_snippets()->db->create_or_upgrade_tables();
261
262 add_settings_error(
263 OPTION_NAME,
264 'database_update_done',
265 __( 'Successfully performed database table upgrade.', 'code-snippets' ),
266 'updated'
267 );
268 }
269
270 if ( isset( $input['debug']['reset_caches'] ) ) {
271 Welcome_API::clear_cache();
272 clean_snippets_cache( code_snippets()->db->get_table_name( false ) );
273
274 if ( is_multisite() ) {
275 clean_snippets_cache( code_snippets()->db->get_table_name( true ) );
276 }
277
278 add_settings_error(
279 OPTION_NAME,
280 'snippet_caches_reset',
281 __( 'Successfully reset snippets caches.', 'code-snippets' ),
282 'updated'
283 );
284 }
285
286 return null;
287 }
288
289 /**
290 * Validate the settings
291 *
292 * @param array<string, array<string, mixed>> $input The received settings.
293 *
294 * @return array<string, array<string, mixed>> The validated settings.
295 */
296 function sanitize_settings( array $input ): array {
297 wp_cache_delete( CACHE_KEY );
298 $result = process_settings_actions( $input );
299
300 if ( ! is_null( $result ) ) {
301 return $result;
302 }
303
304 $settings = get_settings_values();
305 $updated = false;
306
307 // Don't directly loop through $input as it does not include as deselected checkboxes.
308 foreach ( get_settings_fields() as $section_id => $fields ) {
309 foreach ( $fields as $field_id => $field ) {
310
311 // Fetch the corresponding input value from the posted data.
312 $input_value = $input[ $section_id ][ $field_id ] ?? null;
313
314 // Attempt to sanitize the setting value.
315 $sanitized_value = sanitize_setting_value( $field, $input_value );
316
317 if ( ! is_null( $sanitized_value ) && $settings[ $section_id ][ $field_id ] !== $sanitized_value ) {
318 $settings[ $section_id ][ $field_id ] = $sanitized_value;
319 $updated = true;
320 }
321 }
322 }
323
324 // Add an updated message.
325 if ( $updated ) {
326 add_settings_error(
327 OPTION_NAME,
328 'settings-saved',
329 __( 'Settings saved.', 'code-snippets' ),
330 'updated'
331 );
332 }
333
334 return $settings;
335 }
336