Changeset 35436 for trunk/src/wp-includes/class-wp-oembed-controller.php
- Timestamp:
- 10/29/2015 10:50:13 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-oembed-controller.php
r35408 r35436 11 11 * oEmbed API endpoint controller. 12 12 * 13 * Parses the oEmbed API requests and delivers14 * XML and JSON responses.13 * Registers the API route and delivers the response data. 14 * The output format (XML or JSON) is handled by the REST API. 15 15 * 16 16 * @since 4.4.0 … … 18 18 final class WP_oEmbed_Controller { 19 19 /** 20 * Hook into the query parsing to detect oEmbed requests. 21 * 22 * If an oEmbed request is made, trigger the output. 20 * Register the oEmbed REST API route. 23 21 * 24 22 * @since 4.4.0 25 *26 * @param WP_Query $wp_query The WP_Query instance (passed by reference).27 23 */ 28 public function parse_query( $wp_query ) { 29 if ( false === $wp_query->get( 'oembed', false ) ) { 30 return; 31 } 32 33 if ( false === $wp_query->get( 'url', false ) ) { 34 status_header( 400 ); 35 return get_status_header_desc( 400 ); 36 exit; 37 } 38 39 $url = esc_url_raw( get_query_var( 'url' ) ); 40 41 $format = wp_oembed_ensure_format( get_query_var( 'format' ) ); 42 24 public function register_routes() { 43 25 /** 44 26 * Filter the maxwidth oEmbed parameter. … … 49 31 */ 50 32 $maxwidth = apply_filters( 'oembed_default_width', 600 ); 51 $maxwidth = absint( get_query_var( 'maxwidth', $maxwidth ) );52 33 53 $callback = get_query_var( '_jsonp', false ); 54 55 $request = array( 56 'url' => $url, 57 'format' => $format, 58 'maxwidth' => $maxwidth, 59 'callback' => $callback, 60 ); 61 62 echo $this->dispatch( $request ); 63 exit; 34 register_rest_route( 'oembed/1.0/', '/embed', array( 35 array( 36 'methods' => WP_REST_Server::READABLE, 37 'callback' => array( $this, 'get_item' ), 38 'args' => array( 39 'url' => array( 40 'required' => true, 41 'sanitize_callback' => 'esc_url_raw', 42 ), 43 'format' => array( 44 'default' => 'json', 45 'sanitize_callback' => 'wp_oembed_ensure_format', 46 ), 47 'maxwidth' => array( 48 'default' => $maxwidth, 49 'sanitize_callback' => 'absint', 50 ), 51 ), 52 ), 53 ) ); 64 54 } 65 55 66 56 /** 67 * Handle the whole request and print the response. 57 * Callback for the API endpoint. 58 * 59 * Returns the JSON object for the post. 68 60 * 69 61 * @since 4.4.0 70 62 * 71 * @param array $request The request arguments.72 * @return string The oEmbed API response.63 * @param WP_REST_Request $request Full data about the request. 64 * @return WP_Error|array oEmbed response data or WP_Error on failure. 73 65 */ 74 public function dispatch( $request ) {66 public function get_item( $request ) { 75 67 $post_id = url_to_postid( $request['url'] ); 76 68 … … 87 79 $data = get_oembed_response_data( $post_id, $request['maxwidth'] ); 88 80 89 if ( false === $data ) { 90 status_header( 404 ); 91 return get_status_header_desc( 404 ); 81 if ( ! $data ) { 82 return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) ); 92 83 } 93 84 94 if ( 'json' === $request['format'] ) { 95 return $this->json_response( $data, $request ); 96 } 97 98 return $this->xml_response( $data ); 99 } 100 101 /** 102 * Print the oEmbed JSON response. 103 * 104 * @since 4.4.0 105 * 106 * @param array $data The oEmbed response data. 107 * @param array $request The request arguments. 108 * @return string The JSON response data. 109 */ 110 public function json_response( $data, $request ) { 111 if ( ! is_string( $request['callback'] ) || preg_match( '/[^\w\.]/', $request['callback'] ) ) { 112 $request['callback'] = false; 113 } 114 115 $result = wp_json_encode( $data ); 116 117 // Bail if the result couldn't be JSON encoded. 118 if ( ! $result || ! is_array( $data ) || empty( $data ) ) { 119 status_header( 501 ); 120 return get_status_header_desc( 501 ); 121 } 122 123 if ( ! headers_sent() ) { 124 $content_type = $request['callback'] ? 'application/javascript' : 'application/json'; 125 header( 'Content-Type: ' . $content_type . '; charset=' . get_option( 'blog_charset' ) ); 126 header( 'X-Content-Type-Options: nosniff' ); 127 } 128 129 if ( $request['callback'] ) { 130 return '/**/' . $request['callback'] . '(' . $result . ')'; 131 } 132 133 return $result; 134 } 135 136 /** 137 * Print the oEmbed XML response. 138 * 139 * @since 4.4.0 140 * 141 * @param array $data The oEmbed response data. 142 * @return string The XML response data. 143 */ 144 public function xml_response( $data ) { 145 if ( ! class_exists( 'SimpleXMLElement' ) ) { 146 status_header( 501 ); 147 return get_status_header_desc( 501 ); 148 } 149 150 $result = _oembed_create_xml( $data ); 151 152 // Bail if there's no XML. 153 if ( ! $result ) { 154 status_header( 501 ); 155 return get_status_header_desc( 501 ); 156 } 157 158 if ( ! headers_sent() ) { 159 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ) ); 160 } 161 162 return $result; 85 return $data; 163 86 } 164 87 }
Note: See TracChangeset
for help on using the changeset viewer.