Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unintended translations in webhooks #1194

Merged
merged 8 commits into from
Jun 23, 2022
Merged
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: 15 additions & 18 deletions modules/woo-commerce/qwc-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
function qtranxf_wc_add_filters_admin() {
// priority 20 is used because in case other plugins add some untranslated content on normal priority
// it will still hopefully then get translated.
$admin_hooks = array(
'woocommerce_email_footer_text' => 20,
'woocommerce_email_from_address' => 20,
'woocommerce_email_from_name' => 20,
'woocommerce_attribute_taxonomies' => 20,
'woocommerce_variation_option_name' => 20,
);

$email_ids = array(
'backorder' => 20,
'cancelled_order' => 20,
Expand All @@ -26,24 +34,13 @@ function qtranxf_wc_add_filters_admin() {

// not all combinations are in use, but it is ok, they may be added in the future.
foreach ( $email_ids as $name => $priority ) {
add_filter( 'woocommerce_email_recipient_' . $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
add_filter( 'woocommerce_email_subject_' . $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
add_filter( 'woocommerce_email_heading_' . $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
add_filter( 'woocommerce_email_content_' . $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
}

$email_common = array(
'woocommerce_email_footer_text' => 20,
'woocommerce_email_from_address' => 20,
'woocommerce_email_from_name' => 20,
);

foreach ( $email_common as $name => $priority ) {
add_filter( $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
$admin_hooks[ 'woocommerce_email_recipient_' . $name ] = $priority;
$admin_hooks[ 'woocommerce_email_subject_' . $name ] = $priority;
$admin_hooks[ 'woocommerce_email_heading_' . $name ] = $priority;
$admin_hooks[ 'woocommerce_email_content_' . $name ] = $priority;
}

add_filter( 'woocommerce_attribute_taxonomies', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );
add_filter( 'woocommerce_variation_option_name', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage' );
qtranxf_add_filters( [ 'text' => $admin_hooks ] );
}

qtranxf_wc_add_filters_admin();
Expand Down Expand Up @@ -402,15 +399,15 @@ function qtranxf_wc_admin_filters() {
switch ( $pagenow ) {
case 'admin.php':
if ( isset( $_SERVER['QUERY_STRING'] ) && strpos( $_SERVER['QUERY_STRING'], 'page=wc-settings&tab=checkout' ) !== false ) {
add_filter( 'woocommerce_gateway_title', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 5 );
qtranxf_add_filters( [ 'text' => [ 'woocommerce_gateway_title' => 5 ] ] );
}
break;
case 'edit.php':
// translate column 'product_cat'
if ( isset( $_SERVER['QUERY_STRING'] )
&& strpos( $_SERVER['QUERY_STRING'], 'post_type=product' ) !== false
) {
add_filter( 'get_term', 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', 6 );
qtranxf_add_filters( [ 'text' => [ 'get_term' => 6 ] ] );
}
break;
}
Expand Down
79 changes: 28 additions & 51 deletions modules/woo-commerce/qwc-front.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,34 @@ function qtranxf_wc_add_filters_front() {
remove_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5 );
add_filter( 'get_post_metadata', 'qtranxf_wc_filter_postmeta', 5, 4 );

$use_filters = array(
'woocommerce_attribute' => 20,
'woocommerce_attribute_label' => 20,
'woocommerce_cart_item_name' => 20,
'woocommerce_cart_item_thumbnail' => 20,
'woocommerce_cart_shipping_method_full_label' => 20,
'woocommerce_cart_tax_totals' => 20,
'woocommerce_email_footer_text' => 20,
'woocommerce_format_content' => 20,
'woocommerce_gateway_description' => 20,
'woocommerce_gateway_title' => 20,
'woocommerce_gateway_icon' => 20,
'woocommerce_get_privacy_policy_text' => 20,
//'woocommerce_order_details_after_order_table_items' => 20, //TODO: this is an action, not a filter. Actually does not do anything. Intent also not clear. If intent is translating $purchase_note that could only be done through 'pre_kses' filter currently, to be implemented in case.
'woocommerce_order_item_display_meta_value' => 20,
'woocommerce_order_item_name' => 20,
'woocommerce_order_get_tax_totals' => 20,
'woocommerce_order_shipping_to_display' => 20,
'woocommerce_order_subtotal_to_display' => 20,
'woocommerce_page_title' => 20,
'woocommerce_product_get_name' => 20,
'woocommerce_product_title' => 20,
'woocommerce_rate_label' => 20,
'woocommerce_short_description' => 20,
'woocommerce_variation_option_name' => 20,
'wp_mail_from_name' => 20,
$front_hooks = array(
'woocommerce_attribute' => 20,
'woocommerce_attribute_label' => 20,
'woocommerce_cart_item_name' => 20,
'woocommerce_cart_item_thumbnail' => 20,
'woocommerce_cart_shipping_method_full_label' => 20,
'woocommerce_cart_tax_totals' => 20,
'woocommerce_email_footer_text' => 20,
'woocommerce_format_content' => 20,
'woocommerce_gateway_description' => 20,
'woocommerce_gateway_title' => 20,
'woocommerce_gateway_icon' => 20,
'woocommerce_get_privacy_policy_text' => 20,
'woocommerce_order_item_display_meta_value' => 20,
'woocommerce_order_item_name' => 20,
'woocommerce_order_get_tax_totals' => 20,
'woocommerce_order_shipping_to_display' => 20,
'woocommerce_order_subtotal_to_display' => 20,
'woocommerce_page_title' => 20,
'woocommerce_product_get_name' => 20,
'woocommerce_product_title' => 20,
'woocommerce_rate_label' => 20,
'woocommerce_short_description' => 20,
'woocommerce_variation_option_name' => 20,
'wp_mail_from_name' => 20,
);

foreach ( $use_filters as $name => $priority ) {
add_filter( $name, 'qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage', $priority );
}
qtranxf_add_filters( [ 'text' => $front_hooks ] );

add_action( 'woocommerce_dropdown_variation_attribute_options_args', 'qtranxf_wc_dropdown_variation_attribute_options_args', 10, 1 );
add_filter( 'woocommerce_paypal_args', 'qtranxf_wc_paypal_args' );
Expand Down Expand Up @@ -95,28 +92,8 @@ function qtranxf_wc_paypal_args( $args ) {
return $args;
}

/**
* Dealing with webhooks, which should always send information in Raw ML format
*/
if ( wp_doing_cron() ) {

function qtranxf_wc_deliver_webhook_async( $webhook_id, $arg ) {
$page_configs = qtranxf_get_front_page_config();
if ( ! empty( $page_configs['']['filters'] ) ) {
qtranxf_remove_filters( $page_configs['']['filters'] );
}

remove_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5 );
remove_filter( 'the_posts', 'qtranxf_postsFilter', 5 );
remove_action( 'pre_get_posts', 'qtranxf_pre_get_posts', 99 );

remove_filter( 'get_term', 'qtranxf_useTermLib', 0 );
remove_filter( 'get_terms', 'qtranxf_useTermLib', 0 );
}

add_action( 'woocommerce_deliver_webhook_async', 'qtranxf_wc_deliver_webhook_async', 5, 2 );

} else {
// Prevent unintended filtering when webhooks are fired (through cron for cases where $urlinfo['doing_front_end'] is true).
if ( ! wp_doing_cron() ) {
qtranxf_wc_add_filters_front();
}

Expand Down
60 changes: 60 additions & 0 deletions modules/woo-commerce/woo-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,63 @@ function qtranxf_wc_detect_language( $url_info ) {
}

add_filter( 'qtranslate_detect_language', 'qtranxf_wc_detect_language', 5 );

/**
* Handler for webhooks, which should always send information in Raw ML format.
*
* For some cases (e.g. variations updates) the webhook is generated through AJAX instead of cron.
* In that context, qwc-admin.php is loaded instead of qwc-front.php
*/
function qtranxf_wc_deliver_webhook_async( $webhook_id, $arg ) {
if ( function_exists( 'qtranxf_get_front_page_config' ) ) {
$page_configs = qtranxf_get_front_page_config();
if ( ! empty( $page_configs['']['filters'] ) ) {
qtranxf_remove_filters( $page_configs['']['filters'] );
}
}

remove_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5 );
remove_filter( 'the_posts', 'qtranxf_postsFilter', 5 );
remove_action( 'pre_get_posts', 'qtranxf_pre_get_posts', 99 );

/* Raw ML format is not applicable to terms, as default lang only is stored in obj->name and translations are in qtx options.
* Hence qtranxf_wc_get_term_raw_ML filter is added to mimic a raw ML format to be sent through webhook.
*/
add_filter( 'get_term', 'qtranxf_wc_get_term_raw_ML' );
add_filter( 'get_terms', 'qtranxf_wc_get_term_raw_ML' );
wp_cache_flush();

/* Remove admin filters which can affect webhooks
* TODO: check if any test is applicable to prevent qtranxf_wc_add_filters_admin() call when webhook is fired (through AJAX for cases where $urlinfo['doing_front_end'] is false).
* In case qtranxf_wc_add_filters_admin() can be called conditionally, following qtranxf_remove_filters call can be removed.
* Otherwise all filters affecting webhooks added in qtranxf_wc_add_filters_admin() must be removed here.
*/
qtranxf_remove_filters( [
'text' => [
'woocommerce_attribute_taxonomies' => 20,
'woocommerce_variation_option_name' => 20,
]
] );

herrvigg marked this conversation as resolved.
Show resolved Hide resolved
/* Remove WC cached data overwriting current objects. 'product_type' taxonomy is used as a test in WC to avoid multiple registrations.
* This is applicable to objects in dedicated WC tables, as product attributes.
*/
delete_transient( 'wc_attribute_taxonomies' );
unregister_taxonomy( 'product_type' );
WC_Post_Types::register_taxonomies();
}

add_action( 'woocommerce_deliver_webhook_async', 'qtranxf_wc_deliver_webhook_async', 5, 2 );

//TODO: check if this function is to be generalized and moved to inc/qtx_taxonomy.php
function qtranxf_wc_get_term_raw_ML( $obj ) {
$term = qtranxf_useTermLib( $obj );
if ( ! empty( $term->i18n_config['name']['ts'] ) ) {
$term->name = qtranxf_join_b( $term->i18n_config['name']['ts'] );
}
if ( ! empty( $term->i18n_config['description']['ts'] ) ) {
$term->description = qtranxf_join_b( $term->i18n_config['description']['ts'] );
}

return $term;
}