Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate zip file names with the version number in them #143

Merged
merged 5 commits into from
Mar 15, 2019
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
68 changes: 55 additions & 13 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
);

Expand All @@ -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,
Expand All @@ -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;
}
);

Expand Down
4 changes: 2 additions & 2 deletions inc/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
43 changes: 36 additions & 7 deletions inc/ZipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,32 @@ class ZipProvider {
protected const BUILD_TIME_KEY = '_traduttore_build_time';

/**
* The GlotPress translation set.
* The current GlotPress translation set.
*
* @since 2.0.0
*
* @var GP_Translation_Set The translation set.
*/
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.
*
Expand All @@ -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 ) );
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/tests/ZipProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down