PluginProbe
WANotifier for Forms and Actions / 2.4.2
WANotifier for Forms and Actions v2.4.2
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / includes / classes / class-notifier-settings.php

class-notifier-settings.php in WANotifier for Forms and Actions 2.4.2, at includes/classes/class-notifier-settings.php

545 lines 18.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings page class
4 *
5 * @package Wa_Notifier
6 */
7 class Notifier_Settings {
8
9 /**
10 * Init
11 */
12 public static function init() {
13 add_action( 'admin_menu', array( __CLASS__ , 'setup_admin_page') );
14 add_action( 'admin_init', array( __CLASS__ , 'save_settings_fields' ) );
15 add_action( 'admin_init', array( __CLASS__ , 'disconnect_notifier' ) );
16 add_action( 'wp_ajax_notifier_preview_btn_style', array(__CLASS__, 'preview_btn_style'));
17 }
18
19 /**
20 * Add settings page to men
21 */
22 public static function setup_admin_page () {
23 if (!Notifier::is_api_active()){
24 return;
25 }
26
27 add_submenu_page( NOTIFIER_NAME, 'Settings', 'Settings', 'manage_options', NOTIFIER_NAME . '-settings', array( __CLASS__, 'output' ) );
28 }
29
30 /**
31 * Output
32 */
33 public static function output() {
34 include_once NOTIFIER_PATH . '/views/admin-settings.php';
35 }
36
37 /**
38 * Get settings tabs
39 */
40 private static function get_settings_tabs() {
41 $tabs = array(
42 'general' => 'General',
43 'click_to_chat' => 'Click to Chat',
44 'api' => 'API',
45 'advanced' => 'Advanced'
46 );
47 return $tabs;
48 }
49
50 /**
51 * Check if on settings page
52 */
53 public static function is_settings_page() {
54 $current_page = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
55 return strpos($current_page, NOTIFIER_NAME . '-settings') !== false;
56 }
57
58 /**
59 * Settings fields
60 */
61 private static function settings_fields($tab) {
62 $api_key = get_option(NOTIFIER_PREFIX . 'api_key');
63 if(Notifier::is_api_active() && '' != $api_key) {
64 $disabled = true;
65 $description = 'You are successfully connected to WANotifier.com. <a href="?notifier_disconnect=1">Disconnect</a>.';
66 }
67 else{
68 $disabled = false;
69 $description = 'You are not connected to WANotifier.com yet. Please enter valid API key and save the settings. You can get your WANotifier.com API Key from <a href="https://app.wanotifier.com/settings/api/" target="_blank">here</a>.';
70 }
71 $settings = array();
72 switch ($tab) {
73 case 'general':
74 $settings = array(
75 array(
76 'title' => 'General',
77 'type' => 'title',
78 ),
79 array(
80 'id' => 'default_country_code',
81 'title' => 'Default Country Code (Optional)',
82 'type' => 'text',
83 'placeholder' => 'Eg. +91',
84 'description' => 'Enter default country code that will be added to all recipient phone number fields before sending to WANotifier that do not start with a + sign.',
85 'default' => ''
86 ),
87 );
88 break;
89 case 'click_to_chat':
90 $settings = array(
91 array(
92 'title' => 'Click to Chat',
93 'description' => 'Show click to chat button on your website to let your visitors start WhatsApp chat with you.',
94 'type' => 'title',
95 ),
96 array(
97 'id' => 'ctc_enable',
98 'title' => 'Enable',
99 'type' => 'checkbox',
100 'default' => '',
101 'name' => 'ctc_enable',
102 ),
103 array(
104 'id' => 'ctc_whatsapp_number',
105 'title' => 'Whatsapp Number',
106 'type' => 'text',
107 'placeholder' => 'Enter your WhatsApp number',
108 'name' => 'ctc_whatsapp_number',
109 'description' => 'Enter your WhatsApp number with country code. Eg. +919876543210',
110 ),
111 array(
112 'id' => 'ctc_greeting_text',
113 'title' => 'Greeting Message',
114 'type' => 'text',
115 'name' => 'ctc_greeting_text',
116 'placeholder' => 'Enter your greeting message here',
117 'description' => 'This text will be added to user\'s WhatsApp chat text field when they click on the button.' ,
118 ),
119 array(
120 'id' => 'ctc_button_style',
121 'title' => 'Button Style',
122 'name' => 'ctc_button_style',
123 'class' => 'chat-button-style',
124 'type' => 'select',
125 'default' => '',
126 'options' => array(
127 'default' => 'Select button style',
128 'btn-style-1' => 'Style 1' ,
129 'btn-style-2' => 'Style 2',
130 'btn-style-3' => 'Style 3',
131 'btn-style-4' => 'Style 4',
132 'btn-custom-image' => 'Add your own image'
133 ),
134 'description' => 'Select a button style. Preview will be shown on bottom right of this screen. You can update button style by writing custom CSS in your theme.',
135 ),
136 array(
137 'id' => 'ctc_custom_button_image_url',
138 'title' => 'Button image url',
139 'type' => 'text',
140 'placeholder' => 'https://',
141 'name' => 'ctc_custom_button_image_url',
142 'description' => 'Enter button image url here.',
143 'tr_class' => 'notifier-chat-btn-image-url',
144 ),
145 );
146 break;
147 case 'api':
148 $settings = array(
149 array(
150 'title' => 'API Configuration',
151 'description' => '',
152 'type' => 'title',
153 ),
154 array(
155 'id' => 'api_key',
156 'title' => 'WANotifier.com API key',
157 'type' => 'text',
158 'placeholder' => 'Enter your WANotifier.com API key here',
159 'default' => '',
160 'description' => $description,
161 'disabled' => $disabled
162 ),
163 );
164 break;
165 case 'advanced':
166 $settings = array(
167 array(
168 'title' => 'Advanced Settings',
169 'description' => '',
170 'type' => 'title',
171 ),
172 array(
173 'id' => 'hidden_custom_keys',
174 'title' => 'Enable hidden custom meta keys',
175 'type' => 'checkbox',
176 'description' => 'Enable hidden custom meta keys that start with underscores (e.g. _field_name) to be avaialbe in Data and Recipient Fields. Note that enabling this might impact your website performance slightly.',
177 'default' => ''
178 ),
179 array(
180 'id' => 'enable_async_triggers',
181 'title' => 'Enable async triggers',
182 'type' => 'checkbox',
183 'default' => '',
184 'name' => 'enable_async_triggers',
185 'description' => 'Plugin slowing down checkout or form submission? Enable this option to send triggers asynchronously using Action Scheduler. Note that if you have a site with strong caching, this might not work as expected.' ,
186 ),
187 );
188 break;
189 }
190 $settings = apply_filters( 'notifier_$tab_settings_fields', $settings );
191 return $settings;
192 }
193
194 /**
195 * Generate HTML for displaying individual fields
196 */
197 public static function display_field( $field ) {
198 $html = '';
199
200 if (isset($field['id'])) {
201 $option_name = NOTIFIER_PREFIX . $field['id'];
202 $option = get_option( $option_name );
203 }
204
205 $data = $option;
206 if ( isset( $field['default'] ) && empty($option) ) {
207 $data = $field['default'];
208 }
209
210 $custom_attributes = array();
211 if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
212 foreach ( $field['custom_attributes'] as $attribute => $value ) {
213 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
214 }
215 }
216
217 if ( isset($field['required']) && $field['required'] ) {
218 $custom_attributes[] = 'required="required"';
219 }
220
221 if ( isset($field['disabled']) && $field['disabled'] ) {
222 $custom_attributes[] = 'disabled="disabled"';
223 }
224
225 if(isset($field['tr_class'])){
226 $tr_class = $field['tr_class'];
227 }else{
228 $tr_class = '';
229 }
230
231 $custom_attributes_string = implode( ' ', $custom_attributes );
232
233 if ( 'title' === $field['type']) {
234 $html .= '<tr class="'.esc_attr($tr_class).'"><th class="section-title" colspan="2"><h3>' . $field['title'] . '</h3>';
235 $html .= '<p class="description">' . $field['description'] . '</p></th></tr>';
236 } else {
237 $html .= '<tr class="'.esc_attr($tr_class).'">';
238 $html .= '<th>' . $field['title'] . '</th>';
239 $html .= '<td>';
240
241 switch ( $field['type'] ) {
242
243 case 'text':
244 case 'password':
245 case 'number':
246 $html .= '<input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . esc_attr($data) . '" '.$custom_attributes_string.'/>';
247 break;
248
249 case 'textarea':
250 $html .= '<textarea id="' . esc_attr( $option_name ) . '" rows="5" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" '.$custom_attributes_string.'>' . esc_textarea($data) . '</textarea><br/>';
251 break;
252
253 case 'checkbox':
254 $checked = '';
255 if ( $option && 'yes' == $option ) {
256 $checked = 'checked="checked"';
257 }
258 $html .= '<label><input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . ' '.$custom_attributes_string.' value="yes"/>' . $field['label'] . '</label>';
259 break;
260
261 case 'checkbox_multi':
262 foreach ( $field['options'] as $k => $v ) {
263 $checked = false;
264 if ( in_array( $k, $data ) ) {
265 $checked = true;
266 }
267 $html .= '<p><label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="checkbox" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '[]" value="' . esc_attr( $k ) . '" id="' . esc_attr( $option_name . '_' . $k ) . '" '.$custom_attributes_string.'/> ' . $v . '</label></p>';
268 }
269 break;
270
271 case 'radio':
272 foreach ( $field['options'] as $k => $v ) {
273 $checked = false;
274 if ( $k == $data ) {
275 $checked = true;
276 }
277 $html .= '<p><label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="radio" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $k ) . '" id="' . esc_attr( $option_name . '_' . $k ) . '" '.$custom_attributes_string.'/> ' . $v . '</label></p>';
278 }
279 break;
280
281 case 'select':
282 $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '" '.$custom_attributes_string.'>';
283 foreach ( $field['options'] as $k => $v ) {
284 $selected = false;
285 if ( $k == $data ) {
286 $selected = true;
287 }
288 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>';
289 }
290 $html .= '</select> ';
291 break;
292
293 case 'select_multi':
294 $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple" '.$custom_attributes_string.'>';
295 foreach ( $field['options'] as $k => $v ) {
296 $selected = false;
297 if ( in_array( $k, $data ) ) {
298 $selected = true;
299 }
300 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '" />' . $v . '</label> ';
301 }
302 $html .= '</select> ';
303 break;
304
305 case 'file':
306 $uploader_title = isset($field['uploader_title']) ? $field['uploader_title'] : 'Upload media';
307 $uploader_button_text = isset($field['uploader_button_text']) ? $field['uploader_button_text'] : 'Select';
308 $file_types = isset($field['uploader_supported_file_types']) ? $field['uploader_supported_file_types'] : array();
309
310 $image_thumb = '';
311 $video_url = '';
312 $show_image = 'hide';
313 $show_video = 'hide';
314
315 $html .= '<span class="notifier-media-preview">';
316 if ( '' != $data ) {
317 $file_type = $field['uploader_supported_file_types'];
318 switch ($file_type) {
319 case 'image':
320 case 'image/jpeg':
321 case 'image/png':
322 case 'application/pdf':
323 $image_thumb = wp_get_attachment_thumb_url( $data );
324 $show_image = '';
325 $show_video = 'hide';
326 break;
327
328 case 'video/mp4':
329 $video_url = wp_get_attachment_url( $data );
330 $show_image = 'hide';
331 $show_video = '';
332 break;
333
334 default:
335 $show_image = 'hide';
336 $show_video = 'hide';
337 }
338 }
339 $html .= '<img id="' . esc_attr( $option_name ) . '_preview_image" class="notifier-media-preview-item '. esc_attr($show_image) .'" src="' . esc_url($image_thumb) . '" />';
340 $html .= '<video id="' . esc_attr( $option_name ) . '_preview_video" class="notifier-media-preview-item '. esc_attr($show_video) .'" width="300" height="169" controls muted><source src="' . esc_url($video_url) . '" type="video/mp4"></video><br/>';
341 $html .= '</span>';
342
343 $html .= '<input id="' . esc_attr( $option_name ) . '_button" type="button" data-uploader_title="' . esc_attr($uploader_title) . '" data-uploader_button_text="' . esc_attr($uploader_button_text) . '" data-uploader_supported_file_types="' . esc_attr($file_types) . '" class="notifier-media-upload-button button" value="' . 'Upload' . '" /> ';
344 $html .= '<input id="' . esc_attr( $option_name ) . '_delete" type="button" class="notifier-media-delete-button button" value="' . 'Remove' . '" />';
345 $html .= '<input id="' . esc_attr( $option_name ) . '" class="notifier-media-attachment-id" type="hidden" name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $data ) . '"/><br/>';
346 break;
347
348 case 'color':
349 ?>
350 <div class="color-picker" style="position:relative;">
351 <input type="text" name="<?php echo esc_attr( $option_name ); ?>" class="color" value="<?php echo esc_attr( $data ); ?>" />
352 <div style="position:absolute;background:#FFF;z-index:99;border-radius:100%;" class="colorpicker"></div>
353 </div>
354 <?php
355 break;
356
357 }
358
359 if ( '' != $field['description'] ) {
360 $html .= '<p class="description">' . wp_kses_post($field['description']) . '</p>';
361 }
362 }
363
364 //phpcs:ignore
365 echo $html;
366 }
367
368 /**
369 * Show the setting fields
370 */
371 public static function show_settings_fields ($tab) {
372 $setting_fields = self::settings_fields($tab);
373 echo "<table class='notifier-fields-table'>";
374 foreach ($setting_fields as $field) {
375 self::display_field( $field );
376 }
377 echo '</table>';
378 }
379
380 /**
381 * Save the settings.
382 *
383 * TODO - check fields other than text and textarea
384 */
385 public static function save_settings_fields() {
386 if ( ! self::is_settings_page() ) {
387 return;
388 }
389
390 if ( ! isset( $_POST['save'] ) ) {
391 return;
392 }
393
394 //phpcs:ignore
395 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-settings' ) ) {
396 return;
397 }
398
399 $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'general';
400
401 $settings_fields = self::settings_fields($tab);
402 $data = $_POST;
403 $update_options = array();
404
405 // Loop options and get values to save.
406 foreach ( $settings_fields as $option ) {
407 if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
408 continue;
409 }
410
411 if ( isset( $option['disabled'] ) && $option['disabled'] ) {
412 continue;
413 }
414
415 $option_name = NOTIFIER_PREFIX . $option['id'];
416 $setting_name = '';
417 $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null;
418
419 // Format the value based on option type.
420 switch ( $option['type'] ) {
421 case 'checkbox':
422 $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no';
423 break;
424 case 'textarea':
425 $value = wp_kses_post( trim( $raw_value ) );
426 break;
427 case 'select':
428 $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) );
429 if ( empty( $option['default'] ) && empty( $allowed_values ) ) {
430 $value = null;
431 break;
432 }
433 $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] );
434 $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default;
435 break;
436 default:
437 $value = sanitize_text_field( $raw_value );
438 break;
439 }
440
441 // Check if option is an array and handle that differently to single values.
442 if ( $option_name && $setting_name ) {
443 if ( ! isset( $update_options[ $option_name ] ) ) {
444 $update_options[ $option_name ] = get_option( $option_name, array() );
445 }
446 if ( ! is_array( $update_options[ $option_name ] ) ) {
447 $update_options[ $option_name ] = array();
448 }
449 $update_options[ $option_name ][ $setting_name ] = $value;
450 } else {
451 $update_options[ $option_name ] = $value;
452 }
453 }
454
455 // Save all options in our array.
456 foreach ( $update_options as $name => $value ) {
457 if('notifier_api_key' == $name){
458 $current_api_key = get_option($name);
459 if($current_api_key == $value){
460 continue;
461 }
462
463 update_option( NOTIFIER_PREFIX . 'api_key', $value);
464 delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
465 delete_option( NOTIFIER_PREFIX . 'api_activated' );
466
467 $params = array(
468 'site_url' => site_url(),
469 'source' => 'wp'
470 );
471
472 $response = Notifier::send_api_request( 'verify_api', $params, 'POST' );
473
474 if($response->error){
475 $notices[] = array(
476 'message' => 'There was an error validating API key. Error: ' . $response->message,
477 'type' => 'error'
478 );
479 }
480 else{
481 update_option('notifier_api_activated', 'yes');
482 $notices[] = array(
483 'message' => 'API key validated and saved successfully. Your triggers have been reset. Please enable and save your triggers again from the <a href="'.admin_url('admin.php?page=notifier').'">WANotifier</a> page.',
484 'type' => 'success'
485 );
486 }
487 }
488 elseif('notifier_ctc_whatsapp_number' == $name && !notifier_validate_phone_number($value)){
489 $notices[] = array(
490 'message' => 'Please enter a valid WhatsApp phone number with country code.',
491 'type' => 'error'
492 );
493 }
494 else{
495 update_option( $name, $value, 'yes' );
496 }
497 }
498
499 if('advanced' == $tab){
500 delete_transient( '_notifier_custom_meta_keys' );
501 }
502
503 if(empty($notices)){
504 $notices[] = array(
505 'message' => 'Settings updated successfully.',
506 'type' => 'success'
507 );
508 }
509
510 new Notifier_Admin_Notices($notices);
511 }
512
513 /**
514 * Disconnect WANotifier.com connection
515 */
516 public static function disconnect_notifier(){
517 if(isset($_GET['notifier_disconnect']) && '1' == $_GET['notifier_disconnect']){
518 delete_option( NOTIFIER_PREFIX . 'api_key' );
519 delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
520 delete_option( NOTIFIER_PREFIX . 'api_activated' );
521 $notices[] = array(
522 'message' => 'Disconnected successfully. To re-connect with WANotifier.com, enter API key below and click on <b>Save and validate</b>.',
523 'type' => 'success'
524 );
525 new Notifier_Admin_Notices($notices, true);
526 wp_redirect(admin_url('admin.php?page=notifier'));
527 }
528 }
529
530 /**
531 * Show Chat Button Preview
532 */
533 public static function preview_btn_style(){
534 $btn_style = isset($_POST['btn_style']) ? sanitize_text_field($_POST['btn_style']) : '';
535
536 ob_start();
537 include_once NOTIFIER_PATH.'templates/buttons/'.$btn_style.'.php';
538 $btn_output = ob_get_clean();
539
540 wp_send_json( array(
541 'preview' => $btn_output
542 ) );
543 }
544 }
545