/home/edulekha/www/wp-content/plugins/sneeit-framework/includes/articles/articles-query-item.php
<?php
/**
 * This class is for sneeit article query to display items
 */
class Sneeit_Articles_Query_Item {
	var $ID = 0;
	var $permalink = '';
	var $title = '';
	var $title_esc_attr = '';
	var $content = '';
	var $format = '';
	var $args = array();	
	var $index = 0;	
	
	/**
	 * THIS FUNCTION RUN WHEN CREATE CLASS VARIABLE
	 * we will use this to get some basic thing that will be use in every methods
	 */
	function __construct($args = array(), $index = 0) {
		$this->ID = get_the_ID();
		$this->permalink = get_permalink();
		$this->title = wp_kses(get_the_title(), array());		
		$this->title_esc_attr = esc_attr($this->title);			
		$this->index = $index;
		
		$this->args = $args;
		
		// process data
		// if have prefix
		
		// thumbnail height
		if (!is_numeric($this->args['thumb_height'])) {
			$this->args['thumb_height'] = 150;
		}
		$this->args['thumb_height'] = (int) $this->args['thumb_height'];		
	}
	
	/**
	 * 
	 * 
	 */
	public function item_class($extra_class = '') {
		if (!empty($this->args['item_extra_class'])) {
			$this->args['item_extra_class'] = esc_attr(' '.trim($this->args['item_extra_class']));
		} else {
			$this->args['item_extra_class'] = '';
		}
		if (!empty($extra_class)) {
			$extra_class = esc_attr(' '.trim($extra_class));
		}
		return ' class="item item-'.$this->index.$extra_class.$this->args['item_extra_class'].'"';		
	}
	
	/**
	 * return full content of post
	 * call this when you need only to save performances
	 * this will return raw content, without video embedded or shortcode
	 */
	public function content() {
		
		if ($this->content) {
			return $this->content;
		}
		/* we not use the_content
		 * because a post insert code to show recent posts
		 * which include itself, we will have a forver loop
		 * when the_content do shortcode directly
		 * 
		 * So we must have a way to remove all shortcodes
		 * because we don't want to show them to readers
		 */
		$this->content = get_the_content();
		
		/* Since 4.9
		 * 
		 * We don't use global $shortcode_tags; anymore
		 * when it takes too much steps to be done
		 * 
		 * This new method may be not stable, please 
		 * stress test it if you get any report
		 */
		/* remove scripts / styles */
		/* PENDING: still not found an efficient way to do */
		
		/* remove shortcodes */
		$start = strpos($this->content, '[');
		$end = strpos($this->content, ']', $start);
		if ($start !== false && $end !== false && $end > $start + 1) {
			$this->content = str_replace(array('['), '<', $this->content);
			$this->content = str_replace(array(']'), '>', $this->content);
			
			/* we remove all tags but keep images and iframes
			 * just incase some one need to scane images tags
			 * or youtube / vimeo or other social included
			 */
			$this->content = wp_kses($this->content, array(
				'img' => array(
					'src' => array(),
					'class' => array(),
				),
				'iframe' => array(
					'src' => array(),				
				),
			));
		}		
		
		return $this->content;
	}
	
	/**
	 * If you use SNEEIT THUMBNAIL HOOKS:
	 * Images not from library will have src
	 * Images from library will have no src, but data-s,
	 * and javascript will measure the thumbnail size
	 * and get the right image for src attribute
	 */
	private function get_post_image($size = 'post-thumbnail', $attr = array()) {		
		// DEFINE
		$image_html = '';
		$src = '';			

		// IF HAVE THUMBNAIL
		if (has_post_thumbnail( $this->ID ) ) {
			apply_filters( 'sneeit_articles_get_post_image_before', '');
			$image_html = get_the_post_thumbnail( $this->ID, $size, $attr );
			apply_filters( 'sneeit_articles_get_post_image_after', '');
			return $image_html;
		}
		
		// CHECK IF HAVE FEATURE MEDIA FIELD
		// scan in 		
		if (!empty($this->args['post_feature_media_meta_key'])) {
			$feature_media_meta_value = get_post_meta($this->ID, $this->args['post_feature_media_meta_key'], true);
			if ($feature_media_meta_value) {
				$src = sneeit_get_youtube_image($feature_media_meta_value);
				if (!$src) {
					$src = sneeit_get_vimeo_image($feature_media_meta_value);
				}		
			}
		}
		
		
		
		// NOT FOUND ANY IMAGE
		if (!$src) {
			
			// so, now, we must scan the first image			
			$src = sneeit_article_get_image_src($this->content());

			// found an attachment id
			if (is_numeric($src)) {				
				apply_filters( 'sneeit_articles_get_post_image_before', '');
				$image_html = wp_get_attachment_image($src, $size, false, $attr);
				apply_filters( 'sneeit_articles_get_post_image_after', '');
				return $image_html;
			}
		}
		

		// found image, just output the image link
		if ( $src ) {
			// maybe external image or not in library
			$image_html = '<img src="' . esc_url( $src ) . '"';
			foreach ( $attr as $key => $value ) {
				$image_html .= ' ' . $key . '="' . esc_attr( $value ) . '"';
			}
			$image_html .= '/>';			
		}

		return $image_html;
	}
	
	/**
	 * 
	 */
	public function thumbnail($size = 'post-thumbnail') {
		
		if ($this->args['thumb_height'] <= 0 && empty($this->args['auto_thumb'])) {
			return '';
		}
		
		// element attributes
		$image_attr = array(
			'alt' => $this->title_esc_attr
		);
		$image_attr = wp_parse_args($image_attr, $this->args['thumb_img_attr']);
				
		
		$link_attr = array(
			'title' => $this->title_esc_attr,
			'class' => 'sneeit-thumb' . ($this->args['auto_thumb'] ? ' sneeit-thumb-a' : ' sneeit-thumb-f'),
			'href' => $this->permalink,
		);
								
		// get image html
		$image = $this->get_post_image($size, $image_attr);
		
		
		
		
		// if not found, we need to use default thumbnail
		if (!$image && !empty($this->args['default_thumb'])) {
			$image = '<img src="'.esc_url($this->args['default_thumb']).'"';
			foreach ($image_attr as $attr_name => $attr_value) {
				$image .= ' '.$attr_name.'="'.esc_attr($attr_value).'"';
			}
			$image .= '/>';
		}
		if (!$image) {
			return '';
		}
		
		// add link
		$image_link = '<a';
		foreach ($link_attr as $attr_name => $attr_value) {
			$image_link .= ' '.$attr_name.'="'.esc_attr($attr_value).'"';
		}
		$image_link .= '>'.$image.'</a>';
		
		return $image_link;
	}
	
	/**
	 * You must input your format_icon_map
	 */
	public function format_icon() {
		if (empty($this->args['show_format_icon']) || 
			empty($this->args['format_icon_map'])) {
			return;
		}
		if (!$this->format) {
			$this->format = get_post_format();
		}
		if (empty($this->args['format_icon_map'][$this->format])) {
			return '';
		}
		
		return '<a class="item-format-icon" href="'.$this->permalink.'#post-review" title="'.$this->title_esc_attr.'">'.$this->args['format_icon_map'][$this->format].'</a>';
	}
	
	/**
	 * use this if you are using sneeit rating hook
	 * you must input post review average and post review type meta key
	 * or framework will use the default
	 */	
	public function review() {
		// check condition
		if (empty($this->args['show_review_score']) || 
			empty($this->args['post_review_average_meta_key'])) {
			return '';
		}
		
		
		// rating data
		$post_review_average = get_post_meta($this->ID, $this->args['post_review_average_meta_key'], true);				
		if (empty($post_review_average)) {
			return '';
		}
					
		$post_review_type = '';
		if (!empty($this->args['post_review_type_meta_key'])) {
			$post_review_type = get_post_meta($this->ID, $this->args['post_review_type_meta_key'], true);		
		}		
		
		return sneeit_review_percent_circle($post_review_average, $post_review_type, 'a', array(
			'class' => 'item-review',
			'title' => $this->title_esc_attr,
			'href' => $this->permalink
		));		
	}
	
	/** 
	 * use this if you are using sneeit views hook
	 * you must input view meta key
	 * or framework will use the default
	 */
	public function views() {
		if (empty($this->args['show_view_count']) ||
			empty($this->args['views_meta_key'])) {
			return '';
		}
		
		$views = get_post_meta($this->ID, $this->args['views_meta_key'], true);
		
		if (empty($views)) {
			return '';
		}
		
		return '<span class="item-views">'.$this->args['before_view_count'] . $views. $this->args['after_view_count'].'</span>';
	}
	
	/**
	 * 
	 */
	public function author() {
		if (empty($this->args['show_author'])) {
			return '';
		}
		$id = get_the_author_meta('ID');
		$name = get_the_author_meta( 'display_name' );
		switch ($this->args['show_author']) {
			case 'icon':
				$this->args['before_author_name'] .= '<i class="fa fa-user"></i> ';
				break;
			case 'avatar':
				$avatar = get_avatar($id, 16, '', esc_attr($name));
				if (!empty($avatar)) {
					$this->args['before_author_name'] .= $avatar . ' ';
				}				
				break;
			default:
				break;
		}

		return '<a href="'.esc_url(get_author_posts_url($id)).'" target="_blank" class="item-author">'.$this->args['before_author_name'].$name.$this->args['after_author_name'].'</a>';
	}
	public function comment_count() {
		if (empty($this->args['show_comment'])) {
			return '';
		}
		
		return '<a class="item-comment-count" href="'.esc_url(get_comments_link()).'">'.$this->args['before_comment_count'].get_comments_number(). $this->args['after_comment_count'].'</a>';
	}
	
	/**
	 * 
	 */
	public function date_time() {
		if (empty($this->args['show_date'])) {
			return '';
		}
		
		$ret = '<a class="item-date-time" href="'.esc_url($this->permalink).'">'.$this->args['before_date_time'];

		switch ($this->args['show_date']) {
			case 'pretty':
				$ret .= sprintf( $this->args['%s ago'], human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) );
				break;

			case 'short':
				if (get_option('date_format')) {
					$ret .= get_the_date(str_replace('F', 'M', get_option('date_format')));
				}
				break;
				
			case 'time':
				$ret .= get_the_time();
				break;

			case 'date':
				$ret .= get_the_date();
				break;
			
			default:
				$ret .= get_the_date().' '.get_the_time();
				break;
		}
		$ret .= $this->args['after_date_time'].'</a>';
		
		return $ret;
	}
	
	/**
	 * 
	 */
	public function categories() {
		if (empty($this->args['number_cates']) || 
			!is_numeric($this->args['number_cates'])) {
			return '';
		}
		$categories = get_the_category();
		if (empty($categories)) {
			return '';
		}
		
		$ret = '';
		$limit = (int) $this->args['number_cates'];
		$index = 0;
		foreach($categories as $category) :
			if ($ret) {
				$ret .= ', ';
			}
			
			$ret .= 
			'<a class="item-category" href="'.esc_url(get_category_link( $category->term_id )).'" title="' . esc_attr($category->cat_name) . '">' . esc_html( $category->cat_name ) . '</a>';				
			$index++;
			if ($index == $limit) {
				break;
			}
		endforeach;
		
		if ($ret) {
			$ret = '<span class="item-categories">' . $ret . '</span>';
		}
		
		return $ret;
	}
	
	/**
	 * 
	 */
	public function title() {
		return '<h3 class="item-title"><a href="'.$this->permalink.'" title="'.$this->title_esc_attr.'">'.$this->title.'</a></h3>';
	}
	
	/**
	 * 
	 */
	public function meta() {
		if (empty($this->args['meta_order'])) {
			return '';
		}
		$meta_order = explode(',', $this->args['meta_order']);
		$ret = '';
		foreach ($meta_order as $meta) {
			switch ($meta) {
				case 'cat':
					$ret .= $this->categories();
					break;
				
				case 'ico':
					$ret .= $this->format_icon();
					break;
				
				case 'review':
					$ret .= $this->review();
					break;
				
				case 'view':
					$ret .= $this->views();
					break;
				
				case 'author':
					$ret .= $this->author();
					break;
				
				case 'comment':
					$ret .= $this->comment_count();
					break;
				
				case 'date':
					$ret .= $this->date_time();
					break;
				
				case 'readmore':
					$ret .= $this->readmore_link();
					break;
				
				default:
					break;
			}
		}
		
		if ($ret) {
			$ret = '<span class="item-meta">'.$ret.'</span>';
		}
		
		return $ret;
	}
	
	/**
	 * 
	 */
	public function snippet() {
		if (empty($this->args['snippet_length'])) {
			return '';
		}		
		
		// check from excerpt first
		if (has_excerpt()) {
			$snippet = get_the_excerpt();
		} else {
			$snippet = wp_kses($this->content(), array());
		}
		
		// cut off snippet 
		$snippet = wp_trim_words( $snippet, (int) $this->args['snippet_length'], ' ...' );
		return '<p class="item-snippet"><span>'.$snippet . '</span> ' . $this->readmore_link() . '</p>';
	}
	
	/**
	 * 
	 */
	public function readmore_link() {
		if (empty($this->args['show_readmore'])) {
			return '';
		}
		return '<a class="item-readmore" title="'.$this->title_esc_attr.'" href="'.$this->permalink.'#more">'.$this->args['before_read_more'].$this->args['Read More'].$this->args['after_read_more'].'</a>';
	}	
}