Make WordPress Core

Ticket #35567: 35567.4.diff

File 35567.4.diff, 9.6 KB (added by swissspidy, 9 years ago)
  • src/wp-includes/embed.php

    diff --git src/wp-includes/embed.php src/wp-includes/embed.php
    index 8c79cc8..304bc3d 100644
    function wp_oembed_register_route() { 
    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 supports it.
    344345 */
    345346function wp_oembed_add_discovery_links() {
    346347        $output = '';
    347348
    348         if ( is_singular() ) {
     349        if ( is_singular() && is_post_embeddable() ) {
    349350                $output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
    350351
    351352                if ( class_exists( 'SimpleXMLElement' ) ) {
    JS; 
    510511 * Retrieves the oEmbed response data for a given post.
    511512 *
    512513 * @since 4.4.0
     514 * @since x.x.x Output was adjusted to only embed if the post type supports it.
    513515 *
    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.
     516 * @param WP_Post|int  $post  Post object or ID.
     517 * @param int          $width The requested width.
     518 * @return array|false Response data on success, false if post doesn't exist, or if post is not embeddable.
    517519 */
    518520function get_oembed_response_data( $post, $width ) {
    519521        $post = get_post( $post );
    function get_oembed_response_data( $post, $width ) { 
    526528                return false;
    527529        }
    528530
     531        if ( ! is_post_embeddable( $post ) ) {
     532                return false;
     533        }
     534
    529535        /**
    530536         * Filter the allowed minimum and maximum widths for the oEmbed response.
    531537         *
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index be08ae3..74ae1e2 100644
    function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) 
    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.
    function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) 
    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
    function register_post_type( $post_type, $args = array() ) { 
    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,
    function register_post_type( $post_type, $args = array() ) { 
    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 );
    function is_post_type_viewable( $post_type ) { 
    17091718}
    17101719
    17111720/**
     1721 * Determines whether a post is considered "embeddable".
     1722 *
     1723 * The 'is_embeddable' value of the post's type will be evaluated.
     1724 *
     1725 * @since x.x.x
     1726 *
     1727 * @param int|WP_Post|null $post Optional. Post ID or `WP_Post` object. Defaults to global $post.
     1728 * @return bool Whether the post should be considered embeddable.
     1729 */
     1730function is_post_embeddable( $post = null ) {
     1731        $post = get_post( $post );
     1732
     1733        if ( ! $post ) {
     1734                return false;
     1735        }
     1736
     1737        $is_embeddable = false;
     1738
     1739        if ( $post_type = get_post_type_object( $post->post_type ) ) {
     1740                $is_embeddable = $post_type->is_embeddable;
     1741        }
     1742
     1743        /**
     1744         * Filter whether a post is embeddable.
     1745         *
     1746         * @since x.x.x
     1747         *
     1748         * @param bool    $is_embeddable Whether the post is embeddable.
     1749         * @param WP_Post $post          Post object.
     1750         */
     1751        return apply_filters( 'is_post_embeddable', $is_embeddable, $post );
     1752}
     1753
     1754/**
    17121755 * Retrieve list of latest posts or posts matching criteria.
    17131756 *
    17141757 * The defaults are as follows:
  • tests/phpunit/tests/oembed/discovery.php

    diff --git tests/phpunit/tests/oembed/discovery.php tests/phpunit/tests/oembed/discovery.php
    index d3fbab8..e43c2eb 100644
    class Tests_oEmbed_Discovery extends WP_UnitTestCase { 
    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

    diff --git tests/phpunit/tests/oembed/getResponseData.php tests/phpunit/tests/oembed/getResponseData.php
    index 4a5c4f7..0ad9438 100644
    class Tests_oEmbed_Response_Data extends WP_UnitTestCase { 
    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}
  • tests/phpunit/tests/oembed/template.php

    diff --git tests/phpunit/tests/oembed/template.php tests/phpunit/tests/oembed/template.php
    index 21fd485..c3b6116 100644
    class Tests_Embed_Template extends WP_UnitTestCase { 
    301301                $this->assertNotContains( '&', file_get_contents( $file ) );
    302302        }
    303303
     304        /**
     305         * @ticket 35567
     306         */
     307        public function test_is_embeddable_post_non_existent_post() {
     308                $this->assertFalse( is_post_embeddable( 0 ) );
     309        }
     310
     311        /**
     312         * @ticket 35567
     313         */
     314        public function test_is_embeddable_post_non_existent_post_type() {
     315                $post = self::factory()->post->create_and_get( array(
     316                        'post_type' => rand_str(),
     317                ) );
     318
     319                $this->assertFalse( is_post_embeddable( $post ) );
     320        }
     321
     322        /**
     323         * @ticket 35567
     324         */
     325        public function test_is_embeddable_post_should_return_false_for_non_embeddable_post_type() {
     326                register_post_type( 'not_emebeddable', array( 'is_embeddable' => false ) );
     327
     328                $post = self::factory()->post->create_and_get( array(
     329                        'post_type' => 'not_embeddable',
     330                ) );
     331
     332                $this->assertFalse( is_post_embeddable( $post ) );
     333        }
     334
     335        /**
     336         * @ticket 35567
     337         */
     338        public function test_is_embeddable_post_should_return_true_for_embeddable_post_type() {
     339                register_post_type( 'not_emebeddable', array( 'is_embeddable' => true ) );
     340
     341                $post = self::factory()->post->create_and_get( array(
     342                        'post_type' => 'not_embeddable',
     343                ) );
     344
     345                $this->assertFalse( is_post_embeddable( $post ) );
     346        }
     347
     348        /**
     349         * @ticket 35567
     350         */
     351        public function test_is_embeddable_post_filtered() {
     352                register_post_type( 'not_emebeddable', array( 'is_embeddable' => false ) );
     353
     354                $post = self::factory()->post->create_and_get( array(
     355                        'post_type' => 'not_embeddable',
     356                ) );
     357
     358                add_filter( 'is_post_embeddable', '__return_true' );
     359                $is_embeddable = is_post_embeddable( $post );
     360                remove_filter( 'is_post_embeddable', '__return_true' );
     361
     362                $this->assertTrue( $is_embeddable );
     363        }
    304364}
  • tests/phpunit/tests/post/types.php

    diff --git tests/phpunit/tests/post/types.php tests/phpunit/tests/post/types.php
    index 9cf44c8..da26008 100644
    class Tests_Post_Types extends WP_UnitTestCase { 
    428428        public function test_get_post_types_by_support_non_existant_feature() {
    429429                $this->assertEqualSets( array(), get_post_types_by_support( 'somefeature' ) );
    430430        }
     431
     432        /**
     433         * @group oembed
     434         * @ticket 35567
     435         */
     436        public function test_register_post_type_is_embeddable_defaults_to_public_argument() {
     437                $post_type = register_post_type( rand_str( 10) );
     438                $this->assertFalse( $post_type->is_embeddable );
     439
     440                $post_type = register_post_type( rand_str( 10 ), array( 'public' => true ) );
     441                $this->assertTrue( $post_type->is_embeddable );
     442        }
     443
     444        /**
     445         * @group oembed
     446         * @ticket 35567
     447         */
     448        public function test_register_post_type_override_is_embeddable() {
     449                $post_type = register_post_type( rand_str( 10 ), array( 'is_embeddable' => true ) );
     450                $this->assertTrue( $post_type->is_embeddable );
     451
     452                $post_type = register_post_type( rand_str( 10 ), array( 'public' => true, 'is_embeddable' => false ) );
     453                $this->assertFalse( $post_type->is_embeddable );
     454        }
    431455}