/** * Returns an array of page objects indexed by page ID * * TODO: Function incomplete; most arguments ignored. Sort order should allow +1 column * @param $args mixed Wordpress-style arguments (string or array) * @return array Array of pages keyed on page ID or FALSE on problem */ function bu_navigation_get_posts($args = '') { global $wpdb, $bu_navigation_plugin; $defaults = array('post_types' => array('page'), 'post_status' => array('publish'), 'sections' => null, 'post__in' => null, 'max_items' => '', 'include_links' => true, 'suppress_filter_posts' => false, 'suppress_urls' => false); $r = wp_parse_args($args, $defaults); // Start building the query $where = $orderby = ''; // Post fields to return $fields = array('ID', 'post_date', 'post_title', 'post_excerpt', 'post_name', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_status', 'post_password'); $fields = apply_filters('bu_navigation_filter_fields', $fields); $fields = implode(",", $fields); // Append post types $post_types = $r['post_types']; if ('any' != $post_types) { if (is_string($post_types)) { $post_types = explode(',', $post_types); } $post_types = (array) $post_types; $post_types = array_map('trim', $post_types); // Include links? if ($r['include_links'] && !in_array(BU_NAVIGATION_LINK_POST_TYPE, $post_types)) { if (in_array('page', $post_types) && count($post_types) == 1) { $post_types[] = BU_NAVIGATION_LINK_POST_TYPE; } } if (is_object($bu_navigation_plugin) && !$bu_navigation_plugin->supports('links')) { $index = array_search(BU_NAVIGATION_LINK_POST_TYPE, $post_types); if ($index !== false) { unset($post_types[$index]); } } $post_types = implode("','", $post_types); $where .= " AND post_type IN ('{$post_types}')"; } // Append post statuses $post_status = $r['post_status']; if ('any' != $post_status) { if (is_string($post_status)) { $post_status = explode(',', $post_status); } $post_status = (array) $post_status; $post_status = implode("','", array_map('trim', $post_status)); $where .= " AND post_status IN ('{$post_status}')"; } // Limit result set to posts in specific sections if (is_array($r['sections']) && count($r['sections']) > 0) { $sections = array_map('absint', $r['sections']); $sections = implode(',', array_unique($sections)); $where .= " AND post_parent IN ({$sections})"; } // Limit to specific posts if (is_array($r['post__in']) && count($r['post__in']) > 0) { $post__in = array_map('absint', $r['post__in']); $post__in = implode(',', array_unique($post__in)); $where .= " AND ID IN({$post__in})"; } // Result sorting $orderby = 'ORDER BY post_parent ASC, menu_order ASC'; // Execute query, fetch results as objects in an array keyed on posts.ID $posts = $wpdb->get_results("SELECT {$fields} FROM {$wpdb->posts} WHERE 1=1 {$where} {$orderby}", OBJECT_K); if (!is_array($posts) || count($posts) == 0) { return false; } // Add url property to each post object ($post->url = permalink) if (!$r['suppress_urls']) { $posts = bu_navigation_get_urls($posts); } // Allow custom filtering of posts retrieved using this function if (!$r['suppress_filter_posts']) { $posts = apply_filters('bu_navigation_filter_posts', $posts); $posts = apply_filters('bu_navigation_filter_pages', $posts); } // Chop off anything great than max_items if ($r['max_items'] && is_array($posts) && count($posts) > 0) { $items = array(); $nItems = 0; foreach ($posts as $id => $post) { if ($nItems >= $r['max_items']) { break; } $items[$id] = $post; $nItems++; } $posts = $items; } return $posts; }
/** * Covers bu_navigation_get_urls() */ public function test_bu_navigation_get_urls() { // Get test page ids $parent = $this->posts['parent']; $grandchild_one = $this->posts['grandchild_one']; $test_child = $this->posts['test_child']; // Get all pages $args = array('post_types' => array('page', 'bu_link', 'test')); $pages = bu_navigation_get_pages($args); // Get the base url $base_url = trailingslashit(get_option('home')); // Remove current url unset($pages[$parent]->url); unset($pages[$grandchild_one]->url); unset($pages[$test_child]->url); // Use get_urls to get the new url $pages = bu_navigation_get_urls($pages); // Test to make sure the url is the expected one $this->assertEquals(get_permalink($parent), $pages[$parent]->url); $this->assertEquals(get_permalink($grandchild_one), $pages[$grandchild_one]->url); $this->assertEquals(get_permalink($test_child), $pages[$test_child]->url); }