sanitize.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Sanitization functions.
  4. *
  5. * @package Pen
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. if ( ! function_exists( 'pen_sanitize_integer' ) ) {
  11. /**
  12. * Sanitizes numbers.
  13. *
  14. * @param string $input The input integer.
  15. *
  16. * @since Pen 1.0.0
  17. * @return integer
  18. */
  19. function pen_sanitize_integer( $input ) {
  20. return (int) $input;
  21. }
  22. }
  23. if ( ! function_exists( 'pen_sanitize_string' ) ) {
  24. /**
  25. * Sanitizes strings.
  26. *
  27. * @param string $input The input string.
  28. *
  29. * @since Pen 1.0.0
  30. * @return string
  31. */
  32. function pen_sanitize_string( $input ) {
  33. return wp_kses( $input, wp_kses_allowed_html( 'post' ) );
  34. }
  35. }
  36. if ( ! function_exists( 'pen_sanitize_url' ) ) {
  37. /**
  38. * Sanitizes URLs.
  39. *
  40. * @param string $input The input URL.
  41. *
  42. * @since Pen 1.0.0
  43. * @return string
  44. */
  45. function pen_sanitize_url( $input ) {
  46. return esc_url( $input );
  47. }
  48. }
  49. if ( ! function_exists( 'pen_sanitize_boolean' ) ) {
  50. /**
  51. * Sanitizes booleans.
  52. *
  53. * @param boolean $input The input boolean.
  54. *
  55. * @since Pen 1.0.0
  56. * @return string
  57. */
  58. function pen_sanitize_boolean( $input ) {
  59. if ( is_bool( $input ) || in_array( (int) $input, array( 0, 1 ), true ) ) {
  60. return $input;
  61. }
  62. return false;
  63. }
  64. }
  65. if ( ! function_exists( 'pen_sanitize_color' ) ) {
  66. /**
  67. * Sanitizes HEX and RGBA colors.
  68. *
  69. * @param string $input The color code.
  70. *
  71. * @since Pen 1.0.0
  72. * @return string
  73. */
  74. function pen_sanitize_color( $input ) {
  75. $input = str_replace( array( '#', ' ' ), '', strtolower( trim( $input ) ) );
  76. if ( 3 === strlen( $input ) ) {
  77. $input = $input . $input;
  78. } elseif ( false !== strpos( $input, 'rgb(' ) ) {
  79. $input = str_ireplace( array( 'rgb(', ')' ), array( 'rgba(', ',1)' ), $input );
  80. }
  81. if ( false !== strpos( $input, 'rgba(' ) && preg_match( '/\A^rgba\(([0]*[0-9]{1,2}|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\s*,\s*([0]*[0-9]{1,2}|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\s*,\s*([0]*[0-9]{1,2}|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])\s*,\s*([0-9]*\.?[0-9]+)\)$\z/im', $input ) ) {
  82. return $input;
  83. } elseif ( preg_match( '/^[a-f0-9]{6}$/i', $input ) ) {
  84. return '#' . $input;
  85. }
  86. return '#ffffff';
  87. }
  88. }
  89. if ( ! function_exists( 'pen_sanitize_logo_size' ) ) {
  90. /**
  91. * Sanitizes the logo size option.
  92. *
  93. * @param string $input The logo source.
  94. *
  95. * @since Pen 1.0.0
  96. * @return string
  97. */
  98. function pen_sanitize_logo_size( $input ) {
  99. if ( in_array( (string) $input, array( 'none', 'height', 'width' ), true ) ) {
  100. return $input;
  101. }
  102. return 'none';
  103. }
  104. }
  105. if ( ! function_exists( 'pen_sanitize_list_effect' ) ) {
  106. /**
  107. * Sanitizes the content list effects.
  108. *
  109. * @param string $input The content list type.
  110. *
  111. * @since Pen 1.0.0
  112. * @return string
  113. */
  114. function pen_sanitize_list_effect( $input ) {
  115. if ( in_array( (string) $input, array( 'none', 'enlarge', 'fade', 'enlarge_fade' ), true ) ) {
  116. return $input;
  117. }
  118. return 'none';
  119. }
  120. }
  121. if ( ! function_exists( 'pen_sanitize_masonry_thumbnail' ) ) {
  122. /**
  123. * Sanitizes the masonry thumbnail image effect option.
  124. *
  125. * @param string $input The search box display.
  126. *
  127. * @since Pen 1.0.0
  128. * @return string
  129. */
  130. function pen_sanitize_masonry_thumbnail( $input ) {
  131. if ( in_array( (string) $input, array( 'none', 'zoom_in', 'zoom_out' ), true ) ) {
  132. return $input;
  133. }
  134. return 'none';
  135. }
  136. }
  137. if ( ! function_exists( 'pen_sanitize_masonry_columns' ) ) {
  138. /**
  139. * Sanitizes the masonry columns option.
  140. *
  141. * @param string $input The content list type.
  142. *
  143. * @since Pen 1.0.0
  144. * @return string
  145. */
  146. function pen_sanitize_masonry_columns( $input ) {
  147. if ( 'automatic' === $input || ( 1 < $input && 5 >= $input ) ) {
  148. return $input;
  149. }
  150. return 'automatic';
  151. }
  152. }
  153. if ( ! function_exists( 'pen_sanitize_thumbnail_resize' ) ) {
  154. /**
  155. * Sanitizes the post thumbnail size option.
  156. *
  157. * @param string $input The post thumbnail size.
  158. *
  159. * @since Pen 1.0.0
  160. * @return string
  161. */
  162. function pen_sanitize_thumbnail_resize( $input ) {
  163. /* phpcs:disable */
  164. if ( 'none' === $input || in_array( (string) $input, get_intermediate_image_sizes(), true ) ) {
  165. return $input;
  166. }
  167. /* phpcs:enable */
  168. return 'none';
  169. }
  170. }
  171. if ( ! function_exists( 'pen_sanitize_list_type' ) ) {
  172. /**
  173. * Sanitizes the content list types.
  174. *
  175. * @param string $input The content list type.
  176. *
  177. * @since Pen 1.0.0
  178. * @return string
  179. */
  180. function pen_sanitize_list_type( $input ) {
  181. if ( in_array( (string) $input, array( 'masonry', 'plain' ), true ) ) {
  182. return $input;
  183. }
  184. return '';
  185. }
  186. }
  187. if ( ! function_exists( 'pen_sanitize_site_width' ) ) {
  188. /**
  189. * Sanitizes the layout type.
  190. *
  191. * @param string $input The content area layout.
  192. *
  193. * @since Pen 1.0.2
  194. * @return string
  195. */
  196. function pen_sanitize_site_width( $input ) {
  197. // 'default' === 'standard' (Content Meta).
  198. if ( in_array( (string) $input, array( 'default', 'standard', 'wide', 'boxed' ), true ) ) {
  199. return $input;
  200. }
  201. return 'default';
  202. }
  203. }
  204. if ( ! function_exists( 'pen_sanitize_location' ) ) {
  205. /**
  206. * Sanitizes the element location option.
  207. *
  208. * @param string $input The content area layout.
  209. *
  210. * @since Pen 1.0.0
  211. * @return string
  212. */
  213. function pen_sanitize_location( $input ) {
  214. if ( in_array( (string) $input, array( 'header', 'content', 'footer' ), true ) ) {
  215. return $input;
  216. }
  217. return 'header';
  218. }
  219. }
  220. if ( ! function_exists( 'pen_sanitize_alignment' ) ) {
  221. /**
  222. * Sanitizes the alignment option.
  223. *
  224. * @param string $input The "alignment" option.
  225. *
  226. * @since Pen 1.0.4
  227. * @return string
  228. */
  229. function pen_sanitize_alignment( $input ) {
  230. if ( in_array( (string) $input, array( 'left', 'center', 'right' ), true ) ) {
  231. return $input;
  232. }
  233. return 'left';
  234. }
  235. }
  236. if ( ! function_exists( 'pen_sanitize_font_family' ) ) {
  237. /**
  238. * Sanitizes font families.
  239. *
  240. * @param string $input The font size.
  241. *
  242. * @since Pen 1.0.0
  243. * @return string
  244. */
  245. function pen_sanitize_font_family( $input ) {
  246. $fonts = pen_fonts_all();
  247. if ( 'default' === $input || array_key_exists( $input, $fonts ) ) {
  248. return $input;
  249. }
  250. return 'default';
  251. }
  252. }
  253. if ( ! function_exists( 'pen_sanitize_font_size' ) ) {
  254. /**
  255. * Sanitizes font sizes.
  256. *
  257. * @param string $input The font size.
  258. *
  259. * @since Pen 1.0.0
  260. * @return string
  261. */
  262. function pen_sanitize_font_size( $input ) {
  263. if ( in_array( (string) $input, array( 'default', '0.5em', '0.75em', 'normal', '2em', '3em' ), true ) ) {
  264. return $input;
  265. }
  266. return 'default';
  267. }
  268. }
  269. if ( ! function_exists( 'pen_sanitize_animation' ) ) {
  270. /**
  271. * Sanitizes animations.
  272. *
  273. * @param string $input The animation effect ID.
  274. *
  275. * @since Pen 1.0.8
  276. * @return string
  277. */
  278. function pen_sanitize_animation( $input ) {
  279. if ( in_array( (string) $input, pen_animations(), true ) ) {
  280. return $input;
  281. }
  282. return 'none';
  283. }
  284. }