Make WordPress Core

Changeset 59700


Ignore:
Timestamp:
01/24/2025 01:30:27 PM (3 months ago)
Author:
swissspidy
Message:

Posts, Post Types: Embeds: Add new embeddable argument to post types.

This new argument, which defaults to the value of public, can be used to determine whether a post can be embedded using oEmbed. A new is_post_embeddable() function is added to easily check this.

Props pampfelimetten, swissspidy, bradleyt, DrewAPicture, gadelhas, mukesh27.
Fixes #35567.

Location:
trunk
Files:
6 edited

Legend:

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

    r59161 r59700  
    113113     */
    114114    public $publicly_queryable = null;
     115
     116    /**
     117     * Whether this post type is embeddable.
     118     *
     119     * Default is the value of $public.
     120     *
     121     * @since 6.8.0
     122     * @var bool $embeddable
     123     */
     124    public $embeddable = null;
    115125
    116126    /**
     
    522532            'exclude_from_search'             => null,
    523533            'publicly_queryable'              => null,
     534            'embeddable'                      => null,
    524535            'show_ui'                         => null,
    525536            'show_in_menu'                    => null,
     
    564575        if ( null === $args['show_ui'] ) {
    565576            $args['show_ui'] = $args['public'];
     577        }
     578
     579        // If not set, default to the setting for 'public'.
     580        if ( null === $args['embeddable'] ) {
     581            $args['embeddable'] = $args['public'];
    566582        }
    567583
  • trunk/src/wp-includes/embed.php

    r59493 r59700  
    332332 *
    333333 * @since 4.4.0
     334 * @since 6.8.0 Output was adjusted to only embed if the post supports it.
    334335 */
    335336function wp_oembed_add_discovery_links() {
    336337    $output = '';
    337338
    338     if ( is_singular() ) {
     339    if ( is_singular() && is_post_embeddable() ) {
    339340        $output .= '<link rel="alternate" title="' . _x( 'oEmbed (JSON)', 'oEmbed resource link name' ) . '" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";
    340341
     
    539540 *
    540541 * @since 4.4.0
     542 * @since 6.8.0 Output was adjusted to only embed if the post type supports it.
    541543 *
    542544 * @param WP_Post|int $post  Post ID or post object.
    543545 * @param int         $width The requested width.
    544  * @return array|false Response data on success, false if post doesn't exist
    545  *                     or is not publicly viewable.
     546 * @return array|false Response data on success, false if post doesn't exist,
     547 *                     is not publicly viewable or post type is not embeddable.
    546548 */
    547549function get_oembed_response_data( $post, $width ) {
     
    554556
    555557    if ( ! is_post_publicly_viewable( $post ) ) {
     558        return false;
     559    }
     560
     561    if ( ! is_post_embeddable( $post ) ) {
    556562        return false;
    557563    }
  • trunk/src/wp-includes/post.php

    r59657 r59700  
    24772477
    24782478/**
     2479 * Determines whether a post is embeddable.
     2480 *
     2481 * @since 6.8.0
     2482 *
     2483 * @param int|WP_Post|null $post Optional. Post ID or `WP_Post` object. Defaults to global $post.
     2484 * @return bool Whether the post should be considered embeddable.
     2485 */
     2486function is_post_embeddable( $post = null ) {
     2487    $post = get_post( $post );
     2488
     2489    if ( ! $post ) {
     2490        return false;
     2491    }
     2492
     2493    $post_type = get_post_type_object( $post->post_type );
     2494
     2495    if ( ! $post_type ) {
     2496        return false;
     2497    }
     2498
     2499    $is_embeddable = $post_type->embeddable;
     2500
     2501    /**
     2502     * Filter whether a post is embeddable.
     2503     *
     2504     * @since 6.8.0
     2505     *
     2506     * @param bool    $is_embeddable Whether the post is embeddable.
     2507     * @param WP_Post $post          Post object.
     2508     */
     2509    return apply_filters( 'is_post_embeddable', $is_embeddable, $post );
     2510}
     2511
     2512/**
    24792513 * Retrieves an array of the latest posts, or posts matching the given criteria.
    24802514 *
  • trunk/tests/phpunit/tests/oembed/discovery.php

    r58286 r59700  
    8686        $this->assertSame( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
    8787    }
     88
     89    /**
     90     * @ticket 35567
     91     */
     92    public function test_wp_oembed_add_discovery_links_non_embeddable_post_type_output_should_be_empty() {
     93        register_post_type( 'not_embeddable', array( 'embeddable' => false ) );
     94
     95        $post = self::factory()->post->create_and_get(
     96            array(
     97                'post_type' => 'not_embeddable',
     98            )
     99        );
     100
     101        $this->assertFalse( get_oembed_response_data( $post, 100 ) );
     102    }
    88103}
  • trunk/tests/phpunit/tests/oembed/template.php

    r58143 r59700  
    344344        $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) );
    345345    }
     346
     347    /**
     348     * @ticket 35567
     349     */
     350    public function test_is_embeddable_post_non_existent_post() {
     351        $this->assertFalse( is_post_embeddable( 99999 ) );
     352    }
     353
     354    /**
     355     * @ticket 35567
     356     */
     357    public function test_is_embeddable_post_should_return_false_for_non_embeddable_post_type() {
     358        register_post_type( 'not_embeddable', array( 'embeddable' => false ) );
     359
     360        $post = self::factory()->post->create_and_get(
     361            array(
     362                'post_type' => 'not_embeddable',
     363            )
     364        );
     365
     366        $this->assertFalse( is_post_embeddable( $post ) );
     367    }
     368
     369    /**
     370     * @ticket 35567
     371     */
     372    public function test_is_embeddable_post_should_return_true_for_embeddable_post_type() {
     373        register_post_type( 'embeddable', array( 'embeddable' => true ) );
     374
     375        $post = self::factory()->post->create_and_get(
     376            array(
     377                'post_type' => 'embeddable',
     378            )
     379        );
     380
     381        $this->assertTrue( is_post_embeddable( $post ) );
     382    }
     383
     384    /**
     385     * @ticket 35567
     386     */
     387    public function test_is_embeddable_post_filtered() {
     388        register_post_type( 'not_embeddable', array( 'embeddable' => false ) );
     389
     390        $post = self::factory()->post->create_and_get(
     391            array(
     392                'post_type' => 'not_embeddable',
     393            )
     394        );
     395
     396        add_filter( 'is_post_embeddable', '__return_true' );
     397        $embeddable = is_post_embeddable( $post );
     398        remove_filter( 'is_post_embeddable', '__return_true' );
     399
     400        $this->assertTrue( $embeddable );
     401    }
    346402}
  • trunk/tests/phpunit/tests/post/types.php

    r58211 r59700  
    647647        $this->assertTrue( post_type_supports( 'foo', 'autosave' ), 'Post type should still support autosaves.' );
    648648    }
     649
     650    /**
     651     * @group oembed
     652     * @ticket 35567
     653     */
     654    public function test_register_post_type_is_embeddable_should_default_to_value_of_public() {
     655        $post_type = register_post_type( $this->post_type );
     656        $this->assertFalse( $post_type->embeddable, 'Non-public post type should not be embeddable by default' );
     657
     658        $post_type = register_post_type( $this->post_type, array( 'public' => true ) );
     659        $this->assertTrue( $post_type->embeddable, 'Public post type should be embeddable by default' );
     660    }
     661
     662    /**
     663     * @group oembed
     664     * @ticket 35567
     665     */
     666    public function test_register_post_type_override_is_embeddable() {
     667        $post_type = register_post_type( $this->post_type, array( 'embeddable' => true ) );
     668        $this->assertTrue( $post_type->embeddable, 'Post type should be embeddable even though it is not public' );
     669
     670        $post_type = register_post_type(
     671            $this->post_type,
     672            array(
     673                'public'     => true,
     674                'embeddable' => false,
     675            )
     676        );
     677        $this->assertFalse( $post_type->embeddable, 'Post type should not be embeddable even though it is public' );
     678    }
    649679}
Note: See TracChangeset for help on using the changeset viewer.