Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_embeddable argument to post types. #5775

Closed
wants to merge 11 commits into from
6 changes: 6 additions & 0 deletions src/wp-includes/class-wp-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ public function set_props( $args ) {
'hierarchical' => false,
'exclude_from_search' => null,
'publicly_queryable' => null,
'is_embeddable' => null,
'show_ui' => null,
'show_in_menu' => null,
'show_in_nav_menus' => null,
Expand Down Expand Up @@ -565,6 +566,11 @@ public function set_props( $args ) {
$args['show_ui'] = $args['public'];
}

// If not set, default to the setting for 'public'.
if ( null === $args['is_embeddable'] ) {
$args['is_embeddable'] = $args['public'];
}

// If not set, default rest_namespace to wp/v2 if show_in_rest is true.
if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
$args['rest_namespace'] = 'wp/v2';
Expand Down
12 changes: 9 additions & 3 deletions src/wp-includes/embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,12 @@ function wp_oembed_register_route() {
* Adds oEmbed discovery links in the head element of the website.
*
* @since 4.4.0
* @since x.x.x Output was adjusted to only embed if the post supports it.
*/
function wp_oembed_add_discovery_links() {
$output = '';

if ( is_singular() ) {
if ( is_singular() && is_post_embeddable() ) {
$output .= '<link rel="alternate" type="application/json+oembed" href="' . esc_url( get_oembed_endpoint_url( get_permalink() ) ) . '" />' . "\n";

if ( class_exists( 'SimpleXMLElement' ) ) {
Expand Down Expand Up @@ -538,11 +539,12 @@ function get_post_embed_html( $width, $height, $post = null ) {
* Retrieves the oEmbed response data for a given post.
*
* @since 4.4.0
* @since x.x.x Output was adjusted to only embed if the post type supports it.
*
* @param WP_Post|int $post Post ID or post object.
* @param int $width The requested width.
* @return array|false Response data on success, false if post doesn't exist
* or is not publicly viewable.
* @return array|false Response data on success, false if post doesn't exist,
* is not publicly viewable or post type is not embeddable.
*/
function get_oembed_response_data( $post, $width ) {
$post = get_post( $post );
Expand All @@ -556,6 +558,10 @@ function get_oembed_response_data( $post, $width ) {
return false;
}

if ( ! is_post_embeddable( $post ) ) {
return false;
}

/**
* Filters the allowed minimum and maximum widths for the oEmbed response.
*
Expand Down
35 changes: 35 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,41 @@ function is_post_publicly_viewable( $post = null ) {
return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
}

/**
* Determines whether a post is "embeddable".
*
* The "is_embeddable" value of the post's type will be evaluated.
*
* @since x.x.x
*
* @param int|WP_Post|null $post Optional. Post ID or `WP_Post` object. Defaults to global $post.
* @return bool Whether the post should be considered embeddable.
*/
function is_post_embeddable( $post = null ) {
$post = get_post( $post );

if ( ! $post ) {
return false;
}

$is_embeddable = false;

$post_type = get_post_type_object( $post->post_type );
if ( $post_type ) {
$is_embeddable = $post_type->is_embeddable;
}

/**
* Filter whether a post is embeddable.
*
* @since x.x.x
*
* @param bool $is_embeddable Whether the post is embeddable.
* @param WP_Post $post Post object.
*/
return apply_filters( 'is_post_embeddable', $is_embeddable, $post );
}

/**
* Retrieves an array of the latest posts, or posts matching the given criteria.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/tests/oembed/discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ public function test_add_oembed_discovery_links_to_attachment() {

$this->assertSame( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
}

/**
* @ticket 35567
*/
public function test_wp_oembed_add_discovery_links_non_embeddable_post_type_output_should_be_empty() {
register_post_type( 'not_embeddable', array( 'is_embeddable' => false ) );

$post = self::factory()->post->create_and_get(
array(
'post_type' => 'not_embeddable',
)
);

$this->assertFalse( get_oembed_response_data( $post, 100 ) );
}
}
56 changes: 56 additions & 0 deletions tests/phpunit/tests/oembed/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,60 @@ public function test_wp_maybe_enqueue_oembed_host_js_without_wp_head_action() {
wp_maybe_enqueue_oembed_host_js( $post_embed );
$this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) );
}

/**
* @ticket 35567
*/
public function test_is_embeddable_post_non_existent_post() {
$this->assertFalse( is_post_embeddable( 0 ) );
}

/**
* @ticket 35567
*/
public function test_is_embeddable_post_should_return_false_for_non_embeddable_post_type() {
register_post_type( 'not_embeddable', array( 'is_embeddable' => false ) );

$post = self::factory()->post->create_and_get(
array(
'post_type' => 'not_embeddable',
)
);

$this->assertFalse( is_post_embeddable( $post ) );
}

/**
* @ticket 35567
*/
public function test_is_embeddable_post_should_return_true_for_embeddable_post_type() {
register_post_type( 'embeddable', array( 'is_embeddable' => true ) );

$post = self::factory()->post->create_and_get(
array(
'post_type' => 'embeddable',
)
);

$this->assertTrue( is_post_embeddable( $post ) );
}

/**
* @ticket 35567
*/
public function test_is_embeddable_post_filtered() {
register_post_type( 'not_embeddable', array( 'is_embeddable' => false ) );

$post = self::factory()->post->create_and_get(
array(
'post_type' => 'not_embeddable',
)
);

add_filter( 'is_post_embeddable', '__return_true' );
$embeddable = is_post_embeddable( $post );
remove_filter( 'is_post_embeddable', '__return_true' );

$this->assertTrue( $embeddable );
}
}
30 changes: 30 additions & 0 deletions tests/phpunit/tests/post/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,34 @@ public function test_get_post_types_by_support_excluding_features() {
public function test_get_post_types_by_support_non_existant_feature() {
$this->assertSameSets( array(), get_post_types_by_support( 'somefeature' ) );
}

/**
* @group oembed
* @ticket 35567
*/
public function test_register_post_type_is_embeddable_defaults_to_public_argument() {
$post_type = register_post_type( rand_str( 10 ) );
$this->assertFalse( $post_type->is_embeddable );

$post_type = register_post_type( rand_str( 10 ), array( 'public' => true ) );
$this->assertTrue( $post_type->is_embeddable );
}

/**
* @group oembed
* @ticket 35567
*/
public function test_register_post_type_override_is_embeddable() {
$post_type = register_post_type( rand_str( 10 ), array( 'is_embeddable' => true ) );
$this->assertTrue( $post_type->is_embeddable );

$post_type = register_post_type(
rand_str( 10 ),
array(
'public' => true,
'is_embeddable' => false,
)
);
$this->assertFalse( $post_type->is_embeddable );
}
}
Loading