<!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>[677] sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer: Code Reference: Improvements to inline source code display.</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/677">677</a></dd>
<dt>Author</dt> <dd>coffee2code</dd>
<dt>Date</dt> <dd>2014-06-05 21:46:52 +0000 (Thu, 05 Jun 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Code Reference: Improvements to inline source code display. Fixes <a href="http://meta.trac.wordpress.org/ticket/176">#176</a>

* More efficient implementation of get_source_code()
* Add get_source_code_root_dir() which gets 'wp_parser_root_import_dir' option value if set by parser, else ABSPATH
* Add post_type_has_source_code()
* Use 'wporg' as the text domain
* Slight tweak to JS height comparison to ensure functions that just fit don't trigger expansion handling
* Allow x scrollbar for .source-code-container and adjust heights to account for its possible display
* Add extra height to collapsed state of .source-code-container to partially show the first hidden line to better indicate there is more code
* Add right border to .source-code-container so it is easier to tell if code is being clipped
* Change 'View source' link to 'View source code:' label with explicit links to inline source and/or WP Trac code browser
* Source code is stored in post meta; parse and store it as requested if it isn't
* Sanity check to ensure proper conditions exist for parsing
* Add optional 'force_parse' arg to get_source_code() to force reparsing of source file</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdevelopercontentreferencephp">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-reference.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdevelopercontentwpparserhookphp">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-wp-parser-hook.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperfunctionsphp">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperinctemplatetagsphp">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperjsfunctionreferencejs">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/function-reference.js</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperscssmainscss">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/scss/main.scss</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperstylesheetsmaincss">sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/stylesheets/main.css</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdevelopercontentreferencephp"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-reference.php (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-reference.php        2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-reference.php   2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -28,8 +28,15 @@
</span><span class="cx">  if ( ! empty( $source_file ) ) :
</span><span class="cx">          ?>
</span><span class="cx">          <section class="source">
</span><del>-                       <p><strong><?php _e( 'Source file:', 'wporg' ); ?> </strong><a href="<?php echo get_source_file_archive_link( $source_file ); ?>"><?php echo esc_html( $source_file ); ?></a></p>
-                       <p><a href="<?php echo get_source_file_link(); ?>"><?php _e( 'View source&hellip;', 'wporg' ); ?></a></p>
</del><ins>+                        <p>
+                               <strong><?php _e( 'Source file:', 'wporg' ); ?></strong>
+                               <a href="<?php echo get_source_file_archive_link( $source_file ); ?>"><?php echo esc_html( $source_file ); ?></a><br />
+                               <strong><?php _e( 'View source code:', 'wporg' ); ?></strong>
+                               <?php if ( post_type_has_source_code() ) { ?>
+                               <a href="#source-code"><?php _e( 'below', 'wporg' ); ?></a> or
+                               <?php } ?>
+                               <a href="<?php echo get_source_file_link(); ?>"><?php _e( 'on the WP Trac code browser&hellip;', 'wporg' ); ?></a>
+                       </p>
</ins><span class="cx">           </section>
</span><span class="cx">  <?php endif; ?>
</span><span class="cx"> 
</span><span class="lines">@@ -116,14 +123,15 @@
</span><span class="cx">          <?php endif;
</span><span class="cx">  endif; ?>
</span><span class="cx"> 
</span><del>-       <?php if ( 'wp-parser-function' === get_post_type() || 'wp-parser-method' === get_post_type() ) : ?>
</del><ins>+        <?php if ( post_type_has_source_code() ) : ?>
</ins><span class="cx">           <hr />
</span><ins>+               <a id="source-code"></a>
</ins><span class="cx">           <section class="source-content">
</span><span class="cx">                  <h2><?php _e( 'Source', 'wporg' ); ?></h2>
</span><span class="cx">                  <div class="source-code-container">
</span><span class="cx">                          <pre class="brush: php; toolbar: false;"><?php echo esc_html( get_source_code() ); ?></pre>
</span><span class="cx">                  </div>
</span><del>-                       <p><a href="#" class="show-complete-source"><?php _e( 'View full source code&hellip;', 'wporg-developer' ); ?></a></p>
</del><ins>+                        <p><a href="#" class="show-complete-source"><?php _e( 'View full source code&hellip;', 'wporg' ); ?></a></p>
</ins><span class="cx">           </section>
</span><span class="cx">  <?php endif; ?>
</span><span class="cx"> 
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdevelopercontentwpparserhookphp"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-wp-parser-hook.php (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-wp-parser-hook.php   2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/content-wp-parser-hook.php      2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -26,8 +26,12 @@
</span><span class="cx">  if ( ! empty( $source_file ) ) :
</span><span class="cx">          ?>
</span><span class="cx">          <section class="source">
</span><del>-                       <p><strong><?php _e( 'Source file:', 'wporg' ); ?> </strong><a href="<?php echo get_source_file_archive_link( $source_file ); ?>"><?php echo esc_html( $source_file ); ?></a></p>
-                       <p><a href="<?php echo get_source_file_link(); ?>"><?php _e( 'View source&hellip;', 'wporg' ); ?></a></p>
</del><ins>+                        <p>
+                               <strong><?php _e( 'Source file:', 'wporg' ); ?></strong>
+                               <a href="<?php echo get_source_file_archive_link( $source_file ); ?>"><?php echo esc_html( $source_file ); ?></a><br />
+                               <strong><?php _e( 'View source code:', 'wporg' ); ?></strong>
+                               <a href="<?php echo get_source_file_link(); ?>"><?php _e( 'on the WP Trac code browser&hellip;', 'wporg' ); ?></a>
+                       </p>
</ins><span class="cx">           </section>
</span><span class="cx">  <?php endif; ?>
</span><span class="cx"> 
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperfunctionsphp"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php        2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php   2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -343,7 +343,7 @@
</span><span class="cx">  wp_enqueue_script( 'wporg-developer-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
</span><span class="cx">  wp_enqueue_script( 'wporg-developer-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
</span><span class="cx"> 
</span><del>-       if ( 'wp-parser-function' === get_post_type() || 'wp-parser-method' === get_post_type() ) {
</del><ins>+        if ( post_type_has_source_code() ) {
</ins><span class="cx">           wp_enqueue_script( 'wporg-developer-function-reference', get_template_directory_uri() . '/js/function-reference.js', array( 'jquery', 'syntaxhighlighter-core', 'syntaxhighlighter-brush-php' ), '20140515', true );
</span><span class="cx">          wp_enqueue_style( 'syntaxhighlighter-core' );
</span><span class="cx">          wp_enqueue_style( 'syntaxhighlighter-theme-default' );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperinctemplatetagsphp"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php        2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php   2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -639,33 +639,89 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><del>-        * Retrieve source code for a function
</del><ins>+         * Does the post type have source code?
</ins><span class="cx">    *
</span><del>-        * @param int $post_id
</del><ins>+         * @param  string  Optional. The post type name. If blank, assumes current post type.
</ins><span class="cx">    *
</span><del>-        * @return string The sourc
</del><ins>+         * @return boolean
</ins><span class="cx">    */
</span><del>-       function get_source_code( $post_id = null ) {
</del><ins>+        function post_type_has_source_code( $post_type = null ) {
+               $post_type                   = $post_type ? $post_type : get_post_type();
+               $post_types_with_source_code = array( 'wp-parser-method', 'wp-parser-function' );
</ins><span class="cx"> 
</span><ins>+               return in_array( $post_type, $post_types_with_source_code );
+       }
+
+       /**
+        * Retrieve the root directory of the parsed WP code.
+        *
+        * If the option 'wp_parser_root_import_dir' (as set by the parser) is not
+        * set, then assume ABSPATH.
+        *
+        * @return string
+        */
+       function get_source_code_root_dir() {
+               $root_dir = get_option( 'wp_parser_root_import_dir' );
+
+               return $root_dir ? trailingslashit( $root_dir ) : ABSPATH;
+       }
+
+       /**
+        * Retrieve source code for a function or method.
+        *
+        * @param int  $post_id     Optional. The post ID.
+        * @param bool $force_parse Optional. Ignore potential value in post meta and reparse source file for source code?
+        *
+        * @return string The source code.
+        */
+       function get_source_code( $post_id = null, $force_parse = false ) {
+
</ins><span class="cx">           if ( empty( $post_id ) ) {
</span><span class="cx">                  $post_id = get_the_ID();
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // Get the total file sourcecode.
</del><ins>+                // Get the source code stored in post meta.
+               $meta_key = '_wp-parser_source_code';
+               if ( ! $force_parse && $source_code = get_post_meta( $post_id, $meta_key, true ) ) {
+                       return $source_code;
+               }
+
+               /* Source code hasn't been stored in post meta, so parse source file to get it. */
+
+               // Get the name of the source file.
</ins><span class="cx">           $source_file = get_source_file( $post_id );
</span><span class="cx"> 
</span><del>-               // Put the total source code in an array.
-               $total_source_code = file_get_contents( ABSPATH . $source_file );
-               $total_source_code = explode( "\n", $total_source_code );
-
</del><span class="cx">           // Get the start and end lines.
</span><del>-               $start_line = get_post_meta( $post_id, '_wp-parser_line_num', true ) - 1;
-               $end_line =   get_post_meta( $post_id, '_wp-parser_end_line_num', true );
</del><ins>+                $start_line = intval( get_post_meta( $post_id, '_wp-parser_line_num', true ) ) - 1;
+               $end_line   = intval( get_post_meta( $post_id, '_wp-parser_end_line_num', true ) );
</ins><span class="cx"> 
</span><del>-               // Get the correct source code.
-               $source_code = array_slice( $total_source_code, $start_line, $end_line - $start_line );
</del><ins>+                // Sanity check to ensure proper conditions exist for parsing
+               if ( ! $source_file || ! $start_line || ! $end_line || ( $start_line > $end_line ) ) {
+                       return '';
+               }
</ins><span class="cx"> 
</span><del>-               return implode( "\n", $source_code );
</del><ins>+                // Find just the relevant source code
+               $source_code = '';
+               $handle = @fopen( get_source_code_root_dir() . $source_file, 'r' );
+               if ( $handle ) {
+                       $line = -1;
+                       while ( ! feof( $handle ) ) {
+                               $line++;
+                               $source_line = fgets( $handle );
+                               if ( $line > $end_line ) {
+                                       break;
+                               }
+                               if ( $line < $start_line ) {
+                                       continue;
+                               }
+                               $source_code .= $source_line;
+                       }
+                       fclose( $handle );
+               }
+
+               update_post_meta( $post_id, $meta_key, $source_code );
+
+               return $source_code;
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperjsfunctionreferencejs"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/function-reference.js (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/function-reference.js     2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/js/function-reference.js        2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -9,7 +9,7 @@
</span><span class="cx">  function showCompleteSource( e ) {
</span><span class="cx">          e.preventDefault();
</span><span class="cx"> 
</span><del>-               var heightGoal = $sourceCodeTable.height() + 17;
</del><ins>+                var heightGoal = $sourceCodeTable.height() + 47; // takes into consideration potential x-scrollbar
</ins><span class="cx"> 
</span><span class="cx">          $sourceCodeContainer.animate( { height: heightGoal + 'px' } );
</span><span class="cx"> 
</span><span class="lines">@@ -27,17 +27,19 @@
</span><span class="cx"> 
</span><span class="cx">          $sourceCodeTable = $sourceContent.find( 'table' );
</span><span class="cx"> 
</span><del>-               if ( 186 < $sourceCodeTable.height() ) {
</del><ins>+                if ( 188 < $sourceCodeTable.height() ) {
</ins><span class="cx"> 
</span><span class="cx">                  // Do this with javascript so javascript-less can enjoy the total sourcecode
</span><del>-                       // 1em (margin) + 10 * 17px. Lines are 1.1em which rounds to 17px: calc( 1em + 17px * 10 ).
-                       $sourceCodeContainer.css( { height: '186px' } );
</del><ins>+                        // 1em (margin) + 20 * 17px. Lines are 1.1em which rounds to 17px: calc( 1em + 17px * 20 ).
+                       // Extra 10px added to partially show next line so it's clear there is more.
+                       $( '.source-code-container' ).css( { height: '196px' } );
</ins><span class="cx"> 
</span><span class="cx">                  $showCompleteSource = $( '.show-complete-source' );
</span><span class="cx"> 
</span><span class="cx">                  $showCompleteSource.show();
</span><span class="cx">                  $showCompleteSource.on( 'click', showCompleteSource );
</span><span class="cx">          }
</span><ins>+
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  $( onLoad );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperscssmainscss"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/scss/main.scss (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/scss/main.scss       2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/scss/main.scss  2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -856,7 +856,9 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  .source-code-container {
</span><del>-               overflow: hidden;
</del><ins>+                border-right: 1px solid #dfdfdf;
+               overflow-x: auto;
+               overflow-y: hidden;
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  .show-complete-source {
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentthemespubwporgdeveloperstylesheetsmaincss"></a>
<div class="modfile"><h4>Modified: sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/stylesheets/main.css (676 => 677)</h4>
<pre class="diff"><span>
<span class="info">--- sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/stylesheets/main.css 2014-06-05 21:42:29 UTC (rev 676)
+++ sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/stylesheets/main.css    2014-06-05 21:46:52 UTC (rev 677)
</span><span class="lines">@@ -1068,7 +1068,9 @@
</span><span class="cx">   padding: 20px;
</span><span class="cx"> }
</span><span class="cx"> .devhub-wrap .source-code-container {
</span><del>-  overflow: hidden;
</del><ins>+  border-right: 1px solid #dfdfdf;
+  overflow-x: auto;
+  overflow-y: hidden;
</ins><span class="cx"> }
</span><span class="cx"> .devhub-wrap .show-complete-source {
</span><span class="cx">   display: none;
</span></span></pre>
</div>
</div>

</body>
</html>