<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://www.triboomedia.it
 * @since      1.0.0
 *
 * @package    Tbm_Tagmailer
 * @subpackage Tbm_Tagmailer/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Tbm_Tagmailer
 * @subpackage Tbm_Tagmailer/public
 * @author     Dev Triboomedia <dev.publishing@triboo.it>
 */
class Tbm_Tagmailer_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $plugin_name The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $version The current version of this plugin.
	 */
	private $version;

	private $common;

	private $display;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @param string $plugin_name The name of the plugin.
	 * @param string $version The version of this plugin.
	 *
	 * @since    1.0.0
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name; // tbm-tagmailer
		$this->version     = $version;
		$this->common      = new Tbm_Tagmailer_Common();
		$this->display     = new Tbm_Tagmailer_Display();
	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function register_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Tbm_Tagmailer_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Tbm_Tagmailer_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/tbm-tagmailer-public.min.css', array(), $this->version, 'all' );

	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function register_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Tbm_Tagmailer_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Tbm_Tagmailer_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_register_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/tbm-tagmailer-public.min.js', array(), $this->version, array(
			'strategy'  => 'async',
			'in_footer' => true
		) );
		wp_localize_script( $this->plugin_name, 'tbm_tagmailer', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

	}

	public function output_newsletter() {
		$today = 0;
		$today = isset( $_GET['day'] ) && $_GET['day'] == 'yesterday' ? "-1" : $today;
		$today = isset( $_GET['day'] ) && $_GET['day'] == 'tomorrow' ? "+1" : $today;

		if ( isset( $_GET["newsletter"] ) ) {
			header( "Cache-Control: no-store, no-cache, must-revalidate, max-age=0" );
			header( 'cf-edge-cache: no-cache' );
		}

		if ( isset( $_GET["newsletter"] ) && $_GET["newsletter"] == "html" ) {
			$type = isset( $_GET["type"] ) && is_numeric( $_GET["type"] ) && (int) $_GET["type"] > 0 ? (int) $_GET["type"] - 1 : 0;
			$this->display->html_newsletter_html( $type, $today );
			exit;
		}

		if ( isset( $_GET["newsletter"] ) && $_GET["newsletter"] == "xml" ) {
			$type = isset( $_GET["type"] ) && is_numeric( $_GET["type"] ) && (int) $_GET["type"] > 0 ? (int) $_GET["type"] - 1 : 0;
			$this->display->html_newsletter_xml( $type, $today );
			exit;
		}

	}

	public function embed_form( $content ) {
		global $post;

		if ( is_admin() ) {
			return $content;
		}

		if ( ! is_object( $post ) ) {
			return $content;
		}

		if ( ! is_single() && ! is_page() ) {
			return $content;
		}

		// Check the shortcode
		if (
			has_shortcode( $content, TBM_TAGMAILER_SHORTCODE )
			|| str_contains( $content, TBM_TAGMAILER_SHORTCODE )
			|| str_contains( $content, 'tagmailerContainer' )
		) {
			return $content;
		}

		$tbl_nl_automatic_form  = get_field( "tbl_nl_automatic_form", "option" );
		$tbl_nl_post_types_form = get_field( "tbl_nl_post_types_form", "option" ) ?: array();
		$actual_post_type       = get_post_type( $post->ID );
		$check_post_type        = in_array( $actual_post_type, $tbl_nl_post_types_form );

		if ( $tbl_nl_automatic_form && $check_post_type ) {
			$content = $this->common->tagmailer_generate_final_content( $content );
			wp_enqueue_style( 'tbm-tagmailer' );
			wp_enqueue_script( 'tbm-tagmailer' );
			do_action( 'tbm_tagmailer_enqueue_style' );
		}

		return $content;
	}

}
