<!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>[2172] 2013/frederickding/importer/trunk/class-wordpress-importer.php: Ported `process_menu_item()` and `fetch_remote_file()` from non-cron importer.</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/2172">2172</a></dd>
<dt>Author</dt> <dd>frederick.ding</dd>
<dt>Date</dt> <dd>2013-07-29 06:23:42 +0000 (Mon, 29 Jul 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Ported `process_menu_item()` and `fetch_remote_file()` from non-cron importer.

- Includes a few changes to use the `$this->warnings` queue
- 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 (2171 => 2172)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/frederickding/importer/trunk/class-wordpress-importer.php   2013-07-29 06:02:06 UTC (rev 2171)
+++ 2013/frederickding/importer/trunk/class-wordpress-importer.php      2013-07-29 06:23:42 UTC (rev 2172)
</span><span class="lines">@@ -150,6 +150,27 @@
</span><span class="cx">  protected $processed_featured_images = array();
</span><span class="cx">  
</span><span class="cx">  /**
</span><ins>+        *
+        * @see process_menu_item()
+        * @var array
+        */
+       protected $processed_menu_items = array();
+       
+       /**
+        *
+        * @see process_menu_item()
+        * @var array
+        */
+       protected $processed_menu_missing_items = array();
+       
+       /**
+        *
+        * @see fetch_remote_file()
+        * @var array
+        */
+       protected $processed_url_remap = 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">@@ -723,18 +744,110 @@
</span><span class="cx">              $parts_new = pathinfo( $upload['url'] );
</span><span class="cx">              $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
</span><span class="cx">          
</span><del>-                   $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
</del><ins>+                    $this->processed_url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
</ins><span class="cx">           }
</span><span class="cx">          
</span><span class="cx">          return $post_id;
</span><span class="cx">  }
</span><span class="cx">  
</span><span class="cx">  /**
</span><ins>+        * Attempt to create a new menu item from import data
</ins><span class="cx">    *
</span><del>-        * @todo
</del><ins>+         * Fails for draft, orphaned menu items and those without an associated nav_menu
+        * or an invalid nav_menu term. If the post type or term object which the menu item
+        * represents doesn't exist then the menu item will not be imported (waits until the
+        * end of the import to retry again before discarding).
+        *
+        * @param array $item Menu item details from WXR file
+        * @return int|WP_Error
</ins><span class="cx">    */
</span><del>-       protected function process_menu_item() {}
</del><ins>+        protected function process_menu_item( $item ) {
+               // skip draft, orphaned menu items
+               if ( 'draft' == $item['status'] )
+                       return 0;
</ins><span class="cx"> 
</span><ins>+               $menu_slug = false;
+               if ( isset($item['terms']) ) {
+                       // loop through terms, assume first nav_menu term is correct menu
+                       foreach ( $item['terms'] as $term ) {
+                               if ( 'nav_menu' == $term['domain'] ) {
+                                       $menu_slug = $term['slug'];
+                                       break;
+                               }
+                       }
+               }
+
+               // no nav_menu term associated with this menu item
+               if ( ! $menu_slug ) {
+                       $this->warnings[] = new WP_Error( 
+                                       'import_process_menu_item_slug_warning', 
+                                       __( 'Menu item skipped due to missing menu slug', 
+                                                       'wordpress-importer' ) );
+                       return 0;
+               }
+
+               $menu_id = term_exists( $menu_slug, 'nav_menu' );
+               if ( ! $menu_id ) {
+                       $this->warnings[] = new WP_Error( 
+                                       'import_process_menu_item_slug_warning', 
+                                       sprintf( 
+                                                       __( 
+                                                                       'Menu item skipped due to invalid menu slug: %s', 
+                                                                       'wordpress-importer' ), 
+                                                       esc_html( $menu_slug ) ) );
+                       return 0;
+               } else {
+                       $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
+               }
+
+               foreach ( $item['postmeta'] as $meta )
+                       $$meta['key'] = $meta['value'];
+
+               if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
+                       $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
+               } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
+                       $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
+               } else if ( 'custom' != $_menu_item_type ) {
+                       // associated object is missing or not imported yet, we'll retry later
+                       $this->processed_menu_missing_items[] = $item;
+                       return;
+               }
+
+               if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
+                       $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
+               } else if ( $_menu_item_menu_item_parent ) {
+                       $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
+                       $_menu_item_menu_item_parent = 0;
+               }
+
+               // wp_update_nav_menu_item expects CSS classes as a space separated string
+               $_menu_item_classes = maybe_unserialize( $_menu_item_classes );
+               if ( is_array( $_menu_item_classes ) )
+                       $_menu_item_classes = implode( ' ', $_menu_item_classes );
+
+               $args = array(
+                       'menu-item-object-id' => $_menu_item_object_id,
+                       'menu-item-object' => $_menu_item_object,
+                       'menu-item-parent-id' => $_menu_item_menu_item_parent,
+                       'menu-item-position' => intval( $item['menu_order'] ),
+                       'menu-item-type' => $_menu_item_type,
+                       'menu-item-title' => $item['post_title'],
+                       'menu-item-url' => $_menu_item_url,
+                       'menu-item-description' => $item['post_content'],
+                       'menu-item-attr-title' => $item['post_excerpt'],
+                       'menu-item-target' => $_menu_item_target,
+                       'menu-item-classes' => $_menu_item_classes,
+                       'menu-item-xfn' => $_menu_item_xfn,
+                       'menu-item-status' => $item['status']
+               );
+
+               $id = wp_update_nav_menu_item( $menu_id, 0, $args );
+               if ( $id && ! is_wp_error( $id ) )
+                       $this->processed_menu_items[intval($item['post_id'])] = (int) $id;
+               
+               return $id;
+       }
+
</ins><span class="cx">   /**
</span><span class="cx">   * Creates new posts based on import data.
</span><span class="cx">   *
</span><span class="lines">@@ -1085,6 +1198,89 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Attempt to download a remote file attachment
+        *
+        * @param string $url
+        *              URL of item to fetch
+        * @param array $post
+        *              Attachment details
+        * @return array WP_Error file location details on success, WP_Error
+        *         otherwise
+        */
+       protected function fetch_remote_file( $url, $post ) {
+               // extract the file name and extension from the url
+               $file_name = basename( $url );
+               
+               // get placeholder file in the upload dir with a unique, sanitized
+               // filename
+               $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
+               if ( $upload['error'] )
+                       return new WP_Error( 'upload_dir_error', $upload['error'] );
+                       
+                       // fetch the remote url and write it to the placeholder file
+               $headers = wp_get_http( $url, $upload['file'] );
+               
+               // request failed
+               if ( ! $headers ) {
+                       @unlink( $upload['file'] );
+                       return new WP_Error( 'import_file_error', 
+                                       __( 'Remote server did not respond', 
+                                                       'wordpress-importer' ) );
+               }
+               
+               // make sure the fetch was successful
+               if ( $headers['response'] != '200' ) {
+                       @unlink( $upload['file'] );
+                       return new WP_Error( 'import_file_error', 
+                                       sprintf( 
+                                                       __( 
+                                                                       'Remote server returned error response %1$d %2$s', 
+                                                                       'wordpress-importer' ), 
+                                                       esc_html( $headers['response'] ), 
+                                                       get_status_header_desc( $headers['response'] ) ) );
+               }
+               
+               $filesize = filesize( $upload['file'] );
+               
+               if ( isset( $headers['content-length'] ) &&
+                                $filesize != $headers['content-length'] ) {
+                       @unlink( $upload['file'] );
+                       return new WP_Error( 'import_file_error', 
+                                       __( 'Remote file is incorrect size', 
+                                                       'wordpress-importer' ) );
+               }
+               
+               if ( 0 == $filesize ) {
+                       @unlink( $upload['file'] );
+                       return new WP_Error( 'import_file_error', 
+                                       __( 'Zero size file downloaded', 'wordpress-importer' ) );
+               }
+               
+               $max_size = (int) $this->max_attachment_size();
+               if ( ! empty( $max_size ) && $filesize > $max_size ) {
+                       @unlink( $upload['file'] );
+                       return new WP_Error( 'import_file_error', 
+                                       sprintf( 
+                                                       __( 'Remote file is too large, limit is %s', 
+                                                                       'wordpress-importer' ), 
+                                                       size_format( $max_size ) ) );
+               }
+               
+               // keep track of the old and new urls so we can substitute them later
+               $this->processed_url_remap[$url] = $upload['url'];
+               $this->processed_url_remap[$post['guid']] = $upload['url'];
+               // r13735, really needed?
+               // ^ keep track of the destination if the remote url is redirected
+               // somewhere else
+               
+               if ( isset( $headers['x-final-location'] ) &&
+                                $headers['x-final-location'] != $url )
+                       $this->processed_url_remap[$headers['x-final-location']] = $upload['url'];
+               
+               return $upload;
+       }
+
+       /**
</ins><span class="cx">    * Decide if the given meta key maps to information we will want to import
</span><span class="cx">   * 
</span><span class="cx">   * @param string $key
</span></span></pre>
</div>
</div>

</body>
</html>