Skip to content

Commit

Permalink
Schedule a LP ZIP generation if version or textdomain change
Browse files Browse the repository at this point in the history
The package version can change without any changes with the strings
  • Loading branch information
grappler committed Mar 14, 2019
1 parent 545571d commit cfab30e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 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 @@ -120,6 +121,42 @@ function( $translation_set_id ) {
}
);

add_filter(
'gp_update_meta',
function( $meta_tuple ) {
$project = ( new ProjectLocator( $meta_tuple['object_id'] ) )->get_project();
if ( ! $project || ! $project->is_active() ) {
return;
}

$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;
}

$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;
}

$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;
}
);

add_action(
'traduttore.update',
function ( $project_id ) {
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

0 comments on commit cfab30e

Please sign in to comment.