| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Initializers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\Front_End_Conditional; |
| 6 |
use Yoast\WP\SEO\Helpers\Crawl_Cleanup_Helper; |
| 7 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 8 |
use Yoast\WP\SEO\Helpers\Redirect_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Url_Helper; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class Crawl_Cleanup_Permalinks. |
| 13 |
*/ |
| 14 |
class Crawl_Cleanup_Permalinks implements Initializer_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The options helper. |
| 18 |
* |
| 19 |
* @var Options_Helper |
| 20 |
*/ |
| 21 |
private $options_helper; |
| 22 |
|
| 23 |
/** |
| 24 |
* The URL helper. |
| 25 |
* |
| 26 |
* @var Url_Helper |
| 27 |
*/ |
| 28 |
private $url_helper; |
| 29 |
|
| 30 |
/** |
| 31 |
* The Redirect_Helper. |
| 32 |
* |
| 33 |
* @var Redirect_Helper |
| 34 |
*/ |
| 35 |
private $redirect_helper; |
| 36 |
|
| 37 |
/** |
| 38 |
* The Crawl_Cleanup_Helper. |
| 39 |
* |
| 40 |
* @var Crawl_Cleanup_Helper |
| 41 |
*/ |
| 42 |
private $crawl_cleanup_helper; |
| 43 |
|
| 44 |
/** |
| 45 |
* Crawl Cleanup Basic integration constructor. |
| 46 |
* |
| 47 |
* @param Options_Helper $options_helper The option helper. |
| 48 |
* @param Url_Helper $url_helper The URL helper. |
| 49 |
* @param Redirect_Helper $redirect_helper The Redirect Helper. |
| 50 |
* @param Crawl_Cleanup_Helper $crawl_cleanup_helper The Crawl_Cleanup_Helper. |
| 51 |
*/ |
| 52 |
public function __construct( |
| 53 |
Options_Helper $options_helper, |
| 54 |
Url_Helper $url_helper, |
| 55 |
Redirect_Helper $redirect_helper, |
| 56 |
Crawl_Cleanup_Helper $crawl_cleanup_helper |
| 57 |
) { |
| 58 |
$this->options_helper = $options_helper; |
| 59 |
$this->url_helper = $url_helper; |
| 60 |
$this->redirect_helper = $redirect_helper; |
| 61 |
$this->crawl_cleanup_helper = $crawl_cleanup_helper; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Initializes the integration. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function initialize() { |
| 70 |
// We need to hook after 10 because otherwise our options helper isn't available yet. |
| 71 |
\add_action( 'plugins_loaded', [ $this, 'register_hooks' ], 15 ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Hooks our required hooks. |
| 76 |
* |
| 77 |
* This is the place to register hooks and filters. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function register_hooks() { |
| 82 |
if ( $this->options_helper->get( 'clean_campaign_tracking_urls' ) && ! empty( \get_option( 'permalink_structure' ) ) ) { |
| 83 |
\add_action( 'template_redirect', [ $this, 'utm_redirect' ], 0 ); |
| 84 |
} |
| 85 |
if ( $this->options_helper->get( 'clean_permalinks' ) && ! empty( \get_option( 'permalink_structure' ) ) ) { |
| 86 |
\add_action( 'template_redirect', [ $this, 'clean_permalinks' ], 1 ); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns the conditionals based in which this loadable should be active. |
| 92 |
* |
| 93 |
* @return array The array of conditionals. |
| 94 |
*/ |
| 95 |
public static function get_conditionals() { |
| 96 |
return [ Front_End_Conditional::class ]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Redirect utm variables away. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function utm_redirect() { |
| 105 |
// Prevents WP CLI from throwing an error. |
| 106 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 107 |
if ( ! isset( $_SERVER['REQUEST_URI'] ) || \strpos( $_SERVER['REQUEST_URI'], '?' ) === false ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 112 |
if ( ! \stripos( $_SERVER['REQUEST_URI'], 'utm_' ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 117 |
$parsed = \wp_parse_url( $_SERVER['REQUEST_URI'] ); |
| 118 |
|
| 119 |
$query = \explode( '&', $parsed['query'] ); |
| 120 |
$utms = []; |
| 121 |
$other_args = []; |
| 122 |
|
| 123 |
foreach ( $query as $query_arg ) { |
| 124 |
if ( \stripos( $query_arg, 'utm_' ) === 0 ) { |
| 125 |
$utms[] = $query_arg; |
| 126 |
continue; |
| 127 |
} |
| 128 |
$other_args[] = $query_arg; |
| 129 |
} |
| 130 |
|
| 131 |
if ( empty( $utms ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$other_args_str = ''; |
| 136 |
if ( \count( $other_args ) > 0 ) { |
| 137 |
$other_args_str = '?' . \implode( '&', $other_args ); |
| 138 |
} |
| 139 |
|
| 140 |
$new_path = $parsed['path'] . $other_args_str . '#' . \implode( '&', $utms ); |
| 141 |
|
| 142 |
$message = \sprintf( |
| 143 |
/* translators: %1$s: Yoast SEO */ |
| 144 |
\__( '%1$s: redirect utm variables to #', 'wordpress-seo' ), |
| 145 |
'Yoast SEO', |
| 146 |
); |
| 147 |
|
| 148 |
$this->redirect_helper->do_safe_redirect( \trailingslashit( $this->url_helper->recreate_current_url( false ) ) . \ltrim( $new_path, '/' ), 301, $message ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Removes unneeded query variables from the URL. |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function clean_permalinks() { |
| 157 |
|
| 158 |
if ( $this->crawl_cleanup_helper->should_avoid_redirect() ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
$current_url = $this->url_helper->recreate_current_url(); |
| 163 |
$allowed_params = $this->crawl_cleanup_helper->allowed_params( $current_url ); |
| 164 |
|
| 165 |
// If we had only allowed params, let's just bail out, no further processing needed. |
| 166 |
if ( empty( $allowed_params['query'] ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$url_type = $this->crawl_cleanup_helper->get_url_type(); |
| 171 |
|
| 172 |
switch ( $url_type ) { |
| 173 |
case 'singular_url': |
| 174 |
$proper_url = $this->crawl_cleanup_helper->singular_url(); |
| 175 |
break; |
| 176 |
case 'front_page_url': |
| 177 |
$proper_url = $this->crawl_cleanup_helper->front_page_url(); |
| 178 |
break; |
| 179 |
case 'page_for_posts_url': |
| 180 |
$proper_url = $this->crawl_cleanup_helper->page_for_posts_url(); |
| 181 |
break; |
| 182 |
case 'taxonomy_url': |
| 183 |
$proper_url = $this->crawl_cleanup_helper->taxonomy_url(); |
| 184 |
break; |
| 185 |
case 'search_url': |
| 186 |
$proper_url = $this->crawl_cleanup_helper->search_url(); |
| 187 |
break; |
| 188 |
case 'page_not_found_url': |
| 189 |
$proper_url = $this->crawl_cleanup_helper->page_not_found_url( $current_url ); |
| 190 |
break; |
| 191 |
default: |
| 192 |
$proper_url = ''; |
| 193 |
} |
| 194 |
|
| 195 |
if ( $this->crawl_cleanup_helper->is_query_var_page( $proper_url ) ) { |
| 196 |
$proper_url = $this->crawl_cleanup_helper->query_var_page_url( $proper_url ); |
| 197 |
} |
| 198 |
|
| 199 |
$proper_url = \add_query_arg( $allowed_params['allowed_query'], $proper_url ); |
| 200 |
|
| 201 |
if ( empty( $proper_url ) || $current_url === $proper_url ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
$this->crawl_cleanup_helper->do_clean_redirect( $proper_url ); |
| 206 |
} |
| 207 |
} |
| 208 |
|