Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
54.55% |
12 / 22 |
|
50.00% |
2 / 4 |
CRAP | n/a |
0 / 0 |
|
| wpcomsh_rest_api_wordpress_versions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| wpcomsh_get_wordpress_track_version | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| wpcomsh_rest_api_wordpress_versions_permission | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| wpcomsh_rest_api_wordpress_versions_init | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress versions endpoint. |
| 4 | * |
| 5 | * Reports the WordPress versions WP Cloud's latest and beta tracks serve, which can lag |
| 6 | * behind wordpress.org. Calypso shows them when a site switches between the two. |
| 7 | * |
| 8 | * @package endpoints |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Returns the WordPress version each track serves, or null if the track isn't available. |
| 13 | * |
| 14 | * @return WP_REST_Response |
| 15 | */ |
| 16 | function wpcomsh_rest_api_wordpress_versions() { |
| 17 | $versions = array(); |
| 18 | foreach ( array( 'latest', 'beta' ) as $track ) { |
| 19 | $versions[ $track ] = wpcomsh_get_wordpress_track_version( $track ); |
| 20 | } |
| 21 | |
| 22 | return new WP_REST_Response( $versions, 200 ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Reads the WordPress version a WP Cloud track serves. |
| 27 | * |
| 28 | * @param string $track The track, e.g. "beta". |
| 29 | * @return string|null |
| 30 | */ |
| 31 | function wpcomsh_get_wordpress_track_version( $track ) { |
| 32 | $version_file = "/wordpress/core/$track/wp-includes/version.php"; |
| 33 | if ( ! is_readable( $version_file ) ) { |
| 34 | return null; |
| 35 | } |
| 36 | |
| 37 | // The file sets $wp_version in this scope, like wp_get_wp_version() does. |
| 38 | $wp_version = null; |
| 39 | include $version_file; |
| 40 | |
| 41 | return $wp_version; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Permission callback: only allow users who can manage the site. |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | function wpcomsh_rest_api_wordpress_versions_permission() { |
| 50 | return current_user_can( 'manage_options' ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Initialize API. |
| 55 | */ |
| 56 | function wpcomsh_rest_api_wordpress_versions_init() { |
| 57 | register_rest_route( |
| 58 | 'wpcomsh/v1', |
| 59 | '/wordpress-versions', |
| 60 | array( |
| 61 | array( |
| 62 | 'methods' => 'GET', |
| 63 | 'permission_callback' => 'wpcomsh_rest_api_wordpress_versions_permission', |
| 64 | 'callback' => 'wpcomsh_rest_api_wordpress_versions', |
| 65 | ), |
| 66 | ) |
| 67 | ); |
| 68 | } |