/**
  * Populate a Post object with Post_Taxonomy relationships.
  *
  * @param Post $post
  */
 public function get_post_taxonomy_relationships(Post $post)
 {
     $query = $this->wpdb->prepare('SELECT * FROM ' . $this->get_table() . ' WHERE object_id = %d', $post->get_id());
     foreach ($this->wpdb->get_results($query, ARRAY_A) as $relationship) {
         $taxonomy = $this->taxonomy_dao->find($relationship['term_taxonomy_id']);
         if ($taxonomy instanceof Taxonomy) {
             $post->add_post_taxonomy(new Post_Taxonomy($post, $taxonomy));
         }
     }
 }
 /**
  * @param Post $post
  */
 public function set_post(Post $post)
 {
     $this->set_id($post->get_id() . '-' . $this->taxonomy->get_id());
     $this->post = $post;
 }
 /**
  * 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 postmeta for a specific post.
  *
  * Never call before all posts has been imported! In case you do
  * relationships between post IDs on content stage and production has not
  * been established and import of postmeta will fail!
  *
  * Start by changing content staging post IDs to production IDs.
  *
  * The content staging post ID is used as a key in the post relations
  * array and the production post ID is used as value.
  *
  * @param Post $post
  */
 public function import_post_meta(Post $post)
 {
     $meta = $post->get_meta();
     // Keys in postmeta table containing relationship to another post.
     $keys = $this->batch->get_post_rel_keys();
     // Stage ID of current post.
     $stage_id = $post->get_id();
     // Get the production ID of the current post.
     $prod_id = $this->post_dao->get_id_by_guid($post->get_guid());
     // Post not found on production.
     if (!$prod_id) {
         $message = sprintf('Post not found on production. Stage ID %d, GUID: %s', $stage_id, $prod_id);
         $this->api->add_deploy_message($this->batch->get_id(), $message, 'error');
         return;
     }
     for ($i = 0; $i < count($meta); $i++) {
         // Update post ID to point at the post ID on production.
         $meta[$i]['post_id'] = $prod_id;
         // TODO Remove check for "master"
         if (in_array($meta[$i]['meta_key'], $keys) && !empty($meta[$i]['meta_value']) && $meta[$i]['meta_value'] !== 'master') {
             // Post ID this meta value is referring to.
             $referenced_post_id = $this->post_dao->get_id_by_guid($meta[$i]['meta_value']);
             // Referenced post could not be found.
             if (!$referenced_post_id) {
                 $referenced_post_id = 0;
                 $message = 'Failed updating relationship between posts (blog ID %d). The relationship is defined in the postmeta table. ';
                 $message .= '<ul>';
                 $message .= '<li>Stage ID referencing post: %d</li>';
                 $message .= '<li>Production ID referencing post: %d</li>';
                 $message .= '<li>Key holding referenced post: %s</li>';
                 $message .= '<li>GUID referenced post: %s</li>';
                 $message .= '</ul>';
                 $message = sprintf($message, get_current_blog_id(), $stage_id, $prod_id, $meta[$i]['meta_key'], $meta[$i]['meta_value']);
                 $this->api->add_deploy_message($this->batch->get_id(), $message, 'warning');
             }
             // Update meta value to point at the post ID on production.
             $meta[$i]['meta_value'] = $referenced_post_id;
         }
     }
     $this->postmeta_dao->update_postmeta_by_post($prod_id, $meta);
 }
 /**
  * Display checkbox (e.g. for bulk actions). The checkbox should have the
  * value of the post ID.
  *
  * @param Post $post
  * @return string Text to be placed inside the column.
  */
 public function column_cb($post)
 {
     return sprintf('<input type="checkbox" id="sme_select_post_%s" class="sme-select-post" name="%s[]" value="%s"/>', $post->get_id(), $this->_args['plural'], $post->get_id());
 }