← All changes
|
jetpack_vendor/automattic/jetpack-stats-admin/src/class-dashboard.php
+36
-0
16.3-a.3
→
16.3
View file →
| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | namespace Automattic\Jetpack\Stats_Admin; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Admin_UI\Admin_Menu; |
| 11 | 11 | use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State; |
| 12 | +use Automattic\Jetpack\Current_Plan as Jetpack_Plan; | |
| 12 | 13 | use Automattic\Jetpack\Stats\Options as Stats_Options; |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * Responsible for adding a stats dashboard to wp-admin. |
| @@ -24,8 +25,15 @@ | ||
| 24 | 25 | */ |
| 25 | 26 | private static $initialized = false; |
| 26 | 27 | |
| 27 | 28 | /** |
| 29 | + * Transient that throttles the plan refresh below. | |
| 30 | + * | |
| 31 | + * @var string | |
| 32 | + */ | |
| 33 | + private const PLAN_REFRESH_TRANSIENT = 'jetpack_stats_admin_plan_refresh'; | |
| 34 | + | |
| 35 | + /** | |
| 28 | 36 | * Priority for the dashboard menu |
| 29 | 37 | * For Jetpack sites: Jetpack uses 998 and 'Admin_Menu' uses 1000, so we need to use 999. |
| 30 | 38 | * |
| 31 | 39 | * Admin_Menu registers what it has queued at priority 1000, so this has to stay below it. |
| @@ -169,9 +177,37 @@ | ||
| 169 | 177 | /** |
| 170 | 178 | * Initialize the admin resources. |
| 171 | 179 | */ |
| 172 | 180 | public function admin_init() { |
| 181 | + $this->maybe_refresh_plan(); | |
| 173 | 182 | add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ) ); |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Fill an empty plan cache before the config data that reads it is printed. | |
| 187 | + * | |
| 188 | + * The app cannot refresh the plan it paywalls on, so a site that never stored one renders as | |
| 189 | + * free. Throttled and time-boxed, because WordPress.com can keep answering without a plan. | |
| 190 | + */ | |
| 191 | + private function maybe_refresh_plan() { | |
| 192 | + if ( ! Main::is_site_connected() ) { | |
| 193 | + return; | |
| 194 | + } | |
| 195 | + | |
| 196 | + // method_exists guard: an older plans package may win the autoloader on another plugin. | |
| 197 | + if ( method_exists( Jetpack_Plan::class, 'get_wpcom_site_specific_features' ) | |
| 198 | + && null !== Jetpack_Plan::get_wpcom_site_specific_features() ) { | |
| 199 | + return; | |
| 200 | + } | |
| 201 | + | |
| 202 | + $plan = Jetpack_Plan::get(); | |
| 203 | + if ( ! empty( $plan['features']['active'] ) || get_transient( self::PLAN_REFRESH_TRANSIENT ) ) { | |
| 204 | + return; | |
| 205 | + } | |
| 206 | + | |
| 207 | + set_transient( self::PLAN_REFRESH_TRANSIENT, 1, 15 * MINUTE_IN_SECONDS ); | |
| 208 | + | |
| 209 | + Jetpack_Plan::refresh_from_wpcom( array( 'timeout' => 5 ) ); | |
| 174 | 210 | } |
| 175 | 211 | |
| 176 | 212 | /** |
| 177 | 213 | * Load the admin scripts. |