/** * Add the post permalink as a property on the post object. * * Helpful when you need URLs for a large number of posts and don't want to * melt your server with 3000 calls to `get_permalink()`. * * This is most efficient when $pages contains the complete ancestry for each post. If any post * ancestors are missing when calculating hierarchical post names it will load them, * at the expensive of a few extra queries. * * @param array $pages An array of post objects keyed on post ID. Works with all post types. * @return array $pages The input array with $post->url set to the permalink for each post. */ function bu_navigation_get_urls($pages) { if (is_array($pages) && count($pages) > 0) { foreach ($pages as $page) { $url = ''; if ('page' === $page->post_type) { $url = bu_navigation_get_page_link($page, $pages); } else { if (BU_NAVIGATION_LINK_POST_TYPE === $page->post_type) { $url = $page->post_content; } else { $url = bu_navigation_get_post_link($page, $pages); } } $page->url = $url; } } return $pages; }
public function test_bu_navigation_get_page_link_unpublished() { $public = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'publish')); $draft_child = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'draft', 'post_parent' => $public)); $pending_child = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'pending', 'post_parent' => $public)); $draft = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'draft')); $public_draft_child = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'publish', 'post_parent' => $draft)); $pending = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'pending')); $public_pending_child = $this->factory->post->create(array('post_type' => 'page', 'post_status' => 'publish', 'post_parent' => $pending)); // Our functions require post objects $draft = get_post($draft); $draft_child = get_post($draft_child); $pending = get_post($draft_child); $pending_child = get_post($pending_child); $public_draft_child = get_post($public_draft_child); $public_pending_child = get_post($public_pending_child); // Root unpublished $this->assertEquals(get_page_link($draft), bu_navigation_get_page_link($draft)); $this->assertEquals(get_page_link($pending), bu_navigation_get_page_link($pending)); // Public parent, unpublished children $this->assertEquals(get_page_link($draft_child), bu_navigation_get_page_link($draft_child)); $this->assertEquals(get_page_link($pending_child), bu_navigation_get_page_link($pending_child)); // Draft parent, public children $this->assertEquals(get_page_link($public_draft_child), bu_navigation_get_page_link($public_draft_child)); $this->assertEquals(get_page_link($public_pending_child), bu_navigation_get_page_link($public_pending_child)); // Sample permalinks for unpublished pages $this->assertEquals(get_page_link($draft, false, true), bu_navigation_get_page_link($draft, array(), true)); $this->assertEquals(get_page_link($draft_child, false, true), bu_navigation_get_page_link($draft_child, array(), true)); $this->assertEquals(get_page_link($pending, false, true), bu_navigation_get_page_link($pending, array(), true)); $this->assertEquals(get_page_link($pending_child, false, true), bu_navigation_get_page_link($pending_child, array(), true)); }