<!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>[327] sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php: Trac component pages: Archive page support.</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">
<dt>Revision</dt> <dd><a href="http://meta.trac.wordpress.org/changeset/327">327</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-01-29 07:39:12 +0000 (Wed, 29 Jan 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Trac component pages: Archive page support.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginstracnotificationstraccomponentsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginstracnotificationstraccomponentsphp"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php (326 => 327)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php  2014-01-29 07:38:43 UTC (rev 326)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/trac-notifications/trac-components.php     2014-01-29 07:39:12 UTC (rev 327)
</span><span class="lines">@@ -1,11 +1,15 @@
</span><span class="cx"> <?php
</span><span class="cx"> 
</span><span class="cx"> class Make_Core_Trac_Components {
</span><ins>+       const last_x_days = 7;
+
</ins><span class="cx">   function __construct() {
</span><span class="cx">          add_action( 'init', array( $this, 'init' ) );
</span><ins>+               add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
</ins><span class="cx">           add_action( 'admin_menu', array( $this, 'admin_menu' ) );
</span><span class="cx">          add_action( 'the_content', array( $this, 'the_content' ), 5 );
</span><span class="cx">          add_action( 'save_post_component', array( $this, 'save_post' ), 10, 2 );
</span><ins>+               add_action( 'wp_head', array( $this, 'wp_head' ) );
</ins><span class="cx">           $this->trac = $GLOBALS['wpdb'];
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -51,6 +55,14 @@
</span><span class="cx">          ) );
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       function pre_get_posts( $query ) {
+               if ( $query->is_main_query() && $query->is_post_type_archive( 'component' ) ) {
+                       $query->set( 'posts_per_page', -1 );
+                       $query->set( 'orderby', 'title' );
+                       $query->set( 'order', 'asc' );
+               }
+       }
+
</ins><span class="cx">   function page_is_component( $post ) {
</span><span class="cx">          $post = get_post( $post );
</span><span class="cx">          if ( $post->post_type != 'component' ) {
</span><span class="lines">@@ -106,25 +118,26 @@
</span><span class="cx">          remove_submenu_page( 'edit.php?post_type=component', 'post-new.php?post_type=component' );
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       function the_content( $content ) {
-               $post = get_post();
-               if ( ! $this->page_is_component( $post ) ) {
-                       return $content;
-               }       
-
-               if ( $post->post_parent ) {
-                       $top_level = '<h4>This is a subcomponent of the <a href="' . get_permalink( $post->post_parent ) . '">' . get_post( $post->post_parent )->post_title . '</a> component.</h4>';
-                       $content = $top_level . "\n\n" . $content;
</del><ins>+        function wp_head() {
+               if ( ! is_singular( 'component' ) && ! is_post_type_archive( 'component' ) ) {
+                       return;
</ins><span class="cx">           }
</span><del>-               ob_start();
</del><span class="cx"> ?>
</span><span class="cx"> <style>
</span><ins>+#toggle-compact-components { text-align: right }
+.component-info .compact { display: none }
+.compact-components .component-info { width: 49%; float: left; margin-right: 1% }
+.compact-components .compact { display: inline }
+.compact-components .trac-summary { display: none }
</ins><span class="cx"> .postcontent { padding-left: 0; }
</span><del>-.component .meta .actions { display: none }
-.component h3 { color: #333; font-size: 20px; margin: 30px 0 15px; font-weight: normal; }
</del><ins>+.component-info .meta .actions { display: none }
+.component-info h3 { color: #333; font-size: 20px; margin: 30px 0 15px; font-weight: normal; }
+.compact-components .component-info h3 { margin: 10px 0; }
</ins><span class="cx"> .trac-summary th, .trac-summary td { padding: 4px 8px; }
</span><span class="cx"> .trac-summary th { font-weight: bold; text-align: right }
</span><ins>+.trac-summary th.title { font-weight: normal; text-align: left; font-size: 14px }
</ins><span class="cx"> .trac-summary th a { font-weight: bold }
</span><ins>+.trac-summary th.title a { color: #000; }
</ins><span class="cx"> .trac-summary .count { text-align: right; min-width: 3em }
</span><span class="cx"> .trac-summary .zero { color: #ddd }
</span><span class="cx"> #main ul.maintainers { list-style: none; padding: 0; margin: 0 }
</span><span class="lines">@@ -137,8 +150,34 @@
</span><span class="cx"> .history.growing:before { content: "\f142"; color: red }
</span><span class="cx"> .history.shrinking:before { content: "\f140"; color: green }
</span><span class="cx"> </style>
</span><ins>+<script>
+jQuery( document ).ready( function( $ ) {
+       $( '#toggle-compact-components input' ).on( 'change', function() {
+               $( '#main' ).toggleClass( 'compact-components' );
+       });
+});
+</script>
</ins><span class="cx"> <?php
</span><ins>+       }
</ins><span class="cx"> 
</span><ins>+       function the_content( $content ) {
+               $post = get_post();
+               if ( ! $this->page_is_component( $post ) ) {
+                       return $content;
+               }       
+
+               ob_start();
+
+               if ( ! is_singular() ) {
+                       $this->ticket_table( $post->post_title );
+                       return ob_get_clean();
+               }
+
+               if ( $post->post_parent ) {
+                       $top_level = '<h4>This is a subcomponent of the <a href="' . get_permalink( $post->post_parent ) . '">' . get_post( $post->post_parent )->post_title . '</a> component.</h4>';
+                       $content = $top_level . "\n\n" . $content;
+               }
+
</ins><span class="cx">           $recent_posts = new WP_Query( array(
</span><span class="cx">                  'post_type' => 'post',
</span><span class="cx">                  'post_status' => 'publish',
</span><span class="lines">@@ -211,16 +250,19 @@
</span><span class="cx">                  }
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               $content .= "\n\n" . ob_get_clean();
</del><ins>+                $content .= "\n\n" . '<div class="component-info">' . ob_get_clean() . '</div>';
</ins><span class="cx">           return $content;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  function ticket_table( $component ) {
</span><span class="cx">          $type_filled = array_fill_keys( array( 'defect (bug)', 'enhancement', 'feature request', 'task (blessed)' ), 0 );
</span><span class="cx"> 
</span><del>-               // This query is designed to be persistently cached for a few minutes for *all* components. For now, just query directly with a WHERE.
-               $rows = $this->trac->get_results( $this->trac->prepare( "SELECT component, type, milestone, count(*) as count FROM ticket
-                       WHERE status <> 'closed' AND component = %s GROUP BY component, type, milestone ORDER BY component, type, milestone", $component ) );
</del><ins>+                $rows = wp_cache_get( 'trac_tickets_by_component_type_milestone' );
+               if ( ! $rows ) {
+                       $rows = $this->trac->get_results( "SELECT component, type, milestone, count(*) as count FROM ticket
+                               WHERE status <> 'closed' GROUP BY component, type, milestone ORDER BY component, type, milestone" );
+                       wp_cache_add( 'trac_tickets_by_component_type_milestone', $rows, '', 300 );
+               }
</ins><span class="cx"> 
</span><span class="cx">          $component_type_milestone = array();
</span><span class="cx">          foreach ( $rows as $row ) {
</span><span class="lines">@@ -235,11 +277,17 @@
</span><span class="cx">                  $component_milestone_type[ $row->component ][ $row->milestone ][ $row->type ] += $row->count;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               if ( ! $count = array_sum( $component_type[ $component ] ) ) {
-                       echo '<h3>No open tickets!</h3>';
</del><ins>+                if ( ! $component_count = array_sum( $component_type[ $component ] ) ) {
+                       if ( is_singular() ) {
+                               echo '<h3>No open tickets!</h3>';
+                       }
</ins><span class="cx">                   return;
</span><span class="cx">          }
</span><del>-               echo '<h3>' . sprintf( _n( '%s open ticket', '%s open tickets', $count ), $count ) . ' in the ' . $component . ' component</h3>';
</del><ins>+
+               if ( is_singular() ) {
+                       echo '<h3>' . sprintf( _n( '%s open ticket', '%s open tickets', $component_count ), $component_count ) . ' in the ' . $component . ' component</h3>';
+               }
+
</ins><span class="cx">           $history = $this->get_component_history( $component );
</span><span class="cx">          $direction = '';
</span><span class="cx">          if ( $history['change'] > 0 ) {
</span><span class="lines">@@ -255,8 +303,17 @@
</span><span class="cx">                  $history_line[] = $count . ' ' . $action;
</span><span class="cx">          }
</span><span class="cx"> 
</span><ins>+               $num_open_tickets_string = sprintf( _n( '%s open ticket', '%s open tickets', $component_count ), $component_count );
+
+               $last_x = "<strong class='compact'>" . $num_open_tickets_string . ".</strong> Last " . self::last_x_days . " days: <span title='" . wp_sprintf( '%l', $history_line ) . "'>";
+               $last_x .= sprintf( "%+d", $history['change'] ) . ' ' . _n( 'ticket', 'tickets', abs( $history['change'] ) );
+               $last_x .= '<span class="history ' . $direction . '"></span></span>' . "\n\n";
+
+               if ( ! is_singular() ) {
+                       echo $last_x;
+               }
</ins><span class="cx">           echo '<table class="trac-summary">';
</span><del>-               echo '<tr><th></th>';
</del><ins>+                echo '<tr><th class="title">' . $this->trac_query_link( $num_open_tickets_string, array( 'component' => $component ) ) . '</th>';
</ins><span class="cx">           foreach ( $component_type[ $component ] as $type => $count ) {
</span><span class="cx">                  if ( $count ) {
</span><span class="cx">                          echo '<th>' . $this->trac_query_link( $type, array( 'component' => $component, 'type' => $type, 'group' => 'milestone' ) ) . '</th>';
</span><span class="lines">@@ -276,10 +333,10 @@
</span><span class="cx">                  }
</span><span class="cx">                  echo '</tr>';
</span><span class="cx">          }
</span><del>-               echo '</table>';
-               echo "\n\nLast 30 days: <span title='" . wp_sprintf( '%l', $history_line ) . "'>";
-               echo sprintf( "%+d", $history['change'] ) . ' ' . _n( 'ticket', 'tickets', abs( $history['change'] ) );
-               echo '<span class="history ' . $direction . '"></span></span>' . "\n\n";
</del><ins>+                echo "</table>\n\n";
+               if ( is_singular() ) {
+                       echo $last_x;
+               }
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  function trac_content( $component ) {
</span><span class="lines">@@ -356,8 +413,8 @@
</span><span class="cx">          echo '</ul>';
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       function get_component_history( $component, $days = 30 ) {
-               $days_ago = ( time() - ( DAY_IN_SECONDS * $days ) ) * 1000000;
</del><ins>+        function get_component_history( $component ) {
+               $days_ago = ( time() - ( DAY_IN_SECONDS * self::last_x_days ) ) * 1000000;
</ins><span class="cx">           $closed_reopened = $this->trac->get_results( $this->trac->prepare( "SELECT newvalue, COUNT(DISTINCT ticket) as count
</span><span class="cx">                  FROM ticket_change tc INNER JOIN ticket t ON tc.ticket = t.id
</span><span class="cx">                  WHERE field = 'status' AND (newvalue = 'closed' OR newvalue = 'reopened')
</span></span></pre>
</div>
</div>

</body>
</html>