'blog-stats', 'description' => esc_html__( 'Show a hit counter for your blog.', 'jetpack' ), 'customize_selective_refresh' => true, 'show_instance_in_rest' => true, ); parent::__construct( 'blog-stats', /** This filter is documented in modules/widgets/facebook-likebox.php */ apply_filters( 'jetpack_widget_name', esc_html__( 'Blog Stats', 'jetpack' ) ), $widget_ops ); $this->alt_option_name = 'widget_statscounter'; add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); } /** * Remove the "Blog Stats" widget from the Legacy Widget block * * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. * @return array $widget_types New list of widgets that will be removed. */ public function hide_widget_in_block_editor( $widget_types ) { // @TODO: Hide for Simple sites when the block API starts working. if ( ! ( new Host() )->is_wpcom_simple() ) { $widget_types[] = 'blog-stats'; } return $widget_types; } /** * Return an associative array of default values * * These values are used in new widgets. * * @return array Array of default values for the Widget's options */ public function defaults() { return array( 'title' => esc_html__( 'Blog Stats', 'jetpack' ), /* Translators: Number of views, plural */ 'hits' => esc_html__( 'hits', 'jetpack' ), ); } /** * Return All Time Stats for that blog. * * We query the WordPress.com Stats REST API endpoint. * * @uses Automattic\Jetpack\Stats\WPCOM_Stats->get_stats. That function caches data locally for 5 minutes. * * @return string|false $views All Time Stats for that blog. */ public function get_stats() { $wpcom_stats = new WPCOM_Stats(); // Get data from the WordPress.com Stats REST API endpoint. $stats = $wpcom_stats->convert_stats_array_to_object( $wpcom_stats->get_stats( array( 'fields' => 'stats' ) ) ); if ( isset( $stats->stats->views ) ) { return $stats->stats->views; } else { return false; } } /** * Back end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @return void */ public function form( $instance ) { $instance = wp_parse_args( $instance, $this->defaults() ); ?>

defaults() ); /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $instance['title'] ); echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } // Get the Site Stats. $views = $this->get_stats(); if ( 0 === $views ) { esc_html_e( 'There is nothing to display yet', 'jetpack' ); } elseif ( $views ) { printf( '', esc_html( number_format_i18n( $views ) ), isset( $instance['hits'] ) ? esc_html( $instance['hits'] ) : '' ); } else { esc_html_e( 'There was an issue retrieving stats. Please try again later.', 'jetpack' ); } echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped /** This action is already documented in modules/widgets/gravatar-profile.php */ do_action( 'jetpack_stats_extra', 'widget_view', 'blog_stats' ); } } /** * If the Stats module is active in a recent version of Jetpack, register the widget. * * @since 4.5.0 */ function jetpack_blog_stats_widget_init() { if ( Jetpack::is_module_active( 'stats' ) ) { register_widget( 'Jetpack_Blog_Stats_Widget' ); } } add_action( 'widgets_init', 'jetpack_blog_stats_widget_init' );