| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\StaticSite\Engine\Discovery; |
| 4 |
|
| 5 |
/** |
| 6 |
* Year and month archives of posts. Day archives are not included, because there can be many. |
| 7 |
* |
| 8 |
* DISTINCT month queries in batches. |
| 9 |
* |
| 10 |
* @since 1.8.0 |
| 11 |
*/ |
| 12 |
class DateArchiveSource implements UrlSource { |
| 13 |
|
| 14 |
/** |
| 15 |
* @var WordPressGateway |
| 16 |
*/ |
| 17 |
private $wp; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
private $batch_size; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param WordPressGateway $wp Gateway. |
| 26 |
* @param int $batch_size Months in one query. |
| 27 |
*/ |
| 28 |
public function __construct( WordPressGateway $wp, $batch_size = 500 ) { |
| 29 |
$this->wp = $wp; |
| 30 |
$this->batch_size = max( 1, (int) $batch_size ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* {@inheritdoc} |
| 35 |
*/ |
| 36 |
public function urls() { |
| 37 |
$before_key = 0; |
| 38 |
$last_year = null; |
| 39 |
|
| 40 |
do { |
| 41 |
$rows = $this->wp->month_rows( $before_key, $this->batch_size ); |
| 42 |
$full_batch = count( $rows ) === $this->batch_size; |
| 43 |
|
| 44 |
foreach ( $rows as $row ) { |
| 45 |
$before_key = $row['year'] * 100 + $row['month']; |
| 46 |
|
| 47 |
if ( $last_year !== $row['year'] ) { |
| 48 |
$last_year = $row['year']; |
| 49 |
|
| 50 |
yield new DiscoveredUrl( $this->wp->year_link( $row['year'] ), 'archive' ); |
| 51 |
} |
| 52 |
|
| 53 |
yield new DiscoveredUrl( $this->wp->month_link( $row['year'], $row['month'] ), 'archive' ); |
| 54 |
} |
| 55 |
} while ( $full_batch ); |
| 56 |
} |
| 57 |
} |
| 58 |
|