<!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>[2164] 2013/frederickding/importer/trunk/class-wordpress-importer.php: Add post process step.</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://gsoc.trac.wordpress.org/changeset/2164">2164</a></dd>
<dt>Author</dt> <dd>frederick.ding</dd>
<dt>Date</dt> <dd>2013-07-28 23:46:44 +0000 (Sun, 28 Jul 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add post process step.
- Attachment fetching temporarily unimplemented.
- See <a href="http://gsoc.trac.wordpress.org/ticket/327">#327</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013frederickdingimportertrunkclasswordpressimporterphp">2013/frederickding/importer/trunk/class-wordpress-importer.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013frederickdingimportertrunkclasswordpressimporterphp"></a>
<div class="modfile"><h4>Modified: 2013/frederickding/importer/trunk/class-wordpress-importer.php (2163 => 2164)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/frederickding/importer/trunk/class-wordpress-importer.php 2013-07-28 23:46:34 UTC (rev 2163)
+++ 2013/frederickding/importer/trunk/class-wordpress-importer.php 2013-07-28 23:46:44 UTC (rev 2164)
</span><span class="lines">@@ -97,6 +97,13 @@
</span><span class="cx"> */
</span><span class="cx"> protected $terms = array();
</span><span class="cx">
</span><ins>+ /**
+ *
+ * @see process_posts()
+ * @var array
+ */
+ protected $posts = array();
+
</ins><span class="cx"> /*
</span><span class="cx"> * SECTION: mappings from old information to new
</span><span class="cx"> */
</span><span class="lines">@@ -122,6 +129,26 @@
</span><span class="cx"> protected $processed_terms = array();
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ *
+ * @var array
+ */
+ protected $processed_posts = array();
+
+ /**
+ *
+ * @see process_posts()
+ * @var array
+ */
+ protected $processed_post_orphans = array();
+
+ /**
+ *
+ * @see process_posts()
+ * @var array
+ */
+ protected $processed_featured_images = array();
+
+ /**
</ins><span class="cx"> * Stores user-submitted choices on how to proceed with the process.
</span><span class="cx"> *
</span><span class="cx"> * The first dimension of this array is
</span><span class="lines">@@ -623,8 +650,8 @@
</span><span class="cx"> // set a warning in the importer class, and include the
</span><span class="cx"> // WP_Error object emitted by wp_insert_category() as data.
</span><span class="cx"> $this->warnings[] = new WP_Error(
</span><del>- 'import_process_tags_warning',
- __( 'Failed to import post %s %s',
</del><ins>+ 'import_process_term_warning',
+ __( 'Failed to import %s %s',
</ins><span class="cx"> 'wordpress-importer' ),
</span><span class="cx"> esc_html( $term['term_taxonomy'] ),
</span><span class="cx"> esc_html( $term['term_name'] ), $id );
</span><span class="lines">@@ -642,15 +669,316 @@
</span><span class="cx"> * @todo
</span><span class="cx"> */
</span><span class="cx"> protected function process_attachments() {}
</span><del>-
</del><ins>+
</ins><span class="cx"> /**
</span><span class="cx"> *
</span><span class="cx"> * @todo
</span><span class="cx"> */
</span><del>- protected function process_posts() {}
</del><ins>+ protected function process_attachment() {}
</ins><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Creates new posts based on import data.
</ins><span class="cx"> *
</span><ins>+ * Posts marked as having a parent which doesn't exist will become top level
+ * items. Doesn't create a new post if: the post type doesn't exist, the
+ * given post ID is already noted as imported or a post with the same title
+ * and date already exists. Note that new/updated terms, comments and meta
+ * are imported for the last of the above.
+ */
+ protected function process_posts() {
+ $this->posts = apply_filters( 'wp_import_posts', $this->posts );
+
+ foreach ( $this->posts as $post ) {
+ $post = apply_filters( 'wp_import_post_data_raw', $post );
+ if ( ! post_type_exists( $post['post_type'] ) ) {
+ $this->warnings[] = new WP_Error(
+ 'import_process_posts_post_type_warning',
+ __(
+ 'Failed to import “%s”: Invalid post type %s',
+ 'wordpress-importer' ),
+ array(
+ 'title' => esc_html( $post['post_title'] ),
+ 'post_type' => esc_html( $post['post_type'] )
+ ) );
+ do_action( 'wp_import_post_exists', $post );
+ continue;
+ }
+
+ if ( isset( $this->processed_posts[$post['post_id']] ) &&
+ ! empty( $post['post_id'] ) )
+ continue;
+
+ if ( $post['status'] == 'auto-draft' )
+ continue;
+
+ if ( 'nav_menu_item' == $post['post_type'] ) {
+ $this->process_menu_item( $post );
+ continue;
+ }
+
+ $post_type_object = get_post_type_object( $post['post_type'] );
+
+ $post_exists = post_exists( $post['post_title'], '',
+ $post['post_date'] );
+ if ( $post_exists &&
+ get_post_type( $post_exists ) == $post['post_type'] ) {
+ $this->warnings[] = new WP_Error(
+ 'import_process_posts_post_exists_warning',
+ __( '%s “%s” already exists.',
+ 'wordpress-importer' ),
+ array(
+ 'post_type' => $post_type_object->labels->singular_name,
+ 'title' => esc_html( $post['post_title'] )
+ ) );
+ $comment_post_ID = $post_id = $post_exists;
+ } else {
+ $post_parent = (int) $post['post_parent'];
+ if ( $post_parent ) {
+ // if we already know the parent, map it to the new local ID
+ if ( isset( $this->processed_posts[$post_parent] ) ) {
+ $post_parent = $this->processed_posts[$post_parent];
+ // otherwise record the parent for later
+ } else {
+ $this->processed_post_orphans[intval(
+ $post['post_id'] )] = $post_parent;
+ $post_parent = 0;
+ }
+ }
+
+ // map the post author
+ $author = sanitize_user( $post['post_author'], true );
+ if ( isset( $this->processed_author_mappings[$author] ) )
+ $author = $this->processed_author_mappings[$author];
+ else
+ $author = (int) $this->get_param( 'admin_user_id', 0 );
+
+ $postdata = array(
+ 'import_id' => $post['post_id'],
+ 'post_author' => $author,
+ 'post_date' => $post['post_date'],
+ 'post_date_gmt' => $post['post_date_gmt'],
+ 'post_content' => $post['post_content'],
+ 'post_excerpt' => $post['post_excerpt'],
+ 'post_title' => $post['post_title'],
+ 'post_status' => $post['status'],
+ 'post_name' => $post['post_name'],
+ 'comment_status' => $post['comment_status'],
+ 'ping_status' => $post['ping_status'],
+ 'guid' => $post['guid'],
+ 'post_parent' => $post_parent,
+ 'menu_order' => $post['menu_order'],
+ 'post_type' => $post['post_type'],
+ 'post_password' => $post['post_password']
+ );
+
+ $original_post_ID = $post['post_id'];
+ $postdata = apply_filters( 'wp_import_post_data_processed',
+ $postdata, $post );
+
+ /*
+ * @todo decouple attachment processing from post processing
+ */
+ if ( 'attachment' == $postdata['post_type'] ) {
+ $remote_url = ! empty( $post['attachment_url'] ) ? $post['attachment_url'] : $post['guid'];
+
+ // try to use _wp_attached file for upload folder placement
+ // to ensure the same location as the export site
+ // e.g. location is 2003/05/image.jpg but the attachment
+ // post_date is 2010/09, see media_handle_upload()
+ $postdata['upload_date'] = $post['post_date'];
+ if ( isset( $post['postmeta'] ) ) {
+ foreach ( $post['postmeta'] as $meta ) {
+ if ( $meta['key'] == '_wp_attached_file' ) {
+ if ( preg_match( '%^[0-9]{4}/[0-9]{2}%',
+ $meta['value'], $matches ) )
+ $postdata['upload_date'] = $matches[0];
+ break;
+ }
+ }
+ }
+
+ $comment_post_ID = $post_id = $this->process_attachment(
+ $postdata, $remote_url );
+ } else {
+ $comment_post_ID = $post_id = wp_insert_post( $postdata,
+ true );
+ do_action( 'wp_import_insert_post', $post_id,
+ $original_post_ID, $postdata, $post );
+ }
+
+ if ( is_wp_error( $post_id ) ) {
+ $this->warnings[] = new WP_Error(
+ 'import_process_posts_failure_warning',
+ __( 'Failed to import %s “%s”',
+ 'wordpress-importer' ),
+ array(
+ 'post_type' => $post_type_object->labels->singular_name,
+ 'post_title' => esc_html(
+ $post['post_title'] ),
+ 'previous_error' => $post_id
+ ) );
+ continue;
+ }
+
+ if ( $post['is_sticky'] == 1 )
+ stick_post( $post_id );
+ }
+
+ // map pre-import ID to local ID
+ $this->processed_posts[intval( $post['post_id'] )] = (int) $post_id;
+
+ if ( ! isset( $post['terms'] ) )
+ $post['terms'] = array();
+
+ $post['terms'] = apply_filters( 'wp_import_post_terms',
+ $post['terms'], $post_id, $post );
+
+ // add categories, tags and other terms
+ if ( ! empty( $post['terms'] ) ) {
+ $terms_to_set = array();
+ foreach ( $post['terms'] as $term ) {
+ // back compat with WXR 1.0 map 'tag' to 'post_tag'
+ $taxonomy = ('tag' == $term['domain']) ? 'post_tag' : $term['domain'];
+ $term_exists = term_exists( $term['slug'], $taxonomy );
+ $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
+ if ( ! $term_id ) {
+ $t = wp_insert_term( $term['name'], $taxonomy,
+ array(
+ 'slug' => $term['slug']
+ ) );
+ if ( ! is_wp_error( $t ) ) {
+ $term_id = $t['term_id'];
+ do_action( 'wp_import_insert_term', $t, $term,
+ $post_id, $post );
+ } else {
+ $this->warnings[] = new WP_Error(
+ 'import_process_term_warning',
+ __( 'Failed to import %s %s',
+ 'wordpress-importer' ),
+ esc_html( $taxonomy ),
+ esc_html( $term['name'] ), $t );
+ do_action( 'wp_import_insert_term_failed', $t,
+ $term, $post_id, $post );
+ continue;
+ }
+ }
+ $terms_to_set[$taxonomy][] = intval( $term_id );
+ }
+
+ foreach ( $terms_to_set as $tax => $ids ) {
+ $tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
+ do_action( 'wp_import_set_post_terms', $tt_ids, $ids,
+ $tax, $post_id, $post );
+ }
+ unset( $post['terms'], $terms_to_set );
+ }
+
+ if ( ! isset( $post['comments'] ) )
+ $post['comments'] = array();
+
+ $post['comments'] = apply_filters( 'wp_import_post_comments',
+ $post['comments'], $post_id, $post );
+
+ // add/update comments
+ if ( ! empty( $post['comments'] ) ) {
+ $num_comments = 0;
+ $inserted_comments = array();
+ foreach ( $post['comments'] as $comment ) {
+ $comment_id = $comment['comment_id'];
+ $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
+ $newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
+ $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
+ $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
+ $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
+ $newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
+ $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
+ $newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
+ $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
+ $newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
+ $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
+ $newcomments[$comment_id]['commentmeta'] = isset(
+ $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
+ if ( isset(
+ $this->processed_authors[$comment['comment_user_id']] ) )
+ $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
+ }
+ ksort( $newcomments );
+
+ foreach ( $newcomments as $key => $comment ) {
+ // if this is a new post we can skip the comment_exists()
+ // check
+ if ( ! $post_exists || ! comment_exists(
+ $comment['comment_author'],
+ $comment['comment_date'] ) ) {
+ if ( isset(
+ $inserted_comments[$comment['comment_parent']] ) )
+ $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
+ $comment = wp_filter_comment( $comment );
+ $inserted_comments[$key] = wp_insert_comment(
+ $comment );
+ do_action( 'wp_import_insert_comment',
+ $inserted_comments[$key], $comment,
+ $comment_post_ID, $post );
+
+ foreach ( $comment['commentmeta'] as $meta ) {
+ $value = maybe_unserialize( $meta['value'] );
+ add_comment_meta( $inserted_comments[$key],
+ $meta['key'], $value );
+ }
+
+ $num_comments ++;
+ }
+ }
+ unset( $newcomments, $inserted_comments, $post['comments'] );
+ }
+
+ if ( ! isset( $post['postmeta'] ) )
+ $post['postmeta'] = array();
+
+ $post['postmeta'] = apply_filters( 'wp_import_post_meta',
+ $post['postmeta'], $post_id, $post );
+
+ // add/update post meta
+ if ( ! empty( $post['postmeta'] ) ) {
+ foreach ( $post['postmeta'] as $meta ) {
+ $key = apply_filters( 'import_post_meta_key',
+ $meta['key'], $post_id, $post );
+ $value = false;
+
+ if ( '_edit_last' == $key ) {
+ if ( isset(
+ $this->processed_authors[intval(
+ $meta['value'] )] ) )
+ $value = $this->processed_authors[intval(
+ $meta['value'] )];
+ else
+ $key = false;
+ }
+
+ if ( $key ) {
+ // export gets meta straight from the DB so could have a
+ // serialized string
+ if ( ! $value )
+ $value = maybe_unserialize( $meta['value'] );
+
+ add_post_meta( $post_id, $key, $value );
+ do_action( 'import_post_meta', $post_id, $key,
+ $value );
+
+ // if the post has a featured image, take note of this
+ // in case of remap
+ if ( '_thumbnail_id' == $key )
+ $this->processed_featured_images[$post_id] = (int) $value;
+ }
+ }
+ }
+ }
+
+ unset( $this->posts );
+ }
+
+ /**
+ *
</ins><span class="cx"> * @todo
</span><span class="cx"> */
</span><span class="cx"> protected function process_parents() {}
</span></span></pre>
</div>
</div>
</body>
</html>