Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/plugin-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down
212 changes: 212 additions & 0 deletions classes/class-translations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* Language packs from TranslationsPress.
*
* The plugin is no longer distributed through WordPress.org, so WordPress
* cannot find language packs for it there. This class tells WordPress about
* the language packs TranslationsPress builds for the plugin. WordPress then
* downloads, installs and loads them in the same way as WordPress.org packs.
*
* @package Progress_Planner
*/

namespace Progress_Planner;

/**
* Translations class.
*/
class Translations {

/**
* The plugin slug, which is also the text domain.
*
* @var string
*/
const SLUG = 'progress-planner';

/**
* The base URL of the project on TranslationsPress.
*
* Language packs are only accepted when their URL starts with this.
*
* @var string
*/
const PACKAGES_BASE_URL = 'https://packages.translationspress.com/progress-planner/progress-planner/';

/**
* The site transient that caches the list of language packs.
*
* @var string
*/
const CACHE_KEY = 'progress_planner_translation_packs';

/**
* Constructor.
*/
public function __construct() {
// This filter runs when WordPress stores the result of a plugin update
// check. The remote request below therefore only happens in a context
// that already makes remote requests, never on a normal page load.
\add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'add_translation_updates' ] );
}

/**
* Add our language packs to the plugin update data.
*
* @param mixed $transient The value of the `update_plugins` site transient.
*
* @return mixed
*/
public function add_translation_updates( $transient ) {
if ( ! $transient instanceof \stdClass ) {
return $transient;
}

$locales = $this->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<string, array{version: string, updated: string, package: string}> 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;
}
}
Loading
Loading