first commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Llms_Txt\User_Interface;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Remove_File_Command_Handler;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Llms_Txt_Cron_Scheduler;
|
||||
|
||||
/**
|
||||
* Trys to clean up the llms.txt file when the plugin is deactivated.
|
||||
*/
|
||||
class Cleanup_Llms_Txt_On_Deactivation implements Integration_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* The command handler.
|
||||
*
|
||||
* @var Remove_File_Command_Handler
|
||||
*/
|
||||
private $command_handler;
|
||||
|
||||
/**
|
||||
* The cron scheduler.
|
||||
*
|
||||
* @var Llms_Txt_Cron_Scheduler
|
||||
*/
|
||||
private $cron_scheduler;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Remove_File_Command_Handler $command_handler The command handler.
|
||||
* @param Llms_Txt_Cron_Scheduler $cron_scheduler The scheduler.
|
||||
*/
|
||||
public function __construct(
|
||||
Remove_File_Command_Handler $command_handler,
|
||||
Llms_Txt_Cron_Scheduler $cron_scheduler
|
||||
) {
|
||||
$this->command_handler = $command_handler;
|
||||
$this->cron_scheduler = $cron_scheduler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the unscheduling of the cron to the deactivation action.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action( 'wpseo_deactivate', [ $this, 'maybe_remove_llms_file' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the command handler to remove the file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_remove_llms_file(): void {
|
||||
$this->command_handler->handle();
|
||||
$this->cron_scheduler->unschedule_llms_txt_population();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Llms_Txt\User_Interface;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\Traits\Admin_Conditional_Trait;
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Populate_File_Command_Handler;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Remove_File_Command_Handler;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Llms_Txt_Cron_Scheduler;
|
||||
|
||||
/**
|
||||
* Watches and handles changes to the LLMS.txt enabled option.
|
||||
*/
|
||||
class Enable_Llms_Txt_Option_Watcher implements Integration_Interface {
|
||||
|
||||
use Admin_Conditional_Trait;
|
||||
|
||||
/**
|
||||
* The scheduler.
|
||||
*
|
||||
* @var Llms_Txt_Cron_Scheduler
|
||||
*/
|
||||
private $scheduler;
|
||||
|
||||
/**
|
||||
* The remove file command handler.
|
||||
*
|
||||
* @var Remove_File_Command_Handler
|
||||
*/
|
||||
private $remove_file_command_handler;
|
||||
|
||||
/**
|
||||
* The populate file command handler.
|
||||
*
|
||||
* @var Populate_File_Command_Handler
|
||||
*/
|
||||
private $populate_file_command_handler;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Llms_Txt_Cron_Scheduler $scheduler The cron scheduler.
|
||||
* @param Remove_File_Command_Handler $remove_file_command_handler The remove file command handler.
|
||||
* @param Populate_File_Command_Handler $populate_file_command_handler The populate file command handler.
|
||||
*/
|
||||
public function __construct(
|
||||
Llms_Txt_Cron_Scheduler $scheduler,
|
||||
Remove_File_Command_Handler $remove_file_command_handler,
|
||||
Populate_File_Command_Handler $populate_file_command_handler
|
||||
) {
|
||||
$this->scheduler = $scheduler;
|
||||
$this->remove_file_command_handler = $remove_file_command_handler;
|
||||
$this->populate_file_command_handler = $populate_file_command_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the integration.
|
||||
*
|
||||
* This is the place to register hooks and filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action( 'update_option_wpseo', [ $this, 'check_toggle_llms_txt' ], 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the LLMS.txt feature is toggled.
|
||||
*
|
||||
* @param array<string|int|bool|array<string|int|bool>> $old_value The old value of the option.
|
||||
* @param array<string|int|bool|array<string|int|bool>> $new_value The new value of the option.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function check_toggle_llms_txt( $old_value, $new_value ): void {
|
||||
$option_name = 'enable_llms_txt';
|
||||
|
||||
if ( \array_key_exists( $option_name, $old_value ) && \array_key_exists( $option_name, $new_value ) && $old_value[ $option_name ] !== $new_value[ $option_name ] ) {
|
||||
if ( $new_value[ $option_name ] === true ) {
|
||||
$this->scheduler->schedule_weekly_llms_txt_population();
|
||||
$this->populate_file_command_handler->handle();
|
||||
}
|
||||
else {
|
||||
$this->scheduler->unschedule_llms_txt_population();
|
||||
$this->remove_file_command_handler->handle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
|
||||
namespace Yoast\WP\SEO\Llms_Txt\User_Interface\Health_Check;
|
||||
|
||||
use Yoast\WP\SEO\Services\Health_Check\Report_Builder_Factory;
|
||||
use Yoast\WP\SEO\Services\Health_Check\Reports_Trait;
|
||||
|
||||
/**
|
||||
* Presents a set of different messages for the File_Generation health check.
|
||||
*/
|
||||
class File_Reports {
|
||||
|
||||
use Reports_Trait;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Report_Builder_Factory $report_builder_factory The factory for result builder objects.
|
||||
* This class uses the report builder to generate WordPress-friendly
|
||||
* health check results.
|
||||
*/
|
||||
public function __construct( Report_Builder_Factory $report_builder_factory ) {
|
||||
$this->report_builder_factory = $report_builder_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message for a successful health check.
|
||||
*
|
||||
* @return string[] The message as a WordPress site status report.
|
||||
*/
|
||||
public function get_success_result() {
|
||||
$label = \sprintf(
|
||||
/* translators: %s: Yoast SEO. */
|
||||
\__( 'Your llms.txt file is auto-generated by %s', 'wordpress-seo' ),
|
||||
'Yoast SEO',
|
||||
);
|
||||
|
||||
$description = \sprintf(
|
||||
/* translators: %s: Yoast SEO. */
|
||||
\__( '%s keeps your llms.txt file up-to-date. This helps LLMs access and provide your site\'s information more easily.', 'wordpress-seo' ),
|
||||
'Yoast SEO',
|
||||
);
|
||||
|
||||
return $this->get_report_builder()
|
||||
->set_label( $label )
|
||||
->set_status_good()
|
||||
->set_description( $description )
|
||||
->build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the message for a failed health check. In this case, when the llms.txt file couldn't be auto-generated.
|
||||
*
|
||||
* @param string $reason The reason why the llms.txt file couldn't be auto-generated.
|
||||
*
|
||||
* @return string[] The message as a WordPress site status report.
|
||||
*/
|
||||
public function get_generation_failure_result( $reason ) {
|
||||
switch ( $reason ) {
|
||||
case 'not_managed_by_yoast_seo':
|
||||
$title = \__( 'Your llms.txt file couldn\'t be auto-generated', 'wordpress-seo' );
|
||||
$message = \sprintf(
|
||||
/* translators: 1,3,5: expand to opening paragraph tag, 2,4,6: expand to opening paragraph tag. */
|
||||
\__( '%1$sYou have activated the Yoast llms.txt feature, but we couldn\'t generate an llms.txt file.%2$s%3$sIt looks like there is an llms.txt file already that wasn\'t created by Yoast, or the llms.txt file created by Yoast has been edited manually.%4$s%5$sWe don\'t want to overwrite this file\'s content, so if you want to let Yoast keep auto-generating the llms.txt file, you can manually delete the existing one. Otherwise, consider disabling the Yoast feature.%6$s', 'wordpress-seo' ),
|
||||
'<p>',
|
||||
'</p>',
|
||||
'<p>',
|
||||
'</p>',
|
||||
'<p>',
|
||||
'</p>'
|
||||
);
|
||||
break;
|
||||
case 'filesystem_permissions':
|
||||
$title = \__( 'Your llms.txt file couldn\'t be auto-generated', 'wordpress-seo' );
|
||||
$message = \sprintf(
|
||||
/* translators: 1,3: expand to opening paragraph tag, 2,4: expand to opening paragraph tag. */
|
||||
\__( '%1$sYou have activated the Yoast llms.txt feature, but we couldn\'t generate an llms.txt file.%2$s%3$sIt looks like there aren\'t sufficient permissions on the web server\'s filesystem.%4$s', 'wordpress-seo' ),
|
||||
'<p>',
|
||||
'</p>',
|
||||
'<p>',
|
||||
'</p>'
|
||||
);
|
||||
break;
|
||||
default:
|
||||
$title = \__( 'Your llms.txt file couldn\'t be auto-generated', 'wordpress-seo' );
|
||||
$message = \__( 'You have activated the Yoast llms.txt feature, but we couldn\'t generate an llms.txt file, for unknown reasons.', 'wordpress-seo' );
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->get_report_builder()
|
||||
->set_label( $title )
|
||||
->set_status_recommended()
|
||||
->set_description( $message )
|
||||
->build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Llms_Txt\User_Interface;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Helpers\Options_Helper;
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Populate_File_Command_Handler;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Remove_File_Command_Handler;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Llms_Txt_Cron_Scheduler;
|
||||
|
||||
/**
|
||||
* Cron Callback integration. This handles the actual process of populating the llms.txt on a cron trigger.
|
||||
*/
|
||||
class Llms_Txt_Cron_Callback_Integration implements Integration_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* The remove file command handler.
|
||||
*
|
||||
* @var Remove_File_Command_Handler
|
||||
*/
|
||||
private $remove_file_command_handler;
|
||||
|
||||
/**
|
||||
* The Create Populate Command Handler.
|
||||
*
|
||||
* @var Populate_File_Command_Handler
|
||||
*/
|
||||
private $populate_file_command_handler;
|
||||
|
||||
/**
|
||||
* The options helper.
|
||||
*
|
||||
* @var Options_Helper
|
||||
*/
|
||||
private $options_helper;
|
||||
|
||||
/**
|
||||
* The scheduler.
|
||||
*
|
||||
* @var Llms_Txt_Cron_Scheduler
|
||||
*/
|
||||
private $scheduler;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Options_Helper $options_helper The options helper.
|
||||
* @param Llms_Txt_Cron_Scheduler $scheduler The scheduler.
|
||||
* @param Populate_File_Command_Handler $populate_file_command_handler The populate file command handler.
|
||||
* @param Remove_File_Command_Handler $remove_file_command_handler The remove file command handler.
|
||||
*/
|
||||
public function __construct(
|
||||
Options_Helper $options_helper,
|
||||
Llms_Txt_Cron_Scheduler $scheduler,
|
||||
Populate_File_Command_Handler $populate_file_command_handler,
|
||||
Remove_File_Command_Handler $remove_file_command_handler
|
||||
) {
|
||||
$this->options_helper = $options_helper;
|
||||
$this->scheduler = $scheduler;
|
||||
$this->populate_file_command_handler = $populate_file_command_handler;
|
||||
$this->remove_file_command_handler = $remove_file_command_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the hooks with WordPress.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action(
|
||||
Llms_Txt_Cron_Scheduler::LLMS_TXT_POPULATION,
|
||||
[
|
||||
$this,
|
||||
'populate_file',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates and creates the file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function populate_file(): void {
|
||||
if ( ! \wp_doing_cron() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $this->options_helper->get( 'enable_llms_txt', false ) !== true ) {
|
||||
$this->scheduler->unschedule_llms_txt_population();
|
||||
$this->remove_file_command_handler->handle();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->populate_file_command_handler->handle();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Llms_Txt\User_Interface;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Helpers\Options_Helper;
|
||||
use Yoast\WP\SEO\Integrations\Integration_Interface;
|
||||
use Yoast\WP\SEO\Llms_Txt\Application\File\Llms_Txt_Cron_Scheduler;
|
||||
|
||||
|
||||
/**
|
||||
* Handles the cron when the plugin is activated.
|
||||
*/
|
||||
class Schedule_Population_On_Activation_Integration implements Integration_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* The options helper.
|
||||
*
|
||||
* @var Options_Helper $options_helper
|
||||
*/
|
||||
private $options_helper;
|
||||
|
||||
/**
|
||||
* The scheduler.
|
||||
*
|
||||
* @var Llms_Txt_Cron_Scheduler $scheduler
|
||||
*/
|
||||
private $scheduler;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param Llms_Txt_Cron_Scheduler $scheduler The cron scheduler.
|
||||
* @param Options_Helper $options_helper The options helper.
|
||||
*/
|
||||
public function __construct(
|
||||
Llms_Txt_Cron_Scheduler $scheduler,
|
||||
Options_Helper $options_helper
|
||||
) {
|
||||
$this->scheduler = $scheduler;
|
||||
$this->options_helper = $options_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the scheduling of the cron to the activation action.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
\add_action( 'wpseo_activate', [ $this, 'schedule_llms_txt_population' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules the cron if the option is turned on.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function schedule_llms_txt_population() {
|
||||
if ( $this->options_helper->get( 'enable_llms_txt', false ) === true ) {
|
||||
$this->scheduler->schedule_quick_llms_txt_population();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user