/** * 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); }
/** * Make sure parent post exist (if post has any) either in production * database or in batch. * * @param Post $post * @param array $posts * @return bool True if parent post exist (or post does not have a parent), false * otherwise. */ private function parent_post_exists(Post $post, $posts) { // Check if the post has a parent post. if ($post->get_parent() === null) { return true; } // Check if parent post exist on production server. if ($this->post_dao->get_by_guid($post->get_parent()->get_guid())) { return true; } // Parent post is not on production, look in this batch for parent post. foreach ($posts as $item) { if ($item->get_id() == $post->get_parent()->get_id()) { return true; } } return false; }
/** * Find post by GUID. * * @param string $guid * @return Post */ public function get_post_by_guid($guid) { return $this->post_dao->get_by_guid($guid); }