<!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>[2566] sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/navigation.php: Handbook plugin: Add `show_nav_links()` template tag that is aware of the technique employed for that particular handbook for ordering pages.</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/2566">2566</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/2566","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coffee2code</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-02-24 18:22:26 +0000 (Wed, 24 Feb 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'>Handbook plugin: Add `show_nav_links()` template tag that is aware of the technique employed for that particular handbook for ordering pages.

Handbooks can use either the packaged Handbook Pages widget or a custom menu widget to order its pages. The new template tag detects which is in use and generates the navigation links accordingly.

Fixes <a href="http://meta.trac.wordpress.org/ticket/1595">#1595</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookincnavigationphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/navigation.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginshandbookincnavigationphp"></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/handbook/inc/navigation.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/handbook/inc/navigation.php      2016-02-24 18:16:35 UTC (rev 2565)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/navigation.php        2016-02-24 18:22:26 UTC (rev 2566)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,6 +1,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</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 providing navigation-related functionality.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Class providing navigation links.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @package handbook
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8,32 +8,170 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class WPorg_Handbook_Navigation {
</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">-         * Outputs previous and/or next post navigation links using the
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Is the handbook table of contents produced by the handbook pages widget?
+        *
+        * @access private
+        * @var bool
+        */
+       private static $using_pages_widget = false;
+
+       /**
+        * Initializes handbook navigation.
+        */
+       public static function init() {
+               add_action( 'init', array( __CLASS__, 'do_init' ), 100 );
+       }
+
+       /**
+        * Fires on 'init' action.
+        */
+       public static function do_init() {
+               // Note if the WPorg_Handbook_Pages_Widget widget is in use.
+               if ( is_active_widget( false, false, WPorg_Handbook_Pages_Widget::get_widget_id_base(), true ) ) {
+                       self::$using_pages_widget = true;
+               }
+       }
+
+       /**
+        * Outputs previous and next page navigation links (and wrapper markup).
+        *
+        * This function determines the method used for the handbook table of contents
+        * and outputs page navigation links accordingly.
+        *
+        * Recognizes use of either the WPorg_Handbook_Pages_Widget (as provided as
+        * part of this plugin) or a custom menu widget (by default, associated with
+        * the custom menu having the name "Table of Contents"). If both are present,
+        * the WPorg_Handbook_Pages_Widget is used.
+        *
+        * @param string $menu_name Optional. The name of the menu for the table of
+        *                          contents. Only applies if the handbook pages
+        *                          widget is not in use. Default 'Table of Contents'.
+        */
+       public static function show_nav_links( $menu_name = 'Table of Contents' ) {
+               if ( self::$using_pages_widget ) {
+                       self::navigate_via_handbook_pages_widget();
+               } else {
+                       self::navigate_via_menu( $menu_name );
+               }
+       }
+
+       /**
+        * Outputs previous and/or next page navigation links according to the active
+        * handbook widget settings.
+        *
+        * @access protected
+        */
+       protected static function navigate_via_handbook_pages_widget() {
+               // Get current post.
+               if ( ! $post = get_post() ) {
+                       return;
+               }
+
+               // Bail unless a handbook page.
+               if ( ! in_array( get_post_type(), WPorg_Handbook_Init::get_post_types() ) ) {
+                       return;
+               }
+
+               // Get settings for widget.
+               $sort_column = 'menu_order';
+               $exclude     = '';
+               $widget_options = get_option( 'widget_' . WPorg_Handbook_Pages_Widget::get_widget_id_base() );
+               foreach ( (array) $widget_options as $widget ) {
+                       if ( $widget && is_array( $widget ) ) {
+                               if ( ! empty( $widget['sortby']  ) ) { $sort_column = $widget['sortby'];  }
+                               if ( ! empty( $widget['exclude'] ) ) { $exclude     = $widget['exclude']; }
+                               break;
+                       }
+               }
+
+               // Cache key format is pages:{post type}:{sort column}(:{excluded})?.
+               $cache_key = 'pages:' . get_post_type() . ':' . $sort_column;
+               if ( $exclude ) {
+                       $cache_key .= ':' . str_replace( ' ', '', $exclude );
+               }
+               $cache_group = 'wporg_handbook:' . get_current_blog_id();
+
+               // Get the hierarchically and menu_order ordered list of handbook pages.
+               $handbook_pages = wp_cache_get( $cache_key, $cache_group );
+               if ( false === $handbook_pages ) {
+                       if ( 'menu_order' === $sort_column ) {
+                               $sort_column = 'menu_order, post_title';
+                       }
+
+                       $handbook_pages = get_pages( array(
+                               'echo'        => 0,
+                               'exclude'     => $exclude,
+                               'post_type'   => get_post_type(),
+                               'sort_column' => $sort_column,
+                               'sort_order'  => 'asc',
+                       ) );
+
+                       if ( $handbook_pages ) {
+                               wp_cache_add( $cache_key, $handbook_pages, $cache_group, 2 * MINUTE_IN_SECONDS );
+                       }
+               }
+
+               // Determine the previous and next handbook pages.
+               if ( $handbook_pages ) {
+                       $current_page  = wp_list_filter( $handbook_pages, array( 'ID' => get_the_ID() ) );
+                       $current_index = array_keys( $current_page );
+
+                       if ( $current_index ) {
+                               $current_index = $current_index[0];
+                               $current_page  = $current_page[ $current_index ];
+
+                               $prev = $next = false;
+
+                               if ( array_key_exists( $current_index - 1, $handbook_pages ) ) {
+                                       $prev = $handbook_pages[ $current_index - 1 ];
+                                       $prev = (object) array(
+                                               'url'   => get_the_permalink( $prev->ID ),
+                                               'title' => get_the_title( $prev->ID )
+                                       );
+                               }
+
+                               if ( array_key_exists( $current_index + 1, $handbook_pages ) ) {
+                                       $next = $handbook_pages[ $current_index + 1 ];
+                                       $next = (object) array(
+                                               'url'   => get_the_permalink( $next->ID ),
+                                               'title' => get_the_title( $next->ID )
+                                       );
+                               }
+
+                               self::output_navigation( $prev, $next );
+                       }
+               }
+       }
+
+       /**
+        * Outputs previous and/or next page navigation links using the
</ins><span class="cx" style="display: block; padding: 0 10px">          * specified menu to inform navigation ordering.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @access protected
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @param string $menu_name The name of the menu to use for nav ordering.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static function navigate_via_menu( $menu_name ) {
-               // Get the items for the specified menu
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected static function navigate_via_menu( $menu_name ) {
+               // Get current post.
+               if ( ! $post = get_post() ) {
+                       return;
+               }
+
+               // Get the items for the specified menu.
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! $menu_items = wp_get_nav_menu_items( $menu_name ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</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">-                // Get ids for all menu objects
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Get ids for all menu objects.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $menu_ids = wp_list_pluck( $menu_items, 'object_id' );
</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 current post
-               if ( ! $post = get_post() ) {
-                       return;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Index of current post in menu. Return if not in menu.
</span><span class="cx" style="display: block; padding: 0 10px">                $i = array_search( $post->ID, $menu_ids );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $i ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</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">-                // Find the previous post (note: preview menu item may not be a post)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Find the previous post (note: preview menu item may not be a post).
</ins><span class="cx" style="display: block; padding: 0 10px">                 $previous = null;
</span><span class="cx" style="display: block; padding: 0 10px">                for ( $n = $i-1; $n >= 0; $n-- ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( isset( $menu_items[ $n ] ) && is_a( $menu_items[ $n ], 'WP_Post' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -42,7 +180,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">-                // Find the next post (note: next menu item may not be a post)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Find the next post (note: next menu item may not be a post).
</ins><span class="cx" style="display: block; padding: 0 10px">                 $next = null;
</span><span class="cx" style="display: block; padding: 0 10px">                $max = count( $menu_items );
</span><span class="cx" style="display: block; padding: 0 10px">                for ( $n = $i+1; $n < $max; $n++ ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,9 +190,24 @@
</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">+                self::output_navigation( $previous, $next );
+       }
+
+       /**
+        * Outputs navigation markup for the specified previous and/or next pages.
+        *
+        * @access protected
+        *
+        * @param object $previous Object with the 'url' and 'title' attribute for the
+        *                         previous page.
+        * @param object $next     Object with the 'url' and 'title' attribute for the
+        *                         next page.
+        */
+       protected static function output_navigation( $previous, $next ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! $previous && ! $next ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</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">                <nav class="handbook-navigation" role="navigation">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,3 +238,5 @@
</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">+WPorg_Handbook_Navigation::init();
+
</ins></span></pre>
</div>
</div>

</body>
</html>