(function($){ $(document).ready(function() { var $locked_containers = []; $( '.et_bloom_make_form_visible' ).removeAttr( 'style' ); $( '.et_bloom_custom_html_form input[type="radio"], .et_bloom_custom_html_form input[type="checkbox"]' ).uniform(); $( 'body' ).on( 'click', 'span.et_bloom_close_button', function(){ perform_popup_closing( $( this ).closest( '.et_bloom_optin' ) ); return false; }); function toggle_checkbox(element, value) { $(element).children('i').toggleClass('et_bloom_icon et_bloom_icon_check'); } $('body .et_bloom_custom_field_checkbox input[type=checkbox]:checked').each(function() { toggle_checkbox($(this).next('label')); }); $('body').on('click', '.et_bloom_custom_field_checkbox label', function() { toggle_checkbox(this); }); function perform_popup_closing( $popup_container ) { $popup_container.addClass( 'et_bloom_exit_animation' ); setTimeout( function() { if ( $popup_container.hasClass( 'et_bloom_trigger_click' ) ) { $popup_container.removeClass( 'et_bloom_visible et_bloom_animated et_bloom_exit_animation' ); } else { $popup_container.remove(); } }, 400 ); $( 'body' ).removeClass( 'et_bloom_popup_active' ); } function update_stats_table( type, $this_button ) { // do not update stats if visitor logged in if ( 'logged' === bloomSettings.is_user_logged_in ) { return; } var $optin_id = typeof $this_button.data( 'current_optin_id' ) !== 'undefined' ? $this_button.data( 'current_optin_id' ) : $this_button.data( 'optin_id' ), $page_id = $this_button.data( 'page_id' ), $list_id = $this_button.data( 'list_id' ); var cookie = 'et_bloom_optin_'.concat( $optin_id, '_', $list_id, '_', type ); if ( cookieExists( cookie ) ) { return; } $stats_data = JSON.stringify({ 'type' : type, 'optin_id' : $optin_id, 'page_id' : $page_id, 'list_id' : $list_id }); $.ajax({ type: 'POST', url: bloomSettings.ajaxurl, data: { action : 'bloom_handle_stats_adding', stats_data_array : $stats_data, update_stats_nonce : bloomSettings.stats_nonce } }).done( function() { set_cookie( 365, cookie.concat( '=true' ) ); }); } function setCookieExpire( days ) { var ms = days*24*60*60*1000; var date = new Date(); date.setTime( date.getTime() + ms ); return "; expires=" + date.toUTCString(); } function checkCookieValue( cookieName, value ) { return parseCookies()[cookieName] == value; } function cookieExists( cookie_name ) { return 'undefined' !== typeof parseCookies()[cookie_name]; } function parseCookies() { var cookies = document.cookie.split( '; ' ); var ret = {}; for ( var i = cookies.length - 1; i >= 0; i-- ) { var el = cookies[i].split( '=' ); ret[el[0]] = el[1]; } return ret; } function set_cookie( $expire, $cookie_content ) { var $cookie_content = '' == $cookie_content ? 'etBloomCookie=true' : $cookie_content; cookieExpire = setCookieExpire( $expire ); document.cookie = $cookie_content + cookieExpire + "; path=/"; } function get_url_parameter( param_name ) { var page_url = window.location.search.substring(1); var url_variables = page_url.split('&'); for ( var i = 0; i < url_variables.length; i++ ) { var curr_param_name = url_variables[i].split( '=' ); if ( curr_param_name[0] == param_name ) { return curr_param_name[1]; } } } //separate function for the setTimeout to make it work properly within the loop. function make_popup_visible( $popup, $delay, $cookie_exp, $cookie_content ){ if ( ! $popup.hasClass( 'et_bloom_visible' ) ) { setTimeout( function() { $popup.addClass( 'et_bloom_visible et_bloom_animated' ); $stats_data_container = 0 != $popup.find( '.et_bloom_custom_html_form' ).length ? $popup.find( '.et_bloom_custom_html_form' ) : $popup.find( '.et_bloom_submit_subscription' ); update_stats_table( 'imp', $stats_data_container ); if ( '' != $cookie_exp ) { set_cookie( $cookie_exp, $cookie_content ); } if ( $( '.et_bloom_resize' ).length ) { $( '.et_bloom_resize.et_bloom_visible' ).each( function() { define_popup_position( $( this ), true, 0 ); }); } display_image( $popup ); }, $delay ); } } function display_image( $popup ) { setTimeout( function() { $popup.find( '.et_bloom_image' ).addClass( 'et_bloom_visible_image' ); }, 500 ); } function auto_popup( $current_popup_auto, $delay ) { var $data_holder = $current_popup_auto.find( '.et_bloom_custom_html_form' ).length ? $current_popup_auto.find( '.et_bloom_custom_html_form' ) : $current_popup_auto.find( '.et_bloom_submit_subscription' ), page_id = $data_holder.data( 'page_id' ), optin_id = $data_holder.data( 'optin_id' ), list_id = $data_holder.data( 'list_id' ); if ( ! $current_popup_auto.hasClass( 'et_bloom_animated' ) ) { var $cookies_expire_auto = $current_popup_auto.data( 'cookie_duration' ) ? $current_popup_auto.data( 'cookie_duration' ) : false, $already_subscribed = checkCookieValue( 'et_bloom_subscribed_to_' + optin_id + list_id, 'true' ); if ( ( ( false !== $cookies_expire_auto && ! checkCookieValue( 'etBloomCookie_' + optin_id, 'true' ) ) || false == $cookies_expire_auto ) && ! $already_subscribed ) { if ( false !== $cookies_expire_auto ) { make_popup_visible ( $current_popup_auto, $delay, $cookies_expire_auto, 'etBloomCookie_' + optin_id + '=true' ); } else { make_popup_visible ( $current_popup_auto, $delay, '', '' ); } } } } function scroll_trigger( $current_popup_bottom, is_bottom_trigger ) { var triggered = 0, $data_holder = $current_popup_bottom.find( '.et_bloom_custom_html_form' ).length ? $current_popup_bottom.find( '.et_bloom_custom_html_form' ) : $current_popup_bottom.find( '.et_bloom_submit_subscription' ), page_id = $data_holder.data( 'page_id' ), optin_id = $data_holder.data( 'optin_id' ); list_id = $data_holder.data( 'list_id' ); if ( ! $current_popup_bottom.hasClass( 'et_bloom_animated' ) ) { var cookies_expire_bottom = $current_popup_bottom.data( 'cookie_duration' ) ? $current_popup_bottom.data( 'cookie_duration' ) : false, $already_subscribed = checkCookieValue( 'et_bloom_subscribed_to_' + optin_id + list_id, 'true' ); if ( true == is_bottom_trigger ) { var scroll_trigger = $( '.et_bloom_bottom_trigger' ).length ? $( '.et_bloom_bottom_trigger' ).offset().top : $( document ).height() - 500; } else { var scroll_pos = $current_popup_bottom.data( 'scroll_pos' ) > 100 ? 100 : $current_popup_bottom.data( 'scroll_pos' ), scroll_trigger = 100 == scroll_pos ? $( document ).height() - 50 : $( document ).height() * scroll_pos / 100; } $( window ).scroll( function(){ if ( ( ( false !== cookies_expire_bottom && ! checkCookieValue( 'etBloomCookie_' + optin_id, 'true' ) ) || false == cookies_expire_bottom ) && ! $already_subscribed ) { if( $( window ).scrollTop() + $( window ).height() > scroll_trigger ) { if ( 0 == triggered ) { if ( false !== cookies_expire_bottom ) { make_popup_visible ( $current_popup_bottom, 0, cookies_expire_bottom, 'etBloomCookie_' + optin_id + '=true' ); } else { make_popup_visible ( $current_popup_bottom, 0, '', '' ); } triggered++; } } } }); } } $.fn.isInViewport = function() { var elementTop = $( this ).offset().top; var elementBottom = elementTop + $( this ).outerHeight(); var viewportTop = $( window ).scrollTop(); var viewportBottom = viewportTop + $( window ).height(); return elementBottom > viewportTop && elementTop < viewportBottom; }; var $inline_optins = $( '.et_bloom_inline_form .et_bloom_submit_subscription, .et_bloom_widget_content .et_bloom_submit_subscription, .et_bloom_custom_html_form' ); if ( $inline_optins.length > 0 ) { var imp_recorded_count = 0; $(window).on( 'scroll.et_bloom_impressions', function() { $inline_optins.each( function() { if ( ! $(this).hasClass( 'et_bloom_impression_recorded' ) && $(this).isInViewport() ) { $( this ).addClass( 'et_bloom_impression_recorded' ); imp_recorded_count++; update_stats_table( 'imp', $(this) ); } } ); if ( imp_recorded_count >= $inline_optins.length ) { $(window).off( 'scroll.et_bloom_impressions' ); } } ); } if( $( '.et_bloom_auto_popup' ).length ) { $( '.et_bloom_auto_popup:not(.et_bloom_visible)' ).each( function() { var this_el = $( this ), delay = '' !== this_el.data( 'delay' ) ? this_el.data( 'delay' ) * 1000 : 0; auto_popup( this_el, delay ); }); } if( $( '.et_bloom_trigger_bottom' ).length ) { $( '.et_bloom_trigger_bottom:not(.et_bloom_visible)' ).each( function(){ scroll_trigger( $( this ), true ); }); } if( $( '.et_bloom_scroll' ).length ) { $( '.et_bloom_scroll:not(.et_bloom_visible)' ).each( function(){ scroll_trigger( $( this ), false ); }); } if ( $( '.et_bloom_trigger_click' ).length ) { $( '.et_bloom_trigger_click:not(.et_bloom_visible)' ).each( function() { var $this_el = $( this ), selector = $this_el.attr( 'data-trigger_click' ); if ( typeof selector !== 'undefined' ) { $( 'body' ).on( 'click', selector, function() { make_popup_visible ( $this_el, 0, '', '' ); return false; }); } }); } if( $( '.et_bloom_trigger_idle' ).length ) { $( '.et_bloom_trigger_idle:not(.et_bloom_visible)' ).each( function() { var $this_el = $( this ), $data_holder = $this_el.find( '.et_bloom_custom_html_form' ).length ? $this_el.find( '.et_bloom_custom_html_form' ) : $this_el.find( '.et_bloom_submit_subscription' ), page_id = $data_holder.data( 'page_id' ), optin_id = $data_holder.data( 'optin_id' ), list_id = $data_holder.data( 'list_id' ); if ( ! $this_el.hasClass( 'et_bloom_animated' ) ) { var $cookies_expire_idle = $this_el.data( 'cookie_duration' ) ? $this_el.data( 'cookie_duration' ) : false, $already_subscribed = checkCookieValue( 'et_bloom_subscribed_to_' + optin_id + list_id, 'true' ); $idle_timeout = '' !== $this_el.data( 'idle_timeout' ) ? $this_el.data( 'idle_timeout' ) * 1000 : 30000, $delay = 0; if ( ( ( false !== $cookies_expire_idle && ! checkCookieValue( 'etBloomCookie_' + optin_id, 'true' ) ) || false == $cookies_expire_idle ) && ! $already_subscribed ) { $( document ).idleTimer( $idle_timeout ); $( document ).on( 'idle.idleTimer', function() { if ( false !== $cookies_expire_idle ) { make_popup_visible ( $this_el, $delay, $cookies_expire_idle, 'etBloomCookie_' + optin_id + '=true' ); } else { make_popup_visible ( $this_el, $delay, '', '' ); } }); } } }); } if ( 'true' == get_url_parameter( 'et_bloom_popup' ) ) { $( '.et_bloom_after_comment' ).each( function() { auto_popup( $( this ), 0 ); }); } if ( $( '.et_bloom_after_order' ).length ) { $( '.et_bloom_after_purchase' ).each( function() { auto_popup( $( this ), 0 ); }); } if( $( '.et_bloom_locked_container' ).length ) { var $i = 0; $( '.et_bloom_locked_container' ).each( function() { var $this_el = $( this ), content = $this_el.find( '.et_bloom_locked_content' ), form = $this_el.find( '.et_bloom_locked_form' ), page_id = $this_el.data( 'page_id' ), optin_id = $this_el.data( 'optin_id' ); $this_el.data( 'container_id', $i ); $locked_containers.push( content ); if ( checkCookieValue( 'et_bloom_unlocked' + optin_id + page_id, 'true' ) ) { content.css( {'display' : 'block'} ); form.remove(); } else { content.remove(); update_stats_table( 'imp', $this_el ); } $i++; }); } $( 'body' ).on( 'click', '.et_bloom_locked_container .et_bloom_submit_subscription', function(){ var $current_container = $( this ).closest( '.et_bloom_locked_container' ), container_id = $current_container.data( 'container_id' ), page_id = $current_container.data( 'page_id' ), optin_id = typeof $current_container.data( 'current_optin_id' ) !== 'undefined' ? $current_container.data( 'current_optin_id' ) : $current_container.data( 'optin_id' ); perform_subscription( $( this ), $current_container, container_id, page_id, optin_id ); return false; }); // unlock content immediately if custom HTML form is used. $( 'body' ).on( 'click', '.et_bloom_locked_container .et_bloom_custom_html_form input[type="submit"], .et_bloom_locked_container .et_bloom_custom_html_form button[type="submit"]', function() { var current_container = $( this ).closest( '.et_bloom_locked_container' ), container_id = current_container.data( 'container_id' ), page_id = current_container.data( 'page_id' ), optin_id = current_container.data( 'optin_id' ); unlock_content( current_container, container_id, page_id, optin_id ); } ); function unlock_content( current_container, container_id, locked_page_id, locked_optin_id ) { set_cookie( 365, 'et_bloom_unlocked' + locked_optin_id + locked_page_id + '=true' ); current_container.find( '.et_bloom_locked_form' ).replaceWith( $locked_containers[container_id] ); current_container.find( '.et_bloom_locked_content' ).css( { 'display' : 'block' } ); } // Move inline forms into appropriate sections in Divi theme if( $( '.et_bloom_below_post' ).length ) { if ( $( 'body' ).hasClass( 'et_pb_pagebuilder_layout' ) ) { var bottom_inline = $( '.et_bloom_below_post' ), divi_container = '