Changeset 2566
- Timestamp:
- 02/24/2016 06:22:26 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/navigation.php
r2507 r2566 1 1 <?php 2 2 /** 3 * Class providing navigation -related functionality.3 * Class providing navigation links. 4 4 * 5 5 * @package handbook … … 9 9 10 10 /** 11 * Outputs previous and/or next post navigation links using the 11 * Is the handbook table of contents produced by the handbook pages widget? 12 * 13 * @access private 14 * @var bool 15 */ 16 private static $using_pages_widget = false; 17 18 /** 19 * Initializes handbook navigation. 20 */ 21 public static function init() { 22 add_action( 'init', array( __CLASS__, 'do_init' ), 100 ); 23 } 24 25 /** 26 * Fires on 'init' action. 27 */ 28 public static function do_init() { 29 // Note if the WPorg_Handbook_Pages_Widget widget is in use. 30 if ( is_active_widget( false, false, WPorg_Handbook_Pages_Widget::get_widget_id_base(), true ) ) { 31 self::$using_pages_widget = true; 32 } 33 } 34 35 /** 36 * Outputs previous and next page navigation links (and wrapper markup). 37 * 38 * This function determines the method used for the handbook table of contents 39 * and outputs page navigation links accordingly. 40 * 41 * Recognizes use of either the WPorg_Handbook_Pages_Widget (as provided as 42 * part of this plugin) or a custom menu widget (by default, associated with 43 * the custom menu having the name "Table of Contents"). If both are present, 44 * the WPorg_Handbook_Pages_Widget is used. 45 * 46 * @param string $menu_name Optional. The name of the menu for the table of 47 * contents. Only applies if the handbook pages 48 * widget is not in use. Default 'Table of Contents'. 49 */ 50 public static function show_nav_links( $menu_name = 'Table of Contents' ) { 51 if ( self::$using_pages_widget ) { 52 self::navigate_via_handbook_pages_widget(); 53 } else { 54 self::navigate_via_menu( $menu_name ); 55 } 56 } 57 58 /** 59 * Outputs previous and/or next page navigation links according to the active 60 * handbook widget settings. 61 * 62 * @access protected 63 */ 64 protected static function navigate_via_handbook_pages_widget() { 65 // Get current post. 66 if ( ! $post = get_post() ) { 67 return; 68 } 69 70 // Bail unless a handbook page. 71 if ( ! in_array( get_post_type(), WPorg_Handbook_Init::get_post_types() ) ) { 72 return; 73 } 74 75 // Get settings for widget. 76 $sort_column = 'menu_order'; 77 $exclude = ''; 78 $widget_options = get_option( 'widget_' . WPorg_Handbook_Pages_Widget::get_widget_id_base() ); 79 foreach ( (array) $widget_options as $widget ) { 80 if ( $widget && is_array( $widget ) ) { 81 if ( ! empty( $widget['sortby'] ) ) { $sort_column = $widget['sortby']; } 82 if ( ! empty( $widget['exclude'] ) ) { $exclude = $widget['exclude']; } 83 break; 84 } 85 } 86 87 // Cache key format is pages:{post type}:{sort column}(:{excluded})?. 88 $cache_key = 'pages:' . get_post_type() . ':' . $sort_column; 89 if ( $exclude ) { 90 $cache_key .= ':' . str_replace( ' ', '', $exclude ); 91 } 92 $cache_group = 'wporg_handbook:' . get_current_blog_id(); 93 94 // Get the hierarchically and menu_order ordered list of handbook pages. 95 $handbook_pages = wp_cache_get( $cache_key, $cache_group ); 96 if ( false === $handbook_pages ) { 97 if ( 'menu_order' === $sort_column ) { 98 $sort_column = 'menu_order, post_title'; 99 } 100 101 $handbook_pages = get_pages( array( 102 'echo' => 0, 103 'exclude' => $exclude, 104 'post_type' => get_post_type(), 105 'sort_column' => $sort_column, 106 'sort_order' => 'asc', 107 ) ); 108 109 if ( $handbook_pages ) { 110 wp_cache_add( $cache_key, $handbook_pages, $cache_group, 2 * MINUTE_IN_SECONDS ); 111 } 112 } 113 114 // Determine the previous and next handbook pages. 115 if ( $handbook_pages ) { 116 $current_page = wp_list_filter( $handbook_pages, array( 'ID' => get_the_ID() ) ); 117 $current_index = array_keys( $current_page ); 118 119 if ( $current_index ) { 120 $current_index = $current_index[0]; 121 $current_page = $current_page[ $current_index ]; 122 123 $prev = $next = false; 124 125 if ( array_key_exists( $current_index - 1, $handbook_pages ) ) { 126 $prev = $handbook_pages[ $current_index - 1 ]; 127 $prev = (object) array( 128 'url' => get_the_permalink( $prev->ID ), 129 'title' => get_the_title( $prev->ID ) 130 ); 131 } 132 133 if ( array_key_exists( $current_index + 1, $handbook_pages ) ) { 134 $next = $handbook_pages[ $current_index + 1 ]; 135 $next = (object) array( 136 'url' => get_the_permalink( $next->ID ), 137 'title' => get_the_title( $next->ID ) 138 ); 139 } 140 141 self::output_navigation( $prev, $next ); 142 } 143 } 144 } 145 146 /** 147 * Outputs previous and/or next page navigation links using the 12 148 * specified menu to inform navigation ordering. 13 149 * 150 * @access protected 151 * 14 152 * @param string $menu_name The name of the menu to use for nav ordering. 15 153 */ 16 public static function navigate_via_menu( $menu_name ) { 17 // Get the items for the specified menu 154 protected static function navigate_via_menu( $menu_name ) { 155 // Get current post. 156 if ( ! $post = get_post() ) { 157 return; 158 } 159 160 // Get the items for the specified menu. 18 161 if ( ! $menu_items = wp_get_nav_menu_items( $menu_name ) ) { 19 162 return; 20 163 } 21 164 22 // Get ids for all menu objects 165 // Get ids for all menu objects. 23 166 $menu_ids = wp_list_pluck( $menu_items, 'object_id' ); 24 25 // Get current post26 if ( ! $post = get_post() ) {27 return;28 }29 167 30 168 // Index of current post in menu. Return if not in menu. … … 34 172 } 35 173 36 // Find the previous post (note: preview menu item may not be a post) 174 // Find the previous post (note: preview menu item may not be a post). 37 175 $previous = null; 38 176 for ( $n = $i-1; $n >= 0; $n-- ) { … … 43 181 } 44 182 45 // Find the next post (note: next menu item may not be a post) 183 // Find the next post (note: next menu item may not be a post). 46 184 $next = null; 47 185 $max = count( $menu_items ); … … 53 191 } 54 192 193 self::output_navigation( $previous, $next ); 194 } 195 196 /** 197 * Outputs navigation markup for the specified previous and/or next pages. 198 * 199 * @access protected 200 * 201 * @param object $previous Object with the 'url' and 'title' attribute for the 202 * previous page. 203 * @param object $next Object with the 'url' and 'title' attribute for the 204 * next page. 205 */ 206 protected static function output_navigation( $previous, $next ) { 55 207 if ( ! $previous && ! $next ) { 56 208 return; 57 209 } 210 58 211 ?> 59 212 … … 86 239 } 87 240 241 WPorg_Handbook_Navigation::init(); 242
Note: See TracChangeset
for help on using the changeset viewer.