| 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/integrations/front-end/ |
Upload File : |
<?php
namespace Yoast\WP\SEO\Integrations\Front_End;
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
use Yoast\WP\SEO\Helpers\Options_Helper;
use Yoast\WP\SEO\Integrations\Integration_Interface;
use Yoast\WP\SEO\Wrappers\WP_Query_Wrapper;
/**
* Class Force_Rewrite_Title.
*/
class Force_Rewrite_Title implements Integration_Interface {
/**
* The options helper.
*
* @var Options_Helper
*/
private $options;
/**
* Toggle indicating whether output buffering has been started.
*
* @var bool
*/
private $ob_started = false;
/**
* The WP Query wrapper.
*
* @var WP_Query_Wrapper
*/
private $wp_query;
/**
* Sets the helpers.
*
* @codeCoverageIgnore It just handles dependencies.
*
* @param Options_Helper $options Options helper.
* @param WP_Query_Wrapper $wp_query WP query wrapper.
*/
public function __construct( Options_Helper $options, WP_Query_Wrapper $wp_query ) {
$this->options = $options;
$this->wp_query = $wp_query;
}
/**
* Returns the conditionals based in which this loadable should be active.
*
* @return array
*/
public static function get_conditionals() {
return [ Front_End_Conditional::class ];
}
/**
* Initializes the integration.
*
* This is the place to register hooks and filters.
*
* @codeCoverageIgnore
*
* @return void
*/
public function register_hooks() {
// When the option is disabled.
if ( ! $this->options->get( 'forcerewritetitle', false ) ) {
return;
}
// For WordPress versions below 4.4.
if ( \current_theme_supports( 'title-tag' ) ) {
return;
}
\add_action( 'template_redirect', [ $this, 'force_rewrite_output_buffer' ], 99_999 );
\add_action( 'wp_footer', [ $this, 'flush_cache' ], -1 );
}
/**
* Used in the force rewrite functionality this retrieves the output, replaces the title with the proper SEO
* title and then flushes the output.
*
* @return bool
*/
public function flush_cache() {
if ( $this->ob_started !== true ) {
return false;
}
$content = $this->get_buffered_output();
$old_wp_query = $this->wp_query->get_query();
\wp_reset_query();
// When the file has the debug mark.
if ( \preg_match( '/(?\'before\'.*)<!-- This site is optimized with the Yoast SEO.*<!-- \/ Yoast SEO( Premium)? plugin. -->(?\'after\'.*)/is', $content, $matches ) ) {
$content = $this->replace_titles_from_content( $content, $matches );
unset( $matches );
}
// phpcs:ignore WordPress.WP.GlobalVariablesOverride -- The query gets reset here with the original query.
$GLOBALS['wp_query'] = $old_wp_query;
// phpcs:ignore WordPress.Security.EscapeOutput -- The output should already have been escaped, we are only filtering it.
echo $content;
return true;
}
/**
* Starts the output buffer so it can later be fixed by flush_cache().
*
* @return void
*/
public function force_rewrite_output_buffer() {
$this->ob_started = true;
$this->start_output_buffering();
}
/**
* Replaces the titles from the parts that contains a title.
*
* @param string $content The content to remove the titles from.
* @param array $parts_with_title The parts containing a title.
*
* @return string The modified content.
*/
protected function replace_titles_from_content( $content, $parts_with_title ) {
if ( isset( $parts_with_title['before'] ) && \is_string( $parts_with_title['before'] ) ) {
$content = $this->replace_title( $parts_with_title['before'], $content );
}
if ( isset( $parts_with_title['after'] ) ) {
$content = $this->replace_title( $parts_with_title['after'], $content );
}
return $content;
}
/**
* Removes the title from the part that contains the title and put this modified part back
* into the content.
*
* @param string $part_with_title The part with the title that needs to be replaced.
* @param string $content The entire content.
*
* @return string The altered content.
*/
protected function replace_title( $part_with_title, $content ) {
$part_without_title = \preg_replace( '/<title.*?\/title>/i', '', $part_with_title );
return \str_replace( $part_with_title, $part_without_title, $content );
}
/**
* Starts the output buffering.
*
* @codeCoverageIgnore
*
* @return void
*/
protected function start_output_buffering() {
\ob_start();
}
/**
* Retrieves the buffered output.
*
* @codeCoverageIgnore
*
* @return string|false The buffered output.
*/
protected function get_buffered_output() {
return \ob_get_clean();
}
}