/admin/options-sanitize.php

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