From c032d7cf6cebbe3f83bc8adfe5d3f3e57844fc76 Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 10:02:23 -0500 Subject: [PATCH 1/8] Create new method for loading popups found in content --- classes/Site/Popups.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/classes/Site/Popups.php b/classes/Site/Popups.php index 58f81c41e..a26fcd10f 100644 --- a/classes/Site/Popups.php +++ b/classes/Site/Popups.php @@ -42,6 +42,9 @@ public static function init() { // Preload the $loaded query. add_action( 'init', array( __CLASS__, 'get_loaded_popups' ) ); + // Check content for popups. + add_filter( 'the_content', array( __CLASS__, 'check_content_for_popups' ) ); + // TODO determine if the late priority is needed. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_popups' ), 11 ); @@ -110,7 +113,28 @@ public static function load_popups() { // Clear the global $current. pum()->current_popup = null; } + } + /** + * Checks post content to see if there are popups we need to automagically load + * + * @param string $content The content from the filter. + * @return string The content. + */ + public static function check_content_for_popups( $content ) { + /** + * We want to detect instances of popmake-### but only within classes and not in the actual text. + * So, we check to make sure it is wrapped by quotes to make sure it's in the class="" attribute + * but also allow for whitespace and characters in case there are classes before or after it. + */ + preg_match_all( '/[\'\"][\s\w\-\_]*?popmake-(\d+)[\s\w\-\_]*?[\'\"]/', $content, $matches ); + + // Then, if we find any popups, let's preload it. + foreach ( $matches[1] as $popup_id ) { + $popup = pum_get_popup( $popup_id ); + self::preload_popup( $popup ); + } + return $content; } /** From 32ba9c162e27563878488eae5cf79967726e8146 Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 10:15:56 -0500 Subject: [PATCH 2/8] Ensure popup is only loaded if enabled Issue #543 --- classes/Site/Popups.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/classes/Site/Popups.php b/classes/Site/Popups.php index a26fcd10f..f8cd7d770 100644 --- a/classes/Site/Popups.php +++ b/classes/Site/Popups.php @@ -132,7 +132,9 @@ public static function check_content_for_popups( $content ) { // Then, if we find any popups, let's preload it. foreach ( $matches[1] as $popup_id ) { $popup = pum_get_popup( $popup_id ); - self::preload_popup( $popup ); + if ( $popup->is_enabled() ) { + self::preload_popup( $popup ); + } } return $content; } From e3803a72f01852df17ac3e891b0597800e002add Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 11:05:00 -0500 Subject: [PATCH 3/8] Automatically load popups when menu item triggers popup Issue #543 --- includes/modules/menus.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/modules/menus.php b/includes/modules/menus.php index 6faad872c..cd789d759 100644 --- a/includes/modules/menus.php +++ b/includes/modules/menus.php @@ -115,6 +115,10 @@ public static function merge_item_data( $item ) { } if ( isset( $item->popup_id ) ) { + $popup = pum_get_popup( $item->popup_id ); + if ( $popup->is_enabled() ) { + PUM_Site_Popups::preload_popup( $popup ); + } $item->classes[] = 'popmake-' . $item->popup_id; } From 95d344ada217393a4d1de8fb5f228f3939afc6ae Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 11:12:02 -0500 Subject: [PATCH 4/8] Auto load popup when using popup_trigger shortcode Issue #543 --- classes/Shortcode/PopupTrigger.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/classes/Shortcode/PopupTrigger.php b/classes/Shortcode/PopupTrigger.php index 3b5c0adb6..26ac79521 100644 --- a/classes/Shortcode/PopupTrigger.php +++ b/classes/Shortcode/PopupTrigger.php @@ -141,13 +141,16 @@ public function fields() { * @return string */ public function handler( $atts, $content = null ) { - $atts = $this->shortcode_atts( $atts ); - - - $return = '<' . $atts['tag'] . ' class="pum-trigger popmake-' . $atts['id'] . ' ' . $atts['classes'] . '" data-do-default="' . esc_attr( $atts['do_default'] ) . '">'; + $atts = $this->shortcode_atts( $atts ); + $return = '<' . $atts['tag'] . ' class="pum-trigger popmake-' . $atts['id'] . ' ' . $atts['classes'] . '" data-do-default="' . esc_attr( $atts['do_default'] ) . '">'; $return .= PUM_Helpers::do_shortcode( $content ); $return .= ''; + $popup = pum_get_popup( $atts['id'] ); + if ( $popup->is_enabled() ) { + PUM_Site_Popups::preload_popup( $popup ); + } + return $return; } From a6ac952b976a2bdcb431c225686c310dfca32822 Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 12:09:38 -0500 Subject: [PATCH 5/8] Abstract out preloading popup by ID Since we are using this code in several places, and may add more, lets create its own method for this. --- classes/Shortcode/PopupTrigger.php | 5 +---- classes/Site/Popups.php | 19 +++++++++++++++---- includes/modules/menus.php | 5 +---- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/classes/Shortcode/PopupTrigger.php b/classes/Shortcode/PopupTrigger.php index 26ac79521..c90af5457 100644 --- a/classes/Shortcode/PopupTrigger.php +++ b/classes/Shortcode/PopupTrigger.php @@ -146,10 +146,7 @@ public function handler( $atts, $content = null ) { $return .= PUM_Helpers::do_shortcode( $content ); $return .= ''; - $popup = pum_get_popup( $atts['id'] ); - if ( $popup->is_enabled() ) { - PUM_Site_Popups::preload_popup( $popup ); - } + PUM_Site_Popups::preload_popup_by_id_if_enabled( $atts['id'] ); return $return; } diff --git a/classes/Site/Popups.php b/classes/Site/Popups.php index f8cd7d770..f79b48011 100644 --- a/classes/Site/Popups.php +++ b/classes/Site/Popups.php @@ -120,6 +120,7 @@ public static function load_popups() { * * @param string $content The content from the filter. * @return string The content. + * @since 1.15 */ public static function check_content_for_popups( $content ) { /** @@ -131,14 +132,24 @@ public static function check_content_for_popups( $content ) { // Then, if we find any popups, let's preload it. foreach ( $matches[1] as $popup_id ) { - $popup = pum_get_popup( $popup_id ); - if ( $popup->is_enabled() ) { - self::preload_popup( $popup ); - } + self::preload_popup_by_id_if_enabled( $popup_id ); } return $content; } + /** + * Preloads popup, if enabled + * + * @param int $popup_id The popup's ID. + * @since 1.15 + */ + public static function preload_popup_by_id_if_enabled( $popup_id ) { + $popup = pum_get_popup( $popup_id ); + if ( $popup->is_enabled() ) { + self::preload_popup( $popup ); + } + } + /** * @param PUM_Model_Popup $popup */ diff --git a/includes/modules/menus.php b/includes/modules/menus.php index cd789d759..f1156a847 100644 --- a/includes/modules/menus.php +++ b/includes/modules/menus.php @@ -115,10 +115,7 @@ public static function merge_item_data( $item ) { } if ( isset( $item->popup_id ) ) { - $popup = pum_get_popup( $item->popup_id ); - if ( $popup->is_enabled() ) { - PUM_Site_Popups::preload_popup( $popup ); - } + PUM_Site_Popups::preload_popup_by_id_if_enabled( $item->popup_id ); $item->classes[] = 'popmake-' . $item->popup_id; } From 2805d9435bd259099feb18ab95241963c49192fa Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 12:14:12 -0500 Subject: [PATCH 6/8] Ensure popup is only loaded once The preload_popup method will add multiple instances of the popup code which results in flashes and multiple open/closes which prevents the popup from opening. Add conditional to make sure the popup isn't already loaded. --- classes/Site/Popups.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/classes/Site/Popups.php b/classes/Site/Popups.php index f79b48011..ab198c788 100644 --- a/classes/Site/Popups.php +++ b/classes/Site/Popups.php @@ -144,9 +144,11 @@ public static function check_content_for_popups( $content ) { * @since 1.15 */ public static function preload_popup_by_id_if_enabled( $popup_id ) { - $popup = pum_get_popup( $popup_id ); - if ( $popup->is_enabled() ) { - self::preload_popup( $popup ); + if ( ! in_array( $popup_id, self::$loaded_ids ) ) { + $popup = pum_get_popup( $popup_id ); + if ( $popup->is_enabled() ) { + self::preload_popup( $popup ); + } } } From 846647d45b7417a9e7da3e8505e3b605a83af45e Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Tue, 22 Dec 2020 12:52:02 -0500 Subject: [PATCH 7/8] Improve menu item check for preloading popups The original solution only caught when they used our popup setting for triggering popups. But, they can also add the popmake- class instead. So, we now check the classes after we set ours. --- includes/modules/menus.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/includes/modules/menus.php b/includes/modules/menus.php index f1156a847..8f43a5f1c 100644 --- a/includes/modules/menus.php +++ b/includes/modules/menus.php @@ -115,10 +115,22 @@ public static function merge_item_data( $item ) { } if ( isset( $item->popup_id ) ) { - PUM_Site_Popups::preload_popup_by_id_if_enabled( $item->popup_id ); $item->classes[] = 'popmake-' . $item->popup_id; } + /** + * Check menu item's classes for popmake-###. Do this after the above conditional to catch the class we add too. + * Tested both using strpos followed by preg_match as well as just doing preg_match on all and this solution + * was just a tiny bit faster. But, if a site has 100 menu items, that tiny difference will add up. + */ + foreach ( $item->classes as $class ) { + if ( strpos( $class, 'popmake-' ) !== false ) { + if ( 0 !== preg_match( '/popmake-(\d+)/', $class, $matches ) ) { + PUM_Site_Popups::preload_popup_by_id_if_enabled( $matches[1] ); + } + } + } + return $item; } From 0d74d5f2bfc91f96bfdaf722e41b975b1dfcc36d Mon Sep 17 00:00:00 2001 From: Frank Corso Date: Mon, 28 Dec 2020 11:55:24 -0500 Subject: [PATCH 8/8] Ensure our popup scan only happens in main query Issue #543 --- classes/Site/Popups.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/classes/Site/Popups.php b/classes/Site/Popups.php index ab198c788..4076cec00 100644 --- a/classes/Site/Popups.php +++ b/classes/Site/Popups.php @@ -123,17 +123,22 @@ public static function load_popups() { * @since 1.15 */ public static function check_content_for_popups( $content ) { - /** - * We want to detect instances of popmake-### but only within classes and not in the actual text. - * So, we check to make sure it is wrapped by quotes to make sure it's in the class="" attribute - * but also allow for whitespace and characters in case there are classes before or after it. - */ - preg_match_all( '/[\'\"][\s\w\-\_]*?popmake-(\d+)[\s\w\-\_]*?[\'\"]/', $content, $matches ); - - // Then, if we find any popups, let's preload it. - foreach ( $matches[1] as $popup_id ) { - self::preload_popup_by_id_if_enabled( $popup_id ); + + // Only search for popups in the main query of a singular page. + if ( is_singular() && in_the_loop() && is_main_query() ) { + /** + * We want to detect instances of popmake-### but only within classes and not in the actual text. + * So, we check to make sure it is wrapped by quotes to make sure it's in the class="" attribute + * but also allow for whitespace and characters in case there are classes before or after it. + */ + preg_match_all( '/[\'\"][\s\w\-\_]*?popmake-(\d+)[\s\w\-\_]*?[\'\"]/', $content, $matches ); + + // Then, if we find any popups, let's preload it. + foreach ( $matches[1] as $popup_id ) { + self::preload_popup_by_id_if_enabled( $popup_id ); + } } + return $content; }