Make WordPress Core

Changeset 25566


Ignore:
Timestamp:
09/23/2013 02:07:51 AM (10 years ago)
Author:
nacin
Message:

Add a language pack upgrader class.

At the conclusion of any upgrade, after the transients are refreshed from the API, pending translations are downloaded and installed to wp-content/languages.

props dd32.
see #18200.

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-upgrader-skins.php

    r25496 r25566  
    610610    function after() {}
    611611}
     612
     613/**
     614 * A basic Upgrader skin which doesn't have any headers or footers.
     615 *
     616 * @package WordPress
     617 * @subpackage Upgrader
     618 * @since 3.7.0
     619 */
     620class Headerless_Upgrader_Skin extends WP_Upgrader_Skin {
     621    function before() {}
     622    function after() {}
     623    function header() {}
     624    function footer() {}
     625}
  • trunk/src/wp-admin/includes/class-wp-upgrader.php

    r25543 r25566  
    10741074}
    10751075
     1076add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20, 3 );
     1077class Language_Pack_Upgrader extends WP_Upgrader {
     1078
     1079    var $result;
     1080    var $bulk = true;
     1081
     1082    static function async_upgrade( $upgrader, $context, $package ) {
     1083        // Avoid recursion.
     1084        if ( $upgrader instanceof Language_Pack_Upgrader )
     1085            return;
     1086
     1087        $lp_upgrader = new Language_Pack_Upgrader( new Headerless_Upgrader_Skin() );
     1088        $lp_upgrader->upgrade();
     1089    }
     1090
     1091    function upgrade_strings() {
     1092        $this->strings['starting_upgrade'] = __( 'Some of your language files need updating. Sit tight for a few more seconds while we update them as well.' );
     1093        $this->strings['up_to_date'] = __( 'The language is up to date.' ); // We need to silently skip this case
     1094        $this->strings['no_package'] = __( 'Update package not available.' );
     1095        $this->strings['downloading_package'] = __( 'Downloading language update from <span class="code">%s</span>&#8230;' );
     1096        $this->strings['unpack_package'] = __( 'Unpacking the update&#8230;' );
     1097        $this->strings['process_failed'] = __( 'Language update failed.' );
     1098        $this->strings['process_success'] = __( 'Language updated successfully.' );
     1099    }
     1100
     1101    function upgrade() {
     1102        return $this->bulk_upgrade();
     1103    }
     1104
     1105    function bulk_upgrade() {
     1106
     1107        $this->init();
     1108        $this->upgrade_strings();
     1109
     1110        $language_updates = wp_get_translation_updates();
     1111
     1112        if ( empty( $language_updates ) )
     1113            return true;
     1114
     1115        $this->skin->feedback( 'starting_upgrade' );
     1116
     1117        add_filter( 'upgrader_source_selection', array( &$this, 'check_package' ), 10, 3 );
     1118
     1119        $this->skin->header();
     1120
     1121        // Connect to the Filesystem first.
     1122        $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
     1123        if ( ! $res ) {
     1124            $this->skin->footer();
     1125            return false;
     1126        }
     1127
     1128        $results = array();
     1129
     1130        $this->update_count = count( $language_updates );
     1131        $this->update_current = 0;
     1132        foreach ( $language_updates as $language_update ) {
     1133
     1134            $destination = WP_LANG_DIR;
     1135            if ( 'plugin' == $language_update->type )
     1136                $destination .= '/plugins';
     1137            elseif ( 'theme' == $language_update->type )
     1138                $destination .= '/themes';
     1139
     1140            $this->update_current++;
     1141
     1142            $options = array(
     1143                'package' => $language_update->package,
     1144                'destination' => $destination,
     1145                'clear_destination' => false,
     1146                'abort_if_destination_exists' => false, // We expect the destination to exist.
     1147                'clear_working' => true,
     1148                'is_multi' => true,
     1149                'hook_extra' => array(
     1150                    'language_update_type' => $language_update->type,
     1151                    'language_update' => $language_update,
     1152                )
     1153            );
     1154
     1155            $result = $this->run( $options );
     1156
     1157            $results[] = $this->result;
     1158
     1159            // Prevent credentials auth screen from displaying multiple times.
     1160            if ( false === $result )
     1161                break;
     1162        }
     1163
     1164        // Clean up our hooks, in case something else does an upgrade on this connection.
     1165        remove_filter( 'upgrader_source_selection', array( &$this, 'check_package' ), 10, 2 );
     1166
     1167        return $results;
     1168    }
     1169
     1170    function check_package( $source, $remote_source ) {
     1171        global $wp_filesystem;
     1172
     1173        if ( is_wp_error( $source ) )
     1174            return $source;
     1175
     1176        // Check that the folder contains a valid language.
     1177        $files = $wp_filesystem->dirlist( $remote_source );
     1178
     1179        // Check to see if a .po and .mo exist in the folder.
     1180        $po = $mo = false;
     1181        foreach ( (array) $files as $file => $filedata ) {
     1182            if ( '.po' == substr( $file, -3 ) )
     1183                $po = true;
     1184            elseif ( '.mo' == substr( $file, -3 ) )
     1185                $mo = true;
     1186        }
     1187
     1188        if ( ! $mo || ! $po )
     1189            return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'],
     1190                __( 'The language pack is missing either the <code>.po</code> or <code>.mo</code> files.' ) );
     1191
     1192        return $source;
     1193    }
     1194
     1195}
     1196
    10761197/**
    10771198 * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combination with the wp-admin/includes/update-core.php file
  • trunk/src/wp-admin/includes/update.php

    r25540 r25566  
    380380}
    381381add_action( 'admin_notices', 'maintenance_nag' );
     382
     383/**
     384 * Retrieves a list of all language updates available.
     385 *
     386 * @since 3.7.0
     387 */
     388function wp_get_translation_updates() {
     389    $updates = array();
     390    $transients = array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' );
     391    foreach ( $transients as $transient => $type ) {
     392
     393        $transient = get_site_transient( $transient );
     394        if ( empty( $transient->translations ) )
     395            continue;
     396
     397        foreach ( $transient->translations as $translation ) {
     398            $updates[] = (object) $translation;
     399        }
     400    }
     401
     402    return $updates;
     403}
  • trunk/src/wp-includes/update.php

    r25544 r25566  
    159159    // Check for update on a different schedule, depending on the page.
    160160    switch ( current_filter() ) {
     161        case 'upgrader_process_complete' :
     162            $timeout = 0;
     163            break;
    161164        case 'load-update-core.php' :
    162165            $timeout = MINUTE_IN_SECONDS;
     
    284287    // Check for update on a different schedule, depending on the page.
    285288    switch ( current_filter() ) {
     289        case 'upgrader_process_complete' :
     290            $timeout = 0;
     291            break;
    286292        case 'load-update-core.php' :
    287293            $timeout = MINUTE_IN_SECONDS;
     
    486492add_action( 'admin_init', '_maybe_update_core' );
    487493add_action( 'wp_version_check', 'wp_version_check' );
     494add_action( 'upgrader_process_complete', 'wp_version_check' );
    488495
    489496add_action( 'load-plugins.php', 'wp_update_plugins' );
     
    492499add_action( 'admin_init', '_maybe_update_plugins' );
    493500add_action( 'wp_update_plugins', 'wp_update_plugins' );
     501add_action( 'upgrader_process_complete', 'wp_update_plugins' );
    494502
    495503add_action( 'load-themes.php', 'wp_update_themes' );
     
    498506add_action( 'admin_init', '_maybe_update_themes' );
    499507add_action( 'wp_update_themes', 'wp_update_themes' );
     508add_action( 'upgrader_process_complete', 'wp_update_themes' );
    500509
    501510// Automatic Updates - Cron callback
Note: See TracChangeset for help on using the changeset viewer.