<!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>[50130] trunk: Posts, Post Types: Additional functions to check if a post is publicly viewable.</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/50130">50130</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/50130","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-02-01 23:31:54 +0000 (Mon, 01 Feb 2021)</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: Additional functions to check if a post is publicly viewable.
Introduces `is_post_status_viewable()` as a sibling to `is_post_type_viewable()`. Internal and protected statuses are never considered viewable. For built in posts statuses the `public` attribute is checked, for custom statuses the `publicly_queryable` attribute is checked.
Introduces `is_post_publicly_viewable()` for determining if an individual post can be viewed by logged out users. A post is considered viewable if both `is_post_status_viewable()` and `is_post_type_viewable()` return `true` for the post's attributes.
Additionally modifies `is_post_type_viewable()` to return `false` if an unregistered post type is passed to the function to avoid attempting to access properties on a non-object.
Props peterwilsoncc, SergeyBiryukov, whyisjake, TimothyBlynJacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/49380">#49380</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestspostisPostPubliclyViewablephp">trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php</a></li>
<li><a href="#trunktestsphpunittestspostisPostStatusViewablephp">trunk/tests/phpunit/tests/post/isPostStatusViewable.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<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 2021-02-01 22:11:46 UTC (rev 50129)
+++ trunk/src/wp-includes/post.php 2021-02-01 23:31:54 UTC (rev 50130)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2019,10 +2019,68 @@
</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">+ if ( ! is_object( $post_type ) ) {
+ return false;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
</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">+ * Determine whether a post status is considered "viewable".
+ *
+ * For built-in post statuses such as publish and private, the 'public' value will be evaluted.
+ * For all others, the 'publicly_queryable' value will be used.
+ *
+ * @since 5.7.0
+ *
+ * @param string|stdClass $post_status Post status name or object.
+ * @return bool Whether the post status should be considered viewable.
+ */
+function is_post_status_viewable( $post_status ) {
+ if ( is_scalar( $post_status ) ) {
+ $post_status = get_post_status_object( $post_status );
+ if ( ! $post_status ) {
+ return false;
+ }
+ }
+
+ if (
+ ! is_object( $post_status ) ||
+ $post_status->internal ||
+ $post_status->protected
+ ) {
+ return false;
+ }
+
+ return $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
+}
+
+/**
+ * Determine whether a post is publicly viewable.
+ *
+ * Posts are considered publicly viewable if both the post status and post type
+ * are viewable.
+ *
+ * @since 5.7.0
+ *
+ * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
+ * @return bool Whether the post is publicly viewable.
+ */
+function is_post_publicly_viewable( $post = null ) {
+ $post = get_post( $post );
+
+ if ( ! $post ) {
+ return false;
+ }
+
+ $post_type = get_post_type( $post );
+ $post_status = get_post_status( $post );
+
+ return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
+}
+
+/**
</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"> * The defaults are as follows:
</span></span></pre></div>
<a id="trunktestsphpunittestspostisPostPubliclyViewablephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php (rev 0)
+++ trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php 2021-02-01 23:31:54 UTC (rev 50130)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,108 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group post
+ */
+class Tests_Post_IsPostPubliclyViewable extends WP_UnitTestCase {
+
+ /**
+ * Array of post IDs to use as parents.
+ *
+ * @var array
+ */
+ public static $parent_post_ids = array();
+
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ $post_statuses = array( 'publish', 'private', 'future', 'trash', 'delete' );
+ foreach ( $post_statuses as $post_status ) {
+ $date = '';
+ $actual_status = $post_status;
+ if ( 'future' === $post_status ) {
+ $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
+ } elseif ( in_array( $post_status, array( 'trash', 'delete' ), true ) ) {
+ $actual_status = 'publish';
+ }
+
+ self::$parent_post_ids[ $post_status ] = $factory->post->create(
+ array(
+ 'post_status' => $actual_status,
+ 'post_name' => "$post_status-post",
+ 'post_date' => $date,
+ 'post_type' => 'page',
+ )
+ );
+ }
+
+ wp_trash_post( self::$parent_post_ids['trash'] );
+ wp_delete_post( self::$parent_post_ids['delete'], true );
+ }
+
+ /**
+ * Unit tests for is_post_publicly_viewable().
+ *
+ * @dataProvider data_is_post_publicly_viewable
+ * @ticket 49380
+ *
+ * @param string $post_type The post type.
+ * @param string $post_status The post status.
+ * @param bool $expected The expected result of the function call.
+ * @param string $parent_key The parent key as set up in shared fixtures.
+ */
+ public function test_is_post_publicly_viewable( $post_type, $post_status, $expected, $parent_key = '' ) {
+ $date = '';
+ if ( 'future' === $post_status ) {
+ $date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
+ }
+
+ $post_id = $this->factory()->post->create(
+ array(
+ 'post_type' => $post_type,
+ 'post_status' => $post_status,
+ 'post_parent' => $parent_key ? self::$parent_post_ids[ $parent_key ] : 0,
+ 'post_date' => $date,
+ )
+ );
+
+ $this->assertSame( $expected, is_post_publicly_viewable( $post_id ) );
+ }
+
+ /**
+ * Data provider for test_is_post_publicly_viewable().
+ *
+ * return array[] {
+ * @type string $post_type The post type.
+ * @type string $post_status The post status.
+ * @type bool $expected The expected result of the function call.
+ * @type string $parent_key The parent key as set up in shared fixtures.
+ * }
+ */
+ public function data_is_post_publicly_viewable() {
+ return array(
+ array( 'post', 'publish', true ),
+ array( 'post', 'private', false ),
+ array( 'post', 'future', false ),
+
+ array( 'page', 'publish', true ),
+ array( 'page', 'private', false ),
+ array( 'page', 'future', false ),
+
+ array( 'unregistered_cpt', 'publish', false ),
+ array( 'unregistered_cpt', 'private', false ),
+
+ array( 'post', 'unregistered_cps', false ),
+ array( 'page', 'unregistered_cps', false ),
+
+ array( 'attachment', 'inherit', true, 'publish' ),
+ array( 'attachment', 'inherit', false, 'private' ),
+ array( 'attachment', 'inherit', false, 'future' ),
+ array( 'attachment', 'inherit', true, 'trash' ),
+ array( 'attachment', 'inherit', true, 'delete' ),
+
+ array( 'page', 'publish', true, 'publish' ),
+ array( 'page', 'publish', true, 'private' ),
+ array( 'page', 'publish', true, 'future' ),
+ array( 'page', 'publish', true, 'trash' ),
+ array( 'page', 'publish', true, 'delete' ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/post/isPostPubliclyViewable.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestspostisPostStatusViewablephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/post/isPostStatusViewable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/isPostStatusViewable.php (rev 0)
+++ trunk/tests/phpunit/tests/post/isPostStatusViewable.php 2021-02-01 23:31:54 UTC (rev 50130)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,171 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group post
+ */
+class Tests_Post_IsPostStatusViewable extends WP_UnitTestCase {
+
+ /**
+ * Remove the test status from the global when finished.
+ *
+ * @global $wp_post_statuses
+ */
+ static function wpTearDownAfterClass() {
+ global $wp_post_statuses;
+ unset( $wp_post_statuses['wp_tests_ps'] );
+ }
+
+ /**
+ * Test custom post status.
+ *
+ * This may include emulations of built in (_builtin) statuses.
+ *
+ * @ticket 49380
+ * @dataProvider data_custom_post_statuses
+ *
+ * @param array $cps_args Registration arguments.
+ * @param bool $expected Expected result.
+ */
+ public function test_custom_post_statuses( $cps_args, $expected ) {
+ register_post_status(
+ 'wp_tests_ps',
+ $cps_args
+ );
+
+ // Test status passed as string.
+ $this->assertSame( $expected, is_post_status_viewable( 'wp_tests_ps' ) );
+ // Test status passed as object.
+ $this->assertSame( $expected, is_post_status_viewable( get_post_status_object( 'wp_tests_ps' ) ) );
+ }
+
+ /**
+ * Data provider for custom post status tests.
+ *
+ * @return array[] {
+ * array CPS registration args.
+ * bool Expected result.
+ * }
+ */
+ public function data_custom_post_statuses() {
+ return array(
+ // 0. False for non-publically queryable types.
+ array(
+ array(
+ 'publicly_queryable' => false,
+ '_builtin' => false,
+ 'public' => true,
+ ),
+ false,
+ ),
+ // 1. True for publically queryable types.
+ array(
+ array(
+ 'publicly_queryable' => true,
+ '_builtin' => false,
+ 'public' => false,
+ ),
+ true,
+ ),
+ // 2. False for built-in non-public types.
+ array(
+ array(
+ 'publicly_queryable' => false,
+ '_builtin' => true,
+ 'public' => false,
+ ),
+ false,
+ ),
+ // 3. False for non-built-in public types.
+ array(
+ array(
+ 'publicly_queryable' => false,
+ '_builtin' => false,
+ 'public' => true,
+ ),
+ false,
+ ),
+ // 4. True for built-in public types.
+ array(
+ array(
+ 'publicly_queryable' => false,
+ '_builtin' => true,
+ 'public' => true,
+ ),
+ true,
+ ),
+ );
+ }
+
+ /**
+ * Test built-in and unregistered post status.
+ *
+ * @dataProvider data_built_unregistered_in_status_types
+ * @ticket 49380
+ *
+ * @param mixed $status Post status to check.
+ * @param bool $expected Expected viewable status.
+ */
+ function test_built_unregistered_in_status_types( $status, $expected ) {
+ // Test status passed as string.
+ $this->assertSame( $expected, is_post_status_viewable( $status ) );
+ // Test status passed as object.
+ $this->assertSame( $expected, is_post_status_viewable( get_post_status_object( $status ) ) );
+ }
+
+ /**
+ * Data provider for built-in and unregistered post status tests.
+ *
+ * @return array[] {
+ * @type mixed $status Post status to check.
+ * @type bool $expected Expected viewable status.
+ * }
+ */
+ public function data_built_unregistered_in_status_types() {
+ return array(
+ array( 'publish', true ),
+ array( 'future', false ),
+ array( 'draft', false ),
+ array( 'pending', false ),
+ array( 'private', false ),
+ array( 'trash', false ),
+ array( 'auto-draft', false ),
+ array( 'inherit', false ),
+ array( 'request-pending', false ),
+ array( 'request-confirmed', false ),
+ array( 'request-failed', false ),
+ array( 'request-completed', false ),
+
+ // Various unregistered statuses.
+ array( 'unregistered-status', false ),
+ array( false, false ),
+ array( true, false ),
+ array( 20, false ),
+ array( null, false ),
+ array( '', false ),
+ );
+ }
+
+ /**
+ * Sanitize key should not be run when testing.
+ *
+ * @ticket 49380
+ */
+ public function test_sanitize_key_not_run() {
+ register_post_status(
+ 'WP_Tests_ps',
+ array(
+ 'publicly_queryable' => true,
+ '_builtin' => false,
+ 'public' => true,
+ )
+ );
+
+ // Sanitized key should return true.
+ $this->assertTrue( is_post_status_viewable( 'wp_tests_ps' ) );
+ $this->assertTrue( is_post_status_viewable( get_post_status_object( 'wp_tests_ps' ) ) );
+
+ // Unsanitized key should return false.
+ $this->assertFalse( is_post_status_viewable( 'WP_tests_ps' ) );
+ $this->assertFalse( is_post_status_viewable( get_post_status_object( 'WP_tests_ps' ) ) );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/post/isPostStatusViewable.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>