Skip to content

Commit

Permalink
Merge pull request #926 from PopupMaker/feature/543
Browse files Browse the repository at this point in the history
Automagically load popups when needed
  • Loading branch information
fpcorso authored Dec 30, 2020
2 parents b8587a8 + 1203353 commit 484834e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
8 changes: 4 additions & 4 deletions classes/Shortcode/PopupTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ 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 .= '</' . $atts['tag'] . '>';

PUM_Site_Popups::preload_popup_by_id_if_enabled( $atts['id'] );

return $return;
}

Expand Down
44 changes: 44 additions & 0 deletions classes/Site/Popups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -110,7 +113,48 @@ 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.
* @since 1.15
*/
public static function check_content_for_popups( $content ) {

// 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;
}

/**
* 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 ) {
if ( ! in_array( $popup_id, self::$loaded_ids ) ) {
$popup = pum_get_popup( $popup_id );
if ( $popup->is_enabled() ) {
self::preload_popup( $popup );
}
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions includes/modules/menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ public static function merge_item_data( $item ) {
$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;
}

Expand Down

0 comments on commit 484834e

Please sign in to comment.