PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class-kirki-sanitize-values.php

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