Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 31st, 2013  |  syntax: PHP  |  size: 2.82 KB  |  views: 101  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * Blog model for interfacing with a Wordpress backend
  4.  *
  5.  * @author Alex Crooks http://alexcrooks.me
  6.  **/
  7.  
  8. // Load the wordpress bootstrap and it's helpers
  9. define('WP_USE_THEMES', false);
  10. require_once(APP_PATH.BLOG_FOLDER.'wp-blog-header.php');
  11.  
  12. class Blog
  13. {    
  14.     function __construct()
  15.     {
  16.         // STUB
  17.     }
  18.  
  19.     // Get our latest blog posts
  20.     function getLatest($numOfPosts = BLOG_NUM_POSTS, $offset = 0)
  21.     {
  22.         return wp_get_recent_posts(array(
  23.             'numberposts' => $numOfPosts,
  24.             'offset' => $offset,
  25.             'post_status' => 'publish'
  26.         ));
  27.     }
  28.  
  29.     // Get post by slug ("post_name" in wp db)
  30.     function getPost($slug = FALSE)
  31.     {
  32.         if(!$slug)
  33.             return FALSE;
  34.  
  35.         $getPost = get_page_by_path($slug, 'ARRAY_A', 'post');
  36.         if($getPost)
  37.         {
  38.             if($getPost['post_status'] == "publish")
  39.                 return $getPost;
  40.             else
  41.                 return false;
  42.         }
  43.         else
  44.         {
  45.             return false;
  46.         }
  47.     }
  48.  
  49.     // Get preview to a post
  50.     function getPreview($id = FALSE)
  51.     {
  52.         if(!$id)
  53.             return FALSE;
  54.  
  55.         return get_post($id, 'ARRAY_A');        
  56.     }
  57.  
  58.     // Get related posts based on tags used in original post
  59.     function getRelated($postID, $numResults = 3)
  60.     {
  61.         $related = array();
  62.         $tags = array();
  63.         $getTags = wp_get_post_tags($postID);
  64.  
  65.         if($getTags && $postID)
  66.         {            
  67.             foreach($getTags as $tag)
  68.             {
  69.                 $tags[] = $tag->name;
  70.             }
  71.  
  72.             $getRelated = get_posts(array(
  73.                 'numberposts'   => $numResults,
  74.                 'tag_slug__in'  => $tags,
  75.                 'post__not_in'  => array($postID)
  76.             ));
  77.  
  78.             foreach($getRelated as $relatedPost)
  79.             {                
  80.                 $related[] = array(
  81.                     'ID'            => $relatedPost->ID,
  82.                     'image'         => $this->getFeaturedImageUrl($relatedPost->ID),
  83.                     'post_title'    => $relatedPost->post_title,
  84.                     'post_name'     => $relatedPost->post_name
  85.                 );              
  86.             }
  87.         }
  88.  
  89.         return $related;
  90.     }
  91.  
  92.     function getFeaturedImageUrl($postID, $size = "large")
  93.     {
  94.         $image = DEFAULT_BLOG_IMAGE_URL;
  95.  
  96.         $getImage = wp_get_attachment_image_src(get_post_thumbnail_id($postID), $size);
  97.  
  98.         if($getImage)
  99.             $image = $getImage[0];
  100.  
  101.         return $image;
  102.     }
  103.  
  104.     function search($term)
  105.     {
  106.         return get_posts(array('s' => $term, 'numberposts' => -1));
  107.     }
  108.  
  109.     function cleanOut($content)
  110.     {
  111.         return tag_escape($content);
  112.     }
  113. }
  114. $blog = new Blog();
clone this paste RAW Paste Data