/wp-content/themes/filtered/admin/options-sanitize.php

https://github.com/livinglab/openlab · PHP · 336 lines · 210 code · 50 blank · 76 comment · 17 complexity · 7ce7cd9fe3087bc2e4e6e355818885ce MD5 · raw file

  1. <?php
  2. /* Text */
  3. add_filter( 'of_sanitize_text', 'sanitize_text_field' );
  4. /* Textarea */
  5. function of_sanitize_textarea($input) {
  6. global $allowedtags;
  7. $output = wp_kses( $input, $allowedtags);
  8. return $output;
  9. }
  10. add_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
  11. /* Select */
  12. add_filter( 'of_sanitize_select', 'of_sanitize_enum', 10, 2);
  13. /* Radio */
  14. add_filter( 'of_sanitize_radio', 'of_sanitize_enum', 10, 2);
  15. /* Images */
  16. add_filter( 'of_sanitize_images', 'of_sanitize_enum', 10, 2);
  17. /* Checkbox */
  18. function of_sanitize_checkbox( $input ) {
  19. if ( $input ) {
  20. $output = "1";
  21. } else {
  22. $output = "0";
  23. }
  24. return $output;
  25. }
  26. add_filter( 'of_sanitize_checkbox', 'of_sanitize_checkbox' );
  27. /* Multicheck */
  28. function of_sanitize_multicheck( $input, $option ) {
  29. $output = '';
  30. if ( is_array( $input ) ) {
  31. foreach( $option['options'] as $key => $value ) {
  32. $output[$key] = "0";
  33. }
  34. foreach( $input as $key => $value ) {
  35. if ( array_key_exists( $key, $option['options'] ) && $value ) {
  36. $output[$key] = "1";
  37. }
  38. }
  39. }
  40. return $output;
  41. }
  42. add_filter( 'of_sanitize_multicheck', 'of_sanitize_multicheck', 10, 2 );
  43. /* Color Picker */
  44. add_filter( 'of_sanitize_color', 'of_sanitize_hex' );
  45. /* Uploader */
  46. function of_sanitize_upload( $input ) {
  47. $output = '';
  48. $filetype = wp_check_filetype($input);
  49. if ( $filetype["ext"] ) {
  50. $output = $input;
  51. }
  52. return $output;
  53. }
  54. add_filter( 'of_sanitize_upload', 'of_sanitize_upload' );
  55. /* Check that the key value sent is valid */
  56. function of_sanitize_enum( $input, $option ) {
  57. $output = '';
  58. if ( array_key_exists( $input, $option['options'] ) ) {
  59. $output = $input;
  60. }
  61. return $output;
  62. }
  63. /* Background */
  64. function of_sanitize_background( $input ) {
  65. $output = wp_parse_args( $input, array(
  66. 'color' => '',
  67. 'image' => '',
  68. 'repeat' => 'repeat',
  69. 'position' => 'top center',
  70. 'attachment' => 'scroll'
  71. ) );
  72. $output['color'] = apply_filters( 'of_sanitize_hex', $input['color'] );
  73. $output['image'] = apply_filters( 'of_sanitize_upload', $input['image'] );
  74. $output['repeat'] = apply_filters( 'of_background_repeat', $input['repeat'] );
  75. $output['position'] = apply_filters( 'of_background_position', $input['position'] );
  76. $output['attachment'] = apply_filters( 'of_background_attachment', $input['attachment'] );
  77. return $output;
  78. }
  79. add_filter( 'of_sanitize_background', 'of_sanitize_background' );
  80. function of_sanitize_background_repeat( $value ) {
  81. $recognized = of_recognized_background_repeat();
  82. if ( array_key_exists( $value, $recognized ) ) {
  83. return $value;
  84. }
  85. return apply_filters( 'of_default_background_repeat', current( $recognized ) );
  86. }
  87. add_filter( 'of_background_repeat', 'of_sanitize_background_repeat' );
  88. function of_sanitize_background_position( $value ) {
  89. $recognized = of_recognized_background_position();
  90. if ( array_key_exists( $value, $recognized ) ) {
  91. return $value;
  92. }
  93. return apply_filters( 'of_default_background_position', current( $recognized ) );
  94. }
  95. add_filter( 'of_background_position', 'of_sanitize_background_position' );
  96. function of_sanitize_background_attachment( $value ) {
  97. $recognized = of_recognized_background_attachment();
  98. if ( array_key_exists( $value, $recognized ) ) {
  99. return $value;
  100. }
  101. return apply_filters( 'of_default_background_attachment', current( $recognized ) );
  102. }
  103. add_filter( 'of_background_attachment', 'of_sanitize_background_attachment' );
  104. /* Typography */
  105. function of_sanitize_typography( $input ) {
  106. $output = wp_parse_args( $input, array(
  107. 'size' => '',
  108. 'face' => '',
  109. 'style' => '',
  110. 'color' => ''
  111. ) );
  112. $output['size'] = apply_filters( 'of_font_size', $output['size'] );
  113. $output['face'] = apply_filters( 'of_font_face', $output['face'] );
  114. $output['style'] = apply_filters( 'of_font_style', $output['style'] );
  115. $output['color'] = apply_filters( 'of_color', $output['color'] );
  116. return $output;
  117. }
  118. add_filter( 'of_sanitize_typography', 'of_sanitize_typography' );
  119. function of_sanitize_font_size( $value ) {
  120. $recognized = of_recognized_font_sizes();
  121. $value = preg_replace('/px/','', $value);
  122. if ( in_array( (int) $value, $recognized ) ) {
  123. return (int) $value;
  124. }
  125. return (int) apply_filters( 'of_default_font_size', $recognized );
  126. }
  127. add_filter( 'of_font_face', 'of_sanitize_font_face' );
  128. function of_sanitize_font_style( $value ) {
  129. $recognized = of_recognized_font_styles();
  130. if ( array_key_exists( $value, $recognized ) ) {
  131. return $value;
  132. }
  133. return apply_filters( 'of_default_font_style', current( $recognized ) );
  134. }
  135. add_filter( 'of_font_style', 'of_sanitize_font_style' );
  136. function of_sanitize_font_face( $value ) {
  137. $recognized = of_recognized_font_faces();
  138. if ( array_key_exists( $value, $recognized ) ) {
  139. return $value;
  140. }
  141. return apply_filters( 'of_default_font_face', current( $recognized ) );
  142. }
  143. add_filter( 'of_font_face', 'of_sanitize_font_face' );
  144. /**
  145. * Get recognized background repeat settings
  146. *
  147. * @return array
  148. *
  149. */
  150. function of_recognized_background_repeat() {
  151. $default = array(
  152. 'no-repeat' => 'No Repeat',
  153. 'repeat-x' => 'Repeat Horizontally',
  154. 'repeat-y' => 'Repeat Vertically',
  155. 'repeat' => 'Repeat All',
  156. );
  157. return apply_filters( 'of_recognized_background_repeat', $default );
  158. }
  159. /**
  160. * Get recognized background positions
  161. *
  162. * @return array
  163. *
  164. */
  165. function of_recognized_background_position() {
  166. $default = array(
  167. 'top left' => 'Top Left',
  168. 'top center' => 'Top Center',
  169. 'top right' => 'Top Right',
  170. 'center left' => 'Middle Left',
  171. 'center center' => 'Middle Center',
  172. 'center right' => 'Middle Right',
  173. 'bottom left' => 'Bottom Left',
  174. 'bottom center' => 'Bottom Center',
  175. 'bottom right' => 'Bottom Right'
  176. );
  177. return apply_filters( 'of_recognized_background_position', $default );
  178. }
  179. /**
  180. * Get recognized background attachment
  181. *
  182. * @return array
  183. *
  184. */
  185. function of_recognized_background_attachment() {
  186. $default = array(
  187. 'scroll' => 'Scroll Normally',
  188. 'fixed' => 'Fixed in Place'
  189. );
  190. return apply_filters( 'of_recognized_background_attachment', $default );
  191. }
  192. /**
  193. * Sanitize a color represented in hexidecimal notation.
  194. *
  195. * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
  196. * @param string The value that this function should return if it cannot be recognized as a color.
  197. * @return string
  198. *
  199. */
  200. function of_sanitize_hex( $hex, $default = '' ) {
  201. if ( of_validate_hex( $hex ) ) {
  202. return $hex;
  203. }
  204. return $default;
  205. }
  206. /**
  207. * Get recognized font sizes.
  208. *
  209. * Returns an indexed array of all recognized font sizes.
  210. * Values are integers and represent a range of sizes from
  211. * smallest to largest.
  212. *
  213. * @return array
  214. */
  215. function of_recognized_font_sizes() {
  216. $sizes = range( 9, 71 );
  217. $sizes = apply_filters( 'of_recognized_font_sizes', $sizes );
  218. $sizes = array_map( 'absint', $sizes );
  219. return $sizes;
  220. }
  221. /**
  222. * Get recognized font faces.
  223. *
  224. * Returns an array of all recognized font faces.
  225. * Keys are intended to be stored in the database
  226. * while values are ready for display in in html.
  227. *
  228. * @return array
  229. *
  230. */
  231. function of_recognized_font_faces() {
  232. $default = array(
  233. 'arial' => 'Arial',
  234. 'verdana' => 'Verdana, Geneva',
  235. 'trebuchet' => 'Trebuchet',
  236. 'georgia' => 'Georgia',
  237. 'times' => 'Times New Roman',
  238. 'tahoma' => 'Tahoma, Geneva',
  239. 'palatino' => 'Palatino',
  240. 'helvetica' => 'Helvetica*'
  241. );
  242. return apply_filters( 'of_recognized_font_faces', $default );
  243. }
  244. /**
  245. * Get recognized font styles.
  246. *
  247. * Returns an array of all recognized font styles.
  248. * Keys are intended to be stored in the database
  249. * while values are ready for display in in html.
  250. *
  251. * @return array
  252. *
  253. */
  254. function of_recognized_font_styles() {
  255. $default = array(
  256. 'normal' => 'Normal',
  257. 'italic' => 'Italic',
  258. 'bold' => 'Bold',
  259. 'bold italic' => 'Bold Italic'
  260. );
  261. return apply_filters( 'of_recognized_font_styles', $default );
  262. }
  263. /**
  264. * Is a given string a color formatted in hexidecimal notation?
  265. *
  266. * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
  267. * @return bool
  268. *
  269. */
  270. function of_validate_hex( $hex ) {
  271. $hex = trim( $hex );
  272. /* Strip recognized prefixes. */
  273. if ( 0 === strpos( $hex, '#' ) ) {
  274. $hex = substr( $hex, 1 );
  275. }
  276. elseif ( 0 === strpos( $hex, '%23' ) ) {
  277. $hex = substr( $hex, 3 );
  278. }
  279. /* Regex match. */
  280. if ( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
  281. return false;
  282. }
  283. else {
  284. return true;
  285. }
  286. }