addons
2 months ago
templates
2 months ago
check_init.php
2 months ago
feature_tab.php
2 months ago
popup.php
2 months ago
tippy.php
2 months ago
tutorial.php
2 months ago
wpml_compatibility.php
2 months ago
popup.php
189 lines
| 1 | <?php |
| 2 | if( ! class_exists('BeRocket_popup_display') ) { |
| 3 | class BeRocket_popup_display { |
| 4 | public static $elements = array(); |
| 5 | public static $load_popup = false; |
| 6 | function __construct() { |
| 7 | add_action('wp_footer', array($this, 'wp_footer'), 9); |
| 8 | add_action('wp_footer', array($this, 'wp_footer2'), 90000); |
| 9 | add_action('admin_footer', array($this, 'wp_footer'), 9); |
| 10 | add_action('admin_footer', array($this, 'wp_footer2'), 90000); |
| 11 | $open_types = array('click', 'page_open', 'scroll_px', 'scroll_block', 'leave_page', 'event'); |
| 12 | foreach($open_types as $open_type) { |
| 13 | add_filter('BeRocket_popup_open_type_'.$open_type, array($this, 'popup_open_type_'.$open_type), 10, 5); |
| 14 | } |
| 15 | } |
| 16 | public static function include_assets() { |
| 17 | self::$load_popup = true; |
| 18 | } |
| 19 | public static function add_popup($options, $html = '', $popup_open = false) { |
| 20 | self::include_assets(); |
| 21 | if( count(self::$elements) ) { |
| 22 | $max_id = array_keys(self::$elements); |
| 23 | $max_id = max($max_id); |
| 24 | $max_id++; |
| 25 | } else { |
| 26 | $max_id = 1; |
| 27 | } |
| 28 | self::$elements[$max_id] = array( |
| 29 | 'popup_open' => $popup_open, |
| 30 | 'options' => $options, |
| 31 | 'html' => $html |
| 32 | ); |
| 33 | return $max_id; |
| 34 | } |
| 35 | public function wp_footer() { |
| 36 | if( self::$load_popup ) { |
| 37 | wp_register_script( |
| 38 | 'berocket_framework_popup', |
| 39 | plugins_url( '../assets/popup/br_popup.js', __FILE__ ), |
| 40 | array( 'jquery' ) |
| 41 | ); |
| 42 | wp_register_style( |
| 43 | 'berocket_framework_popup-animate', |
| 44 | plugins_url( '../assets/popup/animate.css', __FILE__ ) |
| 45 | ); |
| 46 | wp_register_style( |
| 47 | 'berocket_framework_popup', |
| 48 | plugins_url( '../assets/popup/br_popup.css', __FILE__ ), |
| 49 | array('berocket_framework_popup-animate') |
| 50 | ); |
| 51 | wp_enqueue_script( 'berocket_framework_popup' ); |
| 52 | wp_enqueue_style( 'berocket_framework_popup-animate' ); |
| 53 | wp_enqueue_style( 'berocket_framework_popup' ); |
| 54 | } |
| 55 | } |
| 56 | public function wp_footer2() { |
| 57 | if( count(self::$elements) ) { |
| 58 | $page_elements = array( |
| 59 | 'html_content' => '', |
| 60 | 'ajax_update' => '', |
| 61 | 'page_load' => '' |
| 62 | ); |
| 63 | $popup_list = array(); |
| 64 | foreach(self::$elements as $element_i => $element) { |
| 65 | $element_id = 'br_popup_'.$element_i; |
| 66 | //ADD BLOCK WITH CONTENT |
| 67 | $page_elements['html_content'] .= '<div id="'.$element_id.'" style="display: none!important;">'.$element['html'].'</div>'; |
| 68 | //ADD SCRIPT TO INIT POPUP |
| 69 | $page_elements['ajax_update'] .= ' |
| 70 | jQuery("#'.$element_id.'").br_popup('.json_encode($element['options']).');'; |
| 71 | //ADD SCRIPT FOR BUTTONS |
| 72 | if( ! empty($element['popup_open']) && is_array($element['popup_open']) && count($element['popup_open']) ) { |
| 73 | if( ! empty($element['popup_open']['type']) ) { |
| 74 | $element['popup_open'] = array($element['popup_open']); |
| 75 | } |
| 76 | foreach($element['popup_open'] as $popup_open) { |
| 77 | if( is_array($popup_open) && ! empty($popup_open['type']) ) { |
| 78 | $page_elements = apply_filters('BeRocket_popup_open_type_'.$popup_open['type'], $page_elements, $popup_open, $element, $element_i, $element_id); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | $page_elements = apply_filters('BeRocket_popup_open_page_elements', $page_elements, self::$elements); |
| 84 | echo $page_elements['html_content']; |
| 85 | echo '<script> |
| 86 | var berocket_popup_run_script_timeouts = 0; |
| 87 | function berocket_popup_run_script() { |
| 88 | '.$page_elements['ajax_update'].' |
| 89 | } |
| 90 | jQuery(document).ready(function() { |
| 91 | berocket_popup_run_script(); |
| 92 | jQuery(document).ajaxComplete(function() { |
| 93 | berocket_popup_run_script(); |
| 94 | }); |
| 95 | '.$page_elements['page_load'].' |
| 96 | }); |
| 97 | </script>'; |
| 98 | } |
| 99 | } |
| 100 | public function popup_open_type_click($page_elements, $popup_open, $element, $element_i, $element_id) { |
| 101 | if( ! empty($popup_open['selector']) ) { |
| 102 | $page_elements['ajax_update'] .= ' |
| 103 | if( jQuery("'.$popup_open['selector'].'").length == 0 && berocket_popup_run_script_timeouts < 4 ) { |
| 104 | setTimeout(berocket_popup_run_script, 1000); |
| 105 | berocket_popup_run_script_timeouts++; |
| 106 | } else { |
| 107 | berocket_popup_run_script_timeouts = 0; |
| 108 | if( ! jQuery("'.$popup_open['selector'].'").data("br_popup_event") ) { |
| 109 | jQuery("'.$popup_open['selector'].'").data("br_popup_event", true); |
| 110 | jQuery("'.$popup_open['selector'].'").on("click", function(event) { |
| 111 | event.preventDefault(); |
| 112 | jQuery("#'.$element_id.'").br_popup().open_popup(); |
| 113 | }); |
| 114 | } |
| 115 | }'; |
| 116 | } |
| 117 | return $page_elements; |
| 118 | } |
| 119 | public function popup_open_type_page_open($page_elements, $popup_open, $element, $element_i, $element_id) { |
| 120 | $popup_open_script = 'jQuery("#'.$element_id.'").br_popup().open_popup();'; |
| 121 | if( empty($popup_open['timer']) ) { |
| 122 | $page_elements['page_load'] .= $popup_open_script; |
| 123 | } else { |
| 124 | $page_elements['page_load'] .= 'setTimeout(function() {'.$popup_open_script.'}, '.$popup_open['timer'].');'; |
| 125 | } |
| 126 | return $page_elements; |
| 127 | } |
| 128 | public function popup_open_type_scroll_px($page_elements, $popup_open, $element, $element_i, $element_id) { |
| 129 | $popup_open = array_merge(array( |
| 130 | 'scroll' => '0' |
| 131 | ), $popup_open); |
| 132 | $function_name = 'berocket_popup_open_type_scroll_px_'.$element_i.'_'.$popup_open['scroll']; |
| 133 | $page_elements['page_load'] .= ' |
| 134 | function '.$function_name.'() { |
| 135 | if( ! jQuery("#'.$element_id.'").data("scroll_px_'.$popup_open['scroll'].'") && jQuery(document).scrollTop() > '.$popup_open['scroll'].' ) { |
| 136 | jQuery("#'.$element_id.'").br_popup().open_popup(); |
| 137 | jQuery("#'.$element_id.'").data("scroll_px_'.$popup_open['scroll'].'", true); |
| 138 | } |
| 139 | } |
| 140 | '.$function_name.'(); |
| 141 | jQuery(document).on("scroll", '.$function_name.');'; |
| 142 | return $page_elements; |
| 143 | } |
| 144 | public function popup_open_type_scroll_block($page_elements, $popup_open, $element, $element_i, $element_id) { |
| 145 | $popup_open = array_merge(array( |
| 146 | 'selector' => '' |
| 147 | ), $popup_open); |
| 148 | $md5hash = md5($popup_open['selector']); |
| 149 | $function_name = 'berocket_popup_open_type_scroll_px_'.$element_i.'_'.$md5hash; |
| 150 | if( ! empty($popup_open['selector']) ) { |
| 151 | $page_elements['page_load'] .= ' |
| 152 | function '.$function_name.'() { |
| 153 | if( jQuery("'.$popup_open['selector'].'").filter(":visible").length && ! jQuery("#'.$element_id.'").data("scroll_block_'.$md5hash.'") ) { |
| 154 | var window_pos = jQuery(document).scrollTop()+jQuery(window).height(); |
| 155 | var block_pos = jQuery("'.$popup_open['selector'].'").filter(":visible").offset().top; |
| 156 | if( window_pos > block_pos ) { |
| 157 | jQuery("#'.$element_id.'").br_popup().open_popup(); |
| 158 | jQuery("#'.$element_id.'").data("scroll_block_'.$md5hash.'", true); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | '.$function_name.'(); |
| 163 | jQuery(document).on("scroll", '.$function_name.');'; |
| 164 | } |
| 165 | return $page_elements; |
| 166 | } |
| 167 | public function popup_open_type_leave_page($page_elements, $popup_open, $element, $element_i, $element_id) { |
| 168 | $page_elements['page_load'] .= ' |
| 169 | jQuery(document).mouseleave(function(event){ |
| 170 | if( ! jQuery("#'.$element_id.'").data("leave_page_'.$element_i.'") && event.clientY < 50 ) { |
| 171 | jQuery("#'.$element_id.'").br_popup().open_popup(); |
| 172 | jQuery("#'.$element_id.'").data("leave_page_'.$element_i.'", true); |
| 173 | } |
| 174 | });'; |
| 175 | return $page_elements; |
| 176 | } |
| 177 | function popup_open_type_event($page_elements, $popup_open, $element, $element_i, $element_id) { |
| 178 | if( ! empty($popup_open['event']) ) { |
| 179 | $page_elements['page_load'] .= ' |
| 180 | jQuery(document).on("'.$popup_open['event'].'", function () { |
| 181 | jQuery("#'.$element_id.'").br_popup().open_popup(); |
| 182 | });'; |
| 183 | } |
| 184 | return $page_elements; |
| 185 | } |
| 186 | } |
| 187 | new BeRocket_popup_display(); |
| 188 | } |
| 189 |