PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/code/cake/app/webroot/research/wp-content/plugins/json-api/singletons/introspector.php

https://github.com/DigitalPaulScholtenProject/DPSP-Platform
PHP | 404 lines | 359 code | 38 blank | 7 comment | 59 complexity | 9ce0ab2da80989f027f72898cb9f51ed MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. class JSON_API_Introspector {
  3. public function get_article_categories($slug) {
  4. $the_category=get_term_by('slug',$slug,'article_category');
  5. $args = array(
  6. 'type' => 'page',
  7. 'child_of' => $the_category->term_id,
  8. 'parent' => '',
  9. 'orderby' => 'name',
  10. 'order' => 'ASC',
  11. 'hide_empty' => 0,
  12. 'hierarchical' => 1,
  13. 'exclude' => '',
  14. 'include' => '',
  15. 'number' => '',
  16. 'taxonomy' => 'article_category',
  17. 'pad_counts' => false );
  18. return get_categories($args);
  19. }
  20. public function get_article_category($slug) {
  21. $the_category=get_term_by('slug',$slug,'article_category');
  22. // $loop = new WP_Query( array( 'post_type' => 'article', 'cat' => $the_category->term_id ) );
  23. // $loop = new WP_Query( array( 'post_type' => 'article' ) );
  24. // return $get_terms('articles');
  25. $newargs = array(
  26. 'post_type' => 'article',
  27. 'tax_query' => array(
  28. array(
  29. 'taxonomy' => 'article_category',
  30. 'field' => 'slug',
  31. 'terms' => $the_category->slug
  32. )
  33. ),
  34. );
  35. $return=array('articles'=>query_posts( $newargs ),'category'=>$the_category);
  36. foreach($return['articles'] as $article){
  37. $article->author_name=$this->get_author_by_id($article->post_author);
  38. //var_dump(get_post_custom());
  39. $article->subtitle=get_post_meta($article->ID, '_anno_subtitle', true);
  40. $article->keywords=get_post_meta($article->ID, '_anno_keywords', true);
  41. $article->expected=get_post_meta($article->ID, '_anno_expected', true);
  42. $article->views=get_post_meta($article->ID, 'post_views_count', true);
  43. $article->tags=get_the_term_list($article->ID, 'article_tag', '', ';', '');
  44. if(empty($article->views)){
  45. $article->views=0;
  46. }
  47. $article->downloads=get_post_meta($article->ID, 'post_downloads_count', true);
  48. if(empty($article->downloads)){
  49. $article->downloads=0;
  50. }
  51. //print_r($article);
  52. }
  53. //print_r($return);
  54. return $return;
  55. // return $loop;
  56. }
  57. public function get_posts($query = false, $wp_posts = false) {
  58. global $post, $wp_query;
  59. $this->set_posts_query($query);
  60. $output = array();
  61. while (have_posts()) {
  62. the_post();
  63. if ($wp_posts) {
  64. $new_post = $post;
  65. } else {
  66. $new_post = new JSON_API_Post($post);
  67. }
  68. $output[] = $new_post;
  69. }
  70. return $output;
  71. }
  72. public function get_date_archive_permalinks() {
  73. $archives = wp_get_archives('echo=0');
  74. preg_match_all("/href='([^']+)'/", $archives, $matches);
  75. return $matches[1];
  76. }
  77. public function get_date_archive_tree($permalinks) {
  78. $tree = array();
  79. foreach ($permalinks as $url) {
  80. if (preg_match('#(\d{4})/(\d{2})#', $url, $date)) {
  81. $year = $date[1];
  82. $month = $date[2];
  83. } else if (preg_match('/(\d{4})(\d{2})/', $url, $date)) {
  84. $year = $date[1];
  85. $month = $date[2];
  86. } else {
  87. continue;
  88. }
  89. $count = $this->get_date_archive_count($year, $month);
  90. if (empty($tree[$year])) {
  91. $tree[$year] = array(
  92. $month => $count
  93. );
  94. } else {
  95. $tree[$year][$month] = $count;
  96. }
  97. }
  98. return $tree;
  99. }
  100. public function get_date_archive_count($year, $month) {
  101. if (!isset($this->month_archives)) {
  102. global $wpdb;
  103. $post_counts = $wpdb->get_results("
  104. SELECT DATE_FORMAT(post_date, '%Y%m') AS month,
  105. COUNT(ID) AS post_count
  106. FROM $wpdb->posts
  107. WHERE post_status = 'publish'
  108. AND post_type = 'post'
  109. GROUP BY month
  110. ");
  111. $this->month_archives = array();
  112. foreach ($post_counts as $post_count) {
  113. $this->month_archives[$post_count->month] = $post_count->post_count;
  114. }
  115. }
  116. return $this->month_archives["$year$month"];
  117. }
  118. public function get_categories($args = null) {
  119. $wp_categories = get_categories($args);
  120. $categories = array();
  121. foreach ($wp_categories as $wp_category) {
  122. if ($wp_category->term_id == 1 && $wp_category->slug == 'uncategorized') {
  123. continue;
  124. }
  125. $categories[] = $this->get_category_object($wp_category);
  126. }
  127. return $categories;
  128. }
  129. public function get_current_post() {
  130. global $json_api;
  131. extract($json_api->query->get(array('id', 'slug', 'post_id', 'post_slug')));
  132. if ($id || $post_id) {
  133. if (!$id) {
  134. $id = $post_id;
  135. }
  136. $posts = $this->get_posts(array(
  137. 'p' => $id
  138. ), true);
  139. } else if ($slug || $post_slug) {
  140. if (!$slug) {
  141. $slug = $post_slug;
  142. }
  143. $posts = $this->get_posts(array(
  144. 'name' => $slug
  145. ), true);
  146. } else {
  147. $json_api->error("Include 'id' or 'slug' var in your request.");
  148. }
  149. if (!empty($posts)) {
  150. return $posts[0];
  151. } else {
  152. return null;
  153. }
  154. }
  155. public function get_current_category() {
  156. global $json_api;
  157. extract($json_api->query->get(array('id', 'slug', 'category_id', 'category_slug')));
  158. if ($id || $category_id) {
  159. if (!$id) {
  160. $id = $category_id;
  161. }
  162. return $this->get_category_by_id($id);
  163. } else if ($slug || $category_slug) {
  164. if (!$slug) {
  165. $slug = $category_slug;
  166. }
  167. return $this->get_category_by_slug($slug);
  168. } else {
  169. $json_api->error("Include 'id' or 'slug' var in your request.");
  170. }
  171. return null;
  172. }
  173. public function get_category_by_id($category_id) {
  174. $wp_category = get_term_by('id', $category_id, 'category');
  175. return $this->get_category_object($wp_category);
  176. }
  177. public function get_category_by_slug($category_slug) {
  178. $wp_category = get_term_by('slug', $category_slug, 'category');
  179. return $this->get_category_object($wp_category);
  180. }
  181. public function get_tags() {
  182. $wp_tags = get_tags();
  183. return array_map(array(&$this, 'get_tag_object'), $wp_tags);
  184. }
  185. public function get_current_tag() {
  186. global $json_api;
  187. extract($json_api->query->get(array('id', 'slug', 'tag_id', 'tag_slug')));
  188. if ($id || $tag_id) {
  189. if (!$id) {
  190. $id = $tag_id;
  191. }
  192. return $this->get_tag_by_id($id);
  193. } else if ($slug || $tag_slug) {
  194. if (!$slug) {
  195. $slug = $tag_slug;
  196. }
  197. return $this->get_tag_by_slug($slug);
  198. } else {
  199. $json_api->error("Include 'id' or 'slug' var in your request.");
  200. }
  201. return null;
  202. }
  203. public function get_tag_by_id($tag_id) {
  204. $wp_tag = get_term_by('id', $tag_id, 'post_tag');
  205. return $this->get_tag_object($wp_tag);
  206. }
  207. public function get_tag_by_slug($tag_slug) {
  208. $wp_tag = get_term_by('slug', $tag_slug, 'post_tag');
  209. return $this->get_tag_object($wp_tag);
  210. }
  211. public function get_authors() {
  212. global $wpdb;
  213. $author_ids = $wpdb->get_col("
  214. SELECT u.ID, m.meta_value AS last_name
  215. FROM $wpdb->users AS u,
  216. $wpdb->usermeta AS m
  217. WHERE m.user_id = u.ID
  218. AND m.meta_key = 'last_name'
  219. ORDER BY last_name
  220. ");
  221. $all_authors = array_map(array(&$this, 'get_author_by_id'), $author_ids);
  222. $active_authors = array_filter($all_authors, array(&$this, 'is_active_author'));
  223. return $active_authors;
  224. }
  225. public function get_current_author() {
  226. global $json_api;
  227. extract($json_api->query->get(array('id', 'slug', 'author_id', 'author_slug')));
  228. if ($id || $author_id) {
  229. if (!$id) {
  230. $id = $author_id;
  231. }
  232. return $this->get_author_by_id($id);
  233. } else if ($slug || $author_slug) {
  234. if (!$slug) {
  235. $slug = $author_slug;
  236. }
  237. return $this->get_author_by_login($slug);
  238. } else {
  239. $json_api->error("Include 'id' or 'slug' var in your request.");
  240. }
  241. return null;
  242. }
  243. public function get_author_by_id($id) {
  244. $id = get_the_author_meta('ID', $id);
  245. if (!$id) {
  246. return null;
  247. }
  248. return new JSON_API_Author($id);
  249. }
  250. public function get_author_by_login($login) {
  251. global $wpdb;
  252. $id = $wpdb->get_var($wpdb->prepare("
  253. SELECT ID
  254. FROM $wpdb->users
  255. WHERE user_nicename = %s
  256. ", $login));
  257. return $this->get_author_by_id($id);
  258. }
  259. public function get_comments($post_id) {
  260. global $wpdb;
  261. $wp_comments = $wpdb->get_results($wpdb->prepare("
  262. SELECT *
  263. FROM $wpdb->comments
  264. WHERE comment_post_ID = %d
  265. AND comment_approved = 1
  266. AND comment_type = ''
  267. ORDER BY comment_date
  268. ", $post_id));
  269. $comments = array();
  270. foreach ($wp_comments as $wp_comment) {
  271. $comments[] = new JSON_API_Comment($wp_comment);
  272. }
  273. return $comments;
  274. }
  275. public function get_attachments($post_id) {
  276. $wp_attachments = get_children(array(
  277. 'post_type' => 'attachment',
  278. 'post_parent' => $post_id,
  279. 'orderby' => 'menu_order',
  280. 'order' => 'ASC',
  281. 'suppress_filters' => false
  282. ));
  283. $attachments = array();
  284. if (!empty($wp_attachments)) {
  285. foreach ($wp_attachments as $wp_attachment) {
  286. $attachments[] = new JSON_API_Attachment($wp_attachment);
  287. }
  288. }
  289. return $attachments;
  290. }
  291. public function get_attachment($attachment_id) {
  292. global $wpdb;
  293. $wp_attachment = $wpdb->get_row(
  294. $wpdb->prepare("
  295. SELECT *
  296. FROM $wpdb->posts
  297. WHERE ID = %d
  298. ", $attachment_id)
  299. );
  300. return new JSON_API_Attachment($wp_attachment);
  301. }
  302. public function attach_child_posts(&$post) {
  303. $post->children = array();
  304. $wp_children = get_posts(array(
  305. 'post_type' => $post->type,
  306. 'post_parent' => $post->id,
  307. 'order' => 'ASC',
  308. 'orderby' => 'menu_order',
  309. 'numberposts' => -1,
  310. 'suppress_filters' => false
  311. ));
  312. foreach ($wp_children as $wp_post) {
  313. $new_post = new JSON_API_Post($wp_post);
  314. $new_post->parent = $post->id;
  315. $post->children[] = $new_post;
  316. }
  317. foreach ($post->children as $child) {
  318. $this->attach_child_posts($child);
  319. }
  320. }
  321. protected function get_category_object($wp_category) {
  322. if (!$wp_category) {
  323. return null;
  324. }
  325. return new JSON_API_Category($wp_category);
  326. }
  327. protected function get_tag_object($wp_tag) {
  328. if (!$wp_tag) {
  329. return null;
  330. }
  331. return new JSON_API_Tag($wp_tag);
  332. }
  333. protected function is_active_author($author) {
  334. if (!isset($this->active_authors)) {
  335. $this->active_authors = explode(',', wp_list_authors(array(
  336. 'html' => false,
  337. 'echo' => false,
  338. 'exclude_admin' => false
  339. )));
  340. $this->active_authors = array_map('trim', $this->active_authors);
  341. }
  342. return in_array($author->name, $this->active_authors);
  343. }
  344. protected function set_posts_query($query = false) {
  345. global $json_api, $wp_query;
  346. if (!$query) {
  347. $query = array();
  348. }
  349. $query = array_merge($query, $wp_query->query);
  350. if ($json_api->query->page) {
  351. $query['paged'] = $json_api->query->page;
  352. }
  353. if ($json_api->query->count) {
  354. $query['posts_per_page'] = $json_api->query->count;
  355. }
  356. if ($json_api->query->post_type) {
  357. $query['post_type'] = $json_api->query->post_type;
  358. }
  359. if (!empty($query)) {
  360. query_posts($query);
  361. do_action('json_api_query', $wp_query);
  362. }
  363. }
  364. }
  365. ?>