From 45d3a1e45106512a5df18fd95624d1c096a398a8 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 7 Oct 2013 13:47:07 +0200 Subject: [PATCH] add get menu and add get list menu --- controllers/core.php | 60 +++++++++++++++++++---------- singletons/introspector.php | 76 ++++++++++++++++++++++--------------- 2 files changed, 86 insertions(+), 50 deletions(-) diff --git a/controllers/core.php b/controllers/core.php index a3dda64..1162536 100644 --- a/controllers/core.php +++ b/controllers/core.php @@ -5,7 +5,7 @@ */ class JSON_API_Core_Controller { - + public function info() { global $json_api; $php = ''; @@ -35,13 +35,13 @@ public function info() { ); } } - + public function get_recent_posts() { global $json_api; $posts = $json_api->introspector->get_posts(); return $this->posts_result($posts); } - + public function get_post() { global $json_api, $post; extract($json_api->query->get(array('id', 'slug', 'post_id', 'post_slug'))); @@ -102,7 +102,7 @@ public function get_page() { } else { $json_api->error("Include 'id' or 'slug' var in your request."); } - + // Workaround for https://core.trac.wordpress.org/ticket/12647 if (empty($posts)) { $url = $_SERVER['REQUEST_URI']; @@ -117,7 +117,7 @@ public function get_page() { } $posts = $json_api->introspector->get_posts(array('pagename' => $path)); } - + if (count($posts) == 1) { if (!empty($children)) { $json_api->introspector->attach_child_posts($posts[0]); @@ -129,7 +129,7 @@ public function get_page() { $json_api->error("Not found."); } } - + public function get_date_posts() { global $json_api; if ($json_api->query->date) { @@ -150,7 +150,7 @@ public function get_date_posts() { } return $this->posts_result($posts); } - + public function get_category_posts() { global $json_api; $category = $json_api->introspector->get_current_category(); @@ -162,7 +162,7 @@ public function get_category_posts() { )); return $this->posts_object_result($posts, $category); } - + public function get_tag_posts() { global $json_api; $tag = $json_api->introspector->get_current_tag(); @@ -174,7 +174,7 @@ public function get_tag_posts() { )); return $this->posts_object_result($posts, $tag); } - + public function get_author_posts() { global $json_api; $author = $json_api->introspector->get_current_author(); @@ -186,7 +186,7 @@ public function get_author_posts() { )); return $this->posts_object_result($posts, $author); } - + public function get_search_results() { global $json_api; if ($json_api->query->search) { @@ -198,7 +198,7 @@ public function get_search_results() { } return $this->posts_result($posts); } - + public function get_date_index() { global $json_api; $permalinks = $json_api->introspector->get_date_archive_permalinks(); @@ -208,7 +208,7 @@ public function get_date_index() { 'tree' => $tree ); } - + public function get_category_index() { global $json_api; $categories = $json_api->introspector->get_categories(); @@ -217,7 +217,7 @@ public function get_category_index() { 'categories' => $categories ); } - + public function get_tag_index() { global $json_api; $tags = $json_api->introspector->get_tags(); @@ -226,7 +226,7 @@ public function get_tag_index() { 'tags' => $tags ); } - + public function get_author_index() { global $json_api; $authors = $json_api->introspector->get_authors(); @@ -235,7 +235,7 @@ public function get_author_index() { 'authors' => array_values($authors) ); } - + public function get_page_index() { global $json_api; $pages = array(); @@ -258,7 +258,27 @@ public function get_page_index() { 'pages' => $pages ); } - + + public function get_menu() { + + global $json_api; + $menu = $json_api->introspector->get_menu(); + return array( + 'menu' => $menu + ); + + } + + public function get_list_menu() { + + global $json_api; + $menu = $json_api->introspector->get_list_menu(); + return array( + 'menus' => $menu + ); + + } + public function get_nonce() { global $json_api; extract($json_api->query->get(array('controller', 'method'))); @@ -281,7 +301,7 @@ public function get_nonce() { $json_api->error("Include 'controller' and 'method' vars in your request."); } } - + protected function get_object_posts($object, $id_var, $slug_var) { global $json_api; $object_id = "{$type}_id"; @@ -306,7 +326,7 @@ protected function get_object_posts($object, $id_var, $slug_var) { } return $posts; } - + protected function posts_result($posts) { global $wp_query; return array( @@ -316,7 +336,7 @@ protected function posts_result($posts) { 'posts' => $posts ); } - + protected function posts_object_result($posts, $object) { global $wp_query; // Convert something like "JSON_API_Category" into "category" @@ -328,7 +348,7 @@ protected function posts_object_result($posts, $object) { 'posts' => $posts ); } - + } ?> diff --git a/singletons/introspector.php b/singletons/introspector.php index 35fc63a..37fc731 100644 --- a/singletons/introspector.php +++ b/singletons/introspector.php @@ -1,7 +1,7 @@ set_posts_query($query); @@ -16,13 +16,13 @@ public function get_posts($query = false, $wp_posts = false) { } return $output; } - + public function get_date_archive_permalinks() { $archives = wp_get_archives('echo=0'); preg_match_all("/href='([^']+)'/", $archives, $matches); return $matches[1]; } - + public function get_date_archive_tree($permalinks) { $tree = array(); foreach ($permalinks as $url) { @@ -46,7 +46,7 @@ public function get_date_archive_tree($permalinks) { } return $tree; } - + public function get_date_archive_count($year, $month) { if (!isset($this->month_archives)) { global $wpdb; @@ -65,7 +65,7 @@ public function get_date_archive_count($year, $month) { } return $this->month_archives["$year$month"]; } - + public function get_categories() { $wp_categories = get_categories(); $categories = array(); @@ -77,7 +77,23 @@ public function get_categories() { } return $categories; } - + + public function get_menu() { + global $json_api; + extract($json_api->query->get(array('name'))); + + $items = wp_get_nav_menu_items($name); + return $items; + + } + public function get_list_menu() { + global $json_api; + $menus = get_terms('nav_menu'); + //$items = get_registered_nav_menus(); + return $menus; + + } + public function get_current_category() { global $json_api; extract($json_api->query->get(array('id', 'slug', 'category_id', 'category_slug'))); @@ -96,22 +112,22 @@ public function get_current_category() { } return null; } - + public function get_category_by_id($category_id) { $wp_category = get_term_by('id', $category_id, 'category'); return $this->get_category_object($wp_category); } - + public function get_category_by_slug($category_slug) { $wp_category = get_term_by('slug', $category_slug, 'category'); return $this->get_category_object($wp_category); } - + public function get_tags() { $wp_tags = get_tags(); return array_map(array(&$this, 'get_tag_object'), $wp_tags); } - + public function get_current_tag() { global $json_api; extract($json_api->query->get(array('id', 'slug', 'tag_id', 'tag_slug'))); @@ -130,17 +146,17 @@ public function get_current_tag() { } return null; } - + public function get_tag_by_id($tag_id) { $wp_tag = get_term_by('id', $tag_id, 'post_tag'); return $this->get_tag_object($wp_tag); } - + public function get_tag_by_slug($tag_slug) { $wp_tag = get_term_by('slug', $tag_slug, 'post_tag'); return $this->get_tag_object($wp_tag); } - + public function get_authors() { global $wpdb; $author_ids = $wpdb->get_col($wpdb->prepare(" @@ -155,7 +171,7 @@ public function get_authors() { $active_authors = array_filter($all_authors, array(&$this, 'is_active_author')); return $active_authors; } - + public function get_current_author() { global $json_api; extract($json_api->query->get(array('id', 'slug', 'author_id', 'author_slug'))); @@ -174,7 +190,7 @@ public function get_current_author() { } return null; } - + public function get_author_by_id($id) { $id = get_the_author_meta('ID', $id); if (!$id) { @@ -182,7 +198,7 @@ public function get_author_by_id($id) { } return new JSON_API_Author($id); } - + public function get_author_by_login($login) { global $wpdb; $id = $wpdb->get_var($wpdb->prepare(" @@ -192,7 +208,7 @@ public function get_author_by_login($login) { ", $login)); return $this->get_author_by_id($id); } - + public function get_comments($post_id) { global $wpdb; $wp_comments = $wpdb->get_results($wpdb->prepare(" @@ -209,7 +225,7 @@ public function get_comments($post_id) { } return $comments; } - + public function get_attachments($post_id) { $wp_attachments = get_children(array( 'post_type' => 'attachment', @@ -225,7 +241,7 @@ public function get_attachments($post_id) { } return $attachments; } - + public function attach_child_posts(&$post) { $post->children = array(); $wp_children = get_posts(array( @@ -241,21 +257,21 @@ public function attach_child_posts(&$post) { $this->attach_child_posts($child); } } - + protected function get_category_object($wp_category) { if (!$wp_category) { return null; } return new JSON_API_Category($wp_category); } - + protected function get_tag_object($wp_tag) { if (!$wp_tag) { return null; } return new JSON_API_Tag($wp_tag); } - + protected function is_active_author($author) { if (!isset($this->active_authors)) { $this->active_authors = explode(',', wp_list_authors(array( @@ -267,33 +283,33 @@ protected function is_active_author($author) { } return in_array($author->name, $this->active_authors); } - + protected function set_posts_query($query = false) { global $json_api, $wp_query; - + if (!$query) { $query = array(); } - + $query = array_merge($query, $wp_query->query); - + if ($json_api->query->page) { $query['paged'] = $json_api->query->page; } - + if ($json_api->query->count) { $query['posts_per_page'] = $json_api->query->count; } - + if ($json_api->query->post_type) { $query['post_type'] = $json_api->query->post_type; } - + if (!empty($query)) { query_posts($query); } } - + } ?>