| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP\StaticSite\Engine\Discovery; |
| 4 |
|
| 5 |
/** |
| 6 |
* Term archives of all public taxonomies, with pagination. |
| 7 |
* |
| 8 |
* Generator with term ID batches behind the gateway. |
| 9 |
* |
| 10 |
* @since 1.8.0 |
| 11 |
*/ |
| 12 |
class TaxonomySource 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 Terms 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 |
foreach ( $this->wp->public_taxonomies() as $taxonomy ) { |
| 38 |
$after_id = 0; |
| 39 |
|
| 40 |
do { |
| 41 |
$rows = $this->wp->term_rows( $taxonomy, $after_id, $this->batch_size ); |
| 42 |
$full_batch = count( $rows ) === $this->batch_size; |
| 43 |
|
| 44 |
foreach ( $rows as $row ) { |
| 45 |
$after_id = $row['id']; |
| 46 |
$link = $this->wp->term_link( $row['id'], $taxonomy ); |
| 47 |
|
| 48 |
if ( null === $link ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
yield new DiscoveredUrl( $link, 'term', 'term', $row['id'] ); |
| 53 |
yield from Pagination::urls( $this->wp, $link, $row['count'] ); |
| 54 |
} |
| 55 |
} while ( $full_batch ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|