Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions assets/js/admin/whatsapp-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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-", "");
Expand Down Expand Up @@ -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($("<option></option>").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 = `
<div class="notice-error">
<p>${message}</p>
</div>
`;
$('#events-error-notice').html(errorNoticeHtml).show();
}
});
});

function getEventLanguage(event) {
switch (event) {
case "ORDER_PLACED":
Expand Down
31 changes: 31 additions & 0 deletions includes/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}


Expand Down Expand Up @@ -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
*
Expand Down
16 changes: 0 additions & 16 deletions includes/Admin/Settings_Screens/Whatsapp_Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,6 @@ public function render_manage_events_view() {
<div class="manage-event-card-item">
<p><b><?php esc_html_e( 'Select a language', 'facebook-for-woocommerce' ); ?></b></p>
<select id="manage-event-language" class="manage-event-selector">
<option value="af">Afrikaans</option>
<option value="sq">Albanian</option>
<option value="ar">Arabic</option>
<option value="az">Azerbaijani</option>
<option value="bn">Bengali</option>
<option value="bg">Bulgarian</option>
<option value="ca">Catalan</option>
<option value="zh_CN">Chinese (CHN)</option>
<option value="zh_HK">Chinese (HKG)</option>
<option value="zh_TW">Chinese (TAI)</option>
<option value="hr">Croatian</option>
<option value="nl">Dutch</option>
<option value="en">English</option>
<option value="en_US">English (US)</option>
<option value="en_GB">English (UK)</option>
<option value="es">Spanish</option>
</select>
</div>
<div class="manage-event-card-item">
Expand Down
44 changes: 44 additions & 0 deletions includes/Handlers/WhatsAppUtilityConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down