| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Crawl_Cleanup_Helper. |
| 7 |
* |
| 8 |
* Used by the Crawl_Cleanup_Permalinks class. |
| 9 |
*/ |
| 10 |
class Crawl_Cleanup_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* The current page helper |
| 14 |
* |
| 15 |
* @var Current_Page_Helper |
| 16 |
*/ |
| 17 |
private $current_page_helper; |
| 18 |
|
| 19 |
/** |
| 20 |
* The options helper. |
| 21 |
* |
| 22 |
* @var Options_Helper |
| 23 |
*/ |
| 24 |
private $options_helper; |
| 25 |
|
| 26 |
/** |
| 27 |
* The URL helper. |
| 28 |
* |
| 29 |
* @var Url_Helper |
| 30 |
*/ |
| 31 |
private $url_helper; |
| 32 |
|
| 33 |
/** |
| 34 |
* The Redirect Helper. |
| 35 |
* |
| 36 |
* @var Redirect_Helper |
| 37 |
*/ |
| 38 |
private $redirect_helper; |
| 39 |
|
| 40 |
/** |
| 41 |
* Crawl Cleanup Basic integration constructor. |
| 42 |
* |
| 43 |
* @param Current_Page_Helper $current_page_helper The current page helper. |
| 44 |
* @param Options_Helper $options_helper The option helper. |
| 45 |
* @param Url_Helper $url_helper The URL helper. |
| 46 |
* @param Redirect_Helper $redirect_helper The Redirect Helper. |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
Current_Page_Helper $current_page_helper, |
| 50 |
Options_Helper $options_helper, |
| 51 |
Url_Helper $url_helper, |
| 52 |
Redirect_Helper $redirect_helper |
| 53 |
) { |
| 54 |
$this->current_page_helper = $current_page_helper; |
| 55 |
$this->options_helper = $options_helper; |
| 56 |
$this->url_helper = $url_helper; |
| 57 |
$this->redirect_helper = $redirect_helper; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Checks if the current URL is not robots, sitemap, empty or user is logged in. |
| 62 |
* |
| 63 |
* @return bool True if the current URL is a valid URL. |
| 64 |
*/ |
| 65 |
public function should_avoid_redirect() { |
| 66 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We're not processing anything yet... |
| 67 |
if ( \is_robots() || \get_query_var( 'sitemap' ) || empty( $_GET ) || \is_user_logged_in() ) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the list of the allowed extra vars. |
| 75 |
* |
| 76 |
* @return array The list of the allowed extra vars. |
| 77 |
*/ |
| 78 |
public function get_allowed_extravars() { |
| 79 |
$default_allowed_extravars = [ |
| 80 |
'utm_source', |
| 81 |
'utm_medium', |
| 82 |
'utm_campaign', |
| 83 |
'utm_term', |
| 84 |
'utm_content', |
| 85 |
'gclid', |
| 86 |
'gtm_debug', |
| 87 |
]; |
| 88 |
|
| 89 |
/** |
| 90 |
* Filter: 'Yoast\WP\SEO\allowlist_permalink_vars' - Allows plugins to register their own variables not to clean. |
| 91 |
* |
| 92 |
* @since 19.2.0 |
| 93 |
* |
| 94 |
* @param array $allowed_extravars The list of the allowed vars (empty by default). |
| 95 |
*/ |
| 96 |
$allowed_extravars = \apply_filters( 'Yoast\WP\SEO\allowlist_permalink_vars', $default_allowed_extravars ); |
| 97 |
|
| 98 |
$clean_permalinks_extra_variables = $this->options_helper->get( 'clean_permalinks_extra_variables' ); |
| 99 |
|
| 100 |
if ( $clean_permalinks_extra_variables !== '' ) { |
| 101 |
$allowed_extravars = \array_merge( $allowed_extravars, \explode( ',', $clean_permalinks_extra_variables ) ); |
| 102 |
} |
| 103 |
return $allowed_extravars; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Gets the allowed query vars from the current URL. |
| 108 |
* |
| 109 |
* @param string $current_url The current URL. |
| 110 |
* @return array is_allowed and allowed_query. |
| 111 |
*/ |
| 112 |
public function allowed_params( $current_url ) { |
| 113 |
// This is a Premium plugin-only function: Allows plugins to register their own variables not to clean. |
| 114 |
$allowed_extravars = $this->get_allowed_extravars(); |
| 115 |
|
| 116 |
$allowed_query = []; |
| 117 |
|
| 118 |
$parsed_url = \wp_parse_url( $current_url, \PHP_URL_QUERY ); |
| 119 |
|
| 120 |
$query = $this->url_helper->parse_str_params( $parsed_url ); |
| 121 |
|
| 122 |
if ( ! empty( $allowed_extravars ) ) { |
| 123 |
foreach ( $allowed_extravars as $get ) { |
| 124 |
$get = \trim( $get ); |
| 125 |
if ( isset( $query[ $get ] ) ) { |
| 126 |
$allowed_query[ $get ] = \rawurlencode_deep( $query[ $get ] ); |
| 127 |
unset( $query[ $get ] ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
return [ |
| 132 |
'query' => $query, |
| 133 |
'allowed_query' => $allowed_query, |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Returns the proper URL for singular pages. |
| 139 |
* |
| 140 |
* @return string The proper URL. |
| 141 |
*/ |
| 142 |
public function singular_url() { |
| 143 |
|
| 144 |
global $post; |
| 145 |
$proper_url = \get_permalink( $post->ID ); |
| 146 |
$page = \get_query_var( 'page' ); |
| 147 |
|
| 148 |
if ( $page && $page !== 1 ) { |
| 149 |
$the_post = \get_post( $post->ID ); |
| 150 |
$page_count = \substr_count( $the_post->post_content, '<!--nextpage-->' ); |
| 151 |
$proper_url = \user_trailingslashit( \trailingslashit( $proper_url ) . $page ); |
| 152 |
if ( $page > ( $page_count + 1 ) ) { |
| 153 |
$proper_url = \user_trailingslashit( \trailingslashit( $proper_url ) . ( $page_count + 1 ) ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// Fix reply to comment links, whoever decided this should be a GET variable? |
| 158 |
// phpcs:ignore WordPress.Security -- We know this is scary. |
| 159 |
if ( isset( $_SERVER['REQUEST_URI'] ) && \preg_match( '`(\?replytocom=[^&]+)`', \sanitize_text_field( $_SERVER['REQUEST_URI'] ), $matches ) ) { |
| 160 |
$proper_url .= \str_replace( '?replytocom=', '#comment-', $matches[0] ); |
| 161 |
} |
| 162 |
unset( $matches ); |
| 163 |
|
| 164 |
return $proper_url; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns the proper URL for front page. |
| 169 |
* |
| 170 |
* @return string The proper URL. |
| 171 |
*/ |
| 172 |
public function front_page_url() { |
| 173 |
if ( $this->current_page_helper->is_home_posts_page() ) { |
| 174 |
return \home_url( '/' ); |
| 175 |
} |
| 176 |
if ( $this->current_page_helper->is_home_static_page() ) { |
| 177 |
return \get_permalink( $GLOBALS['post']->ID ); |
| 178 |
} |
| 179 |
return ''; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Returns the proper URL for 404 page. |
| 184 |
* |
| 185 |
* @param string $current_url The current URL. |
| 186 |
* @return string The proper URL. |
| 187 |
*/ |
| 188 |
public function page_not_found_url( $current_url ) { |
| 189 |
if ( ! \is_multisite() || \is_subdomain_install() || ! \is_main_site() ) { |
| 190 |
return ''; |
| 191 |
} |
| 192 |
|
| 193 |
if ( $current_url !== \home_url() . '/blog/' && $current_url !== \home_url() . '/blog' ) { |
| 194 |
return ''; |
| 195 |
} |
| 196 |
|
| 197 |
if ( $this->current_page_helper->is_home_static_page() ) { |
| 198 |
return \get_permalink( \get_option( 'page_for_posts' ) ); |
| 199 |
} |
| 200 |
|
| 201 |
return \home_url(); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the proper URL for taxonomy page. |
| 206 |
* |
| 207 |
* @return string The proper URL. |
| 208 |
*/ |
| 209 |
public function taxonomy_url() { |
| 210 |
global $wp_query; |
| 211 |
$term = $wp_query->get_queried_object(); |
| 212 |
|
| 213 |
if ( \is_feed() ) { |
| 214 |
return \get_term_feed_link( $term->term_id, $term->taxonomy ); |
| 215 |
} |
| 216 |
return \get_term_link( $term, $term->taxonomy ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns the proper URL for search page. |
| 221 |
* |
| 222 |
* @return string The proper URL. |
| 223 |
*/ |
| 224 |
public function search_url() { |
| 225 |
$s = \get_search_query(); |
| 226 |
return \home_url() . '/?s=' . \rawurlencode( $s ); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Returns the proper URL for url with page param. |
| 231 |
* |
| 232 |
* @param string $proper_url The proper URL. |
| 233 |
* @return string The proper URL. |
| 234 |
*/ |
| 235 |
public function query_var_page_url( $proper_url ) { |
| 236 |
global $wp_query; |
| 237 |
if ( \is_search( $proper_url ) ) { |
| 238 |
return \home_url() . '/page/' . $wp_query->query_vars['paged'] . '/?s=' . \rawurlencode( \get_search_query() ); |
| 239 |
} |
| 240 |
return \user_trailingslashit( \trailingslashit( $proper_url ) . 'page/' . $wp_query->query_vars['paged'] ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Returns true if query is with page param. |
| 245 |
* |
| 246 |
* @param string $proper_url The proper URL. |
| 247 |
* @return bool is query with page param. |
| 248 |
*/ |
| 249 |
public function is_query_var_page( $proper_url ) { |
| 250 |
global $wp_query; |
| 251 |
if ( empty( $proper_url ) || $wp_query->query_vars['paged'] === 0 || $wp_query->post_count === 0 ) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Redirects clean permalink. |
| 259 |
* |
| 260 |
* @param string $proper_url The proper URL. |
| 261 |
* @return void |
| 262 |
*/ |
| 263 |
public function do_clean_redirect( $proper_url ) { |
| 264 |
$this->redirect_helper->set_header( 'Content-Type: redirect', true ); |
| 265 |
$this->redirect_helper->remove_header( 'Content-Type' ); |
| 266 |
$this->redirect_helper->remove_header( 'Last-Modified' ); |
| 267 |
$this->redirect_helper->remove_header( 'X-Pingback' ); |
| 268 |
|
| 269 |
$message = \sprintf( |
| 270 |
/* translators: %1$s: Yoast SEO */ |
| 271 |
\__( '%1$s: unregistered URL parameter removed. See %2$s', 'wordpress-seo' ), |
| 272 |
'Yoast SEO', |
| 273 |
'https://yoa.st/advanced-crawl-settings', |
| 274 |
); |
| 275 |
|
| 276 |
$this->redirect_helper->do_safe_redirect( $proper_url, 301, $message ); |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Gets the type of URL. |
| 281 |
* |
| 282 |
* @return string The type of URL. |
| 283 |
*/ |
| 284 |
public function get_url_type() { |
| 285 |
if ( \is_singular() ) { |
| 286 |
return 'singular_url'; |
| 287 |
} |
| 288 |
if ( \is_front_page() ) { |
| 289 |
return 'front_page_url'; |
| 290 |
} |
| 291 |
if ( $this->current_page_helper->is_posts_page() ) { |
| 292 |
return 'page_for_posts_url'; |
| 293 |
} |
| 294 |
if ( \is_category() || \is_tag() || \is_tax() ) { |
| 295 |
return 'taxonomy_url'; |
| 296 |
} |
| 297 |
if ( \is_search() ) { |
| 298 |
return 'search_url'; |
| 299 |
} |
| 300 |
if ( \is_404() ) { |
| 301 |
return 'page_not_found_url'; |
| 302 |
} |
| 303 |
return ''; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Returns the proper URL for posts page. |
| 308 |
* |
| 309 |
* @return string The proper URL. |
| 310 |
*/ |
| 311 |
public function page_for_posts_url() { |
| 312 |
return \get_permalink( \get_option( 'page_for_posts' ) ); |
| 313 |
} |
| 314 |
} |
| 315 |
|