Advertisement
Guest User

Untitled

a guest
May 30th, 2014
1,322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.98 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Blog model for interfacing with a Wordpress backend
  4.  *
  5.  * @author Alex Crooks
  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 posts of any type
  20.     function getPosts($numOfPosts = MAX_ARTICLES, $offset = 0, $type = 'post', $dateFilter = FALSE, $orderBy = 'post_date', $order = 'DESC')
  21.     {        
  22.         $query = array(
  23.             'post_status'   => 'publish',
  24.             'post_type'     => $type,
  25.             'orderby'       => $orderBy,
  26.             'order'         => $order,
  27.             'offset'        => $offset,
  28.             'numberposts'   => $numOfPosts,
  29.         );
  30.  
  31.         if($dateFilter)
  32.         {
  33.             $query['monthnum'] = $dateFilter['month'];      
  34.             $query['year'] = $dateFilter['year'];          
  35.         }
  36.  
  37.         return get_posts($query);
  38.     }
  39.  
  40.     // Get post or page by slug ("post_name" in wp db)
  41.     function getPost($slug = FALSE, $type = 'post')
  42.     {
  43.         if(!$slug)
  44.             return FALSE;
  45.  
  46.         $getPost = get_page_by_path($slug, 'OBJECT', $type);
  47.  
  48.         if($getPost)
  49.         {
  50.             if($getPost->post_status == "publish")
  51.                 return $getPost;
  52.             else
  53.                 return false;
  54.         }
  55.         else
  56.         {
  57.             return false;
  58.         }
  59.     }
  60.  
  61.     function getAttachedImage($id, $type)
  62.     {
  63.         return wp_get_attachment_url(get_post_meta($id, $type, true));
  64.     }
  65.  
  66.     function getTeamMember($id)
  67.     {
  68.         if(!$id)
  69.             return FALSE;
  70.        
  71.         return get_post($id, 'OBJECT');
  72.     }
  73.  
  74.     // Get preview to a post
  75.     function getPreview($id = FALSE)
  76.     {
  77.         if(!$id)
  78.             return FALSE;
  79.  
  80.         return get_post($id, 'OBJECT');        
  81.     }
  82.  
  83.     // Add share click
  84.     function addSocialShare($postID, $type)
  85.     {
  86.         // Increment share count in db
  87.         $count = $this->getShares($postID, $type);
  88.         update_post_meta($postID, '_share_'.$type, ++$count);
  89.     }
  90.  
  91.     // Get count for this share
  92.     function getShares($postID, $type)
  93.     {
  94.         $count = get_post_meta($postID, '_share_'.$type, true);
  95.         if(empty($count) || $count < 0) $count = 0;
  96.  
  97.         return $count;
  98.     }
  99.  
  100.     // Get related posts based on tags used in original post
  101.     function getRelated($postID, $exclude = '', $numResults = 5)
  102.     {
  103.         $related = array();
  104.         $tags = array();
  105.         $getTags = wp_get_post_tags($postID);
  106.  
  107.         if($getTags && $postID)
  108.         {            
  109.             foreach($getTags as $tag)
  110.             {
  111.                 $tags[] = $tag->term_id;
  112.             }
  113.  
  114.             $related = get_posts(array(
  115.                 'numberposts'   => $numResults,
  116.                 'tag__in'       => $tags,
  117.                 'post__not_in'  => array($postID),
  118.                 'exclude'       => $exclude,
  119.             ));
  120.         }
  121.  
  122.         return $related;
  123.     }
  124.  
  125.     function getCategoryBySlug($slug)
  126.     {
  127.         return get_category_by_slug($slug);
  128.     }
  129.  
  130.     function getCategory($postID)
  131.     {
  132.         $category = get_the_category($postID);
  133.        
  134.         if($category)
  135.         {
  136.             $data = new stdClass();
  137.  
  138.             $data->ID = $category[0]->cat_ID;
  139.             $data->name = $category[0]->name;
  140.             $data->slug = $category[0]->slug;
  141.  
  142.             return $data;
  143.         }
  144.         else
  145.         {
  146.             return FALSE;
  147.         }
  148.     }
  149.  
  150.     function getPostsInCategory($cat, $exclude = '', $offset = 0, $maxPosts = MAX_ARTICLES, $type = 'post')
  151.     {
  152.         return get_posts(array(
  153.             'post_status'   => 'publish',
  154.             'post_type'     => $type,
  155.             'category_name' => $cat,
  156.             'exclude'       => $exclude,
  157.             'offset'        => $offset,
  158.             'numberposts'   => $maxPosts
  159.         ));
  160.     }
  161.  
  162.     function getPostsInCustomCategory($tax, $cat, $type = 'post')
  163.     {
  164.         return get_posts(array(
  165.             'post_status'   => 'publish',
  166.             'post_type'     => $type,
  167.             $tax            => $cat,
  168.             'numberposts'   => -1
  169.         ));
  170.     }
  171.  
  172.     // Get our latest blog posts
  173.     function getLatest($numOfPosts = MAX_ARTICLES, $offset = 0)
  174.     {
  175.         return wp_get_recent_posts(array(
  176.             'numberposts' => $numOfPosts,
  177.             'offset' => $offset,
  178.             'post_status' => 'publish'
  179.         ), 'OBJECT');
  180.     }
  181.  
  182.     function search($term, $offset = 0, $maxPosts = MAX_ARTICLES)
  183.     {
  184.         return get_posts(array(
  185.             's'             => $term,
  186.             'post_status'   => 'publish',
  187.             'offset'        => $offset,
  188.             'numberposts'   => $maxPosts
  189.         ));
  190.     }
  191.  
  192.     function clean($content)
  193.     {
  194.         return stripslashes(htmlspecialchars($content, ENT_QUOTES, 'UTF-8'));
  195.     }
  196. }
  197. $blog = new Blog();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement