/**
  * Prepare batch with relevant content.
  *
  * @param Batch $batch
  */
 public function prepare(Batch $batch)
 {
     // Batch not yet created, nothing to prepare.
     if (!$batch->get_id()) {
         return;
     }
     $post_ids = array();
     $batch->set_post_rel_keys(apply_filters('sme_post_relationship_keys', array()));
     // Clean batch from any old content.
     $batch->set_attachments(array());
     $batch->set_users(array());
     $batch->set_posts(array());
     $batch->set_options(array());
     // Get IDs of posts user has selected to include in this batch.
     $meta = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post');
     // Ensure that we got an array back when looking for posts IDs in DB.
     if (is_array($meta)) {
         $post_ids = $meta;
     }
     $this->add_table_prefix($batch);
     $this->add_posts($batch, $post_ids);
     $this->add_users($batch);
     $this->add_options($batch);
     // Add the admin URL of content stage to the batch.
     $batch->add_custom_data('sme_content_stage_admin_url', admin_url());
 }
 /**
  * Deploy a batch from content stage to production.
  *
  * Runs on content stage when a deploy request has been received.
  *
  * @param Batch $batch
  * @param bool  $auto_import If set to false the batch will be sent to production but the import
  *                           will not be triggered.
  *
  * @return array
  */
 public function deploy(Batch $batch, $auto_import = true)
 {
     /*
      * Give third-party developers the option to import images before batch
      * is sent to production.
      */
     do_action('sme_deploy_custom_attachment_importer', $batch->get_attachments(), $batch);
     /*
      * Make it possible for third-party developers to alter the list of
      * attachments to deploy.
      */
     $batch->set_attachments(apply_filters('sme_deploy_attachments', $batch->get_attachments(), $batch));
     // Hook in before deploy.
     do_action('sme_deploy', $batch);
     // Start building request to send to production.
     $request = array('batch' => $batch, 'auto_import' => $auto_import);
     $response = $this->client->request('smeContentStaging.import', $request);
     // Batch deploy in progress.
     $response = apply_filters('sme_deploying', $response, $batch);
     // Get status received from production.
     $status = isset($response['status']) ? $response['status'] : 1;
     // Get messages received from production.
     $messages = isset($response['messages']) ? $response['messages'] : array();
     /*
      * Batch has been deployed and should no longer be accessible by user,
      * delete it (not actually deleting the batch, just setting it to draft
      * to make it invisible to users).
      */
     $this->batch_dao->delete_batch($batch);
     // Return status and messages.
     return array('status' => $status, 'messages' => $messages);
 }
 public function init()
 {
     $order_by = 'post_modified';
     $order = 'desc';
     $per_page = 10;
     $paged = 1;
     $status = array('draft');
     $posts = array();
     if (isset($_GET['orderby'])) {
         $order_by = $_GET['orderby'];
     }
     if (isset($_GET['order'])) {
         $order = $_GET['order'];
     }
     if (isset($_GET['per_page'])) {
         $per_page = $_GET['per_page'];
     }
     if (isset($_GET['paged'])) {
         $paged = $_GET['paged'];
     }
     $count = $this->batch_dao->count($status);
     $batches = $this->batch_dao->get_batches($status, $order_by, $order, $per_page, $paged);
     foreach ($batches as $batch) {
         // Get IDs of posts user selected to include in this batch.
         $post_ids = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post');
         if (!is_array($post_ids)) {
             $post_ids = array();
         }
         $posts = $this->post_dao->find_by_ids($post_ids);
         $batch->set_posts($posts);
     }
     // Prepare table of batches.
     $table = new Batch_History_Table();
     $table->items = $batches;
     $table->set_pagination_args(array('total_items' => $count, 'per_page' => $per_page));
     $table->prepare_items();
     $data = array('table' => $table);
     $this->template->render('batch-history', $data);
 }
 /**
  * Create/update a batch based on input data submitted by user from the
  * Edit Batch page.
  *
  * @param Batch $batch
  * @param array $request_data Input data from the user. Should contain
  * two array keys:
  * 'batch_title' - Title of this batch.
  * 'posts' - Posts to include in this batch.
  */
 private function handle_edit_batch_form_data(Batch $batch, $request_data)
 {
     // Check if a title has been set.
     if (isset($request_data['batch_title']) && $request_data['batch_title']) {
         $batch->set_title($request_data['batch_title']);
     } else {
         $batch->set_title('Batch ' . date('Y-m-d H:i:s'));
     }
     if ($batch->get_id() <= 0) {
         // Create new batch.
         $this->batch_dao->insert($batch);
     } else {
         // Update existing batch.
         $batch->set_status('publish');
         $this->batch_dao->update_batch($batch);
     }
     // IDs of posts user has selected to include in this batch.
     $selected_post_ids = array();
     // Check if any posts to include in batch has been selected.
     if (isset($request_data['post_ids']) && $request_data['post_ids']) {
         $selected_post_ids = array_map('intval', explode(',', $request_data['post_ids']));
     }
     // Set whether WordPress options should be included in batch or not.
     $this->should_include_wp_options($batch, $request_data);
     // Posts that was previously in this batch.
     $old_post_ids = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post');
     // Post IDs to add to this batch.
     $add_post_ids = array_diff($selected_post_ids, $old_post_ids);
     // Post IDs to remove from this batch.
     $remove_post_ids = array_diff($old_post_ids, $selected_post_ids);
     // Add post IDs to batch.
     foreach ($add_post_ids as $post_id) {
         $this->batch_dao->add_post_meta($batch->get_id(), 'sme_selected_post', $post_id);
     }
     // Remove post IDs from batch.
     foreach ($remove_post_ids as $post_id) {
         $this->batch_dao->delete_post_meta($batch->get_id(), 'sme_selected_post', $post_id);
     }
 }