diff --git a/assets/js/admin/whatsapp-events.js b/assets/js/admin/whatsapp-events.js index 478a47eeb..e83dfa202 100644 --- a/assets/js/admin/whatsapp-events.js +++ b/assets/js/admin/whatsapp-events.js @@ -44,9 +44,6 @@ jQuery(document).ready(function ($) { orderRefundedInactiveStatus.show(); } - var eventConfiglanguage = getEventLanguage(facebook_for_woocommerce_whatsapp_events.event); - $("#manage-event-language").val(eventConfiglanguage); - $('#woocommerce-whatsapp-manage-order-placed, #woocommerce-whatsapp-manage-order-fulfilled, #woocommerce-whatsapp-manage-order-refunded').click(function (event) { var clickedButtonId = $(event.target).attr("id"); let view = clickedButtonId.replace("woocommerce-whatsapp-", ""); @@ -166,6 +163,36 @@ jQuery(document).ready(function ($) { }); }); + $("#manage-event-language").load(facebook_for_woocommerce_whatsapp_events.ajax_url, function () { + $.post(facebook_for_woocommerce_whatsapp_events.ajax_url, { + action: 'wc_facebook_whatsapp_fetch_supported_languages', + nonce: facebook_for_woocommerce_whatsapp_events.nonce, + }, function (response) { + if (response.success) { + const parsedData = JSON.parse(response.data); + const supportedLanguages = parsedData.supported_languages; + $.each(supportedLanguages, function (index, languageObj) { + var displayValue = $.parseHTML(languageObj.display_value)[0].textContent; + var locale = $.parseHTML(languageObj.locale)[0].textContent; + $("#manage-event-language").append($("").text(displayValue).val(locale)); + }); + var eventConfiglanguage = getEventLanguage(facebook_for_woocommerce_whatsapp_events.event); + $("#manage-event-language").val(eventConfiglanguage); + console.log('Fetch supported language call succeeded'); + } + else { + console.log('Fetch supported language call failed', response); + const message = facebook_for_woocommerce_whatsapp_finish.i18n.generic_error; + const errorNoticeHtml = ` +
+

${message}

+
+ `; + $('#events-error-notice').html(errorNoticeHtml).show(); + } + }); + }); + function getEventLanguage(event) { switch (event) { case "ORDER_PLACED": diff --git a/includes/AJAX.php b/includes/AJAX.php index 09060b4c7..93fe5d165 100644 --- a/includes/AJAX.php +++ b/includes/AJAX.php @@ -78,6 +78,9 @@ public function __construct() { // disconnect whatsapp account from woocommcerce app add_action( 'wp_ajax_wc_facebook_disconnect_whatsapp', array( $this, 'wc_facebook_disconnect_whatsapp' ) ); + + // get supported languages for whatsapp templates + add_action( 'wp_ajax_wc_facebook_whatsapp_fetch_supported_languages', array( $this, 'whatsapp_fetch_supported_languages' ) ); } @@ -418,6 +421,34 @@ public function whatsapp_fetch_library_template_info() { $event = isset( $_POST['event'] ) ? wc_clean( wp_unslash( $_POST['event'] ) ) : ''; WhatsAppUtilityConnection::get_template_library_content( $event, $bisu_token ); } + + public function whatsapp_fetch_supported_languages() { + wc_get_logger()->info( + sprintf( + __( 'Fetching supported languages for WhatsApp Utility Templates', 'facebook-for-woocommerce' ) + ) + ); + if ( ! check_ajax_referer( 'facebook-for-wc-whatsapp-events-nonce', 'nonce', false ) ) { + wc_get_logger()->info( + sprintf( + __( 'Nonce Verification Failed while fetching supported languages for WhatsApp Utility Templates', 'facebook-for-woocommerce' ) + ) + ); + wp_send_json_error( 'Invalid security token sent.' ); + } + $bisu_token = get_option( 'wc_facebook_wa_integration_bisu_access_token', null ); + $integration_config_id = get_option( 'wc_facebook_wa_integration_config_id', null ); + if ( empty( $bisu_token ) || empty( $integration_config_id ) ) { + wc_get_logger()->info( + sprintf( + __( 'Missing Integration Config ID, BISU token, WABA ID for Integration Config Get API call', 'facebook-for-woocommerce' ) + ) + ); + wp_send_json_error( 'Missing integration_config_id or bisu_token for Integration Config Get API call', 'facebook-for-woocommerce' ); + } + WhatsAppUtilityConnection::get_supported_languages_for_templates( $integration_config_id, $bisu_token ); + } + /** * Creates or Updates WhatsApp Utility Event Configs * diff --git a/includes/Admin/Settings_Screens/Whatsapp_Utility.php b/includes/Admin/Settings_Screens/Whatsapp_Utility.php index 906e65b45..efa705218 100644 --- a/includes/Admin/Settings_Screens/Whatsapp_Utility.php +++ b/includes/Admin/Settings_Screens/Whatsapp_Utility.php @@ -585,22 +585,6 @@ public function render_manage_events_view() {

diff --git a/includes/Handlers/WhatsAppUtilityConnection.php b/includes/Handlers/WhatsAppUtilityConnection.php index c18205559..90c4f4c35 100644 --- a/includes/Handlers/WhatsAppUtilityConnection.php +++ b/includes/Handlers/WhatsAppUtilityConnection.php @@ -405,6 +405,50 @@ public static function post_whatsapp_utility_messages_events_call( $event, $even } } + /** + * Makes an API call to Integration Config Get API + * + * @param string $integration_config_id Integration Config id + * @param string $bisu_token the BISU token received in the webhook + */ + public static function get_supported_languages_for_templates( $integration_config_id, $bisu_token ) { + $base_url = array( self::GRAPH_API_BASE_URL, self::API_VERSION, $integration_config_id ); + $base_url = esc_url( implode( '/', $base_url ) ); + $params = array( + 'access_token' => $bisu_token, + ); + $url = add_query_arg( $params, $base_url ); + $options = array( + 'headers' => array( + 'Authorization' => $bisu_token, + ), + 'body' => array(), + 'timeout' => 300, // 5 minutes + ); + + $response = wp_remote_request( $url, $options ); + $status_code = wp_remote_retrieve_response_code( $response ); + $data = wp_remote_retrieve_body( $response ); + if ( is_wp_error( $response ) || 200 !== $status_code ) { + wc_get_logger()->info( + sprintf( + /* translators: %s $error_message */ + __( 'Integration Config GET API call Failed %1$s ', 'facebook-for-woocommerce' ), + $data, + ) + ); + wp_send_json_error( $response, 'Integration Config GET API call Failed' ); + } else { + wc_get_logger()->info( + sprintf( + __( 'Integration Config GET API call Succeeded', 'facebook-for-woocommerce' ) + ) + ); + // $response_object = json_decode( $data[0] ); + wp_send_json_success( $data, 'Finish Integration Config API Call' ); + } + } + /** * Gets Component Objects for Order Management Events