/** * Get post environment diff object for a batch. * * @param Batch $batch * * @return array */ public function get_post_diffs(Batch $batch) { $objects = array(); $diffs = get_post_meta($batch->get_id(), 'sme_post_diff'); if (empty($diffs)) { return $objects; } foreach ($diffs as $diff) { $obj = new Post_Env_Diff($diff['stage_id']); $obj->set_revision_id($diff['revision_id']); $obj->set_prod_id($diff['prod_id']); $obj->set_stage_status($diff['stage_status']); $obj->set_parent_guid($diff['parent_guid']); $objects[$diff['stage_id']] = $obj; } return $objects; }
/** * Import post. * * @param Post $post */ public function import_post(Post $post) { // Notify listeners that post is about to be imported. do_action('sme_post_import', $post, $this->batch); $publish_status_changed = false; /* * Create object that can keep track of differences between stage and * production post. */ $post_diff = new Post_Env_Diff($post->get_id()); $post_diff->set_stage_status($post->get_post_status()); // Check if post has any parent. if ($post->get_parent() !== null) { $post_diff->set_parent_guid($post->get_parent()->get_guid()); } /* * Check if post already exist on production, if it does then update the * old version of the post rather then creating a new post. */ if (($prod_revision = $this->post_dao->get_by_guid($post->get_guid())) !== null) { /* * This is an existing post. */ $post_diff->set_prod_id($prod_revision->get_id()); $post->set_id($prod_revision->get_id()); $this->post_dao->update_post($post); } else { /* * This is a new post. */ // Turn published posts into drafts for now. if ($post->get_post_status() == 'publish') { $post->set_post_status('draft'); $publish_status_changed = true; } // Insert post. $this->post_dao->insert($post); // Reset publish status. if ($publish_status_changed) { $post->set_post_status('publish'); } // Store new production ID in diff. $post_diff->set_prod_id($post->get_id()); } // Add post diff to array of post diffs. $this->add_post_diff($post_diff); // Clear old post/taxonomy relationships. foreach ($post->get_post_taxonomy_relationships() as $post_taxonomy) { $this->post_taxonomy_dao->clear_post_taxonomy_relationships($post_taxonomy); } // Import post/taxonomy relationships. foreach ($post->get_post_taxonomy_relationships() as $post_taxonomy) { // Import taxonomy. $this->import_taxonomy($post_taxonomy->get_taxonomy()); $this->post_taxonomy_dao->insert($post_taxonomy); } // Notify listeners that post has been imported. do_action('sme_post_imported', $post, $this->batch); }