WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/30/2016 08:20:54 PM (5 years ago)
Author:
westonruter
Message:

Customize: Prevent auto-draft post/page stubs from being saved with empty slugs or published with non-unique slugs.

  • Allow WP_Customize_Nav_Menus::insert_auto_draft_post() to take full post array to pass to wp_insert_post(), except for post_status. Require post_title.
  • Ensure empty post_name gets explicitly set to slugified post_title.
  • Explicitly allow only post_type and post_title params in WP_Customize_Nav_Menus::ajax_insert_auto_draft_post().
  • Use wp_update_post() instead of wp_publish_post() to ensure unique slugs are assigned to published auto-draft posts.
  • Re-use WP_Customize_Nav_Menus::insert_auto_draft_post() when inserting stubs from starter content.


See #38114, #38013, #34923.
Fixes #38539.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r38979 r39038  
    735735     *
    736736     * @param array $postarr {
    737      *     Abbreviated post array.
    738      *
    739      *     @var string $post_title Post title.
    740      *     @var string $post_type  Post type.
     737     *     Post array. Note that post_status is overridden to be `auto-draft`.
     738     *
     739     *     @var string $post_title   Post title. Required.
     740     *     @var string $post_type    Post type. Required.
     741     *     @var string $post_name    Post name.
     742     *     @var string $post_content Post content.
    741743     * }
    742744     * @return WP_Post|WP_Error Inserted auto-draft post object or error.
     
    746748            return new WP_Error( 'unknown_post_type', __( 'Unknown post type' ) );
    747749        }
    748         if ( ! isset( $postarr['post_title'] ) ) {
    749             $postarr['post_title'] = '';
     750        if ( empty( $postarr['post_title'] ) ) {
     751            return new WP_Error( 'empty_title', __( 'Empty title' ) );
     752        }
     753        if ( ! empty( $postarr['post_status'] ) ) {
     754            return new WP_Error( 'status_forbidden', __( 'Status is forbidden' ) );
     755        }
     756
     757        $postarr['post_status'] = 'auto-draft';
     758
     759        // Auto-drafts are allowed to have empty post_names, so it has to be explicitly set.
     760        if ( empty( $postarr['post_name'] ) ) {
     761            $postarr['post_name'] = sanitize_title( $postarr['post_title'] );
    750762        }
    751763
    752764        add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
    753         $args = array(
    754             'post_status' => 'auto-draft',
    755             'post_type'   => $postarr['post_type'],
    756             'post_title'  => $postarr['post_title'],
    757             'post_name'   => sanitize_title( $postarr['post_title'] ), // Auto-drafts are allowed to have empty post_names, so we need to explicitly set it.
    758         );
    759         $r = wp_insert_post( wp_slash( $args ), true );
     765        $r = wp_insert_post( wp_slash( $postarr ), true );
    760766        remove_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
    761767
     
    786792        }
    787793
    788         $params = wp_array_slice_assoc(
    789             array_merge(
    790                 array(
    791                     'post_type' => '',
    792                     'post_title' => '',
    793                 ),
    794                 wp_unslash( $_POST['params'] )
     794        $params = wp_unslash( $_POST['params'] );
     795        $illegal_params = array_diff( array_keys( $params ), array( 'post_type', 'post_title' ) );
     796        if ( ! empty( $illegal_params ) ) {
     797            wp_send_json_error( 'illegal_params', 400 );
     798        }
     799
     800        $params = array_merge(
     801            array(
     802                'post_type' => '',
     803                'post_title' => '',
    795804            ),
    796             array( 'post_type', 'post_title' )
     805            $params
    797806        );
    798807
     
    11401149        if ( ! empty( $post_ids ) ) {
    11411150            foreach ( $post_ids as $post_id ) {
    1142                 wp_publish_post( $post_id );
     1151                // Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
     1152                wp_update_post( array( 'ID' => $post_id, 'post_status' => 'publish' ) );
    11431153            }
    11441154        }
Note: See TracChangeset for help on using the changeset viewer.