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