<!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>[42380] trunk: Role/Capability: When checking capabilities before setting a post slug, ensure the correct post type capabilities are used.</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 { 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/42380">42380</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/42380","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-12-09 22:50:13 +0000 (Sat, 09 Dec 2017)</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'>Role/Capability: When checking capabilities before setting a post slug, ensure the correct post type capabilities are used.

Previously, only the `publish_posts` capability was checked. Now, the correct meta or primitive capability for the post type is used where appropriate.

Props peterwilsoncc

Fixes <a href="https://core.trac.wordpress.org/ticket/42464">#42464</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestspostwpInsertPostphp">trunk/tests/phpunit/tests/post/wpInsertPost.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    2017-12-08 21:00:08 UTC (rev 42379)
+++ trunk/src/wp-includes/post.php      2017-12-09 22:50:13 UTC (rev 42380)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3304,9 +3304,17 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // Don't allow contributors to set the post slug for pending review posts.
-       if ( 'pending' == $post_status && ! current_user_can( 'publish_posts' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+        * Don't allow contributors to set the post slug for pending review posts.
+        *
+        * For new posts check the primitive capability, for updates check the meta capability.
+        */
+       $post_type_object = get_post_type_object( $post_type );
+
+       if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $post_name = '';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) {
+               $post_name = '';
</ins><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></span></pre></div>
<a id="trunktestsphpunittestspostwpInsertPostphp"></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/wpInsertPost.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/wpInsertPost.php   2017-12-08 21:00:08 UTC (rev 42379)
+++ trunk/tests/phpunit/tests/post/wpInsertPost.php     2017-12-09 22:50:13 UTC (rev 42380)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5,6 +5,53 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_WPInsertPost extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected static $user_ids = array(
+               'administrator' => null,
+               'contributor'   => null,
+       );
+
+       static function wpSetUpBeforeClass( $factory ) {
+               self::$user_ids = array(
+                       'administrator' => $factory->user->create( array(
+                               'role' => 'administrator',
+                       ) ),
+                       'contributor'   => $factory->user->create( array(
+                               'role' => 'contributor',
+                       ) ),
+               );
+
+               $role = get_role( 'administrator' );
+               $role->add_cap( 'publish_mapped_meta_caps' );
+               $role->add_cap( 'publish_unmapped_meta_caps' );
+       }
+
+       static function tearDownAfterClass() {
+               $role = get_role( 'administrator' );
+               $role->remove_cap( 'publish_mapped_meta_caps' );
+               $role->remove_cap( 'publish_unmapped_meta_caps' );
+
+               parent::tearDownAfterClass();
+       }
+
+       function setUp() {
+               parent::setUp();
+
+               register_post_type( 'mapped_meta_caps', array(
+                       'capability_type' => array( 'mapped_meta_cap', 'mapped_meta_caps' ),
+                       'map_meta_cap'    => true,
+               ) );
+
+               register_post_type( 'unmapped_meta_caps', array(
+                       'capability_type' => array( 'unmapped_meta_cap', 'unmapped_meta_caps' ),
+                       'map_meta_cap'    => false,
+               ) );
+
+               register_post_type( 'no_admin_caps', array(
+                       'capability_type' => array( 'no_admin_cap', 'no_admin_caps' ),
+                       'map_meta_cap'    => false,
+               ) );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 11863
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -103,4 +150,131 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'about', get_post( $another_about_page_id )->post_name );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'about-2', get_post( $about_page_id )->post_name );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Data for testing the ability for users to set the post slug.
+        *
+        * @return array Array of test arguments.
+        */
+       function data_various_post_types() {
+               return array(
+                       array(
+                               'mapped_meta_caps',
+                       ),
+                       array(
+                               'unmapped_meta_caps',
+                       ),
+                       array(
+                               'post',
+                       ),
+               );
+       }
+
+       /**
+        * Test contributor making changes to the pending post slug.
+        *
+        * @ticket 42464
+        * @dataProvider data_various_post_types
+        */
+       function test_contributor_cannot_set_post_slug( $post_type ) {
+               wp_set_current_user( self::$user_ids['contributor'] );
+
+               $post_id = $this->factory()->post->create( array(
+                       'post_title'   => 'Jefferson claim: nice to have Washington on your side.',
+                       'post_content' => "I’m in the cabinet. I am complicit in watching him grabbin’ at power and kiss it.\n\nIf Washington isn’t gon’ listen to disciplined dissidents, this is the difference: this kid is out!",
+                       'post_type'    => $post_type,
+                       'post_name'    => 'new-washington',
+                       'post_status'  => 'pending',
+               ) );
+
+               $expected = '';
+               $actual = get_post_field( 'post_name', $post_id );
+
+               $this->assertSame( $expected, $actual );
+
+               // Now update the post.
+               wp_update_post( array(
+                       'ID' => $post_id,
+                       'post_title' => 'Hamilton has Washington on side: Jefferson',
+                       'post_name'  => 'edited-washington',
+               ) );
+
+               $expected = '';
+               $actual = get_post_field( 'post_name', $post_id );
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       /**
+        * Test administrator making changes to the pending post slug.
+        *
+        * @ticket 42464
+        * @dataProvider data_various_post_types
+        */
+       function test_administrator_can_set_post_slug( $post_type ) {
+               wp_set_current_user( self::$user_ids['administrator'] );
+
+               $post_id = $this->factory()->post->create( array(
+                       'post_title'   => 'What is the Conner Project?',
+                       'post_content' => "Evan Hansen’s last link to his friend Conner is a signature on his broken arm.",
+                       'post_type'    => $post_type,
+                       'post_name'    => 'dear-evan-hansen-explainer',
+                       'post_status'  => 'pending',
+               ) );
+
+               $expected = 'dear-evan-hansen-explainer';
+               $actual = get_post_field( 'post_name', $post_id );
+
+               $this->assertSame( $expected, $actual );
+
+               // Now update the post.
+               wp_update_post( array(
+                       'ID' => $post_id,
+                       'post_title' => 'Conner Project to close',
+                       'post_name'  => 'dear-evan-hansen-spoiler',
+               ) );
+
+               $expected = 'dear-evan-hansen-spoiler';
+               $actual = get_post_field( 'post_name', $post_id );
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       /**
+        * Test administrator making changes to a pending post slug for a post type they don't
+        * have permission to publish.
+        *
+        * These assertions failed prior to ticket #42464.
+        *
+        * @ticket 42464
+        */
+       function test_administrator_cannot_set_post_slug_on_post_type_they_cannot_publish() {
+               wp_set_current_user( self::$user_ids['administrator'] );
+
+               $post_id = $this->factory()->post->create( array(
+                       'post_title'   => 'Everything is legal in New Jersey',
+                       'post_content' => 'Shortly before his death, Philip Hamilton was heard to claim everything was legal in the garden state.',
+                       'post_type'    => 'no_admin_caps',
+                       'post_name'    => 'yet-another-duel',
+                       'post_status'  => 'pending',
+               ) );
+
+               $expected = '';
+               $actual = get_post_field( 'post_name', $post_id );
+
+               $this->assertSame( $expected, $actual );
+
+               // Now update the post.
+               wp_update_post( array(
+                       'ID' => $post_id,
+                       'post_title' => 'Ten things illegal in New Jersey',
+                       'post_name'  => 'foreshadowing-in-nj',
+               ) );
+
+               $expected = '';
+               $actual = get_post_field( 'post_name', $post_id );
+
+               $this->assertSame( $expected, $actual );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>