PluginProbe
WANotifier for Forms and Actions / 2.4.0
WANotifier for Forms and Actions v2.4.0
3.2.0 3.1.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 All 68 releases
← All changes | includes/classes/class-notifier-settings.php +194 -28 1.0.2 → 2.4.0 View file →
@@ -11,8 +11,10 @@
11 11 */
12 12 public static function init() {
13 13 add_action( 'admin_menu', array( __CLASS__ , 'setup_admin_page') );
14 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'));
15 17 }
16 18
17 19 /**
18 20 * Add settings page to men
@@ -17,8 +19,12 @@
17 19 /**
18 20 * Add settings page to men
19 21 */
20 22 public static function setup_admin_page () {
23 + if (!Notifier::is_api_active()){
24 + return;
25 + }
26 +
21 27 add_submenu_page( NOTIFIER_NAME, 'Settings', 'Settings', 'manage_options', NOTIFIER_NAME . '-settings', array( __CLASS__, 'output' ) );
22 28 }
23 29
24 30 /**
@@ -32,9 +38,12 @@
32 38 * Get settings tabs
33 39 */
34 40 private static function get_settings_tabs() {
35 41 $tabs = array(
36 - 'general' => 'General'
42 + 'general' => 'General',
43 + 'click_to_chat' => 'Click to Chat',
44 + 'api' => 'API',
45 + 'advanced' => 'Advanced'
37 46 );
38 47 return $tabs;
39 48 }
40 49
@@ -49,14 +58,16 @@
49 58 /**
50 59 * Settings fields
51 60 */
52 61 private static function settings_fields($tab) {
53 - $activated = get_option(NOTIFIER_PREFIX . 'api_activated');
54 - if('yes' == $activated) {
55 - $description = 'You are successfully connected to WANotifier.com.';
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>.';
56 66 }
57 67 else{
58 - $description = 'You are not connected to WANotifier.com yet. Please enter valid API key and save the settings.';
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>.';
59 70 }
60 71 $settings = array();
61 72 switch ($tab) {
62 73 case 'general':
@@ -62,8 +73,82 @@
62 73 case 'general':
63 74 $settings = array(
64 75 array(
65 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',
66 151 'description' => '',
67 152 'type' => 'title',
68 153 ),
69 154 array(
@@ -71,12 +156,29 @@
71 156 'title' => 'WANotifier.com API key',
72 157 'type' => 'text',
73 158 'placeholder' => 'Enter your WANotifier.com API key here',
74 159 'default' => '',
75 - 'description' => $description
160 + 'description' => $description,
161 + 'disabled' => $disabled
76 162 ),
77 163 );
78 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;
79 181 }
80 182 $settings = apply_filters( 'notifier_$tab_settings_fields', $settings );
81 183 return $settings;
82 184 }
@@ -91,21 +193,41 @@
91 193 $option_name = NOTIFIER_PREFIX . $field['id'];
92 194 $option = get_option( $option_name );
93 195 }
94 196
95 - $data = '';
96 - if ( isset( $field['default'] ) ) {
197 + $data = $option;
198 + if ( isset( $field['default'] ) && empty($option) ) {
97 199 $data = $field['default'];
98 - if ( isset( $option ) && '' != $option) {
99 - $data = $option;
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 ) . '"';
100 206 }
101 207 }
102 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 +
103 225 if ( 'title' === $field['type']) {
104 - $html .= '<tr><th class="section-title" colspan="2"><h3>' . $field['title'] . '</h3>';
105 - $html .= '<p>' . $field['description'] . '</p></th></tr>';
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>';
106 228 } else {
107 - $html .= '<tr>';
229 + $html .= '<tr class="'.esc_attr($tr_class).'">';
108 230 $html .= '<th>' . $field['title'] . '</th>';
109 231 $html .= '<td>';
110 232
111 233 switch ( $field['type'] ) {
@@ -112,21 +234,21 @@
112 234
113 235 case 'text':
114 236 case 'password':
115 237 case 'number':
116 - $html .= '<input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . $data . '"/>';
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.'/>';
117 239 break;
118 240
119 241 case 'textarea':
120 - $html .= '<textarea id="' . esc_attr( $option_name ) . '" rows="5" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '">' . $data . '</textarea><br/>';
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/>';
121 243 break;
122 244
123 245 case 'checkbox':
124 246 $checked = '';
125 - if ( $option && 'on' == $option ) {
247 + if ( $option && 'yes' == $option ) {
126 248 $checked = 'checked="checked"';
127 249 }
128 - $html .= '<label><input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . $field['label'] . '</label>';
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>';
129 251 break;
130 252
131 253 case 'checkbox_multi':
132 254 foreach ( $field['options'] as $k => $v ) {
@@ -133,9 +255,9 @@
133 255 $checked = false;
134 256 if ( in_array( $k, $data ) ) {
135 257 $checked = true;
136 258 }
137 - $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 ) . '" /> ' . $v . '</label></p>';
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>';
138 260 }
139 261 break;
140 262
141 263 case 'radio':
@@ -143,14 +265,14 @@
143 265 $checked = false;
144 266 if ( $k == $data ) {
145 267 $checked = true;
146 268 }
147 - $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 ) . '" /> ' . $v . '</label></p>';
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>';
148 270 }
149 271 break;
150 272
151 273 case 'select':
152 - $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '">';
274 + $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '" '.$custom_attributes_string.'>';
153 275 foreach ( $field['options'] as $k => $v ) {
154 276 $selected = false;
155 277 if ( $k == $data ) {
156 278 $selected = true;
@@ -160,9 +282,9 @@
160 282 $html .= '</select> ';
161 283 break;
162 284
163 285 case 'select_multi':
164 - $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple">';
286 + $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple" '.$custom_attributes_string.'>';
165 287 foreach ( $field['options'] as $k => $v ) {
166 288 $selected = false;
167 289 if ( in_array( $k, $data ) ) {
168 290 $selected = true;
@@ -226,9 +348,9 @@
226 348
227 349 }
228 350
229 351 if ( '' != $field['description'] ) {
230 - $html .= '<p class="description">' . esc_html($field['description']) . '</p>';
352 + $html .= '<p class="description">' . wp_kses_post($field['description']) . '</p>';
231 353 }
232 354 }
233 355
234 356 //phpcs:ignore
@@ -277,8 +399,12 @@
277 399 if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) {
278 400 continue;
279 401 }
280 402
403 + if ( isset( $option['disabled'] ) && $option['disabled'] ) {
404 + continue;
405 + }
406 +
281 407 $option_name = NOTIFIER_PREFIX . $option['id'];
282 408 $setting_name = '';
283 409 $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null;
284 410
@@ -321,19 +447,22 @@
321 447 // Save all options in our array.
322 448 foreach ( $update_options as $name => $value ) {
323 449 if('notifier_api_key' == $name){
324 450 $current_api_key = get_option($name);
325 - update_option('notifier_api_key', $value);
326 - delete_option('notifier_enabled_triggers');
327 - delete_option('notifier_api_activated');
451 + if($current_api_key == $value){
452 + continue;
453 + }
328 454
455 + update_option( NOTIFIER_PREFIX . 'api_key', $value);
456 + delete_option( NOTIFIER_PREFIX . 'enabled_triggers' );
457 + delete_option( NOTIFIER_PREFIX . 'api_activated' );
458 +
329 459 $params = array(
330 - 'action' => 'verify_api',
331 460 'site_url' => site_url(),
332 461 'source' => 'wp'
333 462 );
334 463
335 - $response = Notifier::send_api_request( $params, 'POST' );
464 + $response = Notifier::send_api_request( 'verify_api', $params, 'POST' );
336 465
337 466 if($response->error){
338 467 $notices[] = array(
339 468 'message' => 'There was an error validating API key. Error: ' . $response->message,
@@ -342,13 +471,19 @@
342 471 }
343 472 else{
344 473 update_option('notifier_api_activated', 'yes');
345 474 $notices[] = array(
346 - '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').'">WA Notifier</a> page.',
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.',
347 476 'type' => 'success'
348 477 );
349 478 }
350 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 + }
351 486 else{
352 487 update_option( $name, $value, 'yes' );
353 488 }
354 489 }
@@ -362,5 +497,36 @@
362 497
363 498 new Notifier_Admin_Notices($notices);
364 499 }
365 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 + }
366 532 }