| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Wrappers\WP_Query_Wrapper; |
| 6 |
use Yoast\WP\SEO\Wrappers\WP_Rewrite_Wrapper; |
| 7 |
|
| 8 |
/** |
| 9 |
* A helper object for pagination. |
| 10 |
* |
| 11 |
* Used for the canonical URL and the rel "next" and "prev" meta tags. |
| 12 |
*/ |
| 13 |
class Pagination_Helper { |
| 14 |
|
| 15 |
/** |
| 16 |
* Holds the WP rewrite wrapper instance. |
| 17 |
* |
| 18 |
* @var WP_Rewrite_Wrapper WP_Rewrite wrapper. |
| 19 |
*/ |
| 20 |
protected $wp_rewrite_wrapper; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the WP query wrapper instance. |
| 24 |
* |
| 25 |
* @var WP_Query_Wrapper WP_Query wrapper. |
| 26 |
*/ |
| 27 |
protected $wp_query_wrapper; |
| 28 |
|
| 29 |
/** |
| 30 |
* Pagination_Helper constructor. |
| 31 |
* |
| 32 |
* @param WP_Rewrite_Wrapper $wp_rewrite_wrapper The rewrite wrapper. |
| 33 |
* @param WP_Query_Wrapper $wp_query_wrapper The query wrapper. |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
WP_Rewrite_Wrapper $wp_rewrite_wrapper, |
| 37 |
WP_Query_Wrapper $wp_query_wrapper |
| 38 |
) { |
| 39 |
$this->wp_rewrite_wrapper = $wp_rewrite_wrapper; |
| 40 |
$this->wp_query_wrapper = $wp_query_wrapper; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Checks whether adjacent rel links are disabled. |
| 45 |
* |
| 46 |
* @return bool Whether adjacent rel links are disabled or not. |
| 47 |
*/ |
| 48 |
public function is_rel_adjacent_disabled() { |
| 49 |
/** |
| 50 |
* Filter: 'wpseo_disable_adjacent_rel_links' - Allows disabling of Yoast adjacent links if this is being handled by other code. |
| 51 |
* |
| 52 |
* @api bool $links_generated Indicates if other code has handled adjacent links. |
| 53 |
*/ |
| 54 |
return \apply_filters( 'wpseo_disable_adjacent_rel_links', false ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Builds a paginated URL. |
| 59 |
* |
| 60 |
* @param string $url The un-paginated URL of the current archive. |
| 61 |
* @param string $page The page number to add on to $url for the $link tag. |
| 62 |
* @param bool $add_pagination_base Optional. Whether to add the pagination base (`page`) to the url. |
| 63 |
* @param string $pagination_query_name Optional. The name of the query argument that holds the current page. |
| 64 |
* |
| 65 |
* @return string The paginated URL. |
| 66 |
*/ |
| 67 |
public function get_paginated_url( $url, $page, $add_pagination_base = true, $pagination_query_name = 'page' ) { |
| 68 |
$wp_rewrite = $this->wp_rewrite_wrapper->get(); |
| 69 |
|
| 70 |
if ( $wp_rewrite->using_permalinks() ) { |
| 71 |
$url = \trailingslashit( $url ); |
| 72 |
if ( $add_pagination_base ) { |
| 73 |
$url .= \trailingslashit( $wp_rewrite->pagination_base ); |
| 74 |
} |
| 75 |
|
| 76 |
return \user_trailingslashit( $url . $page ); |
| 77 |
} |
| 78 |
|
| 79 |
return \add_query_arg( $pagination_query_name, $page, \user_trailingslashit( $url ) ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Gets the number of archive pages. |
| 84 |
* |
| 85 |
* @return int The number of archive pages. |
| 86 |
*/ |
| 87 |
public function get_number_of_archive_pages() { |
| 88 |
$wp_query = $this->wp_query_wrapper->get_query(); |
| 89 |
|
| 90 |
return (int) $wp_query->max_num_pages; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the current page for paged archives. |
| 95 |
* |
| 96 |
* @return int The current archive page. |
| 97 |
*/ |
| 98 |
public function get_current_archive_page_number() { |
| 99 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 100 |
|
| 101 |
return (int) $wp_query->get( 'paged' ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns the current page for paged post types. |
| 106 |
* |
| 107 |
* @return int The current post page. |
| 108 |
*/ |
| 109 |
public function get_current_post_page_number() { |
| 110 |
$wp_query = $this->wp_query_wrapper->get_main_query(); |
| 111 |
|
| 112 |
return (int) $wp_query->get( 'page' ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns the current page number. |
| 117 |
* |
| 118 |
* @return int The current page number. |
| 119 |
*/ |
| 120 |
public function get_current_page_number() { |
| 121 |
// Get the page number for an archive page. |
| 122 |
$page_number = \get_query_var( 'paged', 1 ); |
| 123 |
if ( $page_number > 1 ) { |
| 124 |
return $page_number; |
| 125 |
} |
| 126 |
|
| 127 |
// Get the page number for a page in a paginated post. |
| 128 |
return \get_query_var( 'page', 1 ); |
| 129 |
} |
| 130 |
} |
| 131 |
|