PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 377 lines | 236 code | 62 blank | 79 comment | 17 complexity | be63f32755da57b00d77e1c5ac65d757 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_wpbs_typography( $input ) {
  136. $output = wp_parse_args( $input, array(
  137. 'face' => '',
  138. 'style' => '',
  139. 'color' => ''
  140. ) );
  141. $output['face'] = apply_filters( 'of_font_face', $output['face'] );
  142. $output['style'] = apply_filters( 'of_font_style', $output['style'] );
  143. $output['color'] = apply_filters( 'of_color', $output['color'] );
  144. return $output;
  145. }
  146. add_filter( 'of_sanitize_wpbs_typography', 'of_sanitize_wpbs_typography' );
  147. function of_sanitize_font_size( $value ) {
  148. $recognized = of_recognized_font_sizes();
  149. $value = preg_replace('/px/','', $value);
  150. if ( in_array( (int) $value, $recognized ) ) {
  151. return (int) $value;
  152. }
  153. return (int) apply_filters( 'of_default_font_size', $recognized );
  154. }
  155. add_filter( 'of_font_face', 'of_sanitize_font_face' );
  156. function of_sanitize_font_style( $value ) {
  157. $recognized = of_recognized_font_styles();
  158. if ( array_key_exists( $value, $recognized ) ) {
  159. return $value;
  160. }
  161. return apply_filters( 'of_default_font_style', current( $recognized ) );
  162. }
  163. add_filter( 'of_font_style', 'of_sanitize_font_style' );
  164. function of_sanitize_font_face( $value ) {
  165. $recognized = of_recognized_font_faces();
  166. if ( array_key_exists( $value, $recognized ) ) {
  167. return $value;
  168. }
  169. return apply_filters( 'of_default_font_face', current( $recognized ) );
  170. }
  171. add_filter( 'of_font_face', 'of_sanitize_font_face' );
  172. /**
  173. * Get recognized background repeat settings
  174. *
  175. * @return array
  176. *
  177. */
  178. function of_recognized_background_repeat() {
  179. $default = array(
  180. 'no-repeat' => 'No Repeat',
  181. 'repeat-x' => 'Repeat Horizontally',
  182. 'repeat-y' => 'Repeat Vertically',
  183. 'repeat' => 'Repeat All',
  184. );
  185. return apply_filters( 'of_recognized_background_repeat', $default );
  186. }
  187. /**
  188. * Get recognized background positions
  189. *
  190. * @return array
  191. *
  192. */
  193. function of_recognized_background_position() {
  194. $default = array(
  195. 'top left' => 'Top Left',
  196. 'top center' => 'Top Center',
  197. 'top right' => 'Top Right',
  198. 'center left' => 'Middle Left',
  199. 'center center' => 'Middle Center',
  200. 'center right' => 'Middle Right',
  201. 'bottom left' => 'Bottom Left',
  202. 'bottom center' => 'Bottom Center',
  203. 'bottom right' => 'Bottom Right'
  204. );
  205. return apply_filters( 'of_recognized_background_position', $default );
  206. }
  207. /**
  208. * Get recognized background attachment
  209. *
  210. * @return array
  211. *
  212. */
  213. function of_recognized_background_attachment() {
  214. $default = array(
  215. 'scroll' => 'Scroll Normally',
  216. 'fixed' => 'Fixed in Place'
  217. );
  218. return apply_filters( 'of_recognized_background_attachment', $default );
  219. }
  220. /**
  221. * Sanitize a color represented in hexidecimal notation.
  222. *
  223. * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
  224. * @param string The value that this function should return if it cannot be recognized as a color.
  225. * @return string
  226. *
  227. */
  228. function of_sanitize_hex( $hex, $default = '' ) {
  229. if ( of_validate_hex( $hex ) ) {
  230. return $hex;
  231. }
  232. return $default;
  233. }
  234. /**
  235. * Get recognized font sizes.
  236. *
  237. * Returns an indexed array of all recognized font sizes.
  238. * Values are integers and represent a range of sizes from
  239. * smallest to largest.
  240. *
  241. * @return array
  242. */
  243. function of_recognized_font_sizes() {
  244. $sizes = range( 9, 71 );
  245. $sizes = apply_filters( 'of_recognized_font_sizes', $sizes );
  246. $sizes = array_map( 'absint', $sizes );
  247. return $sizes;
  248. }
  249. /**
  250. * Get recognized font faces.
  251. *
  252. * Returns an array of all recognized font faces.
  253. * Keys are intended to be stored in the database
  254. * while values are ready for display in in html.
  255. *
  256. * @return array
  257. *
  258. */
  259. function of_recognized_font_faces() {
  260. $default = array(
  261. 'Open Sans' => 'Open Sans',
  262. 'arial' => 'Arial',
  263. 'verdana' => 'Verdana, Geneva',
  264. 'trebuchet' => 'Trebuchet',
  265. 'georgia' => 'Georgia',
  266. 'times' => 'Times New Roman',
  267. 'tahoma' => 'Tahoma, Geneva',
  268. 'palatino' => 'Palatino',
  269. 'helvetica' => 'Helvetica*'
  270. );
  271. return apply_filters( 'of_recognized_font_faces', $default );
  272. }
  273. /**
  274. * Get recognized font styles.
  275. *
  276. * Returns an array of all recognized font styles.
  277. * Keys are intended to be stored in the database
  278. * while values are ready for display in in html.
  279. *
  280. * @return array
  281. *
  282. */
  283. function of_recognized_font_styles() {
  284. $default = array(
  285. 'normal' => 'Normal',
  286. 'italic' => 'Italic',
  287. 'bold' => 'Bold',
  288. 'bold italic' => 'Bold Italic'
  289. );
  290. return apply_filters( 'of_recognized_font_styles', $default );
  291. }
  292. /**
  293. * Is a given string a color formatted in hexidecimal notation?
  294. *
  295. * @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
  296. * @return bool
  297. *
  298. */
  299. function of_validate_hex( $hex ) {
  300. $hex = trim( $hex );
  301. /* Strip recognized prefixes. */
  302. if ( 0 === strpos( $hex, '#' ) ) {
  303. $hex = substr( $hex, 1 );
  304. }
  305. elseif ( 0 === strpos( $hex, '%23' ) ) {
  306. $hex = substr( $hex, 3 );
  307. }
  308. /* Regex match. */
  309. if ( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
  310. return false;
  311. }
  312. else {
  313. return true;
  314. }
  315. }