Making WordPress.org

Changeset 6891


Ignore:
Timestamp:
03/20/2018 11:54:08 PM (6 years ago)
Author:
iandunn
Message:

WP15: Add cron job to update PO/MO files hourly.

Location:
sites/trunk/wp15.wordpress.net
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wp15.wordpress.net/.gitignore

    r6877 r6891  
    77config-private.php
    88public_html/content/cache
     9public_html/content/languages
    910public_html/content/upgrade
    1011public_html/content/uploads
  • sites/trunk/wp15.wordpress.net/public_html/content/mu-plugins/miscellaneous.php

    r6882 r6891  
    99*/
    1010
    11 namespace WP15\Updates;
     11namespace WP15\Miscellaneous;
    1212use DateTime;
    1313
     
    1616add_filter( 'map_meta_cap',  __NAMESPACE__ . '\allow_css_editing', 10, 2   );
    1717add_filter( 'tggr_end_date', __NAMESPACE__ . '\set_tagregator_cutoff_date' );
     18add_filter( 'wp15_update_pomo_files', __NAMESPACE__ . '\update_pomo_files' );
     19
     20if ( ! wp_next_scheduled( 'wp15_update_pomo_files' ) ) {
     21    wp_schedule_event( time(), 'hourly', 'wp15_update_pomo_files' );
     22}
    1823
    1924
     
    5459    return new DateTime( 'June 15, 2018' );
    5560}
     61
     62/**
     63 * Update the PO/MO files for the wp15 text domain.
     64 */
     65function update_pomo_files() {
     66    $gp_api            = 'https://translate.wordpress.org';
     67    $gp_project        = 'meta/wp15';
     68    $localizations_dir = WP_CONTENT_DIR . '/languages/wp15';
     69    $set_response      = wp_remote_get( "$gp_api/api/projects/$gp_project" );
     70    $body              = json_decode( wp_remote_retrieve_body( $set_response ) );
     71    $translation_sets  = isset( $body->translation_sets ) ? $body->translation_sets : false;
     72
     73    if ( ! $translation_sets ) {
     74        trigger_error( 'Translation sets missing from response body.' );
     75        return;
     76    }
     77
     78    foreach ( $translation_sets as $set ) {
     79        if ( empty( $set->locale ) || empty( $set->wp_locale ) ) {
     80            continue;
     81        }
     82
     83        $po_response = wp_remote_get( "$gp_api/projects/$gp_project/{$set->locale}/default/export-translations?filters[status]=current&format=po" );
     84        $mo_response = wp_remote_get( "$gp_api/projects/$gp_project/{$set->locale}/default/export-translations?filters[status]=current&format=mo" );
     85        $po_content  = wp_remote_retrieve_body( $po_response );
     86        $mo_content  = wp_remote_retrieve_body( $mo_response );
     87
     88        if ( ! $po_content || ! $mo_content || false === strpos( $po_content, "Language: {$set->wp_locale}" ) ) {
     89            trigger_error( "Invalid PO/MO content for {$set->wp_locale}." );
     90            continue;
     91        }
     92
     93        file_put_contents( "$localizations_dir/wp15-{$set->wp_locale}.po", $po_content );
     94        file_put_contents( "$localizations_dir/wp15-{$set->wp_locale}.mo", $mo_content );
     95    }
     96}
Note: See TracChangeset for help on using the changeset viewer.