| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presentations; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Pagination_Helper; |
| 6 |
use Yoast\WP\SEO\Models\Indexable; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Archive_Adjacent. |
| 10 |
* |
| 11 |
* Presentation object for indexables. |
| 12 |
* |
| 13 |
* @property Indexable $model The indexable. |
| 14 |
*/ |
| 15 |
trait Archive_Adjacent { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the Pagination_Helper instance. |
| 19 |
* |
| 20 |
* @var Pagination_Helper |
| 21 |
*/ |
| 22 |
protected $pagination; |
| 23 |
|
| 24 |
/** |
| 25 |
* Sets the helpers for the trait. |
| 26 |
* |
| 27 |
* @required |
| 28 |
* |
| 29 |
* @codeCoverageIgnore |
| 30 |
* |
| 31 |
* @param Pagination_Helper $pagination The pagination helper. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function set_archive_adjacent_helpers( Pagination_Helper $pagination ) { |
| 36 |
$this->pagination = $pagination; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Generates the rel prev. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function generate_rel_prev() { |
| 45 |
if ( $this->pagination->is_rel_adjacent_disabled() ) { |
| 46 |
return ''; |
| 47 |
} |
| 48 |
|
| 49 |
$current_page = \max( 1, $this->pagination->get_current_archive_page_number() ); |
| 50 |
// Check if there is a previous page. |
| 51 |
if ( $current_page === 1 ) { |
| 52 |
return ''; |
| 53 |
} |
| 54 |
// Check if the previous page is the first page. |
| 55 |
if ( $current_page === 2 ) { |
| 56 |
return $this->permalink; |
| 57 |
} |
| 58 |
|
| 59 |
return $this->pagination->get_paginated_url( $this->permalink, ( $current_page - 1 ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Generates the rel next. |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function generate_rel_next() { |
| 68 |
if ( $this->pagination->is_rel_adjacent_disabled() ) { |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
$current_page = \max( 1, $this->pagination->get_current_archive_page_number() ); |
| 73 |
if ( $this->pagination->get_number_of_archive_pages() <= $current_page ) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
|
| 77 |
return $this->pagination->get_paginated_url( $this->permalink, ( $current_page + 1 ) ); |
| 78 |
} |
| 79 |
} |
| 80 |
|