<?php

class Tbm_Tagmailer_Shortcode {

	private $common;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		$this->common = new Tbm_Tagmailer_Common();
	}


	public function tagmailer_register_block() {
		if ( ! function_exists( 'register_block_type' ) ) {
			// Gutenberg is not active.
			return;
		}
		register_block_type( TBM_TAGMAILER_PLUGIN_DIR . 'blocks/tagmailer-form' );
	}

	public function register_shortcode() {
		add_shortcode( TBM_TAGMAILER_SHORTCODE, function ( $atts ) {

			$atts = shortcode_atts( array(
				'containerid' => '',
				'cta'         => '',
			), $atts, TBM_TAGMAILER_SHORTCODE );

			/**
			 * Enqueue styles and script
			 */
			wp_enqueue_style( 'tbm-tagmailer' );
			wp_enqueue_script( 'tbm-tagmailer' );
			do_action( 'tbm_tagmailer_enqueue_style' );

			/**
			 * Apply filters if needed
			 */
			if ( ! empty( $atts['cta'] ) ) {
				add_filter( 'newsletter_form_cta', function () use ( $atts ) {
					return $atts['cta'];
				}, 9 );
			}

			if ( ! empty( $atts['containerid'] ) ) {
				add_filter( 'newsletter_container_id', function () use ( $atts ) {
					return $atts['containerid'];
				},9 );
			}

			/**
			 * Returns the code
			 */
			return $this->common->tagmailer_generate_final_content();
		} );
	}

}
