PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.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 2.14.6, at php/settings/settings.php

216 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file registers the settings
5 *
6 * @package Code_Snippets
7 */
8
9 /**
10 * Returns 'true' if plugin settings are unified on a multisite installation
11 * under the Network Admin settings menu
12 *
13 * This option is controlled by the "Enable administration menus" setting on the Network Settings menu
14 *
15 * @return bool
16 */
17 function code_snippets_unified_settings() {
18
19 if ( ! is_multisite() ) {
20 return false;
21 }
22
23 $menu_perms = get_site_option( 'menu_items', array() );
24
25 return empty( $menu_perms['snippets_settings'] );
26 }
27
28 /**
29 * Retrieve the setting values from the database.
30 * If a setting does not exist in the database, the default value will be returned.
31 *
32 * @return array
33 */
34 function code_snippets_get_settings() {
35
36 /* Check if the settings have been cached */
37 $settings = wp_cache_get( 'code_snippets_settings' );
38 if ( $settings ) {
39 return $settings;
40 }
41
42 /* Begin with the default settings */
43 $settings = code_snippets_get_default_settings();
44
45 /* Retrieve saved settings from the database */
46 $saved = code_snippets_unified_settings() ?
47 get_site_option( 'code_snippets_settings', array() ) :
48 get_option( 'code_snippets_settings', array() );
49
50 /* Replace the default field values with the ones saved in the database */
51 if ( function_exists( 'array_replace_recursive' ) ) {
52
53 /* Use the much more efficient array_replace_recursive() function in PHP 5.3 and later */
54 $settings = array_replace_recursive( $settings, $saved );
55 } else {
56
57 /* Otherwise, do it manually */
58 foreach ( $settings as $section => $fields ) {
59 foreach ( $fields as $field => $value ) {
60
61 if ( isset( $saved[ $section ][ $field ] ) ) {
62 $settings[ $section ][ $field ] = $saved[ $section ][ $field ];
63 }
64 }
65 }
66 }
67
68 wp_cache_set( 'code_snippets_settings', $settings );
69
70 return $settings;
71 }
72
73 /**
74 * Retrieve an individual setting field value
75 *
76 * @param string $section The ID of the section the setting belongs to
77 * @param string $field The ID of the setting field
78 *
79 * @return mixed
80 */
81 function code_snippets_get_setting( $section, $field ) {
82 $settings = code_snippets_get_settings();
83
84 return $settings[ $section ][ $field ];
85 }
86
87 /**
88 * Retrieve the settings sections
89 * @return array
90 */
91 function code_snippets_get_settings_sections() {
92 $sections = array(
93 'general' => __( 'General', 'code-snippets' ),
94 'description_editor' => __( 'Description Editor', 'code-snippets' ),
95 'editor' => __( 'Code Editor', 'code-snippets' ),
96 );
97
98 return apply_filters( 'code_snippets_settings_sections', $sections );
99 }
100
101 /**
102 * Register settings sections, fields, etc
103 */
104 function code_snippets_register_settings() {
105
106 if ( code_snippets_unified_settings() ) {
107
108 if ( ! get_site_option( 'code_snippets_settings', false ) ) {
109 add_site_option( 'code_snippets_settings', code_snippets_get_default_settings() );
110 }
111 } else {
112
113 if ( ! get_option( 'code_snippets_settings', false ) ) {
114 add_option( 'code_snippets_settings', code_snippets_get_default_settings() );
115 }
116 }
117
118 /* Register the setting */
119 register_setting( 'code-snippets', 'code_snippets_settings', 'code_snippets_settings_validate' );
120
121 /* Register settings sections */
122 foreach ( code_snippets_get_settings_sections() as $section_id => $section_name ) {
123 add_settings_section(
124 'code-snippets-' . $section_id,
125 $section_name,
126 '__return_empty_string',
127 'code-snippets'
128 );
129 }
130
131 /* Register settings fields */
132 foreach ( code_snippets_get_settings_fields() as $section_id => $fields ) {
133 foreach ( $fields as $field_id => $field ) {
134 $atts = $field;
135 $atts['id'] = $field_id;
136 $atts['section'] = $section_id;
137
138 add_settings_field(
139 'code_snippets_' . $field_id,
140 $field['name'],
141 "code_snippets_{$field['type']}_field",
142 'code-snippets',
143 'code-snippets-' . $section_id,
144 $atts
145 );
146 }
147 }
148
149 /* Add editor preview as a field */
150 add_settings_field(
151 'code_snippets_editor_preview',
152 __( 'Editor Preview', 'code-snippets' ),
153 'code_snippets_settings_editor_preview',
154 'code-snippets',
155 'code-snippets-editor'
156 );
157 }
158
159 add_action( 'admin_init', 'code_snippets_register_settings' );
160
161 /**
162 * Validate the settings
163 *
164 * @param array $input The sent settings
165 *
166 * @return array The validated settings
167 */
168 function code_snippets_settings_validate( array $input ) {
169 $settings = code_snippets_get_settings();
170 $settings_fields = code_snippets_get_settings_fields();
171
172 // Don't directly loop through $input as it does not include as deselected checkboxes
173 foreach ( $settings_fields as $section_id => $fields ) {
174
175 // Loop through fields
176 foreach ( $fields as $field_id => $field ) {
177
178 switch ( $field['type'] ) {
179
180 case 'checkbox':
181 $settings[ $section_id ][ $field_id ] =
182 isset( $input[ $section_id ][ $field_id ] ) && 'on' === $input[ $section_id ][ $field_id ];
183 break;
184
185 case 'number':
186 $settings[ $section_id ][ $field_id ] = absint( $input[ $section_id ][ $field_id ] );
187 break;
188
189 case 'codemirror_theme_select':
190 $available_themes = code_snippets_get_available_themes();
191 $selected_theme = $input[ $section_id ][ $field_id ];
192
193 if ( in_array( $selected_theme, $available_themes, true ) ) {
194 $settings[ $section_id ][ $field_id ] = $selected_theme;
195 }
196
197 break;
198
199 default:
200 break;
201
202 }
203 }
204 }
205
206 /* Add an updated message */
207 add_settings_error(
208 'code-snippets-settings-notices',
209 'settings-saved',
210 __( 'Settings saved.', 'code-snippets' ),
211 'updated'
212 );
213
214 return $settings;
215 }
216