<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

class Ecafe_Conditions_Data {

	protected $conditions = array();

	public function __construct() {
		$this->init();
	}

	public function ec_add_condition( $location, $post_id, $conditions = '' ) {
		if ( $location ) {
			if ( is_array( $this->conditions ) && ! isset( $this->conditions[ $location ] ) ) {
				$this->conditions[ $location ] = array();
			}
			$this->conditions[ $location ][ $post_id ] = $conditions;
		}

		return $this;
	}

	public function ec_get_location_data( $location ) {
		if ( isset( $this->conditions[ $location ] ) ) {
			return $this->conditions[ $location ];
		}

		return array();
	}

	public function init() {

		$query = new \WP_Query(
			array(
				'posts_per_page' => -1,
				'post_type'      => ECAFE_POST,
				'post_status'    => 'publish',
			)
		);
		if ( ! empty( $query ) ) {
			if ( $query->have_posts() ) {
				foreach ( $query->posts as $post_id ) {
					$template_type = get_post_meta( $post_id->ID, 'ecafe_build_template_type', true );
					$conditions    = '';
					if ( $template_type === 'header' || $template_type === 'footer' ) {
						$conditions = get_post_meta( $post_id->ID, 'ecafe_build_template_display', true );
					} elseif ( $template_type === 'singular' ) {
						$conditions = get_post_meta( $post_id->ID, 'ecafe_build_display_singular', true );
					} elseif ( $template_type === 'archives' ) {
						$conditions = get_post_meta( $post_id->ID, 'ecafe_build_display_archives', true );
					} elseif ( $template_type === 'page404' ) {
						$conditions = '404page';
					}

					$this->ec_add_condition( $template_type, $post_id->ID, $conditions );
				}
			}
		}
		return $this;
	}
}
