From 6a84867ba37de62afcc618057caba8a5331847e7 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 24 Jan 2025 13:32:21 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@59700 git-svn-id: http://core.svn.wordpress.org/trunk@59042 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-post-type.php | 16 ++++++++++++++ wp-includes/embed.php | 12 ++++++++--- wp-includes/post.php | 34 ++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/wp-includes/class-wp-post-type.php b/wp-includes/class-wp-post-type.php index 5336d3e1cf..400fd0b698 100644 --- a/wp-includes/class-wp-post-type.php +++ b/wp-includes/class-wp-post-type.php @@ -113,6 +113,16 @@ final class WP_Post_Type { */ public $publicly_queryable = null; + /** + * Whether this post type is embeddable. + * + * Default is the value of $public. + * + * @since 6.8.0 + * @var bool $embeddable + */ + public $embeddable = null; + /** * Whether to generate and allow a UI for managing this post type in the admin. * @@ -521,6 +531,7 @@ final class WP_Post_Type { 'hierarchical' => false, 'exclude_from_search' => null, 'publicly_queryable' => null, + 'embeddable' => null, 'show_ui' => null, 'show_in_menu' => null, 'show_in_nav_menus' => null, @@ -565,6 +576,11 @@ final class WP_Post_Type { $args['show_ui'] = $args['public']; } + // If not set, default to the setting for 'public'. + if ( null === $args['embeddable'] ) { + $args['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'; diff --git a/wp-includes/embed.php b/wp-includes/embed.php index f7fe10e482..b5b30acead 100644 --- a/wp-includes/embed.php +++ b/wp-includes/embed.php @@ -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 6.8.0 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 .= '' . "\n"; if ( class_exists( 'SimpleXMLElement' ) ) { @@ -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 6.8.0 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 ); @@ -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. * diff --git a/wp-includes/post.php b/wp-includes/post.php index 009287916d..a78e97b6c1 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -2475,6 +2475,40 @@ 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. + * + * @since 6.8.0 + * + * @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; + } + + $post_type = get_post_type_object( $post->post_type ); + + if ( ! $post_type ) { + return false; + } + + $is_embeddable = $post_type->embeddable; + + /** + * Filter whether a post is embeddable. + * + * @since 6.8.0 + * + * @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. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 2acd33dd4a..ef762d3b8b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8-alpha-59699'; +$wp_version = '6.8-alpha-59700'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.