Advertisement
miguelpeixe

Verite Timeline JSON controller for JSON API WordPress Plugi

May 1st, 2012
2,268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.75 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5.     JSON Controller for JSON API WordPress Plugin to return posts in Timeline format for Verite Timeline WordPress Plugin
  6.  
  7.     Usage:
  8.     http://example.com/api/timeline/category_posts/?category_id=123&post_type=timeline&amount=10&main_post_id=456
  9.  
  10.     Default values:
  11.     category_id = null
  12.     post_type = 'post'
  13.     amount = -1 (all posts)
  14.     main_post_id = first post from query, ordered by date in ascending order (the main post should be an introduction to the timeline being viewed)
  15.  
  16.     Content values can be changed to support custom post meta for enhanced usage.
  17.  
  18. */
  19.  
  20. class JSON_API_Timeline_Controller {
  21.  
  22.     public function category_posts() {
  23.         global $json_api;
  24.         $json = array();
  25.  
  26.         // get attributes
  27.         $category_id = $json_api->query->category_id;
  28.         $post_type = $json_api->query->post_type;
  29.         $amount = $json_api->query->amount;
  30.         $main_post_id = $json_api->query->main_post_id;
  31.  
  32.         if(!$post_type) $post_type = 'post';
  33.         if(!$amount) $amount = -1;
  34.  
  35.         $posts = get_posts(array('post_type' => $post_type, 'numberposts' => $amount, 'category' => $category_id, 'orderby' => 'post_date', 'order' => 'DESC'));
  36.  
  37.         if($main_post_id) $main_post = get_post($main_post_id);
  38.         else  {
  39.             $main_post = $posts[0];
  40.             unset($posts[0]);
  41.         }
  42.  
  43.         if($main_post) {
  44.  
  45.             // setting first (main) post
  46.  
  47.             $json['timeline'] = array();
  48.  
  49.             $json['timeline']['headline'] = $main_post->post_title;
  50.             $json['timeline']['type'] = 'default';
  51.             $json['timeline']['startDate'] = date('Y,m,d', strtotime($main_post->post_date));
  52.             $json['timeline']['text'] = $main_post->post_excerpt;
  53.  
  54.             // example of media asset using the post thumbnail
  55.             if(has_post_thumbnail($main_post->ID)) {
  56.  
  57.                 $thumbnail_id = get_post_thumbnail_id($main_post->ID);
  58.                 $thumbnail_src = wp_get_attachment_image_src($thumbnail_id, 'medium');
  59.                 $json['timeline']['asset']['media'] = $thumbnail_src[0];
  60.                
  61.             }
  62.  
  63.             if($posts) {
  64.                 $json['timeline']['date'] = array();
  65.                 $i = 0;
  66.                 foreach($posts as $post) {
  67.  
  68.                     $json['timeline']['date'][$i]['startDate'] = date('Y,m,d', strtotime($post->post_date));
  69.                     $json['timeline']['date'][$i]['endDate'] = date('Y,m,d', strtotime($post->post_date));
  70.                     $json['timeline']['date'][$i]['headline'] = $post->post_title;
  71.                     $json['timeline']['date'][$i]['text'] = $post->post_excerpt;
  72.  
  73.                     // example of media asset using the post thumbnail
  74.                     if(has_post_thumbnail($post->ID)) {
  75.  
  76.                         $thumbnail_id = get_post_thumbnail_id($post->ID);
  77.                         $thumbnail_src = wp_get_attachment_image_src($thumbnail_id, 'medium');
  78.  
  79.                         $json['timeline']['date'][$i]['asset']['media'] = $thumbnail_src[0];
  80.  
  81.                     }
  82.  
  83.                     $i++;
  84.  
  85.                 }
  86.  
  87.                 return $json;
  88.  
  89.             } else return 'Posts not found';
  90.  
  91.         } else return 'Main post not found';
  92.  
  93.     }
  94.  
  95. }
  96.  
  97. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement