| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Values\Robots\User_Agent_List; |
| 6 |
|
| 7 |
/** |
| 8 |
* A helper object for the robots txt file. |
| 9 |
*/ |
| 10 |
class Robots_Txt_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* Holds a list of user agents with directives. |
| 14 |
* |
| 15 |
* @var User_Agent_List |
| 16 |
*/ |
| 17 |
protected $robots_txt_user_agents; |
| 18 |
|
| 19 |
/** |
| 20 |
* Holds an array with absolute URLs of sitemaps. |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
protected $robots_txt_sitemaps; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor for Robots_Txt_Helper. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->robots_txt_user_agents = new User_Agent_List(); |
| 31 |
$this->robots_txt_sitemaps = []; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Add a disallow rule for a specific user agent if it does not exist yet. |
| 36 |
* |
| 37 |
* @param string $user_agent The user agent to add the disallow rule to. |
| 38 |
* @param string $path The path to add as a disallow rule. |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function add_disallow( $user_agent, $path ) { |
| 43 |
$user_agent_container = $this->robots_txt_user_agents->get_user_agent( $user_agent ); |
| 44 |
$user_agent_container->add_disallow_directive( $path ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Add an allow rule for a specific user agent if it does not exist yet. |
| 49 |
* |
| 50 |
* @param string $user_agent The user agent to add the allow rule to. |
| 51 |
* @param string $path The path to add as a allow rule. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function add_allow( $user_agent, $path ) { |
| 56 |
$user_agent_container = $this->robots_txt_user_agents->get_user_agent( $user_agent ); |
| 57 |
$user_agent_container->add_allow_directive( $path ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Add sitemap to robots.txt if it does not exist yet. |
| 62 |
* |
| 63 |
* @param string $absolute_path The absolute path to the sitemap to add. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function add_sitemap( $absolute_path ) { |
| 68 |
if ( ! \in_array( $absolute_path, $this->robots_txt_sitemaps, true ) ) { |
| 69 |
$this->robots_txt_sitemaps[] = $absolute_path; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Add schema to robots.txt if it does not exist yet. |
| 75 |
* |
| 76 |
* @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 77 |
* |
| 78 |
* @deprecated 27.5 |
| 79 |
* @codeCoverageIgnore |
| 80 |
* |
| 81 |
* @param string $absolute_path The absolute path to the sitemap to add. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function add_schemamap( $absolute_path ) { // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter |
| 86 |
\_deprecated_function( __METHOD__, 'Yoast SEO 27.5' ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get all registered disallow directives per user agent. |
| 91 |
* |
| 92 |
* @return array The registered disallow directives per user agent. |
| 93 |
*/ |
| 94 |
public function get_disallow_directives() { |
| 95 |
return $this->robots_txt_user_agents->get_disallow_directives(); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Get all registered allow directives per user agent. |
| 100 |
* |
| 101 |
* @return array The registered allow directives per user agent. |
| 102 |
*/ |
| 103 |
public function get_allow_directives() { |
| 104 |
return $this->robots_txt_user_agents->get_allow_directives(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get all registered sitemap rules. |
| 109 |
* |
| 110 |
* @return array<string> The registered sitemap rules. |
| 111 |
*/ |
| 112 |
public function get_sitemap_rules() { |
| 113 |
return $this->robots_txt_sitemaps; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get all registered schemamap rules. |
| 118 |
* |
| 119 |
* @deprecated 27.5 |
| 120 |
* @codeCoverageIgnore |
| 121 |
* |
| 122 |
* @return array<string> The registered schemamap rules. |
| 123 |
*/ |
| 124 |
public function get_schemamap_rules() { |
| 125 |
\_deprecated_function( __METHOD__, 'Yoast SEO 27.5' ); |
| 126 |
return []; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get all registered user agents |
| 131 |
* |
| 132 |
* @return array The registered user agents. |
| 133 |
*/ |
| 134 |
public function get_robots_txt_user_agents() { |
| 135 |
return $this->robots_txt_user_agents->get_user_agents(); |
| 136 |
} |
| 137 |
} |
| 138 |
|