WordPress.org

Making WordPress.org

Changeset 2266


Ignore:
Timestamp:
01/10/2016 06:52:35 PM (4 years ago)
Author:
ocean90
Message:

Translate, Plugin Directory: Clear caches when a translation gets approved or rejected.

Use translation_created and translation_saved actions to catch all translation edits.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-plugins.php

    r1160 r2266  
    66 */
    77class GP_WPorg_Plugins extends GP_Plugin {
    8     var $master_project   = 'wp-plugins';
    9     var $i18n_cache_group = 'plugins-i18n';
     8    public $master_project   = 'wp-plugins';
     9    public $i18n_cache_group = 'plugins-i18n';
    1010
    11     function __construct() {
     11    public function __construct() {
    1212        parent::__construct();
    1313        $this->add_action( 'init' );
    1414        $this->add_action( 'originals_imported' );
    15         $this->add_action( 'post_tmpl_load' );
     15
     16        $this->add_action( 'translation_created' );
     17        $this->add_action( 'translation_saved' );
    1618    }
    1719
     
    1921     * Making sure to register the global cache group wordpress.org/plugins/ uses for its display cache.
    2022     */
    21     function init() {
     23    public function init() {
    2224        wp_cache_add_global_groups( $this->i18n_cache_group );
    2325    }
     
    2628     * @param $project_id
    2729     */
    28     function originals_imported( $project_id ) {
     30    public function originals_imported( $project_id ) {
    2931        $project = GP::$project->get( $project_id );
    3032        if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) {
    3133            return;
    3234        }
     35
    3336        $this->delete_plugin_i18n_cache_keys_for_project( $project_id );
    3437    }
    3538
    3639    /**
    37      * @param $action
     40     * Triggers a cache purge when a new translation was created.
     41     *
     42     * @param GP_Translation $translation Created translation.
    3843     */
    39     function post_tmpl_load( $action ) {
    40         if ( !$this->project_is_plugin( $_SERVER["REQUEST_URI"] ) ) {
     44    public function translation_created( $translation ) {
     45        if ( ! $this->project_is_plugin( $_SERVER['REQUEST_URI'] ) ) {
    4146            return;
    4247        }
    43         switch( $action ) {
    44             case 'translation-row':
    45                 if ( ! empty( $_POST['translation'] ) ) {
    46                     $this->delete_plugin_i18n_cache_on_translation_edit();
    47                 }
    48                 break;
     48
     49        $this->delete_plugin_i18n_cache_on_translation_edit( $translation );
     50    }
     51
     52    /**
     53     * Triggers a cache purge when a translation was updated.
     54     *
     55     * @param GP_Translation $translation Updated translation.
     56     */
     57    public function translation_saved( $translation ) {
     58        if ( ! $this->project_is_plugin( $_SERVER['REQUEST_URI'] ) ) {
     59            return;
    4960        }
     61
     62        $this->delete_plugin_i18n_cache_on_translation_edit( $translation );
    5063    }
    5164
     
    5568     * @return bool
    5669     */
    57     function project_is_plugin( $path ) {
     70    private function project_is_plugin( $path ) {
    5871        if ( empty( $path ) ) {
    5972            return false;
    6073        }
     74
    6175        $path = '/' . trim( $path, '/' ) . '/';
    6276        if ( false === strpos( $path, "/{$this->master_project}/" ) ) {
    6377            return false;
    6478        }
     79
    6580        return true;
    6681    }
     
    7186     * @return string
    7287     */
    73     function get_plugin_i18n_cache_prefix( $project_id ) {
     88    private function get_plugin_i18n_cache_prefix( $project_id ) {
    7489        $project = GP::$project->get( $project_id );
    7590        if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) {
    7691            return '';
    7792        }
     93
    7894        $project_dirs = explode( '/', trim( $project->path, '/' ) );
    7995        if ( empty( $project_dirs ) || 3 !== count( $project_dirs ) || $project_dirs[0] !== $this->master_project ) {
    8096            return '';
    8197        }
     98
    8299        return "{$this->master_project}:{$project_dirs[1]}:{$project_dirs[2]}";
    83100    }
     
    89106     * @param $set string Set, such as 'original', 'fr', 'de'.
    90107     */
    91     function delete_plugin_i18n_cache_keys_for( $prefix, $set ) {
     108    private function delete_plugin_i18n_cache_keys_for( $prefix, $set ) {
    92109        $suffixes = array(
    93110            'translation_set_id', 'title', 'short_description', 'installation', 'description',
     
    96113        foreach ( $suffixes as $suffix ) {
    97114            $cache_key = "{$prefix}:{$set}:{$suffix}";
    98             // error_log( serialize( array( $cache_key, $this->i18n_cache_group ) ) );
    99115            wp_cache_delete( $cache_key, $this->i18n_cache_group );
    100         }
    101         // Deal with fr also existing as fr_FR, etc.
    102         if ( 2 === strlen( $set ) ) {
    103             $this->delete_plugin_i18n_cache_keys_for( $prefix, strtolower( $set ) . '_' . strtoupper( $set ) );
    104116        }
    105117    }
     
    108120     * @param $project_id int
    109121     */
    110     function delete_plugin_i18n_cache_keys_for_project( $project_id ) {
     122    private function delete_plugin_i18n_cache_keys_for_project( $project_id ) {
    111123        $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id );
    112         if ( empty( $prefix ) ) {
     124        if ( ! $prefix ) {
    113125            return;
    114126        }
     127
    115128        wp_cache_delete( "{$prefix}:originals", $this->i18n_cache_group );
    116129        wp_cache_delete( "{$prefix}:branch_id", $this->i18n_cache_group );
    117130        $this->delete_plugin_i18n_cache_keys_for( $prefix, 'original' );
     131
    118132        $translation_sets = (array) GP::$translation_set->by_project_id( $project_id );
    119133        foreach ( $translation_sets as $translation_set ) {
     
    126140     * @param $locale string
    127141     */
    128     function delete_plugin_i18n_cache_keys_for_locale( $project_id, $locale ) {
     142    private function delete_plugin_i18n_cache_keys_for_locale( $project_id, $locale ) {
    129143        $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id );
    130         if ( empty( $prefix ) ) {
     144        if ( ! $prefix ) {
    131145            return;
    132146        }
     147
    133148        $this->delete_plugin_i18n_cache_keys_for( $prefix, $locale );
    134149    }
    135150
    136     function delete_plugin_i18n_cache_on_translation_edit() {
    137         if ( empty( $_POST['translation'] ) ) {
     151    /**
     152     * Deletes the cache on a translation edit.
     153     *
     154     * @param GP_Translation $translation The edited translation.
     155     */
     156    private function delete_plugin_i18n_cache_on_translation_edit( $translation ) {
     157        $original = GP::$original->get( $translation->original_id );
     158        if ( ! $original ) {
    138159            return;
    139160        }
    140         $tmp = array_keys( (array) $_POST['translation'] );
    141         if ( empty( $tmp[0] ) || !is_numeric( $tmp[0] ) ) {
     161
     162        $translation_set = GP::$translation_set->get( $translation->translation_set_id );
     163        if ( ! $translation_set ) {
    142164            return;
    143165        }
    144         $original = GP::$original->get( (int) $tmp[0] );
    145         if ( empty( $original ) ) {
    146             return;
    147         }
    148         $tmp = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
    149         if ( empty( $tmp ) || count( $tmp ) < 2 ) {
    150             return;
    151         }
    152         $this->delete_plugin_i18n_cache_keys_for_locale( $original->project_id, gp_sanitize_meta_key( $tmp[ count( $tmp ) - 2 ] ) );
     166
     167        $this->delete_plugin_i18n_cache_keys_for_locale( $original->project_id, $translation_set->locale );
    153168    }
    154169}
Note: See TracChangeset for help on using the changeset viewer.