Skip to content

Commit b33cbca

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

2 files changed

Lines changed: 94 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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
public function tear_down() {
19+
parent::tear_down();
20+
set_current_screen( 'front' );
21+
unset( $_REQUEST['_wpnonce'], $_REQUEST['post_ID'], $_REQUEST['action'] );
22+
}
23+
24+
/**
25+
* Test Happy Path: Successfully validating a correct nonce and post_ID.
26+
* * @ticket 65052
27+
*/
28+
public function test_post_quickdraft_save_happy_path() {
29+
$post_id = self::factory()->post->create( array( 'post_status' => 'draft' ) );
30+
$nonce = wp_create_nonce( 'add-post' );
31+
32+
$_REQUEST['_wpnonce'] = $nonce;
33+
$_REQUEST['post_ID'] = $post_id;
34+
35+
$nonce_req = $_REQUEST['_wpnonce'] ?? '';
36+
$id_req = absint( $_REQUEST['post_ID'] ?? 0 );
37+
$post = $id_req ? get_post( $id_req ) : null;
38+
39+
$error_msg = false;
40+
if ( ! $post || ! wp_verify_nonce( $nonce_req, 'add-post' ) ) {
41+
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
42+
}
43+
44+
$this->assertFalse( $error_msg, 'Happy path should not produce an error message.' );
45+
$this->assertNotNull( $post );
46+
$this->assertEquals( $post_id, $post->ID );
47+
}
48+
49+
/**
50+
* @ticket 65052
51+
* test post quickdraft save missing nonce
52+
*/
53+
public function test_post_quickdraft_save_missing_nonce() {
54+
$_REQUEST['action'] = 'post-quickdraft-save';
55+
unset( $_REQUEST['_wpnonce'] ); // invliad nonce
56+
$_REQUEST['post_ID'] = 0;
57+
58+
$nonce = $_REQUEST['_wpnonce'] ?? '';
59+
$post_id = absint( $_REQUEST['post_ID'] ?? 0 );
60+
$post = $post_id ? get_post( $post_id ) : null;
61+
62+
$error_msg = false;
63+
if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) {
64+
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
65+
}
66+
67+
$this->assertSame( 'Unable to submit this form, please refresh and try again.', $error_msg );
68+
}
69+
70+
/**
71+
* @ticket 65052
72+
* test post quickdraft save invalid all
73+
*/
74+
public function test_post_quickdraft_save_invalid_all() {
75+
$_REQUEST['_wpnonce'] = 'invalid_nonce';
76+
$_REQUEST['post_ID'] = -1; // invalid ID
77+
78+
$nonce = $_REQUEST['_wpnonce'] ?? '';
79+
$post_id = absint( $_REQUEST['post_ID'] ?? 0 );
80+
$post = $post_id ? get_post( $post_id ) : null;
81+
82+
$this->assertNull( $post );
83+
84+
$error_msg = false;
85+
if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) {
86+
$error_msg = __( 'Unable to submit this form, please refresh and try again.' );
87+
}
88+
89+
$this->assertNotEmpty( $error_msg );
90+
}
91+
}

0 commit comments

Comments
 (0)