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