| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Front_End; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\Front_End_Conditional; |
| 6 |
use Yoast\WP\SEO\Helpers\Current_Page_Helper; |
| 7 |
use Yoast\WP\SEO\Helpers\Meta_Helper; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Redirect_Helper; |
| 10 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Redirects. |
| 14 |
*/ |
| 15 |
class Redirects implements Integration_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The options helper. |
| 19 |
* |
| 20 |
* @var Options_Helper |
| 21 |
*/ |
| 22 |
protected $options; |
| 23 |
|
| 24 |
/** |
| 25 |
* The meta helper. |
| 26 |
* |
| 27 |
* @var Meta_Helper |
| 28 |
*/ |
| 29 |
protected $meta; |
| 30 |
|
| 31 |
/** |
| 32 |
* The current page helper. |
| 33 |
* |
| 34 |
* @var Current_Page_Helper |
| 35 |
*/ |
| 36 |
protected $current_page; |
| 37 |
|
| 38 |
/** |
| 39 |
* The redirect helper. |
| 40 |
* |
| 41 |
* @var Redirect_Helper |
| 42 |
*/ |
| 43 |
private $redirect; |
| 44 |
|
| 45 |
/** |
| 46 |
* Sets the helpers. |
| 47 |
* |
| 48 |
* @codeCoverageIgnore |
| 49 |
* |
| 50 |
* @param Options_Helper $options Options helper. |
| 51 |
* @param Meta_Helper $meta Meta helper. |
| 52 |
* @param Current_Page_Helper $current_page The current page helper. |
| 53 |
* @param Redirect_Helper $redirect The redirect helper. |
| 54 |
*/ |
| 55 |
public function __construct( Options_Helper $options, Meta_Helper $meta, Current_Page_Helper $current_page, Redirect_Helper $redirect ) { |
| 56 |
$this->options = $options; |
| 57 |
$this->meta = $meta; |
| 58 |
$this->current_page = $current_page; |
| 59 |
$this->redirect = $redirect; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the conditionals based in which this loadable should be active. |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public static function get_conditionals() { |
| 68 |
return [ Front_End_Conditional::class ]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Initializes the integration. |
| 73 |
* |
| 74 |
* This is the place to register hooks and filters. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function register_hooks() { |
| 79 |
\add_action( 'wp', [ $this, 'archive_redirect' ] ); |
| 80 |
\add_action( 'wp', [ $this, 'page_redirect' ], 99 ); |
| 81 |
\add_action( 'template_redirect', [ $this, 'attachment_redirect' ], 1 ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* When certain archives are disabled, this redirects those to the homepage. |
| 86 |
*/ |
| 87 |
public function archive_redirect() { |
| 88 |
if ( $this->need_archive_redirect() ) { |
| 89 |
$this->redirect->do_safe_redirect( \get_bloginfo( 'url' ), 301 ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Based on the redirect meta value, this function determines whether it should redirect the current post / page. |
| 95 |
*/ |
| 96 |
public function page_redirect() { |
| 97 |
if ( ! $this->current_page->is_simple_page() ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$post = \get_post(); |
| 102 |
if ( ! \is_object( $post ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$redirect = $this->meta->get_value( 'redirect', $post->ID ); |
| 107 |
if ( $redirect === '' ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
$this->redirect->do_safe_redirect( $redirect, 301 ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* If the option to disable attachment URLs is checked, this performs the redirect to the attachment. |
| 116 |
*/ |
| 117 |
public function attachment_redirect() { |
| 118 |
if ( ! $this->current_page->is_attachment() ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
if ( $this->options->get( 'disable-attachment', false ) === false ) { |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
$url = $this->get_attachment_url(); |
| 127 |
if ( empty( $url ) ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$this->redirect->do_unsafe_redirect( $url, 301 ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Checks if certain archive pages are disabled to determine if a archive redirect is needed. |
| 136 |
* |
| 137 |
* @codeCoverageIgnore |
| 138 |
* |
| 139 |
* @return bool Whether or not to redirect an archive page. |
| 140 |
*/ |
| 141 |
protected function need_archive_redirect() { |
| 142 |
if ( $this->options->get( 'disable-date', false ) && $this->current_page->is_date_archive() ) { |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
if ( $this->options->get( 'disable-author', false ) && $this->current_page->is_author_archive() ) { |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
if ( $this->options->get( 'disable-post_format', false ) && $this->current_page->is_post_format_archive() ) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Retrieves the attachment url for the current page. |
| 159 |
* |
| 160 |
* @codeCoverageIgnore It wraps WordPress functions. |
| 161 |
* |
| 162 |
* @return string The attachment url. |
| 163 |
*/ |
| 164 |
protected function get_attachment_url() { |
| 165 |
/** |
| 166 |
* Allows the developer to change the target redirection URL for attachments. |
| 167 |
* |
| 168 |
* @api string $attachment_url The attachment URL for the queried object. |
| 169 |
* @api object $queried_object The queried object. |
| 170 |
* |
| 171 |
* @since 7.5.3 |
| 172 |
*/ |
| 173 |
return \apply_filters( |
| 174 |
'wpseo_attachment_redirect_url', |
| 175 |
\wp_get_attachment_url( \get_queried_object_id() ), |
| 176 |
\get_queried_object() |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
|