PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.6
28.6 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 All 130 releases
wordpress-seo / src / llms-txt / user-interface / enable-llms-txt-option-watcher.php

enable-llms-txt-option-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.6, at src/llms-txt/user-interface/enable-llms-txt-option-watcher.php

142 lines 4.1 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\Llms_Txt\User_Interface;
4
5 use Yoast\WP\SEO\Conditionals\No_Conditionals;
6 use Yoast\WP\SEO\Helpers\Options_Helper;
7 use Yoast\WP\SEO\Integrations\Integration_Interface;
8 use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Populate_File_Command_Handler;
9 use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Remove_File_Command_Handler;
10 use Yoast\WP\SEO\Llms_Txt\Application\File\Llms_Txt_Cron_Scheduler;
11
12 /**
13 * Watches and handles changes to the LLMS.txt enabled option.
14 */
15 class Enable_Llms_Txt_Option_Watcher implements Integration_Interface {
16
17 use No_Conditionals;
18
19 /**
20 * The option names that should trigger a population of the llms.txt file.
21 *
22 * @var string[]
23 */
24 private $option_names = [
25 'llms_txt_selection_mode',
26 'about_us_page',
27 'contact_page',
28 'terms_page',
29 'privacy_policy_page',
30 'shop_page',
31 'other_included_pages',
32 ];
33
34 /**
35 * The scheduler.
36 *
37 * @var Llms_Txt_Cron_Scheduler
38 */
39 private $scheduler;
40
41 /**
42 * The remove file command handler.
43 *
44 * @var Remove_File_Command_Handler
45 */
46 private $remove_file_command_handler;
47
48 /**
49 * The populate file command handler.
50 *
51 * @var Populate_File_Command_Handler
52 */
53 private $populate_file_command_handler;
54
55 /**
56 * The options helper.
57 *
58 * @var Options_Helper
59 */
60 private $options_helper;
61
62 /**
63 * Constructor.
64 *
65 * @param Llms_Txt_Cron_Scheduler $scheduler The cron scheduler.
66 * @param Remove_File_Command_Handler $remove_file_command_handler The remove file command handler.
67 * @param Populate_File_Command_Handler $populate_file_command_handler The populate file command handler.
68 * @param Options_Helper $options_helper The options helper.
69 */
70 public function __construct(
71 Llms_Txt_Cron_Scheduler $scheduler,
72 Remove_File_Command_Handler $remove_file_command_handler,
73 Populate_File_Command_Handler $populate_file_command_handler,
74 Options_Helper $options_helper
75 ) {
76 $this->scheduler = $scheduler;
77 $this->remove_file_command_handler = $remove_file_command_handler;
78 $this->populate_file_command_handler = $populate_file_command_handler;
79 $this->options_helper = $options_helper;
80 }
81
82 /**
83 * Initializes the integration.
84 *
85 * This is the place to register hooks and filters.
86 *
87 * @return void
88 */
89 public function register_hooks() {
90 \add_action( 'update_option_wpseo', [ $this, 'check_toggle_llms_txt' ], 10, 2 );
91 \add_action( 'update_option_wpseo_llmstxt', [ $this, 'check_llms_txt_selection' ], 10, 2 );
92 }
93
94 /**
95 * Checks if the LLMS.txt feature is toggled.
96 *
97 * @param array<string|int|bool|array<string|int|bool>> $old_value The old value of the option.
98 * @param array<string|int|bool|array<string|int|bool>> $new_value The new value of the option.
99 *
100 * @return void
101 */
102 public function check_toggle_llms_txt( $old_value, $new_value ): void {
103 $option_name = 'enable_llms_txt';
104
105 if ( \array_key_exists( $option_name, $old_value ) && \array_key_exists( $option_name, $new_value ) && $old_value[ $option_name ] !== $new_value[ $option_name ] ) {
106 if ( $new_value[ $option_name ] === true ) {
107 $this->scheduler->schedule_weekly_llms_txt_population();
108 $this->populate_file_command_handler->handle();
109 }
110 else {
111 $this->scheduler->unschedule_llms_txt_population();
112 $this->remove_file_command_handler->handle();
113 }
114 }
115 }
116
117 /**
118 * Checks if any of the llms.txt settings were changed.
119 *
120 * @param array<string, string|int|array<int>> $old_value The old value of the option.
121 * @param array<string, string|int|array<int>> $new_value The new value of the option.
122 *
123 * @return void
124 */
125 public function check_llms_txt_selection( $old_value, $new_value ): void {
126 if ( $this->options_helper->get( 'enable_llms_txt', false ) !== true ) {
127 return;
128 }
129
130 foreach ( $this->option_names as $option_name ) {
131 if ( ! \array_key_exists( $option_name, $old_value ) || ! \array_key_exists( $option_name, $new_value ) ) {
132 continue;
133 }
134
135 if ( $old_value[ $option_name ] !== $new_value[ $option_name ] ) {
136 $this->populate_file_command_handler->handle();
137 return;
138 }
139 }
140 }
141 }
142