WordPress.org

Making WordPress.org


Ignore:
Timestamp:
04/10/2018 04:44:56 PM (3 years ago)
Author:
coreymckrill
Message:

WP15: Move pomo functionality to the locales.php file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wp15.wordpress.net/public_html/content/mu-plugins/locales.php

    r7060 r7079  
    1515
    1616require_once trailingslashit( dirname( __FILE__ ) ) . 'locale-detection/locale-detection.php';
     17
     18if ( ! wp_next_scheduled( 'wp15_update_pomo_files' ) ) {
     19    wp_schedule_event( time(), 'hourly', 'wp15_update_pomo_files' );
     20}
     21
     22/**
     23 * Update the PO/MO files for the wp15 text domain.
     24 */
     25function update_pomo_files() {
     26    /*
     27     * The content will probably not need to be updated after the event is over. Updating it anyway would use up API
     28     * resources needlessly, and introduce the risk of overwriting the valid data with invalid data if something breaks.
     29     */
     30    if ( time() >= strtotime( 'June 15, 2018' ) ) {
     31        return;
     32    }
     33
     34    $gp_api            = 'https://translate.wordpress.org';
     35    $gp_project        = 'meta/wp15';
     36    $localizations_dir = WP_CONTENT_DIR . '/languages/wp15';
     37    $set_response      = wp_remote_get( "$gp_api/api/projects/$gp_project" );
     38    $body              = json_decode( wp_remote_retrieve_body( $set_response ) );
     39    $translation_sets  = isset( $body->translation_sets ) ? $body->translation_sets : false;
     40
     41    if ( ! $translation_sets ) {
     42        trigger_error( 'Translation sets missing from response body.' );
     43        return;
     44    }
     45
     46    $locale_status = wp_list_pluck( $translation_sets, 'percent_translated', 'wp_locale' );
     47    update_option( 'wp15_locale_status', $locale_status );
     48
     49    foreach ( $translation_sets as $set ) {
     50        if ( empty( $set->locale ) || empty( $set->wp_locale ) ) {
     51            continue;
     52        }
     53
     54        $po_response = wp_remote_get( "$gp_api/projects/$gp_project/{$set->locale}/default/export-translations?filters[status]=current&format=po" );
     55        $mo_response = wp_remote_get( "$gp_api/projects/$gp_project/{$set->locale}/default/export-translations?filters[status]=current&format=mo" );
     56        $po_content  = wp_remote_retrieve_body( $po_response );
     57        $mo_content  = wp_remote_retrieve_body( $mo_response );
     58
     59        if ( ! $po_content || ! $mo_content || false === strpos( $po_content, 'Project-Id-Version: Meta - wp15.wordpress.net' ) ) {
     60            trigger_error( "Invalid PO/MO content for {$set->wp_locale}." );
     61            continue;
     62        }
     63
     64        file_put_contents( "$localizations_dir/wp15-{$set->wp_locale}.po", $po_content );
     65        file_put_contents( "$localizations_dir/wp15-{$set->wp_locale}.mo", $mo_content );
     66    }
     67}
     68
     69add_filter( 'wp15_update_pomo_files', __NAMESPACE__ . '\update_pomo_files' );
    1770
    1871/**
Note: See TracChangeset for help on using the changeset viewer.