PluginProbe ʕ •ᴥ•ʔ
WP Chat App / 3.7.0
WP Chat App v3.7.0
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 / Settings.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
Settings.php
488 lines
1 <?php
2 namespace NTA_WhatsApp;
3
4 use NTA_WhatsApp\Helper;
5 use NTA_WhatsApp\Fields;
6 use NTA_WhatsApp\PostType;
7
8 defined( 'ABSPATH' ) || exit;
9 /**
10 * Settings Page
11 */
12 class Settings {
13
14 protected $option;
15 protected $option_group = 'nta_whatsapp_group';
16 protected $option_design = 'nta_whatsapp_design';
17 protected $option_button_group = 'nta_whatsapp_button_group';
18 protected $option_woo_button_group = 'nta_wa_woo_button_group';
19 protected $option_ga_group = 'nta_wa_ga_group';
20
21 protected $settings;
22
23 private $floatingWidgetSlug = '';
24 private $settingSlug = '';
25
26 protected static $instance = null;
27
28 public static function getInstance() {
29 if ( null === self::$instance ) {
30 self::$instance = new self();
31 self::$instance->doHooks();
32 }
33 return self::$instance;
34 }
35
36 private function doHooks() {
37 add_action( 'admin_init', array( $this, 'register_setting' ) );
38 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
39 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
40 add_action( 'admin_footer', array( $this, 'admin_footer' ) );
41
42 add_action( 'wp_ajax_njt_wa_set_account_position', array( $this, 'set_account_position' ) );
43 add_action( 'wp_ajax_njt_wa_load_accounts_ajax', array( $this, 'load_accounts_ajax' ) );
44 add_action( 'wp_ajax_njt_wa_set_account_status', array( $this, 'set_account_status' ) );
45
46 add_action( 'wp_ajax_njt_wa_save_display_setting', array( $this, 'save_display_setting' ) );
47 add_action( 'wp_ajax_njt_wa_save_design_setting', array( $this, 'save_design_setting' ) );
48 add_action( 'wp_ajax_njt_wa_save_woocommerce_setting', array( $this, 'save_woocommerce_setting' ) );
49 add_action( 'wp_ajax_njt_wa_save_analytics_setting', array( $this, 'save_analytics_setting' ) );
50 add_action( 'wp_ajax_njt_wa_save_url_setting', array( $this, 'save_url_setting' ) );
51 add_action( 'wp_ajax_njt_wa_save_user_role_setting', array( $this, 'save_user_role_setting' ) );
52
53 add_filter( 'plugin_action_links_' . NTA_WHATSAPP_BASE_NAME, array( $this, 'addActionLinks' ) );
54 add_filter( 'plugin_row_meta', array( $this, 'pluginRowMeta' ), 10, 2 );
55 }
56
57 public function __construct() {
58 }
59
60 public function addActionLinks( $links ) {
61 $links = array_merge(
62 array(
63 '<a href="' . esc_url( admin_url( '/admin.php?page=nta_whatsapp_floating_widget' ) ) . '">' . __( 'Settings', 'wp-whatsapp' ) . '</a>',
64 ),
65 $links
66 );
67
68 $links[] = '<a target="_blank" href="https://1.envato.market/WhatsApp-Plugin" style="color: #43B854; font-weight: bold">' . __( 'Go Pro', 'wp-whatsapp' ) . '</a>';
69
70 return $links;
71 }
72
73 public function pluginRowMeta( $links, $file ) {
74 if ( strpos( $file, 'whatsapp.php' ) !== false ) {
75 $new_links = array(
76 'doc' => '<a href="https://ninjateam.org/wordpress-whatsapp-chat-tutorial/" target="_blank">' . __( 'Documentation', 'wp-whatsapp' ) . '</a>',
77 );
78
79 $links = array_merge( $links, $new_links );
80 }
81
82 return $links;
83 }
84
85 public function admin_menu() {
86 $edit_account_link = 'post-new.php?post_type=whatsapp-accounts';
87
88 add_menu_page( 'NTA Whatsapp', 'WhatsApp', 'nta_wa_manage', 'nta_whatsapp', array( $this, 'create_page_setting_widget' ), NTA_WHATSAPP_PLUGIN_URL . 'assets/img/whatsapp-menu.svg', 60 );
89 add_submenu_page( 'nta_whatsapp', __( 'Add New account', 'wp-whatsapp' ), __( 'Add New account', 'wp-whatsapp' ), 'nta_wa_manage', $edit_account_link );
90 $this->floatingWidgetSlug = add_submenu_page( 'nta_whatsapp', __( 'Floating Widget', 'wp-whatsapp' ), __( 'Floating Widget', 'wp-whatsapp' ), 'nta_wa_manage', 'nta_whatsapp_floating_widget', array( $this, 'floating_widget_view' ) );
91 $this->settingSlug = add_submenu_page( 'nta_whatsapp', __( 'Settings', 'wp-whatsapp' ), __( 'Settings', 'wp-whatsapp' ), 'manage_options', 'nta_whatsapp_setting', array( $this, 'create_page_setting_widget' ) );
92 add_submenu_page(
93 'nta_whatsapp',
94 '',
95 '<span>' . __( 'Go Pro', 'wp-whatsapp' ) . '</span>',
96 'manage_options',
97 'go_whatsapp_pro',
98 array( $this, 'go_pro_redirects' )
99 );
100 }
101
102 public function go_pro_redirects() {
103 if ( empty( $_GET['page'] ) ) {
104 return;
105 }
106
107 if ( 'go_whatsapp_pro' === $_GET['page'] ) {
108 ?>
109 <script>window.location.href = 'https://1.envato.market/whatsapp-pro'</script>
110 <?php
111 }
112 }
113
114 function admin_footer() {
115 ?>
116 <style>
117 body.admin-color-fresh #adminmenu #toplevel_page_nta_whatsapp a[href="admin.php?page=go_whatsapp_pro"] {
118 color: #00BC28;
119 font-weight: bold;
120 position: relative;
121 }
122
123 body.admin-color-fresh #adminmenu #toplevel_page_nta_whatsapp a[href="admin.php?page=go_whatsapp_pro"]::after {
124 content: '';
125 position: absolute;
126 width: 4px;
127 top: 0;
128 bottom: 0;
129 left: 0;
130 background: green;
131 }
132 </style>
133 <script>
134 jQuery(document).ready(function() {
135 jQuery('#toplevel_page_nta_whatsapp a[href="admin.php?page=go_whatsapp_pro"]').click(function(event) {
136 event.preventDefault()
137 window.open('https://1.envato.market/whatsapp-pro', '_blank')
138 })
139 })
140 </script>
141 <?php
142 $screen = get_current_screen();
143 if ( $screen->base !== $this->floatingWidgetSlug ) {
144 return;
145 }
146 require NTA_WHATSAPP_PLUGIN_DIR . 'views/design-preview.php';
147 }
148
149 public function admin_enqueue_scripts( $hook_suffix ) {
150 if ( $hook_suffix === 'edit.php' || $hook_suffix === 'post-new.php' || $hook_suffix === 'post.php' ) {
151 if ( get_post_type() !== 'whatsapp-accounts' ) {
152 return;
153 }
154 } elseif ( ! in_array( $hook_suffix, array( $this->settingSlug, $this->floatingWidgetSlug ) ) ) {
155 return;
156 }
157
158 wp_register_style( 'nta-css', NTA_WHATSAPP_PLUGIN_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), NTA_WHATSAPP_VERSION );
159 wp_enqueue_style( 'nta-css' );
160
161 wp_register_style( 'nta-tippy-css', NTA_WHATSAPP_PLUGIN_URL . 'assets/css/tooltip.css', array(), NTA_WHATSAPP_VERSION );
162 wp_enqueue_style( 'nta-tippy-css' );
163
164 wp_dequeue_style( 'woosea_jquery_ui-css' );
165
166 wp_register_style( 'nta-wa-widget', NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/css/style.css', array(), NTA_WHATSAPP_VERSION );
167 wp_enqueue_style( 'nta-wa-widget' );
168 wp_enqueue_style( 'ui-range', NTA_WHATSAPP_PLUGIN_URL . 'assets/libs/ui-range.css', array(), NTA_WHATSAPP_VERSION );
169
170 if ( function_exists( 'wp_timezone_string' ) ) {
171 $timezone = wp_timezone_string();
172 } else {
173 $timezone = Helper::wp_timezone_string();
174 }
175
176 wp_register_script(
177 'nta-wa-js',
178 NTA_WHATSAPP_PLUGIN_URL . 'assets/dist/js/app.js',
179 array(
180 'jquery',
181 'wp-color-picker',
182 'backbone',
183 'underscore',
184 'jquery-ui-tabs',
185 'jquery-ui-sortable',
186 'jquery-ui-autocomplete',
187 ),
188 NTA_WHATSAPP_VERSION,
189 true
190 );
191 wp_localize_script(
192 'nta-wa-js',
193 'njt_wa',
194 array(
195 'url' => admin_url( 'admin-ajax.php' ),
196 'nonce' => wp_create_nonce( 'njt-wa-nonce' ),
197 'settings' => array(
198 'widget' => array(
199 'styles' => Fields::getWidgetStyles(),
200 ),
201 ),
202 'timezone' => $timezone,
203 'i18n' => array(
204 'select_post' => __( 'Select posts to display the widget', 'wp-whatsapp' ),
205 ),
206 )
207 );
208 wp_enqueue_script( 'nta-wa-js' );
209 wp_enqueue_script( 'jquery-validate', NTA_WHATSAPP_PLUGIN_URL . 'assets/libs/jquery.validate.min.js', array( 'jquery' ), NTA_WHATSAPP_VERSION, true );
210 }
211
212 public function page_display_settings_section_callback() {
213 global $wpdb;
214 $option = Fields::getWidgetDisplay();
215 $option['time_symbols'] = explode( ':', $option['time_symbols'] );
216 $pages = $wpdb->get_results( "Select ID, post_title from {$wpdb->posts} where post_type = 'page' and post_status = 'publish'" );
217 require NTA_WHATSAPP_PLUGIN_DIR . 'views/display-settings.php';
218 }
219
220 public function page_design_settings_section_callback() {
221 $option = Fields::getWidgetStyles();
222 $editor_settings = array(
223 'media_buttons' => false,
224 'textarea_rows' => get_option( 'default_post_edit_rows', 5 ),
225 'quicktags' => false,
226 'teeny' => true,
227 );
228 $editor_settings_quicktags = array(
229 'media_buttons' => false,
230 'textarea_rows' => get_option( 'default_post_edit_rows', 5 ),
231 'quicktags' => true,
232 'teeny' => true,
233 );
234 require NTA_WHATSAPP_PLUGIN_DIR . 'views/design-settings.php';
235 }
236
237 public function page_selected_accounts_section_callback() {
238 require NTA_WHATSAPP_PLUGIN_DIR . 'views/selected-accounts.php';
239 }
240
241 public function woocommerce_button_callback() {
242 $option = Fields::getWoocommerceSetting();
243 require NTA_WHATSAPP_PLUGIN_DIR . 'views/woocommerce-button.php';
244 }
245
246 public function analytics_callback() {
247 $option = Fields::getAnalyticsSetting();
248 require NTA_WHATSAPP_PLUGIN_DIR . 'views/analytics.php';
249 }
250
251 public function url_callback() {
252 $option = Fields::getURLSettings();
253 require NTA_WHATSAPP_PLUGIN_DIR . 'views/url-settings.php';
254 }
255
256 public function user_role_callback() {
257 $option = Fields::getUserRoleSettings();
258 require NTA_WHATSAPP_PLUGIN_DIR . 'views/user-role-settings.php';
259 }
260
261 public function create_page_setting_widget() {
262 require NTA_WHATSAPP_PLUGIN_DIR . 'views/settings.php';
263 }
264
265 public function floating_widget_view() {
266 require NTA_WHATSAPP_PLUGIN_DIR . 'views/floating-widget-settings.php';
267 }
268
269 public function register_setting() {
270 register_setting( $this->option_group, 'nta_whatsapp_setting' );
271 register_setting( $this->option_design, 'nta_whatsapp_setting' );
272 register_setting( $this->option_woo_button_group, 'nta_wa_woobutton_setting', array( $this, 'save_woobutton_setting' ) );
273 register_setting( $this->option_ga_group, 'nta_wa_ga_setting', array( $this, 'save_ga_setting' ) );
274
275 add_settings_section( 'page_selected_accounts_section', '', array( $this, 'page_selected_accounts_section_callback' ), 'floating-widget-whatsapp-1' );
276 add_settings_section( 'page_design_settings_section', '', array( $this, 'page_design_settings_section_callback' ), 'floating-widget-whatsapp-2' );
277 add_settings_section( 'page_display_settings_section', '', array( $this, 'page_display_settings_section_callback' ), 'floating-widget-whatsapp-3' );
278 add_settings_section( 'nta_woocommerce_button', '', array( $this, 'woocommerce_button_callback' ), 'settings-whatsapp-1' );
279 add_settings_section( 'nta_analytics', '', array( $this, 'analytics_callback' ), 'settings-whatsapp-2' );
280 add_settings_section( 'nta_url', '', array( $this, 'url_callback' ), 'settings-whatsapp-3' );
281 add_settings_section( 'nta_user_role', '', array( $this, 'user_role_callback' ), 'settings-whatsapp-4' );
282 }
283
284 public function save_woobutton_setting() {
285 $new_input = array();
286
287 $new_input['nta_woo_button_position'] = sanitize_text_field( $_POST['nta_woo_button_position'] );
288 $new_input['nta_woo_button_status'] = isset( $_POST['nta_woo_button_status'] ) ? 'ON' : 'OFF';
289 return $new_input;
290 }
291
292 public function save_ga_setting() {
293 if ( isset( $_POST['nta_wa_ga_status'] ) ) {
294 return '1';
295 }
296 return '0';
297 }
298
299 public function save_display_setting() {
300 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
301 $new_input = array();
302
303 $excludePages = Helper::sanitize_array( $_POST['excludePages'] );
304 $includePages = Helper::sanitize_array( $_POST['includePages'] );
305 $includePosts = Helper::sanitize_array( $_POST['includePosts'] );
306
307 $new_input = Fields::getWidgetDisplay();
308 $new_input['displayCondition'] = sanitize_text_field( $_POST['displayCondition'] );
309 $new_input['excludePages'] = empty( $excludePages ) ? array() : $excludePages;
310 $new_input['includePages'] = empty( $includePages ) ? array() : $includePages;
311 $new_input['includePosts'] = empty( $includePosts ) ? array() : $includePosts;
312 $new_input['showOnDesktop'] = isset( $_POST['showOnDesktop'] ) ? 'ON' : 'OFF';
313 $new_input['showOnMobile'] = isset( $_POST['showOnMobile'] ) ? 'ON' : 'OFF';
314
315 $time_symbols = Helper::sanitize_array( $_POST['time_symbols'] );
316 $new_input['time_symbols'] = wp_unslash( $time_symbols['hourSymbol'] ) . ':' . wp_unslash( $time_symbols['minSymbol'] );
317
318 update_option( 'nta_wa_widget_display', $new_input );
319 wp_send_json_success();
320 }
321
322 public function save_design_setting() {
323 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
324
325 $new_input = array();
326
327 $new_input = Fields::getWidgetStyles();
328 $new_input['title'] = sanitize_text_field( wp_unslash( $_POST['title'] ) );
329 $new_input['textColor'] = sanitize_hex_color( $_POST['textColor'] );
330 $new_input['titleSize'] = sanitize_text_field( $_POST['titleSize'] );
331 $new_input['accountNameSize'] = sanitize_text_field( $_POST['accountNameSize'] );
332 $new_input['descriptionTextSize'] = sanitize_text_field( $_POST['descriptionTextSize'] );
333 $new_input['regularTextSize'] = sanitize_text_field( $_POST['regularTextSize'] );
334 $new_input['backgroundColor'] = sanitize_hex_color( $_POST['backgroundColor'] );
335 $new_input['description'] = wp_kses_post( wp_unslash( $_POST['description'] ) );
336 $new_input['responseText'] = wp_kses_post( wp_unslash( $_POST['responseText'] ) );
337 $new_input['scrollHeight'] = sanitize_text_field( $_POST['scrollHeight'] );
338 $new_input['isShowScroll'] = isset( $_POST['isShowScroll'] ) ? 'ON' : 'OFF';
339 $new_input['isShowResponseText'] = isset( $_POST['isShowResponseText'] ) ? 'ON' : 'OFF';
340 $new_input['isShowPoweredBy'] = isset( $_POST['isShowPoweredBy'] ) ? 'ON' : 'OFF';
341
342 $new_input['btnLabel'] = wp_kses_post( wp_unslash( $_POST['btnLabel'] ) ); // It can be an html tag
343 $new_input['btnPosition'] = sanitize_text_field( $_POST['btnPosition'] );
344 $new_input['btnLabelWidth'] = sanitize_text_field( $_POST['btnLabelWidth'] );
345 $new_input['btnLeftDistance'] = sanitize_text_field( $_POST['btnLeftDistance'] );
346 $new_input['btnRightDistance'] = sanitize_text_field( $_POST['btnRightDistance'] );
347 $new_input['btnBottomDistance'] = sanitize_text_field( $_POST['btnBottomDistance'] );
348 $new_input['isShowBtnLabel'] = isset( $_POST['isShowBtnLabel'] ) ? 'ON' : 'OFF';
349
350 $new_input['isShowGDPR'] = isset( $_POST['isShowGDPR'] ) ? 'ON' : 'OFF';
351 $new_input['gdprContent'] = wp_kses_post( wp_unslash( $_POST['gdprContent'] ) );
352
353 update_option( 'nta_wa_widget_styles', $new_input );
354 wp_send_json_success();
355 }
356
357 public function save_woocommerce_setting() {
358 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
359
360 $new_input = array();
361
362 $new_input = Fields::getWoocommerceSetting();
363 $new_input['position'] = sanitize_text_field( $_POST['position'] );
364 $new_input['isShow'] = isset( $_POST['isShow'] ) ? 'ON' : 'OFF';
365
366 update_option( 'nta_wa_woocommerce', $new_input );
367 wp_send_json_success();
368 }
369
370 public function save_analytics_setting() {
371 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
372
373 $new_input = array();
374
375 $new_input = Fields::getAnalyticsSetting();
376 $new_input['enabledGoogle'] = isset( $_POST['enabledGoogle'] ) ? 'ON' : 'OFF';
377 $new_input['enabledFacebook'] = isset( $_POST['enabledFacebook'] ) ? 'ON' : 'OFF';
378 $new_input['enabledGoogleGA4'] = isset( $_POST['enabledGoogleGA4'] ) ? 'ON' : 'OFF';
379
380 update_option( 'nta_wa_analytics', $new_input );
381 wp_send_json_success();
382 }
383
384 public function save_url_setting() {
385 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
386
387 $new_input = array();
388
389 $new_input = Fields::getURLSettings();
390 $new_input['openInNewTab'] = isset( $_POST['openInNewTab'] ) ? 'ON' : 'OFF';
391 $new_input['onDesktop'] = sanitize_text_field( $_POST['onDesktop'] );
392 $new_input['onMobile'] = sanitize_text_field( $_POST['onMobile'] );
393
394 update_option( 'nta_wa_url', $new_input );
395 wp_send_json_success();
396 }
397
398 public function save_user_role_setting() {
399 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
400
401 $new_input = array( 'administrator' => true );
402
403 if ( isset( $_POST['userRole'] ) ) {
404 $userRole = Helper::sanitize_array( $_POST['userRole'] );
405 foreach ( $userRole as $role ) {
406 $new_input[ $role ] = true;
407 // Add capability to user role
408 $role = get_role( $role );
409 $role->add_cap( 'nta_wa_manage' );
410 }
411 }
412 $old_settings = Fields::getUserRoleSettings();
413 $roles_remove_cap = array_diff_key( $old_settings, $new_input );
414 foreach ( $roles_remove_cap as $role => $value ) {
415 $role = get_role( $role );
416 $role->remove_cap( 'nta_wa_manage' );
417 }
418 update_option( 'nta_wa_user_role', $new_input );
419
420 wp_send_json_success();
421 }
422
423 public function set_account_position() {
424 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
425
426 $positions = Helper::sanitize_array( $_POST['positions'] );
427 $type = sanitize_text_field( $_POST['type'] );
428
429 foreach ( $positions as $index => $id ) {
430 update_post_meta( $id, "nta_wa_{$type}", $index );
431 }
432
433 wp_send_json_success();
434 }
435
436 public function load_accounts_ajax() {
437 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
438 $postType = PostType::getInstance();
439 $accountsList = $postType->get_posts();
440 $results = array_map(
441 function ( $account ) {
442 $meta = get_post_meta( $account->ID, 'nta_wa_account_info', true );
443 $avatar = get_the_post_thumbnail_url( $account->ID );
444 $wg_show = get_post_meta( $account->ID, 'nta_wa_widget_show', true );
445 $wg_position = get_post_meta( $account->ID, 'nta_wa_widget_position', true );
446 $wc_show = get_post_meta( $account->ID, 'nta_wa_wc_show', true );
447 $wc_position = get_post_meta( $account->ID, 'nta_wa_wc_position', true );
448
449 return array_merge(
450 array(
451 'accountId' => $account->ID,
452 'accountName' => $account->post_title,
453 'edit_link' => get_edit_post_link( $account->ID ),
454 'avatar' => $avatar !== false ? $avatar : '',
455 'widget_show' => empty( $wg_show ) ? 'OFF' : $wg_show,
456 'widget_position' => $wg_position,
457 'wc_show' => empty( $wc_show ) ? 'OFF' : $wc_show,
458 'wc_position' => $wc_position,
459 ),
460 $meta
461 );
462 },
463 $accountsList
464 );
465 wp_send_json_success( $results );
466 }
467
468 public function set_account_status() {
469 check_ajax_referer( 'njt-wa-nonce', 'nonce', true );
470 $id = sanitize_text_field( $_POST['accountId'] );
471 $type = sanitize_text_field( $_POST['type'] );
472 $status = sanitize_text_field( $_POST['status'] );
473
474 $wg_position = get_post_meta( $id, 'nta_wa_widget_position', true );
475 $wc_position = get_post_meta( $id, 'nta_wa_wc_position', true );
476
477 if ( '' === $wg_position ) {
478 update_post_meta( $id, 'nta_wa_widget_position', 0 );
479 }
480
481 if ( '' === $wc_position ) {
482 update_post_meta( $id, 'nta_wa_wc_position', 0 );
483 }
484
485 update_post_meta( $id, "nta_wa_{$type}", $status );
486 wp_send_json_success();
487 }
488 }