<?php
/**
 * class: PageLayoutSettings
 * Description: enregistre les metas et le script pour sélectionner les modèles dans les listes du plugin
 *
 * @since 2.1.0
 */

namespace EACCustomWidgets\Includes\TemplatesLib\Blocks\PostMeta;

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

use EACCustomWidgets\Includes\TemplatesLib\Documents\Site_Header;
use EACCustomWidgets\Includes\TemplatesLib\Documents\Site_Footer;

use Elementor\TemplateLibrary\Source_Local;
use Elementor\Core\Base\Document;
use Exception;

/**
 * PageLayoutSettings
 */
final class Page_Layout_Settings {

	/**
	 * Constructor
	 */
	public function __construct() {
		add_action( 'init', array( $this, 'register' ), 10, 0 );
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts' ), 10, 0 );
	}

	/**
	 * Enregistrement de deux post meta
	 *
	 * @internal Used as a callback.
	 */
	public function register() {
		register_post_meta(
			'',
			'eac_theme_builder_template_siteheader',
			array(
				'type'              => 'string',
				'single'            => true,
				'description'       => esc_html__( 'Select a header template to display on the frontend', 'eac-components' ),
				'show_in_rest'      => 1,
				'sanitize_callback' => 'sanitize_text_field',
			)
		);

		register_post_meta(
			'',
			'eac_theme_builder_template_sitefooter',
			array(
				'type'              => 'string',
				'single'            => true,
				'description'       => esc_html__( 'Select a footer template to display on the frontend', 'eac-components' ),
				'show_in_rest'      => 1,
				'sanitize_callback' => 'sanitize_text_field',
			)
		);
	}

	/**
	 * Enqueue scripts
	 *
	 * @since 2.1.0 Ajout du script hormis pour les screen widgets, FSE, customizer et profile
	 */
	public function enqueue_scripts() {
		if ( ! function_exists( 'get_current_screen' ) ) {
			require_once ABSPATH . 'wp-admin/includes/screen.php';
		}

		$screen = get_current_screen();

		if ( is_object( $screen ) && ! in_array( $screen->id, array( 'widgets', 'site-editor', 'customize', 'profile' ), true ) ) {
			wp_register_script( 'eac-meta-sidebar', EAC_PLUGIN_URL . 'includes/templates-lib/assets/js/meta-sidebar.min.js', array( 'wp-blocks', 'wp-element', 'wp-components' ), '2.1.0', true );
			wp_enqueue_script( 'eac-meta-sidebar' );

			wp_add_inline_script(
				'eac-meta-sidebar',
				'var elementorSiteBuilderData = ' . wp_json_encode(
					array(
						'headerTemplates' => $this->list_header_templates(),
						'footerTemplates' => $this->list_footer_templates(),
					)
				),
				'before'
			);
		}
	}

	/**
	 * Liste de tous les modèles d'entête
	 *
	 * @return array
	 */
	private function list_header_templates() {
		$options = array(
			array(
				'label' => esc_html__( 'Inherit', 'eac-components' ),
				'value' => 'inherit',
			),
			array(
				'label' => esc_html__( 'Default theme', 'eac-components' ),
				'value' => 'default',
			),
		);

		$headers = get_posts(
			array(
				'post_type'              => Source_Local::CPT,
				'post_status'            => 'publish',
				'meta_key'               => Document::TYPE_META_KEY,
				'meta_value'             => Site_Header::TYPE,
				'ignore_sticky_posts'    => true,
				'nopaging'               => true,
				'no_found_rows'          => true,
				'posts_per_page'         => -1,
				'update_post_meta_cache' => false,
				'update_post_term_cache' => false,
			)
		);

		if ( ! is_wp_error( $headers ) && ! empty( $headers ) ) {
			foreach ( $headers as $header ) {
				$options[] = array(
					'label' => esc_html( $header->post_title ),
					'value' => esc_html( $header->post_name ),
				);
			}
		}

		return $options;
	}

	/**
	 * Liste de tous les modèles de pied de page
	 *
	 * @return array
	 */
	private function list_footer_templates() {
		$options = array(
			array(
				'label' => esc_html__( 'Inherit', 'eac-components' ),
				'value' => 'inherit',
			),
			array(
				'label' => esc_html__( 'Default theme', 'eac-components' ),
				'value' => 'default',
			),
		);

		$footers = get_posts(
			array(
				'post_type'              => Source_Local::CPT,
				'post_status'            => 'publish',
				'meta_key'               => Document::TYPE_META_KEY,
				'meta_value'             => Site_Footer::TYPE,
				'ignore_sticky_posts'    => true,
				'nopaging'               => true,
				'no_found_rows'          => true,
				'posts_per_page'         => -1,
				'update_post_meta_cache' => false,
				'update_post_term_cache' => false,
			)
		);

		if ( ! is_wp_error( $footers ) && ! empty( $footers ) ) {
			foreach ( $footers as $footer ) {
				$options[] = array(
					'label' => esc_html( $footer->post_title ),
					'value' => esc_html( $footer->post_name ),
				);
			}
		}

		return $options;
	}
}
new Page_Layout_Settings();
