Changeset 2881
- Timestamp:
- 04/02/2016 03:59:41 PM (9 years ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory
- Files:
-
- 4 added
- 3 deleted
- 1 edited
- 6 copied
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cache-purge/class-cache-purger.php
r2880 r2881 1 1 <?php 2 /**3 * Plugin name: GlotPress: Plugin Directory Bridge4 * 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.16 * Author: WordPress.org7 * Author URI: http://wordpress.org/8 * License: GPLv2 or later9 */10 2 11 class WPorg_GP_Plugin_Directory { 3 namespace WordPressdotorg\GlotPress\Plugin_Directory\Cache_Purge; 4 5 use GP; 6 7 class Cache_Purger { 12 8 public $master_project = 'wp-plugins'; 13 9 public $i18n_cache_group = 'plugins-i18n'; … … 15 11 private $translation_edits_queue = array(); 16 12 17 public function __construct() { 13 /** 14 * Registers actions and filters. 15 */ 16 public function register_events() { 18 17 add_action( 'init', array( $this, 'add_global_cache_group' ) ); 19 18 add_action( 'gp_originals_imported', array( $this, 'originals_imported' ) ); … … 23 22 // Cache purging is delayed until shutdown to prevent multiple purges for the same project. 24 23 add_action( 'shutdown', array( $this, 'delete_plugin_i18n_cache_on_translation_edits' ) ); 25 26 if ( defined( 'WP_CLI' ) && WP_CLI ) {27 $this->register_cli_commands();28 }29 }30 31 /**32 * Registers CLI commands if WP-CLI is loaded.33 */34 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 24 } 43 25 … … 67 49 * or updated. 68 50 * 69 * @param GP_Translation $translation Created/updated translation.51 * @param \GP_Translation $translation Created/updated translation. 70 52 */ 71 53 public function queue_translation_for_cache_purge( $translation ) { … … 79 61 /** 80 62 * Deletes the cache on a translation edits. 81 *82 * @param GP_Translation $translation The edited translation.83 63 */ 84 64 public function delete_plugin_i18n_cache_on_translation_edits() { … … 139 119 private function get_plugin_i18n_cache_prefix( $project_id ) { 140 120 $project = GP::$project->get( $project_id ); 141 if ( empty( $project->path ) || ! $this->project_is_plugin( $project->path ) ) {121 if ( empty( $project->path ) || ! $this->project_is_plugin( $project->path ) ) { 142 122 return ''; 143 123 } … … 204 184 } 205 185 } 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' ); -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/class-plugin.php
r2880 r2881 1 1 <?php 2 /**3 * Plugin name: GlotPress: Plugin Directory Bridge4 * 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.16 * Author: WordPress.org7 * Author URI: http://wordpress.org/8 * License: GPLv2 or later9 */10 2 11 class WPorg_GP_Plugin_Directory { 12 public $master_project = 'wp-plugins'; 13 public $i18n_cache_group = 'plugins-i18n'; 3 namespace WordPressdotorg\GlotPress\Plugin_Directory; 14 4 15 private $translation_edits_queue = array();5 use WP_CLI; 16 6 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' ) ); 7 class Plugin { 22 8 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(); 25 39 26 40 if ( defined( 'WP_CLI' ) && WP_CLI ) { … … 33 47 */ 34 48 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' ); 204 52 } 205 53 } 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' ); -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-delete-plugin-project.php
r2858 r2881 1 1 <?php 2 2 3 class WPorg_GP_CLI_Delete_Plugin_Project extends WP_CLI_Command { 3 namespace WordPressdotorg\GlotPress\Plugin_Directory\CLI; 4 5 use GP; 6 use WP_CLI; 7 use WP_CLI_Command; 8 9 class Delete_Plugin_Project extends WP_CLI_Command { 4 10 5 11 /** -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-import-plugin-translations.php
r2858 r2881 1 1 <?php 2 2 3 class WPorg_GP_CLI_Import_Plugin_Translations extends WP_CLI_Command { 3 namespace WordPressdotorg\GlotPress\Plugin_Directory\CLI; 4 5 use GP; 6 use GP_Locales; 7 use WP_CLI; 8 use WP_CLI_Command; 9 10 class Import_Plugin_Translations extends WP_CLI_Command { 4 11 5 12 /** -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-set-plugin-project.php
r2858 r2881 1 1 <?php 2 2 3 class WPorg_GP_CLI_Set_Plugin_Project extends WP_CLI_Command { 3 namespace WordPressdotorg\GlotPress\Plugin_Directory\CLI; 4 5 use GP; 6 use WP_CLI; 7 use WP_CLI_Command; 8 9 class Set_Plugin_Project extends WP_CLI_Command { 4 10 5 11 /** -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/vendor/wordpressdotorg/class-autoloader.php
r2858 r2881 1 1 <?php 2 namespace WordPressdotorg\ Plugin_Directory\Autoloader;2 namespace WordPressdotorg\Autoload; 3 3 4 4 /** 5 5 * An Autoloader which respects WordPress's filename standards. 6 6 * 7 * @package WordPressdotorg\ Plugin_Directory\Autoloader7 * @package WordPressdotorg\Autoload 8 8 */ 9 9 class Autoloader { -
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/wporg-gp-plugin-directory.php
r2880 r2881 3 3 * Plugin name: GlotPress: Plugin Directory Bridge 4 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.15 * Version: 2.0 6 6 * Author: WordPress.org 7 7 * Author URI: http://wordpress.org/ … … 9 9 */ 10 10 11 class WPorg_GP_Plugin_Directory { 12 public $master_project = 'wp-plugins'; 13 public $i18n_cache_group = 'plugins-i18n'; 11 namespace WordPressdotorg\GlotPress\Plugin_Directory; 14 12 15 private $translation_edits_queue = array();13 use WordPressdotorg\Autoload; 16 14 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' ) ); 15 // Store the root plugin file for usage with functions which use the plugin basename. 16 define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ ); 22 17 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' ) ); 25 26 if ( defined( 'WP_CLI' ) && WP_CLI ) { 27 $this->register_cli_commands(); 28 } 29 } 30 31 /** 32 * Registers CLI commands if WP-CLI is loaded. 33 */ 34 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 ); 204 } 18 if ( ! class_exists( '\WordPressdotorg\Autoload\Autoloader', false ) ) { 19 include __DIR__ . '/vendor/wordpressdotorg/class-autoloader.php'; 205 20 } 206 21 207 function wporg_gp_plugin_directory() { 208 global $wporg_gp_plugin_directory;22 // Register an Autoloader for all files. 23 Autoload\register_class_path( __NAMESPACE__, __DIR__ . '/inc' ); 209 24 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' ); 25 // Instantiate the Plugin. 26 Plugin::get_instance();
Note: See TracChangeset
for help on using the changeset viewer.