/** * Get post by permalink components. * * @param Post $post * * @return Post * * @throws Exception */ public function get_by_permalink(Post $post) { // Parent post ID. $parent_id = $post->get_parent() !== null ? $post->get_parent()->get_id() : 0; // Select post with a specific GUID ending. $query = $this->wpdb->prepare('SELECT * FROM ' . $this->wpdb->posts . ' WHERE post_parent = %s AND post_name = %s AND post_type = %s', $parent_id, $post->get_name(), $post->get_type()); $result = $this->wpdb->get_results($query, ARRAY_A); if (empty($result)) { return null; } if (count($result) > 1) { // Get all post IDs. $ids = array_map(function ($row) { return $row['ID']; }, $result); // Turn array of IDs into string of IDs. $ids = implode(', ', $ids); throw new Exception(sprintf('Permalink components not unique (post_parent: %d, post_name: %s, post_type: %s). Set unique permalink for the following post IDs: %s', $parent_id, $post->get_name(), $post->get_type(), $ids)); } if (isset($result[0]) && isset($result[0]['ID'])) { return $this->create_object($result[0]); } return null; }
/** * 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); }