Make WordPress Core


Ignore:
Timestamp:
09/23/2013 02:07:51 AM (12 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.