|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * WPBootstrap Pager Class |
| 4 | + * |
| 5 | + * @author Jamin VanderBerg |
| 6 | + * @copyright 2014 Jamin VanderBerg |
| 7 | + * @version 1.1 |
| 8 | + * @license http://opensource.org/licenses/MIT |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Functions to Create Bootstrap Pagination Links for Wordpress Pages |
| 13 | + * |
| 14 | + * This class contains two methods for creating Bootstrap pagination |
| 15 | + * links in Wordpress. The method post_pager() creates pagination |
| 16 | + * links for posts and pages. The method archive_pager() creates |
| 17 | + * pagination links for archive-type pages (archives, search, and blog pages). |
| 18 | + * |
| 19 | + * I've created them as a class with static methods, because I prefer this |
| 20 | + * pattern. This design also allows for protected function accessible to both |
| 21 | + * methods. For simplifying calls to these methods, I've added the global |
| 22 | + * functions wpbootstrap_post_pager() and wpbootstrap_archive_pager() that call |
| 23 | + * the static methods of Bootstrap_Pager. |
| 24 | + * |
| 25 | + */ |
| 26 | +class WPBootstrap_Pager { |
| 27 | + /** |
| 28 | + * Override wp_link_pages to allow for Bootstrap classes |
| 29 | + * |
| 30 | + * This is a modified version of wp_link_pages(), designed to suit Bootstrap |
| 31 | + * pagination better. |
| 32 | + * |
| 33 | + * @param array $args See README for a list of arguments. |
| 34 | + * @return string Pager output |
| 35 | + */ |
| 36 | + public static function post_pager( $args = array() ) { |
| 37 | + $args = self::_parse_args( $args ); |
| 38 | + |
| 39 | + global $page, $numpages; |
| 40 | + |
| 41 | + return self::_pager_output( $page, $numpages, $args, 'post' ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Provide pager interface for archive-type pages |
| 46 | + * |
| 47 | + * This method is designed to create pagination similar to paginate_links(), |
| 48 | + * but using the same interface as post_pager() |
| 49 | + * |
| 50 | + * @param array $args See README for a list of arguments. |
| 51 | + * @return string Pager output |
| 52 | + */ |
| 53 | + public static function archive_pager( $args = array() ) { |
| 54 | + $args = self::_parse_args( $args ); |
| 55 | + |
| 56 | + global $wp_query; |
| 57 | + |
| 58 | + // Get the current page |
| 59 | + $paged = get_query_var( 'paged' ); |
| 60 | + if ( ! $paged ) { $paged = 1; } |
| 61 | + |
| 62 | + // Get the number of pages |
| 63 | + $max_num_pages = intval( $wp_query->max_num_pages ); |
| 64 | + |
| 65 | + return self::_pager_output( $paged, $max_num_pages, $args, 'archive' ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Parse Arguments |
| 70 | + * |
| 71 | + * This protected method provides a consistent list of default arguments |
| 72 | + * for all page types. |
| 73 | + * |
| 74 | + * @access protected |
| 75 | + * @param array $args |
| 76 | + * @return array |
| 77 | + */ |
| 78 | + protected static function _parse_args( $args ) { |
| 79 | + $defaults = array( |
| 80 | + 'before' => '<ul class="pagination">', |
| 81 | + 'after' => '</ul>', |
| 82 | + 'link_before' => '<li>', |
| 83 | + 'link_after' => '</li>', |
| 84 | + 'next_or_number' => 'number', |
| 85 | + 'nextpagelink' => '»', |
| 86 | + 'previouspagelink' => '«', |
| 87 | + 'pagelink' => '%', |
| 88 | + 'echo' => true, |
| 89 | + 'current_before' => '<li class="active">', |
| 90 | + 'current_after' => '</li>', |
| 91 | + 'currentlink' => '% <span class="sr-only">'. __('(current)') . '</span>', |
| 92 | + 'disabled_before' => '<li class="disabled">', |
| 93 | + 'disabled_after' => '</li>', |
| 94 | + 'previous_before' => '<li class="previous">', |
| 95 | + 'previous_after' => '</li>', |
| 96 | + 'next_before' => '<li class="next">', |
| 97 | + 'next_after' => '</li>', |
| 98 | + 'show_all' => false, |
| 99 | + 'end_size' => 1, |
| 100 | + 'mid_size' => 2, |
| 101 | + 'hellip' => '<li class="disabled"><a>…</a></li>', |
| 102 | + ); |
| 103 | + |
| 104 | + $args = wp_parse_args( $args, $defaults ); |
| 105 | + $args = apply_filters( 'wp_link_pages_args', $args ); |
| 106 | + |
| 107 | + return $args; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Create the pager output |
| 112 | + * |
| 113 | + * This protected method creates the pager output for each type of page. |
| 114 | + * It provides consistancy across all page types. |
| 115 | + * |
| 116 | + * @todo Break into _pager_output_next() and _pager_output_number() methods. |
| 117 | + * @param int $current_page Current page number |
| 118 | + * @param int $num_pages Total number of pages |
| 119 | + * @param array $args Arguments passed from main function |
| 120 | + * @param string $type Page type ('post' or 'archive') |
| 121 | + */ |
| 122 | + protected static function _pager_output( $current_page, $num_pages, $args, $type ) { |
| 123 | + $output = ''; |
| 124 | + |
| 125 | + if ( 1 < $num_pages ) { |
| 126 | + // Extract our arguments |
| 127 | + extract( $args, EXTR_SKIP ); |
| 128 | + |
| 129 | + if ( 'number' == $next_or_number ) { |
| 130 | + // Display page numbers instead of next/previous links. |
| 131 | + // This is the default behavior |
| 132 | + // This implementation includes next/previous links regardless. |
| 133 | + |
| 134 | + // Add the `before` argument |
| 135 | + $output .= $before; |
| 136 | + |
| 137 | + // Link to previous page |
| 138 | + if(1 == $current_page) { |
| 139 | + // This is page 1 |
| 140 | + // Use `disabled_before` and `disabled_after` |
| 141 | + $b = $disabled_before; $a = $disabled_after; |
| 142 | + } else { |
| 143 | + // This is not page 1 |
| 144 | + // Use normal `link_before` and `link_after` arguments |
| 145 | + $b = $link_before; $a = $link_after; |
| 146 | + } |
| 147 | + $output .= $b . self::_get_page_link($current_page - 1, $type) . $previouspagelink . '</a>' . $a; |
| 148 | + |
| 149 | + // Loop through the pages |
| 150 | + $dots = false; |
| 151 | + for ( $i = 1; $i < $num_pages + 1; $i++ ) { |
| 152 | + |
| 153 | + if ( $i == $current_page ) { |
| 154 | + // Current page |
| 155 | + // Use `currentlink` argument instead of `pagelink` |
| 156 | + $j = str_replace( '%', $i, $currentlink ); |
| 157 | + |
| 158 | + // User `current_before` and `current_after` |
| 159 | + $output .= $current_before |
| 160 | + . self::_get_page_link($i, $type) . $j . '</a>' |
| 161 | + . $current_after; |
| 162 | + $dots = true; |
| 163 | + } /* if ( $i == $current_page ) */ else { |
| 164 | + if ( $show_all |
| 165 | + || ( $i <= $end_size ) |
| 166 | + || ( ( $i >= ( $current_page - $mid_size ) ) && ( $i <= ( $current_page + $mid_size ) ) ) |
| 167 | + || ( $i > ( $num_pages - $end_size ) ) |
| 168 | + ) { |
| 169 | + // Display this page number |
| 170 | + $j = str_replace( '%', $i, $pagelink ); |
| 171 | + |
| 172 | + $output .= $link_before |
| 173 | + . self::_get_page_link($i, $type) . $j . '</a>' |
| 174 | + . $link_after; |
| 175 | + $dots = true; |
| 176 | + } /* if ( <display page> ) */ else if ( $dots ) { |
| 177 | + $output .= $hellip; |
| 178 | + $dots = false; |
| 179 | + } // else if ( $dots ) |
| 180 | + } // if ( $i == $current_page ) : else |
| 181 | + } // for ( $i = 1; $i < $num_pages + 1; $i++ ) |
| 182 | + |
| 183 | + // Link to next page |
| 184 | + if($current_page == $num_pages) { |
| 185 | + // This is the last page |
| 186 | + // Use `disabled_before` and `disabled_after` |
| 187 | + $b = $disabled_before; $a = $disabled_after; |
| 188 | + } else { |
| 189 | + // This not the last page |
| 190 | + // Use normal `link_before` and `link_after` arguments |
| 191 | + $b = $link_before; $a = $link_after; |
| 192 | + } |
| 193 | + $output .= $b . self::_get_page_link($current_page + 1, $type) . $nextpagelink . '</a>' . $a; |
| 194 | + |
| 195 | + // Add the final `after` argument |
| 196 | + $output .= $after; |
| 197 | + } else { // if ( 'number' == $next_or_number ) |
| 198 | + // Display next/previous links instead of page numbers |
| 199 | + |
| 200 | + // Add the `before` argument |
| 201 | + $output .= $before; |
| 202 | + |
| 203 | + // Link to previous page |
| 204 | + $i = $current_page - 1; |
| 205 | + if( $i ) { |
| 206 | + $output .= $previous_before |
| 207 | + . self::_get_page_link($i, $type) . $previouspagelink . '</a>' |
| 208 | + . $previous_after; |
| 209 | + } |
| 210 | + |
| 211 | + // Link to next page |
| 212 | + $i = $current_page + 1; |
| 213 | + if( $i <= $num_pages ) { |
| 214 | + $output .= $next_before |
| 215 | + . self::_get_page_link($i, $type) . $nextpagelink . '</a>' |
| 216 | + . $previous_after; |
| 217 | + } |
| 218 | + |
| 219 | + // Add the final `after` arguement |
| 220 | + $output .= $after; |
| 221 | + } // if ( 'number' == $next_or_number ) |
| 222 | + } // if ( $multipage ) |
| 223 | + |
| 224 | + if ( $echo ) { |
| 225 | + echo $output; |
| 226 | + } |
| 227 | + |
| 228 | + return $output; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Create the link for each page |
| 233 | + * |
| 234 | + * This protect member calls the relevant WordPress function to create |
| 235 | + * the appropriate page link for the page type. |
| 236 | + * |
| 237 | + * NOTE: This calls _wp_link_page, which is considered a __private__ |
| 238 | + * WordPress function. This may cause compatibility issues with future |
| 239 | + * versions of WordPress. |
| 240 | + * |
| 241 | + * @access protected |
| 242 | + * @param int $pagenum |
| 243 | + * @param string $type |
| 244 | + * @return string Page link |
| 245 | + */ |
| 246 | + protected static function _get_page_link( $pagenum, $type ) { |
| 247 | + if ( 'post' == $type ) { |
| 248 | + return _wp_link_page( $pagenum ); |
| 249 | + } else if ( 'archive' == $type ) { |
| 250 | + return '<a href="' . get_pagenum_link( $pagenum ) . '">'; |
| 251 | + } |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +/** |
| 256 | + * Creates Bootstrap pagination for posts and pages |
| 257 | + * |
| 258 | + * Calls BootstrapPager::post_pager() |
| 259 | + * |
| 260 | + * @param array $args See README for a list of arguments. |
| 261 | + * @return string Pager output |
| 262 | + */ |
| 263 | +function wpbootstrap_post_pager( $args = array() ) { |
| 264 | + WPBootstrap_Pager::post_pager( $args ); |
| 265 | +} |
| 266 | +/** |
| 267 | + * Creates Bootstrap pagination for archive-type pages, |
| 268 | + * which includes archives, search, and blog pages. |
| 269 | + * |
| 270 | + * Calls BootstrapPager::archive_pager() |
| 271 | + * |
| 272 | + * @param array $args See README for a list of arguments. |
| 273 | + * @return string Pager output |
| 274 | + */ |
| 275 | +function wpbootstrap_archive_pager( $args = array() ) { |
| 276 | + WPBootstrap_Pager::archive_pager( $args ); |
| 277 | +} |
0 commit comments