wpr-conditions-manager.php
5 days ago
wpr-editor-hooks.php
5 days ago
wpr-render-templates.php
5 days ago
wpr-templates-actions.php
5 days ago
wpr-templates-category-filter.php
5 days ago
wpr-templates-library.php
5 days ago
wpr-templates-loop.php
5 days ago
wpr-templates-modal-popups.php
5 days ago
wpr-templates-shortcode.php
5 days ago
wpr-templates-modal-popups.php
249 lines
| 1 | <?php |
| 2 | namespace WprAddons\Admin\Includes; |
| 3 | |
| 4 | use WprAddons\Plugin; |
| 5 | use WprAddons\Classes\Utilities; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly. |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * WPR_Templates_Modal_Popups setup |
| 13 | * |
| 14 | * @since 1.0 |
| 15 | */ |
| 16 | class WPR_Templates_Modal_Popups { |
| 17 | |
| 18 | /** |
| 19 | ** Instance of Elemenntor Frontend class. |
| 20 | * |
| 21 | ** @var \Elementor\Frontend() |
| 22 | */ |
| 23 | private static $elementor_instance; |
| 24 | |
| 25 | /** |
| 26 | ** Constructor |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | // Elementor Frontend |
| 30 | self::$elementor_instance = \Elementor\Plugin::instance(); |
| 31 | |
| 32 | add_action( 'template_include', [ $this, 'set_post_type_template' ], 9999 ); |
| 33 | |
| 34 | add_action( 'wp_footer', [ $this, 'render_popups' ] ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Set blank template for editor |
| 39 | */ |
| 40 | public function set_post_type_template( $template ) { |
| 41 | |
| 42 | if ( is_singular( 'wpr_templates' ) ) { |
| 43 | if ( 'wpr-popups' === Utilities::get_elementor_template_type(get_the_ID()) && self::$elementor_instance->preview->is_preview_mode() ) { |
| 44 | $template = WPR_ADDONS_PATH . 'modules/popup/editor.php'; |
| 45 | } |
| 46 | |
| 47 | return $template; |
| 48 | } |
| 49 | |
| 50 | return $template; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | ** Popups |
| 55 | */ |
| 56 | public function render_popups() { |
| 57 | $conditions = json_decode( get_option('wpr_popup_conditions'), true ); |
| 58 | |
| 59 | if ( ! empty( $conditions ) ) { |
| 60 | $conditions = $this->reverse_template_conditions( $conditions ); |
| 61 | |
| 62 | // Global |
| 63 | if ( isset( $conditions['global'] ) ) { |
| 64 | WPR_Templates_Modal_Popups::display_popups_by_location( $conditions, 'global' ); |
| 65 | } |
| 66 | |
| 67 | // Custom |
| 68 | if ( defined('WPR_ADDONS_PRO_VERSION') && wpr_fs()->can_use_premium_code() ) { |
| 69 | // Archive |
| 70 | \WprAddonsPro\Classes\Pro_Modules::archive_pages_popup_conditions( $conditions ); |
| 71 | |
| 72 | // Single |
| 73 | \WprAddonsPro\Classes\Pro_Modules::single_pages_popup_conditions( $conditions ); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // Enqueue ScrolBar JS //TODO - check if displayed multiple times |
| 78 | wp_enqueue_script( 'wpr-popup-scroll-js', WPR_ADDONS_URL .'assets/js/lib/perfect-scrollbar/perfect-scrollbar.min.js', [ 'jquery' ], '0.4.9' ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | ** Reverse Template Conditions |
| 84 | */ |
| 85 | public function reverse_template_conditions( $conditions ) { |
| 86 | $reverse = []; |
| 87 | |
| 88 | foreach ( $conditions as $key => $condition ) { |
| 89 | foreach( $condition as $location ) { |
| 90 | if ( ! isset( $reverse[$location] ) ) { |
| 91 | $reverse[$location] = [ $key ]; |
| 92 | } else { |
| 93 | array_push( $reverse[$location], $key ); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $reverse; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | ** Display Popups by Location |
| 103 | */ |
| 104 | public static function display_popups_by_location( $conditions, $page ) { |
| 105 | foreach ( $conditions[$page] as $key => $popup ) { |
| 106 | WPR_Templates_Modal_Popups::render_popup_content( $popup ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | ** Display Elementor Content |
| 112 | */ |
| 113 | public static function render_popup_content( $slug ) { |
| 114 | $template_name = ''; |
| 115 | |
| 116 | $template_id = Utilities::get_template_id( $slug ); |
| 117 | |
| 118 | if ( defined('ICL_LANGUAGE_CODE') ) { |
| 119 | $default_language_code = apply_filters('wpml_default_language', null); |
| 120 | $current_language_code = apply_filters( 'wpml_current_language', NULL ); |
| 121 | |
| 122 | |
| 123 | IF ( ICL_LANGUAGE_CODE !== $default_language_code ) { |
| 124 | $template_id = apply_filters('wpml_object_id', $template_id, 'wpr_templates', true, $current_language_code); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | $get_settings = WPR_Templates_Modal_Popups::get_template_settings( $slug ); |
| 129 | $get_elementor_content = self::$elementor_instance->frontend->get_builder_content( $template_id, false ); |
| 130 | |
| 131 | if ( '' === $get_elementor_content ) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // Encode Settings |
| 136 | $get_encoded_settings = ! empty( $get_settings ) ? wp_json_encode( $get_settings ) : '[]'; |
| 137 | |
| 138 | // Template Settings Attribute |
| 139 | $template_settings_attr = "data-settings='". esc_attr($get_encoded_settings) ."'"; |
| 140 | |
| 141 | // Return if NOT available for current user |
| 142 | if ( ! WPR_Templates_Modal_Popups::check_available_user_roles( $get_settings['popup_show_for_roles'] ) ) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | if ( ! self::$elementor_instance->preview->is_preview_mode() ) { |
| 147 | echo '<div id="wpr-popup-id-'. esc_attr($template_id) .'" class="wpr-template-popup" '. $template_settings_attr .'>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 148 | echo '<div class="wpr-template-popup-inner">'; |
| 149 | |
| 150 | // Popup Overlay & Close Button |
| 151 | echo '<div class="wpr-popup-overlay"></div>'; |
| 152 | |
| 153 | // Template Container |
| 154 | echo '<div class="wpr-popup-container">'; |
| 155 | |
| 156 | // Close Button |
| 157 | if ( \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_font_icon_svg' ) ) { // GOGA: why not included |
| 158 | echo '<div class="wpr-popup-close-btn"><i class="fa fa-times"></i></div>'; |
| 159 | } else { |
| 160 | echo '<div class="wpr-popup-close-btn"><i class="eicon-close"></i></div>'; |
| 161 | } |
| 162 | |
| 163 | // Elementor Template Content |
| 164 | echo '<div class="wpr-popup-container-inner">'; |
| 165 | echo ''. $get_elementor_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 166 | echo '</div>'; |
| 167 | |
| 168 | echo '</div>'; |
| 169 | |
| 170 | echo '</div>'; |
| 171 | echo '</div>'; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | ** Get Template Settings |
| 177 | */ |
| 178 | public static function get_template_settings( $slug ) { |
| 179 | $settings = []; |
| 180 | $defaults = []; |
| 181 | |
| 182 | $template_id = Utilities::get_template_id( $slug ); |
| 183 | $meta_settings = get_post_meta( $template_id, '_elementor_page_settings', true ); |
| 184 | |
| 185 | $popup_defaults = [ |
| 186 | 'popup_trigger' => 'load', |
| 187 | 'popup_load_delay' => 1, |
| 188 | 'popup_scroll_progress' => 10, |
| 189 | 'popup_inactivity_time' => 15, |
| 190 | 'popup_element_scroll' => '', |
| 191 | 'popup_custom_trigger' => '', |
| 192 | 'popup_specific_date' => date( 'Y-m-d H:i', strtotime( '+1 month' ) + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ), |
| 193 | 'popup_stop_after_date' => false, |
| 194 | 'popup_stop_after_date_select' => date( 'Y-m-d H:i', strtotime( '+1 day' ) + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ), |
| 195 | 'popup_show_again_delay' => 1, |
| 196 | 'popup_disable_esc_key' => false, |
| 197 | 'popup_automatic_close_switch' => false, |
| 198 | 'popup_automatic_close_delay' => 10, |
| 199 | 'popup_animation' => 'fade', |
| 200 | 'popup_animation_duration' => 1, |
| 201 | 'popup_show_for_roles' => '', |
| 202 | 'popup_show_via_referral' => false, |
| 203 | 'popup_referral_keyword' => '', |
| 204 | 'popup_display_as' => 'modal', |
| 205 | 'popup_show_on_device' => true, |
| 206 | 'popup_show_on_device_mobile' => true, |
| 207 | 'popup_show_on_device_tablet' => true, |
| 208 | 'popup_disable_page_scroll' => true, |
| 209 | 'popup_overlay_disable_close' => false, |
| 210 | 'popup_close_button_display_delay' => 0, |
| 211 | ]; |
| 212 | |
| 213 | // Determine Template |
| 214 | if ( strpos( $slug, 'popup') ) { |
| 215 | $defaults = $popup_defaults; |
| 216 | } |
| 217 | |
| 218 | foreach( $defaults as $option => $value ) { |
| 219 | if ( isset($meta_settings[$option]) ) { |
| 220 | $settings[$option] = $meta_settings[$option]; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return array_merge( $defaults, $settings ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | ** Check Available User Rols |
| 229 | */ |
| 230 | public static function check_available_user_roles( $selected_roles ) { |
| 231 | if ( empty( $selected_roles ) ) { |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | $current_user = wp_get_current_user(); |
| 236 | |
| 237 | if ( ! empty( $current_user->roles ) ) { |
| 238 | $role = $current_user->roles[0]; |
| 239 | } else { |
| 240 | $role = 'guest'; |
| 241 | } |
| 242 | |
| 243 | if ( in_array( $role, $selected_roles ) ) { |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | } |