<!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>[2344] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory: Translate, Plugin Directory: Open source CLI command for adding/updating 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/2344">2344</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/2344","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-20 23:22:31 +0000 (Wed, 20 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, Plugin Directory: Open source CLI command for adding/updating plugin projects.

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

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

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectoryclisetpluginprojectphp"></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/cli/set-plugin-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-plugin-directory/cli/set-plugin-project.php                             (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/cli/set-plugin-project.php       2016-01-20 23:22:31 UTC (rev 2344)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,285 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WPorg_GP_CLI_Set_Plugin_Project extends WP_CLI_Command {
+
+       /**
+        * Holds the path of the master project.
+        *
+        * @var string
+        */
+       private $master_project_path = 'wp-plugins';
+
+       /**
+        * Holds the path of the projects to copy translation sets from.
+        *
+        * @var string
+        */
+       private $project_path_for_sets = 'wp/dev';
+
+       /**
+        * Add/update a plugin project.
+        *
+        * ## OPTIONS
+        *
+        * <slug>
+        * : Slug of a plugin
+        *
+        * <type>
+        * : Process type. Accepted values: dev, dev-readme, stable, stable-readme.
+        *
+        */
+       public function __invoke( $args, $assoc_args ) {
+               if ( ! preg_match( '/^[^\/]+$/', $args[0] ) ) {
+                       WP_CLI::line( sprintf( "The plugin slug '%s' contains invalid characters.", $args[0] ) );
+                       exit( 1 );
+               }
+
+               $plugin_slug         = $args[0];
+               $process_type        = $args[1];
+               $plugin_project_path = "{$this->master_project_path}/{$plugin_slug}";
+
+               // Get data for master parent project.
+               $master_project = GP::$project->by_path( $this->master_project_path );
+               if ( ! $master_project ) {
+                       WP_CLI::line( sprintf( "The master project '%s' couldn't be found.", $this->master_project_path ) );
+                       exit( 2 );
+               }
+
+               // Get plugin details like title and description.
+               $plugin_details = $this->get_plugin_details( $plugin_slug );
+               if ( ! $plugin_details ) {
+                       WP_CLI::line( "The plugin API couldn't be reached." );
+                       exit( 3 );
+               }
+
+               // Get or create the plugin GP project.
+               $project_args = array(
+                       'name'              => $plugin_details->name,
+                       'slug'              => $plugin_slug,
+                       'parent_project_id' => $master_project->id,
+                       'description'       => $plugin_details->short_description . "<br><br><a href='https://wordpress.org/plugins/{$plugin_slug}'>WordPress.org Plugin Page</a>",
+                       'active'            => 1,
+               );
+
+               $plugin_project = GP::$project->by_path( $plugin_project_path );
+
+               // Is this a new or an existing project?
+               $new_project_flag = true;
+
+               if ( ! $plugin_project ) {
+                       WP_CLI::line( "Creating project for {$plugin_project_path}." );
+
+                       $plugin_project = GP::$project->create_and_select( $project_args );
+                       if ( ! $plugin_project ) {
+                               WP_CLI::line( "Couldn't create project for {$plugin_project_path}." );
+                               exit( 5 );
+                       }
+               } else {
+                       WP_CLI::line( "Updating project for {$plugin_project_path}." );
+
+                       // This is an existing project.
+                       $new_project_flag = false;
+
+                       // Update the project details.
+                       $plugin_project->update( $project_args );
+               }
+
+               // The current stable SVN tag.
+               $stable_tag = empty( $plugin_details->stable_tag ) ? 'trunk' : $plugin_details->stable_tag;
+
+               // Deal with the always-existing dev|dev-readme branch.
+               $plugin_dev_branch_project = $this->handle_plugin_project_branches(
+                       $plugin_project_path,
+                       $plugin_slug,
+                       $plugin_details->name,
+                       $plugin_project->id,
+                       $process_type,
+                       $stable_tag,
+                       'dev'
+               );
+
+               // Deal with the stable|stable-readme branch, if needed.
+               $plugin_stable_branch_project = false;
+               if ( 'trunk' !== $stable_tag ) {
+                       $plugin_stable_branch_project = $this->handle_plugin_project_branches(
+                               $plugin_project_path,
+                               $plugin_slug,
+                               $plugin_details->name,
+                               $plugin_project->id,
+                               $process_type,
+                               $stable_tag,
+                               'stable'
+                       );
+               }
+
+               // Deal with initial translation sets.
+               $source_project = GP::$project->by_path( $this->project_path_for_sets );
+               if ( $source_project && $plugin_dev_branch_project ) {
+                       $translation_sets = (array) GP::$translation_set->by_project_id( $source_project->id );
+
+                       if ( $translation_sets ) {
+                               $this->add_translation_sets_to_branches( $translation_sets, $plugin_dev_branch_project );
+
+                               if ( $plugin_stable_branch_project ) {
+                                       $this->add_translation_sets_to_branches( $translation_sets, $plugin_stable_branch_project );
+                               }
+                       }
+               }
+
+               WP_CLI::line( ( $new_project_flag ? 'Created' : 'Updated' ) . " https://translate.wordpress.org/projects/{$plugin_project_path}" );
+               exit( 0 );
+       }
+
+       /**
+        * Creates/updates sub-projects for plugins.
+        *
+        * @param string $plugin_project_path Path to the plugin project.
+        * @param string $plugin_slug         Slug of the plugin.
+        * @param string $plugin_name         Name of the plugin.
+        * @param int    $parent_id           ID of the main project.
+        * @param string $process_type        Type of the project, 'code' or 'readme'.
+        * @param string $stable_tag          The stable SVN tag of the plugin.
+        * @param string $branch_slug         Slug of the project, 'dev' or 'stable'.
+        * @return GP_Project|null A GP_Project instance on success, null on failure.
+        */
+       private function handle_plugin_project_branches( $plugin_project_path, $plugin_slug, $plugin_name, $parent_id, $process_type, $stable_tag, $branch_slug ) {
+               if ( ! in_array( $branch_slug, array( 'dev', 'stable' ) ) ) {
+                       $branch_slug = 'dev';
+               }
+
+               $is_stable = 'stable' === $branch_slug;
+
+               if ( 'code' !== $process_type ) {
+                       $process_type = 'readme';
+               }
+
+               if ( 'code' === $process_type ) {
+                       $branch_project_path = "{$plugin_project_path}/{$branch_slug}";
+                       $source_url_template = sprintf(
+                               'https://plugins.trac.wordpress.org/browser/%s/%s/%s',
+                               $plugin_slug,
+                               $is_stable ? "tags/{$stable_tag}" : 'trunk',
+                               '%file%#L%line%'
+                       );
+
+                       $project_args = array(
+                               'name'                => $is_stable ? 'Stable (latest release)' : 'Development (trunk)',
+                               'slug'                => $branch_slug,
+                               'parent_project_id'   => $parent_id,
+                               'description'         => ( $is_stable ? 'Stable' : 'Development' ) . " version of the {$plugin_name} plugin.",
+                               'source_url_template' => $source_url_template,
+                               'active'              => 1,
+                       );
+
+                       $branch_project = GP::$project->by_path( $branch_project_path );
+                       if ( ! $branch_project ) {
+                               $branch_project = GP::$project->create_and_select( $project_args );
+                               if ( ! $branch_project ) {
+                                       WP_CLI::line( "Sorry, but couldn't create nonexistent https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               } else {
+                                       WP_CLI::line( "Created https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               }
+                       } else {
+                               $updated = $branch_project->update( $project_args );
+                               if ( ! $updated ) {
+                                       WP_CLI::line( "Sorry, but couldn't update https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               } else {
+                                       WP_CLI::line( "Updated https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               }
+                       }
+               } else {
+                       $branch_project_path = "{$plugin_project_path}/{$branch_slug}-readme";
+
+                       $project_args = array(
+                               'name'              => $is_stable ? 'Stable Readme (latest release)' : 'Development Readme (trunk)',
+                               'slug'              => "{$branch_slug}-readme",
+                               'parent_project_id' => $parent_id,
+                               'description'       => ( $is_stable ? 'Stable' : 'Development' ) . " version of the {$plugin_name} plugin's readme.txt file.",
+                               'active'            => 1,
+                       );
+
+                       $branch_project = GP::$project->by_path( $branch_project_path );
+                       if ( ! $branch_project ) {
+                               $branch_project = GP::$project->create_and_select( $project_args );
+                               if ( ! $branch_project ) {
+                                       WP_CLI::line( "Sorry, but couldn't create nonexistent https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               } else {
+                                       WP_CLI::line( "Created https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               }
+                       } else {
+                               $updated = $branch_project->update( $project_args );
+                               if ( ! $updated ) {
+                                       WP_CLI::line( "Sorry, but couldn't update https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               } else {
+                                       WP_CLI::line( "Updated https://translate.wordpress.org/projects/{$branch_project_path}." );
+                               }
+                       }
+               }
+
+               return $branch_project;
+       }
+
+       /**
+        * Adds translation sets to a new plugin project.
+        *
+        * @param GP_Translation_Set[] $translation_sets Array of translation sets.
+        * @param GP_Project           $project          The project to add the sets to.
+        */
+       private function add_translation_sets_to_branches( $translation_sets, $project ) {
+               if ( empty( $project ) || empty( $project->id ) ) {
+                       return;
+               }
+
+               foreach ( $translation_sets as $ts ) {
+                       if ( empty( $ts ) || empty( $ts->name ) ) {
+                               continue;
+                       }
+
+                       $existing = GP::$translation_set->by_project_id_slug_and_locale( $project->id, $ts->slug, $ts->locale );
+                       if ( ! empty( $existing ) ) {
+                               continue;
+                       }
+
+                       $new_ts = GP::$translation_set->create( array(
+                               'project_id' => $project->id,
+                               'name'       => $ts->name,
+                               'locale'     => $ts->locale,
+                               'slug'       => $ts->slug,
+                       ) );
+
+                       if ( empty( $new_ts ) ) {
+                               WP_CLI::line( "Sorry, but couldn't create nonexistent https://translate.wordpress.org/projects/{$project->path}/{$ts->locale}/{$ts->slug}." );
+                       } else {
+                               WP_CLI::line( "Created https://translate.wordpress.org/projects/{$project->path}/{$ts->locale}/{$ts->slug}." );
+                       }
+               }
+       }
+
+       /**
+        * Retrieves the details of a plugin from the wordpress.org API.
+        *
+        * @param string $slug Slug of a plugin
+        * @return object|null JSON object on success, null on failure.
+        */
+       private function get_plugin_details( $slug ) {
+               $http_context = $this->get_http_context();
+               $json = @file_get_contents( "https://api.wordpress.org/plugins/info/1.0/{$slug}.json?fields=stable_tag", false, $http_context );
+
+               $details = $json && '{' == $json[0] ? json_decode( $json ) : null;
+
+               return $details;
+       }
+
+       /**
+        * Creates a stream context with a default HTTP user-agent.
+        *
+        * @return resource Stream context with a default HTTP user-agent.
+        */
+       private function get_http_context() {
+               return stream_context_create( array(
+                       'http' => array(
+                               'user_agent' => 'WordPress.org Translate',
+                       ),
+               ) );
+       }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpplugindirectorywporggpplugindirectoryphp"></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/wporg-gp-plugin-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-plugin-directory/wporg-gp-plugin-directory.php  2016-01-20 23:18:28 UTC (rev 2343)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-plugin-directory/wporg-gp-plugin-directory.php    2016-01-20 23:22:31 UTC (rev 2344)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,10 +28,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function register_cli_commands() {
</span><span class="cx" style="display: block; padding: 0 10px">                require_once __DIR__ . '/cli/import-plugin-translations.php';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        #       require_once __DIR__ . '/cli/add-plugin-project.php';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         require_once __DIR__ . '/cli/set-plugin-project.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                WP_CLI::add_command( 'wporg-translate import-plugin-translations', 'WPorg_GP_CLI_Import_Plugin_Translations' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        #       WP_CLI::add_command( 'wporg-translate add-plugin-project', 'WPorg_GP_CLI_Set_Plugin_Project' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         WP_CLI::add_command( 'wporg-translate set-plugin-project', 'WPorg_GP_CLI_Set_Plugin_Project' );
</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></pre>
</div>
</div>

</body>
</html>