From c78b83c4cc9ff3e7bb0b3a07dc9e3139a7047ab5 Mon Sep 17 00:00:00 2001 From: Taco Verdonschot Date: Mon, 16 Nov 2026 16:52:12 +0100 Subject: [PATCH 1/2] Load language packs from TranslationsPress The plugin is no longer distributed through WordPress.org, so WordPress finds no language packs for it there. Offer the packs TranslationsPress builds for the plugin as translation updates instead. WordPress then downloads, installs and loads them as it did before. The list of packs is fetched only while WordPress stores the result of a plugin update check, and is cached for 12 hours (1 hour after a failure). Only packages hosted in our own TranslationsPress project are accepted. Co-Authored-By: Claude Fable 5.2 --- classes/class-base.php | 4 + classes/class-translations.php | 212 +++++++++++++ tests/phpunit/test-class-translations.php | 353 ++++++++++++++++++++++ 3 files changed, 569 insertions(+) create mode 100644 classes/class-translations.php create mode 100644 tests/phpunit/test-class-translations.php diff --git a/classes/class-base.php b/classes/class-base.php index 743da4d76..eb453fb7a 100644 --- a/classes/class-base.php +++ b/classes/class-base.php @@ -42,6 +42,7 @@ * @method \Progress_Planner\Utils\Deprecations get_utils__deprecations() * @method \Progress_Planner\UI\Branding get_ui__branding() * @method \Progress_Planner\Plugin_Installer get_plugin_installer() + * @method \Progress_Planner\Translations get_translations() * @method \Progress_Planner\Admin\Widgets\Badge_Streak_Content get_admin__widgets__badge_streak_content() * @method \Progress_Planner\Admin\Widgets\Badge_Streak_Maintenance get_admin__widgets__badge_streak_maintenance() * @method \Progress_Planner\Admin\Enqueue get_admin__enqueue() @@ -178,6 +179,9 @@ public function init() { // Plugin installer. $this->get_plugin_installer(); + // Language packs. + $this->get_translations(); + if ( \defined( 'WP_CLI' ) && \WP_CLI ) { $this->get_wp_cli__get_stats_command(); $this->get_wp_cli__task_command(); diff --git a/classes/class-translations.php b/classes/class-translations.php new file mode 100644 index 000000000..722619f94 --- /dev/null +++ b/classes/class-translations.php @@ -0,0 +1,212 @@ +get_locales(); + if ( empty( $locales ) ) { + return $transient; + } + + $packs = \array_intersect_key( $this->get_packs(), \array_flip( $locales ) ); + if ( empty( $packs ) ) { + return $transient; + } + + $translations = isset( $transient->translations ) && \is_array( $transient->translations ) + ? $transient->translations + : []; + + foreach ( $packs as $locale => $pack ) { + if ( ! $this->is_newer_than_installed( $locale, $pack['updated'] ) ) { + continue; + } + + // Drop any pack already listed for this plugin and locale, so the + // list never holds two packs for the same files. + $translations = \array_filter( + $translations, + static function ( $translation ) use ( $locale ) { + $translation = (array) $translation; + return ! ( isset( $translation['type'], $translation['slug'], $translation['language'] ) + && 'plugin' === $translation['type'] + && self::SLUG === $translation['slug'] + && $locale === $translation['language'] ); + } + ); + + $translations[] = [ + 'type' => 'plugin', + 'slug' => self::SLUG, + 'language' => $locale, + 'version' => $pack['version'], + 'updated' => $pack['updated'], + 'package' => $pack['package'], + 'autoupdate' => true, + ]; + } + + $transient->translations = \array_values( $translations ); + + return $transient; + } + + /** + * Get the locales to look up language packs for. + * + * These are the languages installed on the site, which is also what + * WordPress itself asks WordPress.org for. + * + * @return string[] + */ + protected function get_locales() { + $locales = \get_available_languages(); + $locales[] = \get_locale(); + + return \array_values( \array_diff( \array_unique( $locales ), [ 'en_US' ] ) ); + } + + /** + * Whether a language pack is newer than the installed translation. + * + * @param string $locale The locale. + * @param string $updated The date the language pack was last updated. + * + * @return bool + */ + protected function is_newer_than_installed( $locale, $updated ) { + $installed = $this->get_installed_date( $locale ); + if ( '' === $installed ) { + return true; + } + + $installed_time = \strtotime( $installed ); + $updated_time = \strtotime( $updated ); + + return false !== $updated_time && ( false === $installed_time || $updated_time > $installed_time ); + } + + /** + * Get the revision date of the installed translation for a locale. + * + * @param string $locale The locale. + * + * @return string The date, or an empty string when no translation is installed. + */ + protected function get_installed_date( $locale ) { + $installed = \wp_get_installed_translations( 'plugins' ); + + return isset( $installed[ self::SLUG ][ $locale ]['PO-Revision-Date'] ) + ? (string) $installed[ self::SLUG ][ $locale ]['PO-Revision-Date'] + : ''; + } + + /** + * Get the language packs TranslationsPress has for the plugin. + * + * @return array Packs, keyed by locale. + */ + public function get_packs() { + $cached = \get_site_transient( self::CACHE_KEY ); + if ( \is_array( $cached ) ) { + return $cached; + } + + $response = \wp_remote_get( self::PACKAGES_BASE_URL . 'packages.json' ); + + // Cache failures for a short time, so a broken connection is not retried on every update check. + if ( \is_wp_error( $response ) || 200 !== (int) \wp_remote_retrieve_response_code( $response ) ) { + \set_site_transient( self::CACHE_KEY, [], HOUR_IN_SECONDS ); + return []; + } + + $data = \json_decode( \wp_remote_retrieve_body( $response ), true ); + $packs = []; + + if ( \is_array( $data ) && isset( $data['translations'] ) && \is_array( $data['translations'] ) ) { + foreach ( $data['translations'] as $translation ) { + if ( ! \is_array( $translation ) + || ! isset( $translation['language'], $translation['version'], $translation['updated'], $translation['package'] ) + || ! \is_string( $translation['language'] ) + || ! \is_string( $translation['version'] ) + || ! \is_string( $translation['updated'] ) + || ! \is_string( $translation['package'] ) + || 1 !== \preg_match( '/^[a-z]{2,3}(_[A-Za-z0-9]+)*$/', $translation['language'] ) + // WordPress downloads and unpacks this file, so only accept our own project's packages. + || 0 !== \strpos( $translation['package'], self::PACKAGES_BASE_URL ) + ) { + continue; + } + + $packs[ $translation['language'] ] = [ + 'version' => $translation['version'], + 'updated' => $translation['updated'], + 'package' => $translation['package'], + ]; + } + } + + \set_site_transient( self::CACHE_KEY, $packs, 12 * HOUR_IN_SECONDS ); + + return $packs; + } +} diff --git a/tests/phpunit/test-class-translations.php b/tests/phpunit/test-class-translations.php new file mode 100644 index 000000000..e6e349667 --- /dev/null +++ b/tests/phpunit/test-class-translations.php @@ -0,0 +1,353 @@ +|\WP_Error + */ + private $response; + + /** + * Set up. + * + * @return void + */ + public function setUp(): void { + parent::setUp(); + $this->requests = 0; + $this->response = $this->feed_response( + [ + $this->pack( 'nl_NL' ), + $this->pack( 'de_DE' ), + ] + ); + \delete_site_transient( Translations::CACHE_KEY ); + \add_filter( 'pre_http_request', [ $this, 'mock_feed' ], 10, 3 ); + \add_filter( 'get_available_languages', [ $this, 'available_languages' ] ); + } + + /** + * Tear down. + * + * @return void + */ + public function tearDown(): void { + \remove_filter( 'pre_http_request', [ $this, 'mock_feed' ], 10 ); + \remove_filter( 'get_available_languages', [ $this, 'available_languages' ] ); + \delete_site_transient( Translations::CACHE_KEY ); + parent::tearDown(); + } + + /** + * Mock the feed request. + * + * @param mixed $preempt The preempted response. + * @param array $args The request arguments. + * @param string $url The request URL. + * + * @return mixed + */ + public function mock_feed( $preempt, $args, $url ) { + if ( self::FEED_URL !== $url ) { + return $preempt; + } + ++$this->requests; + return $this->response; + } + + /** + * Pretend Dutch is the only extra language installed on the site. + * + * @return string[] + */ + public function available_languages() { + return [ 'nl_NL' ]; + } + + /** + * Build a language pack entry as the feed returns it. + * + * @param string $locale The locale. + * @param string $package The package URL. Defaults to a valid one. + * + * @return array + */ + private function pack( $locale, $package = '' ) { + return [ + 'language' => $locale, + 'version' => '1.9.1', + 'updated' => '2026-02-20T09:28:05+00:00', + 'package' => '' !== $package ? $package : Translations::PACKAGES_BASE_URL . 'progress-planner-' . $locale . '.zip', + ]; + } + + /** + * Build a successful feed response. + * + * @param array $translations The language packs. + * + * @return array + */ + private function feed_response( $translations ) { + return [ + 'response' => [ 'code' => 200 ], + 'body' => \wp_json_encode( [ 'translations' => $translations ] ), + ]; + } + + /** + * Get an instance that reports the given installed translation date. + * + * @param string $installed_date The installed revision date, or '' for none. + * + * @return Translations + */ + private function instance( $installed_date = '' ) { + return new class( $installed_date ) extends Translations { + + /** + * The installed revision date. + * + * @var string + */ + private $installed_date; + + /** + * Constructor. Skips the hooks. + * + * @param string $installed_date The installed revision date. + */ + public function __construct( $installed_date ) { + $this->installed_date = $installed_date; + } + + /** + * Get the installed revision date. + * + * @param string $locale The locale. + * + * @return string + */ + protected function get_installed_date( $locale ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found + return $this->installed_date; + } + }; + } + + /** + * Get our translation updates from a transient value. + * + * @param object $transient The transient value. + * + * @return array> + */ + private function our_updates( $transient ) { + return \array_values( + \array_filter( + (array) ( $transient->translations ?? [] ), + static function ( $translation ) { + return Translations::SLUG === ( (array) $translation )['slug']; + } + ) + ); + } + + /** + * The plugin registers the filter when it boots. + * + * @return void + */ + public function test_filter_is_registered() { + $this->assertNotFalse( + \has_filter( 'pre_set_site_transient_update_plugins', [ \progress_planner()->get_translations(), 'add_translation_updates' ] ) + ); + } + + /** + * A language pack is offered for an installed language that has no translation yet. + * + * @return void + */ + public function test_offers_pack_for_installed_language() { + $updates = $this->our_updates( $this->instance()->add_translation_updates( new \stdClass() ) ); + + $this->assertCount( 1, $updates, 'Only the installed language gets a pack; de_DE is not installed.' ); + $this->assertSame( + [ + 'type' => 'plugin', + 'slug' => 'progress-planner', + 'language' => 'nl_NL', + 'version' => '1.9.1', + 'updated' => '2026-02-20T09:28:05+00:00', + 'package' => Translations::PACKAGES_BASE_URL . 'progress-planner-nl_NL.zip', + 'autoupdate' => true, + ], + $updates[0] + ); + } + + /** + * A pack is only offered when it is newer than the installed translation. + * + * @dataProvider installed_dates + * + * @param string $installed_date The installed revision date. + * @param int $expected The expected number of updates. + * + * @return void + */ + public function test_compares_with_installed_translation( $installed_date, $expected ) { + $this->assertCount( + $expected, + $this->our_updates( $this->instance( $installed_date )->add_translation_updates( new \stdClass() ) ) + ); + } + + /** + * Installed revision dates, in the format of a PO file header. + * + * @return array + */ + public function installed_dates() { + return [ + 'older' => [ '2025-11-01 10:00:00+0000', 1 ], + 'same' => [ '2026-02-20 09:28:05+0000', 0 ], + 'newer' => [ '2026-03-01 10:00:00+0000', 0 ], + 'unparsable' => [ 'not a date', 1 ], + ]; + } + + /** + * Other translation updates are kept, and running twice adds no duplicate. + * + * @return void + */ + public function test_keeps_other_updates_and_replaces_its_own() { + $transient = new \stdClass(); + $transient->translations = [ + [ + 'type' => 'plugin', + 'slug' => 'akismet', + 'language' => 'nl_NL', + 'package' => 'https://downloads.wordpress.org/translation/plugin/akismet/5.0/nl_NL.zip', + ], + [ + 'type' => 'plugin', + 'slug' => 'progress-planner', + 'language' => 'nl_NL', + 'package' => 'https://downloads.wordpress.org/translation/plugin/progress-planner/1.8.0/nl_NL.zip', + ], + ]; + + $instance = $this->instance(); + $transient = $instance->add_translation_updates( $instance->add_translation_updates( $transient ) ); + + $this->assertCount( 2, $transient->translations ); + $this->assertSame( 'akismet', $transient->translations[0]['slug'] ); + + $ours = $this->our_updates( $transient ); + $this->assertCount( 1, $ours ); + $this->assertSame( Translations::PACKAGES_BASE_URL . 'progress-planner-nl_NL.zip', $ours[0]['package'] ); + } + + /** + * Packs hosted anywhere but our TranslationsPress project are ignored. + * + * @return void + */ + public function test_ignores_packages_from_other_hosts() { + $this->response = $this->feed_response( + [ + $this->pack( 'nl_NL', 'https://example.org/progress-planner-nl_NL.zip' ), + 'not an array', + [ 'language' => 'nl_NL' ], + ] + ); + + $this->assertSame( [], $this->instance()->get_packs() ); + $this->assertSame( [], $this->our_updates( $this->instance()->add_translation_updates( new \stdClass() ) ) ); + } + + /** + * The feed is cached, and so is a failure. + * + * @dataProvider responses + * + * @param string $type The response type. + * @param int $expected The expected number of packs. + * + * @return void + */ + public function test_feed_is_cached( $type, $expected ) { + if ( 'transport' === $type ) { + $this->response = new \WP_Error( 'http_request_failed', 'Connection timed out.' ); + } elseif ( 'http' === $type ) { + $this->response = [ + 'response' => [ 'code' => 503 ], + 'body' => 'Service unavailable', + ]; + } elseif ( 'garbage' === $type ) { + $this->response = [ + 'response' => [ 'code' => 200 ], + 'body' => '', + ]; + } + + $instance = $this->instance(); + $this->assertCount( $expected, $instance->get_packs() ); + $this->assertCount( $expected, $instance->get_packs() ); + $this->assertSame( 1, $this->requests ); + } + + /** + * Feed response types. + * + * @return array + */ + public function responses() { + return [ + 'success' => [ 'success', 2 ], + 'transport error' => [ 'transport', 0 ], + 'http error' => [ 'http', 0 ], + 'invalid body' => [ 'garbage', 0 ], + ]; + } + + /** + * A value that is not update data is returned untouched, without a request. + * + * @return void + */ + public function test_ignores_non_object_transient() { + $this->assertFalse( $this->instance()->add_translation_updates( false ) ); + $this->assertSame( 0, $this->requests ); + } +} From 842386a8b8a2d30ff311ef978318450662593e7b Mon Sep 17 00:00:00 2001 From: Filip Ilic Date: Tue, 17 Nov 2026 10:28:25 +0100 Subject: [PATCH 2/2] Ignore the WordPress.org updater codes in Plugin Check The TranslationsPress language packs hook pre_set_site_transient_update_plugins, which Plugin Check reports as plugin_updater_detected and update_modification_detected. That rule is for plugins hosted on WordPress.org, and this plugin was closed there on 2026-10-26. Ignore only those two codes so the rest of the check keeps running. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/plugin-check.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/plugin-check.yml b/.github/workflows/plugin-check.yml index 665629ce8..4d6cc3916 100644 --- a/.github/workflows/plugin-check.yml +++ b/.github/workflows/plugin-check.yml @@ -56,3 +56,13 @@ jobs: build-dir: './build/${{ github.event.repository.name }}' exclude-checks: | direct_file_access + # classes/class-translations.php adds TranslationsPress language + # packs through pre_set_site_transient_update_plugins, which these + # two codes flag. The rule only applies to plugins hosted on + # WordPress.org, and this plugin was closed there on 2026-10-26. + # Only these codes are ignored, so the rest of the plugin_updater + # check still runs. Remove both if the plugin returns to + # WordPress.org, together with that class. + ignore-codes: | + plugin_updater_detected + update_modification_detected