Skip to content

Commit 7a28f54

Browse files
authored
Merge pull request #578 from Automattic/fix/471-clean-up-dashboard-note-updating
Clean up dashboard-note logic
2 parents 2507889 + 6ece6a3 commit 7a28f54

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

modules/dashboard/widgets/dashboard-notepad.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function init() {
1818
register_post_type( self::notepad_post_type, array(
1919
'rewrite' => false,
2020
'label' => __( 'Dashboard Note', 'edit-flow' )
21-
)
21+
)
2222
);
2323

2424
$this->edit_cap = apply_filters( 'ef_dashboard_notepad_edit_cap', $this->edit_cap );
@@ -34,31 +34,29 @@ public function init() {
3434
public function handle_notepad_update() {
3535
global $pagenow;
3636

37-
if ( 'index.php' != $pagenow
38-
|| ( empty( $_POST['action'] ) || 'dashboard-notepad' != $_POST['action'] ) )
37+
if ( 'index.php' !== $pagenow || empty( $_POST['action'] ) || 'dashboard-notepad' !== $_POST['action'] ) {
3938
return;
39+
}
4040

4141
check_admin_referer( 'dashboard-notepad' );
4242

43-
if ( ! current_user_can( $this->edit_cap ) )
43+
if ( ! current_user_can( $this->edit_cap ) ) {
4444
wp_die( EditFlow()->dashboard->messages['invalid-permissions'] );
45+
}
4546

46-
$current_id = (int) $_POST['notepad-id'];
47-
$current_notepad = get_post( $current_id );
48-
$new_note = array(
49-
'post_content' => wp_filter_nohtml_kses( $_POST['note'] ),
50-
'post_type' => self::notepad_post_type,
51-
'post_status' => 'draft',
52-
'post_author' => get_current_user_id(),
53-
);
54-
if ( $current_notepad
55-
&& self::notepad_post_type == $current_notepad->post_type
56-
&& ! isset ( $_POST['create-note'] ) ) {
57-
$new_note['ID'] = $current_id;
58-
wp_update_post( $new_note );
59-
} else {
60-
wp_insert_post( $new_note );
47+
$note_data = array(
48+
'post_content' => isset( $_POST['note'] ) ? wp_filter_nohtml_kses( $_POST['note'] ) : '',
49+
'post_type' => self::notepad_post_type,
50+
'post_status' => 'draft',
51+
'post_author' => get_current_user_id(),
52+
);
53+
54+
$existing_notepad = isset( $_POST['notepad-id'] ) ? get_post( absint( $_POST['notepad-id'] ) ) : null;
55+
if ( isset( $existing_notepad->post_type ) && self::notepad_post_type === $existing_notepad->post_type ) {
56+
$note_data['ID'] = $existing_notepad->ID;
6157
}
58+
59+
wp_insert_post( $note_data );
6260
}
6361

6462
/**

0 commit comments

Comments
 (0)