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

Yoast / wordpress-seo / 7004843404

27 Nov 2023 11:48AM UTC coverage: 49.206% (-0.03%) from 49.232%
7004843404

push

github

web-flow
Merge pull request #20858 from Yoast/improve-copy-in-the-ftc-57

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

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

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

8
/**
9
 * Class to change or add WordPress dashboard widgets.
10
 */
11
class Yoast_Dashboard_Widget implements WPSEO_WordPress_Integration {
12

13
        /**
14
         * Holds the cache transient key.
15
         *
16
         * @var string
17
         */
18
        const CACHE_TRANSIENT_KEY = 'wpseo-dashboard-totals';
19

20
        /**
21
         * Holds an instance of the admin asset manager.
22
         *
23
         * @var WPSEO_Admin_Asset_Manager
24
         */
25
        protected $asset_manager;
26

27
        /**
28
         * Holds the dashboard statistics.
29
         *
30
         * @var WPSEO_Statistics
31
         */
32
        protected $statistics;
33

34
        /**
35
         * Yoast_Dashboard_Widget constructor.
36
         *
37
         * @param WPSEO_Statistics|null $statistics WPSEO_Statistics instance.
38
         */
39
        public function __construct( WPSEO_Statistics $statistics = null ) {
×
40
                if ( $statistics === null ) {
×
41
                        $statistics = new WPSEO_Statistics();
×
42
                }
43

44
                $this->statistics    = $statistics;
×
45
                $this->asset_manager = new WPSEO_Admin_Asset_Manager();
×
46
        }
47

48
        /**
49
         * Register WordPress hooks.
50
         */
51
        public function register_hooks() {
×
52
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_dashboard_assets' ] );
×
53
                add_action( 'admin_init', [ $this, 'queue_dashboard_widget' ] );
×
54
        }
55

56
        /**
57
         * Adds the dashboard widget if it should be shown.
58
         *
59
         * @return void
60
         */
61
        public function queue_dashboard_widget() {
×
62
                if ( $this->show_widget() ) {
×
63
                        add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] );
×
64
                }
65
        }
66

67
        /**
68
         * Adds dashboard widget to WordPress.
69
         */
70
        public function add_dashboard_widget() {
×
71
                add_filter( 'postbox_classes_dashboard_wpseo-dashboard-overview', [ $this, 'wpseo_dashboard_overview_class' ] );
×
72
                wp_add_dashboard_widget(
×
73
                        'wpseo-dashboard-overview',
×
74
                        /* translators: %s is the plugin name */
75
                        sprintf( __( '%s Posts Overview', 'wordpress-seo' ), 'Yoast SEO' ),
×
76
                        [ $this, 'display_dashboard_widget' ]
×
77
                );
78
        }
79

80
        /**
81
         * Adds CSS classes to the dashboard widget.
82
         *
83
         * @param array $classes An array of postbox CSS classes.
84
         *
85
         * @return array
86
         */
87
        public function wpseo_dashboard_overview_class( $classes ) {
×
88
                $classes[] = 'yoast wpseo-dashboard-overview';
×
89
                return $classes;
×
90
        }
91

92
        /**
93
         * Displays the dashboard widget.
94
         */
95
        public function display_dashboard_widget() {
×
96
                echo '<div id="yoast-seo-dashboard-widget"></div>';
×
97
        }
98

99
        /**
100
         * Enqueues assets for the dashboard if the current page is the dashboard.
101
         */
102
        public function enqueue_dashboard_assets() {
×
103
                if ( ! $this->is_dashboard_screen() ) {
×
104
                        return;
×
105
                }
106

107
                $this->asset_manager->localize_script( 'dashboard-widget', 'wpseoDashboardWidgetL10n', $this->localize_dashboard_script() );
×
108
                $this->asset_manager->enqueue_script( 'dashboard-widget' );
×
109
                $this->asset_manager->enqueue_style( 'wp-dashboard' );
×
110
                $this->asset_manager->enqueue_style( 'monorepo' );
×
111
        }
112

113
        /**
114
         * Translates strings used in the dashboard widget.
115
         *
116
         * @return array The translated strings.
117
         */
118
        public function localize_dashboard_script() {
×
119
                return [
120
                        'feed_header'          => sprintf(
×
121
                                /* translators: %1$s resolves to Yoast.com */
122
                                __( 'Latest blog posts on %1$s', 'wordpress-seo' ),
×
123
                                'Yoast.com'
×
124
                        ),
125
                        'feed_footer'          => __( 'Read more like this on our SEO blog', 'wordpress-seo' ),
×
126
                        'wp_version'           => substr( $GLOBALS['wp_version'], 0, 3 ) . '-' . ( is_plugin_active( 'classic-editor/classic-editor.php' ) ? '1' : '0' ),
×
127
                        'php_version'          => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
128
                ];
129
        }
130

131
        /**
132
         * Checks if the current screen is the dashboard screen.
133
         *
134
         * @return bool Whether or not this is the dashboard screen.
135
         */
136
        private function is_dashboard_screen() {
×
137
                $current_screen = get_current_screen();
×
138

139
                return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' );
×
140
        }
141

142
        /**
143
         * Returns true when the dashboard widget should be shown.
144
         *
145
         * @return bool
146
         */
147
        private function show_widget() {
×
148
                $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
×
149

150
                return $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );
×
151
        }
152
}
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