| 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 |
* @version 1.6.1 |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Checkbox sanitization callback example. |
| 13 |
* |
| 14 |
* Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked` |
| 15 |
* as a boolean value, either TRUE or FALSE. |
| 16 |
* |
| 17 |
* @param bool $checked Whether the checkbox is checked. |
| 18 |
* @return bool Whether the checkbox is checked. |
| 19 |
*/ |
| 20 |
function loginpress_sanitize_checkbox( $checked ) { |
| 21 |
|
| 22 |
// Boolean check. |
| 23 |
return ( ( isset( $checked ) && true == $checked ) ? true : false ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Select sanitization callback example. |
| 28 |
* |
| 29 |
* - Sanitization: select |
| 30 |
* - Control: select, radio |
| 31 |
* |
| 32 |
* Sanitization callback for 'select' and 'radio' type controls. This callback sanitizes `$input` |
| 33 |
* as a slug, and then validates `$input` against the choices defined for the control. |
| 34 |
* |
| 35 |
* @see sanitize_key() https://developer.wordpress.org/reference/functions/sanitize_key/ |
| 36 |
* @see $wp_customize->get_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/get_control/ |
| 37 |
* |
| 38 |
* @param string $input Slug to sanitize. |
| 39 |
* @param WP_Customize_Setting $setting Setting instance. |
| 40 |
* @return string Sanitized slug if it is a valid choice; otherwise, the setting default. |
| 41 |
*/ |
| 42 |
function loginpress_sanitize_select( $input, $setting ) { |
| 43 |
|
| 44 |
// Ensure input is a slug. |
| 45 |
$input = sanitize_key( $input ); |
| 46 |
|
| 47 |
// Get list of choices from the control associated with the setting. |
| 48 |
$choices = $setting->manager->get_control( $setting->id )->choices; |
| 49 |
|
| 50 |
// If the input is a valid key, return it; otherwise, return the default. |
| 51 |
return ( array_key_exists( $input, $choices ) ? $input : $setting->default ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Image sanitization callback example. |
| 56 |
* |
| 57 |
* Checks the image's file extension and mime type against a whitelist. If they're allowed, |
| 58 |
* send back the filename, otherwise, return the setting default. |
| 59 |
* |
| 60 |
* - Sanitization: image file extension |
| 61 |
* - Control: text, WP_Customize_Image_Control |
| 62 |
* |
| 63 |
* @see wp_check_filetype() https://developer.wordpress.org/reference/functions/wp_check_filetype/ |
| 64 |
* |
| 65 |
* @param string $image Image filename. |
| 66 |
* @param WP_Customize_Setting $setting Setting instance. |
| 67 |
* @return string The image filename if the extension is allowed; otherwise, the setting default. |
| 68 |
* |
| 69 |
* @since 1.1.17 |
| 70 |
* |
| 71 |
* @version 3.0.0 |
| 72 |
*/ |
| 73 |
function loginpress_sanitize_image( $image, $setting ) { |
| 74 |
|
| 75 |
/** |
| 76 |
* Array of valid image file types. |
| 77 |
* |
| 78 |
* The array includes image mime types that are included in wp_get_mime_types() |
| 79 |
*/ |
| 80 |
$mimes = array( |
| 81 |
'jpg|jpeg|jpe' => 'image/jpeg', |
| 82 |
'gif' => 'image/gif', |
| 83 |
'png' => 'image/png', |
| 84 |
'bmp' => 'image/bmp', |
| 85 |
'tif|tiff' => 'image/tiff', |
| 86 |
'ico' => 'image/x-icon', |
| 87 |
); |
| 88 |
|
| 89 |
// Allowed svg mime type in version 1.2.2 |
| 90 |
$allowed_mime = get_allowed_mime_types(); |
| 91 |
|
| 92 |
/** |
| 93 |
* Filter the list of mime types that are allowed for uploads. |
| 94 |
* |
| 95 |
* @since 1.6.1 |
| 96 |
*/ |
| 97 |
$extra_mimes = array( |
| 98 |
'svg' => 'image/svg+xml', // Allowed svg mime type in version 1.2.2 |
| 99 |
'webp' => 'image/webp', // Allowed webp mime type in version 1.6.1 |
| 100 |
); |
| 101 |
|
| 102 |
foreach ( $extra_mimes as $key => $value ) { |
| 103 |
$mime_check = isset( $allowed_mime[ $key ] ) ? true : false; |
| 104 |
if ( $mime_check ) { |
| 105 |
$allow_mime = array( $key => $value ); |
| 106 |
$mimes = array_merge( $mimes, $allow_mime ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
$file_type = false; |
| 111 |
|
| 112 |
/** |
| 113 |
* Return an array with file extension and mime_type. |
| 114 |
* |
| 115 |
* @since 3.0.0 |
| 116 |
* @version 3.0.3 |
| 117 |
*/ |
| 118 |
if ( ! empty( $image ) ) { |
| 119 |
|
| 120 |
// Return an array with file extension and mime_type. |
| 121 |
$file = wp_check_filetype( $image, $mimes ); |
| 122 |
|
| 123 |
// If $image has a valid mime_type, return it; otherwise, return the default. |
| 124 |
return ( $file['ext'] ? $image : loginpress_image_content_type( $image, $mimes, $setting ) ); |
| 125 |
} |
| 126 |
|
| 127 |
return $setting->default; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* If CDN is being used get sanitization option. |
| 132 |
* |
| 133 |
* @param string $image The image URL. |
| 134 |
* @param array $mimes The mime type allowed. |
| 135 |
* @param object $setting The settings object. |
| 136 |
* |
| 137 |
* @version 3.0.2 |
| 138 |
* @return mixed The images based on content type. |
| 139 |
*/ |
| 140 |
function loginpress_image_content_type( $image, $mimes, $setting ) { |
| 141 |
$headers = get_headers( $image, 1 ); |
| 142 |
$content_type = false; |
| 143 |
$file_type = false; |
| 144 |
$content_types_can = ['Content-Type', 'content-type']; |
| 145 |
foreach ( $content_types_can as $type ) { |
| 146 |
if ( isset( $headers[$type] ) && !empty( $headers[$type] ) ) { |
| 147 |
$content_type = $headers[$type]; |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( $content_type ) { |
| 153 |
|
| 154 |
$file_type = $content_type ? in_array( $content_type, $mimes ) : false; |
| 155 |
|
| 156 |
if ( is_array( $content_type ) ) { |
| 157 |
foreach ( $content_type as $type ) { |
| 158 |
$file_type = $type ? in_array( $type, $mimes ) : false; |
| 159 |
if ( $file_type ) { |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// If $image has a valid mime_type, return it; otherwise, return the default. |
| 167 |
return ( $file_type ? $image : $setting->default ); |
| 168 |
} |
| 169 |
|