<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[28470] trunk/src/wp-includes/post.php: Eliminate the use of `extract()` in `wp_insert_attachment()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/28470">28470</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-05-17 16:53:44 +0000 (Sat, 17 May 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://core.trac.wordpress.org/ticket/22400">#22400</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/post.php (28469 => 28470)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/post.php 2014-05-17 15:52:11 UTC (rev 28469)
+++ trunk/src/wp-includes/post.php 2014-05-17 16:53:44 UTC (rev 28470)
</span><span class="lines">@@ -4453,97 +4453,135 @@
</span><span class="cx"> $user_id = get_current_user_id();
</span><span class="cx">
</span><span class="cx"> $defaults = array('post_status' => 'inherit', 'post_type' => 'post', 'post_author' => $user_id,
</span><del>- 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0, 'post_title' => '',
- 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '', 'post_content' => '',
- 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0, 'context' => '');
</del><ins>+ 'ping_status' => get_option('default_ping_status'), 'post_parent' => 0,
+ 'menu_order' => 0, 'to_ping' => '', 'pinged' => '', 'post_password' => '',
+ 'guid' => '', 'post_content_filtered' => '', 'post_excerpt' => '', 'import_id' => 0,
+ 'post_title' => '', 'post_content' => '', 'context' => '');
</ins><span class="cx">
</span><span class="cx"> $object = wp_parse_args($object, $defaults);
</span><del>- if ( !empty($parent) )
</del><ins>+ if ( ! empty( $parent ) ) {
</ins><span class="cx"> $object['post_parent'] = $parent;
</span><del>-
</del><ins>+ }
</ins><span class="cx"> unset( $object[ 'filter' ] );
</span><span class="cx">
</span><span class="cx"> $object = sanitize_post($object, 'db');
</span><span class="cx">
</span><del>- // export array as variables
- extract($object, EXTR_SKIP);
</del><ins>+ $post_ID = 0;
+ $update = false;
+ $guid = $object['guid'];
</ins><span class="cx">
</span><del>- if ( empty($post_author) )
- $post_author = $user_id;
</del><ins>+ // Are we updating or creating?
+ if ( ! empty( $object['ID'] ) ) {
+ $update = true;
+ $post_ID = (int) $object['ID'];
</ins><span class="cx">
</span><ins>+ // wp_insert_post() checks for the existence of this post....
+ }
+
</ins><span class="cx"> $post_type = 'attachment';
</span><span class="cx">
</span><del>- if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
</del><ins>+ $post_title = $object['post_title'];
+ $post_content = $object['post_content'];
+ $post_excerpt = $object['post_excerpt'];
+ if ( isset( $object['post_name'] ) ) {
+ $post_name = $object['post_name'];
+ }
+
+ // wp_insert_post() checks $maybe_empty
+
+ $post_status = $object['post_status'];
+ if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) ) {
</ins><span class="cx"> $post_status = 'inherit';
</span><ins>+ }
</ins><span class="cx">
</span><del>- if ( !empty($post_category) )
- $post_category = array_filter($post_category); // Filter out empty terms
</del><ins>+ if ( ! empty( $object['post_category'] ) ) {
+ $post_category = array_filter( $object['post_category'] ); // Filter out empty terms
+ }
</ins><span class="cx">
</span><span class="cx"> // Make sure we set a valid category.
</span><del>- if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
</del><ins>+ if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
+ // ironically, the default post_type for this function is 'post'
+ // as such, this should be probably have the same checks as wp_insert_post(),
+ // since all 'post's require a category. BUT, you can't override the post_type, because the
+ // variable is explicitly set.
</ins><span class="cx"> $post_category = array();
</span><span class="cx"> }
</span><span class="cx">
</span><del>- // Are we updating or creating?
- if ( !empty($ID) ) {
- $update = true;
- $post_ID = (int) $ID;
</del><ins>+ // Create a valid post name.
+ if ( empty( $post_name ) ) {
+ $post_name = sanitize_title($post_title);
</ins><span class="cx"> } else {
</span><del>- $update = false;
- $post_ID = 0;
</del><ins>+ // missing check from wp_insert_post on update:
+ // "On updates, we need to check to see if it's using the old, fixed sanitization context."
+ $post_name = sanitize_title( $post_name );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- // Create a valid post name.
- if ( empty($post_name) )
- $post_name = sanitize_title($post_title);
- else
- $post_name = sanitize_title($post_name);
</del><ins>+ if ( isset( $object['post_parent'] ) ) {
+ $post_parent = (int) $object['post_parent'];
+ } else {
+ $post_parent = 0;
+ }
</ins><span class="cx">
</span><del>- // expected_slashed ($post_name)
- $post_name = wp_unique_post_slug($post_name, $post_ID, $post_status, $post_type, $post_parent);
</del><ins>+ if ( empty( $object['post_date'] ) || '0000-00-00 00:00:00' == $object['post_date'] ) {
+ $post_date = current_time( 'mysql' );
+ } else {
+ $post_date = $object['post_date'];
+ }
</ins><span class="cx">
</span><del>- if ( empty($post_date) )
- $post_date = current_time('mysql');
- if ( empty($post_date_gmt) )
- $post_date_gmt = current_time('mysql', 1);
</del><ins>+ // wp_insert_post() validates the date here
</ins><span class="cx">
</span><del>- if ( empty($post_modified) )
- $post_modified = $post_date;
- if ( empty($post_modified_gmt) )
</del><ins>+ if ( empty( $object['post_date_gmt'] ) || '0000-00-00 00:00:00' == $object['post_date_gmt'] ) {
+ $post_date_gmt = get_gmt_from_date( $post_date );
+ } else {
+ $post_date_gmt = $object['post_date_gmt'];
+ }
+
+ if ( $update || '0000-00-00 00:00:00' == $post_date ) {
+ $post_modified = current_time( 'mysql' );
+ $post_modified_gmt = current_time( 'mysql', 1 );
+ } else {
+ $post_modified = $post_date;
</ins><span class="cx"> $post_modified_gmt = $post_date_gmt;
</span><ins>+ }
</ins><span class="cx">
</span><del>- if ( empty($comment_status) ) {
- if ( $update )
</del><ins>+ // wp_insert_post() does "future" checks
+
+ if ( empty( $object['comment_status'] ) ) {
+ if ( $update ) {
</ins><span class="cx"> $comment_status = 'closed';
</span><del>- else
</del><ins>+ } else {
</ins><span class="cx"> $comment_status = get_option('default_comment_status');
</span><ins>+ }
+ } else {
+ $comment_status = $object['comment_status'];
</ins><span class="cx"> }
</span><del>- if ( empty($ping_status) )
- $ping_status = get_option('default_ping_status');
</del><span class="cx">
</span><del>- if ( isset($to_ping) )
- $to_ping = preg_replace('|\s+|', "\n", $to_ping);
- else
- $to_ping = '';
</del><ins>+ // these variables are needed by compact() later
+ $post_content_filtered = $object['post_content_filtered'];
+ $post_author = empty( $object['post_author'] ) ? $user_id : $object['post_author'];
+ $ping_status = empty( $object['ping_status'] ) ? get_option( 'default_ping_status' ) : $object['ping_status'];
+ $to_ping = isset( $object['to_ping'] ) ? sanitize_trackback_urls( $object['to_ping'] ) : '';
+ $pinged = isset( $object['pinged'] ) ? $object['pinged'] : '';
+ $import_id = isset( $object['import_id'] ) ? $object['import_id'] : 0;
</ins><span class="cx">
</span><del>- if ( isset($post_parent) )
- $post_parent = (int) $post_parent;
- else
- $post_parent = 0;
-
- if ( isset($menu_order) )
- $menu_order = (int) $menu_order;
- else
</del><ins>+ if ( isset( $object['menu_order'] ) ) {
+ $menu_order = (int) $object['menu_order'];
+ } else {
</ins><span class="cx"> $menu_order = 0;
</span><ins>+ }
</ins><span class="cx">
</span><del>- if ( !isset($post_password) )
- $post_password = '';
</del><ins>+ $post_password = isset( $object['post_password'] ) ? $object['post_password'] : '';
</ins><span class="cx">
</span><del>- if ( ! isset($pinged) )
- $pinged = '';
</del><ins>+ // skips the 'wp_insert_post_parent' filter, present in wp_insert_post()
</ins><span class="cx">
</span><ins>+ // expected_slashed ($post_name)
+ $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
+
+ // don't unslash
+ $post_mime_type = isset( $object['post_mime_type'] ) ? $object['post_mime_type'] : '';
+
</ins><span class="cx"> // expected_slashed (everything!)
</span><del>- $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' ) );
</del><ins>+ $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' );
</ins><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * Filter attachment post data before it is updated in or added
</span><span class="lines">@@ -4556,53 +4594,68 @@
</span><span class="cx"> */
</span><span class="cx"> $data = apply_filters( 'wp_insert_attachment_data', $data, $object );
</span><span class="cx"> $data = wp_unslash( $data );
</span><ins>+ $where = array( 'ID' => $post_ID );
</ins><span class="cx">
</span><span class="cx"> if ( $update ) {
</span><del>- $wpdb->update( $wpdb->posts, $data, array( 'ID' => $post_ID ) );
</del><ins>+ // skips 'pre_post_update' action
+ $wpdb->update( $wpdb->posts, $data, $where );
</ins><span class="cx"> } else {
</span><span class="cx"> // If there is a suggested ID, use it if not already present
</span><del>- if ( !empty($import_id) ) {
</del><ins>+ if ( ! empty( $import_id ) ) {
</ins><span class="cx"> $import_id = (int) $import_id;
</span><del>- if ( ! $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id) ) ) {
</del><ins>+ if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
</ins><span class="cx"> $data['ID'] = $import_id;
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> $wpdb->insert( $wpdb->posts, $data );
</span><span class="cx"> $post_ID = (int) $wpdb->insert_id;
</span><ins>+ $where = array( 'ID' => $post_ID );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( empty($post_name) ) {
- $post_name = sanitize_title($post_title, $post_ID);
- $wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
</del><ins>+ if ( empty( $data['post_name'] ) ) {
+ $data['post_name'] = sanitize_title( $data['post_title'], $post_ID );
+ $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( is_object_in_taxonomy($post_type, 'category') )
</del><ins>+ if ( is_object_in_taxonomy( $data['post_type'], 'category' ) ) {
</ins><span class="cx"> wp_set_post_categories( $post_ID, $post_category );
</span><ins>+ }
</ins><span class="cx">
</span><del>- if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
- wp_set_post_tags( $post_ID, $tags_input );
</del><ins>+ if ( isset( $object['tags_input'] ) && is_object_in_taxonomy( $data['post_type'], 'post_tag' ) ) {
+ wp_set_post_tags( $post_ID, $object['tags_input'] );
+ }
</ins><span class="cx">
</span><del>- // support for all custom taxonomies
- if ( !empty($tax_input) ) {
- foreach ( $tax_input as $taxonomy => $tags ) {
</del><ins>+ // new-style support for all custom taxonomies
+ if ( ! empty( $object['tax_input'] ) ) {
+ foreach ( $object['tax_input'] as $taxonomy => $tags ) {
</ins><span class="cx"> $taxonomy_obj = get_taxonomy($taxonomy);
</span><del>- if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
</del><ins>+ if ( is_array( $tags ) ) { // array = hierarchical, string = non-hierarchical.
</ins><span class="cx"> $tags = array_filter($tags);
</span><del>- if ( current_user_can($taxonomy_obj->cap->assign_terms) )
</del><ins>+ }
+ if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
</ins><span class="cx"> wp_set_post_terms( $post_ID, $tags, $taxonomy );
</span><ins>+ }
</ins><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- if ( $file )
</del><ins>+ if ( $file ) {
</ins><span class="cx"> update_attached_file( $post_ID, $file );
</span><ins>+ }
</ins><span class="cx">
</span><ins>+ // wp_insert_post() fills in guid if it is empty
+
</ins><span class="cx"> clean_post_cache( $post_ID );
</span><span class="cx">
</span><del>- if ( ! empty( $context ) )
- add_post_meta( $post_ID, '_wp_attachment_context', $context, true );
</del><ins>+ if ( ! empty( $object['context'] ) ) {
+ add_post_meta( $post_ID, '_wp_attachment_context', $object['context'], true );
+ }
</ins><span class="cx">
</span><del>- if ( $update) {
</del><ins>+ // skips wp_transition_post_status
+
+ // the actions completely diverge from wp_insert_post()
+
+ if ( $update ) {
</ins><span class="cx"> /**
</span><span class="cx"> * Fires once an existing attachment has been updated.
</span><span class="cx"> *
</span></span></pre>
</div>
</div>
</body>
</html>