/**
  * 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);
 }
 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);
 }
 /**
  * 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;
 }
 /**
  * Add post to the batch that is referenced through the post meta of
  * another post.
  *
  * Scope of this method has been extended to also include changing the ID
  * of the referenced post into using the GUID instead.
  *
  * @param Batch $batch
  * @param array $postmeta
  *
  * @return array
  */
 private function add_related_posts(Batch $batch, $postmeta)
 {
     // Check if this post meta key is in the array of post relationship keys.
     if (!in_array($postmeta['meta_key'], $batch->get_post_rel_keys())) {
         return $postmeta;
     }
     // Find post the current post holds a reference to.
     $post = $this->post_dao->find($postmeta['meta_value']);
     if (isset($post) && $post->get_id() !== null) {
         $this->add_post($batch, $post);
         /*
          * Change meta value to post GUID instead of post ID so we can later find
          * the reference on production.
          */
         $postmeta['meta_value'] = $post->get_guid();
     }
     return $postmeta;
 }
 /**
  * Publish a post sent to production.
  *
  * New posts that are sent to production will have a post status of
  * 'publish'. Since we don't want the post to go public until all data
  * has been synced from content stage, post status has been changed to
  * 'draft'. Post status is now changed back to 'publish'.
  *
  * @param Post $post
  */
 public function publish_post(Post $post)
 {
     $prod_id = $this->post_dao->get_id_by_guid($post->get_guid());
     if (!$prod_id) {
         $msg = sprintf('No post with GUID %s found on production.', $post->get_guid());
         $this->api->add_deploy_message($this->batch->get_id(), $msg, 'error');
         return;
     }
     /*
      * Trigger an action before changing the post status to give other plug-ins
      * a chance to act before the post goes public (e.g. cache warm-up).
      */
     do_action('sme_pre_publish_post', $prod_id, get_current_blog_id());
     /*
      * Publish the new post if post status from staging environment is set to
      * "publish".
      */
     if ($post->get_post_status() == 'publish') {
         $this->post_dao->update_post_status($prod_id, 'publish');
     }
 }
 /**
  * Find post ID by providing GUID.
  *
  * @param string $guid
  *
  * @return int
  */
 public function get_post_id_by_guid($guid)
 {
     return $this->post_dao->get_id_by_guid($guid);
 }
 /**
  * Edit a content batch. Lets the user decide what posts to put in the
  * batch.
  */
 public function edit_batch()
 {
     $batch_id = null;
     $order_by = 'post_modified';
     $order = 'desc';
     $per_page = 50;
     $paged = 1;
     // Make sure a query param ID exists in current URL.
     if (!isset($_GET['id'])) {
         wp_die(__('No batch ID has been provided.', 'sme-content-staging'));
     }
     // Get batch ID from URL query param.
     if ($_GET['id'] > 0) {
         $batch_id = intval($_GET['id']);
     }
     $batch = $this->api->get_batch($batch_id);
     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'];
     }
     // Get IDs of posts user has selected to include in this batch.
     $post_ids = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post');
     /*
      * When fetching post IDs an empty string could be returned if no
      * post meta record with the given key exist since before. To
      * ensure the system can rely on us working with an array we perform a
      * check setting $post_ids to array if it is currently an empty.
      */
     if (!$post_ids) {
         $post_ids = array();
     }
     // Get selected posts.
     $selected_posts = array();
     $chunks = array_chunk($post_ids, $per_page);
     if (isset($chunks[$paged - 1])) {
         $use_post_ids = $chunks[$paged - 1];
         $selected_posts = $this->post_dao->find_by_ids($use_post_ids);
     }
     $status = apply_filters('sme_post_list_statuses', array('publish'));
     // Get posts user can select to include in the batch.
     $posts = $this->post_dao->get_posts($status, $order_by, $order, $per_page, $paged, $post_ids);
     $total_posts = $this->post_dao->get_posts_count($status);
     $posts = array_merge($selected_posts, $posts);
     // Create and prepare table of posts.
     $table = new Post_Table($batch);
     $table->items = $posts;
     $table->set_pagination_args(array('total_items' => $total_posts, 'per_page' => $per_page));
     $table->prepare_items();
     $type = get_post_type_object('sme_content_batch');
     if (!$batch->get_id()) {
         $label = $type->labels->new_item;
     } else {
         $label = $type->labels->edit_item;
     }
     // Custom filters for finding posts to include in batch.
     $filters = apply_filters('sme_post_filters', $filters = '', $table);
     // Get WordPress options settings for this batch.
     $wp_options = $this->get_wp_options_settings($batch);
     $data = array('batch' => $batch, 'label' => $label, 'filters' => $filters, 'table' => $table, 'post_ids' => implode(',', $post_ids), 'wp_options' => $wp_options);
     $this->template->render('edit-batch', $data);
 }