<!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>[11177] sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/class-wporg-themes-upload.php: Theme Directory: Split the upload handler into a generic import function, and a ZIP upload handler that wraps it.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/11177">11177</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/11177","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-08-13 03:10:37 +0000 (Fri, 13 Aug 2021)</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'>Theme Directory: Split the upload handler into a generic import function, and a ZIP upload handler that wraps it.

This allows introducing a few feature flags to enable/disable parts of the import process, such as not committing to SVN/creating Trac tickets when being used in a local dev environment.

Special note: Special-case themes now run through theme-check, they simply don't get blocked by Theme Check.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryclasswporgthemesuploadphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/class-wporg-themes-upload.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryclasswporgthemesuploadphp"></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/theme-directory/class-wporg-themes-upload.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/theme-directory/class-wporg-themes-upload.php    2021-08-13 02:25:40 UTC (rev 11176)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/class-wporg-themes-upload.php      2021-08-13 03:10:37 UTC (rev 11177)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -163,7 +163,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Processes the theme upload.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Processes a theme ZIP upload.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * Runs various tests, creates Trac ticket, repopackage post, and saves the files to the SVN repo.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -181,6 +181,48 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->create_tmp_dirs( $file_upload['name'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->unzip_package( $file_upload );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $result = $this->import();
+
+               if ( is_wp_error( $result ) ) {
+                       return $result;
+               }
+
+               return sprintf(
+                       /* translators: 1: theme name, 2: Trac ticket URL */
+                       __( 'Thank you for uploading %1$s to the WordPress Theme Directory. We&rsquo;ve sent you an email verifying that we&rsquo;ve received it. Feedback will be provided at <a href="%2$s">%2$s</a>', 'wporg-themes' ),
+                       $this->theme->display( 'Name' ),
+                       esc_url( 'https://themes.trac.wordpress.org/ticket/' . $this->trac_ticket->id )
+               );
+       }
+
+       /**
+        * Processes a theme import.
+        *
+        * @return WP_Error|true Error object on failure, true on success.
+        */
+       protected function import( $args = array() ) {
+               $args = wp_parse_args(
+                       $args,
+                       array(
+                               // Whether to commit the files to SVN.
+                               'commit_to_svn'       => true,
+                               // Whether Theme Check should maybe block the import.
+                               'run_themecheck'      => true,
+                               // Whether a failing Theme Check blocks the import.
+                               'block_on_themecheck' => true,
+                               // Whether to create a Trac ticket for this import.
+                               'create_trac_ticket'  => true,
+                       )
+               );
+
+               // When running locally, certain actions cannot be performed.
+               if ( ! defined( 'THEME_TRACBOT_PASSWORD' ) || ! THEME_TRACBOT_PASSWORD ) {
+                       $args['create_trac_ticket'] = false;
+               }
+               if ( ! defined( 'THEME_DROPBOX_PASSWORD' ) || ! THEME_DROPBOX_PASSWORD ) {
+                       $args['commit_to_svn'] = false;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $theme_files = $this->get_all_files( $this->theme_dir );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // First things first. Do we have something to work with?
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -231,10 +273,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Let's check some theme headers, shall we?
</span><span class="cx" style="display: block; padding: 0 10px">                $this->theme_name = $this->theme->get( 'Name' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Determine the theme slug (ascii only for compatibility) based on the name of the theme in the stylesheet
-               $this->theme_slug = remove_accents( $this->theme_name );
-               $this->theme_slug = preg_replace( '/%[a-f0-9]{2}/i', '', $this->theme_slug );
-               $this->theme_slug = sanitize_title_with_dashes( $this->theme_slug );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $this->theme_slug ) {
+                       // Determine the theme slug (ascii only for compatibility) based on the name of the theme in the stylesheet
+                       $this->theme_slug = remove_accents( $this->theme_name );
+                       $this->theme_slug = preg_replace( '/%[a-f0-9]{2}/i', '', $this->theme_slug );
+                       $this->theme_slug = sanitize_title_with_dashes( $this->theme_slug );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $this->theme_name || ! $this->theme_slug ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $error = __( 'The theme has no name.', 'wporg-themes' ) . ' ';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -263,8 +307,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Populate the theme post.
+               if ( ! $this->theme_post ) {
+                       $this->theme_post = $this->get_theme_post();
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Populate author.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->author = wp_get_current_user();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $this->author ) {
+                       if ( is_user_logged_in() ) {
+                               $this->author = wp_get_current_user();
+                       } elseif ( $this->theme_post ) {
+                               $this->author = get_user_by( 'id', $this->theme_post->post_author );
+                       }
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Make sure it doesn't use a slug deemed not to be used by the public.
</span><span class="cx" style="display: block; padding: 0 10px">                // This check must be run before `get_theme_post()` to account for "twenty" themes.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -280,9 +335,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Populate the theme post.
-               $this->theme_post = $this->get_theme_post();
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $theme_description = $this->strip_non_utf8( (string) $this->theme->get( 'Description' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( empty( $theme_description ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $error = __( 'The theme has no description.', 'wporg-themes' ) . ' ';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -371,7 +423,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Is there already a theme with the name name by a different author?
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $this->theme_post ) && $this->theme_post->post_author != $this->author->ID ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $this->theme_post ) && ! empty( $this->author ) && $this->theme_post->post_author != $this->author->ID ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $is_allowed_to_upload_for_theme = false;
</span><span class="cx" style="display: block; padding: 0 10px">                        if (
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -385,10 +437,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! $is_allowed_to_upload_for_theme ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                /* translators: 1: theme slug, 2: style.css */
</del><span class="cx" style="display: block; padding: 0 10px">                                 $style_errors->add(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'cannot_upload_theme',
</span><span class="cx" style="display: block; padding: 0 10px">                                        sprintf(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                /* translators: 1: theme slug, 2: style.css */
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 __( 'There is already a theme called %1$s by a different author. Please change the name of your theme in %2$s and upload it again.', 'wporg-themes' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                                '<code>' . $this->theme_slug . '</code>',
</span><span class="cx" style="display: block; padding: 0 10px">                                                '<code>style.css</code>'
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -456,12 +508,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $style_errors;
</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">-                // Don't send special themes through Theme Check.
-               if ( ! has_category( 'special-case-theme', $this->theme_post ) ) {
-                       // Pass it through Theme Check and see how great this theme really is.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Don't block special themes based on Theme Check.
+               if ( has_category( 'special-case-theme', $this->theme_post ) ) {
+                       $args['block_on_themecheck'] = false;
+               }
+
+               // Pass it through Theme Check and see how great this theme really is.
+               if ( $args['run_themecheck'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $result = $this->check_theme( $theme_files );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! $result ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! $result && $args['block_on_themecheck'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 // Log it to slack.
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->log_to_slack( 'blocked' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -481,36 +537,43 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Let's save everything and get things wrapped up.
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Create a new version in SVN.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $result = $this->add_to_svn();
-               if ( ! $result ) {
-                       return new WP_Error(
-                               'failed_to_add_to_svn',
-                               sprintf(
-                                       /* translators: %s: mailto link */
-                                       __( 'There was an error adding your theme to SVN. Please try again, if this error persists report the error to %s.', 'wporg-themes' ),
-                                       '<a href="mailto:themes@wordpress.org">themes@wordpress.org</a>'
-                               )
-                       );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $args['commit_to_svn'] ) {
+                       $result = $this->add_to_svn();
+                       if ( ! $result ) {
+                               return new WP_Error(
+                                       'failed_svn_commit',
+                                       sprintf(
+                                               /* translators: %s: mailto link */
+                                               __( 'There was an error adding your theme to SVN. Please try again, if this error persists report the error to %s.', 'wporg-themes' ),
+                                               '<a href="mailto:themes@wordpress.org">themes@wordpress.org</a>'
+                                       )
+                               );
+                       }
</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">-                // Get all Trac ticket information set up.
-               $this->prepare_trac_ticket();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Create a Trac ticket for this theme version.
+               if ( $args['create_trac_ticket'] ) {
+                       // Get all Trac ticket information set up.
+                       $this->prepare_trac_ticket();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Talk to Trac and let them know about our new version. Or new theme.
-               $ticket_id = $this->create_or_update_trac_ticket();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Talk to Trac and let them know about our new version. Or new theme.
+                       $ticket_id = $this->create_or_update_trac_ticket();
</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 ( ! $ticket_id  ) {
-                       // Since it's been added to SVN at this point, remove it from SVN to prevent future issues.
-                       $this->remove_from_svn( 'Trac ticket creation failed.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! $ticket_id  ) {
+                               if ( $args['commit_to_svn'] ) {
+                                       // Since it's been added to SVN at this point, remove it from SVN to prevent future issues.
+                                       $this->remove_from_svn( 'Trac ticket creation failed.' );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return new WP_Error(
-                               'failed_to_create_trac_ticket',
-                               sprintf(
-                                       /* translators: %s: mailto link */
-                                       __( 'There was an error creating a Trac ticket for your theme, please report this error to %s', 'wporg-themes' ),
-                                       '<a href="mailto:themes@wordpress.org">themes@wordpress.org</a>'
-                               )
-                       );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         return new WP_Error(
+                                       'failed_trac_ticket_creation',
+                                       sprintf(
+                                               /* translators: %s: mailto link */
+                                               __( 'There was an error creating a Trac ticket for your theme, please report this error to %s', 'wporg-themes' ),
+                                               '<a href="mailto:themes@wordpress.org">themes@wordpress.org</a>'
+                                       )
+                               );
+                       }
</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">                // Add a or update the Theme Directory entry for this theme.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -528,12 +591,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->trigger_e2e_run();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Success!
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return sprintf(
-                       /* translators: 1: theme name, 2: Trac ticket URL */
-                       __( 'Thank you for uploading %1$s to the WordPress Theme Directory. We&rsquo;ve sent you an email verifying that we&rsquo;ve received it. Feedback will be provided at <a href="%2$s">%2$s</a>', 'wporg-themes' ),
-                       $this->theme->display( 'Name' ),
-                       esc_url( 'https://themes.trac.wordpress.org/ticket/' . $ticket_id )
-               );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return true;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1111,6 +1169,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $version_status = 'live';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                wporg_themes_update_version_status( $post_id, $this->theme->get( 'Version' ), $version_status );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // refresh the post to avoid stale data.
+               if ( $post_id ) {
+                       $this->theme_post = $this->get_theme_post();
+               }
</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>