PluginProbe ʕ •ᴥ•ʔ
WP Chat App / 3.5
WP Chat App v3.5
3.8.1 3.8.2 trunk 2.6 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5 3.6 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0
wp-whatsapp / includes / Popup.php
wp-whatsapp / includes Last commit date
Recommended 2 years ago Support 2 years ago Cross.php 2 years ago Fallback.php 2 years ago Fields.php 2 years ago Helper.php 2 years ago I18n.php 2 years ago Plugin.php 2 years ago Popup.php 2 years ago PostType.php 2 years ago Review.php 2 years ago Settings.php 2 years ago Shortcode.php 2 years ago Upgrade.php 2 years ago
Popup.php
166 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 private function shouldDisplayWidget(){
58 /**
59 * This code block prevents the display of the popup in Oxygen Builder.
60 */
61 if ( defined("SHOW_CT_BUILDER") && !defined("OXYGEN_IFRAME") ) {
62 return false;
63 }
64
65 return true;
66 }
67
68 public function show_widget()
69 {
70 if ( !$this->shouldDisplayWidget() ) {
71 return;
72 }
73
74 $displayOption = Fields::getWidgetDisplay();
75 $postId = get_the_ID();
76
77 if ( $this->notShowInPage($postId, $displayOption) ) return;
78
79 $activeAccounts = $this->get_accounts_active_and_meta();
80 if ( count($activeAccounts) < 1 ) return;
81
82 if ( wp_is_mobile() && $displayOption['showOnMobile'] === "OFF"
83 || !wp_is_mobile() && $displayOption['showOnDesktop'] === "OFF"
84 || ( $displayOption['showOnMobile'] === "OFF" && $displayOption['showOnDesktop'] === "OFF" )
85 ) {
86 return;
87 }
88
89 echo '<div id="wa"></div>';
90 $this->enqueue_scripts_styles($activeAccounts, $displayOption);
91 }
92
93 public function enqueue_scripts_styles($activeAccounts, $displayOption)
94 {
95 $stylesOption = Fields::getWidgetStyles();
96 $analyticsOption = Fields::getAnalyticsSetting();
97 wp_register_script('nta-js-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/js/whatsapp-popup.js', []);
98 wp_localize_script('nta-js-popup', 'njt_wa', [
99 'gdprStatus' => Helper::checkGDPR($stylesOption),
100 'accounts' => $activeAccounts,
101 'options' => [
102 'display' => $displayOption,
103 'styles' => $stylesOption,
104 'analytics' => $analyticsOption
105 ]
106 ]);
107 wp_enqueue_script('nta-js-popup');
108 }
109
110 public function notShowInPage($postId, $option)
111 {
112 $isPageOrShop = apply_filters('njt_whatsapp_is_page_or_shop_filter', is_page());
113 $postId = apply_filters('njt_whatsapp_get_post_id_filter', $postId);
114 $showInPostTypes = apply_filters( 'njt_whatsapp_display_in_post_types', array() );
115
116 $postType = get_post_type( $postId );
117
118 $isHiddenWidget= apply_filters('njt_whatsapp_hide_widget', false , $postId, $postType, $isPageOrShop, $option);
119
120 if ( $isHiddenWidget ) return true;
121
122 if ( ! empty( $showInPostTypes ) ) {
123 if ( in_array( $postType, $showInPostTypes ) ) {
124 return false;
125 }
126 }
127
128 if( $option['displayCondition'] == 'showAllPage' ) {
129 return false;
130 }
131
132 if ($option['displayCondition'] == 'includePages') {
133 if (is_array($option['includePages']) && $isPageOrShop && in_array(strval($postId), $option['includePages'])) {
134 return false;
135 }
136 return true;
137 } else if ($option['displayCondition'] == 'excludePages') {
138 if (is_array($option['excludePages']) && $isPageOrShop && in_array(strval($postId), $option['excludePages'])) {
139 return true;
140 }
141 }
142
143 return false;
144 }
145
146 public function get_accounts_active_and_meta(){
147 $results = array();
148 $accounts = PostType::getInstance()->get_active_widget_accounts();
149 foreach ( $accounts as $account ) {
150 $meta = get_post_meta( $account->ID, 'nta_wa_account_info', true );
151 $avatar = get_the_post_thumbnail_url( $account->ID );
152 if ('' !== $meta) {
153 $results[] = array_merge(
154 array(
155 'accountId' => $account->ID,
156 'accountName' => $account->post_title,
157 'avatar' => $avatar !== false ? $avatar : '',
158 ),
159 $meta
160 );
161 }
162 }
163 return $results;
164 }
165 }
166