PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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-yoast-dashboard-widget.php

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

159 lines 3.9 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 /**
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 $statistics ??= new WPSEO_Statistics();
41
42 $this->statistics = $statistics;
43 $this->asset_manager = new WPSEO_Admin_Asset_Manager();
44 }
45
46 /**
47 * Register WordPress hooks.
48 *
49 * @return void
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 * @return void
71 */
72 public function add_dashboard_widget() {
73 add_filter( 'postbox_classes_dashboard_wpseo-dashboard-overview', [ $this, 'wpseo_dashboard_overview_class' ] );
74 wp_add_dashboard_widget(
75 'wpseo-dashboard-overview',
76 /* translators: %s is the plugin name */
77 sprintf( __( '%s Posts Overview', 'wordpress-seo' ), 'Yoast SEO' ),
78 [ $this, 'display_dashboard_widget' ],
79 );
80 }
81
82 /**
83 * Adds CSS classes to the dashboard widget.
84 *
85 * @param array $classes An array of postbox CSS classes.
86 *
87 * @return array
88 */
89 public function wpseo_dashboard_overview_class( $classes ) {
90 $classes[] = 'yoast wpseo-dashboard-overview';
91 return $classes;
92 }
93
94 /**
95 * Displays the dashboard widget.
96 *
97 * @return void
98 */
99 public function display_dashboard_widget() {
100 echo '<div id="yoast-seo-dashboard-widget"></div>';
101 }
102
103 /**
104 * Enqueues assets for the dashboard if the current page is the dashboard.
105 *
106 * @return void
107 */
108 public function enqueue_dashboard_assets() {
109 if ( ! $this->is_dashboard_screen() ) {
110 return;
111 }
112
113 $this->asset_manager->localize_script( 'dashboard-widget', 'wpseoDashboardWidgetL10n', $this->localize_dashboard_script() );
114 $this->asset_manager->enqueue_script( 'dashboard-widget' );
115 $this->asset_manager->enqueue_style( 'wp-dashboard' );
116 $this->asset_manager->enqueue_style( 'monorepo' );
117 }
118
119 /**
120 * Translates strings used in the dashboard widget.
121 *
122 * @return array The translated strings.
123 */
124 public function localize_dashboard_script() {
125 return [
126 'feed_header' => sprintf(
127 /* translators: %1$s resolves to Yoast.com */
128 __( 'Latest blog posts on %1$s', 'wordpress-seo' ),
129 'Yoast.com',
130 ),
131 'feed_footer' => __( 'Read more like this on our SEO blog', 'wordpress-seo' ),
132 'wp_version' => substr( $GLOBALS['wp_version'], 0, 3 ) . '-' . ( is_plugin_active( 'classic-editor/classic-editor.php' ) ? '1' : '0' ),
133 'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
134 ];
135 }
136
137 /**
138 * Checks if the current screen is the dashboard screen.
139 *
140 * @return bool Whether or not this is the dashboard screen.
141 */
142 private function is_dashboard_screen() {
143 $current_screen = get_current_screen();
144
145 return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' );
146 }
147
148 /**
149 * Returns true when the dashboard widget should be shown.
150 *
151 * @return bool
152 */
153 private function show_widget() {
154 $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
155
156 return $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );
157 }
158 }
159