Support
2 months ago
Cross.php
2 months ago
Fallback.php
2 months ago
Fields.php
2 months ago
Helper.php
2 months ago
I18n.php
2 months ago
Plugin.php
2 months ago
Popup.php
2 months ago
PostType.php
2 months ago
Review.php
2 months ago
Settings.php
2 months ago
Shortcode.php
2 months ago
Upgrade.php
2 months ago
Popup.php
185 lines
| 1 | <?php |
| 2 | namespace NTA_WhatsApp; |
| 3 | |
| 4 | use NTA_WhatsApp\Fields; |
| 5 | use NTA_WhatsApp\PostType; |
| 6 | |
| 7 | defined( 'ABSPATH' ) || exit; |
| 8 | class Popup { |
| 9 | |
| 10 | protected static $instance = null; |
| 11 | |
| 12 | public static function getInstance() { |
| 13 | if ( null === self::$instance ) { |
| 14 | self::$instance = new self(); |
| 15 | self::$instance->doHooks(); |
| 16 | } |
| 17 | return self::$instance; |
| 18 | } |
| 19 | |
| 20 | public function __construct() { |
| 21 | } |
| 22 | |
| 23 | private function doHooks() { |
| 24 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_global_scripts_styles' ) ); |
| 25 | add_action( 'wp_footer', array( $this, 'show_widget' ) ); |
| 26 | } |
| 27 | |
| 28 | public function enqueue_global_scripts_styles() { |
| 29 | wp_register_style( 'nta-css-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/css/style.css', array(), NTA_WHATSAPP_VERSION ); |
| 30 | wp_enqueue_style( 'nta-css-popup' ); |
| 31 | wp_style_add_data( 'nta-css-popup', 'rtl', 'replace' ); |
| 32 | |
| 33 | //This base script for add_inline_script in shortcode |
| 34 | wp_enqueue_script( 'nta-wa-libs', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/js/njt-whatsapp.js', array(), NTA_WHATSAPP_VERSION, true ); |
| 35 | |
| 36 | if ( function_exists( 'wp_timezone_string' ) ) { |
| 37 | $timezone = wp_timezone_string(); |
| 38 | } else { |
| 39 | $timezone = Helper::wp_timezone_string(); |
| 40 | } |
| 41 | |
| 42 | wp_register_script( 'nta-js-global', NTA_WHATSAPP_PLUGIN_URL . 'assets/js/whatsapp-button.js', array(), NTA_WHATSAPP_VERSION, true ); |
| 43 | wp_localize_script( |
| 44 | 'nta-js-global', |
| 45 | 'njt_wa_global', |
| 46 | array( |
| 47 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 48 | 'nonce' => wp_create_nonce( 'ajax-nonce' ), |
| 49 | 'defaultAvatarSVG' => Helper::print_icon(), |
| 50 | 'defaultAvatarUrl' => NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp_logo.svg', |
| 51 | 'timezone' => $timezone, |
| 52 | 'i18n' => I18n::getTranslation(), |
| 53 | 'urlSettings' => Fields::getURLSettings(), |
| 54 | ) |
| 55 | ); |
| 56 | wp_enqueue_script( 'nta-js-global' ); |
| 57 | } |
| 58 | |
| 59 | private function shouldDisplayWidget() { |
| 60 | /** |
| 61 | * This code block prevents the display of the popup in Oxygen Builder. |
| 62 | */ |
| 63 | if ( defined( 'SHOW_CT_BUILDER' ) && ! defined( 'OXYGEN_IFRAME' ) ) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // Prevents display of the popup in Bricks Builder main builder interface. |
| 68 | if ( function_exists( 'bricks_is_builder' ) && bricks_is_builder() ) { |
| 69 | if ( function_exists( 'bricks_is_builder_main' ) && bricks_is_builder_main() ) { |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | public function show_widget() { |
| 78 | //Used to retrieve the accurate post ID when using Elementor |
| 79 | wp_reset_postdata(); |
| 80 | |
| 81 | if ( ! $this->shouldDisplayWidget() ) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | $displayOption = Fields::getWidgetDisplay(); |
| 86 | $postId = get_the_ID(); |
| 87 | |
| 88 | if ( $this->notShowInPage( $postId, $displayOption ) ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $activeAccounts = $this->get_accounts_active_and_meta(); |
| 93 | if ( count( $activeAccounts ) < 1 ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if ( wp_is_mobile() && $displayOption['showOnMobile'] === 'OFF' |
| 98 | || ! wp_is_mobile() && $displayOption['showOnDesktop'] === 'OFF' |
| 99 | || ( $displayOption['showOnMobile'] === 'OFF' && $displayOption['showOnDesktop'] === 'OFF' ) |
| 100 | ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | echo '<div id="wa"></div>'; |
| 105 | $this->enqueue_scripts_styles( $activeAccounts, $displayOption ); |
| 106 | } |
| 107 | |
| 108 | public function enqueue_scripts_styles( $activeAccounts, $displayOption ) { |
| 109 | $stylesOption = Fields::getWidgetStyles(); |
| 110 | $analyticsOption = Fields::getAnalyticsSetting(); |
| 111 | wp_register_script( 'nta-js-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/js/whatsapp-popup.js', array(), NTA_WHATSAPP_VERSION ); |
| 112 | wp_localize_script( |
| 113 | 'nta-js-popup', |
| 114 | 'njt_wa', |
| 115 | array( |
| 116 | 'gdprStatus' => Helper::checkGDPR( $stylesOption ), |
| 117 | 'accounts' => $activeAccounts, |
| 118 | 'options' => array( |
| 119 | 'display' => $displayOption, |
| 120 | 'styles' => $stylesOption, |
| 121 | 'analytics' => $analyticsOption, |
| 122 | ), |
| 123 | ) |
| 124 | ); |
| 125 | wp_enqueue_script( 'nta-js-popup' ); |
| 126 | } |
| 127 | |
| 128 | public function notShowInPage( $postId, $option ) { |
| 129 | $isPageOrShop = apply_filters( 'njt_whatsapp_is_page_or_shop_filter', is_page() ); |
| 130 | $postId = apply_filters( 'njt_whatsapp_get_post_id_filter', $postId ); |
| 131 | $showInPostTypes = apply_filters( 'njt_whatsapp_display_in_post_types', array() ); |
| 132 | |
| 133 | $postType = get_post_type( $postId ); |
| 134 | |
| 135 | $isHiddenWidget = apply_filters( 'njt_whatsapp_hide_widget', false, $postId, $postType, $isPageOrShop, $option ); |
| 136 | |
| 137 | if ( $isHiddenWidget ) { |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | if ( ! empty( $showInPostTypes ) ) { |
| 142 | if ( in_array( $postType, $showInPostTypes ) ) { |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( $option['displayCondition'] == 'showAllPage' ) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | if ( $option['displayCondition'] == 'includePages' ) { |
| 152 | if ( is_array( $option['includePages'] ) && $isPageOrShop && in_array( strval( $postId ), $option['includePages'] ) ) { |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } elseif ( $option['displayCondition'] == 'excludePages' ) { |
| 157 | if ( is_array( $option['excludePages'] ) && $isPageOrShop && in_array( strval( $postId ), $option['excludePages'] ) ) { |
| 158 | return true; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | public function get_accounts_active_and_meta() { |
| 166 | $results = array(); |
| 167 | $accounts = PostType::getInstance()->get_active_widget_accounts(); |
| 168 | foreach ( $accounts as $account ) { |
| 169 | $meta = get_post_meta( $account->ID, 'nta_wa_account_info', true ); |
| 170 | $avatar = get_the_post_thumbnail_url( $account->ID ); |
| 171 | if ( '' !== $meta ) { |
| 172 | $results[] = array_merge( |
| 173 | array( |
| 174 | 'accountId' => $account->ID, |
| 175 | 'accountName' => $account->post_title, |
| 176 | 'avatar' => $avatar !== false ? $avatar : '', |
| 177 | ), |
| 178 | $meta |
| 179 | ); |
| 180 | } |
| 181 | } |
| 182 | return $results; |
| 183 | } |
| 184 | } |
| 185 |