PluginProbe
WANotifier for Forms and Actions / 2.4.1
WANotifier for Forms and Actions v2.4.1
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.1, at includes/classes/class-notifier-settings.php

533 lines 17.7 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 );
180 break;
181 }
182 $settings = apply_filters( 'notifier_$tab_settings_fields', $settings );
183 return $settings;
184 }
185
186 /**
187 * Generate HTML for displaying individual fields
188 */
189 public static function display_field( $field ) {
190 $html = '';
191
192 if (isset($field['id'])) {
193 $option_name = NOTIFIER_PREFIX . $field['id'];
194 $option = get_option( $option_name );
195 }
196
197 $data = $option;
198 if ( isset( $field['default'] ) && empty($option) ) {
199 $data = $field['default'];
200 }
201
202 $custom_attributes = array();
203 if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) {
204 foreach ( $field['custom_attributes'] as $attribute => $value ) {
205 $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
206 }
207 }
208
209 if ( isset($field['required']) && $field['required'] ) {
210 $custom_attributes[] = 'required="required"';
211 }
212
213 if ( isset($field['disabled']) && $field['disabled'] ) {
214 $custom_attributes[] = 'disabled="disabled"';
215 }
216
217 if(isset($field['tr_class'])){
218 $tr_class = $field['tr_class'];
219 }else{
220 $tr_class = '';
221 }
222
223 $custom_attributes_string = implode( ' ', $custom_attributes );
224
225 if ( 'title' === $field['type']) {
226 $html .= '<tr class="'.esc_attr($tr_class).'"><th class="section-title" colspan="2"><h3>' . $field['title'] . '</h3>';
227 $html .= '<p class="description">' . $field['description'] . '</p></th></tr>';
228 } else {
229 $html .= '<tr class="'.esc_attr($tr_class).'">';
230 $html .= '<th>' . $field['title'] . '</th>';
231 $html .= '<td>';
232
233 switch ( $field['type'] ) {
234
235 case 'text':
236 case 'password':
237 case 'number':
238 $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.'/>';
239 break;
240
241 case 'textarea':
242 $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/>';
243 break;
244
245 case 'checkbox':
246 $checked = '';
247 if ( $option && 'yes' == $option ) {
248 $checked = 'checked="checked"';
249 }
250 $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>';
251 break;
252
253 case 'checkbox_multi':
254 foreach ( $field['options'] as $k => $v ) {
255 $checked = false;
256 if ( in_array( $k, $data ) ) {
257 $checked = true;
258 }
259 $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>';
260 }
261 break;
262
263 case 'radio':
264 foreach ( $field['options'] as $k => $v ) {
265 $checked = false;
266 if ( $k == $data ) {
267 $checked = true;
268 }
269 $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>';
270 }
271 break;
272
273 case 'select':
274 $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '" '.$custom_attributes_string.'>';
275 foreach ( $field['options'] as $k => $v ) {
276 $selected = false;
277 if ( $k == $data ) {
278 $selected = true;
279 }
280 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>';
281 }
282 $html .= '</select> ';
283 break;
284
285 case 'select_multi':
286 $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple" '.$custom_attributes_string.'>';
287 foreach ( $field['options'] as $k => $v ) {
288 $selected = false;
289 if ( in_array( $k, $data ) ) {
290 $selected = true;
291 }
292 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '" />' . $v . '</label> ';
293 }
294 $html .= '</select> ';
295 break;
296
297 case 'file':
298 $uploader_title = isset($field['uploader_title']) ? $field['uploader_title'] : 'Upload media';
299 $uploader_button_text = isset($field['uploader_button_text']) ? $field['uploader_button_text'] : 'Select';
300 $file_types = isset($field['uploader_supported_file_types']) ? $field['uploader_supported_file_types'] : array();
301
302 $image_thumb = '';
303 $video_url = '';
304 $show_image = 'hide';
305 $show_video = 'hide';
306
307 $html .= '<span class="notifier-media-preview">';
308 if ( '' != $data ) {
309 $file_type = $field['uploader_supported_file_types'];
310 switch ($file_type) {
311 case 'image':
312 case 'image/jpeg':
313 case 'image/png':
314 case 'application/pdf':
315 $image_thumb = wp_get_attachment_thumb_url( $data );
316 $show_image = '';
317 $show_video = 'hide';
318 break;
319
320 case 'video/mp4':
321 $video_url = wp_get_attachment_url( $data );
322 $show_image = 'hide';
323 $show_video = '';
324 break;
325
326 default:
327 $show_image = 'hide';
328 $show_video = 'hide';
329 }
330 }
331 $html .= '<img id="' . esc_attr( $option_name ) . '_preview_image" class="notifier-media-preview-item '. esc_attr($show_image) .'" src="' . esc_url($image_thumb) . '" />';
332 $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/>';
333 $html .= '</span>';
334
335 $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' . '" /> ';
336 $html .= '<input id="' . esc_attr( $option_name ) . '_delete" type="button" class="notifier-media-delete-button button" value="' . 'Remove' . '" />';
337 $html .= '<input id="' . esc_attr( $option_name ) . '" class="notifier-media-attachment-id" type="hidden" name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $data ) . '"/><br/>';
338 break;
339
340 case 'color':
341 ?>
342 <div class="color-picker" style="position:relative;">
343 <input type="text" name="<?php echo esc_attr( $option_name ); ?>" class="color" value="<?php echo esc_attr( $data ); ?>" />
344 <div style="position:absolute;background:#FFF;z-index:99;border-radius:100%;" class="colorpicker"></div>
345 </div>
346 <?php
347 break;
348
349 }
350
351 if ( '' != $field['description'] ) {
352 $html .= '<p class="description">' . wp_kses_post($field['description']) . '</p>';
353 }
354 }
355
356 //phpcs:ignore
357 echo $html;
358 }
359
360 /**
361 * Show the setting fields
362 */
363 public static function show_settings_fields ($tab) {
364 $setting_fields = self::settings_fields($tab);
365 echo "<table class='notifier-fields-table'>";
366 foreach ($setting_fields as $field) {
367 self::display_field( $field );
368 }
369 echo '</table>';
370 }
371
372 /**
373 * Save the settings.
374 *
375 * TODO - check fields other than text and textarea
376 */
377 public static function save_settings_fields() {
378 if ( ! self::is_settings_page() ) {
379 return;
380 }
381
382 if ( ! isset( $_POST['save'] ) ) {
383 return;
384 }
385
386 //phpcs:ignore
387 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-settings' ) ) {
388 return;
389 }
390
391 $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'general';
392
393 $settings_fields = self::settings_fields($tab);
394 $data = $_POST;
395 $update_options = array();
396
397 // Loop options and get values to save.
398 foreach ( $settings_fields as $option ) {
399 if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
400 continue;
401 }
402
403 if ( isset( $option['disabled'] ) && $option['disabled'] ) {
404 continue;
405 }
406
407 $option_name = NOTIFIER_PREFIX . $option['id'];
408 $setting_name = '';
409 $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null;
410
411 // Format the value based on option type.
412 switch ( $option['type'] ) {
413 case 'checkbox':
414 $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no';
415 break;
416 case 'textarea':
417 $value = wp_kses_post( trim( $raw_value ) );
418 break;
419 case 'select':
420 $allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) );
421 if ( empty( $option['default'] ) && empty( $allowed_values ) ) {
422 $value = null;
423 break;
424 }
425 $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] );
426 $value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default;
427 break;
428 default:
429 $value = sanitize_text_field( $raw_value );
430 break;
431 }
432
433 // Check if option is an array and handle that differently to single values.
434 if ( $option_name && $setting_name ) {
435 if ( ! isset( $update_options[ $option_name ] ) ) {
436 $update_options[ $option_name ] = get_option( $option_name, array() );
437 }
438 if ( ! is_array( $update_options[ $option_name ] ) ) {
439 $update_options[ $option_name ] = array();
440 }
441 $update_options[ $option_name ][ $setting_name ] = $value;
442 } else {
443 $update_options[ $option_name ] = $value;
444 }
445 }
446
447 // Save all options in our array.
448 foreach ( $update_options as $name => $value ) {
449 if('notifier_api_key' == $name){
450 $current_api_key = get_option($name);
451 if($current_api_key == $value){
452 continue;
453 }
454
455 update_option( NOTIFIER_PREFIX . 'api_key', $value);
456 delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
457 delete_option( NOTIFIER_PREFIX . 'api_activated' );
458
459 $params = array(
460 'site_url' => site_url(),
461 'source' => 'wp'
462 );
463
464 $response = Notifier::send_api_request( 'verify_api', $params, 'POST' );
465
466 if($response->error){
467 $notices[] = array(
468 'message' => 'There was an error validating API key. Error: ' . $response->message,
469 'type' => 'error'
470 );
471 }
472 else{
473 update_option('notifier_api_activated', 'yes');
474 $notices[] = array(
475 '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.',
476 'type' => 'success'
477 );
478 }
479 }
480 elseif('notifier_ctc_whatsapp_number' == $name && !notifier_validate_phone_number($value)){
481 $notices[] = array(
482 'message' => 'Please enter a valid WhatsApp phone number with country code.',
483 'type' => 'error'
484 );
485 }
486 else{
487 update_option( $name, $value, 'yes' );
488 }
489 }
490
491 if(empty($notices)){
492 $notices[] = array(
493 'message' => 'Settings updated successfully.',
494 'type' => 'success'
495 );
496 }
497
498 new Notifier_Admin_Notices($notices);
499 }
500
501 /**
502 * Disconnect WANotifier.com connection
503 */
504 public static function disconnect_notifier(){
505 if(isset($_GET['notifier_disconnect']) && '1' == $_GET['notifier_disconnect']){
506 delete_option( NOTIFIER_PREFIX . 'api_key' );
507 delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
508 delete_option( NOTIFIER_PREFIX . 'api_activated' );
509 $notices[] = array(
510 'message' => 'Disconnected successfully. To re-connect with WANotifier.com, enter API key below and click on <b>Save and validate</b>.',
511 'type' => 'success'
512 );
513 new Notifier_Admin_Notices($notices, true);
514 wp_redirect(admin_url('admin.php?page=notifier'));
515 }
516 }
517
518 /**
519 * Show Chat Button Preview
520 */
521 public static function preview_btn_style(){
522 $btn_style = isset($_POST['btn_style']) ? sanitize_text_field($_POST['btn_style']) : '';
523
524 ob_start();
525 include_once NOTIFIER_PATH.'templates/buttons/'.$btn_style.'.php';
526 $btn_output = ob_get_clean();
527
528 wp_send_json( array(
529 'preview' => $btn_output
530 ) );
531 }
532 }
533