Changeset 48135
- Timestamp:
- 06/23/2020 06:06:11 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-embed.php
r47808 r48135 127 127 128 128 /** 129 * The do_shortcode() callback function. 130 * 131 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of 132 * the registered embed handlers. If none of the regex matches and it's enabled, then the URL 133 * will be given to the WP_oEmbed class. 129 * Returns embed HTML for a given URL from embed handlers. 130 * 131 * Attempts to convert a URL into embed HTML by checking the URL 132 * against the regex of the registered embed handlers. 133 * 134 * @since 5.5.0 134 135 * 135 136 * @param array $attr { … … 140 141 * } 141 142 * @param string $url The URL attempting to be embedded. 142 * @return string|false The embed HTML on success, otherwise the original URL. 143 * `->maybe_make_link()` can return false on failure. 144 */ 145 public function shortcode( $attr, $url = '' ) { 146 $post = get_post(); 147 148 if ( empty( $url ) && ! empty( $attr['src'] ) ) { 149 $url = $attr['src']; 150 } 151 152 $this->last_url = $url; 153 154 if ( empty( $url ) ) { 155 $this->last_attr = $attr; 156 return ''; 157 } 158 143 * @return string|false The embed HTML on success, false otherwise. 144 */ 145 public function get_embed_handler_html( $attr, $url ) { 159 146 $rawattr = $attr; 160 147 $attr = wp_parse_args( $attr, wp_embed_defaults( $url ) ); 161 148 162 $this->last_attr = $attr;163 164 // KSES converts & into & and we need to undo this.165 // See https://core.trac.wordpress.org/ticket/11311166 $url = str_replace( '&', '&', $url );167 168 // Look for known internal handlers.169 149 ksort( $this->handlers ); 170 150 foreach ( $this->handlers as $priority => $handlers ) { … … 188 168 } 189 169 } 170 } 171 172 return false; 173 } 174 175 /** 176 * The do_shortcode() callback function. 177 * 178 * Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of 179 * the registered embed handlers. If none of the regex matches and it's enabled, then the URL 180 * will be given to the WP_oEmbed class. 181 * 182 * @param array $attr { 183 * Shortcode attributes. Optional. 184 * 185 * @type int $width Width of the embed in pixels. 186 * @type int $height Height of the embed in pixels. 187 * } 188 * @param string $url The URL attempting to be embedded. 189 * @return string|false The embed HTML on success, otherwise the original URL. 190 * `->maybe_make_link()` can return false on failure. 191 */ 192 public function shortcode( $attr, $url = '' ) { 193 $post = get_post(); 194 195 if ( empty( $url ) && ! empty( $attr['src'] ) ) { 196 $url = $attr['src']; 197 } 198 199 $this->last_url = $url; 200 201 if ( empty( $url ) ) { 202 $this->last_attr = $attr; 203 return ''; 204 } 205 206 $rawattr = $attr; 207 $attr = wp_parse_args( $attr, wp_embed_defaults( $url ) ); 208 209 $this->last_attr = $attr; 210 211 // KSES converts & into & and we need to undo this. 212 // See https://core.trac.wordpress.org/ticket/11311 213 $url = str_replace( '&', '&', $url ); 214 215 // Look for known internal handlers. 216 $embed_handler_html = $this->get_embed_handler_html( $rawattr, $url ); 217 if ( false !== $embed_handler_html ) { 218 return $embed_handler_html; 190 219 } 191 220 -
trunk/src/wp-includes/class-wp-oembed-controller.php
r48121 r48135 194 194 195 195 if ( false === $data ) { 196 // Try using a classic embed, instead. 197 global $wp_embed; 198 199 /* @var WP_Embed $wp_embed */ 200 $html = $wp_embed->get_embed_handler_html( $args, $url ); 201 202 if ( $html ) { 203 global $wp_scripts; 204 // Check if any scripts were enqueued by the shortcode, and include them in the response. 205 $enqueued_scripts = array(); 206 207 foreach ( $wp_scripts->queue as $script ) { 208 $enqueued_scripts[] = $wp_scripts->registered[ $script ]->src; 209 } 210 211 return (object) array( 212 'provider_name' => __( 'Embed Handler' ), 213 'html' => $html, 214 'scripts' => $enqueued_scripts, 215 ); 216 } 217 196 218 return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) ); 197 219 } -
trunk/tests/phpunit/tests/oembed/controller.php
r47615 r48135 98 98 99 99 // Mock request to YouTube Embed. 100 if ( ! empty( $query_params['url'] ) && false !== strpos( $query_params['url'], self::YOUTUBE_VIDEO_ID ) ) {100 if ( ! empty( $query_params['url'] ) && false !== strpos( $query_params['url'], '?v=' . self::YOUTUBE_VIDEO_ID ) ) { 101 101 return array( 102 102 'response' => array( … … 610 610 } 611 611 612 /** 613 * @ticket 45447 614 * 615 * @see wp_maybe_load_embeds() 616 */ 617 public function test_proxy_with_classic_embed_provider() { 618 wp_set_current_user( self::$editor ); 619 $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' ); 620 $request->set_param( 'url', 'https://www.youtube.com/embed/' . self::YOUTUBE_VIDEO_ID ); 621 $request->set_param( 'maxwidth', 456 ); 622 $request->set_param( 'maxheight', 789 ); 623 $request->set_param( '_wpnonce', wp_create_nonce( 'wp_rest' ) ); 624 $response = rest_get_server()->dispatch( $request ); 625 $this->assertEquals( 200, $response->get_status() ); 626 $this->assertEquals( 2, $this->request_count ); 627 628 // Test data object. 629 $data = $response->get_data(); 630 631 $this->assertNotEmpty( $data ); 632 $this->assertInternalType( 'object', $data ); 633 $this->assertInternalType( 'string', $data->html ); 634 $this->assertInternalType( 'array', $data->scripts ); 635 } 636 612 637 public function test_proxy_with_invalid_oembed_provider_no_discovery() { 613 638 wp_set_current_user( self::$editor ); 614 639 615 // If discover is false for an unk own provider, no discovery request should take place.640 // If discover is false for an unknown provider, no discovery request should take place. 616 641 $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' ); 617 642 $request->set_param( 'url', self::INVALID_OEMBED_URL ); 618 $request->set_param( 'discover', 0);643 $request->set_param( 'discover', false ); 619 644 $response = rest_get_server()->dispatch( $request ); 620 645 $this->assertEquals( 404, $response->get_status() ); … … 625 650 wp_set_current_user( self::$editor ); 626 651 627 // For an unk own provider, a discovery request should happen.652 // For an unknown provider, a discovery request should happen. 628 653 $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' ); 629 654 $request->set_param( 'url', self::INVALID_OEMBED_URL );
Note: See TracChangeset
for help on using the changeset viewer.