← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-google-docs.php
+66
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,66 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Validate whether a google doc is available for embedding. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | + | |
| 8 | +use Automattic\Jetpack\Extensions\GoogleDocsEmbed; | |
| 9 | + | |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 11 | + exit( 0 ); | |
| 12 | +} | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Google Docs block endpoint. | |
| 16 | + */ | |
| 17 | +class WPCOM_REST_API_V2_Endpoint_Google_Docs extends WP_REST_Controller { | |
| 18 | + /** | |
| 19 | + * Constructor. | |
| 20 | + */ | |
| 21 | + public function __construct() { | |
| 22 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * Register endpoint route. | |
| 27 | + */ | |
| 28 | + public function register_routes() { | |
| 29 | + register_rest_route( | |
| 30 | + 'wpcom/v2', | |
| 31 | + '/checkGoogleDocVisibility', | |
| 32 | + array( | |
| 33 | + array( | |
| 34 | + 'methods' => WP_REST_Server::READABLE, | |
| 35 | + 'callback' => array( $this, 'check_document_visibility' ), | |
| 36 | + 'permission_callback' => function () { | |
| 37 | + return current_user_can( 'edit_posts' ); | |
| 38 | + }, | |
| 39 | + ), | |
| 40 | + ) | |
| 41 | + ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Check URL | |
| 46 | + * | |
| 47 | + * @param \WP_REST_Request $request request object. | |
| 48 | + * | |
| 49 | + * @return \WP_REST_Response|\WP_Error | |
| 50 | + */ | |
| 51 | + public function check_document_visibility( $request ) { | |
| 52 | + | |
| 53 | + $document_url = $request->get_param( 'url' ); | |
| 54 | + $document_url = GoogleDocsEmbed\map_gsuite_url( $document_url ); | |
| 55 | + $response_head = wp_safe_remote_head( $document_url ); | |
| 56 | + $is_public_document = ! is_wp_error( $response_head ) && ! empty( $response_head['response']['code'] ) && 200 === absint( $response_head['response']['code'] ); | |
| 57 | + | |
| 58 | + if ( ! $is_public_document ) { | |
| 59 | + return new \WP_Error( 'Unauthorized', esc_html__( 'The document is not publicly accessible', 'jetpack' ), array( 'status' => 401 ) ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + return new \WP_REST_Response( '', 200 ); | |
| 63 | + } | |
| 64 | +} | |
| 65 | + | |
| 66 | +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Google_Docs' ); | |