WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/17/2014 04:53:44 PM (5 years ago)
Author:
wonderboymusic
Message:

Eliminate the use of extract() in wp_insert_attachment().

wp_insert_attachment() and wp_insert_post() are incredibly similar, but have branched logic. I have annotated many places where they diverge.

See #22400.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r28469 r28470  
    44544454
    44554455    $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id,
    4456         'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'post_title' => '',
    4457         'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '', 'post_content' => '',
    4458         'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => '');
     4456        'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
     4457        'menu_order' => 0, 'to_ping' =>  '', 'pinged' => '', 'post_password' => '',
     4458        'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
     4459        'post_title' => '', 'post_content' => '', 'context' => '');
    44594460
    44604461    $object = wp_parse_args($object, $defaults);
    4461     if ( !empty($parent) )
     4462    if ( ! empty( $parent ) ) {
    44624463        $object['post_parent'] = $parent;
    4463 
     4464    }
    44644465    unset( $object[ 'filter' ] );
    44654466
    44664467    $object = sanitize_post($object, 'db');
    44674468
    4468     // export array as variables
    4469     extract($object, EXTR_SKIP);
    4470 
    4471     if ( empty($post_author) )
    4472         $post_author = $user_id;
     4469    $post_ID = 0;
     4470    $update = false;
     4471    $guid = $object['guid'];
     4472
     4473    // Are we updating or creating?
     4474    if ( ! empty( $object['ID'] ) ) {
     4475        $update = true;
     4476        $post_ID = (int) $object['ID'];
     4477
     4478        // wp_insert_post() checks for the existence of this post....
     4479    }
    44734480
    44744481    $post_type = 'attachment';
    44754482
    4476     if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
     4483    $post_title = $object['post_title'];
     4484    $post_content = $object['post_content'];
     4485    $post_excerpt = $object['post_excerpt'];
     4486    if ( isset( $object['post_name'] ) ) {
     4487        $post_name = $object['post_name'];
     4488    }
     4489
     4490    // wp_insert_post() checks $maybe_empty
     4491
     4492    $post_status = $object['post_status'];
     4493    if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
    44774494        $post_status = 'inherit';
    4478 
    4479     if ( !empty($post_category) )
    4480         $post_category = array_filter($post_category); // Filter out empty terms
     4495    }
     4496
     4497    if ( ! empty( $object['post_category'] ) ) {
     4498        $post_category = array_filter( $object['post_category'] ); // Filter out empty terms
     4499    }
    44814500
    44824501    // Make sure we set a valid category.
    4483     if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
     4502    if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
     4503        // ironically, the default post_type for this function is 'post'
     4504        // as such, this should be probably have the same checks as wp_insert_post(),
     4505        // since all 'post's require a category. BUT, you can't override the post_type, because the
     4506        // variable is explicitly set.
    44844507        $post_category = array();
    44854508    }
    44864509
    4487     // Are we updating or creating?
    4488     if ( !empty($ID) ) {
    4489         $update = true;
    4490         $post_ID = (int) $ID;
     4510    // Create a valid post name.
     4511    if ( empty( $post_name ) ) {
     4512        $post_name = sanitize_title($post_title);
    44914513    } else {
    4492         $update = false;
    4493         $post_ID = 0;
    4494     }
    4495 
    4496     // Create a valid post name.
    4497     if ( empty($post_name) )
    4498         $post_name = sanitize_title($post_title);
    4499     else
    4500         $post_name = sanitize_title($post_name);
     4514        // missing check from wp_insert_post on update:
     4515        // "On updates, we need to check to see if it's using the old, fixed sanitization context."
     4516        $post_name = sanitize_title( $post_name );
     4517    }
     4518
     4519    if ( isset( $object['post_parent'] ) ) {
     4520        $post_parent = (int) $object['post_parent'];
     4521    } else {
     4522        $post_parent = 0;
     4523    }
     4524
     4525    if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) {
     4526        $post_date = current_time( 'mysql' );
     4527    } else {
     4528        $post_date = $object['post_date'];
     4529    }
     4530
     4531    // wp_insert_post() validates the date here
     4532
     4533    if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) {
     4534        $post_date_gmt = get_gmt_from_date( $post_date );
     4535    } else {
     4536        $post_date_gmt = $object['post_date_gmt'];
     4537    }
     4538
     4539    if ( $update || '0000-00-00 00:00:00' == $post_date ) {
     4540        $post_modified     = current_time( 'mysql' );
     4541        $post_modified_gmt = current_time( 'mysql', 1 );
     4542    } else {
     4543        $post_modified     = $post_date;
     4544        $post_modified_gmt = $post_date_gmt;
     4545    }
     4546
     4547    // wp_insert_post() does "future" checks
     4548
     4549    if ( empty( $object['comment_status'] ) ) {
     4550        if ( $update ) {
     4551            $comment_status = 'closed';
     4552        } else {
     4553            $comment_status = get_option('default_comment_status');
     4554        }
     4555    } else {
     4556        $comment_status = $object['comment_status'];
     4557    }
     4558
     4559    // these variables are needed by compact() later
     4560    $post_content_filtered = $object['post_content_filtered'];
     4561    $post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author'];
     4562    $ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status'];
     4563    $to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : '';
     4564    $pinged = isset( $object['pinged'] ) ? $object['pinged'] : '';
     4565    $import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0;
     4566
     4567    if ( isset( $object['menu_order'] ) ) {
     4568        $menu_order = (int) $object['menu_order'];
     4569    } else {
     4570        $menu_order = 0;
     4571    }
     4572
     4573    $post_password = isset( $object['post_password'] ) ? $object['post_password'] : '';
     4574
     4575    // skips the 'wp_insert_post_parent' filter, present in wp_insert_post()
    45014576
    45024577    // expected_slashed ($post_name)
    4503     $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
    4504 
    4505     if ( empty($post_date) )
    4506         $post_date = current_time('mysql');
    4507     if ( empty($post_date_gmt) )
    4508         $post_date_gmt = current_time('mysql', 1);
    4509 
    4510     if ( empty($post_modified) )
    4511         $post_modified = $post_date;
    4512     if ( empty($post_modified_gmt) )
    4513         $post_modified_gmt = $post_date_gmt;
    4514 
    4515     if ( empty($comment_status) ) {
    4516         if ( $update )
    4517             $comment_status = 'closed';
    4518         else
    4519             $comment_status = get_option('default_comment_status');
    4520     }
    4521     if ( empty($ping_status) )
    4522         $ping_status = get_option('default_ping_status');
    4523 
    4524     if ( isset($to_ping) )
    4525         $to_ping = preg_replace('|\s+|', "\n", $to_ping);
    4526     else
    4527         $to_ping = '';
    4528 
    4529     if ( isset($post_parent) )
    4530         $post_parent = (int) $post_parent;
    4531     else
    4532         $post_parent = 0;
    4533 
    4534     if ( isset($menu_order) )
    4535         $menu_order = (int) $menu_order;
    4536     else
    4537         $menu_order = 0;
    4538 
    4539     if ( !isset($post_password) )
    4540         $post_password = '';
    4541 
    4542     if ( ! isset($pinged) )
    4543         $pinged = '';
     4578    $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
     4579
     4580    // don't unslash
     4581    $post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : '';
    45444582
    45454583    // expected_slashed (everything!)
    4546     $data = compact( array( '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' ) );
     4584    $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' );
    45474585
    45484586    /**
     
    45574595    $data = apply_filters( 'wp_insert_attachment_data', $data, $object );
    45584596    $data = wp_unslash( $data );
     4597    $where = array( 'ID' => $post_ID );
    45594598
    45604599    if ( $update ) {
    4561         $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
     4600        // skips 'pre_post_update' action
     4601        $wpdb->update( $wpdb->posts, $data, $where );
    45624602    } else {
    45634603        // If there is a suggested ID, use it if not already present
    4564         if ( !empty($import_id) ) {
     4604        if ( ! empty( $import_id ) ) {
    45654605            $import_id = (int) $import_id;
    4566             if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
     4606            if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
    45674607                $data['ID'] = $import_id;
    45684608            }
     
    45714611        $wpdb->insert( $wpdb->posts, $data );
    45724612        $post_ID = (int) $wpdb->insert_id;
    4573     }
    4574 
    4575     if ( empty($post_name) ) {
    4576         $post_name = sanitize_title($post_title, $post_ID);
    4577         $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
    4578     }
    4579 
    4580     if ( is_object_in_taxonomy($post_type, 'category') )
     4613        $where = array( 'ID' => $post_ID );
     4614    }
     4615
     4616    if ( empty( $data['post_name'] ) ) {
     4617        $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
     4618        $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
     4619    }
     4620
     4621    if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) {
    45814622        wp_set_post_categories( $post_ID, $post_category );
    4582 
    4583     if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
    4584         wp_set_post_tags( $post_ID, $tags_input );
    4585 
    4586     // support for all custom taxonomies
    4587     if ( !empty($tax_input) ) {
    4588         foreach ( $tax_input as $taxonomy => $tags ) {
     4623    }
     4624
     4625    if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) {
     4626        wp_set_post_tags( $post_ID, $object['tags_input'] );
     4627    }
     4628
     4629    // new-style support for all custom taxonomies
     4630    if ( ! empty( $object['tax_input'] ) ) {
     4631        foreach ( $object['tax_input'] as $taxonomy => $tags ) {
    45894632            $taxonomy_obj = get_taxonomy($taxonomy);
    4590             if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
     4633            if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical.
    45914634                $tags = array_filter($tags);
    4592             if ( current_user_can($taxonomy_obj->cap->assign_terms) )
     4635            }
     4636            if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
    45934637                wp_set_post_terms( $post_ID, $tags, $taxonomy );
    4594         }
    4595     }
    4596 
    4597     if ( $file )
     4638            }
     4639        }
     4640    }
     4641
     4642    if ( $file ) {
    45984643        update_attached_file( $post_ID, $file );
     4644    }
     4645
     4646    // wp_insert_post() fills in guid if it is empty
    45994647
    46004648    clean_post_cache( $post_ID );
    46014649
    4602     if ( ! empty( $context ) )
    4603         add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
    4604 
    4605     if ( $update) {
     4650    if ( ! empty( $object['context'] ) ) {
     4651        add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true );
     4652    }
     4653
     4654    // skips wp_transition_post_status
     4655
     4656    // the actions completely diverge from wp_insert_post()
     4657
     4658    if ( $update ) {
    46064659        /**
    46074660         * Fires once an existing attachment has been updated.
Note: See TracChangeset for help on using the changeset viewer.