diff --git a/inc/Plugin.php b/inc/Plugin.php index 7da53c73..f16c989d 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -22,6 +22,7 @@ use WP_REST_Request; use WP_REST_Response; use WP_REST_Server; +use \Required\Traduttore\Project; /** * Class used to register main actions and filters. @@ -60,9 +61,18 @@ function ( GP_Translation $translation ) { if ( ! $project || ! $project->is_active() ) { return; } - $zip_provider = new ZipProvider( $translation_set ); - $zip_provider->schedule_generation(); + $last_modified = $translation_set->last_modified(); + if ( $last_modified ) { + $last_modified = new DateTime( $last_modified, new DateTimeZone( 'UTC' ) ); + } else { + $last_modified = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + } + + $zip_provider = new ZipProvider( $translation_set ); + if ( $last_modified > $zip_provider->get_last_build_time() ) { + $zip_provider->schedule_generation(); + } } ); @@ -83,9 +93,17 @@ function ( $project_id, $originals_added, $originals_existing, $originals_obsole /* @var GP_Translation_Set $translation_set */ foreach ( $translation_sets as $translation_set ) { - $zip_provider = new ZipProvider( $translation_set ); + $last_modified = $translation_set->last_modified(); + if ( $last_modified ) { + $last_modified = new DateTime( $last_modified, new DateTimeZone( 'UTC' ) ); + } else { + $last_modified = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + } - $zip_provider->schedule_generation(); + $zip_provider = new ZipProvider( $translation_set ); + if ( $last_modified > $zip_provider->get_last_build_time() ) { + $zip_provider->schedule_generation(); + } } }, 10, @@ -97,21 +115,45 @@ function ( $project_id, $originals_added, $originals_existing, $originals_obsole function( $translation_set_id ) { /* @var GP_Translation_Set $translation_set */ $translation_set = GP::$translation_set->get( $translation_set_id ); - $last_modified = $translation_set->last_modified(); - if ( $last_modified ) { - $last_modified = new DateTime( $last_modified, new DateTimeZone( 'UTC' ) ); - } else { - $last_modified = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); + $zip_provider = new ZipProvider( $translation_set ); + $zip_provider->generate_zip_file(); + } + ); + + add_filter( + 'gp_update_meta', + function( $meta_tuple ) { + $allowed_keys = [ + Project::VERSION_KEY, // '_traduttore_version'. + Project::TEXT_DOMAIN_KEY, // '_traduttore_text_domain'. + ]; + if ( ! in_array( $meta_tuple['meta_key'], $allowed_keys, true ) ) { + return $meta_tuple; } - $zip_provider = new ZipProvider( $translation_set ); + $project = ( new ProjectLocator( $meta_tuple['object_id'] ) )->get_project(); + if ( ! $project || ! $project->is_active() ) { + return $meta_tuple; + } - if ( $last_modified <= $zip_provider->get_last_build_time() ) { - return; + $current_value = gp_get_meta( $meta_tuple['object_type'], $meta_tuple['object_id'], $meta_tuple['meta_key'] ); + if ( $current_value === $meta_tuple['meta_value'] ) { + return $meta_tuple; } - $zip_provider->generate_zip_file(); + $translation_sets = (array) GP::$translation_set->by_project_id( $project->get_id() ); + /* @var GP_Translation_Set $translation_set */ + foreach ( $translation_sets as $translation_set ) { + if ( 0 === $translation_set->current_count() ) { + continue; + } + + $zip_provider = new ZipProvider( $translation_set ); + $zip_provider->schedule_generation(); + } + + return $meta_tuple; } ); diff --git a/inc/Project.php b/inc/Project.php index 1ee2528e..eaea85d9 100644 --- a/inc/Project.php +++ b/inc/Project.php @@ -98,7 +98,7 @@ class Project { * * @var string Text domain meta key. */ - protected const TEXT_DOMAIN_KEY = '_traduttore_text_domain'; + public const TEXT_DOMAIN_KEY = '_traduttore_text_domain'; /** * Last update time meta key. @@ -116,7 +116,7 @@ class Project { * * @var string Version number meta key. */ - protected const VERSION_KEY = '_traduttore_version'; + public const VERSION_KEY = '_traduttore_version'; /** * GlotPress project. diff --git a/inc/ZipProvider.php b/inc/ZipProvider.php index fbfce8ba..318fd907 100644 --- a/inc/ZipProvider.php +++ b/inc/ZipProvider.php @@ -43,7 +43,7 @@ class ZipProvider { protected const BUILD_TIME_KEY = '_traduttore_build_time'; /** - * The GlotPress translation set. + * The current GlotPress translation set. * * @since 2.0.0 * @@ -51,6 +51,24 @@ class ZipProvider { */ protected $translation_set; + /** + * The current GlotPress locale. + * + * @since 3.0.0 + * + * @var GP_Locale The locale. + */ + protected $locale; + + /** + * The current project. + * + * @since 3.0.0 + * + * @var Project The project. + */ + protected $project; + /** * ZipProvider constructor. * @@ -60,6 +78,8 @@ class ZipProvider { */ public function __construct( GP_Translation_Set $translation_set ) { $this->translation_set = $translation_set; + $this->locale = GP_Locales::by_slug( $this->translation_set->locale ); + $this->project = new Project( GP::$project->get( $this->translation_set->project_id ) ); } /** @@ -161,8 +181,9 @@ public function generate_zip_file() : bool { * @param string $file Path to the generated language pack. * @param string $url URL to the generated language pack. * @param GP_Translation_Set $translation_set Translation set the language pack is for. + * @param Project $project The translation set's project. */ - do_action( 'traduttore.zip_generated', $this->get_zip_path(), $this->get_zip_url(), $this->translation_set ); + do_action( 'traduttore.zip_generated', $this->get_zip_path(), $this->get_zip_url(), $this->translation_set, $this->project ); return true; } @@ -209,14 +230,22 @@ public function remove_zip_file() : bool { * @return string ZIP filename. */ protected function get_zip_filename() : string { - /* @var GP_Locale $locale */ - $locale = GP_Locales::by_slug( $this->translation_set->locale ); - $project = GP::$project->get( $this->translation_set->project_id ); + $slug = str_replace( '/', '-', $this->project->get_slug() ); + $version = $this->project->get_version(); + + if ( $version ) { + return sprintf( + '%1$s-%2$s-%3$s.zip', + $slug, + $this->locale->wp_locale, + $version + ); + } return sprintf( '%1$s-%2$s.zip', - str_replace( '/', '-', $project->slug ), - $locale->wp_locale + $slug, + $this->locale->wp_locale ); } diff --git a/tests/phpunit/tests/ZipProvider.php b/tests/phpunit/tests/ZipProvider.php index d5637871..f5d7d4a6 100644 --- a/tests/phpunit/tests/ZipProvider.php +++ b/tests/phpunit/tests/ZipProvider.php @@ -100,12 +100,28 @@ public function test_get_zip_path(): void { $this->assertStringEndsWith( 'wp-content/traduttore/foo-project-de_DE.zip', $provider->get_zip_path() ); } + public function test_get_zip_path_with_version(): void { + $this->project->set_version( '2.0' ); + + $provider = new Provider( $this->translation_set ); + + $this->assertStringEndsWith( 'wp-content/traduttore/foo-project-de_DE-2.0.zip', $provider->get_zip_path() ); + } + public function test_get_zip_url(): void { $provider = new Provider( $this->translation_set ); $this->assertSame( home_url( 'wp-content/traduttore/foo-project-de_DE.zip' ), $provider->get_zip_url() ); } + public function test_get_zip_url_with_version(): void { + $this->project->set_version( '2.0' ); + + $provider = new Provider( $this->translation_set ); + + $this->assertSame( home_url( 'wp-content/traduttore/foo-project-de_DE-2.0.zip' ), $provider->get_zip_url() ); + } + public function test_get_last_build_time_for_new_set(): void { $provider = new Provider( $this->translation_set );