/**
  * Callback for our API endpoint.
  *
  * Returns the JSON object for the post.
  *
  * @param WP_REST_Request $request Full data about the request.
  * @return WP_Error|WP_REST_Response
  */
 public function get_item($request)
 {
     $post_id = url_to_postid($request['url']);
     /**
      * Filter the determined post id.
      *
      * @param int    $post_id The post ID.
      * @param string $url     The requestd URL.
      */
     $post_id = apply_filters('oembed_request_post_id', $post_id, $request['url']);
     if (0 === $post_id) {
         return new WP_Error('oembed_invalid_url', __('Invalid URL.', 'oembed-api'), array('status' => 404));
     }
     return get_oembed_response_data($post_id, $request['maxwidth']);
 }
 /**
  * Callback for the API endpoint.
  *
  * Returns the JSON object for the post.
  *
  * @since 4.4.0
  *
  * @param WP_REST_Request $request Full data about the request.
  * @return WP_Error|array oEmbed response data or WP_Error on failure.
  */
 public function get_item($request)
 {
     $post_id = url_to_postid($request['url']);
     /**
      * Filter the determined post ID.
      *
      * @since 4.4.0
      *
      * @param int    $post_id The post ID.
      * @param string $url     The requested URL.
      */
     $post_id = apply_filters('oembed_request_post_id', $post_id, $request['url']);
     $data = get_oembed_response_data($post_id, $request['maxwidth']);
     if (!$data) {
         return new WP_Error('oembed_invalid_url', get_status_header_desc(404), array('status' => 404));
     }
     return $data;
 }
 /**
  * Callback for our API endpoint.
  *
  * Returns the JSON object for the post.
  *
  * @param WP_REST_Request $request Full details about the request.
  *
  * @return WP_Error|WP_REST_Response
  */
 public function get_item(WP_REST_Request $request)
 {
     $post_id = url_to_postid($request['url']);
     /**
      * Filter the determined post id.
      *
      * @param int    $post_id The post ID.
      * @param string $url     The requestd URL.
      */
     $post_id = apply_filters('oembed_request_post_id', $post_id, $request['url']);
     if (0 === $post_id) {
         return new WP_Error('oembed_invalid_url', __('Invalid URL.', 'oembed-api'), array('status' => 404));
     }
     // Todo: Perhaps just default to json if something invalid is provided.
     if (!in_array($request['format'], array('json', 'xml'))) {
         return new WP_Error('oembed_invalid_format', __('Invalid format.', 'oembed-api'), array('status' => 501));
     }
     return rest_ensure_response(get_oembed_response_data($post_id, $request['maxwidth']));
 }
 /**
  * Handle the whole request and print the response.
  *
  * @param array $request The request arguments.
  * @return string The oEmbed API response.
  */
 public function dispatch($request)
 {
     $post_id = url_to_postid($request['url']);
     /**
      * Filter the determined post id.
      *
      * @param int    $post_id The post ID.
      * @param string $url     The requestd URL.
      */
     $post_id = apply_filters('oembed_request_post_id', $post_id, $request['url']);
     $data = get_oembed_response_data($post_id, $request['maxwidth']);
     if (false === $data) {
         status_header(404);
         return __('Invalid URL.', 'oembed-api');
     }
     if ('json' === $request['format']) {
         return $this->json_response($data, $request);
     }
     return $this->xml_response($data);
 }
 /**
  * Handle the whole request and print the response.
  *
  * @param array $request The request arguments.
  * @return string The oEmbed API response.
  */
 public function dispatch($request)
 {
     if (!in_array($request['format'], array('json', 'xml'))) {
         status_header(501);
         return 'Invalid format';
     }
     $post_id = url_to_postid($request['url']);
     /**
      * Filter the determined post id.
      *
      * @param int    $post_id The post ID.
      * @param string $url     The requestd URL.
      */
     $post_id = apply_filters('rest_oembed_request_post_id', $post_id, $request['url']);
     if (0 === $post_id) {
         status_header(404);
         return 'Not Found';
     }
     $data = get_oembed_response_data($post_id, $request['maxwidth']);
     if ('json' === $request['format']) {
         return $this->json_response($data, $request);
     }
     return $this->xml_response($data);
 }
Beispiel #6
0
 /**
  * Test oEmbed response data with attachments
  */
 function test_get_oembed_response_data_attachment()
 {
     $parent = $this->factory->post->create();
     $file = DIR_TESTDATA . '/images/canola.jpg';
     $post = $this->factory->attachment->create_object($file, $parent, array('post_mime_type' => 'image/jpeg'));
     $data = get_oembed_response_data($post, 400);
     $this->assertArrayHasKey('thumbnail_url', $data);
     $this->assertArrayHasKey('thumbnail_width', $data);
     $this->assertArrayHasKey('thumbnail_height', $data);
     $this->assertTrue(400 >= $data['thumbnail_width']);
 }
Beispiel #7
0
/**
 * Filters the oEmbed result before any HTTP requests are made.
 *
 * If the URL belongs to the current site, the result is fetched directly instead of
 * going through the oEmbed discovery process.
 *
 * @since 4.5.3
 *
 * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.
 * @param string      $url    The URL that should be inspected for discovery `<link>` tags.
 * @param array       $args   oEmbed remote get arguments.
 * @return null|string The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
 *                     Null if the URL does not belong to the current site.
 */
function wp_filter_pre_oembed_result($result, $url, $args)
{
    $post_id = url_to_postid($url);
    /** This filter is documented in wp-includes/class-wp-oembed-controller.php */
    $post_id = apply_filters('oembed_request_post_id', $post_id, $url);
    if (!$post_id) {
        return $result;
    }
    $width = isset($args['width']) ? $args['width'] : 0;
    $data = get_oembed_response_data($post_id, $width);
    $data = _wp_oembed_get_object()->data2html((object) $data, $url);
    if (!$data) {
        return $result;
    }
    return $data;
}