| 1 | <?php |
| 2 | /** |
| 3 | * This plugin clears the content translation cache for plugins (readme/code) hosted on wp.org based on actions taken withing GlotPress. |
| 4 | * |
| 5 | * @author stephdau |
| 6 | */ |
| 7 | class GP_WPorg_Plugins extends GP_Plugin { |
| 8 | var $master_project = 'wp-plugins'; |
| 9 | var $i18n_cache_group = 'plugins-i18n'; |
| 10 | |
| 11 | function __construct() { |
| 12 | parent::__construct(); |
| 13 | $this->add_action( 'init' ); |
| 14 | $this->add_action( 'originals_imported' ); |
| 15 | $this->add_action( 'post_tmpl_load' ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Making sure to register the global cache group wordpress.org/plugins/ uses for its display cache. |
| 20 | */ |
| 21 | function init() { |
| 22 | wp_cache_add_global_groups( $this->i18n_cache_group ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @param $project_id |
| 27 | */ |
| 28 | function originals_imported( $project_id ) { |
| 29 | $project = GP::$project->get( $project_id ); |
| 30 | if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) |
| 31 | return; |
| 32 | $this->delete_plugin_i18n_cache_keys_for_project( $project_id ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @param $action |
| 37 | */ |
| 38 | function post_tmpl_load( $action ) { |
| 39 | if ( !$this->project_is_plugin( $_SERVER["REQUEST_URI"] ) ) |
| 40 | return; |
| 41 | switch( $action ) { |
| 42 | case 'translation-row': |
| 43 | if ( ! empty( $_POST['translation'] ) ) |
| 44 | $this->delete_plugin_i18n_cache_on_translation_edit(); |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param $path |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | function project_is_plugin( $path ) { |
| 55 | if ( empty( $path ) ) |
| 56 | return false; |
| 57 | $path = '/' . trim( $path, '/' ) . '/'; |
| 58 | if ( false === strpos( $path, "/{$this->master_project}/" ) ) |
| 59 | return false; |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param $project_id int |
| 65 | * |
| 66 | * @return string |
| 67 | */ |
| 68 | function get_plugin_i18n_cache_prefix( $project_id ) { |
| 69 | $project = GP::$project->get( $project_id ); |
| 70 | if ( empty( $project->path ) || !$this->project_is_plugin( $project->path ) ) |
| 71 | return ''; |
| 72 | $project_dirs = explode( '/', trim( $project->path, '/' ) ); |
| 73 | if ( empty( $project_dirs ) || 3 !== count( $project_dirs ) || $project_dirs[0] !== $this->master_project ) |
| 74 | return ''; |
| 75 | return "{$this->master_project}:{$project_dirs[1]}:{$project_dirs[2]}"; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Deletes a set of known cache keys for a plugin |
| 80 | * |
| 81 | * @param $prefix string Cache key prefix, such as 'plugin:livejournal-importer:readme-stable' |
| 82 | * @param $set string Set, such as 'original', 'fr', 'de'. |
| 83 | */ |
| 84 | function delete_plugin_i18n_cache_keys_for( $prefix, $set ) { |
| 85 | $suffixes = array( |
| 86 | 'translation_set_id', 'title', 'short_description', 'installation', 'description', |
| 87 | 'faq', 'screenshots', 'changelog', 'other_notes', |
| 88 | ); |
| 89 | foreach ( $suffixes as $suffix ) { |
| 90 | $cache_key = "{$prefix}:{$set}:{$suffix}"; |
| 91 | // error_log( serialize( array( $cache_key, $this->i18n_cache_group ) ) ); |
| 92 | wp_cache_delete( $cache_key, $this->i18n_cache_group ); |
| 93 | } |
| 94 | // Deal with fr also existing as fr_FR, etc. |
| 95 | if ( 2 === strlen( $set ) ) |
| 96 | $this->delete_plugin_i18n_cache_keys_for( $prefix, strtolower( $set ) . '_' . strtoupper( $set ) ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param $project_id int |
| 101 | */ |
| 102 | function delete_plugin_i18n_cache_keys_for_project( $project_id ) { |
| 103 | $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id ); |
| 104 | if ( empty( $prefix ) ) |
| 105 | return; |
| 106 | wp_cache_delete( "{$prefix}:originals", $this->i18n_cache_group ); |
| 107 | wp_cache_delete( "{$prefix}:branch_id", $this->i18n_cache_group ); |
| 108 | $this->delete_plugin_i18n_cache_keys_for( $prefix, 'original' ); |
| 109 | $translation_sets = (array) GP::$translation_set->by_project_id( $project_id ); |
| 110 | foreach ( $translation_sets as $translation_set ) { |
| 111 | $this->delete_plugin_i18n_cache_keys_for( $prefix, $translation_set->locale ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param $project_id int |
| 117 | * @param $locale string |
| 118 | */ |
| 119 | function delete_plugin_i18n_cache_keys_for_locale( $project_id, $locale ) { |
| 120 | $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id ); |
| 121 | if ( empty( $prefix ) ) |
| 122 | return; |
| 123 | $this->delete_plugin_i18n_cache_keys_for( $prefix, $locale ); |
| 124 | } |
| 125 | |
| 126 | function delete_plugin_i18n_cache_on_translation_edit() { |
| 127 | if ( empty( $_POST['translation'] ) ) |
| 128 | return; |
| 129 | $tmp = array_keys( (array) $_POST['translation'] ); |
| 130 | if ( empty( $tmp[0] ) || !is_numeric( $tmp[0] ) ) |
| 131 | return; |
| 132 | $original = GP::$original->get( (int) $tmp[0] ); |
| 133 | if ( empty( $original ) ) |
| 134 | return; |
| 135 | $tmp = explode( '/', $_SERVER[ 'REQUEST_URI' ] ); |
| 136 | if ( empty( $tmp ) || count( $tmp ) < 2 ) |
| 137 | return; |
| 138 | $this->delete_plugin_i18n_cache_keys_for_locale( $original->project_id, gp_sanitize_meta_key( $tmp[ count( $tmp ) - 2 ] ) ); |
| 139 | } |
| 140 | } |
| 141 | GP::$plugins->wporg_plugins = new GP_WPorg_Plugins; |