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

Yoast / wordpress-seo / 56db0408fe2a0dbffe1c2c6cfad9b5c2f6941e34

14 Apr 2025 12:24PM UTC coverage: 52.454% (-2.1%) from 54.594%
56db0408fe2a0dbffe1c2c6cfad9b5c2f6941e34

Pull #22077

github

enricobattocchi
Adjust carryforward in Coveralls action
Pull Request #22077: Drop compatibility with PHP 7.2 and 7.3

7827 of 13877 branches covered (56.4%)

Branch coverage included in aggregate %.

29025 of 56379 relevant lines covered (51.48%)

42277.18 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
        public 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
         * @return void
52
         */
53
        public function register_hooks() {
×
54
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_dashboard_assets' ] );
×
55
                add_action( 'admin_init', [ $this, 'queue_dashboard_widget' ] );
×
56
        }
57

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

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

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

96
        /**
97
         * Displays the dashboard widget.
98
         *
99
         * @return void
100
         */
101
        public function display_dashboard_widget() {
×
102
                echo '<div id="yoast-seo-dashboard-widget"></div>';
×
103
        }
104

105
        /**
106
         * Enqueues assets for the dashboard if the current page is the dashboard.
107
         *
108
         * @return void
109
         */
110
        public function enqueue_dashboard_assets() {
×
111
                if ( ! $this->is_dashboard_screen() ) {
×
112
                        return;
×
113
                }
114

115
                $this->asset_manager->localize_script( 'dashboard-widget', 'wpseoDashboardWidgetL10n', $this->localize_dashboard_script() );
×
116
                $this->asset_manager->enqueue_script( 'dashboard-widget' );
×
117
                $this->asset_manager->enqueue_style( 'wp-dashboard' );
×
118
                $this->asset_manager->enqueue_style( 'monorepo' );
×
119
        }
120

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

139
        /**
140
         * Checks if the current screen is the dashboard screen.
141
         *
142
         * @return bool Whether or not this is the dashboard screen.
143
         */
144
        private function is_dashboard_screen() {
×
145
                $current_screen = get_current_screen();
×
146

147
                return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' );
×
148
        }
149

150
        /**
151
         * Returns true when the dashboard widget should be shown.
152
         *
153
         * @return bool
154
         */
155
        private function show_widget() {
×
156
                $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
×
157

158
                return $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );
×
159
        }
160
}
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