/**
  * Verify that a post is ready for deploy.
  *
  * @param Post  $post
  * @param Batch $batch
  */
 public function verify_post(Post $post, Batch $batch)
 {
     /*
      * If more then one post is found when searching posts with a specific
      * GUID, then add an error message. Two or more posts should never share
      * the same GUID.
      */
     try {
         $revision = $this->post_dao->get_by_guid($post->get_guid());
     } catch (Exception $e) {
         $this->api->set_preflight_status($batch->get_id(), 2);
         $this->api->add_preflight_message($batch->get_id(), $e->getMessage(), 'error');
         return;
     }
     // Check if parent post exist on production or in batch.
     if (!$this->parent_post_exists($post, $batch->get_posts())) {
         // Fail pre-flight.
         $this->api->set_preflight_status($batch->get_id(), 2);
         // Admin URL of content stage.
         $admin_url = $batch->get_custom_data('sme_content_stage_admin_url');
         $message = sprintf('Post <a href="%s" target="_blank">%s</a> has a parent post that does not exist on production and is not part of this batch. Include post <a href="%s" target="_blank">%s</a> in this batch to resolve this issue.', $admin_url . 'post.php?post=' . $post->get_id() . '&action=edit', $post->get_title(), $admin_url . 'post.php?post=' . $post->get_parent()->get_id() . '&action=edit', $post->get_parent()->get_title());
         $this->api->add_preflight_message($batch->get_id(), $message, 'error');
         return;
     }
 }
 /**
  * Batch has been successfully imported.
  *
  * @param Batch $batch
  */
 public function imported(Batch $batch)
 {
     $links = array();
     $output = '';
     $types = array('page', 'post');
     // Only keep published posts of type $types.
     $posts = array_filter($batch->get_posts(), function (Post $post) use($types) {
         return $post->get_post_status() == 'publish' && in_array($post->get_type(), $types);
     });
     // Create links for each of the posts.
     foreach ($posts as $post) {
         $post_id = $this->post_dao->get_id_by_guid($post->get_guid());
         $links[] = array('link' => get_permalink($post_id), 'title' => $post->get_title());
     }
     $links = apply_filters('sme_imported_post_links', $links);
     foreach ($links as $link) {
         $output .= '<li><a href="' . $link['link'] . '" target="_blank">' . $link['title'] . '</a></li>';
     }
     if ($output !== '') {
         $output = '<ul>' . $output . '</ul>';
         $message = '<h3>Posts deployed to ' . get_bloginfo('name') . ':</h3>' . $output;
         $this->api->add_deploy_message($batch->get_id(), $message, 'info', 102);
     }
     $this->api->add_deploy_message($batch->get_id(), 'Batch has been successfully imported!', 'success', 101);
 }
 /**
  * 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);
 }
 /**
  * Remove posts that has been deleted on content stage.
  *
  * Runs on production server after batch has been sent from content stage
  * and received by the production server.
  *
  * @param array $posts
  * @param Batch $batch
  */
 public function import($posts, Batch $batch)
 {
     // No posts provided.
     if (empty($posts)) {
         return;
     }
     // String of deleted post IDs.
     $stage_post_ids = array();
     foreach ($posts as $post) {
         if (!isset($post['guid'])) {
             continue;
         }
         $post_id = $this->api->get_post_id_by_guid($post['guid']);
         wp_delete_post($post_id, true);
         if (isset($post['id'])) {
             array_push($stage_post_ids, $post['id']);
         }
     }
     // String of deleted content staging IDs.
     $str = implode(',', $stage_post_ids);
     // Current blog ID.
     $blog_id = get_current_blog_id();
     $message = sprintf('Posts deleted on Content Stage has been removed from Production. <span class="hidden" data-blog-id="%d">%s</span>', $blog_id, $str);
     $this->api->add_deploy_message($batch->get_id(), $message, 'info', 104);
 }
 /**
  * Helper method to trigger a background import.
  */
 public function run_background_import()
 {
     // Make sure a background import has been requested.
     if (!isset($_GET['sme_background_import']) || !$_GET['sme_background_import']) {
         return;
     }
     // Make sure a batch ID has been provided.
     if (!isset($_GET['sme_batch_id']) || !$_GET['sme_batch_id']) {
         return;
     }
     // Make sure a background import key has been provided.
     if (!isset($_GET['sme_import_key']) || !$_GET['sme_import_key']) {
         return;
     }
     $batch_id = intval($_GET['sme_batch_id']);
     $import_key = $_GET['sme_import_key'];
     $batch_dao = $this->dao_factory->create('Batch');
     // Get batch from database.
     $batch = $batch_dao->find($batch_id);
     // No batch to import found, error.
     if (!$batch) {
         error_log(sprintf('Batch with ID %d could not be imported.', $batch_id));
         wp_die(__('Something went wrong', 'sme-content-staging'));
     }
     // Validate key.
     if ($import_key !== $this->api->get_import_key($batch->get_id())) {
         error_log('Unauthorized batch import attempt terminated.');
         $this->api->add_deploy_message($batch->get_id(), __('Something went wrong', 'sme-content-staging'), 'error');
         $this->api->set_deploy_status($batch->get_id(), 2);
         wp_die(__('Something went wrong', 'sme-content-staging'));
     }
     // Background import is running. Make the old import key useless.
     $this->api->generate_import_key($batch);
     // Create the importer.
     $importer = new Batch_Background_Importer($batch);
     // Trigger import.
     $importer->import();
 }
 /**
  * Runs on production when a import status request is received.
  *
  * @param array $args
  * @return string
  */
 public function import_status(array $args)
 {
     $this->xmlrpc_client->handle_request($args);
     $result = $this->xmlrpc_client->get_request_data();
     $batch = $this->batch_dao->find(intval($result['batch_id']));
     $importer = $this->importer_factory->get_importer($batch);
     $importer->status();
     $response = array('status' => $this->api->get_deploy_status($batch->get_id()), 'messages' => $this->api->get_deploy_messages($batch->get_id()));
     $response = apply_filters('sme_import_status_response', $response, $batch);
     // Get status.
     $status = isset($response['status']) ? $response['status'] : 2;
     // Get messages.
     $messages = isset($response['messages']) ? $response['messages'] : array();
     // Prepare and return the XML-RPC response data.
     return $this->xmlrpc_client->prepare_response(array('status' => $status, 'messages' => $messages));
 }
 public function tear_down()
 {
     // Import finished, update import status.
     $this->api->set_deploy_status($this->batch->get_id(), 3);
     do_action('sme_imported', $this->batch);
 }