| @@ -4,12 +4,10 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package jetpack |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -use Automattic\Jetpack\Assets; | |
| 9 | 8 | use Automattic\Jetpack\Assets\Logo as Jetpack_Logo; |
| 10 | 9 | use Automattic\Jetpack\Redirect; |
| 11 | -use Automattic\Jetpack\Stats\Options as Stats_Options; | |
| 12 | 10 | use Automattic\Jetpack\Stats_Admin\WP_Dashboard_Odyssey_Widget as Dashboard_Stats_Widget; |
| 13 | 11 | use Automattic\Jetpack\Status; |
| 14 | 12 | |
| 15 | 13 | /** |
| @@ -42,56 +40,36 @@ | ||
| 42 | 40 | /** |
| 43 | 41 | * Sets up the Jetpack Stats widget in the WordPress admin dashboard. |
| 44 | 42 | */ |
| 45 | 43 | public static function wp_dashboard_setup() { |
| 44 | + // WordPress.com Simple sites load this class next to the stats-admin copy in jetpack-mu-wpcom, which can predate register_widget(). | |
| 45 | + if ( method_exists( Dashboard_Stats_Widget::class, 'register_widget' ) ) { | |
| 46 | + // Atomic filters readiness to require a connected owner, which the package's own connection check does not see. | |
| 47 | + if ( Jetpack::is_connection_ready() && Dashboard_Stats_Widget::register_widget() ) { | |
| 48 | + add_action( 'admin_head', array( static::class, 'admin_head' ) ); | |
| 49 | + } | |
| 50 | + return; | |
| 51 | + } | |
| 46 | 52 | |
| 47 | - /** | |
| 48 | - * Filter whether the Jetpack Stats dashboard widget should be shown to the current user. | |
| 49 | - * By default, the dashboard widget is shown to users who can view_stats. | |
| 50 | - * | |
| 51 | - * @module stats | |
| 52 | - * @since 11.9 | |
| 53 | - * | |
| 54 | - * @param bool Whether to show the widget to the current user. | |
| 55 | - */ | |
| 56 | - if ( ! apply_filters( 'jetpack_stats_dashboard_widget_show_to_user', current_user_can( 'view_stats' ) ) ) { | |
| 53 | + /** This filter is documented in projects/packages/stats-admin/src/class-wp-dashboard-odyssey-widget.php */ | |
| 54 | + $can_user_view_stats = current_user_can( 'manage_options' ) || current_user_can( 'view_stats' ); | |
| 55 | + if ( ! apply_filters( 'jetpack_stats_dashboard_widget_show_to_user', $can_user_view_stats ) ) { | |
| 57 | 56 | return; |
| 58 | 57 | } |
| 59 | 58 | |
| 60 | - if ( Jetpack::is_connection_ready() ) { | |
| 59 | + if ( Jetpack::is_connection_ready() && Jetpack::is_module_active( 'stats' ) ) { | |
| 61 | 60 | add_action( 'admin_head', array( static::class, 'admin_head' ) ); |
| 62 | 61 | |
| 63 | - $widget_title = sprintf( | |
| 64 | - __( 'Jetpack Stats', 'jetpack' ) | |
| 62 | + // New widget implemented in Odyssey Stats. | |
| 63 | + $stats_widget = new Dashboard_Stats_Widget(); | |
| 64 | + wp_add_dashboard_widget( | |
| 65 | + Dashboard_Stats_Widget::DASHBOARD_WIDGET_ID, | |
| 66 | + /** "Stats" is a product name, do not translate. */ | |
| 67 | + 'Jetpack Stats', | |
| 68 | + array( $stats_widget, 'render' ) | |
| 65 | 69 | ); |
| 66 | - | |
| 67 | - if ( Stats_Options::get_option( 'enable_odyssey_stats' ) ) { | |
| 68 | - // New widget implemented in Odyssey Stats. | |
| 69 | - $stats_widget = new Dashboard_Stats_Widget(); | |
| 70 | - wp_add_dashboard_widget( | |
| 71 | - 'jetpack_summary_widget', | |
| 72 | - $widget_title, | |
| 73 | - array( $stats_widget, 'render' ) | |
| 74 | - ); | |
| 75 | - $stats_widget->load_admin_scripts(); | |
| 76 | - } else { | |
| 77 | - // Legacy widget. | |
| 78 | - wp_add_dashboard_widget( | |
| 79 | - 'jetpack_summary_widget', | |
| 80 | - $widget_title, | |
| 81 | - array( __CLASS__, 'render_widget' ) | |
| 82 | - ); | |
| 83 | - wp_enqueue_style( | |
| 84 | - 'jetpack-dashboard-widget', | |
| 85 | - Assets::get_file_url_for_environment( | |
| 86 | - 'css/dashboard-widget.min.css', | |
| 87 | - 'css/dashboard-widget.css' | |
| 88 | - ), | |
| 89 | - array(), | |
| 90 | - JETPACK__VERSION | |
| 91 | - ); | |
| 92 | - wp_style_add_data( 'jetpack-dashboard-widget', 'rtl', 'replace' ); | |
| 93 | - } | |
| 70 | + // Only load scripts when the widget is not hidden | |
| 71 | + $stats_widget->maybe_load_admin_scripts(); | |
| 94 | 72 | } |
| 95 | 73 | } |
| 96 | 74 | |
| 97 | 75 | /** |