| @@ -9,14 +9,13 @@ | ||
| 9 | 9 | |
| 10 | 10 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 11 | 11 | |
| 12 | 12 | use Automattic\Jetpack\Stats\WPCOM_Stats; |
| 13 | +use Automattic\Jetpack\Status\Host; | |
| 13 | 14 | |
| 14 | -/** | |
| 15 | - * Disable direct access/execution to/of the widget code. | |
| 16 | - */ | |
| 15 | +// Disable direct access/execution to/of the widget code. | |
| 17 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 18 | - exit; | |
| 17 | + exit( 0 ); | |
| 19 | 18 | } |
| 20 | 19 | |
| 21 | 20 | /** |
| 22 | 21 | * Blog Stats Widget. |
| @@ -34,8 +33,9 @@ | ||
| 34 | 33 | $widget_ops = array( |
| 35 | 34 | 'classname' => 'blog-stats', |
| 36 | 35 | 'description' => esc_html__( 'Show a hit counter for your blog.', 'jetpack' ), |
| 37 | 36 | 'customize_selective_refresh' => true, |
| 37 | + 'show_instance_in_rest' => true, | |
| 38 | 38 | ); |
| 39 | 39 | parent::__construct( |
| 40 | 40 | 'blog-stats', |
| 41 | 41 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
| @@ -42,11 +42,26 @@ | ||
| 42 | 42 | apply_filters( 'jetpack_widget_name', esc_html__( 'Blog Stats', 'jetpack' ) ), |
| 43 | 43 | $widget_ops |
| 44 | 44 | ); |
| 45 | 45 | $this->alt_option_name = 'widget_statscounter'; |
| 46 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); | |
| 46 | 47 | } |
| 47 | 48 | |
| 48 | 49 | /** |
| 50 | + * Remove the "Blog Stats" widget from the Legacy Widget block | |
| 51 | + * | |
| 52 | + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. | |
| 53 | + * @return array $widget_types New list of widgets that will be removed. | |
| 54 | + */ | |
| 55 | + public function hide_widget_in_block_editor( $widget_types ) { | |
| 56 | + // @TODO: Hide for Simple sites when the block API starts working. | |
| 57 | + if ( ! ( new Host() )->is_wpcom_simple() ) { | |
| 58 | + $widget_types[] = 'blog-stats'; | |
| 59 | + } | |
| 60 | + return $widget_types; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 49 | 64 | * Return an associative array of default values |
| 50 | 65 | * |
| 51 | 66 | * These values are used in new widgets. |
| 52 | 67 | * |
| @@ -69,10 +84,11 @@ | ||
| 69 | 84 | * |
| 70 | 85 | * @return string|false $views All Time Stats for that blog. |
| 71 | 86 | */ |
| 72 | 87 | public function get_stats() { |
| 88 | + $wpcom_stats = new WPCOM_Stats(); | |
| 73 | 89 | // Get data from the WordPress.com Stats REST API endpoint. |
| 74 | - $stats = convert_stats_array_to_object( ( new WPCOM_Stats() )->get_stats( array( 'fields' => 'stats' ) ) ); | |
| 90 | + $stats = $wpcom_stats->convert_stats_array_to_object( $wpcom_stats->get_stats( array( 'fields' => 'stats' ) ) ); | |
| 75 | 91 | |
| 76 | 92 | if ( isset( $stats->stats->views ) ) { |
| 77 | 93 | return $stats->stats->views; |
| 78 | 94 | } else { |
| @@ -86,9 +102,9 @@ | ||
| 86 | 102 | * @see WP_Widget::form() |
| 87 | 103 | * |
| 88 | 104 | * @param array $instance Previously saved values from database. |
| 89 | 105 | * |
| 90 | - * @return void | |
| 106 | + * @return string|void | |
| 91 | 107 | */ |
| 92 | 108 | public function form( $instance ) { |
| 93 | 109 | $instance = wp_parse_args( $instance, $this->defaults() ); |
| 94 | 110 | ?> |
| @@ -100,9 +116,9 @@ | ||
| 100 | 116 | <p> |
| 101 | 117 | <label for="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>"><?php esc_html_e( 'Pageview Description:', 'jetpack' ); ?></label> |
| 102 | 118 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hits' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['hits'] ); ?>" /> |
| 103 | 119 | </p> |
| 104 | - <p><?php esc_html_e( 'Hit counter is delayed by up to 60 seconds.', 'jetpack' ); ?></p> | |
| 120 | + <p><?php esc_html_e( 'Hit counter is delayed by up to 5 minutes.', 'jetpack' ); ?></p> | |
| 105 | 121 | |
| 106 | 122 | <?php |
| 107 | 123 | } |
| 108 | 124 | |
| @@ -151,9 +167,9 @@ | ||
| 151 | 167 | esc_html_e( 'There is nothing to display yet', 'jetpack' ); |
| 152 | 168 | } elseif ( $views ) { |
| 153 | 169 | printf( |
| 154 | 170 | '<ul><li>%1$s %2$s</li></ul>', |
| 155 | - number_format_i18n( $views ), | |
| 171 | + esc_html( number_format_i18n( $views ) ), | |
| 156 | 172 | isset( $instance['hits'] ) ? esc_html( $instance['hits'] ) : '' |
| 157 | 173 | ); |
| 158 | 174 | } else { |
| 159 | 175 | esc_html_e( 'There was an issue retrieving stats. Please try again later.', 'jetpack' ); |