Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.17% covered (danger)
5.17%
3 / 58
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Stats_Dashboard_Widget
5.17% covered (danger)
5.17%
3 / 58
0.00% covered (danger)
0.00%
0 / 3
157.11
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 wp_dashboard_setup
18.75% covered (danger)
18.75%
3 / 16
0.00% covered (danger)
0.00%
0 / 1
42.33
 admin_head
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
2
 render_widget
n/a
0 / 0
n/a
0 / 0
1
 render_footer
n/a
0 / 0
n/a
0 / 0
1
1<?php
2/**
3 * Adds the Jetpack stats widget to the WordPress admin dashboard.
4 *
5 * @package jetpack
6 */
7
8use Automattic\Jetpack\Stats_Admin\WP_Dashboard_Odyssey_Widget as Dashboard_Stats_Widget;
9
10/**
11 * Class that adds the Jetpack stats widget to the WordPress admin dashboard.
12 */
13class Jetpack_Stats_Dashboard_Widget {
14
15    /**
16     * Indicates whether the class initialized or not.
17     *
18     * @var bool
19     */
20    private static $initialized = false;
21
22    /**
23     * Initialize the class by calling the setup static function.
24     *
25     * @return void
26     */
27    public static function init() {
28        if ( ! self::$initialized ) {
29            self::$initialized = true;
30            self::wp_dashboard_setup();
31        }
32    }
33
34    /**
35     * Sets up the Jetpack Stats widget in the WordPress admin dashboard.
36     */
37    public static function wp_dashboard_setup() {
38        // WordPress.com Simple sites load this class next to the stats-admin copy in jetpack-mu-wpcom, which can predate register_widget().
39        if ( method_exists( Dashboard_Stats_Widget::class, 'register_widget' ) ) {
40            // Atomic filters readiness to require a connected owner, which the package's own connection check does not see.
41            if ( Jetpack::is_connection_ready() && Dashboard_Stats_Widget::register_widget() ) {
42                add_action( 'admin_head', array( static::class, 'admin_head' ) );
43            }
44            return;
45        }
46
47        /** This filter is documented in projects/packages/stats-admin/src/class-wp-dashboard-odyssey-widget.php */
48        $can_user_view_stats = current_user_can( 'manage_options' ) || current_user_can( 'view_stats' );
49        if ( ! apply_filters( 'jetpack_stats_dashboard_widget_show_to_user', $can_user_view_stats ) ) {
50            return;
51        }
52
53        if ( Jetpack::is_connection_ready() && Jetpack::is_module_active( 'stats' ) ) {
54            add_action( 'admin_head', array( static::class, 'admin_head' ) );
55
56            // New widget implemented in Odyssey Stats.
57            $stats_widget = new Dashboard_Stats_Widget();
58            wp_add_dashboard_widget(
59                Dashboard_Stats_Widget::DASHBOARD_WIDGET_ID,
60                /** "Stats" is a product name, do not translate. */
61                'Jetpack Stats',
62                array( $stats_widget, 'render' )
63            );
64            // Only load scripts when the widget is not hidden
65            $stats_widget->maybe_load_admin_scripts();
66        }
67    }
68
69    /**
70     * JavaScript and CSS for dashboard widget.
71     *
72     * @access public
73     * @return void
74     */
75    public static function admin_head() {
76        ?>
77            <script type="text/javascript">
78                /* <![CDATA[ */
79                jQuery( function($) {
80                    var dashStats = jQuery( '#dashboard_stats div.inside' );
81
82                    if ( dashStats.find( '.dashboard-widget-control-form' ).length ) {
83                        return;
84                    }
85
86                    if ( ! dashStats.length ) {
87                        dashStats = jQuery( '#dashboard_stats div.dashboard-widget-content' );
88                        var h = parseInt( dashStats.parent().height() ) - parseInt( dashStats.prev().height() );
89                        var args = 'width=' + dashStats.width() + '&height=' + h.toString();
90                    } else {
91                        if ( jQuery('#dashboard_stats' ).hasClass('postbox') ) {
92                            var args = 'width=' + ( dashStats.prev().width() * 2 ).toString();
93                        } else {
94                            var args = 'width=' + ( dashStats.width() * 2 ).toString();
95                        }
96                    }
97
98                    dashStats
99                        .not( '.dashboard-widget-control' )
100                        .load( 'admin.php?page=stats&noheader&dashboard&' + args, function() {
101                            jQuery( '#dashboard_stats' ).removeClass( 'is-loading' );
102                            jQuery( '#stat-chart' ).css( 'width', 'auto' );
103                        } );
104
105                    // Widget settings toggle container.
106                    var toggle = $( '.js-toggle-stats_dashboard_widget_control' );
107
108                    // Move the toggle in the widget header.
109                    toggle.appendTo( '#jetpack_summary_widget .handle-actions' );
110
111                    // Toggle settings when clicking on it.
112                    toggle.show().click( function( e ) {
113                        e.preventDefault();
114                        e.stopImmediatePropagation();
115                        $( this ).parent().toggleClass( 'controlVisible' );
116                        $( '#stats_dashboard_widget_control' ).slideToggle();
117                    } );
118                } );
119                /* ]]> */
120            </script>
121        <?php
122    }
123
124    /**
125     * Renders the Jetpack Stats widget.
126     *
127     * @deprecated $$next-version$$ Use WP_Dashboard_Odyssey_Widget::render() instead.
128     */
129    public static function render_widget() {
130        _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', Dashboard_Stats_Widget::class . '::render' );
131        ( new Dashboard_Stats_Widget() )->render();
132    }
133
134    /**
135     * Formerly rendered the brute force protection and Akismet footer. Outputs nothing.
136     *
137     * @deprecated $$next-version$$
138     */
139    public static function render_footer() {
140        _deprecated_function( __METHOD__, 'jetpack-$$next-version$$' );
141    }
142}