PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/json-api/controllers/core.php

https://gitlab.com/eita/agencia-consumo-responsavel
PHP | 339 lines | 306 code | 25 blank | 8 comment | 51 complexity | 2639d0efae4cf82dc05f76c4c6e3e51b MD5 | raw file
  1. <?php
  2. /*
  3. Controller name: Core
  4. Controller description: Basic introspection methods
  5. */
  6. class JSON_API_Core_Controller {
  7. public function info() {
  8. global $json_api;
  9. $php = '';
  10. if (!empty($json_api->query->controller)) {
  11. return $json_api->controller_info($json_api->query->controller);
  12. } else {
  13. $dir = json_api_dir();
  14. if (file_exists("$dir/json-api.php")) {
  15. $php = file_get_contents("$dir/json-api.php");
  16. } else {
  17. // Check one directory up, in case json-api.php was moved
  18. $dir = dirname($dir);
  19. if (file_exists("$dir/json-api.php")) {
  20. $php = file_get_contents("$dir/json-api.php");
  21. }
  22. }
  23. if (preg_match('/^\s*Version:\s*(.+)$/m', $php, $matches)) {
  24. $version = $matches[1];
  25. } else {
  26. $version = '(Unknown)';
  27. }
  28. $active_controllers = explode(',', get_option('json_api_controllers', 'core'));
  29. $controllers = array_intersect($json_api->get_controllers(), $active_controllers);
  30. return array(
  31. 'json_api_version' => $version,
  32. 'controllers' => array_values($controllers)
  33. );
  34. }
  35. }
  36. public function get_recent_posts() {
  37. global $json_api;
  38. $posts = $json_api->introspector->get_posts();
  39. return $this->posts_result($posts);
  40. }
  41. public function get_posts() {
  42. global $json_api;
  43. $url = parse_url($_SERVER['REQUEST_URI']);
  44. $defaults = array(
  45. 'ignore_sticky_posts' => true
  46. );
  47. $query = wp_parse_args($url['query']);
  48. unset($query['json']);
  49. unset($query['post_status']);
  50. $query = array_merge($defaults, $query);
  51. $posts = $json_api->introspector->get_posts($query);
  52. $result = $this->posts_result($posts);
  53. $result['query'] = $query;
  54. return $result;
  55. }
  56. public function get_post() {
  57. global $json_api, $post;
  58. $post = $json_api->introspector->get_current_post();
  59. if ($post) {
  60. $previous = get_adjacent_post(false, '', true);
  61. $next = get_adjacent_post(false, '', false);
  62. $response = array(
  63. 'post' => new JSON_API_Post($post)
  64. );
  65. if ($previous) {
  66. $response['previous_url'] = get_permalink($previous->ID);
  67. }
  68. if ($next) {
  69. $response['next_url'] = get_permalink($next->ID);
  70. }
  71. return $response;
  72. } else {
  73. $json_api->error("Not found.");
  74. }
  75. }
  76. public function get_page() {
  77. global $json_api;
  78. extract($json_api->query->get(array('id', 'slug', 'page_id', 'page_slug', 'children')));
  79. if ($id || $page_id) {
  80. if (!$id) {
  81. $id = $page_id;
  82. }
  83. $posts = $json_api->introspector->get_posts(array(
  84. 'page_id' => $id
  85. ));
  86. } else if ($slug || $page_slug) {
  87. if (!$slug) {
  88. $slug = $page_slug;
  89. }
  90. $posts = $json_api->introspector->get_posts(array(
  91. 'pagename' => $slug
  92. ));
  93. } else {
  94. $json_api->error("Include 'id' or 'slug' var in your request.");
  95. }
  96. // Workaround for https://core.trac.wordpress.org/ticket/12647
  97. if (empty($posts)) {
  98. $url = $_SERVER['REQUEST_URI'];
  99. $parsed_url = parse_url($url);
  100. $path = $parsed_url['path'];
  101. if (preg_match('#^http://[^/]+(/.+)$#', get_bloginfo('url'), $matches)) {
  102. $blog_root = $matches[1];
  103. $path = preg_replace("#^$blog_root#", '', $path);
  104. }
  105. if (substr($path, 0, 1) == '/') {
  106. $path = substr($path, 1);
  107. }
  108. $posts = $json_api->introspector->get_posts(array('pagename' => $path));
  109. }
  110. if (count($posts) == 1) {
  111. if (!empty($children)) {
  112. $json_api->introspector->attach_child_posts($posts[0]);
  113. }
  114. return array(
  115. 'page' => $posts[0]
  116. );
  117. } else {
  118. $json_api->error("Not found.");
  119. }
  120. }
  121. public function get_date_posts() {
  122. global $json_api;
  123. if ($json_api->query->date) {
  124. $date = preg_replace('/\D/', '', $json_api->query->date);
  125. if (!preg_match('/^\d{4}(\d{2})?(\d{2})?$/', $date)) {
  126. $json_api->error("Specify a date var in one of 'YYYY' or 'YYYY-MM' or 'YYYY-MM-DD' formats.");
  127. }
  128. $request = array('year' => substr($date, 0, 4));
  129. if (strlen($date) > 4) {
  130. $request['monthnum'] = (int) substr($date, 4, 2);
  131. }
  132. if (strlen($date) > 6) {
  133. $request['day'] = (int) substr($date, 6, 2);
  134. }
  135. $posts = $json_api->introspector->get_posts($request);
  136. } else {
  137. $json_api->error("Include 'date' var in your request.");
  138. }
  139. return $this->posts_result($posts);
  140. }
  141. public function get_category_posts() {
  142. global $json_api;
  143. $category = $json_api->introspector->get_current_category();
  144. if (!$category) {
  145. $json_api->error("Not found.");
  146. }
  147. $posts = $json_api->introspector->get_posts(array(
  148. 'cat' => $category->id
  149. ));
  150. return $this->posts_object_result($posts, $category);
  151. }
  152. public function get_tag_posts() {
  153. global $json_api;
  154. $tag = $json_api->introspector->get_current_tag();
  155. if (!$tag) {
  156. $json_api->error("Not found.");
  157. }
  158. $posts = $json_api->introspector->get_posts(array(
  159. 'tag' => $tag->slug
  160. ));
  161. return $this->posts_object_result($posts, $tag);
  162. }
  163. public function get_author_posts() {
  164. global $json_api;
  165. $author = $json_api->introspector->get_current_author();
  166. if (!$author) {
  167. $json_api->error("Not found.");
  168. }
  169. $posts = $json_api->introspector->get_posts(array(
  170. 'author' => $author->id
  171. ));
  172. return $this->posts_object_result($posts, $author);
  173. }
  174. public function get_search_results() {
  175. global $json_api;
  176. if ($json_api->query->search) {
  177. $posts = $json_api->introspector->get_posts(array(
  178. 's' => $json_api->query->search
  179. ));
  180. } else {
  181. $json_api->error("Include 'search' var in your request.");
  182. }
  183. return $this->posts_result($posts);
  184. }
  185. public function get_date_index() {
  186. global $json_api;
  187. $permalinks = $json_api->introspector->get_date_archive_permalinks();
  188. $tree = $json_api->introspector->get_date_archive_tree($permalinks);
  189. return array(
  190. 'permalinks' => $permalinks,
  191. 'tree' => $tree
  192. );
  193. }
  194. public function get_category_index() {
  195. global $json_api;
  196. $args = null;
  197. if (!empty($json_api->query->parent)) {
  198. $args = array(
  199. 'parent' => $json_api->query->parent
  200. );
  201. }
  202. $categories = $json_api->introspector->get_categories($args);
  203. return array(
  204. 'count' => count($categories),
  205. 'categories' => $categories
  206. );
  207. }
  208. public function get_tag_index() {
  209. global $json_api;
  210. $tags = $json_api->introspector->get_tags();
  211. return array(
  212. 'count' => count($tags),
  213. 'tags' => $tags
  214. );
  215. }
  216. public function get_author_index() {
  217. global $json_api;
  218. $authors = $json_api->introspector->get_authors();
  219. return array(
  220. 'count' => count($authors),
  221. 'authors' => array_values($authors)
  222. );
  223. }
  224. public function get_page_index() {
  225. global $json_api;
  226. $pages = array();
  227. $post_type = $json_api->query->post_type ? $json_api->query->post_type : 'page';
  228. // Thanks to blinder for the fix!
  229. $numberposts = empty($json_api->query->count) ? -1 : $json_api->query->count;
  230. $wp_posts = get_posts(array(
  231. 'post_type' => $post_type,
  232. 'post_parent' => 0,
  233. 'order' => 'ASC',
  234. 'orderby' => 'menu_order',
  235. 'numberposts' => $numberposts
  236. ));
  237. foreach ($wp_posts as $wp_post) {
  238. $pages[] = new JSON_API_Post($wp_post);
  239. }
  240. foreach ($pages as $page) {
  241. $json_api->introspector->attach_child_posts($page);
  242. }
  243. return array(
  244. 'pages' => $pages
  245. );
  246. }
  247. public function get_nonce() {
  248. global $json_api;
  249. extract($json_api->query->get(array('controller', 'method')));
  250. if ($controller && $method) {
  251. $controller = strtolower($controller);
  252. if (!in_array($controller, $json_api->get_controllers())) {
  253. $json_api->error("Unknown controller '$controller'.");
  254. }
  255. require_once $json_api->controller_path($controller);
  256. if (!method_exists($json_api->controller_class($controller), $method)) {
  257. $json_api->error("Unknown method '$method'.");
  258. }
  259. $nonce_id = $json_api->get_nonce_id($controller, $method);
  260. return array(
  261. 'controller' => $controller,
  262. 'method' => $method,
  263. 'nonce' => wp_create_nonce($nonce_id)
  264. );
  265. } else {
  266. $json_api->error("Include 'controller' and 'method' vars in your request.");
  267. }
  268. }
  269. protected function get_object_posts($object, $id_var, $slug_var) {
  270. global $json_api;
  271. $object_id = "{$type}_id";
  272. $object_slug = "{$type}_slug";
  273. extract($json_api->query->get(array('id', 'slug', $object_id, $object_slug)));
  274. if ($id || $$object_id) {
  275. if (!$id) {
  276. $id = $$object_id;
  277. }
  278. $posts = $json_api->introspector->get_posts(array(
  279. $id_var => $id
  280. ));
  281. } else if ($slug || $$object_slug) {
  282. if (!$slug) {
  283. $slug = $$object_slug;
  284. }
  285. $posts = $json_api->introspector->get_posts(array(
  286. $slug_var => $slug
  287. ));
  288. } else {
  289. $json_api->error("No $type specified. Include 'id' or 'slug' var in your request.");
  290. }
  291. return $posts;
  292. }
  293. protected function posts_result($posts) {
  294. global $wp_query;
  295. return array(
  296. 'count' => count($posts),
  297. 'count_total' => (int) $wp_query->found_posts,
  298. 'pages' => $wp_query->max_num_pages,
  299. 'posts' => $posts
  300. );
  301. }
  302. protected function posts_object_result($posts, $object) {
  303. global $wp_query;
  304. // Convert something like "JSON_API_Category" into "category"
  305. $object_key = strtolower(substr(get_class($object), 9));
  306. return array(
  307. 'count' => count($posts),
  308. 'pages' => (int) $wp_query->max_num_pages,
  309. $object_key => $object,
  310. 'posts' => $posts
  311. );
  312. }
  313. }
  314. ?>