Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_rest_api_gutenberg_version | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| wpcomsh_rest_api_gutenberg_version_permission | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| wpcomsh_rest_api_gutenberg_version_nocache | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| wpcomsh_rest_api_gutenberg_version_init | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Gutenberg version endpoint. |
| 4 | * |
| 5 | * Exposes the active Gutenberg plugin version for designated sites, used by |
| 6 | * team tooling (gbstatus) to report Gutenberg versions across environments |
| 7 | * without requiring per-site authentication. |
| 8 | * |
| 9 | * @package endpoints |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Returns the active Gutenberg plugin version, or null if the plugin is not active. |
| 14 | * |
| 15 | * @return WP_REST_Response |
| 16 | */ |
| 17 | function wpcomsh_rest_api_gutenberg_version() { |
| 18 | $version = defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : null; |
| 19 | |
| 20 | return new WP_REST_Response( |
| 21 | array( |
| 22 | 'version' => $version, |
| 23 | ), |
| 24 | 200 |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Permission callback: only allow sites where the `wpcomsh_expose_gutenberg_version` option is enabled. |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | function wpcomsh_rest_api_gutenberg_version_permission() { |
| 34 | return (bool) get_option( 'wpcomsh_expose_gutenberg_version', false ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Add no-cache headers to responses for this endpoint so the WoA edge cache |
| 39 | * doesn't serve stale 200/401 responses after the option is toggled. |
| 40 | * |
| 41 | * @param WP_REST_Response $response The REST response. |
| 42 | * @param WP_REST_Server $server The REST server instance. |
| 43 | * @param WP_REST_Request $request The REST request. |
| 44 | * @return WP_REST_Response |
| 45 | */ |
| 46 | function wpcomsh_rest_api_gutenberg_version_nocache( $response, $server, $request ) { |
| 47 | if ( $request instanceof WP_REST_Request && '/wpcomsh/v1/gutenberg-version' === $request->get_route() ) { |
| 48 | $response->header( 'Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0' ); |
| 49 | } |
| 50 | return $response; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Initialize API. |
| 55 | */ |
| 56 | function wpcomsh_rest_api_gutenberg_version_init() { |
| 57 | register_rest_route( |
| 58 | 'wpcomsh/v1', |
| 59 | '/gutenberg-version', |
| 60 | array( |
| 61 | array( |
| 62 | 'methods' => 'GET', |
| 63 | 'permission_callback' => 'wpcomsh_rest_api_gutenberg_version_permission', |
| 64 | 'callback' => 'wpcomsh_rest_api_gutenberg_version', |
| 65 | ), |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | add_filter( 'rest_post_dispatch', 'wpcomsh_rest_api_gutenberg_version_nocache', 10, 3 ); |
| 70 | } |