wordpress-seo
/
src
/
llms-txt
/
user-interface
/
file-failure-llms-txt-notification-integration.php
file-failure-llms-txt-notification-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.6, at src/llms-txt/user-interface/file-failure-llms-txt-notification-integration.php
| 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\File_Failure_Notification_Presenter; |
| 10 | use Yoast_Notification; |
| 11 | use Yoast_Notification_Center; |
| 12 | |
| 13 | /** |
| 14 | * Watches and handles changes to the LLMS.txt file failure option. |
| 15 | * |
| 16 | * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 17 | */ |
| 18 | class File_Failure_Llms_Txt_Notification_Integration implements Integration_Interface { |
| 19 | |
| 20 | use No_Conditionals; |
| 21 | |
| 22 | /** |
| 23 | * The notification ID. |
| 24 | */ |
| 25 | public const NOTIFICATION_ID = 'wpseo-llms-txt-generation-failure'; |
| 26 | |
| 27 | /** |
| 28 | * The options helper. |
| 29 | * |
| 30 | * @var Options_Helper |
| 31 | */ |
| 32 | private $options_helper; |
| 33 | |
| 34 | /** |
| 35 | * The notification center. |
| 36 | * |
| 37 | * @var Yoast_Notification_Center |
| 38 | */ |
| 39 | private $notification_center; |
| 40 | |
| 41 | /** |
| 42 | * The notification presenter. |
| 43 | * |
| 44 | * @var File_Failure_Notification_Presenter |
| 45 | */ |
| 46 | private $presenter; |
| 47 | |
| 48 | /** |
| 49 | * Constructor. |
| 50 | * |
| 51 | * @param Options_Helper $options_helper The options helper. |
| 52 | * @param Yoast_Notification_Center $notification_center The notification center. |
| 53 | * @param File_Failure_Notification_Presenter $presenter The notification presenter. |
| 54 | */ |
| 55 | public function __construct( |
| 56 | Options_Helper $options_helper, |
| 57 | Yoast_Notification_Center $notification_center, |
| 58 | File_Failure_Notification_Presenter $presenter |
| 59 | ) { |
| 60 | $this->options_helper = $options_helper; |
| 61 | $this->notification_center = $notification_center; |
| 62 | $this->presenter = $presenter; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Initializes the integration. |
| 67 | * |
| 68 | * This is the place to register hooks and filters. |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | public function register_hooks() { |
| 73 | \add_action( 'admin_init', [ $this, 'maybe_show_notification' ], 10, 2 ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Manage the search engines discouraged notification. |
| 78 | * |
| 79 | * Shows the notification if needed and deletes it if needed. |
| 80 | * |
| 81 | * @return void |
| 82 | */ |
| 83 | public function maybe_show_notification() { |
| 84 | if ( ! $this->should_show_file_failure_notification() ) { |
| 85 | $this->remove_file_failure_notification_if_exists(); |
| 86 | } |
| 87 | else { |
| 88 | $this->maybe_add_file_failure_notification(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Whether the file failure notification should be shown. |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | private function should_show_file_failure_notification(): bool { |
| 98 | return $this->options_helper->get( 'enable_llms_txt', false ) && \get_option( Populate_File_Command_Handler::GENERATION_FAILURE_OPTION, false ) !== false; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Remove the search engines discouraged notification if it exists. |
| 103 | * |
| 104 | * @return void |
| 105 | */ |
| 106 | private function remove_file_failure_notification_if_exists() { |
| 107 | $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Add the search engines discouraged notification if it does not exist yet. |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | private function maybe_add_file_failure_notification() { |
| 116 | if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) { |
| 117 | $notification = new Yoast_Notification( |
| 118 | $this->presenter->present(), |
| 119 | [ |
| 120 | 'type' => Yoast_Notification::ERROR, |
| 121 | 'id' => self::NOTIFICATION_ID, |
| 122 | 'capabilities' => 'wpseo_manage_options', |
| 123 | 'priority' => 1, |
| 124 | ], |
| 125 | ); |
| 126 | $this->notification_center->restore_notification( $notification ); |
| 127 | $this->notification_center->add_notification( $notification ); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 |