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