Making WordPress.org


Ignore:
Timestamp:
04/02/2016 03:59:41 PM (8 years ago)
Author:
ocean90
Message:

Translate, Plugin Directory: Switch to using namespaces and an autoloader.

The autoloader is copied from the Plugin Directory plugin: https://meta.trac.wordpress.org/browser/sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-autoloader.php

See #1636.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc
Files:
1 added
1 copied

Legend:

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

    r2880 r2881  
    11<?php
    2 /**
    3  * Plugin name: GlotPress: Plugin Directory Bridge
    4  * 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.1
    6  * Author:      WordPress.org
    7  * Author URI:  http://wordpress.org/
    8  * License:     GPLv2 or later
    9  */
    102
    11 class WPorg_GP_Plugin_Directory {
    12     public $master_project   = 'wp-plugins';
    13     public $i18n_cache_group = 'plugins-i18n';
     3namespace WordPressdotorg\GlotPress\Plugin_Directory;
    144
    15     private $translation_edits_queue = array();
     5use WP_CLI;
    166
    17     public function __construct() {
    18         add_action( 'init', array( $this, 'add_global_cache_group' ) );
    19         add_action( 'gp_originals_imported', array( $this, 'originals_imported' ) );
    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' ) );
     7class Plugin {
    228
    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' ) );
     9    /**
     10     * @var Plugin The singleton instance.
     11     */
     12    private static $instance;
     13
     14    /**
     15     * Returns always the same instance of this plugin.
     16     *
     17     * @return Plugin
     18     */
     19    public static function get_instance() {
     20        if ( ! ( self::$instance instanceof Plugin ) ) {
     21            self::$instance = new Plugin();
     22        }
     23        return self::$instance;
     24    }
     25
     26    /**
     27     * Instantiates a new Plugin object.
     28     */
     29    private function __construct() {
     30        add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
     31    }
     32
     33    /**
     34     * Initializes the plugin.
     35     */
     36    public function plugins_loaded() {
     37        $cache_purger = new Cache_Purge\Cache_Purger();
     38        $cache_purger->register_events();
    2539
    2640        if ( defined( 'WP_CLI' ) && WP_CLI ) {
     
    3347     */
    3448    function register_cli_commands() {
    35         require_once __DIR__ . '/cli/import-plugin-translations.php';
    36         require_once __DIR__ . '/cli/set-plugin-project.php';
    37         require_once __DIR__ . '/cli/delete-plugin-project.php';
    38 
    39         WP_CLI::add_command( 'wporg-translate import-plugin-translations', 'WPorg_GP_CLI_Import_Plugin_Translations' );
    40         WP_CLI::add_command( 'wporg-translate set-plugin-project', 'WPorg_GP_CLI_Set_Plugin_Project' );
    41         WP_CLI::add_command( 'wporg-translate delete-plugin-project', 'WPorg_GP_CLI_Delete_Plugin_Project' );
    42     }
    43 
    44     /**
    45      * Registers the global cache group wordpress.org/plugins/ uses for its display cache.
    46      */
    47     public function add_global_cache_group() {
    48         wp_cache_add_global_groups( $this->i18n_cache_group );
    49     }
    50 
    51     /**
    52      * Triggers a cache purge when a new originals were imported.
    53      *
    54      * @param int $project_id The project ID.
    55      */
    56     public function originals_imported( $project_id ) {
    57         $project = GP::$project->get( $project_id );
    58         if ( empty( $project->path ) || ! $this->project_is_plugin( $project->path ) ) {
    59             return;
    60         }
    61 
    62         $this->delete_plugin_i18n_cache_keys_for_project( $project_id );
    63     }
    64 
    65     /**
    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 ) {
    72         if ( ! $this->project_is_plugin( $_SERVER['REQUEST_URI'] ) ) {
    73             return;
    74         }
    75 
    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         }
    110     }
    111 
    112     /**
    113      * Returns whether a project path belongs to the plugins project.
    114      *
    115      * @param string $path Path of a project.
    116      *
    117      * @return bool True if it's a plugin, false if not.
    118      */
    119     private function project_is_plugin( $path ) {
    120         if ( empty( $path ) ) {
    121             return false;
    122         }
    123 
    124         $path = '/' . trim( $path, '/' ) . '/';
    125         if ( false === strpos( $path, "/{$this->master_project}/" ) ) {
    126             return false;
    127         }
    128 
    129         return true;
    130     }
    131 
    132     /**
    133      * Builds the cache prefix for a plugin.
    134      *
    135      * @param int $project_id The project ID.
    136      *
    137      * @return string The cache prefix, empty if project isn't a plugin.
    138      */
    139     private function get_plugin_i18n_cache_prefix( $project_id ) {
    140         $project = GP::$project->get( $project_id );
    141         if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) {
    142             return '';
    143         }
    144 
    145         $project_dirs = explode( '/', trim( $project->path, '/' ) );
    146         if ( empty( $project_dirs ) || 3 !== count( $project_dirs ) || $project_dirs[0] !== $this->master_project ) {
    147             return '';
    148         }
    149 
    150         return "{$this->master_project}:{$project_dirs[1]}:{$project_dirs[2]}";
    151     }
    152 
    153     /**
    154      * Deletes a set of known cache keys for a plugin.
    155      *
    156      * @param string $prefix Cache key prefix, such as 'plugin:livejournal-importer:readme-stable'.
    157      * @param string $set    Set, such as 'original', 'fr', 'de'.
    158      */
    159     private function delete_plugin_i18n_cache_keys_for( $prefix, $set ) {
    160         $suffixes = array(
    161             'translation_set_id', 'title', 'short_description', 'installation', 'description',
    162             'faq', 'screenshots', 'changelog', 'other_notes',
    163         );
    164         foreach ( $suffixes as $suffix ) {
    165             $cache_key = "{$prefix}:{$set}:{$suffix}";
    166             wp_cache_delete( $cache_key, $this->i18n_cache_group );
    167         }
    168     }
    169 
    170     /**
    171      * Deletes the cached originals of a plugin.
    172      *
    173      * @param int $project_id The project ID.
    174      */
    175     private function delete_plugin_i18n_cache_keys_for_project( $project_id ) {
    176         $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id );
    177         if ( ! $prefix ) {
    178             return;
    179         }
    180 
    181         wp_cache_delete( "{$prefix}:originals", $this->i18n_cache_group );
    182         wp_cache_delete( "{$prefix}:branch_id", $this->i18n_cache_group );
    183         $this->delete_plugin_i18n_cache_keys_for( $prefix, 'original' );
    184 
    185         $translation_sets = (array) GP::$translation_set->by_project_id( $project_id );
    186         foreach ( $translation_sets as $translation_set ) {
    187             $this->delete_plugin_i18n_cache_keys_for( $prefix, $translation_set->locale );
    188         }
    189     }
    190 
    191     /**
    192      * Deletes a cache keys of a locale for a plugin.
    193      *
    194      * @param int    $project_id The project ID.
    195      * @param string $locale     GlotPress slug of a locale.
    196      */
    197     private function delete_plugin_i18n_cache_keys_for_locale( $project_id, $locale ) {
    198         $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id );
    199         if ( ! $prefix ) {
    200             return;
    201         }
    202 
    203         $this->delete_plugin_i18n_cache_keys_for( $prefix, $locale );
     49        WP_CLI::add_command( 'wporg-translate import-plugin-translations', __NAMESPACE__ . '\CLI\Import_Plugin_Translations' );
     50        WP_CLI::add_command( 'wporg-translate set-plugin-project', __NAMESPACE__ . '\CLI\Set_Plugin_Project' );
     51        WP_CLI::add_command( 'wporg-translate delete-plugin-project', __NAMESPACE__ . '\CLI\Delete_Plugin_Project' );
    20452    }
    20553}
    206 
    207 function wporg_gp_plugin_directory() {
    208     global $wporg_gp_plugin_directory;
    209 
    210     if ( ! isset( $wporg_gp_plugin_directory ) ) {
    211         $wporg_gp_plugin_directory = new WPorg_GP_Plugin_Directory();
    212     }
    213 
    214     return $wporg_gp_plugin_directory;
    215 }
    216 add_action( 'plugins_loaded', 'wporg_gp_plugin_directory' );
Note: See TracChangeset for help on using the changeset viewer.