| 55 | |
| 56 | register_rest_route( 'oembed/1.0', '/proxy', array( |
| 57 | array( |
| 58 | 'methods' => WP_REST_Server::READABLE, |
| 59 | 'callback' => array( $this, 'get_proxy_item' ), |
| 60 | 'permission_callback' => array( $this, 'get_proxy_item_permissions_check' ), |
| 61 | 'args' => array( |
| 62 | 'url' => array( |
| 63 | 'required' => true, |
| 64 | 'sanitize_callback' => 'esc_url_raw', |
| 65 | ), |
| 66 | 'format' => array( |
| 67 | 'default' => 'json', |
| 68 | 'sanitize_callback' => 'wp_oembed_ensure_format', |
| 69 | ), |
| 70 | 'maxwidth' => array( |
| 71 | 'default' => $maxwidth, |
| 72 | 'sanitize_callback' => 'absint', |
| 73 | ), |
| 74 | 'maxheight' => array( |
| 75 | 'sanitize_callback' => 'absint', |
| 76 | ), |
| 77 | ), |
| 78 | ), |
| 79 | ) ); |
| 114 | |
| 115 | /** |
| 116 | * Checks if current user can make a proxy oEmbed request. |
| 117 | * |
| 118 | * @since 4.8.0 |
| 119 | * @access public |
| 120 | * |
| 121 | * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 122 | */ |
| 123 | function get_proxy_item_permissions_check() { |
| 124 | |
| 125 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 126 | return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to make proxied oEmbed requests.' ), array( 'status' => rest_authorization_required_code() ) ); |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Callback for the proxy API endpoint. |
| 133 | * |
| 134 | * Returns the JSON object for the proxied item. |
| 135 | * |
| 136 | * @since 4.8.0 |
| 137 | * @access public |
| 138 | * |
| 139 | * @see WP_oEmbed::get_html() |
| 140 | * @param WP_REST_Request $request Full data about the request. |
| 141 | * @return WP_Error|array oEmbed response data or WP_Error on failure. |
| 142 | */ |
| 143 | public function get_proxy_item( $request ) { |
| 144 | |
| 145 | $wp_oembed = _wp_oembed_get_object(); |
| 146 | $url = $request['url']; |
| 147 | $args = $request->get_params(); |
| 148 | unset( $args['url'] ); |
| 149 | |
| 150 | $provider = $wp_oembed->get_provider( $url, $args ); |
| 151 | |
| 152 | if ( ! $provider ) { |
| 153 | return new WP_Error( 'oembed_unknown_provider', get_status_header_desc( 404 ), array( 'status' => 404 ) ); |
| 154 | } |
| 155 | |
| 156 | $data = $wp_oembed->fetch( $provider, $url, $args ); |
| 157 | if ( false === $data ) { |
| 158 | return new WP_Error( 'oembed_no_provider_response', get_status_header_desc( 404 ), array( 'status' => 404 ) ); |
| 159 | } |
| 160 | |
| 161 | return (array) $data; |
| 162 | } |