Recommended
3 years ago
Support
3 years ago
Cross.php
3 years ago
Fallback.php
3 years ago
Fields.php
3 years ago
Helper.php
3 years ago
I18n.php
3 years ago
Plugin.php
3 years ago
Popup.php
3 years ago
PostType.php
3 years ago
Review.php
3 years ago
Settings.php
3 years ago
Shortcode.php
3 years ago
Upgrade.php
3 years ago
Popup.php
147 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 | { |
| 14 | if (null == self::$instance) { |
| 15 | self::$instance = new self; |
| 16 | self::$instance->doHooks(); |
| 17 | } |
| 18 | return self::$instance; |
| 19 | } |
| 20 | |
| 21 | public function __construct() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | private function doHooks(){ |
| 26 | add_action('wp_enqueue_scripts', [$this, 'enqueue_global_scripts_styles']); |
| 27 | add_action('wp_footer', [$this, 'show_widget']); |
| 28 | } |
| 29 | |
| 30 | public function enqueue_global_scripts_styles(){ |
| 31 | wp_register_style('nta-css-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/css/style.css'); |
| 32 | wp_enqueue_style('nta-css-popup'); |
| 33 | wp_style_add_data('nta-css-popup', 'rtl', 'replace'); |
| 34 | |
| 35 | //This base script for add_inline_script in shortcode |
| 36 | wp_enqueue_script('nta-wa-libs', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/js/njt-whatsapp.js', [], NTA_WHATSAPP_VERSION, true); |
| 37 | |
| 38 | if ( function_exists('wp_timezone_string') ) { |
| 39 | $timezone = wp_timezone_string(); |
| 40 | } else { |
| 41 | $timezone = Helper::wp_timezone_string(); |
| 42 | } |
| 43 | |
| 44 | wp_register_script('nta-js-global', NTA_WHATSAPP_PLUGIN_URL . 'assets/js/whatsapp-button.js', [], NTA_WHATSAPP_VERSION, true); |
| 45 | wp_localize_script('nta-js-global', 'njt_wa_global', [ |
| 46 | 'ajax_url' => admin_url('admin-ajax.php'), |
| 47 | 'nonce' => wp_create_nonce('ajax-nonce'), |
| 48 | 'defaultAvatarSVG' => Helper::print_icon(), |
| 49 | 'defaultAvatarUrl' => NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp_logo.svg', |
| 50 | 'timezone' => $timezone, |
| 51 | 'i18n' => I18n::getTranslation(), |
| 52 | 'urlSettings' => Fields::getURLSettings() |
| 53 | ]); |
| 54 | wp_enqueue_script('nta-js-global'); |
| 55 | } |
| 56 | |
| 57 | public function show_widget() |
| 58 | { |
| 59 | $displayOption = Fields::getWidgetDisplay(); |
| 60 | $postId = get_the_ID(); |
| 61 | |
| 62 | if ( $this->notShowInPage($postId, $displayOption) ) return; |
| 63 | |
| 64 | $activeAccounts = $this->get_accounts_active_and_meta(); |
| 65 | if ( count($activeAccounts) < 1 ) return; |
| 66 | |
| 67 | if ( wp_is_mobile() && $displayOption['showOnMobile'] === "OFF" |
| 68 | || !wp_is_mobile() && $displayOption['showOnDesktop'] === "OFF" |
| 69 | || ( $displayOption['showOnMobile'] === "OFF" && $displayOption['showOnDesktop'] === "OFF" ) |
| 70 | ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | echo '<div id="wa"></div>'; |
| 75 | $this->enqueue_scripts_styles($activeAccounts, $displayOption); |
| 76 | } |
| 77 | |
| 78 | public function enqueue_scripts_styles($activeAccounts, $displayOption) |
| 79 | { |
| 80 | $stylesOption = Fields::getWidgetStyles(); |
| 81 | $analyticsOption = Fields::getAnalyticsSetting(); |
| 82 | wp_register_script('nta-js-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/js/whatsapp-popup.js', []); |
| 83 | wp_localize_script('nta-js-popup', 'njt_wa', [ |
| 84 | 'gdprStatus' => Helper::checkGDPR($stylesOption), |
| 85 | 'accounts' => $activeAccounts, |
| 86 | 'options' => [ |
| 87 | 'display' => $displayOption, |
| 88 | 'styles' => $stylesOption, |
| 89 | 'analytics' => $analyticsOption |
| 90 | ] |
| 91 | ]); |
| 92 | wp_enqueue_script('nta-js-popup'); |
| 93 | } |
| 94 | |
| 95 | public function notShowInPage($postId, $option) |
| 96 | { |
| 97 | $isPageOrShop = apply_filters('njt_whatsapp_is_page_or_shop_filter', is_page()); |
| 98 | $postId = apply_filters('njt_whatsapp_get_post_id_filter', $postId); |
| 99 | $showInPostTypes = apply_filters( 'njt_whatsapp_display_in_post_types', array() ); |
| 100 | |
| 101 | if ( ! empty( $showInPostTypes ) ) { |
| 102 | $post_type = get_post_type( $postId ); |
| 103 | |
| 104 | if ( in_array( $post_type, $showInPostTypes ) ) { |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if( $option['displayCondition'] == 'showAllPage' ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if ($option['displayCondition'] == 'includePages') { |
| 114 | if (is_array($option['includePages']) && $isPageOrShop && in_array(strval($postId), $option['includePages'])) { |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
| 118 | } else if ($option['displayCondition'] == 'excludePages') { |
| 119 | if (is_array($option['excludePages']) && $isPageOrShop && in_array(strval($postId), $option['excludePages'])) { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | public function get_accounts_active_and_meta(){ |
| 128 | $results = array(); |
| 129 | $accounts = PostType::getInstance()->get_active_widget_accounts(); |
| 130 | foreach ( $accounts as $account ) { |
| 131 | $meta = get_post_meta( $account->ID, 'nta_wa_account_info', true ); |
| 132 | $avatar = get_the_post_thumbnail_url( $account->ID ); |
| 133 | if ('' !== $meta) { |
| 134 | $results[] = array_merge( |
| 135 | array( |
| 136 | 'accountId' => $account->ID, |
| 137 | 'accountName' => $account->post_title, |
| 138 | 'avatar' => $avatar !== false ? $avatar : '', |
| 139 | ), |
| 140 | $meta |
| 141 | ); |
| 142 | } |
| 143 | } |
| 144 | return $results; |
| 145 | } |
| 146 | } |
| 147 |