| 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\Robots_Helper; |
| 7 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Indexing_Controls. |
| 11 |
*/ |
| 12 |
class Indexing_Controls implements Integration_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* The robots helper. |
| 16 |
* |
| 17 |
* @var Robots_Helper |
| 18 |
*/ |
| 19 |
protected $robots; |
| 20 |
|
| 21 |
/** |
| 22 |
* Returns the conditionals based in which this loadable should be active. |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public static function get_conditionals() { |
| 27 |
return [ Front_End_Conditional::class ]; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* The constructor. |
| 32 |
* |
| 33 |
* @codeCoverageIgnore Sets the dependencies. |
| 34 |
* |
| 35 |
* @param Robots_Helper $robots The robots helper. |
| 36 |
*/ |
| 37 |
public function __construct( Robots_Helper $robots ) { |
| 38 |
$this->robots = $robots; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Initializes the integration. |
| 43 |
* |
| 44 |
* This is the place to register hooks and filters. |
| 45 |
* |
| 46 |
* @codeCoverageIgnore |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function register_hooks() { |
| 51 |
// The option `blog_public` is set in Settings > Reading > Search Engine Visibility. |
| 52 |
if ( (string) \get_option( 'blog_public' ) === '0' ) { |
| 53 |
\add_filter( 'wpseo_robots_array', [ $this->robots, 'set_robots_no_index' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
\add_action( 'template_redirect', [ $this, 'noindex_robots' ] ); |
| 57 |
\add_filter( 'loginout', [ $this, 'nofollow_link' ] ); |
| 58 |
\add_filter( 'register', [ $this, 'nofollow_link' ] ); |
| 59 |
|
| 60 |
// Remove actions that we will handle through our wpseo_head call, and probably change the output of. |
| 61 |
\remove_action( 'wp_head', 'rel_canonical' ); |
| 62 |
\remove_action( 'wp_head', 'index_rel_link' ); |
| 63 |
\remove_action( 'wp_head', 'start_post_rel_link' ); |
| 64 |
\remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head' ); |
| 65 |
\remove_action( 'wp_head', 'noindex', 1 ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Sends a Robots HTTP header preventing URL from being indexed in the search results while allowing search engines |
| 70 |
* to follow the links in the object at the URL. |
| 71 |
* |
| 72 |
* @return bool Boolean indicating whether the noindex header was sent. |
| 73 |
*/ |
| 74 |
public function noindex_robots() { |
| 75 |
if ( ! \is_robots() ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return $this->set_robots_header(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Adds rel="nofollow" to a link, only used for login / registration links. |
| 84 |
* |
| 85 |
* @param string $input The link element as a string. |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function nofollow_link( $input ) { |
| 90 |
return \str_replace( '<a ', '<a rel="nofollow" ', $input ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Sets the x-robots-tag to noindex follow. |
| 95 |
* |
| 96 |
* @codeCoverageIgnore Too difficult to test. |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
protected function set_robots_header() { |
| 101 |
if ( \headers_sent() === false ) { |
| 102 |
\header( 'X-Robots-Tag: noindex, follow', true ); |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
} |
| 110 |
|