Make WordPress Core

Ticket #18200: lp.5.diff

File lp.5.diff, 17.9 KB (added by dimadin, 10 years ago)

Refreshed patch, tested as described in comment:ticket:18200:43

  • wp-admin/includes/class-wp-upgrader.php

     
    165165        function install_package($args = array()) {
    166166                global $wp_filesystem;
    167167                $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
    168                                                 'clear_destination' => false, 'clear_working' => false,
     168                                                'clear_destination' => false, 'clear_working' => false, 'abort_if_destination_exists' => true,
    169169                                                'hook_extra' => array());
    170170
    171171                $args = wp_parse_args($args, $defaults);
     
    224224                                return $removed;
    225225                        else if ( ! $removed )
    226226                                return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
    227                 } elseif ( $wp_filesystem->exists($remote_destination) ) {
     227                } elseif ( $abort_if_destination_exists && $wp_filesystem->exists($remote_destination) ) {
    228228                        //If we're not clearing the destination folder and something exists there already, Bail.
    229229                        //But first check to see if there are actually any files in the folder.
    230230                        $_files = $wp_filesystem->dirlist($remote_destination);
     
    272272                $defaults = array(      'package' => '', //Please always pass this.
    273273                                                        'destination' => '', //And this
    274274                                                        'clear_destination' => false,
     275                                                        'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
    275276                                                        'clear_working' => true,
    276277                                                        'is_multi' => false,
    277278                                                        'hook_extra' => array() //Pass any extra $hook_extra args here, this will be passed to any hooked filters.
     
    318319                                                                                        'source' => $working_dir,
    319320                                                                                        'destination' => $destination,
    320321                                                                                        'clear_destination' => $clear_destination,
     322                                                                                        'abort_if_destination_exists' => $abort_if_destination_exists,
    321323                                                                                        'clear_working' => $clear_working,
    322324                                                                                        'hook_extra' => $hook_extra
    323325                                                                                ) );
     
    412414                // Force refresh of plugin update information
    413415                delete_site_transient('update_plugins');
    414416                wp_cache_delete( 'plugins', 'plugins' );
     417                do_action( 'upgrader_process_complete', $this, 'install-plugin', $package );
    415418
    416419                return true;
    417420        }
     
    457460                // Force refresh of plugin update information
    458461                delete_site_transient('update_plugins');
    459462                wp_cache_delete( 'plugins', 'plugins' );
     463                do_action( 'upgrader_process_complete', $this, 'update-plugin', $plugin );
    460464        }
    461465
    462466        function bulk_upgrade($plugins) {
     
    539543                // Force refresh of plugin update information
    540544                delete_site_transient('update_plugins');
    541545                wp_cache_delete( 'plugins', 'plugins' );
     546                do_action( 'upgrader_process_complete', $this, 'bulk-upgrade-plugins', $plugins );
    542547
    543548                return $results;
    544549        }
     
    764769
    765770                // Force refresh of theme update information
    766771                wp_clean_themes_cache();
     772                do_action( 'upgrader_process_complete', $this, 'install-theme', $package );
    767773
    768774                return true;
    769775        }
     
    810816
    811817                // Force refresh of theme update information
    812818                wp_clean_themes_cache();
     819                do_action( 'upgrader_process_complete', $this, 'upgrade-theme', $theme );
    813820
    814821                return true;
    815822        }
     
    897904
    898905                // Force refresh of theme update information
    899906                wp_clean_themes_cache();
     907                do_action( 'upgrader_process_complete', $this, 'bulk-upgrade-themes', $themes );
    900908
    901909                return $results;
    902910        }
     
    10671075                if ( ! function_exists( 'update_core' ) )
    10681076                        return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
    10691077
    1070                 return update_core($working_dir, $wp_dir);
     1078                $result = update_core($working_dir, $wp_dir);
     1079                do_action( 'upgrader_process_complete', $this, 'upgrade-core', $result );
     1080                return $result;
    10711081        }
    10721082
    10731083}
    10741084
     1085add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20, 3);
     1086class Language_Pack_Upgrader extends WP_Upgrader {
     1087       
     1088        var $result;
     1089        var $bulk = true;
     1090
     1091        static function async_upgrade( $upgrader, $context, $package ) {
     1092                // Don't do Language updates on language updates on language updates..
     1093                if ( $upgrader instanceof Language_Pack_Upgrader )
     1094                        return;
     1095
     1096                echo '<!-- start languages --><div style="padding-left: 3em; border-left: 5px solid grey">';
     1097
     1098                $lp_upgrader = new Language_Pack_Upgrader( new Headerless_Skin() );
     1099
     1100                $lp_upgrader->upgrade();
     1101
     1102                echo '</div><!-- end of languages -->';
     1103        }
     1104        function upgrade_strings() {
     1105                $this->strings['starting_upgrade'] = __( 'Some of your language files need updating, Sit tight for a few more seconds while we update them as well.' );
     1106                $this->strings['up_to_date'] = __('The language is up to date.'); // We need to silently skip this case
     1107                $this->strings['no_package'] = __('Update package not available.');
     1108                $this->strings['downloading_package'] = __('Downloading language update from <span class="code">%s</span>&#8230;');
     1109                $this->strings['unpack_package'] = __('Unpacking the update&#8230;');
     1110                $this->strings['remove_old'] = __('Removing the old version of the language&#8230;');
     1111                $this->strings['remove_old_failed'] = __('Could not remove the old language.');
     1112                $this->strings['process_failed'] = __('Language update failed.');
     1113                $this->strings['process_success'] = __('Language updated successfully.');
     1114        }
     1115
     1116        function upgrade() {
     1117                return $this->bulk_upgrade();
     1118        }
     1119
     1120        function bulk_upgrade() {
     1121
     1122                $this->init();
     1123                $this->upgrade_strings();
     1124
     1125                $language_updates = get_language_updates();
     1126
     1127                if ( empty($language_updates) )
     1128                        return true;
     1129               
     1130                $this->skin->feedback( 'starting_upgrade' );
     1131
     1132                add_filter('upgrader_source_selection', array(&$this, 'check_package'), 10, 3 );
     1133
     1134                $this->skin->header();
     1135
     1136                // Connect to the Filesystem first.
     1137                $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_LANG_DIR) );
     1138                if ( ! $res ) {
     1139                        $this->skin->footer();
     1140                        return false;
     1141                }
     1142
     1143                $results = array();
     1144
     1145                $this->update_count = count($language_updates);
     1146                $this->update_current = 0;
     1147                foreach ( $language_updates as $language_update ) {
     1148
     1149                        $destination = WP_LANG_DIR;
     1150                        if ( 'plugin' == $language_update->type ) {
     1151                                $destination .= '/plugins';
     1152                        } elseif ( 'theme' == $language_update->type ) {
     1153                                $destination .= '/themes';
     1154                        }
     1155
     1156                        $this->update_current++;
     1157
     1158                        $options = array(
     1159                                'package' => $language_update->package,
     1160                                'destination' => $destination,
     1161                                'clear_destination' => false,
     1162                                'abort_if_destination_exists' => false, // We expect the destination to exist.
     1163                                'clear_working' => true,
     1164                                'is_multi' => true,
     1165                                'hook_extra' => array(
     1166                                        'language_update_type' => $language_update->type,
     1167                                        'language_update' => $language_update,
     1168                                )
     1169                        );
     1170
     1171                        $result = $this->run($options);
     1172
     1173                        $results[] = $this->result;
     1174
     1175                        // Prevent credentials auth screen from displaying multiple times
     1176                        if ( false === $result )
     1177                                break;
     1178                } //end foreach $language_updates
     1179
     1180                // Cleanup our hooks, in case something else does a upgrade on this connection.
     1181                remove_filter('upgrader_source_selection', array(&$this, 'check_package'), 10, 2 );
     1182
     1183                return $results;
     1184        }
     1185
     1186        function check_package( $source, $remote_source ) {
     1187                global $wp_filesystem;
     1188
     1189                if ( is_wp_error($source) )
     1190                        return $source;
     1191
     1192                // Check the folder contains a valid language
     1193                $files = $wp_filesystem->dirlist( $remote_source );
     1194
     1195                // Check to see if a .po and .mo exist in the folder..
     1196                $po = $mo = false;
     1197                foreach ( (array)$files as $file => $filedata ) {
     1198                        if ( '.po' == substr( $file, -3) )
     1199                                $po = true;
     1200                        elseif ( '.mo' == substr($file, -3) )
     1201                                $mo = true;
     1202                }
     1203
     1204                if ( ! $mo || ! $po )
     1205                        return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], __( 'The language pack is missing either the <code>.po</code> or <code>.mo</code> files.' ) );
     1206
     1207                return $source;
     1208        }
     1209
     1210}
     1211
    10751212/**
    10761213 * Generic Skin for the WordPress Upgrader classes. This skin is designed to be extended for specific purposes.
    10771214 *
     
    11601297}
    11611298
    11621299/**
     1300 * A basic Upgrader skin which doesn't have any Header/Footers
     1301 *
     1302 * @package WordPress
     1303 * @subpackage Upgrader
     1304 * @since 3.4.0
     1305 */
     1306class Headerless_Skin extends WP_Upgrader_Skin {
     1307        function before() {}
     1308        function after() {}
     1309        function header() {}
     1310        function footer() {}
     1311}
     1312
     1313/**
    11631314 * Plugin Upgrader Skin for WordPress Plugin Upgrades.
    11641315 *
    11651316 * @TODO More Detailed docs, for methods as well.
  • wp-admin/includes/update.php

     
    308308        echo "<div class='update-nag'>$msg</div>";
    309309}
    310310add_action( 'admin_notices', 'maintenance_nag' );
     311
     312// Retrieves a list of all language updates available.
     313function get_language_updates() {
     314
     315        $updates = array();
     316        foreach ( array( 'update_core' => 'core', 'update_plugins' => 'plugin', 'update_themes' => 'theme' ) as $transient => $type ) {
     317
     318                $transient = get_site_transient( $transient );
     319                if ( empty( $transient->language_updates ) )
     320                        continue;
     321
     322                foreach ( (array)$transient->language_updates as $update ) {
     323                        if ( empty( $update->type ) )
     324                                $update->type = $type;
     325
     326                        $updates[] = $update;
     327                }
     328        }
     329
     330        return $updates;
     331
     332/*      // Expected content:
     333        return array(
     334                        (object)array(
     335                                                'type' => 'core',
     336                                                'lang' => 'de_DE', // The language it is.. This isn't currently used anywhere
     337                                                'version' => '2012-02-18 08:48:04', // New Version - Nothing looks at this either.
     338                                                'package' => 'http://core-downloads/core-de_DE.zip'
     339                                                ),
     340                        (object)array(
     341                                                'type' => 'plugin', // The type of the upgrade, 'plugin' or 'theme' will throw files into that folder, any other name (ie. 'core') is thrown into WP_LANG_DIR..
     342                                                'slug' => 'akismet', // not used.
     343                                                'for' => 'akismet/akismet.php', // again, not used yet.
     344                                                'lang' => 'de_DE',
     345                                                'version' => '2012-02-18 08:48:04',
     346                                                'package' => 'http://core-downloads/akismet-de_DE.zip'
     347                                                ),
     348                        (object)array(
     349                                                'type' => 'theme',
     350                                                'slug' => 'twentytwelve',
     351                                                'lang' => 'de_DE',
     352                                                'version' => '2012-02-18 08:48:04',
     353                                                'package' => 'http://core-downloads/twentytwelve-de_DE.zip' // And no, this isn't a translation of 2012, I believe it's a out of date Core set.
     354                                                ),
     355                );
     356Zip file expected format:
     357./akismet-de_DE.mo
     358./akismet-de_DE.po
     359(ie. Language files bundled together in root, other files such as .php and .css would be kept with it)
     360*/
     361}
     362 No newline at end of file
  • wp-includes/l10n.php

     
    550550        }
    551551
    552552        return $languages;
    553 }
    554  No newline at end of file
     553}
     554
     555// Returns a list of translation files and their details.
     556function get_installed_language_files( $type = false ) {
     557        $language_files = array();
     558
     559        if ( ! is_dir( WP_LANG_DIR ) )
     560                return $language_files;
     561
     562        $dirs_to_check = array( WP_LANG_DIR );
     563        foreach ( (array)glob( WP_LANG_DIR . '/*', GLOB_ONLYDIR) as $dir )
     564                $dirs_to_check[] = $dir;
     565
     566        foreach ( $dirs_to_check as $dir ) {
     567                foreach ( (array)glob( $dir . '/*.po') as $lang_file ) {
     568                        $lang_file = str_replace( trailingslashit(WP_LANG_DIR), '', $lang_file );
     569
     570                        $file_data = get_translation_file_data( $lang_file );
     571                        $file_data['file'] = basename( $lang_file );
     572                        $file_data['type'] = dirname( $lang_file );
     573                        if ( '.' == $file_data['type'] )
     574                                $file_data['type'] = 'core';                           
     575
     576                        if ( $type && $type != $file_data['type'] )
     577                                continue;
     578
     579                        if ( preg_match( '!(.*?)-?([a-z]{2}_[a-z]{2})?\.po!i', $file_data['file'], $lang_match ) ) {
     580                                $file_data['slug'] = $lang_match[1];
     581                                if ( isset( $lang_match[2] ) )
     582                                        $file_data['language'] = $lang_match[2];
     583                        }
     584
     585                        $language_files[] = $file_data;
     586
     587                }
     588        }
     589
     590        return $language_files;
     591}
     592
     593// retrieves the date/generator/project-id headers from a po file relative to WP_LANG_DIR
     594function get_translation_file_data( $file ) {
     595        // Just incase someone calls with the machine file instead of the textual version
     596        $file = preg_replace( '!\.mo$!i', '.po', $file );
     597
     598        // Lets just assume that it's within the WP_LANG_DIR for now
     599        // if we keep track of the loaded text domains later, we can probably include a lookup for the location of the files instead.
     600        $data = get_file_data(  WP_LANG_DIR . '/' . $file, array(
     601                'date' => '"PO-Revision-Date',
     602                'generator' => '"X-Generator',
     603                'project-id' => '"Project-Id-Version'
     604        ) );
     605
     606        // Strip the .po field endings off the values, which may, or may not, include a textual \n
     607        foreach ( (array)$data as $key => $value )
     608                $data[ $key ] = preg_replace( '!(\\\n)?"$!', '', $value);
     609
     610        return $data;
     611 }
     612 No newline at end of file
  • wp-includes/update.php

     
    6464                $wp_install = home_url( '/' );
    6565        }
    6666
     67        $installed_languages = get_installed_language_files( 'core' );
     68
    6769        $query = array(
    6870                'version'           => $wp_version,
    6971                'php'               => $php_version,
     
    8082        $options = array(
    8183                'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
    8284                'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
     85                'body' => array( 'installed_languages' => serialize( $installed_languages ) ),
    8386                'headers' => array(
    8487                        'wp_install' => $wp_install,
    8588                        'wp_blog' => home_url( '/' )
    8689                )
    8790        );
    8891
    89         $response = wp_remote_get($url, $options);
     92        $response = wp_remote_post($url, $options);
    9093
    9194        if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
    9295                return false;
     
    117120        $updates->updates = $offers;
    118121        $updates->last_checked = time();
    119122        $updates->version_checked = $wp_version;
     123        if ( isset( $body['language_updates'] ) )
     124                $updates->language_updates = $body['language_updates'];
    120125        set_site_transient( 'update_core',  $updates);
    121126}
    122127
     
    154159
    155160        // Check for update on a different schedule, depending on the page.
    156161        switch ( current_filter() ) {
     162                case 'upgrader_process_complete':
     163                        $timeout = 0;
     164                        break;
    157165                case 'load-update-core.php' :
    158166                        $timeout = MINUTE_IN_SECONDS;
    159167                        break;
     
    194202        $current->last_checked = time();
    195203        set_site_transient( 'update_plugins', $current );
    196204
     205        $installed_languages = get_installed_language_files( 'plugins' );
     206
    197207        $to_send = (object) compact('plugins', 'active');
    198208
    199209        $options = array(
    200210                'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
    201                 'body' => array( 'plugins' => serialize( $to_send ) ),
     211                'body' => array( 'plugins' => serialize( $to_send ), 'installed_languages' => serialize( $installed_languages ) ),
    202212                'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
    203213        );
    204214
     
    209219
    210220        $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
    211221
    212         if ( is_array( $response ) )
     222        if ( is_array( $response ) ) {
     223                if ( isset( $response['language_updates'] ) ) {
     224                        $new_option->language_updates = $response['language_updates'];
     225                        unset( $response['language_updates'] );
     226                }
    213227                $new_option->response = $response;
    214         else
     228        } else {
    215229                $new_option->response = array();
     230        }
    216231
    217232        set_site_transient( 'update_plugins', $new_option );
    218233}
     
    263278
    264279        // Check for update on a different schedule, depending on the page.
    265280        switch ( current_filter() ) {
     281                case 'upgrader_process_complete':
     282                        $timeout = 0;
     283                        break;
    266284                case 'load-update-core.php' :
    267285                        $timeout = MINUTE_IN_SECONDS;
    268286                        break;
     
    301319        $last_update->last_checked = time();
    302320        set_site_transient( 'update_themes', $last_update );
    303321
     322        $installed_languages = get_installed_language_files( 'themes' );
     323
    304324        $options = array(
    305325                'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
    306                 'body'                  => array( 'themes' => serialize( $themes ) ),
     326                'body'                  => array( 'themes' => serialize( $themes ), 'installed_languages' => serialize( $installed_languages ) ),
    307327                'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
    308328        );
    309329
     
    317337        $new_update->checked = $checked;
    318338
    319339        $response = maybe_unserialize( wp_remote_retrieve_body( $raw_response ) );
    320         if ( is_array( $response ) )
     340
     341        if ( is_array( $response ) ) {
     342                if ( isset( $response['language_updates'] ) ) {
     343                        $new_update->language_updates = $response['language_updates'];
     344                        unset( $response['language_updates'] );
     345                }
    321346                $new_update->response = $response;
     347        }
    322348
    323349        set_site_transient( 'update_themes', $new_update );
    324350}
     
    433459
    434460add_action( 'admin_init', '_maybe_update_core' );
    435461add_action( 'wp_version_check', 'wp_version_check' );
     462add_action( 'upgrader_process_complete', 'wp_version_check' );
    436463
    437464add_action( 'load-plugins.php', 'wp_update_plugins' );
    438465add_action( 'load-update.php', 'wp_update_plugins' );
    439466add_action( 'load-update-core.php', 'wp_update_plugins' );
    440467add_action( 'admin_init', '_maybe_update_plugins' );
    441468add_action( 'wp_update_plugins', 'wp_update_plugins' );
     469add_action( 'upgrader_process_complete', 'wp_update_plugins' );
    442470
    443471add_action( 'load-themes.php', 'wp_update_themes' );
    444472add_action( 'load-update.php', 'wp_update_themes' );
    445473add_action( 'load-update-core.php', 'wp_update_themes' );
    446474add_action( 'admin_init', '_maybe_update_themes' );
    447475add_action( 'wp_update_themes', 'wp_update_themes' );
     476add_action( 'upgrader_process_complete', 'wp_update_themes' );
    448477
    449478add_action('init', 'wp_schedule_update_checks');