Changeset 6084
- Timestamp:
- 11/07/2017 10:33:38 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/navigation.php
r2566 r6084 49 49 */ 50 50 public static function show_nav_links( $menu_name = 'Table of Contents' ) { 51 $prev = $next = false; 52 51 53 if ( self::$using_pages_widget ) { 52 self::navigate_via_handbook_pages_widget();54 $adjacent = self::get_adjacent_posts_via_handbook_pages_widget(); 53 55 } else { 54 self::navigate_via_menu( $menu_name ); 55 } 56 $adjacent = self::get_adjacent_posts_via_menu( $menu_name ); 57 } 58 59 if ( is_array( $adjacent ) ) { 60 list( $prev, $next ) = $adjacent; 61 } 62 63 self::output_navigation( $prev, $next ); 56 64 } 57 65 … … 61 69 * 62 70 * @access protected 63 */ 64 protected static function navigate_via_handbook_pages_widget() { 71 * 72 * @param int|WP_Post $post Optional. The post object or ID. Default is 73 * global post. 74 * @param string $type Optional. The type of adjacent post(s) to 75 * return. One of 'prev', 'next', or 'both'. 76 * Default 'both'. 77 * @param int|WP_Post $source_post Optional. The post requesting an adjacent 78 * post, if not $post itself. Default ''. 79 * @return null|array { 80 * The previous and next post. 81 * 82 * @type false|object $prev Object containing 'title' and 'url' for previous post. 83 * @type false|object $prev Object containing 'title' and 'url' for next post. 84 * } 85 */ 86 protected static function get_adjacent_posts_via_handbook_pages_widget( $post = '', $type = 'both', $source_post = '' ) { 65 87 // Get current post. 66 if ( ! $post = get_post( ) ) {88 if ( ! $post = get_post( $post ) ) { 67 89 return; 68 90 } 69 91 70 92 // Bail unless a handbook page. 71 if ( ! in_array( get_post_type(), WPorg_Handbook_Init::get_post_types() ) ) { 72 return; 93 if ( ! wporg_is_handbook_post_type( get_post_type( $post ) ) ) { 94 return; 95 } 96 97 // Determine which adjacent post(s) to find. 98 if ( in_array( $type, array( 'prev', 'next' ) ) ) { 99 if ( 'prev' === $type ) { 100 $get_prev = true; 101 $get_next = false; 102 } else { 103 $get_prev = false; 104 $get_next = true; 105 } 106 } else { 107 $get_prev = $get_next = true; 73 108 } 74 109 … … 86 121 87 122 // Cache key format is pages:{post type}:{sort column}(:{excluded})?. 88 $cache_key = 'pages:' . get_post_type( ) . ':' . $sort_column;123 $cache_key = 'pages:' . get_post_type( $post ) . ':' . $sort_column; 89 124 if ( $exclude ) { 90 125 $cache_key .= ':' . str_replace( ' ', '', $exclude ); 91 126 } 92 127 $cache_group = 'wporg_handbook:' . get_current_blog_id(); 128 129 $post_status = array( 'publish' ); 130 if ( current_user_can( get_post_type_object( get_post_type( $post ) )->cap->read_private_posts ) ) { 131 $post_status[] = 'private'; 132 } 93 133 94 134 // Get the hierarchically and menu_order ordered list of handbook pages. … … 96 136 if ( false === $handbook_pages ) { 97 137 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', 138 $sort_column = array( 'menu_order' => 'ASC', 'post_title' => 'ASC' ); 139 } 140 141 $parent_id = wp_get_post_parent_id( $post ); 142 143 $handbook_pages = get_posts( array( 144 'exclude' => $exclude, 145 'post_parent' => $parent_id, 146 'post_status' => $post_status, 147 'post_type' => get_post_type( $post ), 148 'orderby' => $sort_column, 149 'order' => 'ASC', 150 'posts_per_page' => -1, 107 151 ) ); 108 152 … … 112 156 } 113 157 158 $prev = $next = false; 159 114 160 // Determine the previous and next handbook pages. 115 161 if ( $handbook_pages ) { 116 $current_page = wp_list_filter( $handbook_pages, array( 'ID' => get_the_ID()) );162 $current_page = wp_list_filter( $handbook_pages, array( 'ID' => $post->ID ) ); 117 163 $current_index = array_keys( $current_page ); 118 164 119 if ( $current_index ) {165 if ( false !== $current_index ) { 120 166 $current_index = $current_index[0]; 121 167 $current_page = $current_page[ $current_index ]; 122 168 123 $prev = $next = false; 124 125 if ( array_key_exists( $current_index - 1, $handbook_pages ) ) { 169 // The previous post is the post's immediate sibling. 170 // It's debatable if it should be the last leaf node of the previous 171 // sibling's last child (since if you are on that leaf node, the next 172 // post is the current post). That's what the custom menu-based 173 // navigation does, but it is easier to do there than here. 174 if ( $get_prev && array_key_exists( $current_index - 1, $handbook_pages ) ) { 126 175 $prev = $handbook_pages[ $current_index - 1 ]; 127 176 $prev = (object) array( 128 177 'url' => get_the_permalink( $prev->ID ), 129 'title' => get_the_title( $prev->ID ) 178 'title' => get_the_title( $prev->ID ), 130 179 ); 131 180 } 132 181 133 if ( array_key_exists( $current_index + 1, $handbook_pages ) ) { 182 // If no previous yet, then it's the parent, if there is one. 183 if ( $get_prev && ! $prev && $parent_id ) { 184 $prev = (object) array( 185 'url' => get_the_permalink( $parent_id ), 186 'title' => get_the_title( $parent_id ), 187 ); 188 } 189 190 // The next post may be this post's first child. 191 if ( $get_next && ! $source_post ) { 192 $children = get_posts( array( 193 'exclude' => $exclude, 194 'post_parent' => $post->ID, 195 'post_status' => $post_status, 196 'post_type' => get_post_type( $post ), 197 'orderby' => $sort_column, 198 'order' => 'ASC', 199 'posts_per_page' => 1, 200 ) ); 201 if ( $children ) { 202 $next = (object) array( 203 'url' => get_the_permalink( $children[0]->ID ), 204 'title' => get_the_title( $children[0]->ID ), 205 ); 206 } 207 } 208 209 // If no next yet, get next sibling. 210 if ( $get_next && ! $next && array_key_exists( $current_index + 1, $handbook_pages ) ) { 134 211 $next = $handbook_pages[ $current_index + 1 ]; 135 212 $next = (object) array( 136 213 'url' => get_the_permalink( $next->ID ), 137 'title' => get_the_title( $next->ID ) 214 'title' => get_the_title( $next->ID ), 138 215 ); 139 216 } 140 217 141 self::output_navigation( $prev, $next ); 142 } 143 } 218 // If no next yet, recursively check for a next ancestor. 219 if ( $get_next && ! $next && $parent_id ) { 220 $parent_next = self::get_adjacent_posts_via_handbook_pages_widget( $parent_id, 'next', $post->ID ); 221 if ( is_array( $parent_next ) ) { 222 $next = $parent_next[1]; 223 } 224 } 225 226 } 227 } 228 229 return array( $prev, $next ); 144 230 } 145 231 … … 151 237 * 152 238 * @param string $menu_name The name of the menu to use for nav ordering. 153 */ 154 protected static function navigate_via_menu( $menu_name ) { 239 * @param int|WP_Post $post Optional. The post object or ID. Default is global 240 * post. 241 * @return null|array { 242 * The previous and next post. 243 * 244 * @type false|object $prev Object containing 'title' and 'url' for previous post. 245 * @type false|object $prev Object containing 'title' and 'url' for next post. 246 * } 247 */ 248 protected static function get_adjacent_posts_via_menu( $menu_name, $post = '' ) { 155 249 // Get current post. 156 if ( ! $post = get_post( ) ) {250 if ( ! $post = get_post( $post ) ) { 157 251 return; 158 252 } … … 172 266 } 173 267 174 // Find the previous post (note: previ ewmenu item may not be a post).268 // Find the previous post (note: previous menu item may not be a post). 175 269 $previous = null; 176 270 for ( $n = $i-1; $n >= 0; $n-- ) { … … 191 285 } 192 286 193 self::output_navigation( $previous, $next );287 return array( $previous, $next ); 194 288 } 195 289 … … 240 334 241 335 WPorg_Handbook_Navigation::init(); 242
Note: See TracChangeset
for help on using the changeset viewer.