|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WooCommerce Yoast SEO plugin file. |
| 4 | + * |
| 5 | + * @package WPSEO/WooCommerce |
| 6 | + */ |
| 7 | + |
| 8 | +use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 9 | +use Yoast\WP\SEO\Helpers\Date_Helper; |
| 10 | +use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Allows to download translations from TranslationsPress. |
| 14 | + */ |
| 15 | +class Yoast_WooCommerce_TranslationsPress implements Integration_Interface { |
| 16 | + |
| 17 | + use No_Conditionals; |
| 18 | + |
| 19 | + /** |
| 20 | + * The plugin slug to retrieve the translations for. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $slug = 'yoast-woo-seo'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The key of the custom transient where to store the translations info. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $transient_key; |
| 32 | + |
| 33 | + /** |
| 34 | + * The URL for the TranslationsPress API service. |
| 35 | + * |
| 36 | + * @var string |
| 37 | + */ |
| 38 | + protected $api_url; |
| 39 | + |
| 40 | + /** |
| 41 | + * The array to cache our addition to the `site_transient_update_plugins` filter. |
| 42 | + * |
| 43 | + * @var array|null |
| 44 | + */ |
| 45 | + protected $cached_translations; |
| 46 | + |
| 47 | + /** |
| 48 | + * The Date helper object. |
| 49 | + * |
| 50 | + * @var Date_Helper |
| 51 | + */ |
| 52 | + protected $date_helper; |
| 53 | + |
| 54 | + /** |
| 55 | + * Adds a new project to load translations for. |
| 56 | + * |
| 57 | + * @param Date_Helper $date_helper The Date Helper object. |
| 58 | + */ |
| 59 | + public function __construct( Date_Helper $date_helper ) { |
| 60 | + $this->transient_key = 'yoast_translations_' . $this->slug; |
| 61 | + $this->api_url = 'https://packages.translationspress.com/yoast/' . $this->slug . '/packages.json'; |
| 62 | + $this->date_helper = $date_helper; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Initializes the integration. |
| 67 | + * |
| 68 | + * This is the place to register hooks and filters. |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function register_hooks() { |
| 73 | + add_action( 'init', [ $this, 'register_clean_translations_cache' ], PHP_INT_MAX ); |
| 74 | + add_filter( 'translations_api', [ $this, 'translations_api' ], 10, 3 ); |
| 75 | + add_filter( 'site_transient_update_plugins', [ $this, 'site_transient_update_plugins' ] ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Short-circuits translations API requests for private projects. |
| 80 | + * |
| 81 | + * @param bool|array $result The result array. Default false. |
| 82 | + * @param string $requested_type The type of translations being requested. |
| 83 | + * @param object $args Translation API arguments. |
| 84 | + * |
| 85 | + * @return bool|array The translations array. False by default. |
| 86 | + */ |
| 87 | + public function translations_api( $result, $requested_type, $args ) { |
| 88 | + if ( $requested_type === 'plugins' && $args['slug'] === $this->slug ) { |
| 89 | + return $this->get_translations(); |
| 90 | + } |
| 91 | + |
| 92 | + return $result; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Filters the translations transients to include the private plugin or theme. |
| 97 | + * Caches our own return value to prevent heavy overhead. |
| 98 | + * |
| 99 | + * @param bool|object $value The transient value. |
| 100 | + * |
| 101 | + * @return object The filtered transient value. |
| 102 | + */ |
| 103 | + public function site_transient_update_plugins( $value ) { |
| 104 | + if ( ! is_object( $value ) ) { |
| 105 | + $value = new stdClass(); |
| 106 | + } |
| 107 | + |
| 108 | + if ( ! isset( $value->translations ) ) { |
| 109 | + $value->translations = []; |
| 110 | + } |
| 111 | + |
| 112 | + if ( is_array( $this->cached_translations ) ) { |
| 113 | + $value->translations = array_merge( $value->translations, $this->cached_translations ); |
| 114 | + return $value; |
| 115 | + } |
| 116 | + |
| 117 | + $this->cached_translations = []; |
| 118 | + |
| 119 | + $translations = $this->get_translations(); |
| 120 | + if ( empty( $translations[ $this->slug ]['translations'] ) ) { |
| 121 | + return $value; |
| 122 | + } |
| 123 | + |
| 124 | + // The following call is the reason we need to cache the results of this method. |
| 125 | + $installed_translations = wp_get_installed_translations( 'plugins' ); |
| 126 | + $available_languages = get_available_languages(); |
| 127 | + foreach ( $translations[ $this->slug ]['translations'] as $translation ) { |
| 128 | + if ( ! in_array( $translation['language'], $available_languages, true ) ) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + if ( isset( $installed_translations[ $this->slug ][ $translation['language'] ] ) && $translation['updated'] ) { |
| 133 | + $local = new DateTime( $installed_translations[ $this->slug ][ $translation['language'] ]['PO-Revision-Date'] ); |
| 134 | + $remote = new DateTime( $translation['updated'] ); |
| 135 | + |
| 136 | + if ( $local >= $remote ) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + $translation['type'] = 'plugin'; |
| 142 | + $translation['slug'] = $this->slug; |
| 143 | + $translation['autoupdate'] = true; |
| 144 | + $value->translations[] = $translation; |
| 145 | + $this->cached_translations[] = $translation; |
| 146 | + } |
| 147 | + |
| 148 | + return $value; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Registers actions for clearing translation caches. |
| 153 | + * |
| 154 | + * @return void |
| 155 | + */ |
| 156 | + public function register_clean_translations_cache() { |
| 157 | + add_action( 'set_site_transient_update_plugins', [ $this, 'clean_translations_cache' ] ); |
| 158 | + add_action( 'delete_site_transient_update_plugins', [ $this, 'clean_translations_cache' ] ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Clears existing translation cache. |
| 163 | + * |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + public function clean_translations_cache() { |
| 167 | + $translations = get_site_transient( $this->transient_key ); |
| 168 | + if ( ! is_array( $translations ) ) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + $cache_lifespan = DAY_IN_SECONDS; |
| 173 | + $time_not_changed = isset( $translations['_last_checked'] ) && ( $this->date_helper->current_time() - $translations['_last_checked'] ) > $cache_lifespan; |
| 174 | + |
| 175 | + if ( ! $time_not_changed ) { |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + delete_site_transient( $this->transient_key ); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Gets the translations for a given project. |
| 184 | + * |
| 185 | + * @return array The translation data. |
| 186 | + */ |
| 187 | + public function get_translations() { |
| 188 | + $translations = get_site_transient( $this->transient_key ); |
| 189 | + if ( $translations !== false && is_array( $translations ) ) { |
| 190 | + return $translations; |
| 191 | + } |
| 192 | + |
| 193 | + $translations = []; |
| 194 | + |
| 195 | + $result = json_decode( wp_remote_retrieve_body( wp_remote_get( $this->api_url ) ), true ); |
| 196 | + |
| 197 | + // Nothing found. |
| 198 | + if ( ! is_array( $result ) ) { |
| 199 | + $result = []; |
| 200 | + } |
| 201 | + |
| 202 | + $translations[ $this->slug ] = $result; |
| 203 | + $translations['_last_checked'] = $this->date_helper->current_time(); |
| 204 | + |
| 205 | + set_site_transient( $this->transient_key, $translations ); |
| 206 | + |
| 207 | + return $translations; |
| 208 | + } |
| 209 | +} |
0 commit comments