PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-stats-admin / src / class-wp-dashboard-odyssey-widget.php

class-wp-dashboard-odyssey-widget.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-stats-admin/src/class-wp-dashboard-odyssey-widget.php

130 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class that adds Jetpack Stats widget for WP Dashboard.
4 *
5 * @package automattic/jetpack-stats-admin
6 */
7
8 namespace Automattic\Jetpack\Stats_Admin;
9
10 use Automattic\Jetpack\Modules;
11
12 /**
13 * Responsible for loading Jetpack Stats widget for WP Dashboard.
14 *
15 * @package jetpack-stats-admin
16 */
17 class WP_Dashboard_Odyssey_Widget {
18 const DASHBOARD_WIDGET_ID = 'jetpack_summary_widget';
19
20 /**
21 * Add the widget to the WordPress dashboard of a connected site with Stats active.
22 *
23 * Hook it to `wp_dashboard_setup`.
24 *
25 * @return bool Whether the widget was added.
26 */
27 public static function register_widget() {
28 /**
29 * Filter whether the Jetpack Stats dashboard widget should be shown to the current user.
30 * By default, the dashboard widget is shown to users who can manage options or view Stats.
31 *
32 * @module stats
33 * @since-jetpack 11.9
34 *
35 * @param bool Whether to show the widget to the current user.
36 */
37 if ( ! apply_filters( 'jetpack_stats_dashboard_widget_show_to_user', current_user_can( 'manage_options' ) || current_user_can( 'view_stats' ) ) ) {
38 return false;
39 }
40
41 if ( ! Main::is_site_connected() || ! ( new Modules() )->is_active( 'stats' ) ) {
42 return false;
43 }
44
45 $widget = new self();
46 wp_add_dashboard_widget(
47 self::DASHBOARD_WIDGET_ID,
48 /** "Stats" is a product name, do not translate. */
49 'Jetpack Stats',
50 array( $widget, 'render' )
51 );
52 $widget->maybe_load_admin_scripts();
53
54 return true;
55 }
56
57 /**
58 * Renders the widget and fires a dashboard widget action.
59 */
60 public function render() {
61 // The widget is always rendered, so if it was hidden and then toggled open, we need to ask user to refresh the page to load data properly.
62 $is_toggled_open = $this->is_widget_hidden();
63 ?>
64 <div id="dashboard_stats" class="jp-stats-widget" style="min-height: 200px;">
65 <div class="hide-if-js"><?php esc_html_e( 'Your Jetpack Stats widget requires JavaScript to function properly.', 'jetpack-stats-admin' ); ?></div>
66 <div class="hide-if-no-js" style="height: 100%">
67 <img
68 class="jp-stats-widget-loading-spinner"
69 width="32"
70 height="32"
71 style="position: absolute; left: calc(50% - 28px); top: calc(50% - 36px);"
72 alt=<?php echo esc_attr( __( 'Loading', 'jetpack-stats-admin' ) ); ?>
73 src="//en.wordpress.com/i/loading/loading-64.gif"
74 />
75 <p>
76 <?php
77 if ( $is_toggled_open ) {
78 echo esc_html__( 'Please reload the page to see your stats...', 'jetpack-stats-admin' );
79 }
80 ?>
81 </p>
82 </div>
83 </div>
84 <?php
85 /**
86 * Fires when the dashboard is loaded, but no longer used anywhere in the Jetpack plugin.
87 * The action is still available for backward compatibility.
88 *
89 * @since 3.4.0
90 */
91 do_action( 'jetpack_dashboard_widget' );
92 }
93
94 /**
95 * Load the admin scripts.
96 */
97 public function load_admin_scripts() {
98 $config_data = ( new Odyssey_Config_Data() )->get_data();
99 ( new Odyssey_Assets() )->load_admin_scripts(
100 'jetpack_stats_widget',
101 'widget-loader.min',
102 array(
103 'config_variable_name' => 'jetpackStatsOdysseyWidgetConfigData',
104 'config_data' => $config_data,
105 'enqueue_css' => false,
106 )
107 );
108 }
109
110 /**
111 * Load the admin scripts when the widget is visible.
112 */
113 public function maybe_load_admin_scripts() {
114 if ( $this->is_widget_hidden() ) {
115 return;
116 }
117 $this->load_admin_scripts();
118 }
119
120 /**
121 * Returns true if the widget is hidden for the current screen and current user.
122 *
123 * @return bool
124 */
125 public function is_widget_hidden() {
126 $hidden = get_hidden_meta_boxes( get_current_screen() );
127 return in_array( self::DASHBOARD_WIDGET_ID, $hidden, true );
128 }
129 }
130