/** * Provide a Post object you want to add to the current batch. * * @param Batch $batch * @param Post $post */ private function add_post(Batch $batch, Post $post) { // Make sure the post is not already in the batch. foreach ($batch->get_posts() as $post_in_batch) { if ($post->get_id() === $post_in_batch->get_id()) { return; } } if ($post->get_type() === 'attachment') { $this->add_attachment($batch, $post->get_id()); } // Catch issue with term ID not being set properly. try { $this->post_taxonomy_dao->get_post_taxonomy_relationships($post); } catch (Exception $e) { $this->api->add_preflight_message($batch->get_id(), $e->getMessage(), 'warning'); } $post->set_meta($this->postmeta_dao->get_postmetas_by_post_id($post->get_id())); /* * Make it possible for third-party developers to modify post before it * is added to batch. */ do_action('sme_prepare_post', $post, $batch); $batch->add_post($post); $post_meta = $post->get_meta(); $record_count = count($post_meta); for ($i = 0; $i < $record_count; $i++) { $post_meta[$i] = $this->add_related_posts($batch, $post_meta[$i]); } $post->set_meta($post_meta); }
/** * 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); }