Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odyssey Stats widget: Only load stats script when widget is visible #34284

Merged
merged 5 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Support load scripts conditionally for Stats widget
2 changes: 1 addition & 1 deletion projects/packages/stats-admin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"autotagger": true,
"mirror-repo": "Automattic/jetpack-stats-admin",
"branch-alias": {
"dev-trunk": "0.13.x-dev"
"dev-trunk": "0.14.x-dev"
},
"textdomain": "jetpack-stats-admin",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/stats-admin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-stats-admin",
"version": "0.13.0",
"version": "0.14.0-alpha",
"description": "Stats Dashboard",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/stats-admin/#readme",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/stats-admin/src/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Main {
/**
* Stats version.
*/
const VERSION = '0.13.0';
const VERSION = '0.14.0-alpha';

/**
* Singleton Main instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
* @package jetpack-stats-admin
*/
class WP_Dashboard_Odyssey_Widget {
const DASHBOARD_WIDGET_ID = 'jetpack_summary_widget';

/**
* Renders the widget and fires a dashboard widget action.
*/
public static function render() {
public function render() {
// 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.
$is_toggled_open = $this->is_widget_hidden();
?>
<div id="dashboard_stats" class="jp-stats-widget" style="min-height: 200px;">
<div class="hide-if-js"><?php esc_html_e( 'Your Jetpack Stats widget requires JavaScript to function properly.', 'jetpack-stats-admin' ); ?></div>
Expand All @@ -30,6 +33,13 @@ class="jp-stats-widget-loading-spinner"
alt=<?php echo esc_attr( __( 'Loading', 'jetpack-stats-admin' ) ); ?>
src="//en.wordpress.com/i/loading/loading-64.gif"
/>
<p>
<?php
if ( $is_toggled_open ) {
echo esc_html__( 'Please reload the page to see your stats...', 'jetpack-stats-admin' );
}
?>
</p>
</div>
</div>
<?php
Expand Down Expand Up @@ -57,4 +67,24 @@ public function load_admin_scripts() {
)
);
}

/**
* Load the admin scripts when the widget is visible.
*/
public function maybe_load_admin_scripts() {
if ( $this->is_widget_hidden() ) {
return;
}
$this->load_admin_scripts();
}

/**
* Returns true if the widget is hidden for the current screen and current user.
*
* @return bool
*/
public function is_widget_hidden() {
$hidden = get_hidden_meta_boxes( get_current_screen() );
return in_array( self::DASHBOARD_WIDGET_ID, $hidden, true );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Odyssey Stats widget: only load scripts when the widget is visible
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ public static function wp_dashboard_setup() {
// New widget implemented in Odyssey Stats.
$stats_widget = new Dashboard_Stats_Widget();
wp_add_dashboard_widget(
'jetpack_summary_widget',
Dashboard_Stats_Widget::DASHBOARD_WIDGET_ID,
$widget_title,
array( $stats_widget, 'render' )
);
$stats_widget->load_admin_scripts();
// Only load scripts when the widget is not hidden
$stats_widget->maybe_load_admin_scripts();
} else {
// Legacy widget.
wp_add_dashboard_widget(
'jetpack_summary_widget',
Dashboard_Stats_Widget::DASHBOARD_WIDGET_ID,
$widget_title,
array( __CLASS__, 'render_widget' )
);
Expand Down
4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading