<?php
/*
* Define class pspRichSnippets
* Make sure you skip down to the end of this file, as there are a few
* lines of code that are very important.
*/
!defined('ABSPATH') and exit;
if (class_exists('pspSnippet_article') != true) {
    class pspSnippet_article extends pspRichSnippets
    {
        /*
        * Some required plugin information
        */
        const VERSION = '1.0';

        /*
        * Store some helpers config
        */
		public $the_plugin = null;

		protected $module_folder = '';
		protected $module_folder_path = '';
		
		static protected $_instance;


        /*
        * Required __construct() function that initalizes the AA-Team Framework
        */
        public function __construct( $shortcode=null )
        {
        	global $psp;

        	// access parent class!
        	$this->shortcode_cfg( $shortcode, array(
        		'type'			=> $shortcode,
        		'execute'		=> true
        	) );
        	
        	$this->the_plugin = $psp;
			$this->module_folder = $this->the_plugin->cfg['paths']['plugin_dir_url'] . 'modules/rich_snippets/shortcodes/'.$this->shortcode.'/';
			$this->module_folder_path = $this->the_plugin->cfg['paths']['plugin_dir_path'] . 'modules/rich_snippets/shortcodes/'.$this->shortcode.'/';
			
			$this->init();
        }
        
        
        public function init() {
        	
        	$shortcode = $this->the_plugin->alias . '_rs_' . $this->shortcode;
        	add_shortcode( $shortcode, array( $this, 'gethtml') );
        }
        
        public function gethtml( $atts = array(), $content = null ) {
        	$ret = array();

        	// the attributes
        	extract( $this->shortcode_atts($atts, $content) );

        	// html MOVIE
        	$type   = ( !empty($eventtype) ? $eventtype : ucfirst( $this->shortcodeCfg['type'] ) );
        	//$ret[]	= '<div itemscope itemtype="http://schema.org/' . $type .'">';
        	$ret[]	= '<div itemscope itemtype="http://schema.org/Article">';


        	if ( !empty($image) ) {

        		$imgalt = isset($name) ? $name : __($type.' Image', 'psp');
        		//$ret[]	= '<img class="schema_image" itemprop="image" src="' . esc_url($image) . '" alt="' . $imgalt . '" />';
				$ret[] = 	'<span itemscope itemtype="http://schema.org/ImageObject" itemprop="image"><meta itemprop="url" content="'.esc_url($image).'"><img src="'.esc_url($image).'" alt="' . $imgalt . '" itemprop="image" />'; // 2017-march added
				if ( !empty($image_width) ) {
					$ret[]		= '<meta itemprop="width" content="' .  $image_width . '">';
				}
				if ( !empty($image_height) ) {
					$ret[]		= '<meta itemprop="height" content="' .  $image_height . '">';
				}
				$ret[] = '</span>';
        	}

        	if ( !empty($name) && !empty($url) ) {

        		$ret[]	= '<a class="schema_url" target="_blank" itemprop="url" href="' . esc_url($url) . '">';
        		$ret[]	= 	'<div class="schema_name" itemprop="name">' . $name . '</div>';
        		$ret[]	= '</a>';
        	} else 	if ( !empty($name) && empty($url) ) {

        		$ret[]	= '<div class="schema_name" itemprop="name">' . $name . '</div>';
        	}
			
			if ( !empty($headline) ) {
        		$ret[]	= '<div class="schema_headline" itemprop="headline">' . esc_attr($headline) . '</div>';
        	}

        	if ( !empty($description) ) {
        		$ret[]	= '<div class="schema_description" itemprop="description">' . esc_attr($description) . '</div>';
        	}
        	
	       	if ( !empty($author) ) {
        		$ret[]	= '<div itemprop="author" itemscope itemtype="http://schema.org/Person">' . __('Written by:', 'psp') . ' <span itemprop="name">' . $author . '</span></div>';
        	}
        	
        	if ( !empty($publisher) ) {
        		$ret[]	= '<div itemprop="publisher" itemscope itemtype="http://schema.org/Organization">' . __('Published by:', 'psp') . ' <span itemprop="name">' . $publisher . '</span>';
				
				if ( isset($publisher_logo) && !empty($publisher_logo) ) {
					$ret[] = 	'<span itemscope itemtype="http://schema.org/ImageObject" itemprop="logo"><meta itemprop="url" content="'.esc_url($publisher_logo).'"><img src="'.esc_url($publisher_logo).'" itemprop="image" /></span>'; // 2017-march added
				}
				
				$ret[] = '</div>';
			}

        	if ( !empty($pubdate) ) {
        		$ret[]	= '<div class="bday"><meta itemprop="datePublished" content="' . $pubdate . '">' . __('Date Published:', 'psp') . ' ' . date('m/d/Y', strtotime($pubdate)) . '</div>';
        	}

        	if ( !empty($article_body) ) {
        		$ret[]	= '<div class="schema_description" itemprop="articleBody">' . esc_attr($article_body) . '</div>';
        	}
        	
        	if ( !empty($article_section) ) {
        		$ret[]	= '<div class="schema_description" itemprop="articleSection">' . esc_attr($article_section) . '</div>';
        	}

        	$ret[]	= '</div>';

			// build Full html!
        	return $this->shortcode_execute( $ret, $atts, $content );
        }
        
		
		/**
	    * Singleton pattern
	    *
	    * @return pspSnippet_event Singleton instance
	    */
	    static public function getInstance()
	    {
	        if (!self::$_instance) {
	            self::$_instance = new self;
	        }

	        return self::$_instance;
	    }
    }
}

// Initialize the pspSnippet_article class
$pspSnippet_article = new pspSnippet_article('article');