PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.1
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 / integrations / watchers / search-engines-discouraged-watcher.php

search-engines-discouraged-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.1, at src/integrations/watchers/search-engines-discouraged-watcher.php

247 lines 7.0 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\Integrations\Watchers;
4
5 use Yoast\WP\SEO\Conditionals\No_Conditionals;
6 use Yoast\WP\SEO\Helpers\Capability_Helper;
7 use Yoast\WP\SEO\Helpers\Current_Page_Helper;
8 use Yoast\WP\SEO\Helpers\Notification_Helper;
9 use Yoast\WP\SEO\Helpers\Options_Helper;
10 use Yoast\WP\SEO\Integrations\Integration_Interface;
11 use Yoast\WP\SEO\Presenters\Admin\Search_Engines_Discouraged_Presenter;
12 use Yoast_Notification;
13 use Yoast_Notification_Center;
14
15 /**
16 * Shows a notification for users who have access for robots disabled.
17 */
18 class Search_Engines_Discouraged_Watcher implements Integration_Interface {
19
20 use No_Conditionals;
21
22 /**
23 * The notification ID.
24 */
25 public const NOTIFICATION_ID = 'wpseo-search-engines-discouraged';
26
27 /**
28 * The Yoast notification center.
29 *
30 * @var Yoast_Notification_Center
31 */
32 protected $notification_center;
33
34 /**
35 * The notification helper.
36 *
37 * @var Notification_Helper
38 */
39 protected $notification_helper;
40
41 /**
42 * The search engines discouraged presenter.
43 *
44 * @var Search_Engines_Discouraged_Presenter
45 */
46 protected $presenter;
47
48 /**
49 * The current page helper.
50 *
51 * @var Current_Page_Helper
52 */
53 protected $current_page_helper;
54
55 /**
56 * The options helper.
57 *
58 * @var Options_Helper
59 */
60 protected $options_helper;
61
62 /**
63 * The capability helper.
64 *
65 * @var Capability_Helper
66 */
67 protected $capability_helper;
68
69 /**
70 * Search_Engines_Discouraged_Watcher constructor.
71 *
72 * @param Yoast_Notification_Center $notification_center The notification center.
73 * @param Notification_Helper $notification_helper The notification helper.
74 * @param Current_Page_Helper $current_page_helper The current page helper.
75 * @param Options_Helper $options_helper The options helper.
76 * @param Capability_Helper $capability_helper The capability helper.
77 */
78 public function __construct(
79 Yoast_Notification_Center $notification_center,
80 Notification_Helper $notification_helper,
81 Current_Page_Helper $current_page_helper,
82 Options_Helper $options_helper,
83 Capability_Helper $capability_helper
84 ) {
85 $this->notification_center = $notification_center;
86 $this->notification_helper = $notification_helper;
87 $this->current_page_helper = $current_page_helper;
88 $this->options_helper = $options_helper;
89 $this->capability_helper = $capability_helper;
90 $this->presenter = new Search_Engines_Discouraged_Presenter();
91 }
92
93 /**
94 * Initializes the integration.
95 *
96 * On admin_init, it is checked whether the notification about search engines being discouraged should be shown.
97 * On admin_notices, the notice about the search engines being discouraged will be shown when necessary.
98 *
99 * @return void
100 */
101 public function register_hooks() {
102 \add_action( 'admin_init', [ $this, 'manage_search_engines_discouraged_notification' ] );
103
104 \add_action( 'update_option_blog_public', [ $this, 'restore_ignore_option' ] );
105
106 /*
107 * The `admin_notices` hook fires on single site admin pages vs.
108 * `network_admin_notices` which fires on multisite admin pages and
109 * `user_admin_notices` which fires on multisite user admin pages.
110 */
111 \add_action( 'admin_notices', [ $this, 'maybe_show_search_engines_discouraged_notice' ] );
112 }
113
114 /**
115 * Manage the search engines discouraged notification.
116 *
117 * Shows the notification if needed and deletes it if needed.
118 *
119 * @return void
120 */
121 public function manage_search_engines_discouraged_notification() {
122 if ( ! $this->should_show_search_engines_discouraged_notification() ) {
123 $this->remove_search_engines_discouraged_notification_if_exists();
124 }
125 else {
126 $this->maybe_add_search_engines_discouraged_notification();
127 }
128 }
129
130 /**
131 * Show the search engine discouraged notice when needed.
132 *
133 * @return void
134 */
135 public function maybe_show_search_engines_discouraged_notice() {
136 if ( ! $this->should_show_search_engines_discouraged_notice() ) {
137 return;
138 }
139 $this->show_search_engines_discouraged_notice();
140 }
141
142 /**
143 * Whether the search engines discouraged notification should be shown.
144 *
145 * @return bool
146 */
147 protected function should_show_search_engines_discouraged_notification() {
148 return $this->search_engines_are_discouraged() && $this->options_helper->get( 'ignore_search_engines_discouraged_notice', false ) === false;
149 }
150
151 /**
152 * Remove the search engines discouraged notification if it exists.
153 *
154 * @return void
155 */
156 protected function remove_search_engines_discouraged_notification_if_exists() {
157 $this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
158 }
159
160 /**
161 * Add the search engines discouraged notification if it does not exist yet.
162 *
163 * @return void
164 */
165 protected function maybe_add_search_engines_discouraged_notification() {
166 if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) {
167 $notification = $this->notification();
168 $this->notification_helper->restore_notification( $notification );
169 $this->notification_center->add_notification( $notification );
170 }
171 }
172
173 /**
174 * Checks whether search engines are discouraged from indexing the site.
175 *
176 * @return bool Whether search engines are discouraged from indexing the site.
177 */
178 protected function search_engines_are_discouraged() {
179 return (string) \get_option( 'blog_public' ) === '0';
180 }
181
182 /**
183 * Whether the search engines notice should be shown.
184 *
185 * @return bool
186 */
187 protected function should_show_search_engines_discouraged_notice() {
188 $pages_to_show_notice = [
189 'index.php',
190 'plugins.php',
191 'update-core.php',
192 ];
193
194 return (
195 $this->search_engines_are_discouraged()
196 && $this->capability_helper->current_user_can( 'manage_options' )
197 && $this->options_helper->get( 'ignore_search_engines_discouraged_notice', false ) === false
198 && (
199 $this->current_page_helper->is_yoast_seo_page()
200 || \in_array( $this->current_page_helper->get_current_admin_page(), $pages_to_show_notice, true )
201 )
202 && $this->current_page_helper->get_current_yoast_seo_page() !== 'wpseo_dashboard'
203 );
204 }
205
206 /**
207 * Show the search engines discouraged notice.
208 *
209 * @return void
210 */
211 protected function show_search_engines_discouraged_notice() {
212 \printf(
213 '<div id="robotsmessage" class="notice notice-error">%1$s</div>',
214 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output from present() is considered safe.
215 $this->presenter->present(),
216 );
217 }
218
219 /**
220 * Returns an instance of the notification.
221 *
222 * @return Yoast_Notification The notification to show.
223 */
224 protected function notification() {
225 return new Yoast_Notification(
226 $this->presenter->present(),
227 [
228 'type' => Yoast_Notification::ERROR,
229 'id' => self::NOTIFICATION_ID,
230 'capabilities' => 'wpseo_manage_options',
231 'priority' => 1,
232 ],
233 );
234 }
235
236 /**
237 * Should restore the ignore option for the search engines discouraged notice.
238 *
239 * @return void
240 */
241 public function restore_ignore_option() {
242 if ( ! $this->search_engines_are_discouraged() ) {
243 $this->options_helper->set( 'ignore_search_engines_discouraged_notice', false );
244 }
245 }
246 }
247