| @@ -9,12 +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. |
| 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | - exit; | |
| 17 | + exit( 0 ); | |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * Blog Stats Widget. |
| @@ -32,8 +33,9 @@ | ||
| 32 | 33 | $widget_ops = array( |
| 33 | 34 | 'classname' => 'blog-stats', |
| 34 | 35 | 'description' => esc_html__( 'Show a hit counter for your blog.', 'jetpack' ), |
| 35 | 36 | 'customize_selective_refresh' => true, |
| 37 | + 'show_instance_in_rest' => true, | |
| 36 | 38 | ); |
| 37 | 39 | parent::__construct( |
| 38 | 40 | 'blog-stats', |
| 39 | 41 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
| @@ -40,11 +42,26 @@ | ||
| 40 | 42 | apply_filters( 'jetpack_widget_name', esc_html__( 'Blog Stats', 'jetpack' ) ), |
| 41 | 43 | $widget_ops |
| 42 | 44 | ); |
| 43 | 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' ) ); | |
| 44 | 47 | } |
| 45 | 48 | |
| 46 | 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 | + /** | |
| 47 | 64 | * Return an associative array of default values |
| 48 | 65 | * |
| 49 | 66 | * These values are used in new widgets. |
| 50 | 67 | * |
| @@ -67,10 +84,11 @@ | ||
| 67 | 84 | * |
| 68 | 85 | * @return string|false $views All Time Stats for that blog. |
| 69 | 86 | */ |
| 70 | 87 | public function get_stats() { |
| 88 | + $wpcom_stats = new WPCOM_Stats(); | |
| 71 | 89 | // Get data from the WordPress.com Stats REST API endpoint. |
| 72 | - $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' ) ) ); | |
| 73 | 91 | |
| 74 | 92 | if ( isset( $stats->stats->views ) ) { |
| 75 | 93 | return $stats->stats->views; |
| 76 | 94 | } else { |
| @@ -84,9 +102,9 @@ | ||
| 84 | 102 | * @see WP_Widget::form() |
| 85 | 103 | * |
| 86 | 104 | * @param array $instance Previously saved values from database. |
| 87 | 105 | * |
| 88 | - * @return void | |
| 106 | + * @return string|void | |
| 89 | 107 | */ |
| 90 | 108 | public function form( $instance ) { |
| 91 | 109 | $instance = wp_parse_args( $instance, $this->defaults() ); |
| 92 | 110 | ?> |
| @@ -98,9 +116,9 @@ | ||
| 98 | 116 | <p> |
| 99 | 117 | <label for="<?php echo esc_attr( $this->get_field_id( 'hits' ) ); ?>"><?php esc_html_e( 'Pageview Description:', 'jetpack' ); ?></label> |
| 100 | 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'] ); ?>" /> |
| 101 | 119 | </p> |
| 102 | - <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> | |
| 103 | 121 | |
| 104 | 122 | <?php |
| 105 | 123 | } |
| 106 | 124 | |