Make WordPress Core

Ticket #35567: 35567.3.diff

File 35567.3.diff, 6.8 KB (added by DrewAPicture, 9 years ago)

cleanup + 2 tests

  • src/wp-includes/embed.php

     
    341341 * Adds oEmbed discovery links in the website <head>.
    342342 *
    343343 * @since 4.4.0
     344 * @since x.x.x Output was adjusted to only embed if the post type supports it.
    344345 */
    345346function wp_oembed_add_discovery_links() {
     347        $post_type = get_post_type();
    346348        $output = '';
    347349
    348         if ( is_singular() ) {
    349                 $output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
     350        if ( is_singular() && $post_type ) {
     351                if ( is_post_type_embeddable( $post_type ) ) {
     352                        $output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
    350353
    351                 if ( class_exists( 'SimpleXMLElement' ) ) {
    352                         $output .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
     354                        if ( class_exists( 'SimpleXMLElement' ) ) {
     355                                $output .= '<link rel="alternate" type="text/xml+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink(), 'xml' ) ) . '" />' . "\n";
     356                        }
    353357                }
    354358        }
    355359
     
    357361         * Filter the oEmbed discovery links HTML.
    358362         *
    359363         * @since 4.4.0
     364         * @since x.x.x The `$post_type` parameter was added.
    360365         *
    361          * @param string $output HTML of the discovery links.
     366         * @param string $output    HTML of the discovery links.
     367         * @param string $post_type Post type slug.
    362368         */
    363         echo apply_filters( 'oembed_discovery_links', $output );
     369        echo apply_filters( 'oembed_discovery_links', $output, $post_type );
    364370}
    365371
    366372/**
     
    510516 * Retrieves the oEmbed response data for a given post.
    511517 *
    512518 * @since 4.4.0
     519 * @since x.x.x Output was adjusted to only embed if the post type supports it.
    513520 *
    514  * @param WP_Post|int $post  Post object or ID.
    515  * @param int         $width The requested width.
    516  * @return array|false Response data on success, false if post doesn't exist.
     521 * @param WP_Post|int  $post  Post object or ID.
     522 * @param int          $width The requested width.
     523 * @return array|false Response data on success, false if post doesn't exist, or if post is not embeddable.
    517524 */
    518525function get_oembed_response_data( $post, $width ) {
    519526        $post = get_post( $post );
     
    526533                return false;
    527534        }
    528535
     536        if ( ! is_post_type_embeddable( get_post_type( $post ) ) ) {
     537                return false;
     538        }
     539
    529540        /**
    530541         * Filter the allowed minimum and maximum widths for the oEmbed response.
    531542         *
  • src/wp-includes/post.php

     
    890890 * @since 2.9.0
    891891 * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen.
    892892 * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing screen and post editing screen.
     893 * @since x.x.x Introduced the `is_embeddable` argument.
    893894 *
    894895 * @global array      $wp_post_types List of post types.
    895896 * @global WP_Rewrite $wp_rewrite    Used for default feeds.
     
    962963 *     @type bool|string $has_archive          Whether there should be post type archives, or if a string, the
    963964 *                                             archive slug to use. Will generate the proper rewrite rules if
    964965 *                                             $rewrite is enabled. Default false.
     966 *     @type bool        $is_embeddable        Whether this post type should be embeddable through oEmbed.
     967 *                                             Default is value of `$public`.
    965968 *     @type bool|array  $rewrite              {
    966969 *         Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
    967970 *         Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
     
    10391042                'register_meta_box_cb' => null,
    10401043                'taxonomies'           => array(),
    10411044                'has_archive'          => false,
     1045                'is_embeddable'        => null,
    10421046                'rewrite'              => true,
    10431047                'query_var'            => true,
    10441048                'can_export'           => true,
     
    11631167                add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
    11641168        }
    11651169
     1170        // If not set, default to the setting for public.
     1171        if ( null === $args->is_embeddable ) {
     1172                $args->is_embeddable = $args->public;
     1173        }
     1174
    11661175        // Register the post type meta box if a custom callback was specified.
    11671176        if ( $args->register_meta_box_cb )
    11681177                add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
     
    17091718}
    17101719
    17111720/**
     1721 * Determines whether a post type is considered "embeddable".
     1722 *
     1723 * For all post types the 'is_embeddable' value will be evaluated.
     1724 *
     1725 * @param string $post_type Post type name.
     1726 * @return bool Whether the post type should be considered embeddable. False otherwise.
     1727 */
     1728function is_post_type_embeddable( $post_type ) {
     1729        if ( ! post_type_exists( $post_type ) ) {
     1730                return false;
     1731        }
     1732
     1733        if ( $post_type = get_post_type_object( $post_type ) ) {
     1734                return $post_type->is_embeddable;
     1735        }
     1736        return false;
     1737}
     1738
     1739/**
    17121740 * Retrieve list of latest posts or posts matching criteria.
    17131741 *
    17141742 * The defaults are as follows:
  • tests/phpunit/tests/oembed/discovery.php

     
    7171
    7272                $this->assertEquals( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
    7373        }
     74
     75        /**
     76         * @ticket 35567
     77         */
     78        function test_wp_oembed_add_discovery_links_non_embeddable_post_type_output_should_be_empty() {
     79                registeR_post_type( 'not_embeddable', array( 'is_embeddable' => false ) );
     80
     81                $post = $this->factory()->post->create_and_get( array(
     82                        'post_type' => 'not_embeddable'
     83                ) );
     84
     85                $this->go_to( get_permalink( $post->ID ) );
     86
     87                $this->assertEmpty( wp_oembed_add_discovery_links() );
     88        }
    7489}
  • tests/phpunit/tests/oembed/getResponseData.php

     
    167167                $this->assertArrayHasKey( 'thumbnail_height', $data );
    168168                $this->assertTrue( 400 >= $data['thumbnail_width'] );
    169169        }
     170
     171        /**
     172         * @ticket 35567
     173         */
     174        function test_get_oembed_response_data_non_embeddable_post_type_should_return_false() {
     175                register_post_type( 'not_emebeddable', array( 'is_embeddable' => false ) );
     176
     177                $post = self::factory()->post->create_and_get( array(
     178                        'post_type' => 'not_embeddable'
     179                ) );
     180
     181                $this->assertFalse( get_oembed_response_data( $post, 100 ) );
     182        }
    170183}