| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\StaticSite\Engine\Discovery; |
| 4 |
|
| 5 |
/** |
| 6 |
* Pagination URLs of a list page. |
| 7 |
* |
| 8 |
* Pages come from the item count and posts_per_page. The URL format comes from the gateway (not a fixed "/page/N/"). |
| 9 |
* |
| 10 |
* @since 1.8.0 |
| 11 |
*/ |
| 12 |
class Pagination { |
| 13 |
|
| 14 |
/** |
| 15 |
* @param int $item_count Number of items in the list. |
| 16 |
* @param int $per_page Items on one page. |
| 17 |
* |
| 18 |
* @return int Number of pages, 1 or more. posts_per_page 0 or less (for example -1) shows all items on one page. |
| 19 |
*/ |
| 20 |
public static function page_count( $item_count, $per_page ) { |
| 21 |
if ( (int) $per_page <= 0 ) { |
| 22 |
return 1; |
| 23 |
} |
| 24 |
|
| 25 |
return max( 1, (int) ceil( (int) $item_count / (int) $per_page ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* URLs of page 2 and later pages. |
| 30 |
* |
| 31 |
* @param WordPressGateway $wp Gateway. |
| 32 |
* @param string $base_url Page 1 URL. |
| 33 |
* @param int $item_count Number of items in the list. |
| 34 |
* |
| 35 |
* @return \Generator<DiscoveredUrl> |
| 36 |
*/ |
| 37 |
public static function urls( WordPressGateway $wp, $base_url, $item_count ) { |
| 38 |
$last = self::page_count( $item_count, $wp->option( 'posts_per_page' ) ); |
| 39 |
|
| 40 |
for ( $page = 2; $page <= $last; $page++ ) { |
| 41 |
yield new DiscoveredUrl( $wp->paged_url( $base_url, $page ), 'pagination' ); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|