Nessuna descrizione

NewsletterHtmlSanitizer.php 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace MailPoet\Newsletter;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class NewsletterHtmlSanitizer {
  6. /** @var WPFunctions */
  7. private $wp;
  8. /**
  9. * @var array
  10. * Configuration of allowed tags for form blocks that may contain some html.
  11. * Covers all tags available in the form editor's Rich Text component
  12. */
  13. private $allowedHtml = [
  14. 'p' => [
  15. 'class' => true,
  16. 'style' => true,
  17. ],
  18. 'span' => [
  19. 'class' => true,
  20. 'style' => true,
  21. ],
  22. 'a' => [
  23. 'href' => true,
  24. 'class' => true,
  25. 'title' => true,
  26. 'target' => true,
  27. 'style' => true,
  28. ],
  29. 'h1' => [
  30. 'class' => true,
  31. 'style' => true,
  32. ],
  33. 'h2' => [
  34. 'class' => true,
  35. 'style' => true,
  36. ],
  37. 'h3' => [
  38. 'class' => true,
  39. 'style' => true,
  40. ],
  41. 'ol' => [
  42. 'class' => true,
  43. 'style' => true,
  44. ],
  45. 'ul' => [
  46. 'class' => true,
  47. 'style' => true,
  48. ],
  49. 'li' => [
  50. 'class' => true,
  51. 'style' => true,
  52. ],
  53. 'strong' => [
  54. 'class' => true,
  55. 'style' => true,
  56. ],
  57. 'em' => [
  58. 'class' => true,
  59. 'style' => true,
  60. ],
  61. 'strike' => [],
  62. 'br' => [],
  63. 'blockquote' => [
  64. 'class' => true,
  65. 'style' => true,
  66. ],
  67. 'table' => [
  68. 'class' => true,
  69. 'style' => true,
  70. ],
  71. 'tr' => [
  72. 'class' => true,
  73. 'style' => true,
  74. ],
  75. 'th' => [
  76. 'class' => true,
  77. 'style' => true,
  78. ],
  79. 'td' => [
  80. 'class' => true,
  81. 'style' => true,
  82. ],
  83. 'del' => [],
  84. ];
  85. public function __construct(
  86. WPFunctions $wp
  87. ) {
  88. $this->wp = $wp;
  89. }
  90. public function sanitize(string $html): string {
  91. // Because wpKses break shortcodes we prefix shortcodes with http protocol
  92. $html = str_replace('href="[', 'href="http://[', $html);
  93. $html = $this->wp->wpKses($html, $this->allowedHtml);
  94. $html = str_replace('href="http://[', 'href="[', $html);
  95. return $html;
  96. }
  97. }