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

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