WordPress.org

Make WordPress Core

Changeset 48135


Ignore:
Timestamp:
06/23/2020 06:06:11 AM (4 months ago)
Author:
whyisjake
Message:

Embeds: Ensure that classic embed support works in the block editor.

See https://github.com/WordPress/gutenberg/pull/6345

Fixes #45447.

Props swisspidy, pento, audrasjb, aduth, jrchamp, thrijith, TimothyBlynJacobs, whyisjake.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-embed.php

    r47808 r48135  
    127127
    128128    /**
    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
    134135     *
    135136     * @param array $attr {
     
    140141     * }
    141142     * @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 ) {
    159146        $rawattr = $attr;
    160147        $attr    = wp_parse_args( $attr, wp_embed_defaults( $url ) );
    161148
    162         $this->last_attr = $attr;
    163 
    164         // KSES converts & into & and we need to undo this.
    165         // See https://core.trac.wordpress.org/ticket/11311
    166         $url = str_replace( '&', '&', $url );
    167 
    168         // Look for known internal handlers.
    169149        ksort( $this->handlers );
    170150        foreach ( $this->handlers as $priority => $handlers ) {
     
    188168                }
    189169            }
     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;
    190219        }
    191220
  • trunk/src/wp-includes/class-wp-oembed-controller.php

    r48121 r48135  
    194194
    195195        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
    196218            return new WP_Error( 'oembed_invalid_url', get_status_header_desc( 404 ), array( 'status' => 404 ) );
    197219        }
  • trunk/tests/phpunit/tests/oembed/controller.php

    r47615 r48135  
    9898
    9999        // 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 ) ) {
    101101            return array(
    102102                'response' => array(
     
    610610    }
    611611
     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
    612637    public function test_proxy_with_invalid_oembed_provider_no_discovery() {
    613638        wp_set_current_user( self::$editor );
    614639
    615         // If discover is false for an unkown provider, no discovery request should take place.
     640        // If discover is false for an unknown provider, no discovery request should take place.
    616641        $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    617642        $request->set_param( 'url', self::INVALID_OEMBED_URL );
    618         $request->set_param( 'discover', 0 );
     643        $request->set_param( 'discover', false );
    619644        $response = rest_get_server()->dispatch( $request );
    620645        $this->assertEquals( 404, $response->get_status() );
     
    625650        wp_set_current_user( self::$editor );
    626651
    627         // For an unkown provider, a discovery request should happen.
     652        // For an unknown provider, a discovery request should happen.
    628653        $request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
    629654        $request->set_param( 'url', self::INVALID_OEMBED_URL );
Note: See TracChangeset for help on using the changeset viewer.