/wp-content/themes/business-lite/cyberchimps/options/options-sanitize.php

https://gitlab.com/pornmongkon.p/wordpress · PHP · 481 lines · 302 code · 91 blank · 88 comment · 36 complexity · a9549180635cd3d9ea3b114d4c6d8d03 MD5 · raw file

  1. <?php
  2. /* Text */
  3. add_filter( 'cyberchimps_sanitize_text', 'sanitize_text_field' );
  4. /* Text that allows all html */
  5. function cyberchimps_sanitize_text_html( $input ) {
  6. $output = wp_kses_post( $input );
  7. return $output;
  8. }
  9. add_filter( 'cyberchimps_sanitize_text_html', 'cyberchimps_sanitize_text_html' );
  10. /* Unfiltered Textarea */
  11. function cyberchimps_sanitize_unfiltered_textarea( $input ) {
  12. $output = cyberchimps_get_option( 'html_box', '' );
  13. if( current_user_can( 'unfiltered_html' ) ) {
  14. $output = $input;
  15. return $output;
  16. }
  17. else {
  18. return $output;
  19. }
  20. }
  21. add_filter( 'cyberchimps_sanitize_unfiltered_textarea', 'cyberchimps_sanitize_unfiltered_textarea' );
  22. /* CSS Textarea */
  23. function cyberchimps_sanitize_csstextarea( $input ) {
  24. // Remove unwanted white spaces from start and end.
  25. $input = trim( $input );
  26. if( !strlen( $input ) ) {
  27. return $input;
  28. }
  29. $input = wp_kses_post( $input );
  30. if( strlen( $input ) ) {
  31. $output = $input;
  32. }
  33. else {
  34. $options = get_option( 'cyberchimps_options' );
  35. $output = $options['custom_css'];
  36. }
  37. return $output;
  38. }
  39. add_filter( 'cyberchimps_sanitize_csstextarea', 'cyberchimps_sanitize_csstextarea' );
  40. /* Textarea */
  41. function cyberchimps_sanitize_textarea( $input ) {
  42. global $allowedposttags;
  43. $output = wp_kses( $input, $allowedposttags );
  44. return $output;
  45. }
  46. add_filter( 'cyberchimps_sanitize_textarea', 'cyberchimps_sanitize_textarea' );
  47. /* Select */
  48. add_filter( 'cyberchimps_sanitize_select', 'cyberchimps_sanitize_enum', 10, 2 );
  49. /* Radio */
  50. add_filter( 'cyberchimps_sanitize_radio', 'cyberchimps_sanitize_enum', 10, 2 );
  51. /* Images */
  52. add_filter( 'cyberchimps_sanitize_images', 'cyberchimps_sanitize_enum', 10, 2 );
  53. /* Checkbox */
  54. function cyberchimps_sanitize_checkbox( $input ) {
  55. if( $input ) {
  56. $output = '1';
  57. }
  58. else {
  59. $output = false;
  60. }
  61. return $output;
  62. }
  63. add_filter( 'cyberchimps_sanitize_checkbox', 'cyberchimps_sanitize_checkbox' );
  64. /* Multicheck */
  65. function cyberchimps_sanitize_multicheck( $input, $option ) {
  66. $output = '';
  67. if( is_array( $input ) ) {
  68. foreach( $option['options'] as $key => $value ) {
  69. $output[$key] = "0";
  70. }
  71. foreach( $input as $key => $value ) {
  72. if( array_key_exists( $key, $option['options'] ) && $value ) {
  73. $output[$key] = "1";
  74. }
  75. }
  76. }
  77. return $output;
  78. }
  79. add_filter( 'cyberchimps_sanitize_multicheck', 'cyberchimps_sanitize_multicheck', 10, 2 );
  80. /* Toggle */
  81. function cyberchimps_sanitize_toggle( $input ) {
  82. if( $input ) {
  83. $output = '1';
  84. }
  85. else {
  86. $output = false;
  87. }
  88. return $output;
  89. }
  90. add_filter( 'cyberchimps_sanitize_toggle', 'cyberchimps_sanitize_toggle' );
  91. /* Color Picker */
  92. add_filter( 'cyberchimps_sanitize_color', 'cyberchimps_sanitize_hex' );
  93. /* Uploader */
  94. function cyberchimps_sanitize_upload( $input ) {
  95. $output = '';
  96. $filetype = wp_check_filetype( $input );
  97. // check if gravatar has been set as an image
  98. if( strpos( $input, 'gravatar' ) ) {
  99. $output = $input;
  100. }
  101. elseif( $filetype["ext"] ) {
  102. $output = $input;
  103. }
  104. return $output;
  105. }
  106. add_filter( 'cyberchimps_sanitize_upload', 'cyberchimps_sanitize_upload' );
  107. /* Editor */
  108. function cyberchimps_sanitize_editor( $input ) {
  109. if( current_user_can( 'unfiltered_html' ) ) {
  110. $output = $input;
  111. }
  112. else {
  113. global $allowedtags;
  114. $output = wpautop( wp_kses( $input, $allowedtags ) );
  115. }
  116. return $output;
  117. }
  118. add_filter( 'cyberchimps_sanitize_editor', 'cyberchimps_sanitize_editor' );
  119. /* Allowed Tags */
  120. function cyberchimps_sanitize_allowedtags( $input ) {
  121. global $allowedtags;
  122. $output = wpautop( wp_kses( $input, $allowedtags ) );
  123. return $output;
  124. }
  125. /* Allowed Post Tags */
  126. function cyberchimps_sanitize_allowedposttags( $input ) {
  127. global $allowedposttags;
  128. $output = wpautop( wp_kses( $input, $allowedposttags ) );
  129. return $output;
  130. }
  131. add_filter( 'cyberchimps_sanitize_info', 'cyberchimps_sanitize_allowedposttags' );
  132. /* Check that the key value sent is valid */
  133. function cyberchimps_sanitize_enum( $input, $option ) {
  134. $output = '';
  135. if( $input != false ) {
  136. if( array_key_exists( $input, $option['options'] ) ) {
  137. $output = $input;
  138. }
  139. }
  140. return $output;
  141. }
  142. /* Section Order */
  143. function cyberchimps_sanitize_section_order( $input, $option ) {
  144. $output = '';
  145. if( is_array( $input ) ) {
  146. foreach( $input as $key => $value ) {
  147. if( array_key_exists( $key, $option['options'] ) && $key ) {
  148. $output[] = $key;
  149. }
  150. elseif( array_key_exists( $value, $option['options'] ) && $value ) {
  151. $output[] = $value;
  152. }
  153. }
  154. }
  155. return $output;
  156. }
  157. add_filter( 'cyberchimps_sanitize_section_order', 'cyberchimps_sanitize_section_order', 10, 2 );
  158. /* Background */
  159. function cyberchimps_sanitize_background( $input ) {
  160. $output = wp_parse_args( $input, array(
  161. 'color' => '',
  162. 'image' => '',
  163. 'repeat' => 'repeat',
  164. 'position' => 'top center',
  165. 'attachment' => 'scroll'
  166. ) );
  167. $output['color'] = apply_filters( 'cyberchimps_sanitize_hex', $input['color'] );
  168. $output['image'] = apply_filters( 'cyberchimps_sanitize_upload', $input['image'] );
  169. $output['repeat'] = apply_filters( 'cyberchimps_background_repeat', $input['repeat'] );
  170. $output['position'] = apply_filters( 'cyberchimps_background_position', $input['position'] );
  171. $output['attachment'] = apply_filters( 'cyberchimps_background_attachment', $input['attachment'] );
  172. return $output;
  173. }
  174. add_filter( 'cyberchimps_sanitize_background', 'cyberchimps_sanitize_background' );
  175. function cyberchimps_sanitize_background_repeat( $value ) {
  176. $recognized = cyberchimps_recognized_background_repeat();
  177. if( array_key_exists( $value, $recognized ) ) {
  178. return $value;
  179. }
  180. return apply_filters( 'cyberchimps_default_background_repeat', current( $recognized ) );
  181. }
  182. add_filter( 'cyberchimps_background_repeat', 'cyberchimps_sanitize_background_repeat' );
  183. function cyberchimps_sanitize_background_position( $value ) {
  184. $recognized = cyberchimps_recognized_background_position();
  185. if( array_key_exists( $value, $recognized ) ) {
  186. return $value;
  187. }
  188. return apply_filters( 'cyberchimps_default_background_position', current( $recognized ) );
  189. }
  190. add_filter( 'cyberchimps_background_position', 'cyberchimps_sanitize_background_position' );
  191. function cyberchimps_sanitize_background_attachment( $value ) {
  192. $recognized = cyberchimps_recognized_background_attachment();
  193. if( array_key_exists( $value, $recognized ) ) {
  194. return $value;
  195. }
  196. return apply_filters( 'cyberchimps_default_background_attachment', current( $recognized ) );
  197. }
  198. add_filter( 'cyberchimps_background_attachment', 'cyberchimps_sanitize_background_attachment' );
  199. /* Typography */
  200. function cyberchimps_sanitize_typography( $input, $option ) {
  201. $output = wp_parse_args( $input, array(
  202. 'size' => '',
  203. 'face' => '',
  204. 'style' => '',
  205. 'color' => ''
  206. ) );
  207. if( isset( $option['options']['faces'] ) && isset( $input['face'] ) ) {
  208. if( !( array_key_exists( $input['face'], $option['options']['faces'] ) ) ) {
  209. $output['face'] = '';
  210. }
  211. }
  212. else {
  213. $output['face'] = apply_filters( 'cyberchimps_font_face', $output['face'] );
  214. }
  215. $output['size'] = apply_filters( 'cyberchimps_font_size', $output['size'] );
  216. $output['style'] = apply_filters( 'cyberchimps_font_style', $output['style'] );
  217. $output['color'] = apply_filters( 'cyberchimps_sanitize_color', $output['color'] );
  218. return $output;
  219. }
  220. add_filter( 'cyberchimps_sanitize_typography', 'cyberchimps_sanitize_typography', 10, 2 );
  221. function cyberchimps_sanitize_font_size( $value ) {
  222. $recognized = cyberchimps_recognized_font_sizes();
  223. $value_check = preg_replace( '/px/', '', $value );
  224. if( in_array( (int)$value_check, $recognized ) ) {
  225. return $value;
  226. }
  227. return apply_filters( 'cyberchimps_default_font_size', $recognized );
  228. }
  229. add_filter( 'cyberchimps_font_size', 'cyberchimps_sanitize_font_size' );
  230. function cyberchimps_sanitize_font_style( $value ) {
  231. $recognized = cyberchimps_recognized_font_styles();
  232. if( array_key_exists( $value, $recognized ) ) {
  233. return $value;
  234. }
  235. return apply_filters( 'cyberchimps_default_font_style', current( $recognized ) );
  236. }
  237. add_filter( 'cyberchimps_font_style', 'cyberchimps_sanitize_font_style' );
  238. function cyberchimps_sanitize_font_face( $value ) {
  239. $recognized = cyberchimps_recognized_font_faces();
  240. if( array_key_exists( $value, $recognized ) ) {
  241. return $value;
  242. }
  243. return apply_filters( 'cyberchimps_default_font_face', current( $recognized ) );
  244. }
  245. add_filter( 'cyberchimps_font_face', 'cyberchimps_sanitize_font_face' );
  246. /**
  247. * Get recognized background repeat settings
  248. *
  249. * @return array
  250. *
  251. */
  252. function cyberchimps_recognized_background_repeat() {
  253. $default = array(
  254. 'no-repeat' => __( 'No Repeat', 'cyberchimps_core' ),
  255. 'repeat-x' => __( 'Repeat Horizontally', 'cyberchimps_core' ),
  256. 'repeat-y' => __( 'Repeat Vertically', 'cyberchimps_core' ),
  257. 'repeat' => __( 'Repeat All', 'cyberchimps_core' ),
  258. );
  259. return apply_filters( 'cyberchimps_recognized_background_repeat', $default );
  260. }
  261. /**
  262. * Get recognized background positions
  263. *
  264. * @return array
  265. *
  266. */
  267. function cyberchimps_recognized_background_position() {
  268. $default = array(
  269. 'top left' => __( 'Top Left', 'cyberchimps_core' ),
  270. 'top center' => __( 'Top Center', 'cyberchimps_core' ),
  271. 'top right' => __( 'Top Right', 'cyberchimps_core' ),
  272. 'center left' => __( 'Middle Left', 'cyberchimps_core' ),
  273. 'center center' => __( 'Middle Center', 'cyberchimps_core' ),
  274. 'center right' => __( 'Middle Right', 'cyberchimps_core' ),
  275. 'bottom left' => __( 'Bottom Left', 'cyberchimps_core' ),
  276. 'bottom center' => __( 'Bottom Center', 'cyberchimps_core' ),
  277. 'bottom right' => __( 'Bottom Right', 'cyberchimps_core' )
  278. );
  279. return apply_filters( 'cyberchimps_recognized_background_position', $default );
  280. }
  281. /**
  282. * Get recognized background attachment
  283. *
  284. * @return array
  285. *
  286. */
  287. function cyberchimps_recognized_background_attachment() {
  288. $default = array(
  289. 'scroll' => __( 'Scroll Normally', 'cyberchimps_core' ),
  290. 'fixed' => __( 'Fixed in Place', 'cyberchimps_core' )
  291. );
  292. return apply_filters( 'cyberchimps_recognized_background_attachment', $default );
  293. }
  294. /**
  295. * Sanitize a color represented in hexidecimal notation.
  296. *
  297. * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
  298. * @param string The value that this function should return if it cannot be recognized as a color.
  299. *
  300. * @return string
  301. *
  302. */
  303. function cyberchimps_sanitize_hex( $hex, $default = '' ) {
  304. if( cyberchimps_validate_hex( $hex ) ) {
  305. return $hex;
  306. }
  307. return $default;
  308. }
  309. /**
  310. * Get recognized font sizes.
  311. *
  312. * Returns an indexed array of all recognized font sizes.
  313. * Values are integers and represent a range of sizes from
  314. * smallest to largest.
  315. *
  316. * @return array
  317. */
  318. function cyberchimps_recognized_font_sizes() {
  319. $sizes = range( 8, 71 );
  320. $sizes = apply_filters( 'cyberchimps_recognized_font_sizes', $sizes );
  321. $sizes = array_map( 'absint', $sizes );
  322. return $sizes;
  323. }
  324. /**
  325. * Get recognized font faces.
  326. *
  327. * Returns an array of all recognized font faces.
  328. * Keys are intended to be stored in the database
  329. * while values are ready for display in in html.
  330. *
  331. * @return array
  332. *
  333. */
  334. function cyberchimps_recognized_font_faces() {
  335. $default = array(
  336. 'arial' => 'Arial',
  337. 'verdana' => 'Verdana, Geneva',
  338. 'trebuchet' => 'Trebuchet',
  339. 'georgia' => 'Georgia',
  340. 'times' => 'Times New Roman',
  341. 'tahoma' => 'Tahoma, Geneva',
  342. 'palatino' => 'Palatino',
  343. 'helvetica' => 'Helvetica*'
  344. );
  345. return apply_filters( 'cyberchimps_recognized_font_faces', $default );
  346. }
  347. /**
  348. * Get recognized font styles.
  349. *
  350. * Returns an array of all recognized font styles.
  351. * Keys are intended to be stored in the database
  352. * while values are ready for display in in html.
  353. *
  354. * @return array
  355. *
  356. */
  357. function cyberchimps_recognized_font_styles() {
  358. $default = array(
  359. 'normal' => __( 'Normal', 'cyberchimps_core' ),
  360. 'italic' => __( 'Italic', 'cyberchimps_core' ),
  361. 'bold' => __( 'Bold', 'cyberchimps_core' ),
  362. 'bold italic' => __( 'Bold Italic', 'cyberchimps_core' )
  363. );
  364. return apply_filters( 'cyberchimps_recognized_font_styles', $default );
  365. }
  366. /**
  367. * Is a given string a color formatted in hexidecimal notation?
  368. *
  369. * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
  370. *
  371. * @return bool
  372. *
  373. */
  374. function cyberchimps_validate_hex( $hex ) {
  375. $hex = trim( $hex );
  376. /* Strip recognized prefixes. */
  377. if( 0 === strpos( $hex, '#' ) ) {
  378. $hex = substr( $hex, 1 );
  379. }
  380. elseif( 0 === strpos( $hex, '%23' ) ) {
  381. $hex = substr( $hex, 3 );
  382. }
  383. /* Regex match. */
  384. if( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
  385. return false;
  386. }
  387. else {
  388. return true;
  389. }
  390. }