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