| @@ -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,37 @@ | ||
| 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 | + 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; | |
| 79 | 189 | } |
| 80 | 190 | $settings = apply_filters( 'notifier_$tab_settings_fields', $settings ); |
| 81 | 191 | return $settings; |
| 82 | 192 | } |
| @@ -91,21 +201,41 @@ | ||
| 91 | 201 | $option_name = NOTIFIER_PREFIX . $field['id']; |
| 92 | 202 | $option = get_option( $option_name ); |
| 93 | 203 | } |
| 94 | 204 | |
| 95 | - $data = ''; | |
| 96 | - if ( isset( $field['default'] ) ) { | |
| 205 | + $data = isset($option) ? $option : ''; | |
| 206 | + if ( isset( $field['default'] ) && empty($option) ) { | |
| 97 | 207 | $data = $field['default']; |
| 98 | - if ( isset( $option ) && '' != $option) { | |
| 99 | - $data = $option; | |
| 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 ) . '"'; | |
| 100 | 214 | } |
| 101 | 215 | } |
| 102 | 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 | + | |
| 103 | 233 | 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>'; | |
| 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>'; | |
| 106 | 236 | } else { |
| 107 | - $html .= '<tr>'; | |
| 237 | + $html .= '<tr class="'.esc_attr($tr_class).'">'; | |
| 108 | 238 | $html .= '<th>' . $field['title'] . '</th>'; |
| 109 | 239 | $html .= '<td>'; |
| 110 | 240 | |
| 111 | 241 | switch ( $field['type'] ) { |
| @@ -112,21 +242,21 @@ | ||
| 112 | 242 | |
| 113 | 243 | case 'text': |
| 114 | 244 | case 'password': |
| 115 | 245 | 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 . '"/>'; | |
| 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.'/>'; | |
| 117 | 247 | break; |
| 118 | 248 | |
| 119 | 249 | 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/>'; | |
| 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/>'; | |
| 121 | 251 | break; |
| 122 | 252 | |
| 123 | 253 | case 'checkbox': |
| 124 | 254 | $checked = ''; |
| 125 | - if ( $option && 'on' == $option ) { | |
| 255 | + if ( $option && 'yes' == $option ) { | |
| 126 | 256 | $checked = 'checked="checked"'; |
| 127 | 257 | } |
| 128 | - $html .= '<label><input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . $field['label'] . '</label>'; | |
| 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>'; | |
| 129 | 259 | break; |
| 130 | 260 | |
| 131 | 261 | case 'checkbox_multi': |
| 132 | 262 | foreach ( $field['options'] as $k => $v ) { |
| @@ -133,9 +263,9 @@ | ||
| 133 | 263 | $checked = false; |
| 134 | 264 | if ( in_array( $k, $data ) ) { |
| 135 | 265 | $checked = true; |
| 136 | 266 | } |
| 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>'; | |
| 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>'; | |
| 138 | 268 | } |
| 139 | 269 | break; |
| 140 | 270 | |
| 141 | 271 | case 'radio': |
| @@ -143,14 +273,14 @@ | ||
| 143 | 273 | $checked = false; |
| 144 | 274 | if ( $k == $data ) { |
| 145 | 275 | $checked = true; |
| 146 | 276 | } |
| 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>'; | |
| 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>'; | |
| 148 | 278 | } |
| 149 | 279 | break; |
| 150 | 280 | |
| 151 | 281 | case 'select': |
| 152 | - $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '">'; | |
| 282 | + $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '" '.$custom_attributes_string.'>'; | |
| 153 | 283 | foreach ( $field['options'] as $k => $v ) { |
| 154 | 284 | $selected = false; |
| 155 | 285 | if ( $k == $data ) { |
| 156 | 286 | $selected = true; |
| @@ -160,9 +290,9 @@ | ||
| 160 | 290 | $html .= '</select> '; |
| 161 | 291 | break; |
| 162 | 292 | |
| 163 | 293 | case 'select_multi': |
| 164 | - $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple">'; | |
| 294 | + $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple" '.$custom_attributes_string.'>'; | |
| 165 | 295 | foreach ( $field['options'] as $k => $v ) { |
| 166 | 296 | $selected = false; |
| 167 | 297 | if ( in_array( $k, $data ) ) { |
| 168 | 298 | $selected = true; |
| @@ -226,9 +356,9 @@ | ||
| 226 | 356 | |
| 227 | 357 | } |
| 228 | 358 | |
| 229 | 359 | if ( '' != $field['description'] ) { |
| 230 | - $html .= '<p class="description">' . esc_html($field['description']) . '</p>'; | |
| 360 | + $html .= '<p class="description">' . wp_kses_post($field['description']) . '</p>'; | |
| 231 | 361 | } |
| 232 | 362 | } |
| 233 | 363 | |
| 234 | 364 | //phpcs:ignore |
| @@ -277,8 +407,12 @@ | ||
| 277 | 407 | if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) { |
| 278 | 408 | continue; |
| 279 | 409 | } |
| 280 | 410 | |
| 411 | + if ( isset( $option['disabled'] ) && $option['disabled'] ) { | |
| 412 | + continue; | |
| 413 | + } | |
| 414 | + | |
| 281 | 415 | $option_name = NOTIFIER_PREFIX . $option['id']; |
| 282 | 416 | $setting_name = ''; |
| 283 | 417 | $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null; |
| 284 | 418 | |
| @@ -321,19 +455,22 @@ | ||
| 321 | 455 | // Save all options in our array. |
| 322 | 456 | foreach ( $update_options as $name => $value ) { |
| 323 | 457 | if('notifier_api_key' == $name){ |
| 324 | 458 | $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'); | |
| 459 | + if($current_api_key == $value){ | |
| 460 | + continue; | |
| 461 | + } | |
| 328 | 462 | |
| 463 | + update_option( NOTIFIER_PREFIX . 'api_key', $value); | |
| 464 | + delete_option( NOTIFIER_PREFIX . 'enabled_triggers' ); | |
| 465 | + delete_option( NOTIFIER_PREFIX . 'api_activated' ); | |
| 466 | + | |
| 329 | 467 | $params = array( |
| 330 | - 'action' => 'verify_api', | |
| 331 | 468 | 'site_url' => site_url(), |
| 332 | 469 | 'source' => 'wp' |
| 333 | 470 | ); |
| 334 | 471 | |
| 335 | - $response = Notifier::send_api_request( $params, 'POST' ); | |
| 472 | + $response = Notifier::send_api_request( 'verify_api', $params, 'POST' ); | |
| 336 | 473 | |
| 337 | 474 | if($response->error){ |
| 338 | 475 | $notices[] = array( |
| 339 | 476 | 'message' => 'There was an error validating API key. Error: ' . $response->message, |
| @@ -342,18 +479,28 @@ | ||
| 342 | 479 | } |
| 343 | 480 | else{ |
| 344 | 481 | update_option('notifier_api_activated', 'yes'); |
| 345 | 482 | $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.', | |
| 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.', | |
| 347 | 484 | 'type' => 'success' |
| 348 | 485 | ); |
| 349 | 486 | } |
| 350 | 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 | + } | |
| 351 | 494 | else{ |
| 352 | 495 | update_option( $name, $value, 'yes' ); |
| 353 | 496 | } |
| 354 | 497 | } |
| 355 | 498 | |
| 499 | + if('advanced' == $tab){ | |
| 500 | + delete_transient( '_notifier_custom_meta_keys' ); | |
| 501 | + } | |
| 502 | + | |
| 356 | 503 | if(empty($notices)){ |
| 357 | 504 | $notices[] = array( |
| 358 | 505 | 'message' => 'Settings updated successfully.', |
| 359 | 506 | 'type' => 'success' |
| @@ -362,5 +509,36 @@ | ||
| 362 | 509 | |
| 363 | 510 | new Notifier_Admin_Notices($notices); |
| 364 | 511 | } |
| 365 | 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 | + } | |
| 366 | 544 | } |