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