| 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 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Add settings page to men |
| 19 |
*/ |
| 20 |
public static function setup_admin_page () { |
| 21 |
add_submenu_page( NOTIFIER_NAME, 'Settings', 'Settings', 'manage_options', NOTIFIER_NAME . '-settings', array( __CLASS__, 'output' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Output |
| 26 |
*/ |
| 27 |
public static function output() { |
| 28 |
include_once NOTIFIER_PATH . '/views/admin-settings.php'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get settings tabs |
| 33 |
*/ |
| 34 |
private static function get_settings_tabs() { |
| 35 |
$tabs = array( |
| 36 |
'profile' => 'WhatsApp Profile', |
| 37 |
'general' => 'General', |
| 38 |
'api' => 'API Configuration', |
| 39 |
); |
| 40 |
return $tabs; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Check if on settings page |
| 45 |
*/ |
| 46 |
public static function is_settings_page() { |
| 47 |
$current_page = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 48 |
return strpos($current_page, NOTIFIER_NAME . '-settings') !== false; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Settings fields |
| 53 |
*/ |
| 54 |
private static function settings_fields($tab) { |
| 55 |
$settings = array(); |
| 56 |
switch ($tab) { |
| 57 |
case 'profile': |
| 58 |
$settings = array( |
| 59 |
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.', |
| 62 |
'type' => 'title', |
| 63 |
), |
| 64 |
array( |
| 65 |
'id' => 'wa_profile_picture', |
| 66 |
'title' => 'Profile Picture', |
| 67 |
'description' => 'Recommended profile image size: 640px X 640px.', |
| 68 |
'type' => 'file', |
| 69 |
'default' => '', |
| 70 |
'placeholder' => '', |
| 71 |
'uploader_title' => 'WhatsApp profile image', |
| 72 |
'uploader_button_text' => 'Select', |
| 73 |
'uploader_supported_file_types' => 'image' |
| 74 |
), |
| 75 |
array( |
| 76 |
'id' => 'wa_profile_description', |
| 77 |
'title' => 'Description', |
| 78 |
'description' => '', |
| 79 |
'type' => 'text', |
| 80 |
'default' => '', |
| 81 |
'placeholder' => '' |
| 82 |
), |
| 83 |
array( |
| 84 |
'id' => 'wa_profile_address', |
| 85 |
'title' => 'Address', |
| 86 |
'description' => '', |
| 87 |
'type' => 'text', |
| 88 |
'default' => '', |
| 89 |
'placeholder' => '' |
| 90 |
), |
| 91 |
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' => '' |
| 119 |
), |
| 120 |
array( |
| 121 |
'id' => 'wa_profile_email', |
| 122 |
'title' => 'Email (optional)', |
| 123 |
'description' => '', |
| 124 |
'type' => 'text', |
| 125 |
'default' => '', |
| 126 |
'placeholder' => '' |
| 127 |
), |
| 128 |
array( |
| 129 |
'id' => 'wa_profile_website_1', |
| 130 |
'title' => 'Website URL 1', |
| 131 |
'description' => '', |
| 132 |
'type' => 'text', |
| 133 |
'default' => '', |
| 134 |
'placeholder' => 'https://' |
| 135 |
), |
| 136 |
array( |
| 137 |
'id' => 'wa_profile_website_2', |
| 138 |
'title' => 'Website URL 2', |
| 139 |
'description' => '', |
| 140 |
'type' => 'text', |
| 141 |
'default' => '', |
| 142 |
'placeholder' => 'https://' |
| 143 |
) |
| 144 |
); |
| 145 |
break; |
| 146 |
|
| 147 |
case 'general': |
| 148 |
$settings = array( |
| 149 |
array( |
| 150 |
'title' => 'General', |
| 151 |
'description' => '', |
| 152 |
'type' => 'title', |
| 153 |
), |
| 154 |
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' => '' |
| 161 |
), |
| 162 |
); |
| 163 |
break; |
| 164 |
|
| 165 |
case 'api': |
| 166 |
$settings = array( |
| 167 |
array( |
| 168 |
'title' => 'WhastApp API Configuration', |
| 169 |
'description' => 'Enter the API details below to setup WhatsApp.', |
| 170 |
'type' => 'title', |
| 171 |
), |
| 172 |
array( |
| 173 |
'id' => 'phone_number_id', |
| 174 |
'title' => 'Phone Number ID', |
| 175 |
'description' => '', |
| 176 |
'type' => 'text', |
| 177 |
'default' => '', |
| 178 |
'placeholder' => '' |
| 179 |
), |
| 180 |
array( |
| 181 |
'id' => 'business_account_id', |
| 182 |
'title' => 'Business Account ID', |
| 183 |
'description' => '', |
| 184 |
'type' => 'text', |
| 185 |
'default' => '', |
| 186 |
'placeholder' => '' |
| 187 |
), |
| 188 |
array( |
| 189 |
'id' => 'permanent_access_token', |
| 190 |
'title' => 'Permanent Access Token', |
| 191 |
'description' => '', |
| 192 |
'type' => 'textarea', |
| 193 |
'default' => '', |
| 194 |
'placeholder' => '' |
| 195 |
), |
| 196 |
); |
| 197 |
break; |
| 198 |
} |
| 199 |
$settings = apply_filters( 'notifier_$tab_settings_fields', $settings ); |
| 200 |
return $settings; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Generate HTML for displaying individual fields |
| 205 |
*/ |
| 206 |
public static function display_field( $field ) { |
| 207 |
|
| 208 |
$html = ''; |
| 209 |
|
| 210 |
if (isset($field['id'])) { |
| 211 |
$option_name = NOTIFIER_PREFIX . $field['id']; |
| 212 |
$option = get_option( $option_name ); |
| 213 |
} |
| 214 |
|
| 215 |
$data = ''; |
| 216 |
if ( isset( $field['default'] ) ) { |
| 217 |
$data = $field['default']; |
| 218 |
if ( isset( $option ) && '' != $option) { |
| 219 |
$data = $option; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
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>'; |
| 226 |
} else { |
| 227 |
$html .= '<tr>'; |
| 228 |
$html .= '<th>' . $field['title'] . '</th>'; |
| 229 |
$html .= '<td>'; |
| 230 |
|
| 231 |
switch ( $field['type'] ) { |
| 232 |
|
| 233 |
case 'text': |
| 234 |
case 'password': |
| 235 |
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 . '"/>'; |
| 237 |
break; |
| 238 |
|
| 239 |
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/>'; |
| 241 |
break; |
| 242 |
|
| 243 |
case 'checkbox': |
| 244 |
$checked = ''; |
| 245 |
if ( $option && 'on' == $option ) { |
| 246 |
$checked = 'checked="checked"'; |
| 247 |
} |
| 248 |
$html .= '<label><input id="' . esc_attr( $option_name ) . '" type="' . $field['type'] . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . $field['label'] . '</label>'; |
| 249 |
break; |
| 250 |
|
| 251 |
case 'checkbox_multi': |
| 252 |
foreach ( $field['options'] as $k => $v ) { |
| 253 |
$checked = false; |
| 254 |
if ( in_array( $k, $data ) ) { |
| 255 |
$checked = true; |
| 256 |
} |
| 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>'; |
| 258 |
} |
| 259 |
break; |
| 260 |
|
| 261 |
case 'radio': |
| 262 |
foreach ( $field['options'] as $k => $v ) { |
| 263 |
$checked = false; |
| 264 |
if ( $k == $data ) { |
| 265 |
$checked = true; |
| 266 |
} |
| 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>'; |
| 268 |
} |
| 269 |
break; |
| 270 |
|
| 271 |
case 'select': |
| 272 |
$html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '">'; |
| 273 |
foreach ( $field['options'] as $k => $v ) { |
| 274 |
$selected = false; |
| 275 |
if ( $k == $data ) { |
| 276 |
$selected = true; |
| 277 |
} |
| 278 |
$html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>'; |
| 279 |
} |
| 280 |
$html .= '</select> '; |
| 281 |
break; |
| 282 |
|
| 283 |
case 'select_multi': |
| 284 |
$html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple">'; |
| 285 |
foreach ( $field['options'] as $k => $v ) { |
| 286 |
$selected = false; |
| 287 |
if ( in_array( $k, $data ) ) { |
| 288 |
$selected = true; |
| 289 |
} |
| 290 |
$html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '" />' . $v . '</label> '; |
| 291 |
} |
| 292 |
$html .= '</select> '; |
| 293 |
break; |
| 294 |
|
| 295 |
case 'file': |
| 296 |
$uploader_title = isset($field['uploader_title']) ? $field['uploader_title'] : 'Upload media'; |
| 297 |
$uploader_button_text = isset($field['uploader_button_text']) ? $field['uploader_button_text'] : 'Select'; |
| 298 |
$file_types = isset($field['uploader_supported_file_types']) ? $field['uploader_supported_file_types'] : array(); |
| 299 |
|
| 300 |
$image_thumb = ''; |
| 301 |
$video_url = ''; |
| 302 |
$show_image = 'hide'; |
| 303 |
$show_video = 'hide'; |
| 304 |
|
| 305 |
$html .= '<span class="notifier-media-preview">'; |
| 306 |
if ( '' != $data ) { |
| 307 |
$file_type = $field['uploader_supported_file_types']; |
| 308 |
switch ($file_type) { |
| 309 |
case 'image': |
| 310 |
case 'image/jpeg': |
| 311 |
case 'image/png': |
| 312 |
case 'application/pdf': |
| 313 |
$image_thumb = wp_get_attachment_thumb_url( $data ); |
| 314 |
$show_image = ''; |
| 315 |
$show_video = 'hide'; |
| 316 |
break; |
| 317 |
|
| 318 |
case 'video/mp4': |
| 319 |
$video_url = wp_get_attachment_url( $data ); |
| 320 |
$show_image = 'hide'; |
| 321 |
$show_video = ''; |
| 322 |
break; |
| 323 |
|
| 324 |
default: |
| 325 |
$show_image = 'hide'; |
| 326 |
$show_video = 'hide'; |
| 327 |
} |
| 328 |
} |
| 329 |
$html .= '<img id="' . esc_attr( $option_name ) . '_preview_image" class="notifier-media-preview-item '. esc_attr($show_image) .'" src="' . esc_url($image_thumb) . '" />'; |
| 330 |
$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/>'; |
| 331 |
$html .= '</span>'; |
| 332 |
|
| 333 |
$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' . '" /> '; |
| 334 |
$html .= '<input id="' . esc_attr( $option_name ) . '_delete" type="button" class="notifier-media-delete-button button" value="' . 'Remove' . '" />'; |
| 335 |
$html .= '<input id="' . esc_attr( $option_name ) . '" class="notifier-media-attachment-id" type="hidden" name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $data ) . '"/><br/>'; |
| 336 |
break; |
| 337 |
|
| 338 |
case 'color': |
| 339 |
?> |
| 340 |
<div class="color-picker" style="position:relative;"> |
| 341 |
<input type="text" name="<?php echo esc_attr( $option_name ); ?>" class="color" value="<?php echo esc_attr( $data ); ?>" /> |
| 342 |
<div style="position:absolute;background:#FFF;z-index:99;border-radius:100%;" class="colorpicker"></div> |
| 343 |
</div> |
| 344 |
<?php |
| 345 |
break; |
| 346 |
|
| 347 |
} |
| 348 |
|
| 349 |
if ( '' != $field['description'] ) { |
| 350 |
$html .= '<p class="description">' . esc_html($field['description']) . '</p>'; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
//phpcs:ignore |
| 355 |
echo $html; |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Show the setting fields |
| 360 |
*/ |
| 361 |
public static function show_settings_fields ($tab) { |
| 362 |
$setting_fields = self::settings_fields($tab); |
| 363 |
echo "<table class='notifier-fields-table'>"; |
| 364 |
foreach ($setting_fields as $field) { |
| 365 |
self::display_field( $field ); |
| 366 |
} |
| 367 |
echo '</table>'; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Save the settings. |
| 372 |
* |
| 373 |
* TODO - check fields other than text and textarea |
| 374 |
*/ |
| 375 |
public static function save_settings_fields() { |
| 376 |
if ( ! self::is_settings_page() ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
if ( ! isset( $_POST['save'] ) ) { |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
//phpcs:ignore |
| 385 |
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], NOTIFIER_NAME . '-settings' ) ) { |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
$tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'profile'; |
| 390 |
|
| 391 |
$settings_fields = self::settings_fields($tab); |
| 392 |
$data = $_POST; |
| 393 |
$update_options = array(); |
| 394 |
|
| 395 |
// Loop options and get values to save. |
| 396 |
foreach ( $settings_fields as $option ) { |
| 397 |
if ( ! isset( $option['id'] ) || ! isset( $option['type'] ) ) { |
| 398 |
continue; |
| 399 |
} |
| 400 |
|
| 401 |
$option_name = NOTIFIER_PREFIX . $option['id']; |
| 402 |
$setting_name = ''; |
| 403 |
$raw_value = isset( $data[ $option_name ] ) ? wp_unslash( $data[ $option_name ] ) : null; |
| 404 |
|
| 405 |
// Format the value based on option type. |
| 406 |
switch ( $option['type'] ) { |
| 407 |
case 'checkbox': |
| 408 |
$value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no'; |
| 409 |
break; |
| 410 |
case 'textarea': |
| 411 |
$value = wp_kses_post( trim( $raw_value ) ); |
| 412 |
break; |
| 413 |
case 'select': |
| 414 |
$allowed_values = empty( $option['options'] ) ? array() : array_map( 'strval', array_keys( $option['options'] ) ); |
| 415 |
if ( empty( $option['default'] ) && empty( $allowed_values ) ) { |
| 416 |
$value = null; |
| 417 |
break; |
| 418 |
} |
| 419 |
$default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); |
| 420 |
$value = in_array( $raw_value, $allowed_values, true ) ? $raw_value : $default; |
| 421 |
break; |
| 422 |
default: |
| 423 |
$value = sanitize_text_field( $raw_value ); |
| 424 |
break; |
| 425 |
} |
| 426 |
|
| 427 |
// Check if option is an array and handle that differently to single values. |
| 428 |
if ( $option_name && $setting_name ) { |
| 429 |
if ( ! isset( $update_options[ $option_name ] ) ) { |
| 430 |
$update_options[ $option_name ] = get_option( $option_name, array() ); |
| 431 |
} |
| 432 |
if ( ! is_array( $update_options[ $option_name ] ) ) { |
| 433 |
$update_options[ $option_name ] = array(); |
| 434 |
} |
| 435 |
$update_options[ $option_name ][ $setting_name ] = $value; |
| 436 |
} else { |
| 437 |
$update_options[ $option_name ] = $value; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if ('api' == $tab) { |
| 442 |
$phone_number_id = isset($update_options[ NOTIFIER_PREFIX . 'phone_number_id' ]) ? $update_options[ NOTIFIER_PREFIX . 'phone_number_id' ] : ''; |
| 443 |
$business_account_id = isset($update_options[ NOTIFIER_PREFIX . 'business_account_id' ]) ? $update_options[ NOTIFIER_PREFIX . 'business_account_id' ] : ''; |
| 444 |
$permanent_access_token = isset($update_options[ NOTIFIER_PREFIX . 'permanent_access_token' ]) ? $update_options[ NOTIFIER_PREFIX . 'permanent_access_token' ] : ''; |
| 445 |
|
| 446 |
if ('' == $phone_number_id || '' == $business_account_id || '' == $permanent_access_token) { |
| 447 |
$notices[] = array( |
| 448 |
'message' => 'Phone number ID, Business Account ID and Permanent Access Token are mandatory fields.', |
| 449 |
'type' => 'error' |
| 450 |
); |
| 451 |
new Notifier_Admin_Notices($notices); |
| 452 |
return; |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
// Save all options in our array. |
| 457 |
foreach ( $update_options as $name => $value ) { |
| 458 |
update_option( $name, $value, 'yes' ); |
| 459 |
} |
| 460 |
|
| 461 |
// Do other things after updating settings |
| 462 |
|
| 463 |
switch ($tab) { |
| 464 |
case 'profile': |
| 465 |
$notices = self::save_whatsapp_profile_details($update_options); |
| 466 |
break; |
| 467 |
|
| 468 |
case 'api': |
| 469 |
$notices = self::fetch_and_save_whatsapp_details($phone_number_id); |
| 470 |
break; |
| 471 |
|
| 472 |
default: |
| 473 |
$notices[] = array( |
| 474 |
'message' => 'Settings saved.', |
| 475 |
'type' => 'success' |
| 476 |
); |
| 477 |
} |
| 478 |
|
| 479 |
new Notifier_Admin_Notices($notices); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Save WhatsApp profile details |
| 484 |
*/ |
| 485 |
public static function save_whatsapp_profile_details($profile_fields) { |
| 486 |
$args = array ( |
| 487 |
'messaging_product' => 'whatsapp', |
| 488 |
'description' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_description']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_description' ] : '', |
| 489 |
'address' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_address']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_address' ] : '', |
| 490 |
'vertical' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_category']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_category' ] : '', |
| 491 |
'email' => isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_email']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_email' ] : '', |
| 492 |
); |
| 493 |
|
| 494 |
if (isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_1' ]) && '' != $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_1' ]) { |
| 495 |
$args['websites'][] = $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_1']; |
| 496 |
} |
| 497 |
|
| 498 |
if (isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_2' ]) && '' != $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_2' ]) { |
| 499 |
$args['websites'][] = $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_website_2' ]; |
| 500 |
} |
| 501 |
|
| 502 |
$profile_picture_id = isset($profile_fields[ NOTIFIER_PREFIX . 'wa_profile_picture']) ? $profile_fields[ NOTIFIER_PREFIX . 'wa_profile_picture' ] : ''; |
| 503 |
|
| 504 |
$profile_picture_handle = Notifier::wa_cloud_api_upload_media($profile_picture_id); |
| 505 |
|
| 506 |
if ($profile_picture_handle) { |
| 507 |
$args['profile_picture_handle'] = $profile_picture_handle; |
| 508 |
} |
| 509 |
|
| 510 |
$response = Notifier::wa_cloud_api_request( 'whatsapp_business_profile', $args ); |
| 511 |
|
| 512 |
if (isset($response->error)) { |
| 513 |
$notices[] = array( |
| 514 |
'message' => 'Error Code ' . $response->error->code . ': ' . $response->error->message , |
| 515 |
'type' => 'error' |
| 516 |
); |
| 517 |
return $notices; |
| 518 |
} else { |
| 519 |
$notices[] = array( |
| 520 |
'message' => 'Profile updated successfully.', |
| 521 |
'type' => 'success' |
| 522 |
); |
| 523 |
} |
| 524 |
return $notices; |
| 525 |
} |
| 526 |
|
| 527 |
/** |
| 528 |
* Fetch and save WhatsApp details |
| 529 |
*/ |
| 530 |
public static function fetch_and_save_whatsapp_details($phone_number_id) { |
| 531 |
$response = Notifier::wa_cloud_api_request('', array(), 'GET'); |
| 532 |
if (isset($response->error)) { |
| 533 |
$notices[] = array( |
| 534 |
'message' => 'API request can not be validated. Error Code ' . $response->error->code . ': ' . $response->error->message , |
| 535 |
'type' => 'error' |
| 536 |
); |
| 537 |
return $notices; |
| 538 |
} else { |
| 539 |
$phone_number_details[$phone_number_id] = array ( |
| 540 |
'display_num' => $response->display_phone_number, |
| 541 |
'display_name' => $response->verified_name, |
| 542 |
'phone_num_status' => $response->code_verification_status, |
| 543 |
'quality_rating' => $response->quality_rating |
| 544 |
); |
| 545 |
update_option( NOTIFIER_PREFIX . 'phone_number_details', $phone_number_details ); |
| 546 |
} |
| 547 |
|
| 548 |
$response_profile = Notifier::wa_cloud_api_request('whatsapp_business_profile', array( |
| 549 |
'fields' => 'about,address,description,email,profile_picture_url,websites,vertical' |
| 550 |
), 'GET'); |
| 551 |
|
| 552 |
if (isset($response_profile->error)) { |
| 553 |
$notices[] = array( |
| 554 |
'message' => 'WhatsApp Error Code: ' . $response_profile->error->code . ': ' . $response_profile->error->message , |
| 555 |
'type' => 'error' |
| 556 |
); |
| 557 |
return $notices; |
| 558 |
} else { |
| 559 |
if (isset($response_profile->data)) { |
| 560 |
$data = $response_profile->data[0]; |
| 561 |
update_option( NOTIFIER_PREFIX . 'wa_profile_address', isset($data->address) ? $data->address : '' ); |
| 562 |
update_option( NOTIFIER_PREFIX . 'wa_profile_description', isset($data->description) ? $data->description : ''); |
| 563 |
update_option( NOTIFIER_PREFIX . 'wa_profile_email', isset($data->email) ? $data->email : ''); |
| 564 |
update_option( NOTIFIER_PREFIX . 'wa_profile_category', isset($data->vertical) ? $data->vertical : ''); |
| 565 |
|
| 566 |
if (isset($data->websites)) { |
| 567 |
update_option( NOTIFIER_PREFIX . 'wa_profile_website_1', isset($data->websites[0]) ? $data->websites[0] : ''); |
| 568 |
update_option( NOTIFIER_PREFIX . 'wa_profile_website_2', isset($data->websites[1]) ? $data->websites[1] : ''); |
| 569 |
} |
| 570 |
|
| 571 |
if (isset($data->profile_picture_url)) { |
| 572 |
$profile_id = notifier_upload_file_by_url($data->profile_picture_url); |
| 573 |
update_option( NOTIFIER_PREFIX . 'wa_profile_picture', $profile_id); |
| 574 |
} |
| 575 |
} |
| 576 |
$notices[] = array( |
| 577 |
'message' => 'Settings saved.', |
| 578 |
'type' => 'success' |
| 579 |
); |
| 580 |
return $notices; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* Show WA profile screenshot on Profile tab |
| 586 |
*/ |
| 587 |
public static function show_wa_profile_screenshot_start($tab) { |
| 588 |
if ('profile' != $tab) { |
| 589 |
return; |
| 590 |
} |
| 591 |
echo '<div class="notifier-profile-fields-left">'; |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Show WA profile screenshot on Profile tab |
| 596 |
*/ |
| 597 |
public static function show_wa_profile_screenshot_end($tab) { |
| 598 |
if ('profile' != $tab) { |
| 599 |
return; |
| 600 |
} |
| 601 |
echo '</div><div class="notifier-profile-fields-right"><img src="' . esc_url(NOTIFIER_URL) . 'assets/images/wa-profile.jpg" /></div>'; |
| 602 |
} |
| 603 |
} |
| 604 |
|