theme-fields-is-block-theme.php
105 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Themes API: add is_block_theme field to the themes endpoint. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Field controller for adding the is_block_theme field to the themes endpoint. |
| 10 | */ |
| 11 | class WPCOM_REST_API_V2_Theme_Fields_Is_Block_Theme extends WPCOM_REST_API_V2_Field_Controller { |
| 12 | // phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- extended methods must have the same signatures, even if paramaters are unused. |
| 13 | |
| 14 | /** |
| 15 | * Array of post types that can handle Publicize. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $object_type = array( 'theme' ); |
| 20 | |
| 21 | /** |
| 22 | * Field name |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $field_name = 'is_block_theme'; |
| 27 | |
| 28 | /** |
| 29 | * Permission check when getting the field. |
| 30 | * |
| 31 | * @param array $object_data The theme data. |
| 32 | * @param WP_REST_Request $request WP API request. |
| 33 | * |
| 34 | * @return bool |
| 35 | */ |
| 36 | public function get_permission_check( $object_data, $request ) { |
| 37 | // Allow access to the field if user already has permission to view the theme, |
| 38 | // as checked by the WP_REST_Themes_Controller. |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Get the value of the field. |
| 44 | * |
| 45 | * @param array $object_data The theme data. |
| 46 | * @param WP_REST_Request $request WP API request. |
| 47 | * |
| 48 | * @return bool Whether the theme is a block-based theme. |
| 49 | */ |
| 50 | public function get( $object_data, $request ) { |
| 51 | $theme = wp_get_theme( $object_data['stylesheet'] ); |
| 52 | return $theme->exists() ? $theme->is_block_theme() : false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Permission check when updating the field. |
| 57 | * |
| 58 | * This is not used because themes can't be updated using the API. |
| 59 | * |
| 60 | * @param bool $value The new value of the field. |
| 61 | * @param array $object_data The theme data. |
| 62 | * @param WP_REST_Request $request WP API request. |
| 63 | * |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function update_permission_check( $value, $object_data, $request ) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Update the field. |
| 72 | * |
| 73 | * This is not used because themes can't be updated using the API. |
| 74 | * |
| 75 | * @param mixed $value The new value for the field. |
| 76 | * @param mixed $object_data The theme data. |
| 77 | * @param WP_REST_Request $request WP API request. |
| 78 | * |
| 79 | * @return WP_Error |
| 80 | */ |
| 81 | public function update( $value, $object_data, $request ) { |
| 82 | return new WP_Error( 'not_implemeted', __( 'Themes cannot be updated using the REST API.', 'jetpack' ), array( 'status' => 501 ) ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Schema for the field. |
| 87 | * |
| 88 | * @return array |
| 89 | */ |
| 90 | public function get_schema() { |
| 91 | return array( |
| 92 | 'description' => __( 'Whether the theme is a block-based theme.', 'jetpack' ), |
| 93 | 'type' => 'boolean', |
| 94 | 'readonly' => true, |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // `is_block_theme` was added to the v2/themes endpoint in WordPress 6.3. |
| 100 | // See https://core.trac.wordpress.org/ticket/58123 |
| 101 | global $wp_version; |
| 102 | if ( version_compare( $wp_version, '6.3', '<' ) ) { // @todo Remove when WordPress 6.3 is the minimum. |
| 103 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Theme_Fields_Is_Block_Theme' ); |
| 104 | } |
| 105 |