/** * 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); }
/** * Add a message. * * Messages will be displayed to the user through the UI. * * @param int $post_id Post this message belongs to. * @param string $message The message. * @param string $type What type of message this is, can be any of: * info, warning, error, success * @param string $group Group this message is part of, use this to categorize different kind * of messages, e.g. preflight, deploy, etc. * @param int $code Set a specific code for this message. * * @throws Exception */ public function add_message($post_id, $message, $type = 'info', $group = null, $code = 0) { // Supported message types. $types = array('info', 'warning', 'error', 'success'); $types = apply_filters('sme_message_types', $types); if (!in_array($type, $types)) { throw new Exception('Unsupported message type: ' . $type); } $key = $this->get_message_key($group); $value = array('message' => $message, 'level' => $type, 'code' => $code); $this->postmeta_dao->add_post_meta($post_id, $key, $value); }
/** * 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); }