| @@ -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,11 +38,12 @@ | ||
| 32 | 38 | * Get settings tabs |
| 33 | 39 | */ |
| 34 | 40 | private static function get_settings_tabs() { |
| 35 | 41 | $tabs = array( |
| 36 | - 'profile' => 'WhatsApp Profile', | |
| 37 | - 'general' => 'General', | |
| 38 | - 'api' => 'API Configuration', | |
| 42 | + 'general' => 'General', | |
| 43 | + 'click_to_chat' => 'Click to Chat', | |
| 44 | + 'api' => 'API', | |
| 45 | + 'advanced' => 'Advanced' | |
| 39 | 46 | ); |
| 40 | 47 | return $tabs; |
| 41 | 48 | } |
| 42 | 49 | |
| @@ -51,149 +58,166 @@ | ||
| 51 | 58 | /** |
| 52 | 59 | * Settings fields |
| 53 | 60 | */ |
| 54 | 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 | + } | |
| 55 | 71 | $settings = array(); |
| 56 | 72 | switch ($tab) { |
| 57 | - case 'profile': | |
| 73 | + case 'general': | |
| 58 | 74 | $settings = array( |
| 59 | 75 | array( |
| 60 | - 'title' => 'WhatsApp Profile', | |
| 61 | - 'description' => 'Update your WhatsApp Business profile details. These details will be visible to contacts when they open your profile on WhatsApp.', | |
| 76 | + 'title' => 'General', | |
| 62 | 77 | 'type' => 'title', |
| 78 | + 'description' => '' | |
| 63 | 79 | ), |
| 64 | 80 | array( |
| 65 | - 'id' => 'wa_profile_picture', | |
| 66 | - 'title' => 'Profile Picture', | |
| 67 | - 'description' => 'Recommended profile image size: 640px X 640px.', | |
| 68 | - 'type' => 'media', | |
| 69 | - 'default' => '', | |
| 70 | - 'placeholder' => '', | |
| 71 | - 'uploader_title' => 'WhatsApp profile image', | |
| 72 | - 'uploader_button_text' => 'Select', | |
| 73 | - 'uploader_supported_file_types' => array('image') | |
| 81 | + 'id' => 'default_country_code', | |
| 82 | + 'title' => 'Default Country Code (Optional)', | |
| 83 | + 'type' => 'text', | |
| 84 | + 'placeholder' => 'Eg. +91', | |
| 85 | + '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.', | |
| 86 | + 'default' => '' | |
| 74 | 87 | ), |
| 88 | + ); | |
| 89 | + | |
| 90 | + if ( class_exists( 'WooCommerce' ) ) { | |
| 91 | + $settings = array_merge( $settings, array( | |
| 92 | + array( | |
| 93 | + 'title' => 'WooCommerce', | |
| 94 | + 'type' => 'title', | |
| 95 | + 'description' => '' | |
| 96 | + ), | |
| 97 | + array( | |
| 98 | + 'id' => 'enable_opt_in_checkbox_checkout', | |
| 99 | + 'title' => 'Opt-in Consent on Checkout', | |
| 100 | + 'type' => 'checkbox', | |
| 101 | + 'default' => '', | |
| 102 | + 'label' => 'Enable', | |
| 103 | + 'name' => 'enable_opt_in_checkbox_checkout', | |
| 104 | + 'description' => 'Add an opt-in checkbox to WooCommerce checkout form. Once enabled, WhatsApp notification will be sent only to customers who opt-in during checkout.' | |
| 105 | + ), | |
| 106 | + array( | |
| 107 | + 'id' => 'checkout_opt_in_checkbox_text', | |
| 108 | + 'title' => 'Opt-in Checkbox Text', | |
| 109 | + 'type' => 'textarea', | |
| 110 | + 'placeholder' => 'Enter text for the opt-in checkbox', | |
| 111 | + 'default' => 'Receive updates on WhatsApp', | |
| 112 | + 'name' => 'checkout_opt_in_checkbox_text', | |
| 113 | + ) | |
| 114 | + ) ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + break; | |
| 118 | + case 'click_to_chat': | |
| 119 | + $settings = array( | |
| 75 | 120 | array( |
| 76 | - 'id' => 'wa_profile_description', | |
| 77 | - 'title' => 'Description', | |
| 78 | - 'description' => '', | |
| 79 | - 'type' => 'text', | |
| 80 | - 'default' => '', | |
| 81 | - 'placeholder' => '' | |
| 121 | + 'title' => 'Click to Chat', | |
| 122 | + 'description' => 'Show click to chat button on your website to let your visitors start WhatsApp chat with you.', | |
| 123 | + 'type' => 'title', | |
| 82 | 124 | ), |
| 83 | 125 | array( |
| 84 | - 'id' => 'wa_profile_address', | |
| 85 | - 'title' => 'Address', | |
| 86 | - 'description' => '', | |
| 87 | - 'type' => 'text', | |
| 126 | + 'id' => 'ctc_enable', | |
| 127 | + 'title' => 'Enable', | |
| 128 | + 'type' => 'checkbox', | |
| 88 | 129 | 'default' => '', |
| 89 | - 'placeholder' => '' | |
| 130 | + 'name' => 'ctc_enable', | |
| 90 | 131 | ), |
| 91 | 132 | array( |
| 92 | - 'id' => 'wa_profile_category', | |
| 93 | - 'title' => 'Category', | |
| 94 | - 'description' => '', | |
| 95 | - 'type' => 'select', | |
| 96 | - 'options' => array( | |
| 97 | - 'UNDEFINED' => 'Select your business industry', | |
| 98 | - 'NOT_A_BIZ' => 'Not a business', | |
| 99 | - 'AUTO' => 'Automotive', | |
| 100 | - 'BEAUTY' => 'Beauty, Spa & Salon', | |
| 101 | - 'APPAREL' => 'Clothing & Apparel', | |
| 102 | - 'EDU' => 'Education', | |
| 103 | - 'ENTERTAIN' => 'Entertainment', | |
| 104 | - 'EVENT_PLAN'=> 'Event Planning & Service', | |
| 105 | - 'FINANCE' => 'Finance & Banking', | |
| 106 | - 'GROCERY' => 'Grocery & Supermarket', | |
| 107 | - 'GOVT' => 'Public & Government Service', | |
| 108 | - 'HOTEL' => 'Hotel & Lodging', | |
| 109 | - 'HEALTH' => 'Medical & Health', | |
| 110 | - 'NONPROFIT' => 'Non-profit', | |
| 111 | - 'PROF_SERVICES' => 'Professional Services', | |
| 112 | - 'RETAIL' => 'Shopping & Retail', | |
| 113 | - 'TRAVEL' => 'Travel & Transportation', | |
| 114 | - 'RESTAURANT'=> 'Restaurant', | |
| 115 | - 'OTHER' => 'Other' | |
| 116 | - ), | |
| 117 | - 'default' => '', | |
| 118 | - 'placeholder' => '' | |
| 133 | + 'id' => 'ctc_whatsapp_number', | |
| 134 | + 'title' => 'Whatsapp Number', | |
| 135 | + 'type' => 'text', | |
| 136 | + 'placeholder' => 'Enter your WhatsApp number', | |
| 137 | + 'name' => 'ctc_whatsapp_number', | |
| 138 | + 'description' => 'Enter your WhatsApp number with country code. Eg. +919876543210', | |
| 119 | 139 | ), |
| 120 | 140 | array( |
| 121 | - 'id' => 'wa_profile_email', | |
| 122 | - 'title' => 'Email (optional)', | |
| 123 | - 'description' => '', | |
| 141 | + 'id' => 'ctc_greeting_text', | |
| 142 | + 'title' => 'Greeting Message', | |
| 124 | 143 | 'type' => 'text', |
| 125 | - 'default' => '', | |
| 126 | - 'placeholder' => '' | |
| 144 | + 'name' => 'ctc_greeting_text', | |
| 145 | + 'placeholder' => 'Enter your greeting message here', | |
| 146 | + 'description' => 'This text will be added to user\'s WhatsApp chat text field when they click on the button.' , | |
| 127 | 147 | ), |
| 128 | 148 | array( |
| 129 | - 'id' => 'wa_profile_website_1', | |
| 130 | - 'title' => 'Website URL 1', | |
| 131 | - 'description' => '', | |
| 132 | - 'type' => 'text', | |
| 149 | + 'id' => 'ctc_button_style', | |
| 150 | + 'title' => 'Button Style', | |
| 151 | + 'name' => 'ctc_button_style', | |
| 152 | + 'class' => 'chat-button-style', | |
| 153 | + 'type' => 'select', | |
| 133 | 154 | 'default' => '', |
| 134 | - 'placeholder' => 'https://' | |
| 155 | + 'options' => self::get_button_styles(), | |
| 156 | + '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 | 157 | ), |
| 136 | 158 | array( |
| 137 | - 'id' => 'wa_profile_website_2', | |
| 138 | - 'title' => 'Website URL 2', | |
| 139 | - 'description' => '', | |
| 159 | + 'id' => 'ctc_custom_button_image_url', | |
| 160 | + 'title' => 'Button image url', | |
| 140 | 161 | 'type' => 'text', |
| 141 | - 'default' => '', | |
| 142 | - 'placeholder' => 'https://' | |
| 143 | - ) | |
| 162 | + 'placeholder' => 'https://', | |
| 163 | + 'name' => 'ctc_custom_button_image_url', | |
| 164 | + 'description' => 'Enter button image url here.', | |
| 165 | + 'tr_class' => 'notifier-chat-btn-image-url', | |
| 166 | + ), | |
| 144 | 167 | ); |
| 145 | 168 | break; |
| 146 | - | |
| 147 | - case 'general': | |
| 169 | + case 'api': | |
| 148 | 170 | $settings = array( |
| 149 | 171 | array( |
| 150 | - 'title' => 'General', | |
| 172 | + 'title' => 'API Configuration', | |
| 151 | 173 | 'description' => '', |
| 152 | 174 | 'type' => 'title', |
| 153 | 175 | ), |
| 154 | 176 | array( |
| 155 | - 'id' => 'bulk_message_batch_limit', | |
| 156 | - 'title' => 'Bulk message batch limit', | |
| 157 | - 'description' => 'Enter the number of messages you want to send in each batch when sending bulk messages. Default value is <code>50</code>. If you want send more number of messages in each batch and have good server resources you can increase the limit here.', | |
| 158 | - 'type' => 'number', | |
| 159 | - 'default' => 50, | |
| 160 | - 'placeholder' => '' | |
| 177 | + 'id' => 'api_key', | |
| 178 | + 'title' => 'WANotifier.com API key', | |
| 179 | + 'type' => 'text', | |
| 180 | + 'placeholder' => 'Enter your WANotifier.com API key here', | |
| 181 | + 'default' => '', | |
| 182 | + 'description' => $description, | |
| 183 | + 'disabled' => $disabled | |
| 161 | 184 | ), |
| 162 | 185 | ); |
| 163 | 186 | break; |
| 164 | - | |
| 165 | - case 'api': | |
| 187 | + case 'advanced': | |
| 166 | 188 | $settings = array( |
| 167 | 189 | array( |
| 168 | - 'title' => 'WhastApp API Configuration', | |
| 169 | - 'description' => 'Enter the API details below to setup WhatsApp.', | |
| 190 | + 'title' => 'Advanced Settings', | |
| 191 | + 'description' => '', | |
| 170 | 192 | 'type' => 'title', |
| 171 | 193 | ), |
| 172 | 194 | array( |
| 173 | - 'id' => 'phone_number_id', | |
| 174 | - 'title' => 'Phone Number ID', | |
| 175 | - 'description' => '', | |
| 176 | - 'type' => 'text', | |
| 177 | - 'default' => '', | |
| 178 | - 'placeholder' => '' | |
| 195 | + 'id' => 'hidden_custom_keys', | |
| 196 | + 'title' => 'Hidden custom meta keys', | |
| 197 | + 'type' => 'checkbox', | |
| 198 | + 'label' => 'Enable', | |
| 199 | + '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.', | |
| 200 | + 'default' => '' | |
| 179 | 201 | ), |
| 180 | 202 | array( |
| 181 | - 'id' => 'business_account_id', | |
| 182 | - 'title' => 'Business Account ID', | |
| 183 | - 'description' => '', | |
| 184 | - 'type' => 'text', | |
| 203 | + 'id' => 'enable_async_triggers', | |
| 204 | + 'title' => 'Async triggers', | |
| 205 | + 'type' => 'checkbox', | |
| 185 | 206 | 'default' => '', |
| 186 | - 'placeholder' => '' | |
| 207 | + 'label' => 'Enable', | |
| 208 | + 'name' => 'enable_async_triggers', | |
| 209 | + '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.' , | |
| 187 | 210 | ), |
| 188 | 211 | array( |
| 189 | - 'id' => 'permanent_access_token', | |
| 190 | - 'title' => 'Permanent Access Token', | |
| 191 | - 'description' => '', | |
| 192 | - 'type' => 'textarea', | |
| 212 | + 'id' => 'enable_activity_log', | |
| 213 | + 'title' => 'Activity log', | |
| 214 | + 'type' => 'checkbox', | |
| 193 | 215 | 'default' => '', |
| 194 | - 'placeholder' => '' | |
| 195 | - ), | |
| 216 | + 'label' => 'Enable', | |
| 217 | + 'name' => 'enable_activity_log', | |
| 218 | + 'description' => 'Enabling this option will activate the activity logging feature. You can view activity logs <a href="/wp-admin/admin.php?page=notifier-tools">here</a>.' , | |
| 219 | + ), | |
| 196 | 220 | ); |
| 197 | 221 | break; |
| 198 | 222 | } |
| 199 | 223 | $settings = apply_filters( 'notifier_$tab_settings_fields', $settings ); |
| @@ -203,8 +227,13 @@ | ||
| 203 | 227 | /** |
| 204 | 228 | * Generate HTML for displaying individual fields |
| 205 | 229 | */ |
| 206 | 230 | public static function display_field( $field ) { |
| 231 | + $field = wp_parse_args($field, array( | |
| 232 | + 'title' => '', | |
| 233 | + 'description' => '', | |
| 234 | + 'label' => '' | |
| 235 | + )); | |
| 207 | 236 | |
| 208 | 237 | $html = ''; |
| 209 | 238 | |
| 210 | 239 | if (isset($field['id'])) { |
| @@ -211,21 +240,41 @@ | ||
| 211 | 240 | $option_name = NOTIFIER_PREFIX . $field['id']; |
| 212 | 241 | $option = get_option( $option_name ); |
| 213 | 242 | } |
| 214 | 243 | |
| 215 | - $data = ''; | |
| 216 | - if ( isset( $field['default'] ) ) { | |
| 244 | + $data = isset($option) ? $option : ''; | |
| 245 | + if ( isset( $field['default'] ) && empty($option) ) { | |
| 217 | 246 | $data = $field['default']; |
| 218 | - if ( isset( $option ) && '' != $option) { | |
| 219 | - $data = $option; | |
| 247 | + } | |
| 248 | + | |
| 249 | + $custom_attributes = array(); | |
| 250 | + if ( ! empty( $field['custom_attributes'] ) && is_array( $field['custom_attributes'] ) ) { | |
| 251 | + foreach ( $field['custom_attributes'] as $attribute => $value ) { | |
| 252 | + $custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"'; | |
| 220 | 253 | } |
| 221 | 254 | } |
| 222 | 255 | |
| 256 | + if ( isset($field['required']) && $field['required'] ) { | |
| 257 | + $custom_attributes[] = 'required="required"'; | |
| 258 | + } | |
| 259 | + | |
| 260 | + if ( isset($field['disabled']) && $field['disabled'] ) { | |
| 261 | + $custom_attributes[] = 'disabled="disabled"'; | |
| 262 | + } | |
| 263 | + | |
| 264 | + if(isset($field['tr_class'])){ | |
| 265 | + $tr_class = $field['tr_class']; | |
| 266 | + }else{ | |
| 267 | + $tr_class = ''; | |
| 268 | + } | |
| 269 | + | |
| 270 | + $custom_attributes_string = implode( ' ', $custom_attributes ); | |
| 271 | + | |
| 223 | 272 | if ( 'title' === $field['type']) { |
| 224 | - $html .= '<tr><th class="section-title" colspan="2"><h3>' . $field['title'] . '</h3>'; | |
| 225 | - $html .= '<p>' . $field['description'] . '</p></th></tr>'; | |
| 273 | + $html .= '<tr class="'.esc_attr($tr_class).'"><th class="section-title" colspan="2"><h3>' . $field['title'] . '</h3>'; | |
| 274 | + $html .= '<p class="description">' . $field['description'] . '</p></th></tr>'; | |
| 226 | 275 | } else { |
| 227 | - $html .= '<tr>'; | |
| 276 | + $html .= '<tr class="'.esc_attr($tr_class).'">'; | |
| 228 | 277 | $html .= '<th>' . $field['title'] . '</th>'; |
| 229 | 278 | $html .= '<td>'; |
| 230 | 279 | |
| 231 | 280 | switch ( $field['type'] ) { |
| @@ -232,21 +281,21 @@ | ||
| 232 | 281 | |
| 233 | 282 | case 'text': |
| 234 | 283 | case 'password': |
| 235 | 284 | case 'number': |
| 236 | - $html .= '<input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . $data . '"/>'; | |
| 285 | + $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.'/>'; | |
| 237 | 286 | break; |
| 238 | 287 | |
| 239 | 288 | case 'textarea': |
| 240 | - $html .= '<textarea id="' . esc_attr( $option_name ) . '" rows="5" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '">' . $data . '</textarea><br/>'; | |
| 289 | + $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/>'; | |
| 241 | 290 | break; |
| 242 | 291 | |
| 243 | 292 | case 'checkbox': |
| 244 | 293 | $checked = ''; |
| 245 | - if ( $option && 'on' == $option ) { | |
| 294 | + if ( $option && 'yes' == $option ) { | |
| 246 | 295 | $checked = 'checked="checked"'; |
| 247 | 296 | } |
| 248 | - $html .= '<label><input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . $field['label'] . '</label>'; | |
| 297 | + $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>'; | |
| 249 | 298 | break; |
| 250 | 299 | |
| 251 | 300 | case 'checkbox_multi': |
| 252 | 301 | foreach ( $field['options'] as $k => $v ) { |
| @@ -253,9 +302,9 @@ | ||
| 253 | 302 | $checked = false; |
| 254 | 303 | if ( in_array( $k, $data ) ) { |
| 255 | 304 | $checked = true; |
| 256 | 305 | } |
| 257 | - $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>'; | |
| 306 | + $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>'; | |
| 258 | 307 | } |
| 259 | 308 | break; |
| 260 | 309 | |
| 261 | 310 | case 'radio': |
| @@ -263,14 +312,14 @@ | ||
| 263 | 312 | $checked = false; |
| 264 | 313 | if ( $k == $data ) { |
| 265 | 314 | $checked = true; |
| 266 | 315 | } |
| 267 | - $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>'; | |
| 316 | + $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>'; | |
| 268 | 317 | } |
| 269 | 318 | break; |
| 270 | 319 | |
| 271 | 320 | case 'select': |
| 272 | - $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '">'; | |
| 321 | + $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '" '.$custom_attributes_string.'>'; | |
| 273 | 322 | foreach ( $field['options'] as $k => $v ) { |
| 274 | 323 | $selected = false; |
| 275 | 324 | if ( $k == $data ) { |
| 276 | 325 | $selected = true; |
| @@ -280,9 +329,9 @@ | ||
| 280 | 329 | $html .= '</select> '; |
| 281 | 330 | break; |
| 282 | 331 | |
| 283 | 332 | case 'select_multi': |
| 284 | - $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple">'; | |
| 333 | + $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple" '.$custom_attributes_string.'>'; | |
| 285 | 334 | foreach ( $field['options'] as $k => $v ) { |
| 286 | 335 | $selected = false; |
| 287 | 336 | if ( in_array( $k, $data ) ) { |
| 288 | 337 | $selected = true; |
| @@ -291,23 +340,44 @@ | ||
| 291 | 340 | } |
| 292 | 341 | $html .= '</select> '; |
| 293 | 342 | break; |
| 294 | 343 | |
| 295 | - case 'media': | |
| 344 | + case 'file': | |
| 296 | 345 | $uploader_title = isset($field['uploader_title']) ? $field['uploader_title'] : 'Upload media'; |
| 297 | 346 | $uploader_button_text = isset($field['uploader_button_text']) ? $field['uploader_button_text'] : 'Select'; |
| 298 | - $file_types = isset($field['uploader_supported_file_types']) ? implode(',', $field['uploader_supported_file_types']) : array(); | |
| 347 | + $file_types = isset($field['uploader_supported_file_types']) ? $field['uploader_supported_file_types'] : array(); | |
| 299 | 348 | |
| 300 | 349 | $image_thumb = ''; |
| 301 | - if ( $data ) { | |
| 302 | - $image_thumb = wp_get_attachment_thumb_url( $data ); | |
| 303 | - } | |
| 350 | + $video_url = ''; | |
| 351 | + $show_image = 'hide'; | |
| 352 | + $show_video = 'hide'; | |
| 304 | 353 | |
| 305 | 354 | $html .= '<span class="notifier-media-preview">'; |
| 306 | - if ('' != $image_thumb) { | |
| 307 | - $html .= '<img id="' . esc_attr( $option_name ) . '_preview_image" src="' . esc_url( $image_thumb ) . '" /><br/>'; | |
| 308 | - $html .= '<video id="' . esc_attr( $option_name ) . '_preview_video"><source src="' . esc_url($image_thumb) . '"></video><br/>'; | |
| 355 | + if ( '' != $data ) { | |
| 356 | + $file_type = $field['uploader_supported_file_types']; | |
| 357 | + switch ($file_type) { | |
| 358 | + case 'image': | |
| 359 | + case 'image/jpeg': | |
| 360 | + case 'image/png': | |
| 361 | + case 'application/pdf': | |
| 362 | + $image_thumb = wp_get_attachment_thumb_url( $data ); | |
| 363 | + $show_image = ''; | |
| 364 | + $show_video = 'hide'; | |
| 365 | + break; | |
| 366 | + | |
| 367 | + case 'video/mp4': | |
| 368 | + $video_url = wp_get_attachment_url( $data ); | |
| 369 | + $show_image = 'hide'; | |
| 370 | + $show_video = ''; | |
| 371 | + break; | |
| 372 | + | |
| 373 | + default: | |
| 374 | + $show_image = 'hide'; | |
| 375 | + $show_video = 'hide'; | |
| 376 | + } | |
| 309 | 377 | } |
| 378 | + $html .= '<img id="' . esc_attr( $option_name ) . '_preview_image" class="notifier-media-preview-item '. esc_attr($show_image) .'" src="' . esc_url($image_thumb) . '" />'; | |
| 379 | + $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/>'; | |
| 310 | 380 | $html .= '</span>'; |
| 311 | 381 | |
| 312 | 382 | $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' . '" /> '; |
| 313 | 383 | $html .= '<input id="' . esc_attr( $option_name ) . '_delete" type="button" class="notifier-media-delete-button button" value="' . 'Remove' . '" />'; |
| @@ -325,9 +395,9 @@ | ||
| 325 | 395 | |
| 326 | 396 | } |
| 327 | 397 | |
| 328 | 398 | if ( '' != $field['description'] ) { |
| 329 | - $html .= '<p class="description">' . esc_html($field['description']) . '</p>'; | |
| 399 | + $html .= '<p class="description">' . wp_kses_post($field['description']) . '</p>'; | |
| 330 | 400 | } |
| 331 | 401 | } |
| 332 | 402 | |
| 333 | 403 | //phpcs:ignore |
| @@ -364,9 +434,9 @@ | ||
| 364 | 434 | if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-settings' ) ) { |
| 365 | 435 | return; |
| 366 | 436 | } |
| 367 | 437 | |
| 368 | - $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'profile'; | |
| 438 | + $tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'general'; | |
| 369 | 439 | |
| 370 | 440 | $settings_fields = self::settings_fields($tab); |
| 371 | 441 | $data = $_POST; |
| 372 | 442 | $update_options = array(); |
| @@ -376,8 +446,12 @@ | ||
| 376 | 446 | if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) { |
| 377 | 447 | continue; |
| 378 | 448 | } |
| 379 | 449 | |
| 450 | + if ( isset( $option['disabled'] ) && $option['disabled'] ) { | |
| 451 | + continue; | |
| 452 | + } | |
| 453 | + | |
| 380 | 454 | $option_name = NOTIFIER_PREFIX . $option['id']; |
| 381 | 455 | $setting_name = ''; |
| 382 | 456 | $raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null; |
| 383 | 457 | |
| @@ -416,167 +490,117 @@ | ||
| 416 | 490 | $update_options[ $option_name ] = $value; |
| 417 | 491 | } |
| 418 | 492 | } |
| 419 | 493 | |
| 420 | - if ('api' == $tab) { | |
| 421 | - $phone_number_id = isset($update_options[ NOTIFIER_PREFIX . 'phone_number_id' ]) ? $update_options[ NOTIFIER_PREFIX . 'phone_number_id' ] : ''; | |
| 422 | - $business_account_id = isset($update_options[ NOTIFIER_PREFIX . 'business_account_id' ]) ? $update_options[ NOTIFIER_PREFIX . 'business_account_id' ] : ''; | |
| 423 | - $permanent_access_token = isset($update_options[ NOTIFIER_PREFIX . 'permanent_access_token' ]) ? $update_options[ NOTIFIER_PREFIX . 'permanent_access_token' ] : ''; | |
| 424 | - | |
| 425 | - if ('' == $phone_number_id || '' == $business_account_id || '' == $permanent_access_token) { | |
| 426 | - $notices[] = array( | |
| 427 | - 'message' => 'Phone number ID, Business Account ID and Permanent Access Token are mandatory fields.', | |
| 428 | - 'type' => 'error' | |
| 429 | - ); | |
| 430 | - new Notifier_Admin_Notices($notices); | |
| 431 | - return; | |
| 432 | - } | |
| 433 | - } | |
| 434 | - | |
| 435 | 494 | // Save all options in our array. |
| 436 | 495 | foreach ( $update_options as $name => $value ) { |
| 437 | - update_option( $name, $value, 'yes' ); | |
| 438 | - } | |
| 496 | + if('notifier_api_key' == $name){ | |
| 497 | + $current_api_key = get_option($name); | |
| 498 | + if($current_api_key == $value){ | |
| 499 | + continue; | |
| 500 | + } | |
| 439 | 501 | |
| 440 | - // Do other things after updating settings | |
| 502 | + update_option( NOTIFIER_PREFIX . 'api_key', $value); | |
| 503 | + delete_option( NOTIFIER_PREFIX . 'enabled_triggers' ); | |
| 504 | + delete_option( NOTIFIER_PREFIX . 'api_activated' ); | |
| 441 | 505 | |
| 442 | - switch ($tab) { | |
| 443 | - case 'profile': | |
| 444 | - $notices = self::save_whatsapp_profile_details($update_options); | |
| 445 | - break; | |
| 506 | + $params = array( | |
| 507 | + 'site_url' => site_url(), | |
| 508 | + 'source' => 'wp' | |
| 509 | + ); | |
| 446 | 510 | |
| 447 | - case 'api': | |
| 448 | - $notices = self::fetch_and_save_whatsapp_details($phone_number_id); | |
| 449 | - break; | |
| 511 | + $response = Notifier::send_api_request( 'verify_api', $params, 'POST' ); | |
| 450 | 512 | |
| 451 | - default: | |
| 513 | + if($response->error){ | |
| 514 | + $notices[] = array( | |
| 515 | + 'message' => 'There was an error validating API key. Error: ' . $response->message, | |
| 516 | + 'type' => 'error' | |
| 517 | + ); | |
| 518 | + } | |
| 519 | + else{ | |
| 520 | + update_option('notifier_api_activated', 'yes'); | |
| 521 | + $notices[] = array( | |
| 522 | + '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.', | |
| 523 | + 'type' => 'success' | |
| 524 | + ); | |
| 525 | + } | |
| 526 | + } | |
| 527 | + elseif('notifier_ctc_whatsapp_number' == $name && !notifier_validate_phone_number($value)){ | |
| 452 | 528 | $notices[] = array( |
| 453 | - 'message' => 'Settings saved.', | |
| 454 | - 'type' => 'success' | |
| 529 | + 'message' => 'Please enter a valid WhatsApp phone number with country code.', | |
| 530 | + 'type' => 'error' | |
| 455 | 531 | ); |
| 532 | + } | |
| 533 | + else{ | |
| 534 | + update_option( $name, $value, 'yes' ); | |
| 535 | + } | |
| 456 | 536 | } |
| 457 | 537 | |
| 458 | - new Notifier_Admin_Notices($notices); | |
| 459 | - } | |
| 460 | - | |
| 461 | - /** | |
| 462 | - * Save WhatsApp profile details | |
| 463 | - */ | |
| 464 | - public static function save_whatsapp_profile_details($profile_fields) { | |
| 465 | - $args = array ( | |
| 466 | - 'messaging_product' => 'whatsapp', | |
| 467 | - 'description' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_description']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_description' ] : '', | |
| 468 | - 'address' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_address']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_address' ] : '', | |
| 469 | - 'vertical' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_category']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_category' ] : '', | |
| 470 | - 'email' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_email']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_email' ] : '', | |
| 471 | - ); | |
| 472 | - | |
| 473 | - if (isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_1' ]) && '' != $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_1' ]) { | |
| 474 | - $args['websites'][] = $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_1']; | |
| 538 | + if('advanced' == $tab){ | |
| 539 | + delete_transient( '_notifier_custom_meta_keys' ); | |
| 475 | 540 | } |
| 476 | 541 | |
| 477 | - if (isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_2' ]) && '' != $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_2' ]) { | |
| 478 | - $args['websites'][] = $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_2' ]; | |
| 479 | - } | |
| 480 | - | |
| 481 | - $profile_picture_id = isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_picture']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_picture' ] : ''; | |
| 482 | - | |
| 483 | - $profile_picture_handle = Notifier::wa_cloud_api_upload_profile_pic($profile_picture_id); | |
| 484 | - | |
| 485 | - if ($profile_picture_handle) { | |
| 486 | - $args['profile_picture_handle'] = $profile_picture_handle; | |
| 487 | - } | |
| 488 | - | |
| 489 | - $response = Notifier::wa_cloud_api_request( 'whatsapp_business_profile', $args ); | |
| 490 | - | |
| 491 | - if (isset($response->error)) { | |
| 542 | + if(empty($notices)){ | |
| 492 | 543 | $notices[] = array( |
| 493 | - 'message' => 'Error Code ' . $response->error->code . ': ' . $response->error->message , | |
| 494 | - 'type' => 'error' | |
| 495 | - ); | |
| 496 | - return $notices; | |
| 497 | - } else { | |
| 498 | - $notices[] = array( | |
| 499 | - 'message' => 'Profile updated successfully.', | |
| 544 | + 'message' => 'Settings updated successfully.', | |
| 500 | 545 | 'type' => 'success' |
| 501 | 546 | ); |
| 502 | 547 | } |
| 503 | - return $notices; | |
| 548 | + | |
| 549 | + new Notifier_Admin_Notices($notices); | |
| 504 | 550 | } |
| 505 | 551 | |
| 506 | 552 | /** |
| 507 | - * Fetch and save WhatsApp details | |
| 553 | + * Disconnect WANotifier.com connection | |
| 508 | 554 | */ |
| 509 | - public static function fetch_and_save_whatsapp_details($phone_number_id) { | |
| 510 | - $response = Notifier::wa_cloud_api_request('', array(), 'GET'); | |
| 511 | - if (isset($response->error)) { | |
| 555 | + public static function disconnect_notifier(){ | |
| 556 | + if(isset($_GET['notifier_disconnect']) && '1' == $_GET['notifier_disconnect']){ | |
| 557 | + delete_option( NOTIFIER_PREFIX . 'api_key' ); | |
| 558 | + delete_option( NOTIFIER_PREFIX . 'enabled_triggers' ); | |
| 559 | + delete_option( NOTIFIER_PREFIX . 'api_activated' ); | |
| 512 | 560 | $notices[] = array( |
| 513 | - 'message' => 'API request can not be validated. Error Code ' . $response->error->code . ': ' . $response->error->message , | |
| 514 | - 'type' => 'error' | |
| 515 | - ); | |
| 516 | - return $notices; | |
| 517 | - } else { | |
| 518 | - $phone_number_details[$phone_number_id] = array ( | |
| 519 | - 'display_num' => $response->display_phone_number, | |
| 520 | - 'display_name' => $response->verified_name, | |
| 521 | - 'phone_num_status' => $response->code_verification_status, | |
| 522 | - 'quality_rating' => $response->quality_rating | |
| 523 | - ); | |
| 524 | - update_option( NOTIFIER_PREFIX . 'phone_number_details', $phone_number_details ); | |
| 525 | - } | |
| 526 | - | |
| 527 | - $response_profile = Notifier::wa_cloud_api_request('whatsapp_business_profile', array( | |
| 528 | - 'fields' => 'about,address,description,email,profile_picture_url,websites,vertical' | |
| 529 | - ), 'GET'); | |
| 530 | - | |
| 531 | - if (isset($response_profile->error)) { | |
| 532 | - $notices[] = array( | |
| 533 | - 'message' => 'WhatsApp Error Code: ' . $response_profile->error->code . ': ' . $response_profile->error->message , | |
| 534 | - 'type' => 'error' | |
| 535 | - ); | |
| 536 | - return $notices; | |
| 537 | - } else { | |
| 538 | - if (isset($response_profile->data)) { | |
| 539 | - $data = $response_profile->data[0]; | |
| 540 | - update_option( NOTIFIER_PREFIX . 'wa_profile_address', isset($data->address) ? $data->address : '' ); | |
| 541 | - update_option( NOTIFIER_PREFIX . 'wa_profile_description', isset($data->description) ? $data->description : ''); | |
| 542 | - update_option( NOTIFIER_PREFIX . 'wa_profile_email', isset($data->email) ? $data->email : ''); | |
| 543 | - update_option( NOTIFIER_PREFIX . 'wa_profile_category', isset($data->vertical) ? $data->vertical : ''); | |
| 544 | - | |
| 545 | - if (isset($data->websites)) { | |
| 546 | - update_option( NOTIFIER_PREFIX . 'wa_profile_website_1', isset($data->websites[0]) ? $data->websites[0] : ''); | |
| 547 | - update_option( NOTIFIER_PREFIX . 'wa_profile_website_2', isset($data->websites[1]) ? $data->websites[1] : ''); | |
| 548 | - } | |
| 549 | - | |
| 550 | - if (isset($data->profile_picture_url)) { | |
| 551 | - $profile_id = notifier_upload_file_by_url($data->profile_picture_url); | |
| 552 | - update_option( NOTIFIER_PREFIX . 'wa_profile_picture', $profile_id); | |
| 553 | - } | |
| 554 | - } | |
| 555 | - $notices[] = array( | |
| 556 | - 'message' => 'Settings saved.', | |
| 561 | + 'message' => 'Disconnected successfully. To re-connect with WANotifier.com, enter API key below and click on <b>Save and validate</b>.', | |
| 557 | 562 | 'type' => 'success' |
| 558 | 563 | ); |
| 559 | - return $notices; | |
| 564 | + new Notifier_Admin_Notices($notices, true); | |
| 565 | + wp_redirect(admin_url('admin.php?page=notifier')); | |
| 560 | 566 | } |
| 561 | 567 | } |
| 562 | 568 | |
| 563 | 569 | /** |
| 564 | - * Show WA profile screenshot on Profile tab | |
| 570 | + * Get button styles | |
| 565 | 571 | */ |
| 566 | - public static function show_wa_profile_screenshot_start($tab) { | |
| 567 | - if ('profile' != $tab) { | |
| 568 | - return; | |
| 569 | - } | |
| 570 | - echo '<div class="notifier-profile-fields-left">'; | |
| 572 | + public static function get_button_styles(){ | |
| 573 | + return array( | |
| 574 | + 'default' => 'Select button style', | |
| 575 | + 'btn-style-1' => 'Style 1' , | |
| 576 | + 'btn-style-2' => 'Style 2', | |
| 577 | + 'btn-style-3' => 'Style 3', | |
| 578 | + 'btn-style-4' => 'Style 4', | |
| 579 | + 'btn-custom-image' => 'Add your own image' | |
| 580 | + ); | |
| 571 | 581 | } |
| 572 | 582 | |
| 573 | 583 | /** |
| 574 | - * Show WA profile screenshot on Profile tab | |
| 584 | + * Show Chat Button Preview | |
| 575 | 585 | */ |
| 576 | - public static function show_wa_profile_screenshot_end($tab) { | |
| 577 | - if ('profile' != $tab) { | |
| 578 | - return; | |
| 586 | + public static function preview_btn_style(){ | |
| 587 | + $btn_style = isset($_POST['btn_style']) ? sanitize_text_field($_POST['btn_style']) : ''; | |
| 588 | + | |
| 589 | + $button_styles = self::get_button_styles(); | |
| 590 | + $button_style_keys = array_keys($button_styles); | |
| 591 | + | |
| 592 | + if(! in_array($btn_style, $button_style_keys)){ | |
| 593 | + wp_send_json( array( | |
| 594 | + 'preview' => 'Invalid request' | |
| 595 | + ) ); | |
| 579 | 596 | } |
| 580 | - echo '</div><div class="notifier-profile-fields-right"><img src="' . esc_url(NOTIFIER_URL) . 'assets/images/wa-profile.jpg" /></div>'; | |
| 597 | + | |
| 598 | + ob_start(); | |
| 599 | + include_once NOTIFIER_PATH.'templates/buttons/'.$btn_style.'.php'; | |
| 600 | + $btn_output = ob_get_clean(); | |
| 601 | + | |
| 602 | + wp_send_json( array( | |
| 603 | + 'preview' => $btn_output | |
| 604 | + ) ); | |
| 581 | 605 | } |
| 582 | 606 | } |