<?php

class FGT_RENDER
{
	public function __construct()
	{
		$this->initial = NULL;
		add_action('wp', array($this, 'fgt_load_renderer'));
		add_action('wp_head', array($this, 'fgt_reset_renderer'), 4);
	}

	public function fgt_get_footer_content()
	{
		global $post;

		// if WMPL enabled, return the right footer for each language
		if ($post) {
			$postID = $post->ID;
			if (function_exists('icl_object_id')) {
				$postID = apply_filters('wpml_object_id', $post->ID, '_flothemes_header_metabox_field');
			}
			if ($postID === NULL) {
				$postID = $post->ID;
			}
		}

		if (is_singular()) {
			$value = get_post_meta($postID, '_flothemes_footer_metabox_field', TRUE);
			$useGlobal = get_post_meta($postID, '_flothemes_use_global_footer_metabox_field', TRUE);
		}

		if (!isset($useGlobal) || ($useGlobal === "true" || $useGlobal === "")) {
			$footers = get_posts(array(
		'numberposts' => -1,
		'post_type'   => 'fgt_footer',
		'fields'      => 'ids',
		'meta_query' => array(
		  array(
			'key' => '_flothemes_global_footer_metabox_field',
			'value' => 'on',
			'compare' => '=',
		  )
		)
	  ));

			if (isset($footers[0])) {
				$footer = get_post($footers[0]);

				if (function_exists('icl_object_id')) {
					$footerID = apply_filters('wpml_object_id', $footers[0], 'fgt_footer');
					if (!$footerID) {
						$footerID = $footers[0];
					}
					$footer = get_post($footerID);
				}
			}
		} else {
			switch ($value) {
		case 'DO_NOT_USE_FOOTER':
		  $footer = '';
		  break;
		default:
		  $footer = get_post($value);
		  break;
	  }
		}

		if (isset($footer->post_content)) {
			return $footer->post_content;
		} else {
			return '';
		}
	}

	public function fgt_get_header_content()
	{
		global $post, $isSticky, $isStickyClass;

		// if WMPL enabled, return the right header for each language
		// apply_filters( 'wpml_object_id', int $element_id, string $element_type, bool $return_original_if_missing, mixed $ulanguage_code )
		if ($post) {
			$postID = $post->ID;
			if (function_exists('icl_object_id')) {
				$postID = apply_filters('wpml_object_id', $post->ID, '_flothemes_header_metabox_field');
			}

			if ($postID === NULL) {
				$postID = $post->ID;
			}
		}

		if (is_singular()) {
			$value = get_post_meta($postID, '_flothemes_header_metabox_field', TRUE);
			$useGlobal = get_post_meta($postID, '_flothemes_use_global_header_metabox_field', TRUE);
		}

		if (!isset($useGlobal) || ($useGlobal === "true" || $useGlobal === "")) {
			$headers = get_posts(array(
		'numberposts' => -1,
		'post_type'   => 'fgt_header',
		'fields'      => 'ids',
		'meta_query' => array(
		  array(
			'key' => '_flothemes_global_header_metabox_field',
			'value' => 'on',
			'compare' => '=',
		  )
		)
	  ));

			if (isset($headers[0])) {
				$header = get_post($headers[0]);
				$isSticky = get_post_meta($headers[0], '_flothemes_sticky_header_metabox_field', TRUE);
				$isStickyClass = $isSticky ? 'fgt-sticky-header' : '';

				if (function_exists('icl_object_id')) {
					$headerID = apply_filters('wpml_object_id', $headers[0], 'fgt_header');
					// if no post ID returned, used original ID
					if (!$headerID) {
						$headerID = $headers[0];
					}
					$header = get_post($headerID);
					$isSticky = get_post_meta($headerID, '_flothemes_sticky_header_metabox_field', TRUE);
					$isStickyClass = $isSticky ? 'fgt-sticky-header' : '';
				}
			}
		} else {
			switch ($value) {
		case 'DO_NOT_USE_HEADER':
		  $header = '';
		  break;
		default:
		  $header = get_post($value);
		  $isSticky = get_post_meta($value, '_flothemes_sticky_header_metabox_field', TRUE);
		  $isStickyClass = $isSticky ? 'fgt-sticky-header' : '';
		  break;
	  }
		}

		if (isset($header->post_content)) {
			return $header->post_content;
		} else {
			return '';
		}
	}

	public function fgt_reset_renderer()
	{
		$GLOBALS['post'] = $this->initial;
	}

	public function fgt_load_renderer()
	{
		global $post;

		$original_global_post = $post;
		$currentId = isset($post->post_type) ? $post->post_type : '';

		if (is_search()) {
			$currentId = 'search';
		} elseif (is_archive() || ((is_home() && is_front_page())) || is_home()) {
			// for the Archive page - is_archive()
			// for the home page displaying the latest posts - is_home() && is_front_page()
			// for the blog page - is_home()
			$currentId = 'archive';
		} elseif (is_404()) {
			$currentId = 'page_404';
		}

		$condition = $currentId !== 'search' && $currentId !== 'archive' && $currentId !== 'page_404';
		$template_ID = $condition ? get_post_meta($post->ID, '_flothemes_template_metabox_field', TRUE) : '';

		// if ($template_ID === '') {
		$globalWrappers = json_decode(get_option('fgt_templates_array'));

		switch ($currentId) {
	  case 'post':
		$isGlobal = get_post_meta($original_global_post->ID, '_flothemes_use_global_p_metabox_field', TRUE);
		$value = get_post_meta($original_global_post->ID, '_flothemes_p_template_metabox_field', TRUE);

		if ($value === '' || $isGlobal === 'on') {
			$template_ID = $globalWrappers->global_post;
		} else {
			if ($value) {
				$template_ID = $value;
			}
		}
		break;
	  case 'gallery':
		$isGlobal = get_post_meta($original_global_post->ID, '_flothemes_use_global_g_metabox_field', TRUE);
		$value = get_post_meta($original_global_post->ID, '_flothemes_g_template_metabox_field', TRUE);

		if ($value === '' || $isGlobal === 'on') {
			$template_ID = $globalWrappers->global_gallery;
		} else {
			if ($value) {
				$template_ID = $value;
			}
		}
		break;
	  case 'search':
		$template_ID = $globalWrappers->search;
		break;
	  case 'archive':
		$template_ID = $globalWrappers->archive;
		break;
	  case 'page_404':
		$template_ID = $globalWrappers->page_404;
		break;
	  case 'product':
		if (defined('FGT_WOO_SUPPORT') && FGT_WOO_SUPPORT) {
			$isGlobal = get_post_meta($original_global_post->ID, '_flothemes_use_global_woo_metabox_field', TRUE);
			$value = get_post_meta($original_global_post->ID, '_flothemes_woo_template_metabox_field', TRUE);

			if ($value === '' || $isGlobal === 'on') {
				$template_ID = $globalWrappers->global_gallery;
			} else {
				if ($value) {
					$template_ID = $value;
				}
			}
		}
		break;
	  default:
		# code...
		break;
	}
		// }

		$this->initial = $GLOBALS['post'];

		global $fgt_header_render, $fgt_global_wrapper_render, $fgt_footer_render , $fgt_global_post;
		$fgt_header_render = $this->fgt_get_header_content();
		$fgt_footer_render = $this->fgt_get_footer_content();
		$fgt_global_post = $GLOBALS['post'];

		$content_post = $template_ID ? get_post($template_ID) : '';

		if (isset($content_post) && $content_post) {

	  //$GLOBALS['post'] = $content_post;
			$fgt_global_post = $content_post;
			$fgt_global_wrapper_render = get_the_content('', '', $template_ID);

			$fgt_content_post = isset($content_post->post_content) ? $content_post->post_content : '';
			$fgt_original_global_post = isset($original_global_post->post_content) ? $original_global_post->post_content : '';

			$content_post->post_content = $fgt_header_render . $fgt_content_post . $fgt_original_global_post . $fgt_footer_render;

			$fgt_global_post = $content_post;
		}
	}
}

new FGT_RENDER();
