WordPress.org

Making WordPress.org

Changeset 2880


Ignore:
Timestamp:
04/02/2016 10:24:29 AM (5 years ago)
Author:
ocean90
Message:

Translate, Plugin Directory: Delay cache purging until shutdown to prevent multiple purges for the same project.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/wporg-gp-plugin-directory.php

    r2690 r2880  
    33 * Plugin name: GlotPress: Plugin Directory Bridge
    44 * Description: Clears the content translation cache for plugins (readme/code) hosted on wordpress.org based on actions taken withing translate.wordpress.org.
    5  * Version:     1.0
     5 * Version:     1.1
    66 * Author:      WordPress.org
    77 * Author URI:  http://wordpress.org/
     
    1313    public $i18n_cache_group = 'plugins-i18n';
    1414
     15    private $translation_edits_queue = array();
     16
    1517    public function __construct() {
    1618        add_action( 'init', array( $this, 'add_global_cache_group' ) );
    1719        add_action( 'gp_originals_imported', array( $this, 'originals_imported' ) );
    18         add_action( 'gp_translation_created', array( $this, 'translation_created' ) );
    19         add_action( 'gp_translation_saved', array( $this, 'translation_saved' ) );
     20        add_action( 'gp_translation_created', array( $this, 'queue_translation_for_cache_purge' ) );
     21        add_action( 'gp_translation_saved', array( $this, 'queue_translation_for_cache_purge' ) );
     22
     23        // Cache purging is delayed until shutdown to prevent multiple purges for the same project.
     24        add_action( 'shutdown', array( $this, 'delete_plugin_i18n_cache_on_translation_edits' ) );
    2025
    2126        if ( defined( 'WP_CLI' ) && WP_CLI ) {
     
    5964
    6065    /**
    61      * Triggers a cache purge when a new translation was created.
    62      *
    63      * @param GP_Translation $translation Created translation.
    64      */
    65     public function translation_created( $translation ) {
     66     * Adds a translation to a cache purge queue when a translation was created
     67     * or updated.
     68     *
     69     * @param GP_Translation $translation Created/updated translation.
     70     */
     71    public function queue_translation_for_cache_purge( $translation ) {
    6672        if ( ! $this->project_is_plugin( $_SERVER['REQUEST_URI'] ) ) {
    6773            return;
    6874        }
    6975
    70         $this->delete_plugin_i18n_cache_on_translation_edit( $translation );
    71     }
    72 
    73     /**
    74      * Triggers a cache purge when a translation was updated.
    75      *
    76      * @param GP_Translation $translation Updated translation.
    77      */
    78     public function translation_saved( $translation ) {
    79         if ( ! $this->project_is_plugin( $_SERVER['REQUEST_URI'] ) ) {
    80             return;
    81         }
    82 
    83         $this->delete_plugin_i18n_cache_on_translation_edit( $translation );
     76        $this->translation_edits_queue[ $translation->original_id ][ $translation->translation_set_id ] = true;
     77    }
     78
     79    /**
     80     * Deletes the cache on a translation edits.
     81     *
     82     * @param GP_Translation $translation The edited translation.
     83     */
     84    public function delete_plugin_i18n_cache_on_translation_edits() {
     85        if ( empty( $this->translation_edits_queue ) ) {
     86            return;
     87        }
     88
     89        $purged = array();
     90        foreach ( $this->translation_edits_queue as $original_id => $set_ids ) {
     91            $original = GP::$original->get( $original_id );
     92            if ( ! $original ) {
     93                return;
     94            }
     95
     96            foreach ( array_keys( $set_ids ) as $set_id ) {
     97                if ( in_array( "{$original->project_id}-{$set_id}", $purged ) ) {
     98                    continue;
     99                }
     100
     101                $translation_set = GP::$translation_set->get( $set_id );
     102                if ( ! $translation_set ) {
     103                    return;
     104                }
     105
     106                $this->delete_plugin_i18n_cache_keys_for_locale( $original->project_id, $translation_set->locale );
     107                $purged[] = "{$original->project_id}-{$set_id}";
     108            }
     109        }
    84110    }
    85111
     
    177203        $this->delete_plugin_i18n_cache_keys_for( $prefix, $locale );
    178204    }
    179 
    180     /**
    181      * Deletes the cache on a translation edit.
    182      *
    183      * @param GP_Translation $translation The edited translation.
    184      */
    185     private function delete_plugin_i18n_cache_on_translation_edit( $translation ) {
    186         $original = GP::$original->get( $translation->original_id );
    187         if ( ! $original ) {
    188             return;
    189         }
    190 
    191         $translation_set = GP::$translation_set->get( $translation->translation_set_id );
    192         if ( ! $translation_set ) {
    193             return;
    194         }
    195 
    196         $this->delete_plugin_i18n_cache_keys_for_locale( $original->project_id, $translation_set->locale );
    197     }
    198205}
    199206
Note: See TracChangeset for help on using the changeset viewer.