| 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( bool $network, string $option, $value ): bool { |
| 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( bool $network, string $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( bool $network, string $option, $value ): bool { |
| 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(): bool { |
| 63 |
if ( ! is_multisite() ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$menu_perms = get_site_option( 'menu_items', array() ); |
| 68 |
return empty( $menu_perms['snippets_settings'] ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Retrieve the setting values from the database. |
| 73 |
* If a setting does not exist in the database, the default value will be returned. |
| 74 |
* |
| 75 |
* @return array<string, array<string, mixed>> |
| 76 |
*/ |
| 77 |
function get_settings_values(): array { |
| 78 |
$settings = wp_cache_get( CACHE_KEY ); |
| 79 |
if ( $settings ) { |
| 80 |
return $settings; |
| 81 |
} |
| 82 |
|
| 83 |
$settings = get_default_settings(); |
| 84 |
$saved = get_self_option( are_settings_unified(), 'code_snippets_settings', array() ); |
| 85 |
|
| 86 |
foreach ( $settings as $section => $fields ) { |
| 87 |
if ( isset( $saved[ $section ] ) ) { |
| 88 |
$settings[ $section ] = array_replace( $fields, $saved[ $section ] ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
wp_cache_set( CACHE_KEY, $settings ); |
| 93 |
return $settings; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Retrieve an individual setting field value |
| 98 |
* |
| 99 |
* @param string $section ID of the section the setting belongs to. |
| 100 |
* @param string $field ID of the setting field. |
| 101 |
* |
| 102 |
* @return mixed |
| 103 |
*/ |
| 104 |
function get_setting( string $section, string $field ) { |
| 105 |
$settings = get_settings_values(); |
| 106 |
|
| 107 |
return $settings[ $section ][ $field ]; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Update a single setting to a new value. |
| 112 |
* |
| 113 |
* @param string $section ID of the section the setting belongs to. |
| 114 |
* @param string $field ID of the setting field. |
| 115 |
* @param mixed $new_value Setting value. Expected to not be SQL-escaped. |
| 116 |
* |
| 117 |
* @return bool False if value was not updated. True if value was updated. |
| 118 |
*/ |
| 119 |
function update_setting( string $section, string $field, $new_value ): bool { |
| 120 |
$settings = get_settings_values(); |
| 121 |
|
| 122 |
$settings[ $section ][ $field ] = $new_value; |
| 123 |
|
| 124 |
wp_cache_set( CACHE_KEY, $settings ); |
| 125 |
return update_self_option( are_settings_unified(), 'code_snippets_settings', $settings ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Retrieve the settings sections |
| 130 |
* |
| 131 |
* @return array<string, string> Settings sections. |
| 132 |
*/ |
| 133 |
function get_settings_sections(): array { |
| 134 |
$sections = array( |
| 135 |
'general' => __( 'General', 'code-snippets' ), |
| 136 |
'editor' => __( 'Code Editor', 'code-snippets' ), |
| 137 |
); |
| 138 |
|
| 139 |
return apply_filters( 'code_snippets_settings_sections', $sections ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register settings sections, fields, etc |
| 144 |
*/ |
| 145 |
function register_plugin_settings() { |
| 146 |
|
| 147 |
if ( are_settings_unified() ) { |
| 148 |
if ( ! get_site_option( 'code_snippets_settings' ) ) { |
| 149 |
add_site_option( 'code_snippets_settings', get_default_settings() ); |
| 150 |
} |
| 151 |
} elseif ( ! get_option( 'code_snippets_settings' ) ) { |
| 152 |
add_option( 'code_snippets_settings', get_default_settings() ); |
| 153 |
} |
| 154 |
|
| 155 |
// Register the setting. |
| 156 |
register_setting( |
| 157 |
'code-snippets', |
| 158 |
'code_snippets_settings', |
| 159 |
array( 'sanitize_callback' => NS . 'sanitize_settings' ) |
| 160 |
); |
| 161 |
|
| 162 |
// Register settings sections. |
| 163 |
foreach ( get_settings_sections() as $section_id => $section_name ) { |
| 164 |
add_settings_section( $section_id, $section_name, '__return_empty_string', 'code-snippets' ); |
| 165 |
} |
| 166 |
|
| 167 |
// Register settings fields. |
| 168 |
foreach ( get_settings_fields() as $section_id => $fields ) { |
| 169 |
foreach ( $fields as $field_id => $field ) { |
| 170 |
$field_object = new Setting_Field( $section_id, $field_id, $field ); |
| 171 |
add_settings_field( $field_id, $field['name'], [ $field_object, 'render' ], 'code-snippets', $section_id ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
// Add editor preview as a field. |
| 176 |
add_settings_field( |
| 177 |
'editor_preview', |
| 178 |
__( 'Editor Preview', 'code-snippets' ), |
| 179 |
NS . 'render_editor_preview', |
| 180 |
'code-snippets', |
| 181 |
'editor' |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
add_action( 'admin_init', NS . 'register_plugin_settings' ); |
| 186 |
|
| 187 |
/** |
| 188 |
* Sanitize a single setting value. |
| 189 |
* |
| 190 |
* @param array<string, mixed> $field Setting field information. |
| 191 |
* @param mixed $input_value User input setting value, or null if missing. |
| 192 |
* |
| 193 |
* @return mixed Sanitized setting value, or null if unset. |
| 194 |
*/ |
| 195 |
function sanitize_setting_value( array $field, $input_value ) { |
| 196 |
switch ( $field['type'] ) { |
| 197 |
|
| 198 |
case 'checkbox': |
| 199 |
return 'on' === $input_value; |
| 200 |
|
| 201 |
case 'number': |
| 202 |
return absint( $input_value ); |
| 203 |
|
| 204 |
case 'select': |
| 205 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 206 |
return in_array( $input_value, array_keys( $field['options'] ) ) ? $input_value : null; |
| 207 |
|
| 208 |
case 'checkboxes': |
| 209 |
$results = []; |
| 210 |
|
| 211 |
if ( ! empty( $input_value ) ) { |
| 212 |
foreach ( $field['options'] as $option_id => $option_label ) { |
| 213 |
if ( isset( $input_value[ $option_id ] ) && 'on' === $input_value[ $option_id ] ) { |
| 214 |
$results[] = $option_id; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return $results; |
| 220 |
|
| 221 |
case 'text': |
| 222 |
return trim( sanitize_text_field( $input_value ) ); |
| 223 |
|
| 224 |
case 'callback': |
| 225 |
return array_key_exists( 'sanitize_callback', $field ) && is_callable( $field['sanitize_callback'] ) ? |
| 226 |
call_user_func( $field['sanitize_callback'], $input_value ) : |
| 227 |
null; |
| 228 |
|
| 229 |
default: |
| 230 |
return null; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Validate the settings |
| 236 |
* |
| 237 |
* @param array<string, array<string, mixed>> $input The received settings. |
| 238 |
* |
| 239 |
* @return array<string, array<string, mixed>> The validated settings. |
| 240 |
*/ |
| 241 |
function sanitize_settings( array $input ): array { |
| 242 |
$settings = get_settings_values(); |
| 243 |
$updated = false; |
| 244 |
|
| 245 |
// Don't directly loop through $input as it does not include as deselected checkboxes. |
| 246 |
foreach ( get_settings_fields() as $section_id => $fields ) { |
| 247 |
foreach ( $fields as $field_id => $field ) { |
| 248 |
|
| 249 |
// Fetch the corresponding input value from the posted data. |
| 250 |
$input_value = $input[ $section_id ][ $field_id ] ?? null; |
| 251 |
|
| 252 |
// Attempt to sanitize the setting value. |
| 253 |
$sanitized_value = sanitize_setting_value( $field, $input_value ); |
| 254 |
|
| 255 |
if ( ! is_null( $sanitized_value ) && $settings[ $section_id ][ $field_id ] !== $sanitized_value ) { |
| 256 |
$settings[ $section_id ][ $field_id ] = $sanitized_value; |
| 257 |
$updated = true; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
wp_cache_delete( CACHE_KEY ); |
| 263 |
|
| 264 |
// Add an updated message. |
| 265 |
if ( $updated ) { |
| 266 |
add_settings_error( |
| 267 |
'code-snippets-settings-notices', |
| 268 |
'settings-saved', |
| 269 |
__( 'Settings saved.', 'code-snippets' ), |
| 270 |
'updated' |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
return $settings; |
| 275 |
} |
| 276 |
|