Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.26% |
322 / 349 |
|
75.00% |
9 / 12 |
CRAP | |
0.00% |
0 / 1 |
| Boost_Abilities | |
92.26% |
322 / 349 |
|
75.00% |
9 / 12 |
64.84 | |
0.00% |
0 / 1 |
| enabled_by_default | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category_definition | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| get_abilities | |
100.00% |
176 / 176 |
|
100.00% |
1 / 1 |
1 | |||
| can_view_modules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| can_manage_modules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| build_module_index | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| render_module | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| get_modules | |
65.96% |
31 / 47 |
|
0.00% |
0 / 1 |
41.09 | |||
| set_module_status | |
92.86% |
65 / 70 |
|
0.00% |
0 / 1 |
17.11 | |||
| get_speed_score | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
8 | |||
| clear_page_cache | |
53.85% |
7 / 13 |
|
0.00% |
0 / 1 |
3.88 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack Boost Abilities Registration |
| 4 | * |
| 5 | * Registers Jetpack Boost abilities with the WordPress Abilities API so AI |
| 6 | * agents can read module state, toggle modules, fetch the latest speed score, |
| 7 | * and clear the page cache through the standard `wp-abilities/v1` REST surface. |
| 8 | * |
| 9 | * @package automattic/jetpack-boost |
| 10 | */ |
| 11 | |
| 12 | namespace Automattic\Jetpack_Boost\Abilities; |
| 13 | |
| 14 | use Automattic\Jetpack\Boost_Speed_Score\Speed_Score_History; |
| 15 | use Automattic\Jetpack\WP_Abilities\Registrar; |
| 16 | use Automattic\Jetpack_Boost\Modules\Features_Index; |
| 17 | use Automattic\Jetpack_Boost\Modules\Module; |
| 18 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Page_Cache; |
| 19 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Page_Cache_Setup; |
| 20 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache; |
| 21 | |
| 22 | /** |
| 23 | * Registers Jetpack Boost abilities with the WordPress Abilities API. |
| 24 | * |
| 25 | * Surface (4 abilities, all under the `jetpack-boost/` namespace): |
| 26 | * |
| 27 | * - get-modules — filtered read of every Boost module + submodule. |
| 28 | * - set-module-status — declarative toggle, idempotent. |
| 29 | * - get-speed-score — latest mobile/desktop scores from history. |
| 30 | * - clear-page-cache — flush the Boost page cache for the home URL. |
| 31 | * |
| 32 | * @since 4.6.0 |
| 33 | */ |
| 34 | class Boost_Abilities extends Registrar { |
| 35 | |
| 36 | /** |
| 37 | * {@inheritDoc} |
| 38 | */ |
| 39 | protected static function enabled_by_default(): bool { |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @inheritDoc |
| 45 | */ |
| 46 | public static function get_category_slug(): string { |
| 47 | return 'jetpack-boost'; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | public static function get_category_definition(): array { |
| 54 | return array( |
| 55 | // "Jetpack Boost" is a product name and is not translated. |
| 56 | 'label' => 'Jetpack Boost', |
| 57 | 'description' => __( 'Abilities for inspecting and managing Jetpack Boost performance modules, speed scores, and page cache.', 'jetpack-boost' ), |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @inheritDoc |
| 63 | */ |
| 64 | public static function get_abilities(): array { |
| 65 | $module_object_schema = array( |
| 66 | 'type' => 'object', |
| 67 | 'properties' => array( |
| 68 | 'slug' => array( 'type' => 'string' ), |
| 69 | 'active' => array( 'type' => 'boolean' ), |
| 70 | 'available' => array( 'type' => 'boolean' ), |
| 71 | 'optimizing' => array( 'type' => 'boolean' ), |
| 72 | ), |
| 73 | ); |
| 74 | |
| 75 | return array( |
| 76 | 'jetpack-boost/get-modules' => array( |
| 77 | 'label' => __( 'Get Boost modules', 'jetpack-boost' ), |
| 78 | 'description' => __( 'List Jetpack Boost performance modules and submodules. Returns an array of { slug, active, available, optimizing }. Slugs use underscores (e.g. "critical_css", "page_cache", "image_cdn"). Pass slug to fetch a single module (returns a 0- or 1-element array; unknown slugs yield an empty array, never an error). Pass status to filter by active/inactive/available/optimizing. Use the returned slugs as input to jetpack-boost/set-module-status.', 'jetpack-boost' ), |
| 79 | 'input_schema' => array( |
| 80 | 'type' => 'object', |
| 81 | 'default' => array(), |
| 82 | 'properties' => array( |
| 83 | 'slug' => array( |
| 84 | 'type' => 'string', |
| 85 | 'description' => __( 'Return a single module by its slug (e.g. "critical_css", "page_cache"). Unknown slugs yield an empty array.', 'jetpack-boost' ), |
| 86 | 'minLength' => 1, |
| 87 | ), |
| 88 | 'status' => array( |
| 89 | 'type' => 'string', |
| 90 | 'description' => __( 'Filter by lifecycle state. "active" = enabled by the user. "inactive" = available but disabled. "available" = currently loadable on this site (regardless of enabled state). "optimizing" = active and currently serving optimized output.', 'jetpack-boost' ), |
| 91 | 'enum' => array( 'active', 'inactive', 'available', 'optimizing' ), |
| 92 | ), |
| 93 | 'search' => array( |
| 94 | 'type' => 'string', |
| 95 | 'description' => __( 'Case-insensitive substring match against the module slug.', 'jetpack-boost' ), |
| 96 | 'minLength' => 1, |
| 97 | ), |
| 98 | ), |
| 99 | 'additionalProperties' => false, |
| 100 | ), |
| 101 | 'output_schema' => array( |
| 102 | 'type' => 'array', |
| 103 | 'items' => $module_object_schema, |
| 104 | ), |
| 105 | 'execute_callback' => array( __CLASS__, 'get_modules' ), |
| 106 | 'permission_callback' => array( __CLASS__, 'can_view_modules' ), |
| 107 | 'meta' => array( |
| 108 | 'annotations' => array( |
| 109 | 'readonly' => true, |
| 110 | 'destructive' => false, |
| 111 | 'idempotent' => true, |
| 112 | ), |
| 113 | 'public' => true, |
| 114 | 'show_in_rest' => true, |
| 115 | 'mcp' => array( |
| 116 | 'public' => true, |
| 117 | 'type' => 'tool', |
| 118 | ), |
| 119 | ), |
| 120 | ), |
| 121 | |
| 122 | 'jetpack-boost/set-module-status' => array( |
| 123 | 'label' => __( 'Set Boost module status', 'jetpack-boost' ), |
| 124 | 'description' => __( 'Enable or disable a single Jetpack Boost module by slug. Required: { slug, active }. Returns { slug, active, changed }. Idempotent: setting a module to its current state returns changed=false. Slugs use underscores (e.g. "critical_css", "page_cache"). Unknown slugs return jetpack_boost_invalid_slug; modules not loadable on this site return jetpack_boost_module_unavailable; always-on modules cannot be disabled and return jetpack_boost_module_always_on — call jetpack-boost/get-modules to enumerate available slugs. Toggling a parent module also drives submodule lifecycle. Turning on page_cache also sets up the cache, as the Boost settings screen does; if that setup fails, the module stays on and jetpack_boost_page_cache_setup_failed explains why. When setup does something the site owner should know about, such as switching off WP Super Cache, the result (or the error data on failure) includes notices: [ { title, message } ]; pass them on to the user.', 'jetpack-boost' ), |
| 125 | 'input_schema' => array( |
| 126 | 'type' => 'object', |
| 127 | 'required' => array( 'slug', 'active' ), |
| 128 | 'properties' => array( |
| 129 | 'slug' => array( |
| 130 | 'type' => 'string', |
| 131 | 'description' => __( 'Module slug to toggle (e.g. "critical_css", "page_cache").', 'jetpack-boost' ), |
| 132 | 'minLength' => 1, |
| 133 | ), |
| 134 | 'active' => array( |
| 135 | 'type' => 'boolean', |
| 136 | 'description' => __( 'Desired state: true to enable, false to disable.', 'jetpack-boost' ), |
| 137 | ), |
| 138 | ), |
| 139 | 'additionalProperties' => false, |
| 140 | ), |
| 141 | 'output_schema' => array( |
| 142 | 'type' => 'object', |
| 143 | 'properties' => array( |
| 144 | 'slug' => array( 'type' => 'string' ), |
| 145 | 'active' => array( 'type' => 'boolean' ), |
| 146 | 'changed' => array( 'type' => 'boolean' ), |
| 147 | 'notices' => array( |
| 148 | 'type' => 'array', |
| 149 | 'items' => array( |
| 150 | 'type' => 'object', |
| 151 | 'properties' => array( |
| 152 | 'title' => array( 'type' => 'string' ), |
| 153 | 'message' => array( 'type' => 'string' ), |
| 154 | ), |
| 155 | ), |
| 156 | ), |
| 157 | ), |
| 158 | ), |
| 159 | 'execute_callback' => array( __CLASS__, 'set_module_status' ), |
| 160 | 'permission_callback' => array( __CLASS__, 'can_manage_modules' ), |
| 161 | 'meta' => array( |
| 162 | 'annotations' => array( |
| 163 | 'readonly' => false, |
| 164 | 'destructive' => false, |
| 165 | 'idempotent' => true, |
| 166 | ), |
| 167 | 'public' => true, |
| 168 | 'show_in_rest' => true, |
| 169 | 'mcp' => array( |
| 170 | 'public' => true, |
| 171 | 'type' => 'tool', |
| 172 | ), |
| 173 | ), |
| 174 | ), |
| 175 | |
| 176 | 'jetpack-boost/get-speed-score' => array( |
| 177 | 'label' => __( 'Get latest speed score', 'jetpack-boost' ), |
| 178 | 'description' => __( 'Return the most recent Jetpack Boost speed score for the home URL. Returns { mobile, desktop, timestamp, is_stale, has_history }. mobile/desktop are integers 0-100 (Google PageSpeed scale) or null when no score has been recorded. timestamp is a Unix epoch in seconds marking when the score was last measured, not when it last changed. is_stale=true means the latest score is older than 24 hours or invalidated by a site change; agents should request a refresh from the Boost UI before quoting it. has_history=false means no scores have been recorded yet.', 'jetpack-boost' ), |
| 179 | 'input_schema' => array( |
| 180 | 'type' => 'object', |
| 181 | 'default' => array(), |
| 182 | 'properties' => array(), |
| 183 | 'additionalProperties' => false, |
| 184 | ), |
| 185 | 'output_schema' => array( |
| 186 | 'type' => 'object', |
| 187 | 'properties' => array( |
| 188 | 'mobile' => array( 'type' => array( 'integer', 'null' ) ), |
| 189 | 'desktop' => array( 'type' => array( 'integer', 'null' ) ), |
| 190 | 'timestamp' => array( 'type' => array( 'integer', 'null' ) ), |
| 191 | 'is_stale' => array( 'type' => 'boolean' ), |
| 192 | 'has_history' => array( 'type' => 'boolean' ), |
| 193 | ), |
| 194 | ), |
| 195 | 'execute_callback' => array( __CLASS__, 'get_speed_score' ), |
| 196 | 'permission_callback' => array( __CLASS__, 'can_view_modules' ), |
| 197 | 'meta' => array( |
| 198 | 'annotations' => array( |
| 199 | 'readonly' => true, |
| 200 | 'destructive' => false, |
| 201 | 'idempotent' => true, |
| 202 | ), |
| 203 | 'public' => true, |
| 204 | 'show_in_rest' => true, |
| 205 | 'mcp' => array( |
| 206 | 'public' => true, |
| 207 | 'type' => 'tool', |
| 208 | ), |
| 209 | ), |
| 210 | ), |
| 211 | |
| 212 | 'jetpack-boost/clear-page-cache' => array( |
| 213 | 'label' => __( 'Clear page cache', 'jetpack-boost' ), |
| 214 | 'description' => __( 'Clear every cached page under the site home URL. Idempotent: re-running on an empty cache is a no-op. Returns { cleared, message }. cleared=true means the clear request was dispatched against an active page_cache module; it does not promise that cached files actually existed (the underlying API does not surface a count). Requires the page_cache module to be active; if it is not, returns jetpack_boost_page_cache_inactive — enable it first via jetpack-boost/set-module-status with slug="page_cache".', 'jetpack-boost' ), |
| 215 | 'input_schema' => array( |
| 216 | 'type' => 'object', |
| 217 | 'default' => array(), |
| 218 | 'properties' => array(), |
| 219 | 'additionalProperties' => false, |
| 220 | ), |
| 221 | 'output_schema' => array( |
| 222 | 'type' => 'object', |
| 223 | 'properties' => array( |
| 224 | 'cleared' => array( 'type' => 'boolean' ), |
| 225 | 'message' => array( 'type' => 'string' ), |
| 226 | ), |
| 227 | ), |
| 228 | 'execute_callback' => array( __CLASS__, 'clear_page_cache' ), |
| 229 | 'permission_callback' => array( __CLASS__, 'can_manage_modules' ), |
| 230 | 'meta' => array( |
| 231 | 'annotations' => array( |
| 232 | 'readonly' => false, |
| 233 | 'destructive' => false, |
| 234 | 'idempotent' => true, |
| 235 | ), |
| 236 | 'public' => true, |
| 237 | 'show_in_rest' => true, |
| 238 | 'mcp' => array( |
| 239 | 'public' => true, |
| 240 | 'type' => 'tool', |
| 241 | ), |
| 242 | ), |
| 243 | ), |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Permission check for read-only abilities. |
| 249 | * |
| 250 | * Boost has no domain-specific capability; every existing Boost surface |
| 251 | * gates on `manage_options`. We mirror that here so abilities are no more |
| 252 | * (or less) permissive than the REST and admin surfaces. |
| 253 | * |
| 254 | * @since 4.6.0 |
| 255 | */ |
| 256 | public static function can_view_modules(): bool { |
| 257 | return is_user_logged_in() && current_user_can( 'manage_options' ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Permission check for write abilities. |
| 262 | * |
| 263 | * @since 4.6.0 |
| 264 | */ |
| 265 | public static function can_manage_modules(): bool { |
| 266 | return is_user_logged_in() && current_user_can( 'manage_options' ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Cache for `build_module_index()`. Module construction touches options for |
| 271 | * every feature, so we memoise per-request — both `get_modules()` and |
| 272 | * `set_module_status()` consume the same map. |
| 273 | * |
| 274 | * @var array<string, Module>|null |
| 275 | */ |
| 276 | private static $module_index_cache = null; |
| 277 | |
| 278 | /** |
| 279 | * Build a `Module` instance for every feature + submodule, keyed by slug. |
| 280 | * |
| 281 | * @return array<string, Module> |
| 282 | */ |
| 283 | private static function build_module_index(): array { |
| 284 | if ( null !== self::$module_index_cache ) { |
| 285 | return self::$module_index_cache; |
| 286 | } |
| 287 | |
| 288 | $index = array(); |
| 289 | foreach ( Features_Index::get_all_features() as $feature_class ) { |
| 290 | $module = new Module( new $feature_class() ); |
| 291 | $index[ $module->get_slug() ] = $module; |
| 292 | } |
| 293 | |
| 294 | self::$module_index_cache = $index; |
| 295 | return $index; |
| 296 | } |
| 297 | |
| 298 | private static function render_module( Module $module ): array { |
| 299 | return array( |
| 300 | 'slug' => $module->get_slug(), |
| 301 | 'active' => $module->is_available() && $module->is_enabled(), |
| 302 | 'available' => $module->is_available(), |
| 303 | 'optimizing' => $module->is_optimizing(), |
| 304 | ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Execute: filtered read of Boost modules. |
| 309 | * |
| 310 | * @since 4.6.0 |
| 311 | * |
| 312 | * @param array|null $input Input matching the ability's input_schema. |
| 313 | * @return array|\WP_Error |
| 314 | */ |
| 315 | public static function get_modules( $input = null ) { |
| 316 | $input = is_array( $input ) ? $input : array(); |
| 317 | $index = self::build_module_index(); |
| 318 | |
| 319 | // Single-slug short-circuit — return 0- or 1-element array, same shape as the list case. |
| 320 | // A non-string slug is invalid input (not "unknown"); rejecting it prevents the bad-shape |
| 321 | // fall-through where slug:123 would silently return every module. |
| 322 | if ( isset( $input['slug'] ) ) { |
| 323 | if ( ! is_string( $input['slug'] ) ) { |
| 324 | return new \WP_Error( |
| 325 | 'jetpack_boost_invalid_slug', |
| 326 | __( 'The slug parameter must be a string.', 'jetpack-boost' ) |
| 327 | ); |
| 328 | } |
| 329 | if ( '' !== $input['slug'] ) { |
| 330 | return isset( $index[ $input['slug'] ] ) |
| 331 | ? array( self::render_module( $index[ $input['slug'] ] ) ) |
| 332 | : array(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | $status_filter = isset( $input['status'] ) && is_string( $input['status'] ) && '' !== $input['status'] |
| 337 | ? $input['status'] |
| 338 | : null; |
| 339 | $search_filter = isset( $input['search'] ) && is_string( $input['search'] ) && '' !== $input['search'] |
| 340 | ? $input['search'] |
| 341 | : null; |
| 342 | |
| 343 | $out = array(); |
| 344 | foreach ( $index as $slug => $module ) { |
| 345 | $rendered = self::render_module( $module ); |
| 346 | |
| 347 | if ( null !== $status_filter ) { |
| 348 | $matches = false; |
| 349 | switch ( $status_filter ) { |
| 350 | case 'active': |
| 351 | $matches = $rendered['active']; |
| 352 | break; |
| 353 | case 'inactive': |
| 354 | $matches = $rendered['available'] && ! $rendered['active']; |
| 355 | break; |
| 356 | case 'available': |
| 357 | $matches = $rendered['available']; |
| 358 | break; |
| 359 | case 'optimizing': |
| 360 | $matches = $rendered['optimizing']; |
| 361 | break; |
| 362 | } |
| 363 | if ( ! $matches ) { |
| 364 | continue; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // Boost slugs are ASCII snake_case; stripos is sufficient and avoids ext-mbstring. |
| 369 | if ( null !== $search_filter && false === stripos( $slug, $search_filter ) ) { |
| 370 | continue; |
| 371 | } |
| 372 | |
| 373 | $out[] = $rendered; |
| 374 | } |
| 375 | |
| 376 | // Deterministic ordering — agents diff results across calls. |
| 377 | usort( |
| 378 | $out, |
| 379 | static function ( $a, $b ) { |
| 380 | return strcmp( $a['slug'], $b['slug'] ); |
| 381 | } |
| 382 | ); |
| 383 | |
| 384 | return $out; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Execute: declarative module toggle. Idempotent. |
| 389 | * |
| 390 | * @since 4.6.0 |
| 391 | * |
| 392 | * @param array|null $input Input matching the ability's input_schema. |
| 393 | * @return array|\WP_Error |
| 394 | */ |
| 395 | public static function set_module_status( $input = null ) { |
| 396 | $input = is_array( $input ) ? $input : array(); |
| 397 | |
| 398 | // Required-id validation: not empty(), so "0" remains a legal slug if a future module ever uses it. |
| 399 | if ( ! isset( $input['slug'] ) || ! is_string( $input['slug'] ) || '' === $input['slug'] ) { |
| 400 | return new \WP_Error( |
| 401 | 'jetpack_boost_missing_slug', |
| 402 | __( 'A module slug is required. Call jetpack-boost/get-modules to enumerate available slugs.', 'jetpack-boost' ) |
| 403 | ); |
| 404 | } |
| 405 | if ( ! array_key_exists( 'active', $input ) ) { |
| 406 | return new \WP_Error( |
| 407 | 'jetpack_boost_missing_active', |
| 408 | __( 'A desired active state (boolean) is required.', 'jetpack-boost' ) |
| 409 | ); |
| 410 | } |
| 411 | if ( ! is_bool( $input['active'] ) ) { |
| 412 | return new \WP_Error( |
| 413 | 'jetpack_boost_invalid_active', |
| 414 | __( 'The active parameter must be a boolean. Strings like "true" / "false" are not accepted.', 'jetpack-boost' ) |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | $slug = $input['slug']; |
| 419 | $desired = $input['active']; |
| 420 | $index = self::build_module_index(); |
| 421 | |
| 422 | if ( ! isset( $index[ $slug ] ) ) { |
| 423 | return new \WP_Error( |
| 424 | 'jetpack_boost_invalid_slug', |
| 425 | __( 'Unknown Boost module slug. Call jetpack-boost/get-modules to enumerate available slugs.', 'jetpack-boost' ) |
| 426 | ); |
| 427 | } |
| 428 | |
| 429 | $module = $index[ $slug ]; |
| 430 | |
| 431 | if ( ! $module->is_available() ) { |
| 432 | return new \WP_Error( |
| 433 | 'jetpack_boost_module_unavailable', |
| 434 | __( 'This module is not available on this site (e.g. requires a connection or a paid plan).', 'jetpack-boost' ) |
| 435 | ); |
| 436 | } |
| 437 | |
| 438 | // Always-on modules ignore the persisted option at runtime. Writing here would leave |
| 439 | // on-disk state diverged from runtime state with no rollback, so refuse the write up front. |
| 440 | if ( $module->is_always_on() ) { |
| 441 | if ( $desired ) { |
| 442 | return array( |
| 443 | 'slug' => $slug, |
| 444 | 'active' => true, |
| 445 | 'changed' => false, |
| 446 | ); |
| 447 | } |
| 448 | return new \WP_Error( |
| 449 | 'jetpack_boost_module_always_on', |
| 450 | __( 'This module is always on and cannot be disabled.', 'jetpack-boost' ) |
| 451 | ); |
| 452 | } |
| 453 | |
| 454 | // A saved status reads back as "1", not true. |
| 455 | $changed = $desired !== (bool) $module->is_enabled(); |
| 456 | if ( $changed ) { |
| 457 | if ( ! $module->update( $desired ) ) { |
| 458 | return new \WP_Error( |
| 459 | 'jetpack_boost_module_update_failed', |
| 460 | __( 'Failed to persist the module status.', 'jetpack-boost' ) |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Fires when a module is enabled or disabled through the abilities surface. |
| 466 | * |
| 467 | * Mirrors the action emitted by `Modules_State_Entry::set()` so submodule |
| 468 | * lifecycle handlers fire identically regardless of caller. |
| 469 | * |
| 470 | * @param string $module_slug The module slug. |
| 471 | * @param bool $is_active The new state. |
| 472 | */ |
| 473 | do_action( 'jetpack_boost_module_status_updated', $slug, $desired ); |
| 474 | } |
| 475 | |
| 476 | $notices = array(); |
| 477 | // Also when it was already on, as the settings screen does on load, so calling again retries a failed setup. |
| 478 | if ( $desired && Page_Cache::get_slug() === $slug ) { |
| 479 | $setup_result = Page_Cache_Setup::enable(); |
| 480 | // A step can switch off WP Super Cache before a later step fails. |
| 481 | $notices = Page_Cache_Setup::get_notices(); |
| 482 | if ( is_wp_error( $setup_result ) ) { |
| 483 | return new \WP_Error( |
| 484 | 'jetpack_boost_page_cache_setup_failed', |
| 485 | sprintf( |
| 486 | /* translators: %s: setup error code, such as "wp-config-not-writable". */ |
| 487 | __( 'Page Cache is on, but its setup failed (%s), so pages are not cached yet. Fix the problem shown in the Page Cache settings in Jetpack Boost, then call this again.', 'jetpack-boost' ), |
| 488 | $setup_result->get_error_code() |
| 489 | ), |
| 490 | array( 'notices' => $notices ) |
| 491 | ); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | $result = array( |
| 496 | 'slug' => $slug, |
| 497 | 'active' => $desired, |
| 498 | 'changed' => $changed, |
| 499 | ); |
| 500 | if ( $notices ) { |
| 501 | $result['notices'] = $notices; |
| 502 | } |
| 503 | return $result; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Execute: latest speed score for the home URL. |
| 508 | * |
| 509 | * @since 4.6.0 |
| 510 | * |
| 511 | * @param array|null $input Unused; ability has no inputs. |
| 512 | * @return array |
| 513 | */ |
| 514 | public static function get_speed_score( $input = null ) { |
| 515 | unset( $input ); |
| 516 | |
| 517 | $history = new Speed_Score_History( home_url() ); |
| 518 | $latest = $history->latest(); |
| 519 | |
| 520 | if ( null === $latest ) { |
| 521 | return array( |
| 522 | 'mobile' => null, |
| 523 | 'desktop' => null, |
| 524 | 'timestamp' => null, |
| 525 | 'is_stale' => false, |
| 526 | 'has_history' => false, |
| 527 | ); |
| 528 | } |
| 529 | |
| 530 | // `scores` may be stored as either an associative array or stdClass depending on |
| 531 | // the API response payload. Normalise to array before reading. |
| 532 | $scores = isset( $latest['scores'] ) ? (array) $latest['scores'] : array(); |
| 533 | $mobile = isset( $scores['mobile'] ) && is_numeric( $scores['mobile'] ) ? (int) $scores['mobile'] : null; |
| 534 | $desktop = isset( $scores['desktop'] ) && is_numeric( $scores['desktop'] ) ? (int) $scores['desktop'] : null; |
| 535 | |
| 536 | return array( |
| 537 | 'mobile' => $mobile, |
| 538 | 'desktop' => $desktop, |
| 539 | 'timestamp' => isset( $latest['timestamp'] ) ? (int) $latest['timestamp'] : null, |
| 540 | 'is_stale' => $history->is_stale(), |
| 541 | 'has_history' => true, |
| 542 | ); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Execute: clear the Boost page cache for the home URL. |
| 547 | * |
| 548 | * @since 4.6.0 |
| 549 | * |
| 550 | * @param array|null $input Unused; ability has no inputs. |
| 551 | * @return array|\WP_Error |
| 552 | */ |
| 553 | public static function clear_page_cache( $input = null ) { |
| 554 | unset( $input ); |
| 555 | |
| 556 | $page_cache = new Module( new Page_Cache() ); |
| 557 | if ( ! $page_cache->is_available() || ! $page_cache->is_enabled() ) { |
| 558 | return new \WP_Error( |
| 559 | 'jetpack_boost_page_cache_inactive', |
| 560 | __( 'The page_cache module is not active. Enable it first via jetpack-boost/set-module-status with slug="page_cache" and active=true.', 'jetpack-boost' ) |
| 561 | ); |
| 562 | } |
| 563 | |
| 564 | $cache = new Boost_Cache(); |
| 565 | // Boost_Cache::delete_recursive() returns void — no useful success/no-op signal |
| 566 | // to surface to the agent. Reporting `cleared: true` after a successful module-active |
| 567 | // gate is the most honest answer we can give. |
| 568 | $cache->delete_recursive( home_url() ); |
| 569 | |
| 570 | return array( |
| 571 | 'cleared' => true, |
| 572 | 'message' => __( 'Page cache cleared.', 'jetpack-boost' ), |
| 573 | ); |
| 574 | } |
| 575 | } |