Make WordPress Core

Changeset 44676


Ignore:
Timestamp:
01/21/2019 09:12:23 PM (6 years ago)
Author:
ocean90
Message:

I18N: Enable clear_destination in upgrader for updating/installing language packs.

Introduces Language_Pack_Upgrader::clear_destination() to clear existing translations before installing new translations. Ensures that unused translations in JSON format are cleaned up.

Props dd32, swissspidy, ocean90.
Fixes #45468.

File:
1 edited

Legend:

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

    r43598 r44676  
    119119        $this->strings['process_failed']      = __( 'Translation update failed.' );
    120120        $this->strings['process_success']     = __( 'Translation updated successfully.' );
     121        $this->strings['remove_old']          = __( 'Removing the old version of the translation…' );
     122        $this->strings['remove_old_failed']   = __( 'Could not remove the old translation.' );
    121123    }
    122124
     
    242244                'package'                     => $language_update->package,
    243245                'destination'                 => $destination,
    244                 'clear_destination'           => false,
     246                'clear_destination'           => true,
    245247                'abort_if_destination_exists' => false, // We expect the destination to exist.
    246248                'clear_working'               => true,
     
    386388    }
    387389
     390    /**
     391     * Clears existing translations where this item is going to be installed into.
     392     *
     393     * @since 5.1.0
     394     *
     395     * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
     396     *
     397     * @param string $remote_destination The location on the remote filesystem to be cleared.
     398     * @return bool|WP_Error True upon success, WP_Error on failure.
     399     */
     400    public function clear_destination( $remote_destination ) {
     401        global $wp_filesystem;
     402
     403        $language_update    = $this->skin->language_update;
     404        $language_directory = WP_LANG_DIR . '/'; // Local path for use with glob().
     405
     406        if ( 'core' === $language_update->type ) {
     407            $files = array(
     408                $remote_destination . $language_update->language . '.po',
     409                $remote_destination . $language_update->language . '.mo',
     410                $remote_destination . 'admin-' . $language_update->language . '.po',
     411                $remote_destination . 'admin-' . $language_update->language . '.mo',
     412                $remote_destination . 'admin-network-' . $language_update->language . '.po',
     413                $remote_destination . 'admin-network-' . $language_update->language . '.mo',
     414                $remote_destination . 'continents-cities-' . $language_update->language . '.po',
     415                $remote_destination . 'continents-cities-' . $language_update->language . '.mo',
     416            );
     417
     418            $json_translation_files = glob( $language_directory . $language_update->language . '-*.json' );
     419            if ( $json_translation_files ) {
     420                foreach ( $json_translation_files as $json_translation_file ) {
     421                    $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file );
     422                }
     423            }
     424        } else {
     425            $files = array(
     426                $remote_destination . $language_update->slug . '-' . $language_update->language . '.po',
     427                $remote_destination . $language_update->slug . '-' . $language_update->language . '.mo',
     428            );
     429
     430            $language_directory     = $language_directory . $language_update->type . 's/';
     431            $json_translation_files = glob( $language_directory . $language_update->slug . '-' . $language_update->language . '-*.json' );
     432            if ( $json_translation_files ) {
     433                foreach ( $json_translation_files as $json_translation_file ) {
     434                    $files[] = str_replace( $language_directory, $remote_destination, $json_translation_file );
     435                }
     436            }
     437        }
     438
     439        $files = array_filter( $files, array( $wp_filesystem, 'exists' ) );
     440
     441        // No files to delete.
     442        if ( ! $files ) {
     443            return true;
     444        }
     445
     446        // Check all files are writable before attempting to clear the destination.
     447        $unwritable_files = array();
     448
     449        // Check writability.
     450        foreach ( $files as $file ) {
     451            if ( ! $wp_filesystem->is_writable( $file ) ) {
     452                // Attempt to alter permissions to allow writes and try again.
     453                $wp_filesystem->chmod( $file, FS_CHMOD_FILE );
     454                if ( ! $wp_filesystem->is_writable( $file ) ) {
     455                    $unwritable_files[] = $file;
     456                }
     457            }
     458        }
     459
     460        if ( ! empty( $unwritable_files ) ) {
     461            return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
     462        }
     463
     464        foreach ( $files as $file ) {
     465            if ( ! $wp_filesystem->delete( $file ) ) {
     466                return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
     467            }
     468        }
     469
     470        return true;
     471    }
    388472}
Note: See TracChangeset for help on using the changeset viewer.