options-sanitize.php 9.4 KB

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