<!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>[6289] sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory: Themes: Use Cavalcade for Trac sync.</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/6289">6289</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/6289","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>obenland</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-12-19 17:39:27 +0000 (Tue, 19 Dec 2017)</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'>Themes: Use Cavalcade for Trac sync.

Props tellyworth, noisysocks, bpayton, obenland, dd32.
Fixes <a href="http://meta.trac.wordpress.org/ticket/3336">#3336</a>.</pre>

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

<h3>Added Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclassmanagerphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-manager.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclasstracsyncphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-trac-sync.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclassmanagerphp"></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/theme-directory/jobs/class-manager.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/jobs/class-manager.php                           (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-manager.php     2017-12-19 17:39:27 UTC (rev 6289)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,63 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Manager to wrap up all the logic for Cron tasks.
+ *
+ * @package WordPressdotorg\Theme_Directory\Jobs
+ */
+
+namespace WordPressdotorg\Theme_Directory\Jobs;
+
+/**
+ * Class Manager
+ *
+ * @package WordPressdotorg\Theme_Directory\Jobs
+ */
+class Manager {
+
+       /**
+        * Add all the actions for cron tasks and schedules.
+        */
+       public function __construct() {
+               // Register all the cron task handlers.
+               add_action( 'admin_init', [ $this, 'register_cron_tasks' ] );
+               add_filter( 'cron_schedules', [ $this, 'register_schedules' ] );
+
+               // The actual cron hooks.
+               add_action( 'theme_directory_trac_sync', [ __NAMESPACE__ . '\Trac_Sync', 'cron_trigger' ] );
+
+               // A cronjob to check cronjobs.
+               add_action( 'theme_directory_check_cronjobs', [ $this, 'register_cron_tasks' ] );
+       }
+
+       /**
+        * Register any cron schedules needed.
+        *
+        * @see wp_get_schedules() for core-registered schedules.
+        *
+        * @param array $schedules Registered schedule intervals.
+        * @return array
+        */
+       public function register_schedules( $schedules ) {
+               $schedules['every_15m']  = [
+                       'interval' => 15 * MINUTE_IN_SECONDS,
+                       'display'  => 'Every 15 minutes',
+               ];
+
+               return $schedules;
+       }
+
+       /**
+        * Queue all of our cron tasks.
+        *
+        * The jobs are queued for 1 minutes time to avoid recurring job failures from repeating too soon.
+        */
+       public function register_cron_tasks() {
+               if ( ! wp_next_scheduled( 'theme_directory_trac_sync' ) ) {
+                       wp_schedule_event( time() + 60, 'every_15m', 'theme_directory_trac_sync' );
+               }
+
+               if ( ! wp_next_scheduled( 'theme_directory_check_cronjobs' ) ) {
+                       wp_schedule_event( time() + 60, 'every_15m', 'theme_directory_check_cronjobs' );
+               }
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-manager.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectoryjobsclasstracsyncphp"></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/theme-directory/jobs/class-trac-sync.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-trac-sync.php                         (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-trac-sync.php   2017-12-19 17:39:27 UTC (rev 6289)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,129 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Watch Trac reviews and queue up jobs to update themes with review results.
+ *
+ * @package WordPressdotorg\Theme_Directory\Jobs
+ */
+
+namespace WordPressdotorg\Theme_Directory\Jobs;
+
+/**
+ * Class Trac_Sync
+ *
+ * @package WordPressdotorg\Theme_Directory\Jobs
+ */
+class Trac_Sync {
+
+       /**
+        * Trac statuses.
+        *
+        * @var array
+        */
+       protected static $stati = [
+               'new'  => [
+                       'status' => 'reopened',
+               ],
+               'live' => [
+                       'status'     => 'closed',
+                       'resolution' => 'live',
+               ],
+               'old'  => [
+                       'status'     => 'closed',
+                       'resolution' => 'not-approved',
+               ],
+       ];
+
+       /**
+        * The cron trigger for the svn import job.
+        */
+       public static function cron_trigger() {
+               if ( ! class_exists( 'Trac' ) ) {
+                       require_once ABSPATH . WPINC . '/class-IXR.php';
+                       require_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php';
+                       require_once dirname( __DIR__ ) . '/lib/class-trac.php';
+               }
+
+               $trac         = new \Trac( 'themetracbot', THEME_TRACBOT_PASSWORD, 'https://themes.trac.wordpress.org/login/xmlrpc' );
+               $last_request = get_option( 'wporg-themes-last-trac-sync', strtotime( '-2 days' ) );
+               update_option( 'wporg-themes-last-trac-sync', time() );
+
+               foreach ( self::$stati as $new_status => $args ) {
+                       // Get array of tickets.
+                       $tickets = (array) $trac->ticket_query( add_query_arg( wp_parse_args( $args, [
+                               'order'      => 'changetime',
+                               'changetime' => date( 'c', $last_request ),
+                               'desc'       => 1,
+                       ] ) ) );
+
+                       foreach ( $tickets as $ticket_id ) {
+                               // Get the theme associated with that ticket.
+                               $theme_id = self::get_theme_id( $ticket_id );
+                               if ( ! $theme_id ) {
+                                       continue;
+                               }
+
+                               // If there was a newer-version-uploaded, we have more than one version per ticket.
+                               $versions = array_keys( (array) get_post_meta( $theme_id, '_ticket_id', true ), $ticket_id, true );
+                               usort( $versions, 'version_compare' );
+                               $version = end( $versions );
+
+                               // There should always be a version associated with a ticket.
+                               if ( ! $version ) {
+                                       continue;
+                               }
+
+                               /*
+                                * Bail if the the theme has the wrong status.
+                                *
+                                * For approved and rejected themes, we bail if the current status is not
+                                * 'new' That can happen when there are additional ticket updates (like
+                                * comments) after the ticket was closed.
+                                *
+                                * For reopened tickets we bail if the version is already marked as 'new'.
+                                * This should only be the case if the ticket was closed and reopened before
+                                * this script was able to sync the closed status.
+                                */
+                               $current_status = wporg_themes_get_version_status( $theme_id, $version );
+                               if ( ( 'new' !== $new_status && 'new' !== $current_status ) || ( 'new' === $new_status && 'new' === $current_status ) ) {
+                                       continue;
+                               }
+
+                               // We don't need to set an already approved live version to live again.
+                               if ( 'live' === $current_status && 'live' === $new_status ) {
+                                       continue;
+                               }
+
+                               wporg_themes_update_version_status( $theme_id, $version, $new_status );
+                       }
+               }
+       }
+
+       /**
+        * Returns the ID of a theme associated with the passed ticket number.
+        *
+        * @param string $ticket_id Trac ticket number.
+        * @return int The post ID, or 0 if none can be found.
+        */
+       public static function get_theme_id( $ticket_id ) {
+               $post_id = 0;
+
+               $post_ids = new WP_Query( [
+                       'fields'         => 'ids',
+                       'post_status'    => 'any',
+                       'post_type'      => 'repopackage',
+                       'posts_per_page' => - 1,
+                       'meta_query'     => [
+                               [
+                                       'value'   => $ticket_id,
+                                       'compare' => 'IN',
+                               ],
+                       ],
+               ] );
+
+               if ( ! empty( $post_ids ) ) {
+                       $post_id = current( $post_ids );
+               }
+
+               return $post_id;
+       }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/jobs/class-trac-sync.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsthemedirectorythemedirectoryphp"></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/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/theme-directory/theme-directory.php      2017-12-19 17:14:51 UTC (rev 6288)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/theme-directory.php        2017-12-19 17:39:27 UTC (rev 6289)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -29,6 +29,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Load the query modifications needed for the directory.
</span><span class="cx" style="display: block; padding: 0 10px"> include __DIR__ . '/query-modifications.php';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Load repo jobs.
+include __DIR__ . '/jobs/class-manager.php';
+include __DIR__ . '/jobs/class-trac-sync.php';
+new WordPressdotorg\Theme_Directory\Jobs\Manager();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Things to change on activation.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span></span></pre>
</div>
</div>

</body>
</html>