| Server IP : 151.101.66.159 / Your IP : 216.73.216.66 Web Server : Flywheel/5.1.0 System : Linux wordpress 6.12.113+ #1 SMP Thu Oct 22 02:38:20 UTC 2026 x86_64 User : root ( ) PHP Version : 7.4.33.18 Disable Function : disk_free_space,diskfreespace,dl,exec,fpassthru,getmypid,getmyuid,highlight_file,link,opcache_compile_file,opcache_get_configuration,opcache_is_script_cached,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifcontinued,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,phpinfo,popen,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,proc_close,proc_get_status,proc_nice,proc_open,proc_terminate,shell_exec,show_source,socket_listen,source,system,virtual MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wp-content/plugins/wordpress-seo/src/presenters/ |
Upload File : |
<?php
namespace Yoast\WP\SEO\Presenters;
use Yoast\WP\SEO\Helpers\Robots_Txt_Helper;
/**
* Presenter class for the robots.txt file helper.
*/
class Robots_Txt_Presenter extends Abstract_Presenter {
public const YOAST_OUTPUT_BEFORE_COMMENT = '# START YOAST BLOCK' . \PHP_EOL . '# ---------------------------' . \PHP_EOL;
public const YOAST_OUTPUT_AFTER_COMMENT = '# ---------------------------' . \PHP_EOL . '# END YOAST BLOCK';
/**
* Text to be outputted for the allow directive.
*
* @var string
*/
public const ALLOW_DIRECTIVE = 'Allow';
/**
* Text to be outputted for the disallow directive.
*
* @var string
*/
public const DISALLOW_DIRECTIVE = 'Disallow';
/**
* Text to be outputted for the user-agent rule.
*
* @var string
*/
public const USER_AGENT_FIELD = 'User-agent';
/**
* Text to be outputted for the sitemap rule.
*
* @var string
*/
public const SITEMAP_FIELD = 'Sitemap';
/**
* Text to be outputted for the schemamap rule.
*
* @var string
*/
public const SCHEMAMAP_FIELD = 'Schemamap';
/**
* Holds the Robots_Txt_Helper.
*
* @var Robots_Txt_Helper
*/
protected $robots_txt_helper;
/**
* Constructor.
*
* @param Robots_Txt_Helper $robots_txt_helper The robots txt helper.
*/
public function __construct( Robots_Txt_Helper $robots_txt_helper ) {
$this->robots_txt_helper = $robots_txt_helper;
}
/**
* Generate content to be placed in a robots.txt file.
*
* @return string Content to be placed in a robots.txt file.
*/
public function present() {
$robots_txt_content = self::YOAST_OUTPUT_BEFORE_COMMENT;
$robots_txt_content = $this->handle_user_agents( $robots_txt_content );
$robots_txt_content = $this->handle_site_maps( $robots_txt_content );
$robots_txt_content = $this->handle_schema_maps( $robots_txt_content );
return $robots_txt_content . self::YOAST_OUTPUT_AFTER_COMMENT;
}
/**
* Adds user agent directives to the robots txt output string.
*
* @param array $user_agents The list if available user agents.
* @param string $robots_txt_content The current working robots txt string.
*
* @return string
*/
private function add_user_agent_directives( $user_agents, $robots_txt_content ) {
foreach ( $user_agents as $user_agent ) {
$robots_txt_content .= self::USER_AGENT_FIELD . ': ' . $user_agent->get_user_agent() . \PHP_EOL;
$robots_txt_content = $this->add_directive_path( $robots_txt_content, $user_agent->get_disallow_paths(), self::DISALLOW_DIRECTIVE );
$robots_txt_content = $this->add_directive_path( $robots_txt_content, $user_agent->get_allow_paths(), self::ALLOW_DIRECTIVE );
$robots_txt_content .= \PHP_EOL;
}
return $robots_txt_content;
}
/**
* Adds user agent directives path content to the robots txt output string.
*
* @param string $robots_txt_content The current working robots txt string.
* @param array $paths The list of paths for which to add a txt entry.
* @param string $directive_identifier The identifier for the directives. (Disallow of Allow).
*
* @return string
*/
private function add_directive_path( $robots_txt_content, $paths, $directive_identifier ) {
if ( \count( $paths ) > 0 ) {
foreach ( $paths as $path ) {
$robots_txt_content .= $directive_identifier . ': ' . $path . \PHP_EOL;
}
}
return $robots_txt_content;
}
/**
* Handles adding user agent content to the robots txt content if there is any.
*
* @param string $robots_txt_content The current working robots txt string.
*
* @return string
*/
private function handle_user_agents( $robots_txt_content ) {
$user_agents = $this->robots_txt_helper->get_robots_txt_user_agents();
if ( ! isset( $user_agents['*'] ) ) {
$robots_txt_content .= 'User-agent: *' . \PHP_EOL;
$robots_txt_content .= 'Disallow:' . \PHP_EOL . \PHP_EOL;
}
$robots_txt_content = $this->add_user_agent_directives( $user_agents, $robots_txt_content );
return $robots_txt_content;
}
/**
* Handles adding sitemap content to the robots txt content.
*
* @param string $robots_txt_content The current working robots txt string.
*
* @return string
*/
private function handle_site_maps( $robots_txt_content ) {
$registered_sitemaps = $this->robots_txt_helper->get_sitemap_rules();
foreach ( $registered_sitemaps as $sitemap ) {
$robots_txt_content .= self::SITEMAP_FIELD . ': ' . $sitemap . \PHP_EOL;
}
return $robots_txt_content;
}
/**
* Handles adding schema map content to the robots txt content.
*
* @param string $robots_txt_content The current working robots txt string.
*
* @return string
*/
private function handle_schema_maps( $robots_txt_content ) {
$registered_schemamaps = $this->robots_txt_helper->get_schemamap_rules();
foreach ( $registered_schemamaps as $schemamap ) {
$robots_txt_content .= self::SCHEMAMAP_FIELD . ': ' . $schemamap . \PHP_EOL;
}
return $robots_txt_content;
}
}