PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / presenters / robots-txt-presenter.php

robots-txt-presenter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.5, at src/presenters/robots-txt-presenter.php

168 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Presenters;
4
5 use Yoast\WP\SEO\Helpers\Robots_Txt_Helper;
6
7 /**
8 * Presenter class for the robots.txt file helper.
9 */
10 class Robots_Txt_Presenter extends Abstract_Presenter {
11
12 public const YOAST_OUTPUT_BEFORE_COMMENT = '# START YOAST BLOCK' . \PHP_EOL . '# ---------------------------' . \PHP_EOL;
13
14 public const YOAST_OUTPUT_AFTER_COMMENT = '# ---------------------------' . \PHP_EOL . '# END YOAST BLOCK';
15
16 /**
17 * Text to be outputted for the allow directive.
18 *
19 * @var string
20 */
21 public const ALLOW_DIRECTIVE = 'Allow';
22
23 /**
24 * Text to be outputted for the disallow directive.
25 *
26 * @var string
27 */
28 public const DISALLOW_DIRECTIVE = 'Disallow';
29
30 /**
31 * Text to be outputted for the user-agent rule.
32 *
33 * @var string
34 */
35 public const USER_AGENT_FIELD = 'User-agent';
36
37 /**
38 * Text to be outputted for the sitemap rule.
39 *
40 * @var string
41 */
42 public const SITEMAP_FIELD = 'Sitemap';
43
44 /**
45 * Holds the Robots_Txt_Helper.
46 *
47 * @var Robots_Txt_Helper
48 */
49 protected $robots_txt_helper;
50
51 /**
52 * Constructor.
53 *
54 * @param Robots_Txt_Helper $robots_txt_helper The robots txt helper.
55 */
56 public function __construct( Robots_Txt_Helper $robots_txt_helper ) {
57 $this->robots_txt_helper = $robots_txt_helper;
58 }
59
60 /**
61 * Generate content to be placed in a robots.txt file.
62 *
63 * @return string Content to be placed in a robots.txt file.
64 */
65 public function present() {
66 $robots_txt_content = self::YOAST_OUTPUT_BEFORE_COMMENT;
67 $robots_txt_content = $this->handle_user_agents( $robots_txt_content );
68
69 $robots_txt_content = $this->handle_site_maps( $robots_txt_content );
70
71 return $robots_txt_content . self::YOAST_OUTPUT_AFTER_COMMENT;
72 }
73
74 /**
75 * Adds user agent directives to the robots txt output string.
76 *
77 * @param array $user_agents The list if available user agents.
78 * @param string $robots_txt_content The current working robots txt string.
79 *
80 * @return string
81 */
82 private function add_user_agent_directives( $user_agents, $robots_txt_content ) {
83 foreach ( $user_agents as $user_agent ) {
84 $robots_txt_content .= self::USER_AGENT_FIELD . ': ' . $user_agent->get_user_agent() . \PHP_EOL;
85
86 $robots_txt_content = $this->add_directive_path( $robots_txt_content, $user_agent->get_disallow_paths(), self::DISALLOW_DIRECTIVE );
87 $robots_txt_content = $this->add_directive_path( $robots_txt_content, $user_agent->get_allow_paths(), self::ALLOW_DIRECTIVE );
88
89 $robots_txt_content .= \PHP_EOL;
90 }
91
92 return $robots_txt_content;
93 }
94
95 /**
96 * Adds user agent directives path content to the robots txt output string.
97 *
98 * @param string $robots_txt_content The current working robots txt string.
99 * @param array $paths The list of paths for which to add a txt entry.
100 * @param string $directive_identifier The identifier for the directives. (Disallow of Allow).
101 *
102 * @return string
103 */
104 private function add_directive_path( $robots_txt_content, $paths, $directive_identifier ) {
105 if ( \count( $paths ) > 0 ) {
106 foreach ( $paths as $path ) {
107 $robots_txt_content .= $directive_identifier . ': ' . $path . \PHP_EOL;
108 }
109 }
110
111 return $robots_txt_content;
112 }
113
114 /**
115 * Handles adding user agent content to the robots txt content if there is any.
116 *
117 * @param string $robots_txt_content The current working robots txt string.
118 *
119 * @return string
120 */
121 private function handle_user_agents( $robots_txt_content ) {
122 $user_agents = $this->robots_txt_helper->get_robots_txt_user_agents();
123
124 if ( ! isset( $user_agents['*'] ) ) {
125 $robots_txt_content .= 'User-agent: *' . \PHP_EOL;
126 $robots_txt_content .= 'Disallow:' . \PHP_EOL . \PHP_EOL;
127 }
128
129 $robots_txt_content = $this->add_user_agent_directives( $user_agents, $robots_txt_content );
130
131 return $robots_txt_content;
132 }
133
134 /**
135 * Handles adding sitemap content to the robots txt content.
136 *
137 * @param string $robots_txt_content The current working robots txt string.
138 *
139 * @return string
140 */
141 private function handle_site_maps( $robots_txt_content ) {
142 $registered_sitemaps = $this->robots_txt_helper->get_sitemap_rules();
143
144 foreach ( $registered_sitemaps as $sitemap ) {
145 $robots_txt_content .= self::SITEMAP_FIELD . ': ' . $sitemap . \PHP_EOL;
146 }
147
148 return $robots_txt_content;
149 }
150
151 /**
152 * Handles adding schema map content to the robots txt content.
153 *
154 * @phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
155 *
156 * @deprecated 27.5
157 * @codeCoverageIgnore
158 *
159 * @param string $robots_txt_content The current working robots txt string.
160 *
161 * @return string
162 */
163 private function handle_schema_maps( $robots_txt_content ) {
164 \_deprecated_function( __METHOD__, 'Yoast SEO 27.5' );
165 return '';
166 }
167 }
168