| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\StaticSite\Engine\Discovery; |
| 4 |
|
| 5 |
/** |
| 6 |
* The home page and the pagination of the posts list. |
| 7 |
* |
| 8 |
* Generator behind the gateway. The posts page gets the pagination, a static front page does not. |
| 9 |
* |
| 10 |
* @since 1.8.0 |
| 11 |
*/ |
| 12 |
class HomeSource implements UrlSource { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var WordPressGateway |
| 16 |
*/ |
| 17 |
private $wp; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param WordPressGateway $wp Gateway. |
| 21 |
*/ |
| 22 |
public function __construct( WordPressGateway $wp ) { |
| 23 |
$this->wp = $wp; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* {@inheritdoc} |
| 28 |
*/ |
| 29 |
public function urls() { |
| 30 |
$home = $this->wp->home_url(); |
| 31 |
$blog_url = self::blog_url( $this->wp ); |
| 32 |
|
| 33 |
yield new DiscoveredUrl( $home, 'home', null, null, $home === $blog_url ? $this->wp->newest_modified_gmt( [ 'post' ] ) : null ); |
| 34 |
|
| 35 |
if ( null !== $blog_url ) { |
| 36 |
yield from Pagination::urls( $this->wp, $blog_url, $this->wp->published_count( 'post' ) ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* URL of the list of posts: the home page, or the "posts page" when the front page is static. |
| 42 |
* |
| 43 |
* @param WordPressGateway $wp Gateway. |
| 44 |
* |
| 45 |
* @return string|null |
| 46 |
*/ |
| 47 |
public static function blog_url( WordPressGateway $wp ) { |
| 48 |
if ( 'page' !== $wp->option( 'show_on_front' ) || (int) $wp->option( 'page_on_front' ) < 1 ) { |
| 49 |
return $wp->home_url(); |
| 50 |
} |
| 51 |
|
| 52 |
$posts_page = (int) $wp->option( 'page_for_posts' ); |
| 53 |
|
| 54 |
return $posts_page > 0 ? $wp->permalink( $posts_page ) : null; |
| 55 |
} |
| 56 |
} |
| 57 |
|