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