| 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 | 'all', '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 | } |
| 78 | |
| 79 | /** |
| 80 | * @param $project_id int |
| 81 | */ |
| 82 | function delete_plugin_i18n_cache_keys_for_project( $project_id ) { |
| 83 | $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id ); |
| 84 | if ( empty( $prefix ) ) |
| 85 | return; |
| 86 | wp_cache_delete( "{$prefix}:originals", $this->i18n_cache_group ); |
| 87 | $this->delete_plugin_i18n_cache_keys_for( $prefix, 'original' ); |
| 88 | $translation_sets = (array) GP::$translation_set->by_project_id( $project_id ); |
| 89 | foreach ( $translation_sets as $translation_set ) { |
| 90 | $this->delete_plugin_i18n_cache_keys_for( $prefix, $translation_set->locale ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param $project_id int |
| 96 | * @param $locale string |
| 97 | */ |
| 98 | function delete_plugin_i18n_cache_keys_for_locale( $project_id, $locale ) { |
| 99 | $prefix = $this->get_plugin_i18n_cache_prefix( (int) $project_id ); |
| 100 | if ( empty( $prefix ) ) |
| 101 | return; |
| 102 | $this->delete_plugin_i18n_cache_keys_for( $prefix, $locale ); |
| 103 | } |
| 104 | |
| 105 | function delete_plugin_i18n_cache_on_translation_edit() { |
| 106 | if ( empty( $_POST['translation'] ) ) |
| 107 | return; |
| 108 | $tmp = array_keys( (array) $_POST['translation'] ); |
| 109 | if ( empty( $tmp[0] ) || !is_numeric( $tmp[0] ) ) |
| 110 | return; |
| 111 | $original = GP::$original->get( (int) $tmp[0] ); |
| 112 | if ( empty( $original ) ) |
| 113 | return; |
| 114 | $tmp = explode( '/', $_SERVER[ 'REQUEST_URI' ] ); |
| 115 | if ( empty( $tmp ) || count( $tmp ) < 2 ) |
| 116 | return; |
| 117 | $this->delete_plugin_i18n_cache_keys_for_locale( $original->project_id, gp_sanitize_meta_key( $tmp[ count( $tmp ) - 2 ] ) ); |
| 118 | } |
| 119 | } |
| 120 | GP::$plugins->wporg_plugins = new GP_WPorg_Plugins; |