class-wp-simplepie-sanitize-kses.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Feed API: WP_SimplePie_Sanitize_KSES class
  4. *
  5. * @package WordPress
  6. * @subpackage Feed
  7. * @since 4.7.0
  8. */
  9. /**
  10. * Core class used to implement SimpliePie feed sanitization.
  11. *
  12. * Extends the SimplePie_Sanitize class to use KSES, because
  13. * we cannot universally count on DOMDocument being available.
  14. *
  15. * @since 3.5.0
  16. *
  17. * @see SimplePie_Sanitize
  18. */
  19. class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
  20. /**
  21. * WordPress SimplePie sanitization using KSES.
  22. *
  23. * Sanitizes the incoming data, to ensure that it matches the type of data expected, using KSES.
  24. *
  25. * @since 3.5.0
  26. *
  27. * @param mixed $data The data that needs to be sanitized.
  28. * @param integer $type The type of data that it's supposed to be.
  29. * @param string $base Optional. The `xml:base` value to use when converting relative
  30. * URLs to absolute ones. Default empty.
  31. * @return mixed Sanitized data.
  32. */
  33. public function sanitize( $data, $type, $base = '' ) {
  34. $data = trim( $data );
  35. if ( $type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML ) {
  36. if ( preg_match( '/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>)/', $data ) ) {
  37. $type |= SIMPLEPIE_CONSTRUCT_HTML;
  38. } else {
  39. $type |= SIMPLEPIE_CONSTRUCT_TEXT;
  40. }
  41. }
  42. if ( $type & SIMPLEPIE_CONSTRUCT_BASE64 ) {
  43. $data = base64_decode( $data );
  44. }
  45. if ( $type & ( SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML ) ) {
  46. $data = wp_kses_post( $data );
  47. if ( $this->output_encoding !== 'UTF-8' ) {
  48. $data = $this->registry->call( 'Misc', 'change_encoding', array( $data, 'UTF-8', $this->output_encoding ) );
  49. }
  50. return $data;
  51. } else {
  52. return parent::sanitize( $data, $type, $base );
  53. }
  54. }
  55. }