deprecated
1 month ago
Aliases.php
2 months ago
Config.php
5 months ago
Control.php
5 months ago
Deprecated.php
5 months ago
Field.php
2 months ago
Framework.php
5 months ago
Init.php
2 months ago
Kirki.php
2 months ago
Modules.php
2 months ago
Pro_Namespace_Compatibility.php
5 months ago
Sanitize_Values.php
5 months ago
Scripts.php
2 months ago
Settings.php
5 months ago
Values.php
5 months ago
Sanitize_Values.php
210 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Additional sanitization methods for controls. |
| 4 | * These are used in the field's 'sanitize_callback' argument. |
| 5 | * |
| 6 | * @package Kirki |
| 7 | * @category Core |
| 8 | * @author Themeum |
| 9 | * @copyright Copyright (c) 2023, Themeum |
| 10 | * @license https://opensource.org/licenses/MIT |
| 11 | * @since 1.0 |
| 12 | */ |
| 13 | |
| 14 | namespace Kirki\Compatibility; |
| 15 | |
| 16 | use Kirki\Field\Checkbox; |
| 17 | |
| 18 | // Exit if accessed directly. |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * A simple wrapper class for static methods. |
| 25 | */ |
| 26 | class Sanitize_Values { |
| 27 | |
| 28 | /** |
| 29 | * Checkbox sanitization callback. |
| 30 | * |
| 31 | * Sanitization callback for 'checkbox' type controls. |
| 32 | * This callback sanitizes `$value` as a boolean value, either TRUE or FALSE. |
| 33 | * |
| 34 | * Deprecated. Use \Kirki\Field\Checkbox::sanitize() instead. |
| 35 | * |
| 36 | * @static |
| 37 | * @access public |
| 38 | * @see \Kirki\Field\Checkbox::sanitize() |
| 39 | * @param bool|string $value Whether the checkbox is checked. |
| 40 | * @return bool Whether the checkbox is checked. |
| 41 | */ |
| 42 | public static function checkbox( $value ) { |
| 43 | $obj = new Checkbox(); |
| 44 | |
| 45 | // ! This sanitize function doesn't exist. A method exists check should be used before actually calling it. |
| 46 | return (bool) $obj->sanitize( $value ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Sanitize number options. |
| 51 | * |
| 52 | * @static |
| 53 | * @access public |
| 54 | * @since 0.5 |
| 55 | * @param int|float|double|string $value The value to be sanitized. |
| 56 | * @return integer|double|string |
| 57 | */ |
| 58 | public static function number( $value ) { |
| 59 | return ( is_numeric( $value ) ) ? $value : intval( $value ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Drop-down Pages sanitization callback. |
| 64 | * |
| 65 | * - Sanitization: dropdown-pages |
| 66 | * - Control: dropdown-pages |
| 67 | * |
| 68 | * Sanitization callback for 'dropdown-pages' type controls. This callback sanitizes `$page_id` |
| 69 | * as an absolute integer, and then validates that $input is the ID of a published page. |
| 70 | * |
| 71 | * @see absint() https://developer.wordpress.org/reference/functions/absint/ |
| 72 | * @see get_post_status() https://developer.wordpress.org/reference/functions/get_post_status/ |
| 73 | * |
| 74 | * @param int $page_id Page ID. |
| 75 | * @param WP_Customize_Setting $setting Setting instance. |
| 76 | * @return int|string Page ID if the page is published; otherwise, the setting default. |
| 77 | */ |
| 78 | public static function dropdown_pages( $page_id, $setting ) { |
| 79 | |
| 80 | // Ensure $input is an absolute integer. |
| 81 | $page_id = absint( $page_id ); |
| 82 | |
| 83 | // If $page_id is an ID of a published page, return it; otherwise, return the default. |
| 84 | return ( 'publish' === get_post_status( $page_id ) ? $page_id : $setting->default ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sanitizes css dimensions. |
| 89 | * |
| 90 | * @static |
| 91 | * @access public |
| 92 | * @since 2.2.0 |
| 93 | * @param string $value The value to be sanitized. |
| 94 | * @return string |
| 95 | */ |
| 96 | public static function css_dimension( $value ) { |
| 97 | |
| 98 | // Trim it. |
| 99 | $value = trim( $value ); |
| 100 | |
| 101 | // If the value is round, then return 50%. |
| 102 | if ( 'round' === $value ) { |
| 103 | $value = '50%'; |
| 104 | } |
| 105 | |
| 106 | // If the value is empty, return empty. |
| 107 | if ( '' === $value ) { |
| 108 | return ''; |
| 109 | } |
| 110 | |
| 111 | // If auto, inherit or initial, return the value. |
| 112 | if ( 'auto' === $value || 'initial' === $value || 'inherit' === $value || 'normal' === $value ) { |
| 113 | return $value; |
| 114 | } |
| 115 | |
| 116 | // Return empty if there are no numbers in the value. |
| 117 | if ( ! preg_match( '#[0-9]#', $value ) ) { |
| 118 | return ''; |
| 119 | } |
| 120 | |
| 121 | // If we're using calc() then return the value. |
| 122 | if ( false !== strpos( $value, 'calc(' ) ) { |
| 123 | return $value; |
| 124 | } |
| 125 | |
| 126 | // The raw value without the units. |
| 127 | $raw_value = self::filter_number( $value ); |
| 128 | $unit_used = ''; |
| 129 | |
| 130 | // An array of all valid CSS units. Their order was carefully chosen for this evaluation, don't mix it up!!! |
| 131 | $units = [ 'fr', 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' ]; |
| 132 | foreach ( $units as $unit ) { |
| 133 | if ( false !== strpos( $value, $unit ) ) { |
| 134 | $unit_used = $unit; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Hack for rem values. |
| 139 | if ( 'em' === $unit_used && false !== strpos( $value, 'rem' ) ) { |
| 140 | $unit_used = 'rem'; |
| 141 | } |
| 142 | |
| 143 | return $raw_value . $unit_used; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Filters numeric values. |
| 148 | * |
| 149 | * @static |
| 150 | * @access public |
| 151 | * @param string $value The value to be sanitized. |
| 152 | * @return int|float |
| 153 | */ |
| 154 | public static function filter_number( $value ) { |
| 155 | return filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sanitize RGBA colors |
| 160 | * |
| 161 | * @static |
| 162 | * @since 0.8.5 |
| 163 | * @param string $value The value to be sanitized. |
| 164 | * @return string |
| 165 | */ |
| 166 | public static function rgba( $value ) { |
| 167 | $color = \ariColor::newColor( $value ); |
| 168 | return $color->toCSS( 'rgba' ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Sanitize colors. |
| 173 | * |
| 174 | * @static |
| 175 | * @since 0.8.5 |
| 176 | * @param string $value The value to be sanitized. |
| 177 | * @return string |
| 178 | */ |
| 179 | public static function color( $value ) { |
| 180 | |
| 181 | // If the value is empty, then return empty. |
| 182 | if ( '' === $value ) { |
| 183 | return ''; |
| 184 | } |
| 185 | |
| 186 | // If transparent, then return 'transparent'. |
| 187 | if ( is_string( $value ) && 'transparent' === trim( $value ) ) { |
| 188 | return 'transparent'; |
| 189 | } |
| 190 | |
| 191 | // Instantiate the object. |
| 192 | $color = \ariColor::newColor( $value ); |
| 193 | |
| 194 | // Return a CSS value, using the auto-detected mode. |
| 195 | return $color->toCSS( $color->mode ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * DOES NOT SANITIZE ANYTHING. |
| 200 | * |
| 201 | * @static |
| 202 | * @since 0.5 |
| 203 | * @param int|string|array $value The value to be sanitized. |
| 204 | * @return int|string|array |
| 205 | */ |
| 206 | public static function unfiltered( $value ) { |
| 207 | return $value; |
| 208 | } |
| 209 | } |
| 210 |