| 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 |
use Code_Snippets\Welcome_API; |
| 12 |
use function Code_Snippets\clean_snippets_cache; |
| 13 |
use function Code_Snippets\code_snippets; |
| 14 |
use const Code_Snippets\CACHE_GROUP; |
| 15 |
|
| 16 |
const CACHE_KEY = 'code_snippets_settings'; |
| 17 |
const OPTION_GROUP = 'code-snippets'; |
| 18 |
const OPTION_NAME = 'code_snippets_settings'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Add a new option for either the current site or the current network |
| 22 |
* |
| 23 |
* @param bool $network Whether to add a network-wide option. |
| 24 |
* @param string $option Name of option to add. Expected to not be SQL-escaped. |
| 25 |
* @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. |
| 26 |
* |
| 27 |
* @return bool False if the option was not added. True if the option was added. |
| 28 |
*/ |
| 29 |
function add_self_option( bool $network, string $option, $value ): bool { |
| 30 |
return $network ? add_site_option( $option, $value ) : add_option( $option, $value ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Retrieves an option value based on an option name from either the current site or the current network |
| 35 |
* |
| 36 |
* @param bool $network Whether to get a network-wide option. |
| 37 |
* @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
| 38 |
* @param mixed $default_value Optional value to return if option doesn't exist. Default false. |
| 39 |
* |
| 40 |
* @return mixed Value set for the option. |
| 41 |
*/ |
| 42 |
function get_self_option( bool $network, string $option, $default_value = false ) { |
| 43 |
return $network ? get_site_option( $option, $default_value ) : get_option( $option, $default_value ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Update the value of an option that was already added on the current site or the current network |
| 48 |
* |
| 49 |
* @param bool $network Whether to update a network-wide option. |
| 50 |
* @param string $option Name of option. Expected to not be SQL-escaped. |
| 51 |
* @param mixed $value Option value. Expected to not be SQL-escaped. |
| 52 |
* |
| 53 |
* @return bool False if value was not updated. True if value was updated. |
| 54 |
*/ |
| 55 |
function update_self_option( bool $network, string $option, $value ): bool { |
| 56 |
return $network ? update_site_option( $option, $value ) : update_option( $option, $value ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns 'true' if plugin settings are unified on a multisite installation |
| 61 |
* under the Network Admin settings menu |
| 62 |
* |
| 63 |
* This option is controlled by the "Enable administration menus" setting on the Network Settings menu |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
function are_settings_unified(): bool { |
| 68 |
if ( ! is_multisite() ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
$menu_perms = get_site_option( 'menu_items', array() ); |
| 73 |
return empty( $menu_perms['snippets_settings'] ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Retrieve the setting values from the database. |
| 78 |
* If a setting does not exist in the database, the default value will be returned. |
| 79 |
* |
| 80 |
* @return array<string, array<string, mixed>> |
| 81 |
*/ |
| 82 |
function get_settings_values(): array { |
| 83 |
$settings = wp_cache_get( CACHE_KEY, CACHE_GROUP ); |
| 84 |
if ( $settings ) { |
| 85 |
return $settings; |
| 86 |
} |
| 87 |
|
| 88 |
$settings = get_default_settings(); |
| 89 |
$saved = get_self_option( are_settings_unified(), OPTION_NAME, array() ); |
| 90 |
|
| 91 |
foreach ( $settings as $section => $fields ) { |
| 92 |
if ( isset( $saved[ $section ] ) ) { |
| 93 |
$settings[ $section ] = array_replace( $fields, $saved[ $section ] ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
wp_cache_set( CACHE_KEY, $settings, CACHE_GROUP ); |
| 98 |
return $settings; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Retrieve an individual setting field value |
| 103 |
* |
| 104 |
* @param string $section ID of the section the setting belongs to. |
| 105 |
* @param string $field ID of the setting field. |
| 106 |
* |
| 107 |
* @return mixed |
| 108 |
*/ |
| 109 |
function get_setting( string $section, string $field ) { |
| 110 |
$settings = get_settings_values(); |
| 111 |
|
| 112 |
return $settings[ $section ][ $field ] ?? null; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Update a single setting to a new value. |
| 117 |
* |
| 118 |
* @param string $section ID of the section the setting belongs to. |
| 119 |
* @param string $field ID of the setting field. |
| 120 |
* @param mixed $new_value Setting value. Expected to not be SQL-escaped. |
| 121 |
* |
| 122 |
* @return bool False if value was not updated. True if value was updated. |
| 123 |
*/ |
| 124 |
function update_setting( string $section, string $field, $new_value ): bool { |
| 125 |
$settings = get_settings_values(); |
| 126 |
|
| 127 |
$settings[ $section ][ $field ] = $new_value; |
| 128 |
|
| 129 |
wp_cache_set( CACHE_KEY, $settings, CACHE_GROUP ); |
| 130 |
return update_self_option( are_settings_unified(), OPTION_NAME, $settings ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Retrieve the settings sections |
| 135 |
* |
| 136 |
* @return array<string, string> Settings sections. |
| 137 |
*/ |
| 138 |
function get_settings_sections(): array { |
| 139 |
$sections = array( |
| 140 |
'general' => __( 'General', 'code-snippets' ), |
| 141 |
'editor' => __( 'Code Editor', 'code-snippets' ), |
| 142 |
'debug' => __( 'Debug', 'code-snippets' ), |
| 143 |
); |
| 144 |
|
| 145 |
// Only show the Version section when the debug setting to enable version changes is enabled. |
| 146 |
$enable_version = get_setting( 'debug', 'enable_version_change' ); |
| 147 |
if ( $enable_version ) { |
| 148 |
$sections['version-switch'] = __( 'Version', 'code-snippets' ); |
| 149 |
} |
| 150 |
|
| 151 |
return apply_filters( 'code_snippets_settings_sections', $sections ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Register settings sections, fields, etc |
| 156 |
*/ |
| 157 |
function register_plugin_settings() { |
| 158 |
if ( are_settings_unified() ) { |
| 159 |
if ( ! get_site_option( OPTION_NAME ) ) { |
| 160 |
add_site_option( OPTION_NAME, get_default_settings() ); |
| 161 |
} |
| 162 |
} elseif ( ! get_option( OPTION_NAME ) ) { |
| 163 |
add_option( OPTION_NAME, get_default_settings() ); |
| 164 |
} |
| 165 |
|
| 166 |
// Register the setting. |
| 167 |
register_setting( |
| 168 |
OPTION_GROUP, |
| 169 |
OPTION_NAME, |
| 170 |
[ 'sanitize_callback' => __NAMESPACE__ . '\\sanitize_settings' ] |
| 171 |
); |
| 172 |
|
| 173 |
// Register settings sections. |
| 174 |
foreach ( get_settings_sections() as $section_id => $section_name ) { |
| 175 |
add_settings_section( $section_id, $section_name, '__return_empty_string', 'code-snippets' ); |
| 176 |
} |
| 177 |
|
| 178 |
// Register settings fields. Only register fields for sections that exist (some sections may be gated by settings). |
| 179 |
$registered_sections = get_settings_sections(); |
| 180 |
foreach ( get_settings_fields() as $section_id => $fields ) { |
| 181 |
if ( ! isset( $registered_sections[ $section_id ] ) ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
|
| 185 |
foreach ( $fields as $field_id => $field ) { |
| 186 |
$field_object = new Setting_Field( $section_id, $field_id, $field ); |
| 187 |
add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// Add editor preview as a field. |
| 192 |
add_settings_field( |
| 193 |
'editor_preview', |
| 194 |
__( 'Editor Preview', 'code-snippets' ), |
| 195 |
__NAMESPACE__ . '\\render_editor_preview', |
| 196 |
'code-snippets', |
| 197 |
'editor' |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
add_action( 'admin_init', __NAMESPACE__ . '\\register_plugin_settings' ); |
| 202 |
|
| 203 |
/** |
| 204 |
* Sanitize a single setting value. |
| 205 |
* |
| 206 |
* @param array<string, mixed> $field Setting field information. |
| 207 |
* @param mixed $input_value User input setting value, or null if missing. |
| 208 |
* |
| 209 |
* @return mixed Sanitized setting value, or null if unset. |
| 210 |
*/ |
| 211 |
function sanitize_setting_value( array $field, $input_value ) { |
| 212 |
switch ( $field['type'] ) { |
| 213 |
|
| 214 |
case 'checkbox': |
| 215 |
return 'on' === $input_value; |
| 216 |
|
| 217 |
case 'number': |
| 218 |
return intval( $input_value ); |
| 219 |
|
| 220 |
case 'select': |
| 221 |
$select_options = array_map( 'strval', array_keys( $field['options'] ) ); |
| 222 |
return in_array( strval( $input_value ), $select_options, true ) ? $input_value : null; |
| 223 |
|
| 224 |
case 'checkboxes': |
| 225 |
$results = []; |
| 226 |
|
| 227 |
if ( ! empty( $input_value ) ) { |
| 228 |
foreach ( $field['options'] as $option_id => $option_label ) { |
| 229 |
if ( isset( $input_value[ $option_id ] ) && 'on' === $input_value[ $option_id ] ) { |
| 230 |
$results[] = $option_id; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
return $results; |
| 236 |
|
| 237 |
case 'text': |
| 238 |
case 'hidden': |
| 239 |
return trim( sanitize_text_field( $input_value ) ); |
| 240 |
|
| 241 |
case 'callback': |
| 242 |
return isset( $field['sanitize_callback'] ) && is_callable( $field['sanitize_callback'] ) ? |
| 243 |
call_user_func( $field['sanitize_callback'], $input_value ) : |
| 244 |
null; |
| 245 |
|
| 246 |
default: |
| 247 |
return null; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Process settings actions. |
| 253 |
* |
| 254 |
* @param array $input Provided settings input. |
| 255 |
* |
| 256 |
* @return array|null New $input value to return, or null to continue with settings update process. |
| 257 |
*/ |
| 258 |
function process_settings_actions( array $input ): ?array { |
| 259 |
|
| 260 |
if ( isset( $input['reset_settings'] ) ) { |
| 261 |
add_settings_error( |
| 262 |
OPTION_NAME, |
| 263 |
'settings_reset', |
| 264 |
__( 'All settings have been reset to their defaults.', 'code-snippets' ), |
| 265 |
'updated' |
| 266 |
); |
| 267 |
|
| 268 |
delete_option( 'code_snippets_cloud_settings' ); |
| 269 |
return []; |
| 270 |
} |
| 271 |
|
| 272 |
if ( isset( $input['debug']['database_update'] ) ) { |
| 273 |
code_snippets()->db->create_or_upgrade_tables(); |
| 274 |
|
| 275 |
add_settings_error( |
| 276 |
OPTION_NAME, |
| 277 |
'database_update_done', |
| 278 |
__( 'Successfully performed database table upgrade.', 'code-snippets' ), |
| 279 |
'updated' |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
if ( isset( $input['debug']['reset_caches'] ) ) { |
| 284 |
Welcome_API::clear_cache(); |
| 285 |
clean_snippets_cache( code_snippets()->db->get_table_name( false ) ); |
| 286 |
|
| 287 |
if ( is_multisite() ) { |
| 288 |
clean_snippets_cache( code_snippets()->db->get_table_name( true ) ); |
| 289 |
} |
| 290 |
|
| 291 |
add_settings_error( |
| 292 |
OPTION_NAME, |
| 293 |
'snippet_caches_reset', |
| 294 |
__( 'Successfully reset snippets caches.', 'code-snippets' ), |
| 295 |
'updated' |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
return null; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Validate the settings |
| 304 |
* |
| 305 |
* @param array<string, array<string, mixed>> $input The received settings. |
| 306 |
* |
| 307 |
* @return array<string, array<string, mixed>> The validated settings. |
| 308 |
*/ |
| 309 |
function sanitize_settings( array $input ): array { |
| 310 |
wp_cache_delete( CACHE_KEY, CACHE_GROUP ); |
| 311 |
$result = process_settings_actions( $input ); |
| 312 |
|
| 313 |
if ( ! is_null( $result ) ) { |
| 314 |
return $result; |
| 315 |
} |
| 316 |
|
| 317 |
$settings = get_settings_values(); |
| 318 |
$updated = false; |
| 319 |
|
| 320 |
// Don't directly loop through $input as it does not include as deselected checkboxes. |
| 321 |
foreach ( get_settings_fields() as $section_id => $fields ) { |
| 322 |
foreach ( $fields as $field_id => $field ) { |
| 323 |
|
| 324 |
// Fetch the corresponding input value from the posted data. |
| 325 |
$input_value = $input[ $section_id ][ $field_id ] ?? null; |
| 326 |
|
| 327 |
// Attempt to sanitize the setting value. |
| 328 |
$sanitized_value = sanitize_setting_value( $field, $input_value ); |
| 329 |
|
| 330 |
if ( ! is_null( $sanitized_value ) && $settings[ $section_id ][ $field_id ] !== $sanitized_value ) { |
| 331 |
$settings[ $section_id ][ $field_id ] = $sanitized_value; |
| 332 |
$updated = true; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
// Add an updated message. |
| 338 |
if ( $updated ) { |
| 339 |
add_settings_error( |
| 340 |
OPTION_NAME, |
| 341 |
'settings-saved', |
| 342 |
__( 'Settings saved.', 'code-snippets' ), |
| 343 |
'updated' |
| 344 |
); |
| 345 |
|
| 346 |
do_action( 'code_snippets/settings_updated', $settings, $input ); |
| 347 |
} |
| 348 |
|
| 349 |
return $settings; |
| 350 |
} |
| 351 |
|