Making WordPress.org


Ignore:
Timestamp:
06/10/2019 02:45:10 PM (7 years ago)
Author:
ocean90
Message:

Translate: Send approved translations to translation memory.

Props yoavf, ocean90.
See #4430.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/class-plugin.php

    r8767 r8937  
    55use GP;
    66use GP_Locales;
    7 use Text_Diff;
    8 use WP_Error;
    9 use WP_Http;
    10 use WP_Text_Diff_Renderer_inline;
    11 
    12 require_once ABSPATH . '/wp-includes/wp-diff.php' ;
    137
    148class Plugin {
     9
     10    const TM_UPDATE_EVENT = 'wporg_translate_tm_update';
    1511
    1612    /**
     
    1814     */
    1915    private static $instance;
     16
     17    /**
     18     * @var array
     19     */
     20    private $queue = [];
    2021
    2122    /**
     
    4546        add_action( 'gp_pre_tmpl_load', [ $this, 'pre_tmpl_load' ], 10, 2 );
    4647        add_action( 'wporg_translate_suggestions', [ $this, 'extend_translation_suggestions' ] );
     48
     49        if ( 'cli' !== PHP_SAPI ) {
     50            add_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
     51            add_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
     52
     53            // DB Writes are delayed until shutdown to bulk-update the stats during imports.
     54            add_action( 'shutdown', [ $this, 'schedule_tm_update' ], 3 );
     55        }
     56
     57        add_action( self::TM_UPDATE_EVENT, [ Translation_Memory_Client::class, 'update' ] );
     58    }
     59
     60    /**
     61     * Adds a translation in queue when a translation was created
     62     * or updated.
     63     *
     64     * @param \GP_Translation $translation Created/updated translation.
     65     */
     66    public function translation_updated( $translation ) {
     67        if ( ! $translation->user_id || 'current' !== $translation->status ) {
     68            return;
     69        }
     70
     71        $this->queue[ $translation->original_id ] = $translation->id;
     72    }
     73
     74    /**
     75     * Schedules a single event to update translation memory for new translations.
     76     */
     77    public function schedule_tm_update() {
     78        remove_action( 'gp_translation_created', [ $this, 'translation_updated' ], 3 );
     79        remove_action( 'gp_translation_saved', [ $this, 'translation_updated' ], 3 );
     80
     81        if ( ! $this->queue ) {
     82            return;
     83        }
     84
     85        wp_schedule_single_event( time() + 60, self::TM_UPDATE_EVENT, [ 'translations' => $this->queue ] );
    4786    }
    4887
Note: See TracChangeset for help on using the changeset viewer.