2022-04-02 10:26:41 +02:00
< ? php
/**
* Adds the Jetpack stats widget to the WordPress admin dashboard .
*
* @ package jetpack
*/
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo ;
2022-06-16 14:01:47 +02:00
use Automattic\Jetpack\Redirect ;
2023-05-23 23:18:12 +02:00
use Automattic\Jetpack\Stats_Admin\WP_Dashboard_Odyssey_Widget as Dashboard_Stats_Widget ;
2022-04-02 10:26:41 +02:00
use Automattic\Jetpack\Status ;
/**
* Class that adds the Jetpack stats widget to the WordPress admin dashboard .
2023-04-26 17:39:43 +02:00
*
* Note that this widget renders whether or not the stats module is active because it currently
* displays information about Akismet and Protect .
2022-04-02 10:26:41 +02:00
*/
class Jetpack_Stats_Dashboard_Widget {
/**
* Indicates whether the class initialized or not .
*
* @ var bool
*/
private static $initialized = false ;
/**
* Initialize the class by calling the setup static function .
*
* @ return void
*/
public static function init () {
if ( ! self :: $initialized ) {
self :: $initialized = true ;
self :: wp_dashboard_setup ();
}
}
/**
* Sets up the Jetpack Stats widget in the WordPress admin dashboard .
*/
public static function wp_dashboard_setup () {
2023-04-26 17:39:43 +02:00
/**
* Filter whether the Jetpack Stats dashboard widget should be shown to the current user .
* By default , the dashboard widget is shown to users who can view_stats .
*
* @ module stats
* @ since 11.9
*
* @ param bool Whether to show the widget to the current user .
*/
2025-04-25 12:30:07 +02:00
// Temporarily show the widget to administrators for Simple sites as the view_stats capability is not available.
// TODO: Grant the view_stats capability to corresponding users for Simple sites.
$can_user_view_stats = current_user_can ( 'manage_options' ) || current_user_can ( 'view_stats' );
if ( ! apply_filters ( 'jetpack_stats_dashboard_widget_show_to_user' , $can_user_view_stats ) ) {
2023-04-26 17:39:43 +02:00
return ;
2022-04-02 10:26:41 +02:00
}
2025-04-25 12:30:07 +02:00
if ( Jetpack :: is_connection_ready () && Jetpack :: is_module_active ( 'stats' ) ) {
2023-04-26 17:39:43 +02:00
add_action ( 'admin_head' , array ( static :: class , 'admin_head' ) );
2025-02-28 08:42:11 +01:00
// New widget implemented in Odyssey Stats.
$stats_widget = new Dashboard_Stats_Widget ();
wp_add_dashboard_widget (
Dashboard_Stats_Widget :: DASHBOARD_WIDGET_ID ,
2026-03-31 11:30:59 +02:00
/** "Stats" is a product name, do not translate. */
'Jetpack Stats' ,
2025-02-28 08:42:11 +01:00
array ( $stats_widget , 'render' )
);
// Only load scripts when the widget is not hidden
$stats_widget -> maybe_load_admin_scripts ();
2022-04-02 10:26:41 +02:00
}
}
/**
2023-04-26 17:39:43 +02:00
* JavaScript and CSS for dashboard widget .
*
* @ access public
* @ return void
*/
public static function admin_head () {
?>
< script type = " text/javascript " >
/* <![CDATA[ */
jQuery ( function ( $ ) {
var dashStats = jQuery ( '#dashboard_stats div.inside' );
if ( dashStats . find ( '.dashboard-widget-control-form' ) . length ) {
return ;
}
if ( ! dashStats . length ) {
dashStats = jQuery ( '#dashboard_stats div.dashboard-widget-content' );
var h = parseInt ( dashStats . parent () . height () ) - parseInt ( dashStats . prev () . height () );
var args = 'width=' + dashStats . width () + '&height=' + h . toString ();
} else {
if ( jQuery ( '#dashboard_stats' ) . hasClass ( 'postbox' ) ) {
var args = 'width=' + ( dashStats . prev () . width () * 2 ) . toString ();
} else {
var args = 'width=' + ( dashStats . width () * 2 ) . toString ();
}
}
dashStats
. not ( '.dashboard-widget-control' )
. load ( 'admin.php?page=stats&noheader&dashboard&' + args , function () {
jQuery ( '#dashboard_stats' ) . removeClass ( 'is-loading' );
jQuery ( '#stat-chart' ) . css ( 'width' , 'auto' );
} );
// Widget settings toggle container.
var toggle = $ ( '.js-toggle-stats_dashboard_widget_control' );
// Move the toggle in the widget header.
toggle . appendTo ( '#jetpack_summary_widget .handle-actions' );
// Toggle settings when clicking on it.
toggle . show () . click ( function ( e ) {
e . preventDefault ();
e . stopImmediatePropagation ();
$ ( this ) . parent () . toggleClass ( 'controlVisible' );
$ ( '#stats_dashboard_widget_control' ) . slideToggle ();
} );
} );
/* ]]> */
</ script >
< ? php
}
/**
* Renders the widget and fires a dashboard widget action .
2022-04-02 10:26:41 +02:00
*/
2023-04-26 17:39:43 +02:00
public static function render_widget () {
// This function won't exist if the stats module is disabled.
if ( function_exists ( 'stats_jetpack_dashboard_widget' ) ) {
stats_jetpack_dashboard_widget ();
}
2022-04-02 10:26:41 +02:00
/**
2023-04-26 17:39:43 +02:00
* Fires when the dashboard is loaded , but no longer used anywhere in the Jetpack plugin .
* The action is still available for backward compatibility .
2022-04-02 10:26:41 +02:00
*
* @ since 3.4 . 0
*/
do_action ( 'jetpack_dashboard_widget' );
2023-04-26 17:39:43 +02:00
self :: render_footer ();
2022-04-02 10:26:41 +02:00
}
/**
2023-04-26 17:39:43 +02:00
* Load the widget footer showing brute force protection and Akismet stats .
2022-04-02 10:26:41 +02:00
*/
2023-04-26 17:39:43 +02:00
public static function render_footer () {
2022-04-02 10:26:41 +02:00
?>
< footer >
2022-06-16 14:01:47 +02:00
< div class = " blocked-container " >
< div class = " protect " >
< h3 >< ? php esc_html_e ( 'Brute force attack protection' , 'jetpack' ); ?> </h3>
< ? php if ( Jetpack :: is_module_active ( 'protect' ) ) : ?>
< p class = " blocked-count " >
< ? php echo esc_html ( number_format_i18n ( get_site_option ( 'jetpack_protect_blocked_attempts' , 0 ) ) ); ?>
</ p >
< p >< ? php echo esc_html_x ( 'Blocked malicious login attempts' , '{#} Blocked malicious login attempts -- number is on a prior line, text is a caption.' , 'jetpack' ); ?> </p>
< ? php elseif ( current_user_can ( 'jetpack_activate_modules' ) && ! ( new Status () ) -> is_offline_mode () ) : ?>
< a href = "
< ? php
echo esc_url (
wp_nonce_url (
Jetpack :: admin_url (
array (
'action' => 'activate' ,
'module' => 'protect' ,
)
),
'jetpack_activate-protect'
)
);
?>
2023-05-23 23:18:12 +02:00
" class= " button button - primary " title= " < ? php esc_attr_e ( 'Jetpack helps to keep you secure from brute-force login attacks.' , 'jetpack' ); ?> ">
< ? php esc_html_e ( 'Activate' , 'jetpack' ); ?>
2022-06-16 14:01:47 +02:00
</ a >
< ? php else : ?>
< ? php esc_html_e ( 'Brute force attack protection is inactive.' , 'jetpack' ); ?>
< ? php endif ; ?>
</ div >
2022-04-02 10:26:41 +02:00
2022-06-16 14:01:47 +02:00
< div class = " akismet " >
2023-04-26 17:39:43 +02:00
< h3 >< ? php esc_html_e ( 'Akismet Anti-spam' , 'jetpack' ); ?> </h3>
2022-06-16 14:01:47 +02:00
< ? php if ( is_plugin_active ( 'akismet/akismet.php' ) ) : ?>
< p class = " blocked-count " >
< ? php echo esc_html ( number_format_i18n ( get_option ( 'akismet_spam_count' , 0 ) ) ); ?>
</ p >
2023-04-26 17:39:43 +02:00
< p >< ? php echo esc_html_x ( 'Blocked spam comments' , '{#} Spam comments blocked by Akismet -- number is on a prior line, text is a caption.' , 'jetpack' ); ?> </p>
2022-06-16 14:01:47 +02:00
< ? php elseif ( current_user_can ( 'activate_plugins' ) && ! is_wp_error ( validate_plugin ( 'akismet/akismet.php' ) ) ) : ?>
< a href = "
< ? php
echo esc_url (
wp_nonce_url (
add_query_arg (
array (
'action' => 'activate' ,
'plugin' => 'akismet/akismet.php' ,
),
admin_url ( 'plugins.php' )
),
'activate-plugin_akismet/akismet.php'
)
);
?>
2023-05-23 23:18:12 +02:00
" class= " button button - primary " >
< ? php esc_html_e ( 'Activate' , 'jetpack' ); ?>
2022-06-16 14:01:47 +02:00
</ a >
< ? php else : ?>
< p >< a href = " <?php echo esc_url( 'https://akismet.com/?utm_source=jetpack&utm_medium=link&utm_campaign=Jetpack%20Dashboard%20Widget%20Footer%20Link' ); ?> " >< ? php esc_html_e ( 'Anti-spam can help to keep your blog safe from spam!' , 'jetpack' ); ?> </a></p>
< ? php endif ; ?>
</ div >
2022-04-02 10:26:41 +02:00
</ div >
2022-06-16 14:01:47 +02:00
< div class = " footer-links " >
2023-04-26 17:39:43 +02:00
< a href = " <?php echo esc_url( Redirect::get_url( 'jetpack-support-wordpress-com-stats' ) ); ?> " target = " _blank " >
2022-12-15 17:41:53 +01:00
< ? php
2023-04-26 17:39:43 +02:00
$jetpack_logo = new Jetpack_Logo ();
echo $jetpack_logo -> get_jp_emblem ( true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
?>
</ a >
2022-06-16 14:01:47 +02:00
</ div >
2022-04-02 10:26:41 +02:00
</ footer >
2022-06-16 14:01:47 +02:00
2022-04-02 10:26:41 +02:00
< ? php
}
}