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