← All changes
|
_inc/lib/core-api/class.jetpack-core-api-widgets-endpoints.php
+64
-0
16.2-beta
→
16.3-beta
View file →
| @@ -1,0 +1,64 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Interact with a specific widget via the REST API. | |
| 4 | + * Currently only supports the Milestone widget. | |
| 5 | + * | |
| 6 | + * @package automattic/jetpack | |
| 7 | + */ | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Widget information getter endpoint. | |
| 11 | + */ | |
| 12 | +class Jetpack_Core_API_Widget_Endpoint { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Get information about a widget that is supported by this endpoint. | |
| 16 | + * | |
| 17 | + * @since 5.5.0 | |
| 18 | + * | |
| 19 | + * @param WP_REST_Request $request { | |
| 20 | + * Array of parameters received by request. | |
| 21 | + * | |
| 22 | + * @type string $id Widget id. | |
| 23 | + * } | |
| 24 | + * | |
| 25 | + * @return WP_REST_Response|WP_Error A REST response if the request was served successfully, otherwise an error. | |
| 26 | + */ | |
| 27 | + public function process( $request ) { | |
| 28 | + $widget_base = _get_widget_id_base( $request['id'] ); | |
| 29 | + $widget_id = (int) substr( $request['id'], strlen( $widget_base ) + 1 ); | |
| 30 | + | |
| 31 | + switch ( $widget_base ) { | |
| 32 | + case 'milestone_widget': | |
| 33 | + $instances = get_option( 'widget_milestone_widget', array() ); | |
| 34 | + | |
| 35 | + if ( | |
| 36 | + class_exists( 'Milestone_Widget' ) | |
| 37 | + && is_active_widget( false, $widget_base . '-' . $widget_id, $widget_base ) | |
| 38 | + && isset( $instances[ $widget_id ] ) | |
| 39 | + ) { | |
| 40 | + $instance = $instances[ $widget_id ]; | |
| 41 | + $widget = new Milestone_Widget(); | |
| 42 | + return $widget->get_widget_data( $instance ); | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + return new WP_Error( | |
| 47 | + 'not_found', | |
| 48 | + esc_html__( 'The requested widget was not found.', 'jetpack' ), | |
| 49 | + array( 'status' => 404 ) | |
| 50 | + ); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Check that the current user has permissions to view widget information. | |
| 55 | + * For the currently supported widget there are no permissions required. | |
| 56 | + * | |
| 57 | + * @since 5.5.0 | |
| 58 | + * | |
| 59 | + * @return bool | |
| 60 | + */ | |
| 61 | + public function can_request() { | |
| 62 | + return true; | |
| 63 | + } | |
| 64 | +} | |