PluginProbe ʕ •ᴥ•ʔ
WP Chat App / 3.6.1
WP Chat App v3.6.1
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
169 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 //Used to retrieve the accurate post ID when using Elementor
71 wp_reset_postdata();
72
73 if ( !$this->shouldDisplayWidget() ) {
74 return;
75 }
76
77 $displayOption = Fields::getWidgetDisplay();
78 $postId = get_the_ID();
79
80 if ( $this->notShowInPage($postId, $displayOption) ) return;
81
82 $activeAccounts = $this->get_accounts_active_and_meta();
83 if ( count($activeAccounts) < 1 ) return;
84
85 if ( wp_is_mobile() && $displayOption['showOnMobile'] === "OFF"
86 || !wp_is_mobile() && $displayOption['showOnDesktop'] === "OFF"
87 || ( $displayOption['showOnMobile'] === "OFF" && $displayOption['showOnDesktop'] === "OFF" )
88 ) {
89 return;
90 }
91
92 echo '<div id="wa"></div>';
93 $this->enqueue_scripts_styles($activeAccounts, $displayOption);
94 }
95
96 public function enqueue_scripts_styles($activeAccounts, $displayOption)
97 {
98 $stylesOption = Fields::getWidgetStyles();
99 $analyticsOption = Fields::getAnalyticsSetting();
100 wp_register_script('nta-js-popup', NTA_WHATSAPP_PLUGIN_URL . 'assets/js/whatsapp-popup.js', []);
101 wp_localize_script('nta-js-popup', 'njt_wa', [
102 'gdprStatus' => Helper::checkGDPR($stylesOption),
103 'accounts' => $activeAccounts,
104 'options' => [
105 'display' => $displayOption,
106 'styles' => $stylesOption,
107 'analytics' => $analyticsOption
108 ]
109 ]);
110 wp_enqueue_script('nta-js-popup');
111 }
112
113 public function notShowInPage($postId, $option)
114 {
115 $isPageOrShop = apply_filters('njt_whatsapp_is_page_or_shop_filter', is_page());
116 $postId = apply_filters('njt_whatsapp_get_post_id_filter', $postId);
117 $showInPostTypes = apply_filters( 'njt_whatsapp_display_in_post_types', array() );
118
119 $postType = get_post_type( $postId );
120
121 $isHiddenWidget= apply_filters('njt_whatsapp_hide_widget', false , $postId, $postType, $isPageOrShop, $option);
122
123 if ( $isHiddenWidget ) return true;
124
125 if ( ! empty( $showInPostTypes ) ) {
126 if ( in_array( $postType, $showInPostTypes ) ) {
127 return false;
128 }
129 }
130
131 if( $option['displayCondition'] == 'showAllPage' ) {
132 return false;
133 }
134
135 if ($option['displayCondition'] == 'includePages') {
136 if (is_array($option['includePages']) && $isPageOrShop && in_array(strval($postId), $option['includePages'])) {
137 return false;
138 }
139 return true;
140 } else if ($option['displayCondition'] == 'excludePages') {
141 if (is_array($option['excludePages']) && $isPageOrShop && in_array(strval($postId), $option['excludePages'])) {
142 return true;
143 }
144 }
145
146 return false;
147 }
148
149 public function get_accounts_active_and_meta(){
150 $results = array();
151 $accounts = PostType::getInstance()->get_active_widget_accounts();
152 foreach ( $accounts as $account ) {
153 $meta = get_post_meta( $account->ID, 'nta_wa_account_info', true );
154 $avatar = get_the_post_thumbnail_url( $account->ID );
155 if ('' !== $meta) {
156 $results[] = array_merge(
157 array(
158 'accountId' => $account->ID,
159 'accountName' => $account->post_title,
160 'avatar' => $avatar !== false ? $avatar : '',
161 ),
162 $meta
163 );
164 }
165 }
166 return $results;
167 }
168 }
169