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

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