Exemplo n.º 1
0
 protected function get_attachments()
 {
     $atts = get_posts(array('posts_per_page' => 0, 'post_type' => 'attachment', 'post_status' => 'inherit', 'order' => 'ASC', 'post_parent' => $this->post->ID));
     if (!empty($atts)) {
         $attachments = array();
         foreach ($atts as $att) {
             $attachment = new cfd_attachment($att);
             $attachments[$attachment->guid()] = $attachment;
             unset($attachment);
         }
         ksort($attachments);
         return $attachments;
     } else {
         return false;
     }
 }
Exemplo n.º 2
0
 /**
  * Import/Update post
  * Will determine wether import or update is best option
  *
  * @param array $args 
  * @return post_id
  */
 protected function import_post($post)
 {
     cfd_tmp_dbg('post-import.txt', $post, 'print');
     try {
         $local_post = new cfd_post(array('guid' => $post['post']['guid']));
         $update = true;
     } catch (Exception $e) {
         // s'okay
         $update = false;
     }
     // Parent
     if (!empty($post['parent'])) {
         try {
             $post_parent = new cfd_post(array('guid' => $post['parent']['guid'], 'shallow_fetch' => true));
         } catch (Exception $e) {
             $error = sprintf(__('%s "%s" could not be inserted. Parent post does not exist.', 'cf-deploy'), $this->humanize($post['post']['post_type']), esc_html($post['post']['post_title']));
             $this->add_import_message('post_types.' . $post['post']['post_type'], '__error__', $error);
             return false;
         }
     }
     // Author
     $local_user = get_user_by('login', $post['author']);
     if (empty($local_user) && !empty($this->batch_items['users'][$post['author']])) {
         // once we start doing imports in parts this'll not be possible
         $author_id = $this->import_user($this->batch_items['users'][$post['author']]);
     } else {
         $author_id = $local_user->ID;
     }
     if (empty($author_id)) {
         $error = sprintf(__('User "%s" does not exist on remote server. Post cannot be imported', 'cf-deploy'), $post['author']);
         $this->add_import_message('post_types.' . $post['post']['post_type'], '__error__', $error);
         return false;
     }
     // Taxonomies - this is a little redundant to the full tax-import routine, but needed for post specific messaging
     $post_category = $tax_input = array();
     if (!empty($post['taxonomies'])) {
         foreach ($post['taxonomies'] as $tax_type => $tax_terms) {
             if (!taxonomy_exists($tax_type)) {
                 $error = sprintf(__('Post "%s" cannot be inserted. Taxonomy "%s" does not exist on this server.', 'cf-deploy'), esc_html($post['post_title']), $tax_type);
                 $this->add_import_message('post_types.' . $post['post']['post_types'], '__error__', $error);
                 return false;
             }
             $taxonomy = get_taxonomy($tax_type);
             if ($tax_type != 'category') {
                 // heirarchal taxonomy types are imported as arrays, non hierarchal as comma separated strings
                 $tax_input[$taxonomy->name] = $taxonomy->hierarchical == true ? array() : '';
             }
             if (!empty($tax_terms)) {
                 foreach ($tax_terms as $term) {
                     $term = strval($term);
                     if (!term_exists($term, $tax_type)) {
                         if (!empty($this->batch_items['taxonomies'][$tax_type][$term])) {
                             $ret = $this->import_term($this->batch_items['taxonomies'][$tax_type][$term]);
                             if (is_wp_error($ret)) {
                                 $error = sprintf(__('Post "%s" cannot be inserted. Term "%s" could not be imported.', 'cf-deploy'), esc_html($post['post']['post_title']), $term);
                                 $this->add_import_message('post_types.' . $post['post']['post_type'], '__error__', $error);
                                 return $false;
                             }
                         } else {
                             $this->add_import_message('post_types.' . $post['post']['post_type'], '__error__', $error);
                             return false;
                         }
                     }
                     $term = get_term_by('slug', $term, $tax_type);
                     if ($tax_type == 'category') {
                         $post_category[] = $term->term_id;
                     } elseif ($taxonomy->hierarchical == true) {
                         $tax_input[$taxonomy->name][] = $term->term_id;
                     } else {
                         $tax_input[$taxonomy->name] .= (!empty($tax_input[$taxonomy->name]) ? ',' : '') . $term->slug;
                     }
                 }
             }
         }
     }
     // Insert Post
     $_post = $post['post'];
     unset($_post['ID']);
     $_post['post_author'] = $author_id;
     if (!empty($post_parent)) {
         $_post['post_parent'] = $post_parent->ID;
     }
     $_post['post_category'] = $post_category;
     $_post['tax_input'] = $tax_input;
     $this->add_post_date_filter(array('post_date' => $_post['post_date'], 'post_date_gmt' => $_post['post_date_gmt'], 'post_modified' => $_post['post_modified'], 'post_modified_gmt' => $_post['post_modified_gmt']));
     if ($update) {
         $_post['ID'] = $local_post->ID;
         $insert_post_id = wp_update_post($_post);
     } else {
         $insert_post_id = wp_insert_post($_post);
     }
     $this->remove_post_date_filter();
     // Attachment Duties
     if ($post['post']['post_type'] == 'attachment') {
         // don't mess with actual files on rollback
         if (!$this->in_rollback) {
             // determine upload folder date. It doesn't seem that we can rely on the post-date to correlate to
             // the upload folder date, so if the files were organized by date then grab that date from the
             // pathinfo of the _wp_attached_file postmeta value
             $upload_folder_date = null;
             $pathinfo = pathinfo($post['meta']['_wp_attached_file']);
             if (preg_match('|^\\d{4}/\\d{2}$|', $pathinfo['dirname'])) {
                 $upload_folder_date = $pathinfo['dirname'];
             }
             // Transfer file
             $uploaded = $this->import_file($post, $insert_post_id, $upload_folder_date);
             if (is_wp_error($uploaded)) {
                 $error = sprintf(__('Import of file "%s" failed: ', 'cf-deploy'), $post['file']['url']) . $uploaded->get_error_message();
                 $this->add_import_message('post_types.' . $post['post']['post_type'], '__error__', $error);
                 return false;
             }
         }
         // handle featured image
         $featured_image_spots = $this->get_featured_image_data($post['post']['guid']);
         if (!empty($featured_image_spots)) {
             foreach ($featured_image_spots as $fi_post_data) {
                 update_post_meta($fi_post_data['local_post_id'], '_thumbnail_id', $insert_post_id);
             }
         }
     }
     if (is_wp_error($insert_post_id)) {
         $error = sprintf(__('Import of post "%s" failed. Error: %s', 'cf-deploy'), esc_html($post['post']['post_title']), $insert_post_id->get_error_message());
         $this->add_import_message('post_types.' . $post['post']['post_type'], '__error__', $error);
         return false;
     }
     // Postmeta
     if ($update) {
         // get existing to sniff for deletions
         $meta = get_metadata('post', $insert_post_id);
         if (!empty($meta)) {
             foreach ($meta as $key => $value) {
                 if (!array_key_exists($key, $post['meta'])) {
                     delete_post_meta($insert_post_id, $key);
                 }
             }
         }
     }
     if (!empty($post['meta'])) {
         // add/update from source
         foreach ($post['meta'] as $key => $value) {
             if (!get_post_meta($insert_post_id, $key)) {
                 add_post_meta($insert_post_id, $key, $value);
             } else {
                 update_post_meta($insert_post_id, $key, $value);
             }
         }
     }
     if (!empty($post['featured_image'])) {
         // check to see if the item already exists and associate it if we can,
         // else shove it in a transient for later so we can process it when the
         // attachments come in
         try {
             $featured_image = new cfd_attachment(array('guid' => $post['featured_image']['guid'], 'shallow_fetch' => true));
             update_post_meta($insert_post_id, '_thumbnail_id', $featured_image->id());
         } catch (Exception $e) {
             $saved = $this->hold_featured_image_data($insert_post_id, $post['featured_image']);
         }
     }
     $item_change['post_types'][$post['post']['post_type']][$post['post']['guid']] = 'new';
     if (!empty($local_post)) {
         if ($post['post']['post_type'] == 'attachment') {
             $revision = array($local_post->get_data_for_transit());
             array_walk_recursive($revision, array($this, 'object_to_array'));
             $revision = current($revision);
             $revision['author'] = $revision['author']['user_login'];
             // put the taxonomies in place
             $taxes = $revision['taxonomies'];
             $revision['taxonomies'] = array();
             if (!empty($taxes)) {
                 foreach ($taxes as $tax_type => $terms) {
                     if (!empty($terms)) {
                         // store just the slugs with the post data
                         $revision['taxonomies'][$tax_type] = array_keys($terms);
                     }
                 }
             }
             // We're not gonna try to rollback or handle differences in the actual file, so put the new data in the revision content
             $revision['file'] = $post['file'];
             $item_change['post_types'][$post['post']['post_type']][$post['post']['guid']] = $revision;
         } else {
             // revision
             $revision = wp_get_post_revisions($local_post->post->ID, array('showposts' => 1));
             $item_change['post_types'][$post['post']['post_type']][$post['post']['guid']] = current($revision)->ID;
             // handle differences in attachments, disassociate attachments that have been removed from this post
             if (!empty($local_post->attachments) && count($local_post->attachments)) {
                 foreach ($local_post->attachments as $guid => $attachment) {
                     if (!array_key_exists($guid, $post['attachments'])) {
                         // attachment is no longer part of this post, clear its parent association
                         wp_update_post(array('ID' => $attachment->post->ID, 'post_parent' => 0));
                     }
                 }
             }
         }
     }
     $this->log_item_change($item_change);
     $this->add_import_message('post_types.' . $post['post']['post_type'], '__notice__', sprintf(__('Post "%s" successfully imported', 'cf-deploy'), esc_html($post['post']['post_title'])));
     // cleanup
     if (!empty($local_post)) {
         unset($local_post);
     }
     return true;
 }