Advertisement
alchymyth

CatList.php modded

Jan 6th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.70 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The CatList object gets the info for the CatListDisplayer to show.
  4.  * Each time you use the shortcode, you get an instance of this class.
  5.  * @author fernando@picandocodigo.net
  6.  */
  7.  
  8. class CatList{
  9.     private $params = array();
  10.     private $lcp_categories_posts = array();
  11.     private $lcp_category_id = 0;
  12.  
  13.     /**
  14.      * Constructor gets the shortcode attributes as parameter
  15.      * @param array $atts
  16.      */
  17.     public function __construct($atts) {
  18.         $this->params = $atts;
  19.         //Get the category posts:
  20.          $this->lcp_set_categories();
  21.     }
  22.  
  23.     /**
  24.      * Get the categories & posts
  25.      */
  26.     private function lcp_set_categories(){
  27.         $args = array();
  28.         if ( isset($this->params['categorypage']) && $this->params['categorypage'] == 'yes' ){
  29.           global $post;
  30.           $categories = get_the_category($post->ID);
  31.           $this->lcp_category_id = $categories[0]->cat_ID;
  32.         } elseif ( isset($this->params['name']) && $this->params['name'] != '' ){
  33.           $this->lcp_category_id = $this->get_category_id_by_name($this->params['name']);
  34.         } elseif ( isset($this->params['id']) && $this->params['id'] != '0' ){
  35.           $this->lcp_category_id = $this->params['id'];
  36.         }
  37.        
  38.         if ($this->lcp_category_id != 0){
  39.           $args['category'] = $this->lcp_category_id;
  40.         }
  41.    
  42.         $args = array_merge($args, array(
  43.         'numberposts' => $this->params['numberposts'],
  44.         'orderby' => $this->params['orderby'],
  45.         'order' => $this->params['order'],
  46.         'exclude' => $this->params['excludeposts'],
  47.         'offset' => $this->params['offset']
  48.         ));
  49.  
  50.         // Post type and post parent:
  51.         if(isset($this->params['post_type'])): $args['post_type'] = $this->params['post_type']; endif;
  52.         if(isset($this->params['post_parent'])): $args['post_parent'] = $this->params['post_parent']; endif;
  53.  
  54.         // Custom fields 'customfield_name' & 'customfield_value' should both be defined
  55.         if(isset($this->params['customfield_name']) && $this->params['customfield_value'] != ''):
  56.           $args['meta_key'] = $this->params['customfield_name'];
  57.           $args['meta_value'] = $this->params['customfield_value'];
  58.         endif;
  59.  
  60.         //Get private posts
  61.         if(is_user_logged_in()){
  62.             $args['post_status'] = array('publish','private');
  63.         }
  64.  
  65.         // Added custom taxonomy support
  66.         if (isset($this->params['taxonomy']) && $this->params['taxonomy'] != '' && $this->params['tags'] != "") {
  67.           $args['tax_query'] = array(array(
  68.               'taxonomy' => $this->params['taxonomy'],
  69.               'field' => 'slug',
  70.               'terms' => explode(",",$this->params['tags'])
  71.           ));
  72.         } else if (isset($this->params['tags'])) {
  73.           $args['tag'] = $this->params['tags'];
  74.         }
  75.  
  76.         $this->lcp_categories_posts = get_posts($args);
  77.     }
  78.  
  79.     /**
  80.      * Get the category id from its name
  81.      * by Eric Celeste / http://eric.clst.org
  82.      */
  83.     private function get_category_id_by_name($cat_name){
  84.         #TODO: Support multiple names (this used to work, but not anymore)
  85.        //We check if the name gets the category id, if not, we check the slug.
  86.         $term = get_term_by('slug', $cat_name, 'category');
  87.         if (!$term):
  88.             $term = get_term_by('name', $cat_name, 'category');
  89.         endif;
  90.  
  91.         return ($term) ? $term->term_id : 0;
  92.     }
  93.  
  94.     public function get_category_id(){
  95.         return $this->lcp_category_id;
  96.     }
  97.  
  98.     public function get_categories_posts(){
  99.         return $this->lcp_categories_posts;
  100.     }
  101.  
  102.     /**
  103.      * Load category name and link to the category:
  104.      */
  105.     public function get_category_link(){
  106.         if($this->params['catlink'] == 'yes' && $this->lcp_category_id != 0){
  107.             $cat_link = get_category_link($this->lcp_category_id);
  108.             $cat_title = get_cat_name($this->lcp_category_id);
  109.             return '<a href="' . $cat_link . '" title="' . $cat_title . '">' . $cat_title . '</a>';
  110.         } else {
  111.             return null;
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Display custom fields.
  117.      * @see http://codex.wordpress.org/Function_Reference/get_post_custom
  118.      * @param string $custom_key
  119.      * @param int $post_id
  120.      */
  121.     public function get_custom_fields($custom_key, $post_id){
  122.         if($this->params['customfield_display'] != ''){
  123.             $lcp_customs = '';
  124.             //Doesn't work for many when having spaces:
  125.             $custom_key = trim($custom_key);
  126.             //Create array for many fields:
  127.             $custom_array = explode(",", $custom_key);
  128.             //Get post custom fields:
  129.             $custom_fields = get_post_custom($post_id);
  130.             //Loop on custom fields and if there's a value, add it:
  131.             foreach ($custom_array as $something){
  132.                 $my_custom_field = $custom_fields[$something];
  133.                 if (sizeof($my_custom_field) > 0 ):
  134.                     foreach ( $my_custom_field as $key => $value ){
  135.                         $lcp_customs .= "<div class=\"lcp-customfield\">" . $something. " : " . $value . "</div>";
  136.                     }
  137.                 endif;
  138.             }
  139.             return $lcp_customs;
  140.         } else {
  141.             return null;
  142.         }
  143.     }
  144.  
  145.     public function get_comments_count($single){
  146.         if (isset($this->params['comments']) && $this->params['comments'] == 'yes'){
  147.                 return ' (' . $single->comment_count . ')';
  148.         } else {
  149.             return null;
  150.         }
  151.     }
  152.  
  153.     public function get_author_to_show($single){
  154.         if ($this->params['author']=='yes'){
  155.             $lcp_userdata = get_userdata($single->post_author);
  156.             return $lcp_userdata->display_name;
  157.         } else {
  158.             return null;
  159.         }
  160.     }
  161.  
  162.  
  163.  
  164.     public function get_date_to_show($single){
  165.         if ($this->params['date']=='yes'){
  166.             //by Verex, great idea!
  167.             return  get_the_time($this->params['dateformat'], $single);
  168.         } else {
  169.             return null;
  170.         }
  171.     }
  172.  
  173.     public function get_content($single){
  174.         if (isset($this->params['content']) && $this->params['content'] =='yes' && $single->post_content){
  175.             $lcp_content = $single->post_content;
  176.             //Need to put some more thought on this!
  177.             //Added to stop a post with catlist to display an infinite loop of catlist shortcode parsing
  178.             /*if (preg_match("/\[catlist.*\]/", $lcp_content, $regmatch)){
  179.                     foreach ($regmatch as $match){
  180.                             $lcp_content = str_replace($match, '(...)',$lcp_content);
  181.                     }
  182.             }*/
  183.             $lcp_content = apply_filters('the_content', $lcp_content); // added to parse shortcodes
  184.             $lcp_content = str_replace(']]>', ']]&gt', $lcp_content); // added to parse shortcodes
  185.             return $lcp_content;
  186.        } else {
  187.             return null;
  188.         }
  189.     }
  190.  
  191.     public function get_excerpt($single){
  192.         if ($this->params['excerpt']=='yes' && !($this->params['content']=='yes' && $single->post_content) ){
  193.             if($single->post_excerpt){
  194.                     return $single->post_excerpt;
  195.             }
  196.             $lcp_excerpt = strip_tags($single->post_content);
  197.             if (strlen($lcp_excerpt) > 255) {
  198.                     $lcp_excerpt = substr($lcp_excerpt, 0, 252) . '...';
  199.             }
  200.             return $lcp_excerpt;
  201.         } else {
  202.             return null;
  203.         }
  204.     }
  205.  
  206.     /**
  207.      * Get the post Thumbnail
  208.      * @see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
  209.      * @param unknown_type $single
  210.      *
  211.      */
  212.     public function get_thumbnail($single){
  213.         if ($this->params['thumbnail']=='yes'){
  214.             $lcp_thumbnail = '';
  215.             if ( has_post_thumbnail($single->ID) ) {
  216.  
  217. //start of adaptation to show caption//
  218.   $thumb_id = get_post_thumbnail_id($single->ID);
  219.   $args = array( 'post_type' => 'attachment', 'post_status' => null,
  220.     'parent' => $single->ID, 'include'  => $thumb_id );
  221.    $thumbnail_image = get_posts($args);
  222.    if ($thumb_id && $thumbnail_image && isset($thumbnail_image[0])) {  
  223.     $caption = $thumbnail_image[0]->post_excerpt;
  224.     if($caption) $caption = '<span class="thumbnail-caption-text">' . $caption . '</span>';
  225.     }  
  226. //end of adaptation to show caption//
  227.  
  228.                  $lcp_thumbnail = '<a href="' . get_permalink($single->ID).'">' . get_the_post_thumbnail($single->ID, $this->params['thumbnail_size']) . $caption . '</a>'; //edited adaptation to show caption//
  229.             }
  230.             return $lcp_thumbnail;
  231.         } else {
  232.             return null;
  233.         }
  234.     }
  235.  
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement