WordPress.org

Make WordPress Core

Ticket #21963: 21963.8.diff

File 21963.8.diff, 18.1 KB (added by wonderboymusic, 6 years ago)
  • src/wp-includes/post.php

     
    29272927                'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    29282928                'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    29292929                'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    2930                 'post_content' => '', 'post_title' => '');
     2930                'post_content' => '', 'post_title' => '', 'context' => '');
    29312931
    29322932        $postarr = wp_parse_args($postarr, $defaults);
    29332933
     
    29682968                $post_name = $postarr['post_name'];
    29692969        }
    29702970
    2971         $maybe_empty = ! $post_content && ! $post_title && ! $post_excerpt && post_type_supports( $post_type, 'editor' )
    2972                 && post_type_supports( $post_type, 'title' ) && post_type_supports( $post_type, 'excerpt' );
     2971        $maybe_empty = 'attachment' !== $post_type
     2972                && ! $post_content && ! $post_title && ! $post_excerpt
     2973                && post_type_supports( $post_type, 'editor' )
     2974                && post_type_supports( $post_type, 'title' )
     2975                && post_type_supports( $post_type, 'excerpt' );
    29732976
    29742977        /**
    29752978         * Filter whether the post should be considered "empty".
     
    29962999        }
    29973000
    29983001        $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
     3002        if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
     3003                $post_status = 'inherit';
     3004        }
    29993005
    30003006        if ( ! empty( $postarr['post_category'] ) ) {
    30013007                $post_category = array_filter( $postarr['post_category'] ); // Filter out empty terms
     
    30723078                $post_modified_gmt = $post_date_gmt;
    30733079        }
    30743080
    3075         if ( 'publish' == $post_status ) {
    3076                 $now = gmdate('Y-m-d H:i:59');
    3077                 if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
    3078                         $post_status = 'future';
     3081        if ( 'attachment' !== $post_type ) {
     3082                if ( 'publish' == $post_status ) {
     3083                        $now = gmdate('Y-m-d H:i:59');
     3084                        if ( mysql2date('U', $post_date_gmt, false) > mysql2date('U', $now, false) ) {
     3085                                $post_status = 'future';
     3086                        }
     3087                } elseif( 'future' == $post_status ) {
     3088                        $now = gmdate('Y-m-d H:i:59');
     3089                        if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
     3090                                $post_status = 'publish';
     3091                        }
    30793092                }
    3080         } elseif( 'future' == $post_status ) {
    3081                 $now = gmdate('Y-m-d H:i:59');
    3082                 if ( mysql2date('U', $post_date_gmt, false) <= mysql2date('U', $now, false) ) {
    3083                         $post_status = 'publish';
    3084                 }
    30853093        }
    30863094
    30873095        if ( empty( $postarr['comment_status'] ) ) {
     
    31353143
    31363144        $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
    31373145
     3146        // don't unslash
     3147        $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
     3148
    31383149        // expected_slashed (everything!)
    3139         $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', 'guid' );
     3150        $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' );
    31403151
    3141         /**
    3142          * Filter slashed post data just before it is inserted into the database.
    3143          *
    3144          * @since 2.7.0
    3145          *
    3146          * @param array $data    Array of slashed post data.
    3147          * @param array $postarr Array of sanitized, but otherwise unmodified post data.
    3148          */
    3149         $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3152        if ( 'attachment' === $post_type ) {
     3153                /**
     3154                 * Filter attachment post data before it is updated in or added to the database.
     3155                 *
     3156                 * @since 3.9.0
     3157                 *
     3158                 * @param array $data    An array of sanitized attachment post data.
     3159                 * @param array $postarr An array of unsanitized attachment post data.
     3160                 */
     3161                $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
     3162        } else {
     3163                /**
     3164                 * Filter slashed post data just before it is inserted into the database.
     3165                 *
     3166                 * @since 2.7.0
     3167                 *
     3168                 * @param array $data    An array of slashed post data.
     3169                 * @param array $postarr An array of sanitized, but otherwise unmodified post data.
     3170                 */
     3171                $data = apply_filters( 'wp_insert_post_data', $data, $postarr );
     3172        }
    31503173        $data = wp_unslash( $data );
    31513174        $where = array( 'ID' => $post_ID );
    31523175
     
    31683191                        }
    31693192                }
    31703193        } else {
    3171                 if ( isset( $postarr['post_mime_type'] ) ) {
    3172                         $data['post_mime_type'] = wp_unslash( $postarr['post_mime_type'] ); // This isn't in the update
    3173                 }
    31743194                // If there is a suggested ID, use it if not already present
    31753195                if ( ! empty( $import_id ) ) {
    31763196                        $import_id = (int) $import_id;
     
    31913211                $where = array( 'ID' => $post_ID );
    31923212        }
    31933213
    3194         if ( empty($data['post_name']) && !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3195                 $data['post_name'] = sanitize_title($data['post_title'], $post_ID);
     3214        if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
     3215                $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
    31963216                $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    31973217        }
    31983218
     
    32233243        if ( ! $update && '' == $current_guid ) {
    32243244                $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
    32253245        }
     3246       
     3247        if ( 'attachment' === $postarr['post_type'] ) {
     3248                if ( ! empty( $postarr['file'] ) ) {
     3249                        update_attached_file( $post_ID, $postarr['file'] );
     3250                }
     3251
     3252                if ( ! empty( $postarr['context'] ) ) {
     3253                        add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
     3254                }
     3255        }
     3256
    32263257        clean_post_cache( $post_ID );
    32273258
    32283259        $post = get_post( $post_ID );
     
    32403271                update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
    32413272        }
    32423273
    3243         wp_transition_post_status($data['post_status'], $previous_status, $post);
     3274        if ( 'attachment' !== $postarr['post_type'] ) {
     3275                wp_transition_post_status( $data['post_status'], $previous_status, $post );
     3276        } else {
     3277                if ( $update ) {
     3278                        /**
     3279                         * Fires once an existing attachment has been updated.
     3280                         *
     3281                         * @since 2.0.0
     3282                         *
     3283                         * @param int $post_ID Attachment ID.
     3284                         */
     3285                        do_action( 'edit_attachment', $post_ID );
     3286                } else {
    32443287
     3288                        /**
     3289                         * Fires once an attachment has been added.
     3290                         *
     3291                         * @since 2.0.0
     3292                         *
     3293                         * @param int $post_ID Attachment ID.
     3294                         */
     3295                        do_action( 'add_attachment', $post_ID );
     3296                }
     3297
     3298                return $post_ID;
     3299        }
     3300
    32453301        if ( $update ) {
    32463302                /**
    32473303                 * Fires once an existing post has been updated.
     
    44494505/**
    44504506 * Insert an attachment.
    44514507 *
    4452  * If you set the 'ID' in the $object parameter, it will mean that you are
     4508 * If you set the 'ID' in the $args parameter, it will mean that you are
    44534509 * updating and attempt to update the attachment. You can also set the
    44544510 * attachment name or title by setting the key 'post_name' or 'post_title'.
    44554511 *
     
    44604516 * comments are allowed. You can close them manually or keep them open by
    44614517 * setting the value for the 'comment_status' key.
    44624518 *
    4463  * The $object parameter can have the following:
    4464  *     'post_status'   - Default is 'draft'. Can not be overridden, set the same as parent post.
    4465  *     'post_type'     - Default is 'post', will be set to attachment. Can not override.
    4466  *     'post_author'   - Default is current user ID. The ID of the user, who added the attachment.
    4467  *     'ping_status'   - Default is the value in default ping status option. Whether the attachment
    4468  *                       can accept pings.
    4469  *     'post_parent'   - Default is 0. Can use $parent parameter or set this for the post it belongs
    4470  *                       to, if any.
    4471  *     'menu_order'    - Default is 0. The order it is displayed.
    4472  *     'to_ping'       - Whether to ping.
    4473  *     'pinged'        - Default is empty string.
    4474  *     'post_password' - Default is empty string. The password to access the attachment.
    4475  *     'guid'          - Global Unique ID for referencing the attachment.
    4476  *     'post_content_filtered' - Attachment post content filtered.
    4477  *     'post_excerpt'  - Attachment excerpt.
    4478  *
    44794519 * @since 2.0.0
    4480  * @uses $wpdb
    44814520 *
    4482  * @param string|array $object Arguments to override defaults.
    4483  * @param string $file Optional filename.
    4484  * @param int $parent Parent post ID.
     4521 * @see wp_insert_post()
     4522 *
     4523 * @param string|array $args   Arguments for inserting an attachment.
     4524 * @param string       $file   Optional. Filename.
     4525 * @param int          $parent Optional. Parent post ID.
    44854526 * @return int Attachment ID.
    44864527 */
    4487 function wp_insert_attachment($object, $file = false, $parent = 0) {
    4488         global $wpdb;
     4528function wp_insert_attachment( $args, $file = false, $parent = 0 ) {
     4529        $defaults = array(
     4530                'file'        => $file,
     4531                'post_parent' => $parent
     4532        );
     4533        $data = wp_parse_args( $args, $defaults );
    44894534
    4490         $user_id = get_current_user_id();
     4535        $data['post_type'] = 'attachment';
    44914536
    4492         $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id,
    4493                 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
    4494                 'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
    4495                 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
    4496                 'post_title' => '', 'post_content' => '', 'context' => '');
    4497 
    4498         $object = wp_parse_args($object, $defaults);
    4499         if ( ! empty( $parent ) ) {
    4500                 $object['post_parent'] = $parent;
    4501         }
    4502         unset( $object[ 'filter' ] );
    4503 
    4504         $object = sanitize_post($object, 'db');
    4505 
    4506         $post_ID = 0;
    4507         $update = false;
    4508         $guid = $object['guid'];
    4509 
    4510         // Are we updating or creating?
    4511         if ( ! empty( $object['ID'] ) ) {
    4512                 $update = true;
    4513                 $post_ID = (int) $object['ID'];
    4514 
    4515                 // wp_insert_post() checks for the existence of this post....
    4516         }
    4517 
    4518         $post_type = 'attachment';
    4519 
    4520         $post_title = $object['post_title'];
    4521         $post_content = $object['post_content'];
    4522         $post_excerpt = $object['post_excerpt'];
    4523         if ( isset( $object['post_name'] ) ) {
    4524                 $post_name = $object['post_name'];
    4525         }
    4526 
    4527         // wp_insert_post() checks $maybe_empty
    4528 
    4529         $post_status = $object['post_status'];
    4530         if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
    4531                 $post_status = 'inherit';
    4532         }
    4533 
    4534         if ( ! empty( $object['post_category'] ) ) {
    4535                 $post_category = array_filter( $object['post_category'] ); // Filter out empty terms
    4536         }
    4537 
    4538         // Make sure we set a valid category.
    4539         if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
    4540                 // ironically, the default post_type for this function is 'post'
    4541                 // as such, this should be probably have the same checks as wp_insert_post(),
    4542                 // since all 'post's require a category. BUT, you can't override the post_type, because the
    4543                 // variable is explicitly set.
    4544                 $post_category = array();
    4545         }
    4546 
    4547         // Create a valid post name.
    4548         if ( empty( $post_name ) ) {
    4549                 $post_name = sanitize_title($post_title);
    4550         } else {
    4551                 // missing check from wp_insert_post on update:
    4552                 // "On updates, we need to check to see if it's using the old, fixed sanitization context."
    4553                 $post_name = sanitize_title( $post_name );
    4554         }
    4555 
    4556         if ( isset( $object['post_parent'] ) ) {
    4557                 $post_parent = (int) $object['post_parent'];
    4558         } else {
    4559                 $post_parent = 0;
    4560         }
    4561 
    4562         if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) {
    4563                 $post_date = current_time( 'mysql' );
    4564         } else {
    4565                 $post_date = $object['post_date'];
    4566         }
    4567 
    4568         // wp_insert_post() validates the date here
    4569 
    4570         if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) {
    4571                 $post_date_gmt = get_gmt_from_date( $post_date );
    4572         } else {
    4573                 $post_date_gmt = $object['post_date_gmt'];
    4574         }
    4575 
    4576         if ( $update || '0000-00-00 00:00:00' == $post_date ) {
    4577                 $post_modified     = current_time( 'mysql' );
    4578                 $post_modified_gmt = current_time( 'mysql', 1 );
    4579         } else {
    4580                 $post_modified     = $post_date;
    4581                 $post_modified_gmt = $post_date_gmt;
    4582         }
    4583 
    4584         // wp_insert_post() does "future" checks
    4585 
    4586         if ( empty( $object['comment_status'] ) ) {
    4587                 if ( $update ) {
    4588                         $comment_status = 'closed';
    4589                 } else {
    4590                         $comment_status = get_option('default_comment_status');
    4591                 }
    4592         } else {
    4593                 $comment_status = $object['comment_status'];
    4594         }
    4595 
    4596         // these variables are needed by compact() later
    4597         $post_content_filtered = $object['post_content_filtered'];
    4598         $post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author'];
    4599         $ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status'];
    4600         $to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : '';
    4601         $pinged = isset( $object['pinged'] ) ? $object['pinged'] : '';
    4602         $import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0;
    4603 
    4604         if ( isset( $object['menu_order'] ) ) {
    4605                 $menu_order = (int) $object['menu_order'];
    4606         } else {
    4607                 $menu_order = 0;
    4608         }
    4609 
    4610         $post_password = isset( $object['post_password'] ) ? $object['post_password'] : '';
    4611 
    4612         // skips the 'wp_insert_post_parent' filter, present in wp_insert_post()
    4613 
    4614         // expected_slashed ($post_name)
    4615         $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
    4616 
    4617         // don't unslash
    4618         $post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : '';
    4619 
    4620         // expected_slashed (everything!)
    4621         $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' );
    4622 
    4623         /**
    4624          * Filter attachment post data before it is updated in or added
    4625          * to the database.
    4626          *
    4627          * @since 3.9.0
    4628          *
    4629          * @param array $data   Array of sanitized attachment post data.
    4630          * @param array $object Array of un-sanitized attachment post data.
    4631          */
    4632         $data = apply_filters( 'wp_insert_attachment_data', $data, $object );
    4633         $data = wp_unslash( $data );
    4634         $where = array( 'ID' => $post_ID );
    4635 
    4636         if ( $update ) {
    4637                 // skips 'pre_post_update' action
    4638                 $wpdb->update( $wpdb->posts, $data, $where );
    4639         } else {
    4640                 // If there is a suggested ID, use it if not already present
    4641                 if ( ! empty( $import_id ) ) {
    4642                         $import_id = (int) $import_id;
    4643                         if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
    4644                                 $data['ID'] = $import_id;
    4645                         }
    4646                 }
    4647 
    4648                 $wpdb->insert( $wpdb->posts, $data );
    4649                 $post_ID = (int) $wpdb->insert_id;
    4650                 $where = array( 'ID' => $post_ID );
    4651         }
    4652 
    4653         if ( empty( $data['post_name'] ) ) {
    4654                 $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
    4655                 $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
    4656         }
    4657 
    4658         if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) {
    4659                 wp_set_post_categories( $post_ID, $post_category );
    4660         }
    4661 
    4662         if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) {
    4663                 wp_set_post_tags( $post_ID, $object['tags_input'] );
    4664         }
    4665 
    4666         // new-style support for all custom taxonomies
    4667         if ( ! empty( $object['tax_input'] ) ) {
    4668                 foreach ( $object['tax_input'] as $taxonomy => $tags ) {
    4669                         $taxonomy_obj = get_taxonomy($taxonomy);
    4670                         if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical.
    4671                                 $tags = array_filter($tags);
    4672                         }
    4673                         if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
    4674                                 wp_set_post_terms( $post_ID, $tags, $taxonomy );
    4675                         }
    4676                 }
    4677         }
    4678 
    4679         if ( $file ) {
    4680                 update_attached_file( $post_ID, $file );
    4681         }
    4682 
    4683         // wp_insert_post() fills in guid if it is empty
    4684 
    4685         clean_post_cache( $post_ID );
    4686 
    4687         if ( ! empty( $object['context'] ) ) {
    4688                 add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true );
    4689         }
    4690 
    4691         // skips wp_transition_post_status
    4692 
    4693         // the actions completely diverge from wp_insert_post()
    4694 
    4695         if ( $update ) {
    4696                 /**
    4697                  * Fires once an existing attachment has been updated.
    4698                  *
    4699                  * @since 2.0.0
    4700                  *
    4701                  * @param int $post_ID Attachment ID.
    4702                  */
    4703                 do_action( 'edit_attachment', $post_ID );
    4704         } else {
    4705 
    4706                 /**
    4707                  * Fires once an attachment has been added.
    4708                  *
    4709                  * @since 2.0.0
    4710                  *
    4711                  * @param int $post_ID Attachment ID.
    4712                  */
    4713                 do_action( 'add_attachment', $post_ID );
    4714         }
    4715 
    4716         return $post_ID;
     4537        return wp_insert_post( $data );
    47174538}
    47184539
    47194540/**
     
    57995620                update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
    58005621        }
    58015622}
     5623
     5624
  • tests/phpunit/tests/media.php

     
    148148                $this->assertEquals( '', $prepped['mime'] );
    149149                $this->assertEquals( '', $prepped['type'] );
    150150                $this->assertEquals( '', $prepped['subtype'] );
    151                 $this->assertEquals( '', $prepped['url'] );
     151                // #21963, there will be a guid always, so there will be a URL
     152                $this->assertNotEquals( '', $prepped['url'] );
    152153                $this->assertEquals( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
    153154
    154155                // Fake a mime
  • tests/phpunit/tests/post/attachments.php

     
    231231                $this->assertFalse( empty( $guid ) );
    232232        }
    233233
     234        /**
     235         * @ticket 21963
     236         */
     237        function test_update_attachment_fields() {
     238                $filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
     239                $contents = file_get_contents($filename);
     240
     241                $upload = wp_upload_bits( basename( $filename ), null, $contents );
     242                $this->assertTrue( empty( $upload['error'] ) );
     243
     244                $id = $this->_make_attachment( $upload );
     245
     246                $attached_file = get_post_meta( $id, '_wp_attached_file', true );
     247
     248                $post = get_post( $id, ARRAY_A );
     249
     250                $post['post_title'] = 'title';
     251                $post['post_excerpt'] = 'caption';
     252                $post['post_content'] = 'description';
     253
     254                wp_update_post( $post );
     255
     256                // Make sure the update didn't remove the attached file.
     257                $this->assertEquals( $attached_file, get_post_meta( $id, '_wp_attached_file', true ) );
     258        }
     259
    234260}