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