Нет описания

Popups.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class Post_Types
  10. */
  11. class PUM_Site_Popups {
  12. /**
  13. * @var PUM_Popup|null
  14. *
  15. * @deprecated 1.8.0
  16. */
  17. public static $current;
  18. /**
  19. * @var WP_Query|null
  20. */
  21. public static $loaded;
  22. /**
  23. * @var array
  24. */
  25. public static $cached_content = [];
  26. /**
  27. * @var array
  28. */
  29. public static $loaded_ids = [];
  30. /**
  31. * Hook the initialize method to the WP init action.
  32. */
  33. public static function init() {
  34. // Preload the $loaded query.
  35. add_action( 'init', [ __CLASS__, 'get_loaded_popups' ] );
  36. // Check content for popups.
  37. add_filter( 'the_content', [ __CLASS__, 'check_content_for_popups' ] );
  38. // TODO determine if the late priority is needed.
  39. add_action( 'wp_enqueue_scripts', [ __CLASS__, 'load_popups' ], 11 );
  40. add_action( 'wp_footer', [ __CLASS__, 'render_popups' ] );
  41. }
  42. /**
  43. * Returns the current popup.
  44. *
  45. * @param bool|object|null $new_popup
  46. *
  47. * @return null|PUM_Model_Popup
  48. *
  49. * @deprecated 1.8.0
  50. */
  51. public static function current_popup( $new_popup = false ) {
  52. global $popup;
  53. if ( $new_popup !== false ) {
  54. pum()->current_popup = $new_popup;
  55. $popup = $new_popup;
  56. }
  57. return pum()->current_popup;
  58. }
  59. /**
  60. * Gets the loaded popup query.
  61. *
  62. * @return null|WP_Query
  63. */
  64. public static function get_loaded_popups() {
  65. if ( ! self::$loaded instanceof WP_Query ) {
  66. self::$loaded = new WP_Query();
  67. self::$loaded->posts = [];
  68. }
  69. return self::$loaded;
  70. }
  71. /**
  72. * Preload popups in the head and determine if they will be rendered or not.
  73. *
  74. * @uses `pum_preload_popup` filter
  75. * @uses `popmake_preload_popup` filter
  76. */
  77. public static function load_popups() {
  78. if ( is_admin() ) {
  79. return;
  80. }
  81. $popups = pum_get_all_popups( [ 'post_status' => [ 'publish', 'private' ] ] );
  82. if ( ! empty( $popups ) ) {
  83. foreach ( $popups as $popup ) {
  84. // Set this popup as the global $current.
  85. pum()->current_popup = $popup;
  86. // If the popup is loadable (passes conditions) load it.
  87. if ( pum_is_popup_loadable( $popup->ID ) ) {
  88. self::preload_popup( $popup );
  89. }
  90. }
  91. // Clear the global $current.
  92. pum()->current_popup = null;
  93. }
  94. }
  95. /**
  96. * Checks post content to see if there are popups we need to automagically load
  97. *
  98. * @param string $content The content from the filter.
  99. * @return string The content.
  100. * @since 1.15
  101. */
  102. public static function check_content_for_popups( $content ) {
  103. // Only search for popups in the main query of a singular page.
  104. if ( is_singular() && in_the_loop() && is_main_query() ) {
  105. /**
  106. * We want to detect instances of popmake-### but only within classes and not in the actual text.
  107. * So, we check to make sure it is wrapped by quotes to make sure it's in the class="" attribute
  108. * but also allow for whitespace and characters in case there are classes before or after it.
  109. */
  110. preg_match_all( '/[\'\"][\s\w\-\_]*?popmake-(\d+)[\s\w\-\_]*?[\'\"]/', $content, $matches );
  111. // Then, if we find any popups, let's preload it.
  112. foreach ( $matches[1] as $popup_id ) {
  113. self::preload_popup_by_id_if_enabled( $popup_id );
  114. }
  115. }
  116. return $content;
  117. }
  118. /**
  119. * Preloads popup, if enabled
  120. *
  121. * @param int $popup_id The popup's ID.
  122. * @since 1.15
  123. */
  124. public static function preload_popup_by_id_if_enabled( $popup_id ) {
  125. if ( ! in_array( $popup_id, self::$loaded_ids ) ) {
  126. $popup = pum_get_popup( $popup_id );
  127. if ( $popup->is_enabled() ) {
  128. self::preload_popup( $popup );
  129. }
  130. }
  131. }
  132. /**
  133. * @param PUM_Model_Popup $popup
  134. */
  135. public static function preload_popup( $popup ) {
  136. // Bail early if the popup is preloaded already.
  137. if ( in_array( $popup->ID, self::$loaded_ids, true ) ) {
  138. return;
  139. }
  140. // Add to the $loaded_ids list.
  141. self::$loaded_ids[] = $popup->ID;
  142. // Add to the $loaded query.
  143. self::$loaded->posts[] = $popup;
  144. self::$loaded->post_count ++;
  145. // Preprocess the content for shortcodes that need to enqueue their own assets.
  146. self::$cached_content[ $popup->ID ] = $popup->get_content();
  147. // Fire off preload action.
  148. do_action( 'pum_preload_popup', $popup->ID );
  149. // Deprecated filter.
  150. do_action( 'popmake_preload_popup', $popup->ID );
  151. }
  152. // REWRITE THIS
  153. public static function load_popup( $id ) {
  154. if ( did_action( 'wp_head' ) && ! in_array( $id, self::$loaded_ids ) ) {
  155. $args1 = [
  156. 'post_type' => 'popup',
  157. 'p' => $id,
  158. ];
  159. $query = new WP_Query( $args1 );
  160. if ( $query->have_posts() ) {
  161. while ( $query->have_posts() ) :
  162. $query->next_post();
  163. pum()->current_popup = $query->post;
  164. self::preload_popup( $query->post );
  165. endwhile;
  166. pum()->current_popup = null;
  167. }
  168. }
  169. return;
  170. }
  171. /**
  172. * Render the popups in the footer.
  173. */
  174. public static function render_popups() {
  175. $loaded = self::get_loaded_popups();
  176. if ( $loaded->have_posts() ) {
  177. while ( $loaded->have_posts() ) :
  178. $loaded->next_post();
  179. pum()->current_popup = $loaded->post;
  180. pum_template_part( 'popup' );
  181. endwhile;
  182. pum()->current_popup = null;
  183. }
  184. }
  185. /**
  186. * @param $popup_id
  187. *
  188. * @return string|bool
  189. */
  190. public static function get_cache_content( $popup_id ) {
  191. return isset( self::$cached_content[ $popup_id ] ) ? self::$cached_content[ $popup_id ] : false;
  192. }
  193. }