Make WordPress Core

Ticket #44416: 44416-wp_insert_post-rework.diff

File 44416-wp_insert_post-rework.diff, 31.4 KB (added by desrosj, 7 years ago)

More complex rework of wp_insert_post()

  • src/wp-includes/class-wp-comment-query.php

     
    635635                $number = absint( $this->query_vars['number'] );
    636636                $offset = absint( $this->query_vars['offset'] );
    637637                $paged  = absint( $this->query_vars['paged'] );
     638                $limits = '';
    638639
    639640                if ( ! empty( $number ) ) {
    640641                        if ( $offset ) {
     
    819820                        $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
    820821                }
    821822
    822                 $join = '';
     823                $join    = '';
     824                $groupby = '';
    823825
    824826                if ( $join_posts_table ) {
    825827                        $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
  • src/wp-includes/class-wp-xmlrpc-server.php

     
    37003700                /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
    37013701                do_action( 'xmlrpc_call', 'wp.editComment' );
    37023702
     3703                $comment = array(
     3704                        'comment_ID' => $comment_ID,
     3705                );
     3706
    37033707                if ( isset( $content_struct['status'] ) ) {
    37043708                        $statuses = get_comment_statuses();
    37053709                        $statuses = array_keys( $statuses );
     
    37073711                        if ( ! in_array( $content_struct['status'], $statuses ) ) {
    37083712                                return new IXR_Error( 401, __( 'Invalid comment status.' ) );
    37093713                        }
    3710                         $comment_approved = $content_struct['status'];
     3714                        $comment['comment_approved'] = $content_struct['status'];
    37113715                }
    37123716
    37133717                // Do some timestamp voodoo
    37143718                if ( ! empty( $content_struct['date_created_gmt'] ) ) {
    37153719                        // We know this is supposed to be GMT, so we're going to slap that Z on there by force
    3716                         $dateCreated      = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
    3717                         $comment_date     = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
    3718                         $comment_date_gmt = iso8601_to_datetime( $dateCreated, 'GMT' );
     3720                        $dateCreated                 = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
     3721                        $comment['comment_date']     = get_date_from_gmt( iso8601_to_datetime( $dateCreated ) );
     3722                        $comment['comment_date_gmt'] = iso8601_to_datetime( $dateCreated, 'GMT' );
    37193723                }
    37203724
    37213725                if ( isset( $content_struct['content'] ) ) {
    3722                         $comment_content = $content_struct['content'];
     3726                        $comment['comment_content'] = $content_struct['content'];
    37233727                }
    37243728
    37253729                if ( isset( $content_struct['author'] ) ) {
    3726                         $comment_author = $content_struct['author'];
     3730                        $comment['comment_author'] = $content_struct['author'];
    37273731                }
    37283732
    37293733                if ( isset( $content_struct['author_url'] ) ) {
    3730                         $comment_author_url = $content_struct['author_url'];
     3734                        $comment['comment_author_url'] = $content_struct['author_url'];
    37313735                }
    37323736
    37333737                if ( isset( $content_struct['author_email'] ) ) {
    3734                         $comment_author_email = $content_struct['author_email'];
     3738                        $comment['comment_author_email'] = $content_struct['author_email'];
    37353739                }
    37363740
    3737                 // We've got all the data -- post it:
    3738                 $comment = compact( 'comment_ID', 'comment_content', 'comment_approved', 'comment_date', 'comment_date_gmt', 'comment_author', 'comment_author_email', 'comment_author_url' );
    3739 
    37403741                $result = wp_update_comment( $comment );
    37413742                if ( is_wp_error( $result ) ) {
    37423743                        return new IXR_Error( 500, $result->get_error_message() );
     
    52615262                // Only use a password if one was given.
    52625263                if ( isset( $content_struct['wp_password'] ) ) {
    52635264                        $post_password = $content_struct['wp_password'];
     5265                } else {
     5266                        $post_password = '';
    52645267                }
    52655268
    52665269                // Only set a post parent if one was provided.
    52675270                if ( isset( $content_struct['wp_page_parent_id'] ) ) {
    52685271                        $post_parent = $content_struct['wp_page_parent_id'];
     5272                } else {
     5273                        $post_parent = 0;
    52695274                }
    52705275
    52715276                // Only set the menu_order if it was provided.
    52725277                if ( isset( $content_struct['wp_page_order'] ) ) {
    52735278                        $menu_order = $content_struct['wp_page_order'];
     5279                } else {
     5280                        $menu_order = 0;
    52745281                }
    52755282
    52765283                $post_author = $user->ID;
     
    55995606
    56005607                $this->escape( $postdata );
    56015608
    5602                 $ID            = $postdata['ID'];
    5603                 $post_content  = $postdata['post_content'];
    5604                 $post_title    = $postdata['post_title'];
    5605                 $post_excerpt  = $postdata['post_excerpt'];
    5606                 $post_password = $postdata['post_password'];
    5607                 $post_parent   = $postdata['post_parent'];
    5608                 $post_type     = $postdata['post_type'];
    5609                 $menu_order    = $postdata['menu_order'];
     5609                $ID             = $postdata['ID'];
     5610                $post_content   = $postdata['post_content'];
     5611                $post_title     = $postdata['post_title'];
     5612                $post_excerpt   = $postdata['post_excerpt'];
     5613                $post_password  = $postdata['post_password'];
     5614                $post_parent    = $postdata['post_parent'];
     5615                $post_type      = $postdata['post_type'];
     5616                $menu_order     = $postdata['menu_order'];
     5617                $ping_status    = $postdata['ping_status'];
     5618                $comment_status = $postdata['comment_status'];
    56105619
    56115620                // Let WordPress manage slug if none was provided.
    56125621                $post_name = $postdata['post_name'];
  • src/wp-includes/comment.php

     
    30943094 */
    30953095function wp_handle_comment_submission( $comment_data ) {
    30963096
    3097         $comment_post_ID = $comment_parent = 0;
     3097        $comment_post_ID = $comment_parent = $user_ID = 0;
    30983098        $comment_author  = $comment_author_email = $comment_author_url = $comment_content = null;
    30993099
    31003100        if ( isset( $comment_data['comment_post_ID'] ) ) {
  • src/wp-includes/post.php

     
    33453345
    33463346        unset( $postarr['filter'] );
    33473347
    3348         $postarr = sanitize_post( $postarr, 'db' );
     3348        $postarr           = sanitize_post( $postarr, 'db' );
     3349        $sanitized_postarr = $postarr;
    33493350
    33503351        // Are we updating or creating?
    3351         $post_ID = 0;
    3352         $update  = false;
    3353         $guid    = $postarr['guid'];
     3352        $update = false;
    33543353
    33553354        if ( ! empty( $postarr['ID'] ) ) {
    33563355                $update = true;
    33573356
    33583357                // Get the post ID and GUID.
    3359                 $post_ID     = $postarr['ID'];
    3360                 $post_before = get_post( $post_ID );
     3358                $post_before = get_post( $postarr['ID'] );
    33613359                if ( is_null( $post_before ) ) {
    33623360                        if ( $wp_error ) {
    33633361                                return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
     
    33653363                        return 0;
    33663364                }
    33673365
    3368                 $guid            = get_post_field( 'guid', $post_ID );
    3369                 $previous_status = get_post_field( 'post_status', $post_ID );
     3366                $postarr['guid'] = get_post_field( 'guid', $postarr['ID'] );
     3367                $previous_status = get_post_field( 'post_status', $postarr['ID'] );
    33703368        } else {
    33713369                $previous_status = 'new';
    33723370        }
    33733371
    3374         $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
     3372        if ( empty( $postarr['post_type'] ) ) {
     3373                $postarr['post_type'] = 'post';
     3374        }
    33753375
    3376         $post_title   = $postarr['post_title'];
    3377         $post_content = $postarr['post_content'];
    3378         $post_excerpt = $postarr['post_excerpt'];
    3379         if ( isset( $postarr['post_name'] ) ) {
    3380                 $post_name = $postarr['post_name'];
    3381         } elseif ( $update ) {
     3376        if ( ! isset( $postarr['post_name'] ) && $update ) {
    33823377                // For an update, don't modify the post_name if it wasn't supplied as an argument.
    3383                 $post_name = $post_before->post_name;
     3378                $postarr['post_name'] = $post_before->post_name;
    33843379        }
    33853380
    3386         $maybe_empty = 'attachment' !== $post_type
    3387                 && ! $post_content && ! $post_title && ! $post_excerpt
    3388                 && post_type_supports( $post_type, 'editor' )
    3389                 && post_type_supports( $post_type, 'title' )
    3390                 && post_type_supports( $post_type, 'excerpt' );
     3381        $maybe_empty = 'attachment' !== $postarr['post_type']
     3382                && ! $postarr['post_content'] && ! $postarr['post_title'] && ! $postarr['post_excerpt']
     3383                && post_type_supports( $postarr['post_type'], 'editor' )
     3384                && post_type_supports( $postarr['post_type'], 'title' )
     3385                && post_type_supports( $postarr['post_type'], 'excerpt' );
    33913386
    33923387        /**
    33933388         * Filters whether the post should be considered "empty".
     
    34133408                }
    34143409        }
    34153410
    3416         $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
    3417         if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
    3418                 $post_status = 'inherit';
     3411        if ( empty( $postarr['post_status'] ) ) {
     3412                $postarr['post_status'] = 'draft';
    34193413        }
    34203414
     3415        if ( 'attachment' === $postarr['post_type'] && ! in_array( $postarr['post_status'], array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
     3416                $postarr['post_status'] = 'inherit';
     3417        }
     3418
    34213419        if ( ! empty( $postarr['post_category'] ) ) {
    34223420                // Filter out empty terms.
    3423                 $post_category = array_filter( $postarr['post_category'] );
     3421                $postarr['post_category'] = array_filter( $postarr['post_category'] );
    34243422        }
    34253423
    34263424        // Make sure we set a valid category.
    3427         if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
     3425        if ( empty( $postarr['post_category'] ) || 0 == count( $postarr['post_category'] ) || ! is_array( $postarr['post_category'] ) ) {
    34283426                // 'post' requires at least one category.
    3429                 if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
    3430                         $post_category = array( get_option( 'default_category' ) );
     3427                if ( 'post' == $postarr['post_type'] && 'auto-draft' != $postarr['post_status'] ) {
     3428                        $postarr['post_category'] = array( get_option( 'default_category' ) );
    34313429                } else {
    3432                         $post_category = array();
     3430                        $postarr['post_category'] = array();
    34333431                }
    34343432        }
    34353433
     
    34383436         *
    34393437         * For new posts check the primitive capability, for updates check the meta capability.
    34403438         */
    3441         $post_type_object = get_post_type_object( $post_type );
     3439        $post_type_object = get_post_type_object( $postarr['post_type'] );
    34423440
    3443         if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
    3444                 $post_name = '';
    3445         } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) {
    3446                 $post_name = '';
     3441        if ( ! $update && 'pending' === $postarr['post_status'] && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
     3442                $postarr['post_name'] = '';
     3443        } elseif ( $update && 'pending' === $postarr['post_status'] && ! current_user_can( 'publish_post', $postarr['ID'] ) ) {
     3444                $postarr['post_name'] = '';
    34473445        }
    34483446
    34493447        /*
     
    34503448         * Create a valid post name. Drafts and pending posts are allowed to have
    34513449         * an empty post name.
    34523450         */
    3453         if ( empty( $post_name ) ) {
    3454                 if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3455                         $post_name = sanitize_title( $post_title );
     3451        if ( empty( $postarr['post_name'] ) ) {
     3452                if ( ! in_array( $postarr['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3453                        $postarr['post_name'] = sanitize_title( $postarr['post_title'] );
    34563454                } else {
    3457                         $post_name = '';
     3455                        $postarr['post_name'] = '';
    34583456                }
    34593457        } else {
    34603458                // On updates, we need to check to see if it's using the old, fixed sanitization context.
    3461                 $check_name = sanitize_title( $post_name, '', 'old-save' );
    3462                 if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
    3463                         $post_name = $check_name;
     3459                $check_name = sanitize_title( $postarr['post_name'], '', 'old-save' );
     3460                if ( $update && strtolower( urlencode( $postarr['post_name'] ) ) == $check_name && get_post_field( 'post_name', $postarr['ID'] ) == $check_name ) {
     3461                        $postarr['post_name'] = $check_name;
    34643462                } else { // new post, or slug has changed.
    3465                         $post_name = sanitize_title( $post_name );
     3463                        $postarr['post_name'] = sanitize_title( $postarr['post_name'] );
    34663464                }
    34673465        }
    34683466
     
    34723470         */
    34733471        if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
    34743472                if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
    3475                         $post_date = current_time( 'mysql' );
     3473                        $postarr['post_date'] = current_time( 'mysql' );
    34763474                } else {
    3477                         $post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
     3475                        $postarr['post_date'] = get_date_from_gmt( $postarr['post_date_gmt'] );
    34783476                }
    3479         } else {
    3480                 $post_date = $postarr['post_date'];
    34813477        }
    34823478
    34833479        // Validate the date.
    3484         $mm         = substr( $post_date, 5, 2 );
    3485         $jj         = substr( $post_date, 8, 2 );
    3486         $aa         = substr( $post_date, 0, 4 );
    3487         $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
     3480        $mm         = substr( $postarr['post_date'], 5, 2 );
     3481        $jj         = substr( $postarr['post_date'], 8, 2 );
     3482        $aa         = substr( $postarr['post_date'], 0, 4 );
     3483        $valid_date = wp_checkdate( $mm, $jj, $aa, $postarr['post_date'] );
    34883484        if ( ! $valid_date ) {
    34893485                if ( $wp_error ) {
    34903486                        return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
     
    34943490        }
    34953491
    34963492        if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
    3497                 if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3498                         $post_date_gmt = get_gmt_from_date( $post_date );
     3493                if ( ! in_array( $postarr['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3494                        $postarr['post_date_gmt'] = get_gmt_from_date( $postarr['post_date'] );
    34993495                } else {
    3500                         $post_date_gmt = '0000-00-00 00:00:00';
     3496                        $postarr['post_date_gmt'] = '0000-00-00 00:00:00';
    35013497                }
    3502         } else {
    3503                 $post_date_gmt = $postarr['post_date_gmt'];
    35043498        }
    35053499
    3506         if ( $update || '0000-00-00 00:00:00' == $post_date ) {
    3507                 $post_modified     = current_time( 'mysql' );
    3508                 $post_modified_gmt = current_time( 'mysql', 1 );
     3500        if ( $update || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
     3501                $postarr['post_modified']     = current_time( 'mysql' );
     3502                $postarr['post_modified_gmt'] = current_time( 'mysql', 1 );
    35093503        } else {
    3510                 $post_modified     = $post_date;
    3511                 $post_modified_gmt = $post_date_gmt;
     3504                $postarr['post_modified']     = $postarr['post_date'];
     3505                $postarr['post_modified_gmt'] = $postarr['post_date_gmt'];
    35123506        }
    35133507
    3514         if ( 'attachment' !== $post_type ) {
    3515                 if ( 'publish' == $post_status ) {
     3508        if ( 'attachment' !== $postarr['post_type'] ) {
     3509                if ( 'publish' == $postarr['post_status'] ) {
    35163510                        $now = gmdate( 'Y-m-d H:i:59' );
    3517                         if ( mysql2date( 'U', $post_date_gmt, false ) > mysql2date( 'U', $now, false ) ) {
    3518                                 $post_status = 'future';
     3511                        if ( mysql2date( 'U', $postarr['post_date_gmt'], false ) > mysql2date( 'U', $now, false ) ) {
     3512                                $postarr['post_status'] = 'future';
    35193513                        }
    3520                 } elseif ( 'future' == $post_status ) {
     3514                } elseif ( 'future' == $postarr['post_status'] ) {
    35213515                        $now = gmdate( 'Y-m-d H:i:59' );
    3522                         if ( mysql2date( 'U', $post_date_gmt, false ) <= mysql2date( 'U', $now, false ) ) {
    3523                                 $post_status = 'publish';
     3516                        if ( mysql2date( 'U', $postarr['post_date_gmt'], false ) <= mysql2date( 'U', $now, false ) ) {
     3517                                $postarr['post_status'] = 'publish';
    35243518                        }
    35253519                }
    35263520        }
     
    35283522        // Comment status.
    35293523        if ( empty( $postarr['comment_status'] ) ) {
    35303524                if ( $update ) {
    3531                         $comment_status = 'closed';
     3525                        $postarr['comment_status'] = 'closed';
    35323526                } else {
    3533                         $comment_status = get_default_comment_status( $post_type );
     3527                        $postarr['comment_status'] = get_default_comment_status( $postarr['post_type'] );
    35343528                }
     3529        }
     3530
     3531        if ( ! isset( $postarr['post_author'] ) ) {
     3532                $postarr['post_author'] = $user_id;
     3533        }
     3534
     3535        if ( empty( $postarr['ping_status'] ) ) {
     3536                $postarr['ping_status'] = get_default_comment_status( $postarr['post_type'], 'pingback' );
     3537        }
     3538
     3539        if ( isset( $postarr['to_ping'] ) ) {
     3540                $postarr['to_ping'] = sanitize_trackback_urls( $postarr['to_ping'] );
    35353541        } else {
    3536                 $comment_status = $postarr['comment_status'];
     3542                $postarr['to_ping'];
    35373543        }
    35383544
    3539         // These variables are needed by compact() later.
    3540         $post_content_filtered = $postarr['post_content_filtered'];
    3541         $post_author           = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
    3542         $ping_status           = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
    3543         $to_ping               = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
    3544         $pinged                = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
    3545         $import_id             = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
    3546 
    35473545        /*
    35483546         * The 'wp_insert_post_parent' filter expects all variables to be present.
    35493547         * Previously, these variables would have already been extracted
    35503548         */
    35513549        if ( isset( $postarr['menu_order'] ) ) {
    3552                 $menu_order = (int) $postarr['menu_order'];
     3550                $postarr['menu_order'] = (int) $postarr['menu_order'];
    35533551        } else {
    3554                 $menu_order = 0;
     3552                $postarr['menu_order'] = 0;
    35553553        }
    35563554
    3557         $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
    3558         if ( 'private' == $post_status ) {
    3559                 $post_password = '';
     3555        if ( 'private' == $postarr['post_status'] ) {
     3556                $postarr['post_password'] = '';
    35603557        }
    35613558
    35623559        if ( isset( $postarr['post_parent'] ) ) {
    3563                 $post_parent = (int) $postarr['post_parent'];
     3560                $postarr['post_parent'] = (int) $postarr['post_parent'];
    35643561        } else {
    3565                 $post_parent = 0;
     3562                $postarr['post_parent'] = 0;
    35663563        }
    35673564
    35683565        /**
     
    35703567         *
    35713568         * @since 3.1.0
    35723569         *
    3573          * @param int   $post_parent Post parent ID.
    3574          * @param int   $post_ID     Post ID.
    3575          * @param array $new_postarr Array of parsed post data.
    3576          * @param array $postarr    Array of sanitized, but otherwise unmodified post data.
     3570         * @param int   $post_parent       Post parent ID.
     3571         * @param int   $post_ID           Post ID.
     3572         * @param array $postarr          Array of parsed post data.
     3573         * @param array $sanitized_postarr Array of sanitized, but otherwise unmodified post data.
    35773574         */
    3578         $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
     3575        $postarr['post_parent'] = apply_filters( 'wp_insert_post_parent', $postarr['post_parent'], $postarr['ID'], $postarr, $sanitized_postarr );
    35793576
    35803577        /*
    35813578         * If the post is being untrashed and it has a desired slug stored in post meta,
    35823579         * reassign it.
    35833580         */
    3584         if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
    3585                 $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
     3581        if ( 'trash' === $previous_status && 'trash' !== $postarr['post_status'] ) {
     3582                $desired_post_slug = get_post_meta( $postarr['ID'], '_wp_desired_post_slug', true );
    35863583                if ( $desired_post_slug ) {
    3587                         delete_post_meta( $post_ID, '_wp_desired_post_slug' );
    3588                         $post_name = $desired_post_slug;
     3584                        delete_post_meta( $postarr['ID'], '_wp_desired_post_slug' );
     3585                        $postarr['post_name'] = $desired_post_slug;
    35893586                }
    35903587        }
    35913588
    35923589        // If a trashed post has the desired slug, change it and let this post have it.
    3593         if ( 'trash' !== $post_status && $post_name ) {
    3594                 wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
     3590        if ( 'trash' !== $postarr['post_status'] && $postarr['post_name'] ) {
     3591                wp_add_trashed_suffix_to_post_name_for_trashed_posts( $postarr['post_name'], $postarr['ID'] );
    35953592        }
    35963593
    35973594        // When trashing an existing post, change its slug to allow non-trashed posts to use it.
    3598         if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {
    3599                 $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
     3595        if ( 'trash' === $postarr['post_status'] && 'trash' !== $previous_status && 'new' !== $previous_status ) {
     3596                $postarr['post_name'] = wp_add_trashed_suffix_to_post_name_for_post( $postarr['ID'] );
    36003597        }
    36013598
    3602         $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
     3599        $postarr['post_name'] = wp_unique_post_slug( $postarr['post_name'], $postarr['ID'], $postarr['post_status'], $postarr['post_type'], $postarr['post_parent'] );
    36033600
    36043601        // Don't unslash.
    3605         $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
     3602        if ( ! isset( $postarr['post_mime_type'] ) ) {
     3603                $postarr['post_mime_type'] = '';
     3604        }
    36063605
    36073606        // Expected_slashed (everything!).
    3608         $data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' );
     3607        $data = wp_array_slice_assoc(
     3608                $postarr,
     3609                array(
     3610                        'post_author',
     3611                        'post_date',
     3612                        'post_date_gmt',
     3613                        'post_content',
     3614                        'post_content_filtered',
     3615                        'post_title',
     3616                        'post_excerpt',
     3617                        'post_status',
     3618                        'post_type',
     3619                        'comment_status',
     3620                        'ping_status',
     3621                        'post_password',
     3622                        'post_name',
     3623                        'to_ping',
     3624                        'pinged',
     3625                        'post_modified',
     3626                        'post_modified_gmt',
     3627                        'post_parent',
     3628                        'menu_order',
     3629                        'post_mime_type',
     3630                        'guid',
     3631                )
     3632        );
    36093633
    36103634        $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' );
    36113635
     
    36183642                }
    36193643        }
    36203644
    3621         if ( 'attachment' === $post_type ) {
     3645        if ( 'attachment' === $postarr['post_type'] ) {
    36223646                /**
    36233647                 * Filters attachment post data before it is updated in or added to the database.
    36243648                 *
    36253649                 * @since 3.9.0
    36263650                 *
    3627                  * @param array $data    An array of sanitized attachment post data.
    3628                  * @param array $postarr An array of unsanitized attachment post data.
     3651                 * @param array $data              An array of sanitized attachment post data.
     3652                 * @param array $sanitized_postarr An array of unsanitized attachment post data.
    36293653                 */
    3630                 $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
     3654                $data = apply_filters( 'wp_insert_attachment_data', $data, $sanitized_postarr );
    36313655        } else {
    36323656                /**
    36333657                 * Filters slashed post data just before it is inserted into the database.
     
    36343658                 *
    36353659                 * @since 2.7.0
    36363660                 *
    3637                  * @param array $data    An array of slashed post data.
    3638                  * @param array $postarr An array of sanitized, but otherwise unmodified post data.
     3661                 * @param array $data              An array of slashed post data.
     3662                 * @param array $sanitized_postarr An array of sanitized, but otherwise unmodified post data.
    36393663                 */
    3640                 $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3664                $data = apply_filters( 'wp_insert_post_data', $data, $sanitized_postarr );
    36413665        }
    36423666        $data  = wp_unslash( $data );
    3643         $where = array( 'ID' => $post_ID );
     3667        $where = array( 'ID' => $postarr['ID'] );
    36443668
    36453669        if ( $update ) {
    36463670                /**
     
    36513675                 * @param int   $post_ID Post ID.
    36523676                 * @param array $data    Array of unslashed post data.
    36533677                 */
    3654                 do_action( 'pre_post_update', $post_ID, $data );
     3678                do_action( 'pre_post_update', $postarr['ID'], $data );
    36553679                if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
    36563680                        if ( $wp_error ) {
    36573681                                return new WP_Error( 'db_update_error', __( 'Could not update post in the database' ), $wpdb->last_error );
     
    36613685                }
    36623686        } else {
    36633687                // If there is a suggested ID, use it if not already present.
    3664                 if ( ! empty( $import_id ) ) {
    3665                         $import_id = (int) $import_id;
    3666                         if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
    3667                                 $data['ID'] = $import_id;
     3688                if ( ! empty( $postarr['import_id'] ) ) {
     3689                        $postarr['import_id'] = (int) $postarr['import_id'];
     3690                        if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $postarr['import_id'] ) ) ) {
     3691                                $data['ID'] = $postarr['import_id'];
    36683692                        }
    36693693                }
    36703694                if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
     
    36743698                                return 0;
    36753699                        }
    36763700                }
    3677                 $post_ID = (int) $wpdb->insert_id;
     3701                $postarr['ID'] = (int) $wpdb->insert_id;
    36783702
    3679                 // Use the newly generated $post_ID.
    3680                 $where = array( 'ID' => $post_ID );
     3703                // Use the newly generated $postarr['ID'].
     3704                $where = array( 'ID' => $postarr['ID'] );
    36813705        }
    36823706
    36833707        if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3684                 $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent );
     3708                $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $postarr['ID'] ), $postarr['ID'], $data['post_status'], $postarr['post_type'], $postarr['post_parent'] );
    36853709                $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    3686                 clean_post_cache( $post_ID );
     3710                clean_post_cache( $postarr['ID'] );
    36873711        }
    36883712
    3689         if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
    3690                 wp_set_post_categories( $post_ID, $post_category );
     3713        if ( is_object_in_taxonomy( $postarr['post_type'], 'category' ) ) {
     3714                wp_set_post_categories( $postarr['ID'], $postarr['post_category'] );
    36913715        }
    36923716
    3693         if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
    3694                 wp_set_post_tags( $post_ID, $postarr['tags_input'] );
     3717        if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
     3718                wp_set_post_tags( $postarr['ID'], $postarr['tags_input'] );
    36953719        }
    36963720
    36973721        // New-style support for all custom taxonomies.
     
    37093733                                $tags = array_filter( $tags );
    37103734                        }
    37113735                        if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
    3712                                 wp_set_post_terms( $post_ID, $tags, $taxonomy );
     3736                                wp_set_post_terms( $postarr['ID'], $tags, $taxonomy );
    37133737                        }
    37143738                }
    37153739        }
     
    37163740
    37173741        if ( ! empty( $postarr['meta_input'] ) ) {
    37183742                foreach ( $postarr['meta_input'] as $field => $value ) {
    3719                         update_post_meta( $post_ID, $field, $value );
     3743                        update_post_meta( $postarr['ID'], $field, $value );
    37203744                }
    37213745        }
    37223746
    3723         $current_guid = get_post_field( 'guid', $post_ID );
     3747        $current_guid = get_post_field( 'guid', $postarr['ID'] );
    37243748
    37253749        // Set GUID.
    37263750        if ( ! $update && '' == $current_guid ) {
    3727                 $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
     3751                $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $postarr['ID'] ) ), $where );
    37283752        }
    37293753
    37303754        if ( 'attachment' === $postarr['post_type'] ) {
    37313755                if ( ! empty( $postarr['file'] ) ) {
    3732                         update_attached_file( $post_ID, $postarr['file'] );
     3756                        update_attached_file( $postarr['ID'], $postarr['file'] );
    37333757                }
    37343758
    37353759                if ( ! empty( $postarr['context'] ) ) {
    3736                         add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
     3760                        add_post_meta( $postarr['ID'], '_wp_attachment_context', $postarr['context'], true );
    37373761                }
    37383762        }
    37393763
    37403764        // Set or remove featured image.
    37413765        if ( isset( $postarr['_thumbnail_id'] ) ) {
    3742                 $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type;
    3743                 if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
    3744                         if ( wp_attachment_is( 'audio', $post_ID ) ) {
     3766                $thumbnail_support = current_theme_supports( 'post-thumbnails', $postarr['post_type'] ) && post_type_supports( $postarr['post_type'], 'thumbnail' ) || 'revision' === $postarr['post_type'];
     3767                if ( ! $thumbnail_support && 'attachment' === $postarr['post_type'] && $postarr['post_mime_type'] ) {
     3768                        if ( wp_attachment_is( 'audio', $postarr['ID'] ) ) {
    37453769                                $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
    3746                         } elseif ( wp_attachment_is( 'video', $post_ID ) ) {
     3770                        } elseif ( wp_attachment_is( 'video', $postarr['ID'] ) ) {
    37473771                                $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
    37483772                        }
    37493773                }
     
    37513775                if ( $thumbnail_support ) {
    37523776                        $thumbnail_id = intval( $postarr['_thumbnail_id'] );
    37533777                        if ( -1 === $thumbnail_id ) {
    3754                                 delete_post_thumbnail( $post_ID );
     3778                                delete_post_thumbnail( $postarr['ID'] );
    37553779                        } else {
    3756                                 set_post_thumbnail( $post_ID, $thumbnail_id );
     3780                                set_post_thumbnail( $postarr['ID'], $thumbnail_id );
    37573781                        }
    37583782                }
    37593783        }
    37603784
    3761         clean_post_cache( $post_ID );
     3785        clean_post_cache( $postarr['ID'] );
    37623786
    3763         $post = get_post( $post_ID );
     3787        $post = get_post( $postarr['ID'] );
    37643788
    37653789        if ( ! empty( $postarr['page_template'] ) ) {
    37663790                $post->page_template = $postarr['page_template'];
     
    37693793                        if ( $wp_error ) {
    37703794                                return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) );
    37713795                        }
    3772                         update_post_meta( $post_ID, '_wp_page_template', 'default' );
     3796                        update_post_meta( $postarr['ID'], '_wp_page_template', 'default' );
    37733797                } else {
    3774                         update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
     3798                        update_post_meta( $postarr['ID'], '_wp_page_template', $postarr['page_template'] );
    37753799                }
    37763800        }
    37773801
     
    37863810                         *
    37873811                         * @param int $post_ID Attachment ID.
    37883812                         */
    3789                         do_action( 'edit_attachment', $post_ID );
    3790                         $post_after = get_post( $post_ID );
     3813                        do_action( 'edit_attachment', $postarr['ID'] );
     3814                        $post_after = get_post( $postarr['ID'] );
    37913815
    37923816                        /**
    37933817                         * Fires once an existing attachment has been updated.
     
    37983822                         * @param WP_Post $post_after   Post object following the update.
    37993823                         * @param WP_Post $post_before  Post object before the update.
    38003824                         */
    3801                         do_action( 'attachment_updated', $post_ID, $post_after, $post_before );
     3825                        do_action( 'attachment_updated', $postarr['ID'], $post_after, $post_before );
    38023826                } else {
    38033827
    38043828                        /**
     
    38083832                         *
    38093833                         * @param int $post_ID Attachment ID.
    38103834                         */
    3811                         do_action( 'add_attachment', $post_ID );
     3835                        do_action( 'add_attachment', $postarr['ID'] );
    38123836                }
    38133837
    3814                 return $post_ID;
     3838                return $postarr['ID'];
    38153839        }
    38163840
    38173841        if ( $update ) {
     
    38263850                 * @param int     $post_ID Post ID.
    38273851                 * @param WP_Post $post    Post object.
    38283852                 */
    3829                 do_action( "edit_post_{$post->post_type}", $post_ID, $post );
     3853                do_action( "edit_post_{$post->post_type}", $postarr['ID'], $post );
    38303854
    38313855                /**
    38323856                 * Fires once an existing post has been updated.
     
    38363860                 * @param int     $post_ID Post ID.
    38373861                 * @param WP_Post $post    Post object.
    38383862                 */
    3839                 do_action( 'edit_post', $post_ID, $post );
     3863                do_action( 'edit_post', $postarr['ID'], $post );
    38403864
    3841                 $post_after = get_post( $post_ID );
     3865                $post_after = get_post( $postarr['ID'] );
    38423866
    38433867                /**
    38443868                 * Fires once an existing post has been updated.
     
    38493873                 * @param WP_Post $post_after   Post object following the update.
    38503874                 * @param WP_Post $post_before  Post object before the update.
    38513875                 */
    3852                 do_action( 'post_updated', $post_ID, $post_after, $post_before );
     3876                do_action( 'post_updated', $postarr['ID'], $post_after, $post_before );
    38533877        }
    38543878
    38553879        /**
     
    38643888         * @param WP_Post $post    Post object.
    38653889         * @param bool    $update  Whether this is an existing post being updated or not.
    38663890         */
    3867         do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );
     3891        do_action( "save_post_{$post->post_type}", $postarr['ID'], $post, $update );
    38683892
    38693893        /**
    38703894         * Fires once a post has been saved.
     
    38753899         * @param WP_Post $post    Post object.
    38763900         * @param bool    $update  Whether this is an existing post being updated or not.
    38773901         */
    3878         do_action( 'save_post', $post_ID, $post, $update );
     3902        do_action( 'save_post', $postarr['ID'], $post, $update );
    38793903
    38803904        /**
    38813905         * Fires once a post has been saved.
     
    38863910         * @param WP_Post $post    Post object.
    38873911         * @param bool    $update  Whether this is an existing post being updated or not.
    38883912         */
    3889         do_action( 'wp_insert_post', $post_ID, $post, $update );
     3913        do_action( 'wp_insert_post', $postarr['ID'], $post, $update );
    38903914
    3891         return $post_ID;
     3915        return $postarr['ID'];
    38923916}
    38933917
    38943918/**
  • tests/phpunit/tests/dependencies/scripts.php

     
    866866         * @covers ::wp_enqueue_code_editor()
    867867         */
    868868        public function test_wp_enqueue_code_editor_when_generated_array_by_compact_will_be_passed() {
     869                $file                   = '';
    869870                $wp_enqueue_code_editor = wp_enqueue_code_editor( compact( 'file' ) );
    870871                $this->assertNonEmptyMultidimensionalArray( $wp_enqueue_code_editor );
    871872