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

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