| @@ -31,12 +31,9 @@ | ||
| 31 | 31 | * |
| 32 | 32 | * @param WP_Rewrite_Wrapper $wp_rewrite_wrapper The rewrite wrapper. |
| 33 | 33 | * @param WP_Query_Wrapper $wp_query_wrapper The query wrapper. |
| 34 | 34 | */ |
| 35 | - public function __construct( | |
| 36 | - WP_Rewrite_Wrapper $wp_rewrite_wrapper, | |
| 37 | - WP_Query_Wrapper $wp_query_wrapper | |
| 38 | - ) { | |
| 35 | + public function __construct( WP_Rewrite_Wrapper $wp_rewrite_wrapper, WP_Query_Wrapper $wp_query_wrapper ) { | |
| 39 | 36 | $this->wp_rewrite_wrapper = $wp_rewrite_wrapper; |
| 40 | 37 | $this->wp_query_wrapper = $wp_query_wrapper; |
| 41 | 38 | } |
| 42 | 39 | |
| @@ -48,9 +45,9 @@ | ||
| 48 | 45 | public function is_rel_adjacent_disabled() { |
| 49 | 46 | /** |
| 50 | 47 | * Filter: 'wpseo_disable_adjacent_rel_links' - Allows disabling of Yoast adjacent links if this is being handled by other code. |
| 51 | 48 | * |
| 52 | - * @api bool $links_generated Indicates if other code has handled adjacent links. | |
| 49 | + * @param bool $links_generated Indicates if other code has handled adjacent links. | |
| 53 | 50 | */ |
| 54 | 51 | return \apply_filters( 'wpseo_disable_adjacent_rel_links', false ); |
| 55 | 52 | } |
| 56 | 53 | |
| @@ -66,9 +63,31 @@ | ||
| 66 | 63 | */ |
| 67 | 64 | public function get_paginated_url( $url, $page, $add_pagination_base = true, $pagination_query_name = 'page' ) { |
| 68 | 65 | $wp_rewrite = $this->wp_rewrite_wrapper->get(); |
| 69 | 66 | |
| 70 | - if ( $wp_rewrite->using_permalinks() ) { | |
| 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 | + | |
| 71 | 90 | $url = \trailingslashit( $url ); |
| 72 | 91 | if ( $add_pagination_base ) { |
| 73 | 92 | $url .= \trailingslashit( $wp_rewrite->pagination_base ); |
| 74 | 93 | } |
| @@ -95,11 +114,20 @@ | ||
| 95 | 114 | * |
| 96 | 115 | * @return int The current archive page. |
| 97 | 116 | */ |
| 98 | 117 | public function get_current_archive_page_number() { |
| 99 | - $wp_query = $this->wp_query_wrapper->get_main_query(); | |
| 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 | + } | |
| 100 | 123 | |
| 101 | - return (int) $wp_query->get( 'paged' ); | |
| 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; | |
| 102 | 130 | } |
| 103 | 131 | |
| 104 | 132 | /** |
| 105 | 133 | * Returns the current page for paged post types. |
| @@ -108,8 +136,14 @@ | ||
| 108 | 136 | */ |
| 109 | 137 | public function get_current_post_page_number() { |
| 110 | 138 | $wp_query = $this->wp_query_wrapper->get_main_query(); |
| 111 | 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 | + | |
| 112 | 146 | return (int) $wp_query->get( 'page' ); |
| 113 | 147 | } |
| 114 | 148 | |
| 115 | 149 | /** |
| @@ -123,8 +157,48 @@ | ||
| 123 | 157 | if ( $page_number > 1 ) { |
| 124 | 158 | return $page_number; |
| 125 | 159 | } |
| 126 | 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 | + | |
| 127 | 167 | // Get the page number for a page in a paginated post. |
| 128 | 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 ''; | |
| 129 | 203 | } |
| 130 | 204 | } |