@@ -174,6 +174,27 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
174
174
return $ post_data ;
175
175
}
176
176
177
+ /**
178
+ * Returns only allowed post data fields
179
+ *
180
+ * @since 4.9.9
181
+ *
182
+ * @param array $post_data Array of post data. Defaults to the contents of $_POST.
183
+ * @return object|bool WP_Error on failure, true on success.
184
+ */
185
+ function _wp_get_allowed_postdata ( $ post_data = null ) {
186
+ if ( empty ( $ post_data ) ) {
187
+ $ post_data = $ _POST ;
188
+ }
189
+
190
+ // Pass through errors
191
+ if ( is_wp_error ( $ post_data ) ) {
192
+ return $ post_data ;
193
+ }
194
+
195
+ return array_diff_key ( $ post_data , array_flip ( array ( 'meta_input ' , 'file ' , 'guid ' ) ) );
196
+ }
197
+
177
198
/**
178
199
* Update an existing post with values provided in $_POST.
179
200
*
@@ -242,6 +263,7 @@ function edit_post( $post_data = null ) {
242
263
$ post_data = _wp_translate_postdata ( true , $ post_data );
243
264
if ( is_wp_error ($ post_data ) )
244
265
wp_die ( $ post_data ->get_error_message () );
266
+ $ translated = _wp_get_allowed_postdata ( $ post_data );
245
267
246
268
// Post Formats
247
269
if ( isset ( $ post_data ['post_format ' ] ) )
@@ -321,7 +343,7 @@ function edit_post( $post_data = null ) {
321
343
$ attachment_data = isset ( $ post_data ['attachments ' ][ $ post_ID ] ) ? $ post_data ['attachments ' ][ $ post_ID ] : array ();
322
344
323
345
/** This filter is documented in wp-admin/includes/media.php */
324
- $ post_data = apply_filters ( 'attachment_fields_to_save ' , $ post_data , $ attachment_data );
346
+ $ translated = apply_filters ( 'attachment_fields_to_save ' , $ translated , $ attachment_data );
325
347
}
326
348
327
349
// Convert taxonomy input to term IDs, to avoid ambiguity.
@@ -366,26 +388,26 @@ function edit_post( $post_data = null ) {
366
388
}
367
389
}
368
390
369
- $ post_data ['tax_input ' ][ $ taxonomy ] = $ clean_terms ;
391
+ $ translated ['tax_input ' ][ $ taxonomy ] = $ clean_terms ;
370
392
}
371
393
}
372
394
373
395
add_meta ( $ post_ID );
374
396
375
397
update_post_meta ( $ post_ID , '_edit_last ' , get_current_user_id () );
376
398
377
- $ success = wp_update_post ( $ post_data );
399
+ $ success = wp_update_post ( $ translated );
378
400
// If the save failed, see if we can sanity check the main fields and try again
379
401
if ( ! $ success && is_callable ( array ( $ wpdb , 'strip_invalid_text_for_column ' ) ) ) {
380
402
$ fields = array ( 'post_title ' , 'post_content ' , 'post_excerpt ' );
381
403
382
404
foreach ( $ fields as $ field ) {
383
- if ( isset ( $ post_data [ $ field ] ) ) {
384
- $ post_data [ $ field ] = $ wpdb ->strip_invalid_text_for_column ( $ wpdb ->posts , $ field , $ post_data [ $ field ] );
405
+ if ( isset ( $ translated [ $ field ] ) ) {
406
+ $ translated [ $ field ] = $ wpdb ->strip_invalid_text_for_column ( $ wpdb ->posts , $ field , $ translated [ $ field ] );
385
407
}
386
408
}
387
409
388
- wp_update_post ( $ post_data );
410
+ wp_update_post ( $ translated );
389
411
}
390
412
391
413
// Now that we have an ID we can fix any attachment anchor hrefs
@@ -545,27 +567,25 @@ function bulk_edit_posts( $post_data = null ) {
545
567
unset( $ post_data ['tax_input ' ]['category ' ] );
546
568
}
547
569
570
+ $ post_data ['post_ID ' ] = $ post_ID ;
548
571
$ post_data ['post_type ' ] = $ post ->post_type ;
549
572
$ post_data ['post_mime_type ' ] = $ post ->post_mime_type ;
550
- $ post_data ['guid ' ] = $ post ->guid ;
551
573
552
574
foreach ( array ( 'comment_status ' , 'ping_status ' , 'post_author ' ) as $ field ) {
553
575
if ( ! isset ( $ post_data [ $ field ] ) ) {
554
576
$ post_data [ $ field ] = $ post ->$ field ;
555
577
}
556
578
}
557
579
558
- $ post_data ['ID ' ] = $ post_ID ;
559
- $ post_data ['post_ID ' ] = $ post_ID ;
560
-
561
580
$ post_data = _wp_translate_postdata ( true , $ post_data );
562
581
if ( is_wp_error ( $ post_data ) ) {
563
582
$ skipped [] = $ post_ID ;
564
583
continue ;
565
584
}
585
+ $ post_data = _wp_get_allowed_postdata ( $ post_data );
566
586
567
- if ( isset ( $ post_data ['post_format ' ] ) ) {
568
- set_post_format ( $ post_ID , $ post_data ['post_format ' ] );
587
+ if ( isset ( $ shared_post_data ['post_format ' ] ) ) {
588
+ set_post_format ( $ post_ID , $ shared_post_data ['post_format ' ] );
569
589
unset( $ post_data ['tax_input ' ]['post_format ' ] );
570
590
}
571
591
@@ -757,9 +777,10 @@ function wp_write_post() {
757
777
$ translated = _wp_translate_postdata ( false );
758
778
if ( is_wp_error ($ translated ) )
759
779
return $ translated ;
780
+ $ translated = _wp_get_allowed_postdata ( $ translated );
760
781
761
782
// Create the post.
762
- $ post_ID = wp_insert_post ( $ _POST );
783
+ $ post_ID = wp_insert_post ( $ translated );
763
784
if ( is_wp_error ( $ post_ID ) )
764
785
return $ post_ID ;
765
786
@@ -1685,6 +1706,7 @@ function wp_create_post_autosave( $post_data ) {
1685
1706
$ post_data = _wp_translate_postdata ( true , $ post_data );
1686
1707
if ( is_wp_error ( $ post_data ) )
1687
1708
return $ post_data ;
1709
+ $ post_data = _wp_get_allowed_postdata ( $ post_data );
1688
1710
1689
1711
$ post_author = get_current_user_id ();
1690
1712
0 commit comments