PluginProbe
Code Snippets / 3.0.0
Code Snippets v3.0.0
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.0.0, at php/settings/settings.php

287 lines 7.9 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 const NS = __NAMESPACE__ . '\\';
12
13 /**
14 * Add a new option for either the current site or the current network
15 *
16 * @param bool $network Whether to add a network-wide option.
17 * @param string $option Name of option to add. Expected to not be SQL-escaped.
18 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped.
19 *
20 * @return bool False if the option was not added. True if the option was added.
21 */
22 function add_self_option( $network, $option, $value ) {
23 return $network ? add_site_option( $option, $value ) : add_option( $option, $value );
24 }
25
26 /**
27 * Retrieves an option value based on an option name from either the current site or the current network
28 *
29 * @param bool $network Whether to get a network-wide option.
30 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
31 * @param mixed $default Optional value to return if option doesn't exist. Default false.
32 *
33 * @return mixed Value set for the option.
34 */
35 function get_self_option( $network, $option, $default = false ) {
36 return $network ? get_site_option( $option, $default ) : get_option( $option, $default );
37 }
38
39 /**
40 * Update the value of an option that was already added on the current site or the current network
41 *
42 * @param bool $network Whether to update a network-wide option.
43 * @param string $option Name of option. Expected to not be SQL-escaped.
44 * @param mixed $value Option value. Expected to not be SQL-escaped.
45 *
46 * @return bool False if value was not updated. True if value was updated.
47 */
48 function update_self_option( $network, $option, $value ) {
49 return $network ? update_site_option( $option, $value ) : update_option( $option, $value );
50 }
51
52 /**
53 * Returns 'true' if plugin settings are unified on a multisite installation
54 * under the Network Admin settings menu
55 *
56 * This option is controlled by the "Enable administration menus" setting on the Network Settings menu
57 *
58 * @return bool
59 */
60 function are_settings_unified() {
61
62 if ( ! is_multisite() ) {
63 return false;
64 }
65
66 $menu_perms = get_site_option( 'menu_items', array() );
67
68 return empty( $menu_perms['snippets_settings'] );
69 }
70
71 /**
72 * Retrieve the setting values from the database.
73 * If a setting does not exist in the database, the default value will be returned.
74 *
75 * @return array
76 */
77 function get_settings_values() {
78
79 /* Check if the settings have been cached */
80 $settings = wp_cache_get( 'code_snippets_settings' );
81 if ( $settings ) {
82 return $settings;
83 }
84
85 /* Begin with the default settings */
86 $settings = get_default_settings();
87
88 /* Retrieve saved settings from the database */
89 $saved = get_self_option( are_settings_unified(), 'code_snippets_settings', array() );
90
91 /* Replace the default field values with the ones saved in the database */
92 foreach ( $settings as $section => $fields ) {
93 if ( isset( $saved[ $section ] ) ) {
94 $settings[ $section ] = array_replace( $fields, $saved[ $section ] );
95 }
96 }
97
98 wp_cache_set( 'code_snippets_settings', $settings );
99
100 return $settings;
101 }
102
103 /**
104 * Retrieve an individual setting field value
105 *
106 * @param string $section ID of the section the setting belongs to.
107 * @param string $field ID of the setting field.
108 *
109 * @return mixed
110 */
111 function get_setting( $section, $field ) {
112 $settings = get_settings_values();
113
114 return $settings[ $section ][ $field ];
115 }
116
117 /**
118 * Update a single setting to a new value.
119 *
120 * @param string $section ID of the section the setting belongs to.
121 * @param string $field ID of the setting field.
122 * @param mixed $new_value Setting value. Expected to not be SQL-escaped.
123 *
124 * @return bool False if value was not updated. True if value was updated.
125 */
126 function update_setting( $section, $field, $new_value ) {
127 $settings = get_settings_values();
128
129 $settings[ $section ][ $field ] = $new_value;
130
131 wp_cache_set( 'code_snippets_settings', $settings );
132 return update_self_option( are_settings_unified(), 'code_snippets_settings', $settings );
133 }
134
135 /**
136 * Retrieve the settings sections
137 *
138 * @return array
139 */
140 function get_settings_sections() {
141 $sections = array(
142 'general' => __( 'General', 'code-snippets' ),
143 'description_editor' => __( 'Description Editor', 'code-snippets' ),
144 'editor' => __( 'Code Editor', 'code-snippets' ),
145 );
146
147 return apply_filters( 'code_snippets_settings_sections', $sections );
148 }
149
150 /**
151 * Register settings sections, fields, etc
152 */
153 function register_plugin_settings() {
154
155 if ( are_settings_unified() ) {
156
157 if ( ! get_site_option( 'code_snippets_settings', false ) ) {
158 add_site_option( 'code_snippets_settings', get_default_settings() );
159 }
160 } else {
161
162 if ( ! get_option( 'code_snippets_settings', false ) ) {
163 add_option( 'code_snippets_settings', get_default_settings() );
164 }
165 }
166
167 /* Register the setting */
168 register_setting(
169 'code-snippets',
170 'code_snippets_settings',
171 array( 'sanitize_callback' => NS . 'sanitize_settings' )
172 );
173
174 /* Register settings sections */
175 $sections = get_settings_sections();
176
177 if ( ! get_setting( 'general', 'enable_description' ) ) {
178 unset( $sections['description_editor'] );
179 }
180
181 foreach ( $sections as $section_id => $section_name ) {
182 add_settings_section( $section_id, $section_name, null, 'code-snippets' );
183 }
184
185 /* Register settings fields */
186 foreach ( get_settings_fields() as $section_id => $fields ) {
187 foreach ( $fields as $field_id => $field ) {
188 $field_object = new Setting_Field( $section_id, $field_id, $field );
189 add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id );
190 }
191 }
192
193 /* Add editor preview as a field */
194 add_settings_field(
195 'editor_preview',
196 __( 'Editor Preview', 'code-snippets' ),
197 NS . 'render_editor_preview',
198 'code-snippets',
199 'editor'
200 );
201 }
202
203 add_action( 'admin_init', NS . 'register_plugin_settings' );
204
205 /**
206 * Sanitize a single setting value.
207 *
208 * @param array $field Setting field information.
209 * @param mixed $input_value User input setting value, or null if missing.
210 *
211 * @return mixed Sanitized setting value, or null if unset.
212 */
213 function sanitize_setting_value( $field, $input_value ) {
214 switch ( $field['type'] ) {
215
216 case 'checkbox':
217 return 'on' === $input_value;
218
219 case 'number':
220 return absint( $input_value );
221
222 case 'select':
223 // phpcs:ignore WordPress.PHP.StrictInArray.FoundNonStrictFalse
224 return in_array( $input_value, array_keys( $field['options'] ), false ) ? $input_value : null;
225
226 case 'checkboxes':
227 $results = [];
228
229 if ( ! empty( $input_value ) ) {
230 foreach ( $field['options'] as $option_id => $option_label ) {
231 if ( isset( $input_value[ $option_id ] ) && 'on' === $input_value[ $option_id ] ) {
232 $results[] = $option_id;
233 }
234 }
235 }
236
237 return $results;
238
239 case 'text':
240 return trim( sanitize_text_field( $input_value ) );
241
242 default:
243 return null;
244 }
245 }
246
247 /**
248 * Validate the settings
249 *
250 * @param array $input The received settings.
251 *
252 * @return array The validated settings.
253 */
254 function sanitize_settings( array $input ) {
255 $settings = get_settings_values();
256 $updated = false;
257
258 // don't directly loop through $input as it does not include as deselected checkboxes.
259 foreach ( get_settings_fields() as $section_id => $fields ) {
260 foreach ( $fields as $field_id => $field ) {
261
262 // fetch the corresponding input value from the posted data.
263 $input_value = isset( $input[ $section_id ][ $field_id ] ) ? $input[ $section_id ][ $field_id ] : null;
264
265 // attempt to sanitize the setting value
266 $sanitized_value = sanitize_setting_value( $field, $input_value );
267
268 if ( ! is_null( $sanitized_value ) && $settings[ $section_id ][ $field_id ] !== $sanitized_value ) {
269 $settings[ $section_id ][ $field_id ] = $sanitized_value;
270 $updated = true;
271 }
272 }
273 }
274
275 /* Add an updated message */
276 if ( $updated ) {
277 add_settings_error(
278 'code-snippets-settings-notices',
279 'settings-saved',
280 __( 'Settings saved.', 'code-snippets' ),
281 'updated'
282 );
283 }
284
285 return $settings;
286 }
287