PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / helpers / pagination-helper.php

pagination-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/helpers/pagination-helper.php

205 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( WP_Rewrite_Wrapper $wp_rewrite_wrapper, WP_Query_Wrapper $wp_query_wrapper ) {
36 $this->wp_rewrite_wrapper = $wp_rewrite_wrapper;
37 $this->wp_query_wrapper = $wp_query_wrapper;
38 }
39
40 /**
41 * Checks whether adjacent rel links are disabled.
42 *
43 * @return bool Whether adjacent rel links are disabled or not.
44 */
45 public function is_rel_adjacent_disabled() {
46 /**
47 * Filter: 'wpseo_disable_adjacent_rel_links' - Allows disabling of Yoast adjacent links if this is being handled by other code.
48 *
49 * @param bool $links_generated Indicates if other code has handled adjacent links.
50 */
51 return \apply_filters( 'wpseo_disable_adjacent_rel_links', false );
52 }
53
54 /**
55 * Builds a paginated URL.
56 *
57 * @param string $url The un-paginated URL of the current archive.
58 * @param string $page The page number to add on to $url for the $link tag.
59 * @param bool $add_pagination_base Optional. Whether to add the pagination base (`page`) to the url.
60 * @param string $pagination_query_name Optional. The name of the query argument that holds the current page.
61 *
62 * @return string The paginated URL.
63 */
64 public function get_paginated_url( $url, $page, $add_pagination_base = true, $pagination_query_name = 'page' ) {
65 $wp_rewrite = $this->wp_rewrite_wrapper->get();
66
67 $key_query_loop = $this->get_key_query_loop();
68 if ( $key_query_loop ) {
69 $pagination_query_name = $key_query_loop;
70 }
71
72 if ( $wp_rewrite->using_permalinks() && ! $key_query_loop ) {
73 $url_parts = \wp_parse_url( $url );
74 $has_url_params = \array_key_exists( 'query', $url_parts );
75
76 if ( $has_url_params ) {
77 // We need to first remove the query params, before potentially adding the pagination parts.
78 \wp_parse_str( $url_parts['query'], $query_parts );
79
80 $url = \trailingslashit( \remove_query_arg( \array_keys( $query_parts ), $url ) );
81
82 if ( $add_pagination_base ) {
83 $url .= \trailingslashit( $wp_rewrite->pagination_base );
84 }
85
86 // We can now re-add the query params, after appending the last pagination parts.
87 return \add_query_arg( $query_parts, \user_trailingslashit( $url . $page ) );
88 }
89
90 $url = \trailingslashit( $url );
91 if ( $add_pagination_base ) {
92 $url .= \trailingslashit( $wp_rewrite->pagination_base );
93 }
94
95 return \user_trailingslashit( $url . $page );
96 }
97
98 return \add_query_arg( $pagination_query_name, $page, \user_trailingslashit( $url ) );
99 }
100
101 /**
102 * Gets the number of archive pages.
103 *
104 * @return int The number of archive pages.
105 */
106 public function get_number_of_archive_pages() {
107 $wp_query = $this->wp_query_wrapper->get_query();
108
109 return (int) $wp_query->max_num_pages;
110 }
111
112 /**
113 * Returns the current page for paged archives.
114 *
115 * @return int The current archive page.
116 */
117 public function get_current_archive_page_number() {
118 $wp_query = $this->wp_query_wrapper->get_main_query();
119 $page_number = (int) $wp_query->get( 'paged' );
120 if ( $page_number > 1 ) {
121 return $page_number;
122 }
123
124 $query_loop_page_number = $this->get_page_number_from_query_loop();
125
126 if ( $query_loop_page_number ) {
127 return $query_loop_page_number;
128 }
129 return 0;
130 }
131
132 /**
133 * Returns the current page for paged post types.
134 *
135 * @return int The current post page.
136 */
137 public function get_current_post_page_number() {
138 $wp_query = $this->wp_query_wrapper->get_main_query();
139
140 $query_loop_page_number = $this->get_page_number_from_query_loop();
141
142 if ( $query_loop_page_number ) {
143 return $query_loop_page_number;
144 }
145
146 return (int) $wp_query->get( 'page' );
147 }
148
149 /**
150 * Returns the current page number.
151 *
152 * @return int The current page number.
153 */
154 public function get_current_page_number() {
155 // Get the page number for an archive page.
156 $page_number = \get_query_var( 'paged', 1 );
157 if ( $page_number > 1 ) {
158 return $page_number;
159 }
160
161 $query_loop_page_number = $this->get_page_number_from_query_loop();
162
163 if ( $query_loop_page_number ) {
164 return $query_loop_page_number;
165 }
166
167 // Get the page number for a page in a paginated post.
168 return \get_query_var( 'page', 1 );
169 }
170
171 /**
172 * Returns the key of the query loop.
173 *
174 * @return string The key of the query loop.
175 */
176 public function get_key_query_loop() {
177 $regex_pattern = '/^query-\d+-page$/';
178 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not form data.
179 foreach ( $_GET as $key => $value ) {
180 if ( \preg_match( $regex_pattern, $key ) ) {
181 return $key;
182 }
183 }
184 return '';
185 }
186
187 /**
188 * Returns the page number from the query loop.
189 *
190 * @return string The page number from the query loop.
191 */
192 public function get_page_number_from_query_loop() {
193 $key_query_loop = $this->get_key_query_loop();
194
195 if ( $key_query_loop ) {
196 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.NonceVerification.Recommended -- Validated in get_key_query_loop().
197 $page_number = (int) $_GET[ $key_query_loop ];
198 if ( $page_number > 1 ) {
199 return $page_number;
200 }
201 }
202 return '';
203 }
204 }
205