Skip to content

Commit 5d73f7a

Browse files
committed
Admin: Correct logic flow for nonce and capability checks in post.php
1 parent 4d3b0b9 commit 5d73f7a

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

src/wp-admin/post.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@
7373
case 'post-quickdraft-save':
7474
// Check nonce and capabilities.
7575
$nonce = $_REQUEST['_wpnonce'];
76+
$post_id = absint( $_REQUEST['post_ID'] ?? 0 );
77+
$post = $post_id ? get_post( $post_id ) : null;
7678
$error_msg = false;
7779

7880
// For output of the Quick Draft dashboard widget.
7981
require_once ABSPATH . 'wp-admin/includes/dashboard.php';
8082

81-
if ( ! wp_verify_nonce( $nonce, 'add-post' ) ) {
83+
if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) {
8284
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
8385
}
8486

@@ -90,7 +92,6 @@
9092
return wp_dashboard_quick_press( $error_msg );
9193
}
9294

93-
$post = get_post( $_REQUEST['post_ID'] );
9495
check_admin_referer( 'add-' . $post->post_type );
9596

9697
$_POST['comment_status'] = get_default_comment_status( $post->post_type );
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @group admin
4+
*/
5+
class Tests_Admin_Post_QuickDraftSave extends WP_UnitTestCase {
6+
protected static $admin_id;
7+
8+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
9+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
10+
}
11+
12+
public function set_up() {
13+
parent::set_up();
14+
wp_set_current_user( self::$admin_id );
15+
set_current_screen( 'dashboard' );
16+
}
17+
18+
/**
19+
* Test Happy Path: Successfully validating a correct nonce and post_ID.
20+
* * @ticket 65052
21+
*/
22+
public function test_post_quickdraft_save_happy_path() {
23+
$post_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
24+
$nonce = wp_create_nonce( 'add-post' );
25+
26+
$_REQUEST['_wpnonce'] = $nonce;
27+
$_REQUEST['post_ID'] = $post_id;
28+
29+
$nonce_req = $_REQUEST['_wpnonce'] ?? '';
30+
$id_req = absint( $_REQUEST['post_ID'] ?? 0 );
31+
$post = $id_req ? get_post( $id_req ) : null;
32+
33+
$error_msg = false;
34+
if ( ! $post || ! wp_verify_nonce( $nonce_req, 'add-post' ) ) {
35+
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
36+
}
37+
38+
$this->assertFalse( $error_msg, 'Happy path should not produce an error message.' );
39+
$this->assertNotNull( $post );
40+
$this->assertEquals( $post_id, $post->ID );
41+
}
42+
43+
/**
44+
* @ticket 65052
45+
* test post quickdraft save missing nonce
46+
*/
47+
public function test_post_quickdraft_save_missing_nonce() {
48+
$_REQUEST['action'] = 'post-quickdraft-save';
49+
unset( $_REQUEST['_wpnonce'] ); // invliad nonce
50+
$_REQUEST['post_ID'] = 0;
51+
52+
$nonce = $_REQUEST['_wpnonce'] ?? '';
53+
$post_id = absint( $_REQUEST['post_ID'] ?? 0 );
54+
$post = $post_id ? get_post( $post_id ) : null;
55+
56+
$error_msg = false;
57+
if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) {
58+
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
59+
}
60+
61+
$this->assertSame( 'Unable to submit this form, please refresh and try again.', $error_msg );
62+
}
63+
64+
/**
65+
* @ticket 65052
66+
* test post quickdraft save invalid all
67+
*/
68+
public function test_post_quickdraft_save_invalid_all() {
69+
$_REQUEST['_wpnonce'] = 'invalid_nonce';
70+
$_REQUEST['post_ID'] = -1; // invalid ID
71+
72+
$nonce = $_REQUEST['_wpnonce'] ?? '';
73+
$post_id = absint( $_REQUEST['post_ID'] ?? 0 );
74+
$post = $post_id ? get_post( $post_id ) : null;
75+
76+
$this->assertNull( $post );
77+
78+
$error_msg = false;
79+
if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) {
80+
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
81+
}
82+
83+
$this->assertNotEmpty( $error_msg );
84+
}
85+
}

0 commit comments

Comments
 (0)