PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 19.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v19.0
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 19.0, at admin/class-yoast-dashboard-widget.php

158 lines 4.2 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 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 $is_wincher_active = YoastSEO()->helpers->wincher->is_active();
120
121 return [
122 'feed_header' => sprintf(
123 /* translators: %1$s resolves to Yoast.com */
124 __( 'Latest blog posts on %1$s', 'wordpress-seo' ),
125 'Yoast.com'
126 ),
127 'feed_footer' => __( 'Read more like this on our SEO blog', 'wordpress-seo' ),
128 'wp_version' => substr( $GLOBALS['wp_version'], 0, 3 ) . '-' . ( is_plugin_active( 'classic-editor/classic-editor.php' ) ? '1' : '0' ),
129 'php_version' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
130 'is_wincher_active' => ( $is_wincher_active ) ? 1 : 0,
131 'wincher_is_logged_in' => ( $is_wincher_active ) ? YoastSEO()->helpers->wincher->login_status() : false,
132 'wincher_website_id' => WPSEO_Options::get( 'wincher_website_id', '' ),
133 ];
134 }
135
136 /**
137 * Checks if the current screen is the dashboard screen.
138 *
139 * @return bool Whether or not this is the dashboard screen.
140 */
141 private function is_dashboard_screen() {
142 $current_screen = get_current_screen();
143
144 return ( $current_screen instanceof WP_Screen && $current_screen->id === 'dashboard' );
145 }
146
147 /**
148 * Returns true when the dashboard widget should be shown.
149 *
150 * @return bool
151 */
152 private function show_widget() {
153 $analysis_seo = new WPSEO_Metabox_Analysis_SEO();
154
155 return $analysis_seo->is_enabled() && current_user_can( 'edit_posts' );
156 }
157 }
158