Changeset 2266
- Timestamp:
- 01/10/2016 06:52:35 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/translate.wordpress.org/includes/gp-plugins/wporg-plugins.php
r1160 r2266 6 6 */ 7 7 class 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'; 10 10 11 function __construct() {11 public function __construct() { 12 12 parent::__construct(); 13 13 $this->add_action( 'init' ); 14 14 $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' ); 16 18 } 17 19 … … 19 21 * Making sure to register the global cache group wordpress.org/plugins/ uses for its display cache. 20 22 */ 21 function init() {23 public function init() { 22 24 wp_cache_add_global_groups( $this->i18n_cache_group ); 23 25 } … … 26 28 * @param $project_id 27 29 */ 28 function originals_imported( $project_id ) {30 public function originals_imported( $project_id ) { 29 31 $project = GP::$project->get( $project_id ); 30 32 if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) { 31 33 return; 32 34 } 35 33 36 $this->delete_plugin_i18n_cache_keys_for_project( $project_id ); 34 37 } 35 38 36 39 /** 37 * @param $action 40 * Triggers a cache purge when a new translation was created. 41 * 42 * @param GP_Translation $translation Created translation. 38 43 */ 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'] ) ) { 41 46 return; 42 47 } 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; 49 60 } 61 62 $this->delete_plugin_i18n_cache_on_translation_edit( $translation ); 50 63 } 51 64 … … 55 68 * @return bool 56 69 */ 57 function project_is_plugin( $path ) {70 private function project_is_plugin( $path ) { 58 71 if ( empty( $path ) ) { 59 72 return false; 60 73 } 74 61 75 $path = '/' . trim( $path, '/' ) . '/'; 62 76 if ( false === strpos( $path, "/{$this->master_project}/" ) ) { 63 77 return false; 64 78 } 79 65 80 return true; 66 81 } … … 71 86 * @return string 72 87 */ 73 function get_plugin_i18n_cache_prefix( $project_id ) {88 private function get_plugin_i18n_cache_prefix( $project_id ) { 74 89 $project = GP::$project->get( $project_id ); 75 90 if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) { 76 91 return ''; 77 92 } 93 78 94 $project_dirs = explode( '/', trim( $project->path, '/' ) ); 79 95 if ( empty( $project_dirs ) || 3 !== count( $project_dirs ) || $project_dirs[0] !== $this->master_project ) { 80 96 return ''; 81 97 } 98 82 99 return "{$this->master_project}:{$project_dirs[1]}:{$project_dirs[2]}"; 83 100 } … … 89 106 * @param $set string Set, such as 'original', 'fr', 'de'. 90 107 */ 91 function delete_plugin_i18n_cache_keys_for( $prefix, $set ) {108 private function delete_plugin_i18n_cache_keys_for( $prefix, $set ) { 92 109 $suffixes = array( 93 110 'translation_set_id', 'title', 'short_description', 'installation', 'description', … … 96 113 foreach ( $suffixes as $suffix ) { 97 114 $cache_key = "{$prefix}:{$set}:{$suffix}"; 98 // error_log( serialize( array( $cache_key, $this->i18n_cache_group ) ) );99 115 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 ) );104 116 } 105 117 } … … 108 120 * @param $project_id int 109 121 */ 110 function delete_plugin_i18n_cache_keys_for_project( $project_id ) {122 private function delete_plugin_i18n_cache_keys_for_project( $project_id ) { 111 123 $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id ); 112 if ( empty( $prefix )) {124 if ( ! $prefix ) { 113 125 return; 114 126 } 127 115 128 wp_cache_delete( "{$prefix}:originals", $this->i18n_cache_group ); 116 129 wp_cache_delete( "{$prefix}:branch_id", $this->i18n_cache_group ); 117 130 $this->delete_plugin_i18n_cache_keys_for( $prefix, 'original' ); 131 118 132 $translation_sets = (array) GP::$translation_set->by_project_id( $project_id ); 119 133 foreach ( $translation_sets as $translation_set ) { … … 126 140 * @param $locale string 127 141 */ 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 ) { 129 143 $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id ); 130 if ( empty( $prefix )) {144 if ( ! $prefix ) { 131 145 return; 132 146 } 147 133 148 $this->delete_plugin_i18n_cache_keys_for( $prefix, $locale ); 134 149 } 135 150 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 ) { 138 159 return; 139 160 } 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 ) { 142 164 return; 143 165 } 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 ); 153 168 } 154 169 }
Note: See TracChangeset
for help on using the changeset viewer.