/**
  * Prepare a batch.
  *
  * Here we take all available information about a batch and use it to
  * populate the batch with actual content. Example on available
  * information would be what post IDs should be included in the batch.
  *
  * Runs on content stage.
  *
  * @param Batch $batch
  */
 public function prepare($batch)
 {
     // Hook in before batch is built
     do_action('sme_prepare', $batch);
     $this->batch_mgr->prepare($batch);
     // Let third-party developers filter batch data.
     $batch->set_posts(apply_filters('sme_prepare_posts', $batch->get_posts()));
     $batch->set_attachments(apply_filters('sme_prepare_attachments', $batch->get_attachments()));
     $batch->set_users(apply_filters('sme_prepare_users', $batch->get_users()));
     // Hook in after batch has been built.
     do_action('sme_prepared', $batch);
     // Store prepared batch.
     $this->batch_dao->update_batch($batch);
 }
 /**
  * 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);
     }
 }