<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59700] trunk: Posts, Post Types: Embeds: Add new `embeddable` argument to post types.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59700">59700</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59700","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>swissspidy</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-01-24 13:30:27 +0000 (Fri, 24 Jan 2025)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/35567">#35567</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpposttypephp">trunk/src/wp-includes/class-wp-post-type.php</a></li>
<li><a href="#trunksrcwpincludesembedphp">trunk/src/wp-includes/embed.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestsoembeddiscoveryphp">trunk/tests/phpunit/tests/oembed/discovery.php</a></li>
<li><a href="#trunktestsphpunittestsoembedtemplatephp">trunk/tests/phpunit/tests/oembed/template.php</a></li>
<li><a href="#trunktestsphpunittestsposttypesphp">trunk/tests/phpunit/tests/post/types.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpposttypephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-post-type.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-post-type.php      2025-01-24 12:28:26 UTC (rev 59699)
+++ trunk/src/wp-includes/class-wp-post-type.php        2025-01-24 13:30:27 UTC (rev 59700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -114,6 +114,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $publicly_queryable = null;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Whether this post type is embeddable.
+        *
+        * Default is the value of $public.
+        *
+        * @since 6.8.0
+        * @var bool $embeddable
+        */
+       public $embeddable = null;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Whether to generate and allow a UI for managing this post type in the admin.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Default is the value of $public.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -521,6 +531,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'hierarchical'                    => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'exclude_from_search'             => null,
</span><span class="cx" style="display: block; padding: 0 10px">                        'publicly_queryable'              => null,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'embeddable'                      => null,
</ins><span class="cx" style="display: block; padding: 0 10px">                         'show_ui'                         => null,
</span><span class="cx" style="display: block; padding: 0 10px">                        'show_in_menu'                    => null,
</span><span class="cx" style="display: block; padding: 0 10px">                        'show_in_nav_menus'               => null,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -565,6 +576,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['show_ui'] = $args['public'];
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // If not set, default to the setting for 'public'.
+               if ( null === $args['embeddable'] ) {
+                       $args['embeddable'] = $args['public'];
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // If not set, default rest_namespace to wp/v2 if show_in_rest is true.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $args['rest_namespace'] && ! empty( $args['show_in_rest'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $args['rest_namespace'] = 'wp/v2';
</span></span></pre></div>
<a id="trunksrcwpincludesembedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/embed.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/embed.php   2025-01-24 12:28:26 UTC (rev 59699)
+++ trunk/src/wp-includes/embed.php     2025-01-24 13:30:27 UTC (rev 59700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -331,11 +331,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Adds oEmbed discovery links in the head element of the website.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.8.0 Output was adjusted to only embed if the post supports it.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_oembed_add_discovery_links() {
</span><span class="cx" style="display: block; padding: 0 10px">        $output = '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( is_singular() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_singular() && is_post_embeddable() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $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";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( class_exists( 'SimpleXMLElement' ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -538,11 +539,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Retrieves the oEmbed response data for a given post.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.4.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.8.0 Output was adjusted to only embed if the post type supports it.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param WP_Post|int $post  Post ID or post object.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param int         $width The requested width.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return array|false Response data on success, false if post doesn't exist
- *                     or is not publicly viewable.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array|false Response data on success, false if post doesn't exist,
+ *                     is not publicly viewable or post type is not embeddable.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_oembed_response_data( $post, $width ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $post  = get_post( $post );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -556,6 +558,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( ! is_post_embeddable( $post ) ) {
+               return false;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters the allowed minimum and maximum widths for the oEmbed response.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2025-01-24 12:28:26 UTC (rev 59699)
+++ trunk/src/wp-includes/post.php      2025-01-24 13:30:27 UTC (rev 59700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2476,6 +2476,40 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 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 );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Retrieves an array of the latest posts, or posts matching the given criteria.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * For more information on the accepted arguments, see the
</span></span></pre></div>
<a id="trunktestsphpunittestsoembeddiscoveryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/oembed/discovery.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/oembed/discovery.php    2025-01-24 12:28:26 UTC (rev 59699)
+++ trunk/tests/phpunit/tests/oembed/discovery.php      2025-01-24 13:30:27 UTC (rev 59700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,4 +85,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $expected, get_echo( 'wp_oembed_add_discovery_links' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 35567
+        */
+       public function test_wp_oembed_add_discovery_links_non_embeddable_post_type_output_should_be_empty() {
+               register_post_type( 'not_embeddable', array( 'embeddable' => false ) );
+
+               $post = self::factory()->post->create_and_get(
+                       array(
+                               'post_type' => 'not_embeddable',
+                       )
+               );
+
+               $this->assertFalse( get_oembed_response_data( $post, 100 ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsoembedtemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/oembed/template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/oembed/template.php     2025-01-24 12:28:26 UTC (rev 59699)
+++ trunk/tests/phpunit/tests/oembed/template.php       2025-01-24 13:30:27 UTC (rev 59700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -343,4 +343,60 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_maybe_enqueue_oembed_host_js( $post_embed );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( $scripts->query( 'wp-embed', 'enqueued' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 35567
+        */
+       public function test_is_embeddable_post_non_existent_post() {
+               $this->assertFalse( is_post_embeddable( 99999 ) );
+       }
+
+       /**
+        * @ticket 35567
+        */
+       public function test_is_embeddable_post_should_return_false_for_non_embeddable_post_type() {
+               register_post_type( 'not_embeddable', array( '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( '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( '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 );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsposttypesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/types.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/types.php  2025-01-24 12:28:26 UTC (rev 59699)
+++ trunk/tests/phpunit/tests/post/types.php    2025-01-24 13:30:27 UTC (rev 59700)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -646,4 +646,34 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( post_type_supports( 'foo', 'editor' ), 'Post type should not support editor.' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( post_type_supports( 'foo', 'autosave' ), 'Post type should still support autosaves.' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @group oembed
+        * @ticket 35567
+        */
+       public function test_register_post_type_is_embeddable_should_default_to_value_of_public() {
+               $post_type = register_post_type( $this->post_type );
+               $this->assertFalse( $post_type->embeddable, 'Non-public post type should not be embeddable by default' );
+
+               $post_type = register_post_type( $this->post_type, array( 'public' => true ) );
+               $this->assertTrue( $post_type->embeddable, 'Public post type should be embeddable by default' );
+       }
+
+       /**
+        * @group oembed
+        * @ticket 35567
+        */
+       public function test_register_post_type_override_is_embeddable() {
+               $post_type = register_post_type( $this->post_type, array( 'embeddable' => true ) );
+               $this->assertTrue( $post_type->embeddable, 'Post type should be embeddable even though it is not public' );
+
+               $post_type = register_post_type(
+                       $this->post_type,
+                       array(
+                               'public'     => true,
+                               'embeddable' => false,
+                       )
+               );
+               $this->assertFalse( $post_type->embeddable, 'Post type should not be embeddable even though it is public' );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>