Sin descripción

Site.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Class PUM_Site
  4. */
  5. class PUM_Site {
  6. public static function init() {
  7. PUM_Site_Assets::init();
  8. PUM_Site_Popups::init();
  9. PUM_Analytics::init();
  10. self::add_core_content_filters();
  11. add_action( 'init', array( __CLASS__, 'actions' ) );
  12. }
  13. /**
  14. * Hook core filters into `pum_popup_content`.
  15. */
  16. public static function add_core_content_filters() {
  17. global $wp_version;
  18. /**
  19. * Copied from wp-includes/class-wp-embed.php:32:40
  20. *
  21. * @note Hack to get the [embed] shortcode to run before wpautop().
  22. *
  23. * @since 1.4 hooks & filters
  24. */
  25. add_filter( 'pum_popup_content', array( $GLOBALS['wp_embed'], 'run_shortcode' ), 8 );
  26. add_filter( 'pum_popup_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
  27. /**
  28. * Copied & from wp-includes/default-filters.php:141:144.
  29. *
  30. * Format WordPress.
  31. *
  32. * @since 1.10.0
  33. * @sinceWP 5.4
  34. */
  35. foreach ( array( 'pum_popup_content', 'pum_popup_title' ) as $filter ) {
  36. add_filter( $filter, 'capital_P_dangit', 11 );
  37. }
  38. /**
  39. * Copied & from wp-includes/default-filters.php:172:178.
  40. *
  41. * @since 1.10.0
  42. * @sinceWP 5.4
  43. */
  44. if ( version_compare( $wp_version, '5.0.0', '>=' ) ) {
  45. add_filter( 'pum_popup_content', array( __CLASS__, 'do_blocks' ), 9 );
  46. }
  47. add_filter( 'pum_popup_content', 'wptexturize' );
  48. add_filter( 'pum_popup_content', 'convert_smilies', 20 );
  49. add_filter( 'pum_popup_content', 'wpautop' );
  50. add_filter( 'pum_popup_content', 'shortcode_unautop' );
  51. add_filter( 'pum_popup_content', 'prepend_attachment' );
  52. if ( version_compare( $wp_version, '5.5', '>=' ) ) {
  53. add_filter( 'pum_popup_content', 'wp_filter_content_tags' );
  54. } else {
  55. add_filter( 'pum_popup_content', 'wp_make_content_images_responsive' );
  56. }
  57. /**
  58. * Copied & from wp-includes/default-filters.php:172:178.
  59. *
  60. * @note Shortcodes must run AFTER wpautop().
  61. *
  62. * @since 1.10.0
  63. * @sinceWP 5.4
  64. */
  65. $do_shortcode_handler = pum_get_option( 'disable_shortcode_compatibility_mode' ) ? 'do_shortcode' : array( 'PUM_Helpers', 'do_shortcode' );
  66. add_filter( 'pum_popup_content', $do_shortcode_handler, 11 );
  67. }
  68. /**
  69. * Parses dynamic blocks out of `post_content` and re-renders them.
  70. *
  71. * @since 1.10.0
  72. * @sinceWP 5.0.0
  73. *
  74. * @param string $content Post content.
  75. * @return string Updated post content.
  76. */
  77. public static function do_blocks( $content ) {
  78. $blocks = parse_blocks( $content );
  79. $output = '';
  80. foreach ( $blocks as $block ) {
  81. $output .= render_block( $block );
  82. }
  83. // If there are blocks in this content, we shouldn't run wpautop() on it later.
  84. $priority = has_filter( 'pum_popup_content', 'wpautop' );
  85. if ( false !== $priority && doing_filter( 'pum_popup_content' ) && has_blocks( $content ) ) {
  86. remove_filter( 'pum_popup_content', 'wpautop', $priority );
  87. add_filter( 'pum_popup_content', array( __CLASS__, '_restore_wpautop_hook' ), $priority + 1 );
  88. }
  89. return $output;
  90. }
  91. /**
  92. * If do_blocks() needs to remove wpautop() from the `pum_popup_content` filter, this re-adds it afterwards,
  93. * for subsequent `pum_popup_content` usage.
  94. *
  95. * @access private
  96. *
  97. * @since 1.10.0
  98. * @sinceWP 5.0.0
  99. *
  100. * @param string $content The post content running through this filter.
  101. * @return string The unmodified content.
  102. */
  103. public static function _restore_wpautop_hook( $content ) {
  104. $current_priority = has_filter( 'pum_popup_content', [ __CLASS__, '_restore_wpautop_hook' ] );
  105. add_filter( 'pum_popup_content', 'wpautop', $current_priority - 1 );
  106. remove_filter( 'pum_popup_content', [ __CLASS__, '_restore_wpautop_hook' ], $current_priority );
  107. return $content;
  108. }
  109. /**
  110. * Hooks Popup Maker actions, when present in the $_GET superglobal. Every popmake_action
  111. * present in $_GET is called using WordPress's do_action function. These
  112. * functions are called on init.
  113. */
  114. public static function actions() {
  115. if ( empty( $_REQUEST['pum_action'] ) ) {
  116. return;
  117. }
  118. $valid_actions = apply_filters(
  119. 'pum_valid_request_actions',
  120. array(
  121. 'save_enabled_betas',
  122. 'download_batch_export',
  123. 'empty_error_log',
  124. )
  125. );
  126. $action = sanitize_text_field( $_REQUEST['pum_action'] );
  127. if ( ! in_array( $action, $valid_actions ) || ! has_action( 'pum_' . $action ) ) {
  128. return;
  129. }
  130. do_action( 'pum_' . $action, $_REQUEST );
  131. }
  132. }