Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 128
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Stats_Dashboard_Widget
0.00% covered (danger)
0.00%
0 / 128
0.00% covered (danger)
0.00%
0 / 5
306
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
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 admin_head
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
2
 render_widget
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 render_footer
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * Adds the Jetpack stats widget to the WordPress admin dashboard.
4 *
5 * @package jetpack
6 */
7
8use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
9use Automattic\Jetpack\Redirect;
10use Automattic\Jetpack\Stats_Admin\WP_Dashboard_Odyssey_Widget as Dashboard_Stats_Widget;
11use Automattic\Jetpack\Status;
12
13/**
14 * Class that adds the Jetpack stats widget to the WordPress admin dashboard.
15 *
16 * Note that this widget renders whether or not the stats module is active because it currently
17 * displays information about Akismet and Protect.
18 */
19class Jetpack_Stats_Dashboard_Widget {
20
21    /**
22     * Indicates whether the class initialized or not.
23     *
24     * @var bool
25     */
26    private static $initialized = false;
27
28    /**
29     * Initialize the class by calling the setup static function.
30     *
31     * @return void
32     */
33    public static function init() {
34        if ( ! self::$initialized ) {
35            self::$initialized = true;
36            self::wp_dashboard_setup();
37        }
38    }
39
40    /**
41     * Sets up the Jetpack Stats widget in the WordPress admin dashboard.
42     */
43    public static function wp_dashboard_setup() {
44
45        /**
46         * Filter whether the Jetpack Stats dashboard widget should be shown to the current user.
47         * By default, the dashboard widget is shown to users who can view_stats.
48         *
49         * @module stats
50         * @since 11.9
51         *
52         * @param bool Whether to show the widget to the current user.
53         */
54        // Temporarily show the widget to administrators for Simple sites as the view_stats capability is not available.
55        // TODO: Grant the view_stats capability to corresponding users for Simple sites.
56        $can_user_view_stats = current_user_can( 'manage_options' ) || current_user_can( 'view_stats' );
57        if ( ! apply_filters( 'jetpack_stats_dashboard_widget_show_to_user', $can_user_view_stats ) ) {
58            return;
59        }
60
61        if ( Jetpack::is_connection_ready() && Jetpack::is_module_active( 'stats' ) ) {
62            add_action( 'admin_head', array( static::class, 'admin_head' ) );
63
64            $widget_title = sprintf(
65                __( 'Jetpack Stats', 'jetpack' )
66            );
67
68            // New widget implemented in Odyssey Stats.
69            $stats_widget = new Dashboard_Stats_Widget();
70            wp_add_dashboard_widget(
71                Dashboard_Stats_Widget::DASHBOARD_WIDGET_ID,
72                $widget_title,
73                array( $stats_widget, 'render' )
74            );
75            // Only load scripts when the widget is not hidden
76            $stats_widget->maybe_load_admin_scripts();
77        }
78    }
79
80    /**
81     * JavaScript and CSS for dashboard widget.
82     *
83     * @access public
84     * @return void
85     */
86    public static function admin_head() {
87        ?>
88            <script type="text/javascript">
89                /* <![CDATA[ */
90                jQuery( function($) {
91                    var dashStats = jQuery( '#dashboard_stats div.inside' );
92
93                    if ( dashStats.find( '.dashboard-widget-control-form' ).length ) {
94                        return;
95                    }
96
97                    if ( ! dashStats.length ) {
98                        dashStats = jQuery( '#dashboard_stats div.dashboard-widget-content' );
99                        var h = parseInt( dashStats.parent().height() ) - parseInt( dashStats.prev().height() );
100                        var args = 'width=' + dashStats.width() + '&height=' + h.toString();
101                    } else {
102                        if ( jQuery('#dashboard_stats' ).hasClass('postbox') ) {
103                            var args = 'width=' + ( dashStats.prev().width() * 2 ).toString();
104                        } else {
105                            var args = 'width=' + ( dashStats.width() * 2 ).toString();
106                        }
107                    }
108
109                    dashStats
110                        .not( '.dashboard-widget-control' )
111                        .load( 'admin.php?page=stats&noheader&dashboard&' + args, function() {
112                            jQuery( '#dashboard_stats' ).removeClass( 'is-loading' );
113                            jQuery( '#stat-chart' ).css( 'width', 'auto' );
114                        } );
115
116                    // Widget settings toggle container.
117                    var toggle = $( '.js-toggle-stats_dashboard_widget_control' );
118
119                    // Move the toggle in the widget header.
120                    toggle.appendTo( '#jetpack_summary_widget .handle-actions' );
121
122                    // Toggle settings when clicking on it.
123                    toggle.show().click( function( e ) {
124                        e.preventDefault();
125                        e.stopImmediatePropagation();
126                        $( this ).parent().toggleClass( 'controlVisible' );
127                        $( '#stats_dashboard_widget_control' ).slideToggle();
128                    } );
129                } );
130                /* ]]> */
131            </script>
132        <?php
133    }
134
135    /**
136     * Renders the widget and fires a dashboard widget action.
137     */
138    public static function render_widget() {
139        // This function won't exist if the stats module is disabled.
140        if ( function_exists( 'stats_jetpack_dashboard_widget' ) ) {
141            stats_jetpack_dashboard_widget();
142        }
143
144        /**
145         * Fires when the dashboard is loaded, but no longer used anywhere in the Jetpack plugin.
146         * The action is still available for backward compatibility.
147         *
148         * @since 3.4.0
149         */
150        do_action( 'jetpack_dashboard_widget' );
151
152        self::render_footer();
153    }
154
155    /**
156     * Load the widget footer showing brute force protection and Akismet stats.
157     */
158    public static function render_footer() {
159        ?>
160        <footer>
161        <div class="blocked-container">
162            <div class="protect">
163                <h3><?php esc_html_e( 'Brute force attack protection', 'jetpack' ); ?></h3>
164                <?php if ( Jetpack::is_module_active( 'protect' ) ) : ?>
165                    <p class="blocked-count">
166                        <?php echo esc_html( number_format_i18n( get_site_option( 'jetpack_protect_blocked_attempts', 0 ) ) ); ?>
167                    </p>
168                    <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>
169                <?php elseif ( current_user_can( 'jetpack_activate_modules' ) && ! ( new Status() )->is_offline_mode() ) : ?>
170                    <a href="
171                    <?php
172                    echo esc_url(
173                        wp_nonce_url(
174                            Jetpack::admin_url(
175                                array(
176                                    'action' => 'activate',
177                                    'module' => 'protect',
178                                )
179                            ),
180                            'jetpack_activate-protect'
181                        )
182                    );
183                    ?>
184                                " class="button button-primary" title="<?php esc_attr_e( 'Jetpack helps to keep you secure from brute-force login attacks.', 'jetpack' ); ?>">
185                        <?php esc_html_e( 'Activate', 'jetpack' ); ?>
186                    </a>
187                <?php else : ?>
188                    <?php esc_html_e( 'Brute force attack protection is inactive.', 'jetpack' ); ?>
189                <?php endif; ?>
190            </div>
191
192            <div class="akismet">
193                <h3><?php esc_html_e( 'Akismet Anti-spam', 'jetpack' ); ?></h3>
194                <?php if ( is_plugin_active( 'akismet/akismet.php' ) ) : ?>
195                    <p class="blocked-count">
196                        <?php echo esc_html( number_format_i18n( get_option( 'akismet_spam_count', 0 ) ) ); ?>
197                    </p>
198                    <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>
199                <?php elseif ( current_user_can( 'activate_plugins' ) && ! is_wp_error( validate_plugin( 'akismet/akismet.php' ) ) ) : ?>
200                    <a href="
201                    <?php
202                    echo esc_url(
203                        wp_nonce_url(
204                            add_query_arg(
205                                array(
206                                    'action' => 'activate',
207                                    'plugin' => 'akismet/akismet.php',
208                                ),
209                                admin_url( 'plugins.php' )
210                            ),
211                            'activate-plugin_akismet/akismet.php'
212                        )
213                    );
214                    ?>
215                                " class="button button-primary">
216                        <?php esc_html_e( 'Activate', 'jetpack' ); ?>
217                    </a>
218                <?php else : ?>
219                    <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>
220                <?php endif; ?>
221            </div>
222        </div>
223        <div class="footer-links">
224            <a href="<?php echo esc_url( Redirect::get_url( 'jetpack-support-wordpress-com-stats' ) ); ?>" target="_blank">
225                <?php
226                    $jetpack_logo = new Jetpack_Logo();
227                    echo $jetpack_logo->get_jp_emblem( true );// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
228                ?>
229            </a>
230        </div>
231        </footer>
232
233        <?php
234    }
235}