<!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>[2931] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc: Translate, Plugin Directory: Enhance translation sync for plugin projects.</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 { 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/2931">2931</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/2931","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>2016-04-11 13:50:03 +0000 (Mon, 11 Apr 2016)</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, Plugin Directory: Enhance translation sync for plugin projects.

* Sync new translations and edits between the stable and dev project of a plugin.
* Provide a WPCLI command to sync all translations between dev and stable. The command will be used by the SVN tracker when stable gets updated.
* Disabled until the update to GlotPress 2.0.

See <a href="http://meta.trac.wordpress.org/ticket/1636">#1636</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/class-plugin.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryinccliclasssyncplugintranslationsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-sync-plugin-translations.php</a></li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/sync/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryincsyncclasstranslationsyncphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/sync/class-translation-sync.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryincclasspluginphp"></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-plugin-directory/inc/class-plugin.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-plugin-directory/inc/class-plugin.php   2016-04-11 13:42:53 UTC (rev 2930)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/class-plugin.php     2016-04-11 13:50:03 UTC (rev 2931)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +12,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        private static $instance;
</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">+         *
+        * @var Sync\Translation_Sync
+        */
+       public $translation_sync = null;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Returns always the same instance of this plugin.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return Plugin
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -37,6 +43,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $cache_purger = new Cache_Purge\Cache_Purger();
</span><span class="cx" style="display: block; padding: 0 10px">                $cache_purger->register_events();
</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->translation_sync = new Sync\Translation_Sync();
+       //      $this->translation_sync->register_events();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( defined( 'WP_CLI' ) && WP_CLI ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->register_cli_commands();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,5 +58,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                WP_CLI::add_command( 'wporg-translate import-plugin-translations', __NAMESPACE__ . '\CLI\Import_Plugin_Translations' );
</span><span class="cx" style="display: block; padding: 0 10px">                WP_CLI::add_command( 'wporg-translate set-plugin-project', __NAMESPACE__ . '\CLI\Set_Plugin_Project' );
</span><span class="cx" style="display: block; padding: 0 10px">                WP_CLI::add_command( 'wporg-translate delete-plugin-project', __NAMESPACE__ . '\CLI\Delete_Plugin_Project' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        //      WP_CLI::add_command( 'wporg-translate sync-plugin-translations', __NAMESPACE__ . '\CLI\Sync_Plugin_Translations' );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryinccliclasssyncplugintranslationsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-sync-plugin-translations.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-plugin-directory/inc/cli/class-sync-plugin-translations.php                             (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/cli/class-sync-plugin-translations.php       2016-04-11 13:50:03 UTC (rev 2931)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,100 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\Plugin_Directory\CLI;
+
+use GP;
+use GP_Locales;
+use Translation_Entry;
+use Translations;
+use WordPressdotorg\GlotPress\Plugin_Directory\Plugin as Plugin;
+use WP_CLI;
+use WP_CLI_Command;
+
+class Sync_Plugin_Translations extends WP_CLI_Command {
+
+       /**
+        * Delete a plugin project and its translations.
+        *
+        * ## OPTIONS
+        *
+        * <slug>
+        * : Slug of a plugin
+        *
+        * <locale>
+        * : Locale to export
+        *
+        * [--set=<set>]
+        * : Translation set slug; default is "default"
+        */
+       public function __invoke( $args, $assoc_args ) {
+               $plugin = Plugin::get_instance();
+               $translation_sync = $plugin->translation_sync;
+
+               // Avoid recursion.
+               remove_action( 'gp_translation_created', array( $translation_sync, 'queue_translation_for_sync' ), 5 );
+               remove_action( 'gp_translation_saved', array( $translation_sync, 'queue_translation_for_sync' ), 5 );
+
+               $project_path = $translation_sync->master_project . '/' . $args[0];
+               $project_src = GP::$project->by_path( $project_path );
+               if ( ! $project_src ) {
+                       WP_CLI::error( 'Source project not found!' );
+               }
+
+               $project_dest = $translation_sync->get_dev_or_stable_project( $project_path );
+               if ( ! $project_dest ) {
+                       WP_CLI::error( 'Destination project not found!' );
+               }
+
+               $locale = GP_Locales::by_slug( $args[1] );
+               if ( ! $locale ) {
+                       WP_CLI::error( 'Locale not found!' );
+               }
+
+               $set_slug = isset( $assoc_args['set'] ) ? $assoc_args['set'] : 'default';
+
+               $translation_set_src = GP::$translation_set->by_project_id_slug_and_locale( $project_src->id, $set_slug, $locale->slug );
+               if ( ! $translation_set_src ) {
+                       WP_CLI::error( 'Source translation set not found!' );
+               }
+
+               $translation_set_dest = GP::$translation_set->by_project_id_slug_and_locale( $project_dest->id, $set_slug, $locale->slug );
+               if ( ! $translation_set_dest ) {
+                       WP_CLI::error( 'Source translation set not found!' );
+               }
+
+               $current_translations_list = GP::$translation->for_translation( $project_src, $translation_set_src, 'no-limit', array( 'status' => 'current' ) );
+               if ( ! $current_translations_list ) {
+                       WP_CLI::log( 'No translations available.' );
+                       return;
+               }
+
+               $current_translations = new Translations();
+               $user_id_map = [];
+               foreach ( $current_translations_list as $translation ) {
+                       $current_translations->add_entry( $translation );
+                       $user_id_map[ $translation->key() ] = $translation->user_id;
+               }
+               unset( $current_translations_list );
+
+               add_action( 'gp_translation_created', function( $translation ) use ( $user_id_map ) {
+                       $original = GP::$original->get( $translation->original_id );
+                       if ( ! $original ) {
+                               return;
+                       }
+
+                       $translation_entry = new Translation_Entry( [
+                               'singular' => $original->singular,
+                               'plural'   => $original->plural,
+                               'context'  => $original->context,
+                       ] );
+                       $key = $translation_entry->key();
+                       if ( isset( $user_id_map[ $key ] ) && $user_id_map[ $key ] !== $translation->user_id ) {
+                               $translation->update( [ 'user_id' => $user_id_map[ $key ] ] );
+                       }
+               } );
+
+               $synced = $translation_set_dest->import( $current_translations );
+
+               WP_CLI::line( sprintf( '%s translations were synced for %s/%s.', $synced, $locale->slug, $set_slug ) );
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryincsyncclasstranslationsyncphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/sync/class-translation-sync.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-plugin-directory/inc/sync/class-translation-sync.php                            (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/inc/sync/class-translation-sync.php      2016-04-11 13:50:03 UTC (rev 2931)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,208 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\Plugin_Directory\Sync;
+
+use GP;
+use GP_Locales;
+use GP_Translation;
+
+class Translation_Sync {
+       public $master_project = 'wp-plugins';
+
+       private $queue = array();
+
+       public $project_mapping = array(
+               'dev'           => 'stable',
+               'stable'        => 'dev',
+               'dev-readme'    => 'stable-readme',
+               'stable-readme' => 'dev-readme',
+       );
+
+       public function register_events() {
+               add_action( 'gp_translation_created', array( $this, 'queue_translation_for_sync' ), 5 );
+               add_action( 'gp_translation_saved', array( $this, 'queue_translation_for_sync' ), 5 );
+
+               add_action( 'shutdown', array( $this, 'sync_translations' ) );
+       }
+
+       /**
+        * Adds a translation to a cache purge queue when a translation was created
+        * or updated.
+        *
+        * @param \GP_Translation $translation Created/updated translation.
+        */
+       public function queue_translation_for_sync( $translation ) {
+               global $wpdb;
+
+               // Only propagate current translations without warnings.
+               if ( 'current' !== $translation->status || ! empty( $translation->warnings ) ) {
+                       return;
+               }
+
+               $project = GP::$project->one(
+                       "SELECT p.* FROM {$wpdb->gp_projects} AS p JOIN {$wpdb->gp_originals} AS o ON o.project_id = p.id WHERE o.id = %d",
+                       $translation->original_id
+               );
+
+               if ( ! $project ) {
+                       return;
+               }
+
+               if ( ! $this->project_is_plugin( $project->path ) ) {
+                       return;
+               }
+
+               $this->queue[ $project->path ][ $translation->id ] = $translation;
+       }
+
+       /**
+        * Syncs translations between two plugin projects.
+        */
+       public function sync_translations() {
+               if ( empty( $this->queue ) ) {
+                       return;
+               }
+
+               // Avoid recursion.
+               remove_action( 'gp_translation_created', array( $this, 'queue_translation_for_sync' ), 5 );
+               remove_action( 'gp_translation_saved', array( $this, 'queue_translation_for_sync' ), 5 );
+
+               foreach ( $this->queue as $project_path => $translations ) {
+                       $project = $this->get_dev_or_stable_project( $project_path );
+                       if ( ! $project ) {
+                               continue;
+                       }
+
+                       foreach ( $translations as $translation ) {
+                               $original = GP::$original->get( $translation->original_id );
+                               if ( ! $original ) {
+                                       continue;
+                               }
+
+                               $translation_set = GP::$translation_set->get( $translation->translation_set_id );
+                               if ( ! $translation_set ) {
+                                       continue;
+                               }
+
+                               $original_counterpart = GP::$original->by_project_id_and_entry(
+                                       $project->id,
+                                       $original,
+                                       '+active'
+                               );
+
+                               if ( ! $original_counterpart ) {
+                                       continue;
+                               }
+
+                               $translation_set_counterpart = GP::$translation_set->by_project_id_slug_and_locale(
+                                       $project->id,
+                                       $translation_set->slug,
+                                       $translation_set->locale
+                               );
+
+                               if ( ! $translation_set_counterpart ) {
+                                       continue;
+                               }
+
+                               $this->copy_translation_into_set( $translation, $translation_set_counterpart, $original_counterpart );
+                       }
+               }
+       }
+
+       /**
+        * Duplicates a translation to another translation set.
+        *
+        * @param \GP_Translation     $translation         The translation which should be duplicated.
+        * @param \GP_Translation_Set $new_translation_set The new translation set.
+        * @param \GP_Original        $new_original        The new original.
+        * @return bool False on failure, true on success.
+        */
+       private function copy_translation_into_set( $translation, $new_translation_set, $new_original ) {
+               $locale = GP_Locales::by_slug( $new_translation_set->locale );
+               $new_translation = array();
+
+               for ( $i = 0; $i < $locale->nplurals; $i++ ) {
+                       $new_translation[] = $translation->{"translation_{$i}"};
+               }
+
+               // Check if the translation already exists.
+               $existing_translations = GP::$translation->find( array(
+                       'translation_set_id' => $new_translation_set->id,
+                       'original_id'        => $new_original->id,
+                       'status'             => array( 'current', 'waiting' ),
+               ) );
+
+               foreach ( $existing_translations as $_existing_translation ) {
+                       $existing_translation = array();
+                       for ( $i = 0; $i < $locale->nplurals; $i++ ) {
+                               $existing_translation[] = $_existing_translation->{"translation_{$i}"};
+                       }
+
+                       if ( $existing_translation === $new_translation ) {
+                               $_existing_translation->set_as_current();
+                               return true;
+                       }
+               }
+
+               $copy = new GP_Translation( $translation->fields() );
+               $copy->original_id = $new_original->id;
+               $copy->translation_set_id = $new_translation_set->id;
+               $copy->status = 'current';
+
+               $translation = GP::$translation->create( $copy );
+               if ( ! $translation ) {
+                       return false;
+               }
+
+               $translation->set_as_current();
+
+               return true;
+       }
+
+       /**
+        * Retrieves the counterpart of a plugin project.
+        *
+        * @param string $project_path The path of a plugin project.
+        * @return \GP_Project|null A project on success, null on failure.
+        */
+       public function get_dev_or_stable_project( $project_path ) {
+               static $project_cache;
+
+               if ( null === $project_cache ) {
+                       $project_cache = array();
+               }
+
+               if ( isset( $project_cache[ $project_path ] ) ) {
+                       return $project_cache[ $project_path ];
+               }
+
+               $project = basename( $project_path );
+               $counterpart = $this->project_mapping[ $project ];
+               $new_project_path = preg_replace( "#/{$project}$#", "/$counterpart", $project_path, 1 );
+
+               $project = GP::$project->by_path( $new_project_path );
+               $project_cache[ $project_path ] = $project;
+
+               return $project;
+       }
+
+       /**
+        * Returns whether a project path belongs to the plugins project.
+        *
+        * @param string $path Path of a project.
+        *
+        * @return bool True if it's a plugin, false if not.
+        */
+       public function project_is_plugin( $path ) {
+               if ( empty( $path ) ) {
+                       return false;
+               }
+
+               $path = '/' . trim( $path, '/' ) . '/';
+               if ( false === strpos( $path, "/{$this->master_project}/" ) ) {
+                       return false;
+               }
+
+               return true;
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>