<!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>[2367] sites/trunk/wordpress.org/public_html/wp-content/plugins: Translate, Theme Directory: Open source CLI command for adding/updating theme 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/2367">2367</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/2367","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-01-25 22:20:06 +0000 (Mon, 25 Jan 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, Theme Directory: Open source CLI command for adding/updating theme projects.

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

<h3>Added Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/cli/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryclisetthemeprojectphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/cli/set-theme-project.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectorywporggpthemedirectoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/wporg-gp-theme-directory.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryclisetthemeprojectphp"></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-theme-directory/cli/set-theme-project.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-theme-directory/cli/set-theme-project.php                               (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/cli/set-theme-project.php 2016-01-25 22:20:06 UTC (rev 2367)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,335 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WPorg_GP_CLI_Set_Theme_Project extends WP_CLI_Command {
+
+       /**
+        * Holds the path of the master project.
+        *
+        * @var string
+        */
+       private $master_project_path = 'wp-themes';
+
+       /**
+        * Holds the path of the projects to copy translation sets from.
+        *
+        * @var string
+        */
+       private $project_path_for_sets = 'wp/dev';
+
+       /**
+        * Holds the path to a temporary directory.
+        *
+        * @var string
+        */
+       private $temp_dir;
+
+       /**
+        * MakePot instance.
+        *
+        * @var MakePot
+        */
+       private $makepot;
+
+       public function __construct() {
+               if ( ! file_exists( '/tmp/wporg-themes-i18n/' ) ) {
+                       mkdir( '/tmp/wporg-themes-i18n/' );
+               }
+
+               $this->temp_dir = tempnam( '/tmp/wporg-themes-i18n/', '' );
+               unlink( $this->temp_dir );
+               if ( ! mkdir( $this->temp_dir ) ) {
+                       WP_CLI::error( "Couldn't create temporary directory." );
+               }
+
+               $this->checkout_tools();
+               if ( ! file_exists( $this->temp_dir . '/i18n-tools/makepot.php' ) ) {
+                       WP_CLI::error( "Couldn't find MakePot." );
+               }
+               require_once $this->temp_dir . '/i18n-tools/makepot.php';
+               $this->makepot = new MakePot();
+       }
+
+       /**
+        * Add/update a theme project.
+        *
+        * ## OPTIONS
+        *
+        * <slug>
+        * : Slug of a theme
+        *
+        * <version>
+        * : Version of a theme
+        *
+        */
+       public function __invoke( $args, $assoc_args ) {
+               $theme_slug    = $args[0];
+               $theme_version = $args[1];
+
+               // We pass 'inactive' as the theme version due to caching issues of fetching the API response.
+               if ( 'inactive' === $theme_version ) {
+                       $this->mark_glotpress_project_inactive( $theme_slug );
+                       WP_CLI::success( "{$theme_slug} marked as inactive." );
+                       return;
+               }
+
+               $theme_dir = $this->checkout_theme( $theme_slug, $theme_version );
+               if ( ! $theme_dir ) {
+                       WP_CLI::error( "{$theme_slug} {$theme_version} could not be found." );
+               }
+
+               $theme_data = $this->get_theme_data( $theme_slug, $theme_dir );
+
+               $project = $this->find_create_update_glotpress_project( $theme_slug, $theme_data );
+               $pot = $this->generate_pot( $theme_slug, $theme_dir );
+               $this->import_pot_to_glotpress( $pot, $project );
+
+               $this->cleanup_theme( $theme_slug );
+
+               gp_clean_translation_sets_cache( $project->id );
+               WP_CLI::success( "{$theme_slug} {$theme_version} imported." );
+       }
+
+       /**
+        * Creates the Theme checkout to operate upon
+        */
+       private function checkout_theme( $theme_slug, $theme_version ) {
+               $theme_dir = "{$this->temp_dir}/{$theme_slug}/";
+               $theme_svn = "https://themes.svn.wordpress.org/{$theme_slug}/{$theme_version}/";
+               $esc_theme_dir = escapeshellarg( $theme_dir );
+               $esc_theme_svn = escapeshellarg( $theme_svn );
+
+               `svn export --non-interactive {$esc_theme_svn} {$esc_theme_dir}`;
+
+               if ( ! file_exists( $theme_dir ) || ! file_exists( "{$theme_dir}style.css" ) ) {
+                       return false;
+               }
+
+               return $theme_dir;
+       }
+
+       /**
+        * A cutdown version of core's get_file_data() to return specific headers from a Theme, plus it's screenshot.
+        */
+       private function get_theme_data( $theme_slug, $theme_dir ) {
+               $style_css = "{$theme_dir}style.css";
+
+               $theme_data = array(
+                       'name'        => 'Theme Name',
+                       'version'     => 'Version',
+                       'description' => 'Description',
+               );
+
+               $file_data = file_get_contents( $style_css, false, null, -1, 8192 );
+
+               // Make sure we catch CR-only line endings.
+               $file_data = str_replace( "\r", "\n", $file_data );
+
+               foreach ( $theme_data as $field => $regex ) {
+                       if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_data, $match ) && $match[1] ) {
+                               $theme_data[ $field ] = strip_tags( _cleanup_header_comment( $match[1] ) );
+                       } else {
+                               $theme_data[ $field ] = '';
+                       }
+               }
+
+               // Screenshot
+               $theme_data['screenshot'] = '';
+               foreach ( array( 'png', 'jpg' ) as $ext ) {
+                       if ( ! file_exists( "{$theme_dir}screenshot.$ext" ) ) {
+                               continue;
+                       }
+
+                       $theme_data['screenshot'] = "themes.svn.wordpress.org/{$theme_slug}/{$theme_data['version']}/screenshot.{$ext}";
+               }
+
+               return $theme_data;
+       }
+
+       /**
+        * Create a temporary pot file for a theme.
+        *
+        * @param string $theme_slug The theme slug to generate the pot for.
+        * @param string $theme_dir  The directory containing the theme.
+        *
+        * @return string The path to the .pot file.
+        */
+       private function generate_pot( $theme_slug, $theme_dir ) {
+               $pot_file = "{$this->temp_dir}/{$theme_slug}.pot";
+               $this->makepot->wp_theme( $theme_dir, $pot_file );
+               return $pot_file;
+       }
+
+       /**
+        * Finds / Creates / Updates a GlotPress project for a given Theme.
+        *
+        * @param string $theme_slug The theme slug to generate the project from.
+        * @param array  $theme_data The theme data (headers + screenshot) from get_theme_data().
+        */
+       private function find_create_update_glotpress_project( $theme_slug, $theme_data ) {
+               $parent_project = GP::$project->by_path( $this->master_project_path );
+               $project_path = $parent_project->path . '/' . $theme_slug;
+
+               $project_args = array(
+                       'name'                => $theme_data['name'],
+                       'slug'                => $theme_slug,
+                       'description'         => $theme_data['description'],
+                       'parent_project_id'   => $parent_project->id,
+                       'source_url_template' => "https://themes.trac.wordpress.org/browser/$theme_slug/{$theme_data['version']}/%file%#L%line%",
+                       'active'              => 1
+               );
+
+               $project = GP::$project->by_path( $project_path );
+               if ( ! $project ) {
+                       WP_CLI::line( 'Creating project' );
+                       $project = GP::$project->create_and_select( $project_args );
+                       if ( ! $project ) {
+                               return false;
+                       }
+               } else {
+                       /*
+                        * Update the project details if any have changed.
+                        * GlotPress doesn't do a check first, so we shall - avoids subsequent reads going to master.
+                        */
+                       foreach ( $project_args as $arg => $value ) {
+                               if ( $project->$arg != $value ) {
+                                       $project->save( $project_args );
+                                       break;
+                               }
+                       }
+               }
+
+               $this->create_update_glotpress_translation_sets( $project );
+
+               gp_update_meta( $project->id, 'screenshot', $theme_data['screenshot'], 'wp-themes' );
+               gp_update_meta( $project->id, 'version',    $theme_data['version'],    'wp-themes' );
+
+               return $project;
+       }
+
+       /**
+        * Marks a theme project as inactive.
+        *
+        * @param string $theme_slug The theme to mark inactive.
+        */
+       private function mark_glotpress_project_inactive( $theme_slug ) {
+               $project = GP::$project->by_path( "{$this->master_project_path}/{$theme_slug}" );
+               if ( $project ) {
+                       $project->save( array(
+                               'active' => 0
+                       ) );
+               }
+       }
+
+       /**
+        * Creates / Updates the translation sets for a theme project.
+        *
+        * @param GP_Project $project The GlotPress project to create the sets on.
+        */
+       private function create_update_glotpress_translation_sets( $project ) {
+               $translation_sets = (array) GP::$translation_set->by_project_id( GP::$project->by_path( $this->project_path_for_sets )->id );
+
+               $existing_sets = array();
+               foreach ( GP::$translation_set->by_project_id( $project->id ) as $set ) {
+                       $existing_sets[ $set->locale . ':' . $set->slug ] = true;
+               }
+
+               foreach ( $translation_sets as $set ) {
+                       if ( isset( $existing_sets[ $set->locale . ':' . $set->slug ] ) ) {
+                               // This translation set already exists.
+                               continue;
+                       }
+
+                       WP_CLI::line( sprintf( "Creating translation set %s (%s)", $set->name, $set->locale . ( $set->slug != 'default' ? '/' . $set->slug : '' ) ) );
+
+                       GP::$translation_set->create( array(
+                               'project_id' => $project->id,
+                               'name'       => $set->name,
+                               'locale'     => $set->locale,
+                               'slug'       => $set->slug,
+                       ) );
+               }
+       }
+
+       /**
+        * Imports a pot file into GlotPress, also alters the priority for various fields.
+        *
+        * @param string     $pot_file The themes pot file to import.
+        * @param GP_Project $project  The theme project to import into.
+        */
+       private function import_pot_to_glotpress( $pot_file, $project ) {
+               $format = gp_array_get( GP::$formats, 'po', null );
+               $originals = $format->read_originals_from_file( $pot_file, $project );
+
+               add_filter( 'gp_import_original_array', array( $this, 'filter_import_original_priority' ) );
+
+               list( $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted ) = GP::$original->import_for_project( $project, $originals );
+
+               remove_filter( 'gp_import_original_array', array( $this, 'filter_import_original_priority' ) );
+
+               WP_CLI::line( sprintf(
+                       '%1$s new strings added, %2$s updated, %3$s fuzzied, and %4$s obsoleted.',
+                       $originals_added,
+                       $originals_existing,
+                       $originals_fuzzied,
+                       $originals_obsoleted
+               ) );
+       }
+
+
+       /**
+        * Sets the priority of a string in GlotPress.
+        * 1 = High, 0 = normal, -1 = low.
+        *
+        * @param array $data The original data.
+        * @return array Original data with priorities.
+        */
+       public function filter_import_original_priority( $data ) {
+               $priorities = array(
+                       // These are important strings that we need translated
+                       'Plugin Name of the plugin/theme' => 1,
+                       'Theme Name of the plugin/theme'  => 1,
+                       'Description of the plugin/theme' => 1,
+                       // Regular strings are more important than these:
+                       'Plugin URI of the plugin/theme'  => -1,
+                       'Theme URI of the plugin/theme'   => -1,
+                       'Author of the plugin/theme'      => -1,
+                       'Author URI of the plugin/theme'  => -1,
+               );
+               if ( isset( $priorities[ $data['comment'] ] ) ) {
+                       $data['priority'] = $priorities[ $data['comment'] ];
+               }
+               return $data;
+       }
+
+       /**
+        * Creates an i18n-tools checkout so we have MakePot available.
+        */
+       private function checkout_tools() {
+               $tools_dir = "{$this->temp_dir}/i18n-tools/";
+               if ( ! file_exists( $tools_dir ) ) {
+                       $esc_tools_dir = escapeshellarg( $tools_dir );
+                       `svn export --non-interactive https://i18n.svn.wordpress.org/tools/trunk {$esc_tools_dir}`;
+               }
+       }
+
+       /**
+        * Cleanup a theme directory, for when this is used in batch mode.
+        */
+       private function cleanup_theme( $theme_slug ) {
+               if ( $this->temp_dir ) {
+                       $esc_theme_dir = escapeshellarg( $this->temp_dir . $theme_slug );
+                       `rm -rf {$esc_theme_dir}`;
+                       @unlink( $this->temp_dir . $theme_slug . '.pot' );
+               }
+       }
+
+       /**
+        * Clean up after the theme process is complete.
+        */
+       public function __destruct() {
+               if ( $this->temp_dir ) {
+                       $esc_temp_dir = escapeshellarg( $this->temp_dir );
+                       `rm -rf {$esc_temp_dir}`;
+               }
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectorywporggpthemedirectoryphp"></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-theme-directory/wporg-gp-theme-directory.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-theme-directory/wporg-gp-theme-directory.php                            (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/wporg-gp-theme-directory.php      2016-01-25 22:20:06 UTC (rev 2367)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,40 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Plugin name: GlotPress: Theme Directory Bridge
+ * Description: Provides CLI commands to import themes into translate.wordpress.org or to mark themes as inactive.
+ * Version:     1.0
+ * Author:      WordPress.org
+ * Author URI:  http://wordpress.org/
+ * License:     GPLv2 or later
+ */
+
+class WPorg_GP_Theme_Directory {
+       public $master_project   = 'wp-themes';
+
+       public function __construct() {
+
+               if ( defined( 'WP_CLI' ) && WP_CLI ) {
+                       $this->register_cli_commands();
+               }
+       }
+
+       /**
+        * Registers CLI commands if WP-CLI is loaded.
+        */
+       function register_cli_commands() {
+               require_once __DIR__ . '/cli/set-theme-project.php';
+
+               WP_CLI::add_command( 'wporg-translate set-theme-project', 'WPorg_GP_CLI_Set_Theme_Project' );
+       }
+}
+
+function wporg_gp_theme_directory() {
+       global $wporg_gp_theme_directory;
+
+       if ( ! isset( $wporg_gp_theme_directory ) ) {
+               $wporg_gp_theme_directory = new WPorg_GP_Theme_Directory();
+       }
+
+       return $wporg_gp_theme_directory;
+}
+add_action( 'plugins_loaded', 'wporg_gp_theme_directory' );
</ins></span></pre>
</div>
</div>

</body>
</html>