Exemple #1
0
 function import_line($line, $options)
 {
     global $wpdb, $wp_object_cache;
     $post = array();
     // Post to be sent to WP
     $imported_post = new TI_Import_Line();
     // Post details saved to the database after the import
     $update = false;
     // Whether this is an update
     // Get blog ID and switch to that blog if needed
     $post_blog_id = $this->import_blog_id($line);
     if (is_wp_error($post_blog_id)) {
         return $post_blog_id;
     } else {
         $imported_post->blog_id = $post_blog_id;
     }
     // Determine if this is an insert or an update (it's an update if a post ID was specified AND that post exists already)
     if ($this->template->postid_col) {
         // ID value to match
         $update_id = $this->read_line_col($line, $this->template->postid_col);
         if (is_wp_error($update_id)) {
             return $update_id;
         }
         // If column is not empty, then try to find the matching post ID
         if ($update_id) {
             // Field to match against
             $update_field = $this->template->update_field;
             // Try to find the post to be updated
             $found_id = $this->find_post_id($update_field, $update_id);
             if ($found_id) {
                 $post['ID'] = $found_id;
                 $update = true;
             }
         }
     }
     // Parse the tokens in the post body/title
     $result = $this->import_post_content($line, $post, $update);
     if (is_wp_error($result)) {
         return $result;
     }
     // Assign basic settings fields and validate them
     $result = $this->import_post_settings($line, $post);
     if (is_wp_error($result)) {
         return $result;
     }
     // Assign post dates
     $result = $this->import_post_date($line, $post, $update);
     if (is_wp_error($result)) {
         return $result;
     }
     // Taxonomies
     $result = $this->import_post_taxonomies($line, $options);
     if (is_wp_error($result)) {
         return $result;
     } elseif (!empty($result)) {
         $post['tax_input'] = $result;
     }
     // Update or insert the post
     // If revisions are active, this may return a new post ID
     if ($update) {
         $post_id = wp_update_post($post);
     } else {
         $post_id = wp_insert_post($post, true);
     }
     // Record memory usage after update/insert
     $memory_usage = memory_get_usage();
     $this->max_memory = $memory_usage > $this->max_memory ? $memory_usage : $this->max_memory;
     // Flush the object cache - if this isn't done WordPress leaks 4k of memory with each new post.
     // Even with the flush, it's about 900B.
     $wp_object_cache->flush();
     // Any error from posting is fatal.  wp_insert gives a full error, wp_update just returns FALSE
     if (is_wp_error($post_id)) {
         return $this->log_line($post_id);
     } elseif ($post_id === false) {
         return $this->log_line(sprintf(__("Wordpress error when updating post id=%s"), $post_id));
     }
     // If updating, remember revision post number for later undo
     if ($update) {
         // Figure out if revisions are supported and switched on
         if (defined('WP_POST_REVISIONS') && WP_POST_REVISIONS && !empty($post['post_type']) && post_type_supports($post['post_type'], 'revisions')) {
             // A revision was created, save it for later UNDO
             $imported_post->revision_id = $this->get_last_revision($post_id);
         } else {
             // If revision management not turned on, store FALSE in revision
             $imported_post->revision_id = false;
         }
     }
     // Post was OK - save the post data to the import; content and excerpt are NOT saved to conserve memory
     $imported_post->updated = $update;
     $imported_post->post_id = $post_id;
     $imported_post->import_id = $this->_id;
     $result = $imported_post->save();
     if (is_wp_error($result)) {
         return $result;
     }
     // Featured image / thumbnail - update/insert after post is complete
     $result = $this->import_thumbnail($line, $post_id);
     if (is_wp_error($result)) {
         return $result;
     }
     // Update/insert post metadata after the post is complete
     $result = $this->import_post_meta($line, $post_id, $options, $update);
     if (is_wp_error($result)) {
         return $result;
     }
     return true;
 }