<?php
/**
 * @package AMP - Accelerated Mobile Pages
 * @since 0.1
 */
if( !class_exists( 'etruel_amp' ) ) {
	class etruel_amp {

		public static $styles_scripts_added = false;
		public static $ignore_enqueue = false;
		/**
		* Static function hooks
		* @access public
		* @return void
		* @since 0.1.1
		*/
		public static function hooks() {
			
			add_filter( 'the_content', array(__CLASS__, 'get_post_scripts_styles' ), 9999, 1);
			add_action( 'wp_head', array(__CLASS__, 'head') );
			add_action( 'wp_footer', array(__CLASS__, 'footer') );
		}
		/**
		* Static function head
		* @access public
		* @return void
		* @since 0.1
		*/
		public static function head() {
			etruel_amp_compatibilities::get_style_theme();
		}
		/**
		* Static function exist_logo
		* @access public
		* @return void
		* @since version
		*/
		public static function exist_logo() {
			return etruel_amp_compatibilities::$exist_logo;
		}
		/**
		* Static function footer
		* @access public
		* @return void
		* @since 0.2
		*/
		public static function footer() {
			
		}

		/**
		* Static function comment_form
		* @access public
		* @return void
		* @since 0.1
		*/
		public static function comment_form() {
			etruel_amp_compatibilities::comment_form();
		}
		/**
		* Static function amp_content
		* @access public
		* @return $html_content String with html of post content.
		* @since 0.2.0
		*/
		public static function amp_content($html_content) {
			global $content_width;
			$html_content = self::get_amp_content($html_content);
			return $html_content;
		}
		/**
		* Static function get_amp_content
		* @access public
		* @return $html_content String with html of post content.
		* @since 0.2.0
		*/
		public static function get_amp_content($html_content) {
			global $content_width;
			/** 
			* @deprecated 
			if (!self::$styles_scripts_added && !self::$ignore_enqueue) { 
				return $html_content;
			}
			*/

			$amp_content = new etruel_amp_content($html_content,
				apply_filters( 'etruel_amp_content_embed_handlers', array(
					'etruel_amp_twitter_embed_handler' => array(),
					'etruel_amp_youTube_embed_handler' => array(),
					'etruel_amp_instagram_embed_Handler' => array(),
					'etruel_amp_vine_embed_handler' => array(),
					'etruel_amp_facebook_embed_handler' => array(),
				), null ),
				apply_filters( 'etruel_amp_content_sanitizers', array(
					 'etruel_amp_style_sanitizer' => array(),
					 'etruel_amp_blacklist_sanitizer' => array(),
					 'etruel_amp_img_sanitizer' => array(),
					 'etruel_amp_video_sanitizer' => array(),
					 'etruel_amp_audio_sanitizer' => array(),
					 'etruel_amp_iframe_sanitizer' => array(
						 'add_placeholder' => true,
					 ),
				), null ),
				array(
					'content_max_width' => $content_width,
				)
			);
			$content_amp_string = $amp_content->get_amp_content();
			if (!empty($content_amp_string)) {
				$html_content = $content_amp_string;
			}
			return $html_content;
		}
		/**
		* Static function get_post_scripts_styles
		* @access public
		* @return $html_content String with html of post content.
		* @since 0.2.0
		*/
		public static function get_post_scripts_styles($html_content) {
			global $content_width, $etruel_amp_theme_extra_style_before, $etruel_amp_defaults_scripts;
			/** 
			* @deprecated 
			if (!self::$styles_scripts_added) {
				self::$styles_scripts_added = true;
			} 
			*/
			$amp_content = new etruel_amp_content($html_content,
				apply_filters( 'etruel_amp_content_embed_handlers', array(
					'etruel_amp_twitter_embed_handler' => array(),
					'etruel_amp_youTube_embed_handler' => array(),
					'etruel_amp_instagram_embed_handler' => array(),
					'etruel_amp_vine_embed_handler' => array(),
					'etruel_amp_facebook_embed_handler' => array(),
				), null ),
				apply_filters( 'etruel_amp_content_sanitizers', array(
					 'etruel_amp_style_sanitizer' => array(),
					 'etruel_amp_blacklist_sanitizer' => array(),
					 'etruel_amp_img_sanitizer' => array(),
					 'etruel_amp_video_sanitizer' => array(),
					 'etruel_amp_audio_sanitizer' => array(),
					 'etruel_amp_iframe_sanitizer' => array(
						 'add_placeholder' => true,
					 ),
				), null ),
				array(
					'content_max_width' => $content_width,
				)
			);
			if (!etruel_amp_compatibilities::$get_style) {
				foreach ($amp_content->get_amp_styles() as $selector => $atributes) {
					$etruel_amp_theme_extra_style_before .= $selector.'{';
					foreach ($atributes as $key => $value) {
						$etruel_amp_theme_extra_style_before .= $value.';';
					}
					$etruel_amp_theme_extra_style_before .= '}';
				}

			}
			
			foreach ($amp_content->get_amp_scripts() as $name => $src) {
				if (empty($etruel_amp_defaults_scripts[$name])) {
					$etruel_amp_defaults_scripts[$name] = $src;
				}
			}
			$content_amp_string = $amp_content->get_amp_content();
			if (!empty($content_amp_string)) {
				$html_content = $content_amp_string;
			}
			
			return $html_content;
		}
		/**
		* Static function post_scripts_styles
		* @access public
		* @since 0.2.0
		*/
		public static function post_scripts_styles($more_link_text = null, $strip_teaser = false) {
	        $content = get_the_content( $more_link_text, $strip_teaser );
	        $content = apply_filters( 'the_content', $content );
	        return true;
		}

		/**
		* Static function get_script_styles
		* @access public
		* @return void
		* @since version
		*/
		public static function get_script_styles() {
			while (have_posts()) : the_post();
			  	self::post_scripts_styles();        
			endwhile;
		}
	}
}
etruel_amp::hooks();
?>