PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
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 19.0 All 129 releases
wordpress-seo / admin / class-admin.php

class-admin.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.1, at admin/class-admin.php

380 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin
6 */
7
8 use Yoast\WP\SEO\Helpers\Wordpress_Helper;
9
10 /**
11 * Class that holds most of the admin functionality for Yoast SEO.
12 */
13 class WPSEO_Admin {
14
15 /**
16 * The page identifier used in WordPress to register the admin page.
17 *
18 * !DO NOT CHANGE THIS!
19 *
20 * @var string
21 */
22 const PAGE_IDENTIFIER = 'wpseo_dashboard';
23
24 /**
25 * Array of classes that add admin functionality.
26 *
27 * @var array
28 */
29 protected $admin_features;
30
31 /**
32 * Class constructor.
33 */
34 public function __construct() {
35 $integrations = [];
36
37 global $pagenow;
38
39 $wpseo_menu = new WPSEO_Menu();
40 $wpseo_menu->register_hooks();
41
42 if ( is_multisite() ) {
43 WPSEO_Options::maybe_set_multisite_defaults( false );
44 }
45
46 if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
47 add_action( 'created_category', [ $this, 'schedule_rewrite_flush' ] );
48 add_action( 'edited_category', [ $this, 'schedule_rewrite_flush' ] );
49 add_action( 'delete_category', [ $this, 'schedule_rewrite_flush' ] );
50 }
51
52 if ( WPSEO_Options::get( 'disable-attachment' ) === true ) {
53 add_filter( 'wpseo_accessible_post_types', [ 'WPSEO_Post_Type', 'filter_attachment_post_type' ] );
54 }
55
56 add_filter( 'plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 );
57 add_filter( 'network_admin_plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 );
58
59 add_action( 'admin_enqueue_scripts', [ $this, 'config_page_scripts' ] );
60 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_style' ] );
61
62 add_filter( 'user_contactmethods', [ $this, 'update_contactmethods' ], 10, 1 );
63
64 add_action( 'after_switch_theme', [ $this, 'switch_theme' ] );
65 add_action( 'switch_theme', [ $this, 'switch_theme' ] );
66
67 add_filter( 'set-screen-option', [ $this, 'save_bulk_edit_options' ], 10, 3 );
68
69 add_action( 'admin_init', [ 'WPSEO_Plugin_Conflict', 'hook_check_for_plugin_conflicts' ], 10, 1 );
70
71 add_action( 'admin_init', [ $this, 'map_manage_options_cap' ] );
72
73 WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo' );
74 WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'home' );
75
76 if ( YoastSEO()->helpers->current_page->is_yoast_seo_page() ) {
77 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
78 }
79
80 $this->set_upsell_notice();
81
82 $this->initialize_cornerstone_content();
83
84 if ( WPSEO_Utils::is_plugin_network_active() ) {
85 $integrations[] = new Yoast_Network_Admin();
86 }
87
88 $this->admin_features = [
89 'dashboard_widget' => new Yoast_Dashboard_Widget(),
90 ];
91
92 if ( WPSEO_Metabox::is_post_overview( $pagenow ) || WPSEO_Metabox::is_post_edit( $pagenow ) ) {
93 $this->admin_features['primary_category'] = new WPSEO_Primary_Term_Admin();
94 }
95
96 $integrations[] = new WPSEO_Yoast_Columns();
97 $integrations[] = new WPSEO_Statistic_Integration();
98 $integrations[] = new WPSEO_Capability_Manager_Integration( WPSEO_Capability_Manager_Factory::get() );
99 $integrations[] = new WPSEO_Admin_Gutenberg_Compatibility_Notification();
100 $integrations[] = new WPSEO_Expose_Shortlinks();
101 $integrations[] = new WPSEO_MyYoast_Proxy();
102 $integrations[] = new WPSEO_Schema_Person_Upgrade_Notification();
103 $integrations[] = new WPSEO_Tracking( 'https://tracking.yoast.com/stats', ( WEEK_IN_SECONDS * 2 ) );
104 $integrations[] = new WPSEO_Admin_Settings_Changed_Listener();
105
106 $integrations = array_merge(
107 $integrations,
108 $this->get_admin_features(),
109 $this->initialize_cornerstone_content()
110 );
111
112 foreach ( $integrations as $integration ) {
113 $integration->register_hooks();
114 }
115 }
116
117 /**
118 * Schedules a rewrite flush to happen at shutdown.
119 */
120 public function schedule_rewrite_flush() {
121 // Bail if this is a multisite installation and the site has been switched.
122 if ( is_multisite() && ms_is_switched() ) {
123 return;
124 }
125
126 add_action( 'shutdown', 'flush_rewrite_rules' );
127 }
128
129 /**
130 * Returns all the classes for the admin features.
131 *
132 * @return array
133 */
134 public function get_admin_features() {
135 return $this->admin_features;
136 }
137
138 /**
139 * Register assets needed on admin pages.
140 */
141 public function enqueue_assets() {
142 if ( filter_input( INPUT_GET, 'page' ) === 'wpseo_licenses' ) {
143 $asset_manager = new WPSEO_Admin_Asset_Manager();
144 $asset_manager->enqueue_style( 'extensions' );
145 }
146 }
147
148 /**
149 * Returns the manage_options capability.
150 *
151 * @return string The capability to use.
152 */
153 public function get_manage_options_cap() {
154 /**
155 * Filter: 'wpseo_manage_options_capability' - Allow changing the capability users need to view the settings pages.
156 *
157 * @api string unsigned The capability.
158 */
159 return apply_filters( 'wpseo_manage_options_capability', 'wpseo_manage_options' );
160 }
161
162 /**
163 * Maps the manage_options cap on saving an options page to wpseo_manage_options.
164 */
165 public function map_manage_options_cap() {
166 // phpcs:ignore WordPress.Security -- The variable is only used in strpos and thus safe to not unslash or sanitize.
167 $option_page = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : '';
168
169 if ( strpos( $option_page, 'yoast_wpseo' ) === 0 ) {
170 add_filter( 'option_page_capability_' . $option_page, [ $this, 'get_manage_options_cap' ] );
171 }
172 }
173
174 /**
175 * Adds the ability to choose how many posts are displayed per page
176 * on the bulk edit pages.
177 */
178 public function bulk_edit_options() {
179 $option = 'per_page';
180 $args = [
181 'label' => __( 'Posts', 'wordpress-seo' ),
182 'default' => 10,
183 'option' => 'wpseo_posts_per_page',
184 ];
185 add_screen_option( $option, $args );
186 }
187
188 /**
189 * Saves the posts per page limit for bulk edit pages.
190 *
191 * @param int $status Status value to pass through.
192 * @param string $option Option name.
193 * @param int $value Count value to check.
194 *
195 * @return int
196 */
197 public function save_bulk_edit_options( $status, $option, $value ) {
198 if ( $option && ( $value > 0 && $value < 1000 ) === 'wpseo_posts_per_page' ) {
199 return $value;
200 }
201
202 return $status;
203 }
204
205 /**
206 * Adds links to Premium Support and FAQ under the plugin in the plugin overview page.
207 *
208 * @param array $links Array of links for the plugins, adapted when the current plugin is found.
209 * @param string $file The filename for the current plugin, which the filter loops through.
210 *
211 * @return array
212 */
213 public function add_action_link( $links, $file ) {
214 if ( $file === WPSEO_BASENAME && WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
215 if ( is_network_admin() ) {
216 $settings_url = network_admin_url( 'admin.php?page=' . self::PAGE_IDENTIFIER );
217 }
218 else {
219 $settings_url = admin_url( 'admin.php?page=' . self::PAGE_IDENTIFIER );
220 }
221 $settings_link = '<a href="' . esc_url( $settings_url ) . '">' . __( 'Settings', 'wordpress-seo' ) . '</a>';
222 array_unshift( $links, $settings_link );
223 }
224
225 // Add link to docs.
226 $faq_link = '<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yc' ) ) . '" target="_blank">' . __( 'FAQ', 'wordpress-seo' ) . '</a>';
227 array_unshift( $links, $faq_link );
228
229 $addon_manager = new WPSEO_Addon_Manager();
230 if ( YoastSEO()->helpers->product->is_premium() ) {
231
232 // Remove Free 'deactivate' link if Premium is active as well. We don't want users to deactivate Free when Premium is active.
233 unset( $links['deactivate'] );
234 $no_deactivation_explanation = '<span style="color: #32373c">' . sprintf(
235 /* translators: %s expands to Yoast SEO Premium. */
236 __( 'Required by %s', 'wordpress-seo' ),
237 'Yoast SEO Premium'
238 ) . '</span>';
239
240 array_unshift( $links, $no_deactivation_explanation );
241
242 if ( $addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
243 return $links;
244 }
245
246 // Add link to where premium can be activated.
247 $activation_link = '<a style="font-weight: bold;" href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/activate-my-yoast' ) ) . '" target="_blank">' . __( 'Activate your subscription', 'wordpress-seo' ) . '</a>';
248 array_unshift( $links, $activation_link );
249
250 return $links;
251 }
252
253 // Add link to premium landing page.
254 $premium_link = '<a style="font-weight: bold;" href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yb' ) ) . '" target="_blank">' . __( 'Get Premium', 'wordpress-seo' ) . '</a>';
255 array_unshift( $links, $premium_link );
256
257 return $links;
258 }
259
260 /**
261 * Enqueues the (tiny) global JS needed for the plugin.
262 */
263 public function config_page_scripts() {
264 $asset_manager = new WPSEO_Admin_Asset_Manager();
265 $asset_manager->enqueue_script( 'admin-global' );
266 $asset_manager->localize_script( 'admin-global', 'wpseoAdminGlobalL10n', $this->localize_admin_global_script() );
267 }
268
269 /**
270 * Enqueues the (tiny) global stylesheet needed for the plugin.
271 */
272 public function enqueue_global_style() {
273 $asset_manager = new WPSEO_Admin_Asset_Manager();
274 $asset_manager->enqueue_style( 'admin-global' );
275 }
276
277 /**
278 * Filter the $contactmethods array and add a set of social profiles.
279 *
280 * These are used with the Facebook author, rel="author" and Twitter cards implementation.
281 *
282 * @param array $contactmethods Currently set contactmethods.
283 *
284 * @return array Contactmethods with added contactmethods.
285 */
286 public function update_contactmethods( $contactmethods ) {
287 $contactmethods['facebook'] = __( 'Facebook profile URL', 'wordpress-seo' );
288 $contactmethods['instagram'] = __( 'Instagram profile URL', 'wordpress-seo' );
289 $contactmethods['linkedin'] = __( 'LinkedIn profile URL', 'wordpress-seo' );
290 $contactmethods['myspace'] = __( 'MySpace profile URL', 'wordpress-seo' );
291 $contactmethods['pinterest'] = __( 'Pinterest profile URL', 'wordpress-seo' );
292 $contactmethods['soundcloud'] = __( 'SoundCloud profile URL', 'wordpress-seo' );
293 $contactmethods['tumblr'] = __( 'Tumblr profile URL', 'wordpress-seo' );
294 $contactmethods['twitter'] = __( 'Twitter username (without @)', 'wordpress-seo' );
295 $contactmethods['youtube'] = __( 'YouTube profile URL', 'wordpress-seo' );
296 $contactmethods['wikipedia'] = __( 'Wikipedia page about you', 'wordpress-seo' ) . '<br/><small>' . __( '(if one exists)', 'wordpress-seo' ) . '</small>';
297
298 return $contactmethods;
299 }
300
301 /**
302 * Log the updated timestamp for user profiles when theme is changed.
303 */
304 public function switch_theme() {
305 $wordpress_helper = new Wordpress_Helper();
306 $wordpress_version = $wordpress_helper->get_wordpress_version();
307
308 // Capability queries were only introduced in WP 5.9.
309 if ( version_compare( $wordpress_version, '5.8.99', '<' ) ) {
310 $users = get_users( [ 'who' => 'authors' ] );
311 }
312 else {
313 $users = get_users( [ 'capability' => [ 'edit_posts' ] ] );
314 }
315
316 if ( is_array( $users ) && $users !== [] ) {
317 foreach ( $users as $user ) {
318 update_user_meta( $user->ID, '_yoast_wpseo_profile_updated', time() );
319 }
320 }
321 }
322
323 /**
324 * Localization for the dismiss urls.
325 *
326 * @return array
327 */
328 private function localize_admin_global_script() {
329 return array_merge(
330 [
331 'isRtl' => is_rtl(),
332 'variable_warning' => sprintf(
333 /* translators: %1$s: '%%term_title%%' variable used in titles and meta's template that's not compatible with the given template, %2$s: expands to 'HelpScout beacon' */
334 __( 'Warning: the variable %1$s cannot be used in this template. See the %2$s for more info.', 'wordpress-seo' ),
335 '<code>%s</code>',
336 'HelpScout beacon'
337 ),
338 /* translators: %s: expends to Yoast SEO */
339 'help_video_iframe_title' => sprintf( __( '%s video tutorial', 'wordpress-seo' ), 'Yoast SEO' ),
340 'scrollable_table_hint' => __( 'Scroll to see the table content.', 'wordpress-seo' ),
341 'wincher_is_logged_in' => WPSEO_Options::get( 'wincher_integration_active', true ) ? YoastSEO()->helpers->wincher->login_status() : false,
342 ],
343 YoastSEO()->helpers->wincher->get_admin_global_links()
344 );
345 }
346
347 /**
348 * Sets the upsell notice.
349 */
350 protected function set_upsell_notice() {
351 $upsell = new WPSEO_Product_Upsell_Notice();
352 $upsell->dismiss_notice_listener();
353 $upsell->initialize();
354 }
355
356 /**
357 * Whether we are on the admin dashboard page.
358 *
359 * @return bool
360 */
361 protected function on_dashboard_page() {
362 return $GLOBALS['pagenow'] === 'index.php';
363 }
364
365 /**
366 * Loads the cornerstone filter.
367 *
368 * @return WPSEO_WordPress_Integration[] The integrations to initialize.
369 */
370 protected function initialize_cornerstone_content() {
371 if ( ! WPSEO_Options::get( 'enable_cornerstone_content' ) ) {
372 return [];
373 }
374
375 return [
376 'cornerstone_filter' => new WPSEO_Cornerstone_Filter(),
377 ];
378 }
379 }
380