• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / abba4d5bc97ae5733ba89412cc79bb357e5d7f21

22 Oct 2024 07:57AM UTC coverage: 54.556% (+0.06%) from 54.494%
abba4d5bc97ae5733ba89412cc79bb357e5d7f21

push

github

YoastBot
Bump version to 23.7 on free

7539 of 13559 branches covered (55.6%)

Branch coverage included in aggregate %.

29669 of 54642 relevant lines covered (54.3%)

41714.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

1.41
/admin/class-admin.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin
6
 */
7

8
use Yoast\WP\SEO\Conditionals\New_Dashboard_Ui_Conditional;
9
use Yoast\WP\SEO\Integrations\Settings_Integration;
10

11
/**
12
 * Class that holds most of the admin functionality for Yoast SEO.
13
 */
14
class WPSEO_Admin {
15

16
        /**
17
         * The page identifier used in WordPress to register the admin page.
18
         *
19
         * !DO NOT CHANGE THIS!
20
         *
21
         * @var string
22
         */
23
        public const PAGE_IDENTIFIER = 'wpseo_dashboard';
24

25
        /**
26
         * Array of classes that add admin functionality.
27
         *
28
         * @var array
29
         */
30
        protected $admin_features;
31

32
        /**
33
         * Class constructor.
34
         */
35
        public function __construct() {
×
36
                $integrations = [];
×
37

38
                global $pagenow;
×
39

40
                $wpseo_menu = new WPSEO_Menu();
×
41
                $wpseo_menu->register_hooks();
×
42

43
                if ( is_multisite() ) {
×
44
                        WPSEO_Options::maybe_set_multisite_defaults( false );
×
45
                }
46

47
                if ( WPSEO_Options::get( 'stripcategorybase' ) === true ) {
×
48
                        add_action( 'created_category', [ $this, 'schedule_rewrite_flush' ] );
×
49
                        add_action( 'edited_category', [ $this, 'schedule_rewrite_flush' ] );
×
50
                        add_action( 'delete_category', [ $this, 'schedule_rewrite_flush' ] );
×
51
                }
52

53
                if ( WPSEO_Options::get( 'disable-attachment' ) === true ) {
×
54
                        add_filter( 'wpseo_accessible_post_types', [ 'WPSEO_Post_Type', 'filter_attachment_post_type' ] );
×
55
                }
56

57
                add_filter( 'plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 );
×
58
                add_filter( 'network_admin_plugin_action_links_' . WPSEO_BASENAME, [ $this, 'add_action_link' ], 10, 2 );
×
59

60
                add_action( 'admin_enqueue_scripts', [ $this, 'config_page_scripts' ] );
×
61
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_style' ] );
×
62

63
                add_action( 'after_switch_theme', [ $this, 'switch_theme' ] );
×
64
                add_action( 'switch_theme', [ $this, 'switch_theme' ] );
×
65

66
                add_filter( 'set-screen-option', [ $this, 'save_bulk_edit_options' ], 10, 3 );
×
67

68
                add_action( 'admin_init', [ 'WPSEO_Plugin_Conflict', 'hook_check_for_plugin_conflicts' ], 10, 1 );
×
69

70
                add_action( 'admin_init', [ $this, 'map_manage_options_cap' ] );
×
71

72
                WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'wpseo' );
×
73
                WPSEO_Sitemaps_Cache::register_clear_on_option_update( 'home' );
×
74

75
                if ( YoastSEO()->helpers->current_page->is_yoast_seo_page() ) {
×
76
                        add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
×
77
                }
78

79
                $this->set_upsell_notice();
×
80

81
                $this->initialize_cornerstone_content();
×
82

83
                if ( WPSEO_Utils::is_plugin_network_active() ) {
×
84
                        $integrations[] = new Yoast_Network_Admin();
×
85
                }
86

87
                $this->admin_features = [
×
88
                        'dashboard_widget'         => new Yoast_Dashboard_Widget(),
×
89
                        'wincher_dashboard_widget' => new Wincher_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
         * @return void
121
         */
122
        public function schedule_rewrite_flush() {
×
123
                // Bail if this is a multisite installation and the site has been switched.
124
                if ( is_multisite() && ms_is_switched() ) {
×
125
                        return;
×
126
                }
127

128
                add_action( 'shutdown', 'flush_rewrite_rules' );
×
129
        }
130

131
        /**
132
         * Returns all the classes for the admin features.
133
         *
134
         * @return array
135
         */
136
        public function get_admin_features() {
4✔
137
                return $this->admin_features;
4✔
138
        }
139

140
        /**
141
         * Register assets needed on admin pages.
142
         *
143
         * @return void
144
         */
145
        public function enqueue_assets() {
×
146
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form data.
147
                $page = isset( $_GET['page'] ) && is_string( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
×
148
                if ( $page === 'wpseo_licenses' ) {
×
149
                        $asset_manager = new WPSEO_Admin_Asset_Manager();
×
150
                        $asset_manager->enqueue_style( 'extensions' );
×
151
                }
152
        }
153

154
        /**
155
         * Returns the manage_options capability.
156
         *
157
         * @return string The capability to use.
158
         */
159
        public function get_manage_options_cap() {
×
160
                /**
161
                 * Filter: 'wpseo_manage_options_capability' - Allow changing the capability users need to view the settings pages.
162
                 *
163
                 * @param string $capability The capability.
164
                 */
165
                return apply_filters( 'wpseo_manage_options_capability', 'wpseo_manage_options' );
×
166
        }
167

168
        /**
169
         * Maps the manage_options cap on saving an options page to wpseo_manage_options.
170
         *
171
         * @return void
172
         */
173
        public function map_manage_options_cap() {
×
174
                // phpcs:ignore WordPress.Security -- The variable is only used in strpos and thus safe to not unslash or sanitize.
175
                $option_page = ! empty( $_POST['option_page'] ) ? $_POST['option_page'] : '';
×
176

177
                if ( strpos( $option_page, 'yoast_wpseo' ) === 0 || strpos( $option_page, Settings_Integration::PAGE ) === 0 ) {
×
178
                        add_filter( 'option_page_capability_' . $option_page, [ $this, 'get_manage_options_cap' ] );
×
179
                }
180
        }
181

182
        /**
183
         * Adds the ability to choose how many posts are displayed per page
184
         * on the bulk edit pages.
185
         *
186
         * @return void
187
         */
188
        public function bulk_edit_options() {
×
189
                $option = 'per_page';
×
190
                $args   = [
191
                        'label'   => __( 'Posts', 'wordpress-seo' ),
×
192
                        'default' => 10,
×
193
                        'option'  => 'wpseo_posts_per_page',
×
194
                ];
195
                add_screen_option( $option, $args );
×
196
        }
197

198
        /**
199
         * Saves the posts per page limit for bulk edit pages.
200
         *
201
         * @param int    $status Status value to pass through.
202
         * @param string $option Option name.
203
         * @param int    $value  Count value to check.
204
         *
205
         * @return int
206
         */
207
        public function save_bulk_edit_options( $status, $option, $value ) {
×
208
                if ( $option && ( $value > 0 && $value < 1000 ) === 'wpseo_posts_per_page' ) {
×
209
                        return $value;
×
210
                }
211

212
                return $status;
×
213
        }
214

215
        /**
216
         * Adds links to Premium Support and FAQ under the plugin in the plugin overview page.
217
         *
218
         * @param array  $links Array of links for the plugins, adapted when the current plugin is found.
219
         * @param string $file  The filename for the current plugin, which the filter loops through.
220
         *
221
         * @return array
222
         */
223
        public function add_action_link( $links, $file ) {
×
224
                $first_time_configuration_notice_helper = YoastSEO()->helpers->first_time_configuration_notice;
×
225

226
                if ( $file === WPSEO_BASENAME && WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) {
×
227
                        if ( is_network_admin() ) {
×
228
                                $settings_url = network_admin_url( 'admin.php?page=' . self::PAGE_IDENTIFIER );
×
229
                        }
230
                        else {
231
                                $settings_url = admin_url( 'admin.php?page=' . self::PAGE_IDENTIFIER );
×
232
                        }
233
                        $settings_link = '<a href="' . esc_url( $settings_url ) . '">' . __( 'Settings', 'wordpress-seo' ) . '</a>';
×
234
                        array_unshift( $links, $settings_link );
×
235
                }
236

237
                // Add link to docs.
238
                $faq_link = '<a href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yc' ) ) . '" target="_blank">' . __( 'FAQ', 'wordpress-seo' ) . '</a>';
×
239
                array_unshift( $links, $faq_link );
×
240

241
                if ( $first_time_configuration_notice_helper->first_time_configuration_not_finished() && ! is_network_admin() ) {
×
242
                        $configuration_title = ( ! $first_time_configuration_notice_helper->should_show_alternate_message() ) ? 'first-time configuration' : 'SEO configuration';
×
243
                        /* translators: CTA to finish the first time configuration. %s: Either first-time SEO configuration or SEO configuration. */
244
                        $message  = sprintf( __( 'Finish your %s', 'wordpress-seo' ), $configuration_title );
×
245
                        $ftc_page = ( ( new New_Dashboard_Ui_Conditional() )->is_met() ) ? 'admin.php?page=wpseo_dashboard#/first-time-configuration' : 'admin.php?page=wpseo_dashboard#top#first-time-configuration';
×
246
                        $ftc_link = '<a href="' . esc_url( admin_url( $ftc_page ) ) . '" target="_blank">' . $message . '</a>';
×
247
                        array_unshift( $links, $ftc_link );
×
248
                }
249

250
                $addon_manager = new WPSEO_Addon_Manager();
×
251
                if ( YoastSEO()->helpers->product->is_premium() ) {
×
252

253
                        // Remove Free 'deactivate' link if Premium is active as well. We don't want users to deactivate Free when Premium is active.
254
                        unset( $links['deactivate'] );
×
255
                        $no_deactivation_explanation = '<span style="color: #32373c">' . sprintf(
×
256
                                /* translators: %s expands to Yoast SEO Premium. */
257
                                __( 'Required by %s', 'wordpress-seo' ),
×
258
                                'Yoast SEO Premium'
×
259
                        ) . '</span>';
×
260

261
                        array_unshift( $links, $no_deactivation_explanation );
×
262

263
                        if ( $addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ) ) {
×
264
                                return $links;
×
265
                        }
266

267
                        // Add link to where premium can be activated.
268
                        $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>';
×
269
                        array_unshift( $links, $activation_link );
×
270

271
                        return $links;
×
272
                }
273

274
                // Add link to premium landing page.
275
                $premium_link = '<a style="font-weight: bold;" href="' . esc_url( WPSEO_Shortlinker::get( 'https://yoa.st/1yb' ) ) . '" target="_blank" data-action="load-nfd-ctb" data-ctb-id="f6a84663-465f-4cb5-8ba5-f7a6d72224b2">' . __( 'Get Premium', 'wordpress-seo' ) . '</a>';
×
276
                array_unshift( $links, $premium_link );
×
277

278
                return $links;
×
279
        }
280

281
        /**
282
         * Enqueues the (tiny) global JS needed for the plugin.
283
         *
284
         * @return void
285
         */
286
        public function config_page_scripts() {
×
287
                $asset_manager = new WPSEO_Admin_Asset_Manager();
×
288
                $asset_manager->enqueue_script( 'admin-global' );
×
289
                $asset_manager->localize_script( 'admin-global', 'wpseoAdminGlobalL10n', $this->localize_admin_global_script() );
×
290
        }
291

292
        /**
293
         * Enqueues the (tiny) global stylesheet needed for the plugin.
294
         *
295
         * @return void
296
         */
297
        public function enqueue_global_style() {
×
298
                $asset_manager = new WPSEO_Admin_Asset_Manager();
×
299
                $asset_manager->enqueue_style( 'admin-global' );
×
300
        }
301

302
        /**
303
         * Filter the $contactmethods array and add a set of social profiles.
304
         *
305
         * These are used with the Facebook author, rel="author" and Twitter cards implementation.
306
         *
307
         * @deprecated 22.6
308
         * @codeCoverageIgnore
309
         *
310
         * @param array<string, string> $contactmethods Currently set contactmethods.
311
         *
312
         * @return array<string, string> Contactmethods with added contactmethods.
313
         */
314
        public function update_contactmethods( $contactmethods ) {
315
                _deprecated_function( __METHOD__, 'Yoast SEO 22.6' );
316

317
                $contactmethods['facebook']   = __( 'Facebook profile URL', 'wordpress-seo' );
318
                $contactmethods['instagram']  = __( 'Instagram profile URL', 'wordpress-seo' );
319
                $contactmethods['linkedin']   = __( 'LinkedIn profile URL', 'wordpress-seo' );
320
                $contactmethods['myspace']    = __( 'MySpace profile URL', 'wordpress-seo' );
321
                $contactmethods['pinterest']  = __( 'Pinterest profile URL', 'wordpress-seo' );
322
                $contactmethods['soundcloud'] = __( 'SoundCloud profile URL', 'wordpress-seo' );
323
                $contactmethods['tumblr']     = __( 'Tumblr profile URL', 'wordpress-seo' );
324
                $contactmethods['twitter']    = __( 'X username (without @)', 'wordpress-seo' );
325
                $contactmethods['youtube']    = __( 'YouTube profile URL', 'wordpress-seo' );
326
                $contactmethods['wikipedia']  = __( 'Wikipedia page about you', 'wordpress-seo' ) . '<br/><small>' . __( '(if one exists)', 'wordpress-seo' ) . '</small>';
327

328
                return $contactmethods;
329
        }
330

331
        /**
332
         * Log the updated timestamp for user profiles when theme is changed.
333
         *
334
         * @return void
335
         */
336
        public function switch_theme() {
×
337

338
                $users = get_users( [ 'capability' => [ 'edit_posts' ] ] );
×
339

340
                if ( is_array( $users ) && $users !== [] ) {
×
341
                        foreach ( $users as $user ) {
×
342
                                update_user_meta( $user->ID, '_yoast_wpseo_profile_updated', time() );
×
343
                        }
344
                }
345
        }
346

347
        /**
348
         * Localization for the dismiss urls.
349
         *
350
         * @return array
351
         */
352
        private function localize_admin_global_script() {
×
353
                return array_merge(
×
354
                        [
355
                                'isRtl'                   => is_rtl(),
×
356
                                'variable_warning'        => sprintf(
×
357
                                /* 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' */
358
                                        __( 'Warning: the variable %1$s cannot be used in this template. See the %2$s for more info.', 'wordpress-seo' ),
×
359
                                        '<code>%s</code>',
×
360
                                        'HelpScout beacon'
×
361
                                ),
362
                                /* translators: %s: expends to Yoast SEO */
363
                                'help_video_iframe_title' => sprintf( __( '%s video tutorial', 'wordpress-seo' ), 'Yoast SEO' ),
×
364
                                'scrollable_table_hint'   => __( 'Scroll to see the table content.', 'wordpress-seo' ),
×
365
                                'wincher_is_logged_in'    => WPSEO_Options::get( 'wincher_integration_active', true ) ? YoastSEO()->helpers->wincher->login_status() : false,
×
366
                        ],
367
                        YoastSEO()->helpers->wincher->get_admin_global_links()
×
368
                );
369
        }
370

371
        /**
372
         * Sets the upsell notice.
373
         *
374
         * @return void
375
         */
376
        protected function set_upsell_notice() {
×
377
                $upsell = new WPSEO_Product_Upsell_Notice();
×
378
                $upsell->dismiss_notice_listener();
×
379
                $upsell->initialize();
×
380
        }
381

382
        /**
383
         * Whether we are on the admin dashboard page.
384
         *
385
         * @return bool
386
         */
387
        protected function on_dashboard_page() {
×
388
                return $GLOBALS['pagenow'] === 'index.php';
×
389
        }
390

391
        /**
392
         * Loads the cornerstone filter.
393
         *
394
         * @return WPSEO_WordPress_Integration[] The integrations to initialize.
395
         */
396
        protected function initialize_cornerstone_content() {
×
397
                if ( ! WPSEO_Options::get( 'enable_cornerstone_content' ) ) {
×
398
                        return [];
×
399
                }
400

401
                return [
402
                        'cornerstone_filter' => new WPSEO_Cornerstone_Filter(),
×
403
                ];
404
        }
405
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc