| 1 |
<?php |
| 2 |
/** |
| 3 |
* Customizer: Sanitization Callbacks |
| 4 |
* |
| 5 |
* This file demonstrates how to define sanitization callback functions for various data types. |
| 6 |
* @since 1.1.16 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Checkbox sanitization callback example. |
| 11 |
* |
| 12 |
* Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked` |
| 13 |
* as a boolean value, either TRUE or FALSE. |
| 14 |
* |
| 15 |
* @param bool $checked Whether the checkbox is checked. |
| 16 |
* @return bool Whether the checkbox is checked. |
| 17 |
*/ |
| 18 |
function loginpress_sanitize_checkbox( $checked ) { |
| 19 |
// Boolean check. |
| 20 |
return ( ( isset( $checked ) && true == $checked ) ? true : false ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Select sanitization callback example. |
| 25 |
* |
| 26 |
* - Sanitization: select |
| 27 |
* - Control: select, radio |
| 28 |
* |
| 29 |
* Sanitization callback for 'select' and 'radio' type controls. This callback sanitizes `$input` |
| 30 |
* as a slug, and then validates `$input` against the choices defined for the control. |
| 31 |
* |
| 32 |
* @see sanitize_key() https://developer.wordpress.org/reference/functions/sanitize_key/ |
| 33 |
* @see $wp_customize->get_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/get_control/ |
| 34 |
* |
| 35 |
* @param string $input Slug to sanitize. |
| 36 |
* @param WP_Customize_Setting $setting Setting instance. |
| 37 |
* @return string Sanitized slug if it is a valid choice; otherwise, the setting default. |
| 38 |
*/ |
| 39 |
function loginpress_sanitize_select( $input, $setting ) { |
| 40 |
|
| 41 |
// Ensure input is a slug. |
| 42 |
$input = sanitize_key( $input ); |
| 43 |
|
| 44 |
// Get list of choices from the control associated with the setting. |
| 45 |
$choices = $setting->manager->get_control( $setting->id )->choices; |
| 46 |
|
| 47 |
// If the input is a valid key, return it; otherwise, return the default. |
| 48 |
return ( array_key_exists( $input, $choices ) ? $input : $setting->default ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Image sanitization callback example. |
| 53 |
* |
| 54 |
* Checks the image's file extension and mime type against a whitelist. If they're allowed, |
| 55 |
* send back the filename, otherwise, return the setting default. |
| 56 |
* |
| 57 |
* - Sanitization: image file extension |
| 58 |
* - Control: text, WP_Customize_Image_Control |
| 59 |
* |
| 60 |
* @see wp_check_filetype() https://developer.wordpress.org/reference/functions/wp_check_filetype/ |
| 61 |
* |
| 62 |
* @param string $image Image filename. |
| 63 |
* @param WP_Customize_Setting $setting Setting instance. |
| 64 |
* @return string The image filename if the extension is allowed; otherwise, the setting default. |
| 65 |
* @version 1.2.2 |
| 66 |
*/ |
| 67 |
function loginpress_sanitize_image( $image, $setting ) { |
| 68 |
|
| 69 |
/* |
| 70 |
* Array of valid image file types. |
| 71 |
* |
| 72 |
* The array includes image mime types that are included in wp_get_mime_types() |
| 73 |
*/ |
| 74 |
$mimes = array( |
| 75 |
'jpg|jpeg|jpe' => 'image/jpeg', |
| 76 |
'gif' => 'image/gif', |
| 77 |
'png' => 'image/png', |
| 78 |
'bmp' => 'image/bmp', |
| 79 |
'tif|tiff' => 'image/tiff', |
| 80 |
'ico' => 'image/x-icon' |
| 81 |
); |
| 82 |
|
| 83 |
// Allowed svg mime type in version 1.2.2 |
| 84 |
$allowed_mime = get_allowed_mime_types(); |
| 85 |
$svg_mime_check = isset( $allowed_mime['svg'] ) ? true : false; |
| 86 |
|
| 87 |
if ( $svg_mime_check ) { |
| 88 |
$allow_mime = array( 'svg' => 'image/svg+xml' ); |
| 89 |
$mimes = array_merge( $mimes, $allow_mime ); |
| 90 |
} |
| 91 |
|
| 92 |
// Return an array with file extension and mime_type. |
| 93 |
$file = wp_check_filetype( $image, $mimes ); |
| 94 |
// If $image has a valid mime_type, return it; otherwise, return the default. |
| 95 |
return ( $file['ext'] ? $image : $setting->default ); |
| 96 |
} |
| 97 |
|