<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[8338] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php: Translate: Include JSON translations in language packs.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/8338">8338</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/8338","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-02-24 18:22:00 +0000 (Sun, 24 Feb 2019)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Translate: Include JSON translations in language packs.
Props herregroen, ocean90.
See <a href="http://meta.trac.wordpress.org/ticket/3876">#3876</a>, <a href="http://meta.trac.wordpress.org/ticket/3976">#3976</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpcustomizationsinccliclasslanguagepackphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpcustomizationsinccliclasslanguagepackphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php 2019-02-24 16:13:21 UTC (rev 8337)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-language-pack.php 2019-02-24 18:22:00 UTC (rev 8338)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,6 +6,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> use GP_Locales;
</span><span class="cx" style="display: block; padding: 0 10px"> use stdClass;
</span><span class="cx" style="display: block; padding: 0 10px"> use WP_CLI;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use WP_CLI\Utils;
</ins><span class="cx" style="display: block; padding: 0 10px"> use WP_CLI_Command;
</span><span class="cx" style="display: block; padding: 0 10px"> use WP_Error;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,6 +18,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> const PACKAGE_THRESHOLD = 95;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Whether a language pack should be enforced.
+ *
+ * @var bool
+ */
+ private $force = false;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Generates a language pack.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * ## OPTIONS
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -35,11 +43,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * [--version]
</span><span class="cx" style="display: block; padding: 0 10px"> * : Current version of the theme or plugin.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * [--force]
+ * : Generate language pack even when threshold is not reached or no updates exist.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function generate( $args, $assoc_args ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $type = $args[0];
</span><span class="cx" style="display: block; padding: 0 10px"> $slug = $args[1];
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->force = Utils\get_flag_value( $assoc_args, 'force' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $args = wp_parse_args( $assoc_args, [
</span><span class="cx" style="display: block; padding: 0 10px"> 'locale' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'locale-slug' => 'default',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -220,7 +233,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * The temporary directory returned will be removed upon script termination.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $prefix Optional. The prefix for the directory, 'hello-dolly' for example.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return string The temporary directory.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function get_temp_directory( $prefix = '' ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -245,7 +257,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Removes a directory.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $dir The directory which should be removed.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return bool False if directory is removed, false otherwise.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function remove_temp_directory( $dir ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -260,7 +271,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the stable tag of a plugin.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $plugin_slug Slug of a plugin.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return false|string False on failure, stable tag on success.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function get_plugin_stable_tag( $plugin_slug ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -278,7 +288,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the current stable version of a theme.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $theme_slug Slug of a theme.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return false|string False on failure, version on success.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function get_latest_theme_version( $theme_slug ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -296,7 +305,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Retrieves the current stable version of a plugin.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $plugin_slug Slug of a plugin.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return false|string False on failure, version on success.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function get_latest_plugin_version( $plugin_slug ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -374,20 +382,97 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Build a mapping of JS files to translation entries occurring in those files.
+ * Translation entries occurring in other files are added to the 'po' key.
+ *
+ * @param Translation_Entry[] $entries The translation entries to map.
+ * @return array The mapping of sources to translation entries.
+ */
+ private function build_mapping( $entries ) {
+ $mapping = [];
+
+ foreach ( $entries as $entry ) {
+ /** @var Translation_Entry $entry */
+
+ // Find all unique sources this translation originates from.
+ $sources = array_map( function ( $reference ) {
+ $parts = explode( ':', $reference );
+ $file = $parts[0];
+
+ if ( substr( $file, -7 ) === '.min.js' ) {
+ return substr( $file, 0, -7 ) . '.js';
+ }
+
+ if ( substr( $file, -3 ) === '.js' ) {
+ return $file;
+ }
+ return 'po';
+ }, $entry->references );
+
+ // Always add all entries to the PO file.
+ $sources[] = 'po';
+
+ $sources = array_unique( $sources );
+
+ foreach ( $sources as $source ) {
+ $mapping[ $source ][] = $entry;
+ }
+ }
+
+ return $mapping;
+ }
+
+ /**
+ * Builds a a separate JSON file with translations for each JavaScript file.
+ *
+ * @param GP_Project $gp_project The GlotPress project.
+ * @param GP_Locale $gp_locale The GlotPress locale.
+ * @param GP_Translation_Set $set The translation set.
+ * @param array $mapping A mapping of files to translation entries.
+ * @param string $base_dest Destination file name.
+ * @return array An array of translation files built, may be empty if no translations in JS files exist.
+ */
+ private function build_json_files( $gp_project, $gp_locale, $set, $mapping, $base_dest ) {
+ $files = array();
+ $format = gp_array_get( GP::$formats, 'jed1x' );
+
+ foreach ( $mapping as $file => $entries ) {
+ // Don't create JSON files for source files.
+ if ( 0 === strpos( $file, 'src/' ) || false !== strpos( $file, '/src/' ) ) {
+ continue;
+ }
+
+ // Get the translations in Jed 1.x compatible JSON format.
+ $json_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries );
+
+ // Decode and add comment with file reference for debugging.
+ $json_content_decoded = json_decode( $json_content );
+ $json_content_decoded->comment = [ 'reference' => $file ];
+
+ $json_content = wp_json_encode( $json_content_decoded );
+
+ $hash = md5( $file );
+ $dest = "{$base_dest}-{$hash}.json";
+
+ file_put_contents( $dest, $json_content );
+
+ $files[] = $dest;
+ }
+
+ return $files;
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Builds a PO file for translations.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param GP_Project $gp_project The GlotPress project.
- * @param GP_Locale $gp_locale The GlotPress locale.
- * @param GP_Translation_Set $set The translation set.
- * @param string $dest Destination file name.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param GP_Project $gp_project The GlotPress project.
+ * @param GP_Locale $gp_locale The GlotPress locale.
+ * @param GP_Translation_Set $set The translation set.
+ * @param Translation_Entry[] $entries The translation entries.
+ * @param string $dest Destination file name.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string|WP_Error Last updated date on success, WP_Error on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private function build_po_file( $gp_project, $gp_locale, $set, $dest ) {
- $entries = GP::$translation->for_export( $gp_project, $set, [ 'status' => 'current' ] );
- if ( ! $entries ) {
- return new WP_Error( 'no_translations', 'No current translations available.' );
- }
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function build_po_file( $gp_project, $gp_locale, $set, $entries, $dest ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $format = gp_array_get( GP::$formats, 'po' );
</span><span class="cx" style="display: block; padding: 0 10px"> $po_content = $format->print_exported_file( $gp_project, $gp_locale, $set, $entries );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -406,7 +491,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Executes a command via exec().
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $command The escaped command to execute.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return true|WP_Error True on success, WP_Error on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function execute_command( $command ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -427,8 +511,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $language Language the language pack is for.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $version Version of the theme/plugin.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $updated Last updated.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return string|WP_Error 'updated' when language pack was updated, 'inserted' if it's a new
- * language pack. WP_Error on failure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return true|WP_Error true when language pack was updated, WP_Error on failure.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function insert_language_pack( $type, $domain, $language, $version, $updated ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wpdb;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -443,7 +526,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $existing ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return new WP_Error( 'language_pack_exists', 'The language pack is already imported for this version.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return true;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $now = current_time( 'mysql', 1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -473,11 +556,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $wpdb->insert_id
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $wpdb->rows_affected ) {
- return 'updated';
- } else {
- return 'inserted';
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return true;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -487,7 +566,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private function build_language_packs( $data ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $existing_packs = $this->get_active_language_packs( $data->type, $data->domain, $data->version );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $svn_command = $this->get_svn_command();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $svn_command = $this->get_svn_command();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $data->translation_sets as $set ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // Get WP locale.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -510,13 +589,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Check if percent translated is above threshold.
</span><span class="cx" style="display: block; padding: 0 10px"> $percent_translated = $set->percent_translated();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $percent_translated < self::PACKAGE_THRESHOLD ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $this->force && $percent_translated < self::PACKAGE_THRESHOLD ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::log( "Skip {$wp_locale}, translations below threshold ({$percent_translated}%)." );
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Check if new translations are available since last build.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $existing_packs[ $wp_locale ] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $this->force && isset( $existing_packs[ $wp_locale ] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $pack_time = strtotime( $existing_packs[ $wp_locale ]->updated );
</span><span class="cx" style="display: block; padding: 0 10px"> $glotpress_time = strtotime( $set->last_modified() );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -526,25 +605,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $export_directory = "{$data->svn_checkout}/{$data->domain}/{$data->version}/{$wp_locale}";
- $build_directory = self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}";
- $filename = "{$data->domain}-{$wp_locale}";
- $po_file = "{$export_directory}/{$filename}.po";
- $mo_file = "{$export_directory}/{$filename}.mo";
- $zip_file = "{$export_directory}/{$filename}.zip";
- $build_zip_file = "{$build_directory}/{$wp_locale}.zip";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $entries = GP::$translation->for_export( $data->gp_project, $set, [ 'status' => 'current' ] );
+ if ( ! $entries ) {
+ WP_CLI::warning( "No current translations available for {$wp_locale}." );
+ continue;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $working_directory = "{$data->svn_checkout}/{$data->domain}";
+ $export_directory = "{$working_directory}/{$data->version}/{$wp_locale}";
+ $build_directory = self::BUILD_DIR . "/{$data->type}s/{$data->domain}/{$data->version}";
+
+ $filename = "{$data->domain}-{$wp_locale}";
+ $json_file_base = "{$export_directory}/{$filename}";
+ $po_file = "{$export_directory}/{$filename}.po";
+ $mo_file = "{$export_directory}/{$filename}.mo";
+ $zip_file = "{$export_directory}/{$filename}.zip";
+ $build_zip_file = "{$build_directory}/{$wp_locale}.zip";
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Update/create directories.
</span><span class="cx" style="display: block; padding: 0 10px"> $this->update_svn_directory( $export_directory );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Build a mapping based on where the translation entries occur and separate the po entries.
+ $mapping = $this->build_mapping( $entries );
+ $po_entries = array_key_exists( 'po', $mapping ) ? $mapping['po'] : [];
+
+ unset( $mapping['po'] );
+
+ // Create JED json files for each JS file.
+ $json_files = $this->build_json_files( $data->gp_project, $gp_locale, $set, $mapping, $json_file_base );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Create PO file.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_file );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $last_modified = $this->build_po_file( $data->gp_project, $gp_locale, $set, $po_entries, $po_file );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $last_modified ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( sprintf( "PO generation for {$wp_locale} failed: %s", $last_modified->get_error_message() ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -561,7 +658,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( "MO generation for {$wp_locale} failed." );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -568,10 +665,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Create ZIP file.
</span><span class="cx" style="display: block; padding: 0 10px"> $result = $this->execute_command( sprintf(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'zip -9 -j %s %s %s 2>&1',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'zip -9 -j %s %s %s %s 2>&1',
</ins><span class="cx" style="display: block; padding: 0 10px"> escapeshellarg( $zip_file ),
</span><span class="cx" style="display: block; padding: 0 10px"> escapeshellarg( $po_file ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- escapeshellarg( $mo_file )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ escapeshellarg( $mo_file ),
+ implode( ' ', array_map( 'escapeshellarg', $json_files ) )
</ins><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( is_wp_error( $result ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -579,7 +677,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( "ZIP generation for {$wp_locale} failed." );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -595,7 +693,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( "Creating build directories for {$wp_locale} failed." );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -612,7 +710,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( "Moving ZIP file for {$wp_locale} failed." );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -624,7 +722,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( sprintf( "Language pack for {$wp_locale} failed: %s", $result->get_error_message() ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -649,13 +747,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::warning( "SVN commit for {$wp_locale} failed." );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> continue;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Clean up.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->execute_command( "rm -rf {$data->svn_checkout}/{$data->domain}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->execute_command( sprintf( 'rm -rf %s', escapeshellarg( $working_directory ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> WP_CLI::success( "Language pack for {$wp_locale} generated." );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>