<!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>[3056] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory: Translate: Refactor the Theme Directory Bridge plugin to use an autoloader.</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/3056">3056</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/3056","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-05-02 09:14:37 +0000 (Mon, 02 May 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: Refactor the Theme Directory Bridge plugin to use an autoloader.</pre>
<h3>Modified Paths</h3>
<ul>
<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>
<h3>Added Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/class-plugin.php</a></li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/cli/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryinccliclasssetthemeprojectphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/cli/class-set-theme-project.php</a></li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/vendor/</li>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/vendor/wordpressdotorg/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryvendorwordpressdotorgclassautoloaderphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/vendor/wordpressdotorg/class-autoloader.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/cli/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryincclisetthemeprojectphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/cli/set-theme-project.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryincclasspluginphpfromrev3051sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectorywporggpthemedirectoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/class-plugin.php (from rev 3051, 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/inc/class-plugin.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/class-plugin.php 2016-05-02 09:14:37 UTC (rev 3056)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,54 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\Theme_Directory;
+
+use WP_CLI;
+
+class Plugin {
+
+ /**
+ * @var Plugin The singleton instance.
+ */
+ private static $instance;
+
+ /**
+ *
+ * @var Sync\Translation_Sync
+ */
+ public $translation_sync = null;
+
+ /**
+ * Returns always the same instance of this plugin.
+ *
+ * @return Plugin
+ */
+ public static function get_instance() {
+ if ( ! ( self::$instance instanceof Plugin ) ) {
+ self::$instance = new Plugin();
+ }
+ return self::$instance;
+ }
+
+ /**
+ * Instantiates a new Plugin object.
+ */
+ private function __construct() {
+ add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
+ }
+
+ /**
+ * Initializes the plugin.
+ */
+ public function plugins_loaded() {
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
+ $this->register_cli_commands();
+ }
+ }
+
+ /**
+ * Registers CLI commands if WP-CLI is loaded.
+ */
+ function register_cli_commands() {
+ WP_CLI::add_command( 'wporg-translate set-theme-project', __NAMESPACE__ . '\CLI\Set_Theme_Project' );
+ }
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryinccliclasssetthemeprojectphpfromrev3051sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryclisetthemeprojectphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/cli/class-set-theme-project.php (from rev 3051, 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/inc/cli/class-set-theme-project.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/cli/class-set-theme-project.php 2016-05-02 09:14:37 UTC (rev 3056)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,343 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\GlotPress\Theme_Directory\CLI;
+
+use GP;
+use MakePOT;
+use WP_CLI;
+use WP_CLI_Command;
+
+class 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().
+ * @return \GP_Project|false GP project on success, false on failure.
+ */
+ 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'] . "<br><br><a href='https://wordpress.org/themes/{$theme_slug}'>WordPress.org Theme Page</a>",
+ '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_htmlwpcontentpluginswporggpthemedirectoryincclisetthemeprojectphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/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 2016-04-29 18:56:04 UTC (rev 3051)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/inc/cli/set-theme-project.php 2016-05-02 09:14:37 UTC (rev 3056)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,335 +0,0 @@
</span><del style="background-color: #fdd; 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'] . "<br><br><a href='https://wordpress.org/themes/{$theme_slug}'>WordPress.org Theme Page</a>",
- '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}`;
- }
- }
-}
</del></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectoryvendorwordpressdotorgclassautoloaderphp"></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/vendor/wordpressdotorg/class-autoloader.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/vendor/wordpressdotorg/class-autoloader.php (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/vendor/wordpressdotorg/class-autoloader.php 2016-05-02 09:14:37 UTC (rev 3056)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,93 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Autoload;
+
+/**
+ * An Autoloader which respects WordPress's filename standards.
+ *
+ * @package WordPressdotorg\Autoload
+ */
+class Autoloader {
+
+ /**
+ * Namespace separator.
+ */
+ const NS_SEPARATOR = '\\';
+
+ /**
+ * The prefix to compare classes against.
+ *
+ * @var string
+ * @access protected
+ */
+ protected $prefix;
+
+ /**
+ * Length of the prefix string.
+ *
+ * @var int
+ * @access protected
+ */
+ protected $prefix_length;
+
+ /**
+ * Path to the file to be loaded.
+ *
+ * @var string
+ * @access protected
+ */
+ protected $path;
+
+ /**
+ * Constructor.
+ *
+ * @param string $prefix Prefix all classes have in common.
+ * @param string $path Path to the files to be loaded.
+ */
+ public function __construct( $prefix, $path ) {
+ $this->prefix = $prefix;
+ $this->prefix_length = strlen( $prefix );
+ $this->path = trailingslashit( $path );
+ }
+
+ /**
+ * Loads a class if it starts with `$this->prefix`.
+ *
+ * @param string $class The class to be loaded.
+ */
+ public function load( $class ) {
+ if ( strpos( $class, $this->prefix . self::NS_SEPARATOR ) !== 0 ) {
+ return;
+ }
+
+ // Strip prefix from the start (ala PSR-4)
+ $class = substr( $class, $this->prefix_length + 1 );
+ $class = strtolower( $class );
+ $file = '';
+
+ if ( false !== ( $last_ns_pos = strripos( $class, self::NS_SEPARATOR ) ) ) {
+ $namespace = substr( $class, 0, $last_ns_pos );
+ $namespace = str_replace( '_', '-', $namespace );
+ $class = substr( $class, $last_ns_pos + 1 );
+ $file = str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR;
+ }
+
+ $file .= 'class-' . str_replace( '_', '-', $class ) . '.php';
+
+ $path = $this->path . $file;
+
+ if ( file_exists( $path ) ) {
+ require $path;
+ }
+ }
+}
+
+/**
+ * Registers Autoloader's autoload function.
+ *
+ * @param string $prefix
+ * @param string $path
+ */
+function register_class_path( $prefix, $path ) {
+ $loader = new Autoloader( $prefix, $path );
+ spl_autoload_register( array( $loader, 'load' ) );
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggpthemedirectorywporggpthemedirectoryphp"></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-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 2016-05-01 22:27:41 UTC (rev 3055)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-theme-directory/wporg-gp-theme-directory.php 2016-05-02 09:14:37 UTC (rev 3056)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2,39 +2,25 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Plugin name: GlotPress: Theme Directory Bridge
</span><span class="cx" style="display: block; padding: 0 10px"> * Description: Provides CLI commands to import themes into translate.wordpress.org or to mark themes as inactive.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Version: 1.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Version: 2.0
</ins><span class="cx" style="display: block; padding: 0 10px"> * Author: WordPress.org
</span><span class="cx" style="display: block; padding: 0 10px"> * Author URI: http://wordpress.org/
</span><span class="cx" style="display: block; padding: 0 10px"> * License: GPLv2 or later
</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">-class WPorg_GP_Theme_Directory {
- public $master_project = 'wp-themes';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+namespace WordPressdotorg\GlotPress\Theme_Directory;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function __construct() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+use WordPressdotorg\Autoload;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( defined( 'WP_CLI' ) && WP_CLI ) {
- $this->register_cli_commands();
- }
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Store the root plugin file for usage with functions which use the plugin basename.
+define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * 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' );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! class_exists( '\WordPressdotorg\Autoload\Autoloader', false ) ) {
+ include __DIR__ . '/vendor/wordpressdotorg/class-autoloader.php';
</ins><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">-function wporg_gp_theme_directory() {
- global $wporg_gp_theme_directory;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Register an Autoloader for all files.
+Autoload\register_class_path( __NAMESPACE__, __DIR__ . '/inc' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 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' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Instantiate the Plugin.
+Plugin::get_instance();
</ins></span></pre>
</div>
</div>
</body>
</html>