Skip to content

Commit

Permalink
Added loading of WooPayments translation data from translate.wordpres…
Browse files Browse the repository at this point in the history
…s.com
  • Loading branch information
vbelolapotkov committed Jan 19, 2024
1 parent d567e0b commit 355241b
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,110 @@ public static function init() {
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_assets_script' ] );

self::$duplicate_payment_prevention_service->init( self::$card_gateway, self::$order_service );

// Load automated translations.
add_filter( 'pre_set_site_transient_update_plugins', [ __CLASS__, 'load_wcpay_translations' ] );
}

/**
* Hooks into auto-update process to load plugin translations from translate.wordpress.com.
*
* Runs in a cron thread, or in a visitor thread if triggered
* by _maybe_update_plugins(), or in an auto-update thread.
*
* @param object $transient The update_plugins transient object.
*
* @return object The same or a modified version of the transient.
*/
public static function load_wcpay_translations( $transient ) {
try {
if ( $transient instanceof stdClass ) {
$translations = self::get_translations_update_data();
$merged_translations = array_merge( isset( $transient->translations ) ? $transient->translations : [], $translations );
$transient->translations = $merged_translations;
}
} catch ( \Exception $ex ) {
// TODO: log error.
return $transient;
}
return $transient;
}

/**
* Get translations updates information.
*
* @return array Update data {product_id => data}
*/
public static function get_translations_update_data() {
// TODO: lots of WP core code, probably worth moving out of this class.
$installed_translations = wp_get_installed_translations( 'plugins' );
$locales = array_values( get_available_languages() );

if ( empty( $locales ) ) {
return [];
}

// Use the same timeout values as Woo Core https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/admin/helper/class-wc-helper-updater.php#L257.
$timeout = wp_doing_cron() ? 30 : 3;
$plugin_name = 'woocommerce-payments'; // TODO: check if there is a better way of getting the plugin name.

$request_body = [
'locales' => $locales,
'plugins' => [],
];

$request_body['plugins'][ $plugin_name ] = [
'version' => WCPAY_VERSION_NUMBER,
];

$raw_response = wp_remote_post(
'https://translate.wordpress.com/api/translations-updates/woocommerce',
[
'body' => wp_json_encode( $request_body ),
'headers' => [ 'Content-Type: application/json' ],
'timeout' => $timeout,
]
);

// Something wrong happened on the translate server side.
$response_code = wp_remote_retrieve_response_code( $raw_response );
if ( 200 !== $response_code ) {
return [];
}

$response = json_decode( wp_remote_retrieve_body( $raw_response ), true );

// API error, api returned but something was wrong.
if ( array_key_exists( 'success', $response ) && false === $response['success'] ) {
return [];
}

$language_packs = $response['data'][ $plugin_name ];

$translations = [];

foreach ( $language_packs as $language_pack ) {
// Maybe we have this language pack already installed so lets check revision date.
if ( array_key_exists( $plugin_name, $installed_translations ) && array_key_exists( $language_pack['wp_locale'], $installed_translations[ $plugin_name ] ) ) {
$installed_translation_revision_time = new DateTime( $installed_translations[ $plugin_name ][ $language_pack['wp_locale'] ]['PO-Revision-Date'] );
$new_translation_revision_time = new DateTime( $language_pack['last_modified'] );
// Skip if translation language pack is not newer than what is installed already.
if ( $new_translation_revision_time <= $installed_translation_revision_time ) {
continue;
}
}
$translations[] = [
'type' => 'plugin',
'slug' => $plugin_name,
'language' => $language_pack['wp_locale'],
'version' => $language_pack['version'],
'updated' => $language_pack['last_modified'],
'package' => $language_pack['package'],
'autoupdate' => true,
];
}

return $translations;
}

/**
Expand Down

0 comments on commit 355241b

Please sign in to comment.