<?php
/**
 * Shortcode Wrapper element.
 *
 * @package BricksExtras
 */

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

/**
 * Shortcode Wrapper element class.
 */
class X_Shortcode_Wrapper extends \Bricks\Element {

	/**
	 * Element category.
	 *
	 * @var string
	 */
	public $category = 'extras';
	/**
	 * Element name.
	 *
	 * @var string
	 */
	public $name = 'xshortcodewrapper';
	/**
	 * Element icon.
	 *
	 * @var string
	 */
	public $icon = 'ti-shortcode';
	/**
	 * CSS selector.
	 *
	 * @var string
	 */
	public $css_selector = '';
	/**
	 * Element is nestable.
	 *
	 * @var bool
	 */
	public $nestable = true;


	/**
	 * Get element label.
	 */
	public function get_label() {
		return esc_html__( 'Shortcode Wrapper', 'bricksextras' );
	}

	/**
	 * Get element keywords.
	 */
	public function get_keywords() {
		return array( 'shortcode', 'wrapper' );
	}

	/**
	 * Set control groups.
	 */
	public function set_control_groups() {

		$this->control_groups['content'] = array(
			'title' => esc_html__( 'Content', 'bricks' ),
			'tab'   => 'content',
		);
	}

	/**
	 * Set element controls.
	 */
	public function set_controls() {

		$this->controls['builderHidden'] = array(
			'tab'    => 'content',
			'inline' => true,
			'small'  => true,
			'label'  => esc_html__( 'Hide content in builder', 'bricksextras' ),
			'type'   => 'checkbox',
		);

		$this->controls['fullShortcode'] = array(
			'tab'         => 'content',
			'label'       => esc_html__( 'Shortcode', 'bricks' ),
			'description' => esc_html__( 'Enter opening and closing shortcode tags', 'bricksextras' ),
			'type'        => 'text',
			'placeholder' => '[shortcode_open_tag][/shortcode_close_tag]',
		);

		$this->controls['shortcodeContent'] = array(
			'tab'         => 'content',
			'group'       => 'content',
			'label'       => esc_html__( 'Content', 'bricks' ),
			'type'        => 'select',
			'options'     => array(
				'template' => esc_html__( 'Template', 'bricks' ),
				'wysiwyg'  => esc_html__( 'Text editor', 'bricks' ),
				'nestable' => esc_html__( 'Add elements', 'bricks' ),
			),
			'inline'      => true,
			'placeholder' => esc_html__( 'Text editor', 'bricks' ),
			'clearable'   => false,
			'default'     => 'wysiwyg',
		);

		if ( ! class_exists( '\Bricks\Popups' ) ) {
			$templates_list = bricks_is_builder() ? \Bricks\Templates::get_templates_list( get_the_ID() ) : array();
		} else {
			$templates_list = bricks_is_builder() ? \Bricks\Templates::get_templates_list( array( 'section', 'content' ), get_the_ID() ) : array();
		}

		$this->controls['templateId'] = array(
			'tab'         => 'content',
			'group'       => 'content',
			'label'       => esc_html__( 'Template', 'bricks' ),
			'type'        => 'select',
			'options'     => $templates_list,
			'searchable'  => true,
			'placeholder' => esc_html__( 'Select template', 'bricks' ),
			'required'    => array( 'shortcodeContent', '!=', array( 'nestable', 'wysiwyg' ) ),
		);

		$this->controls['shortcodeWysiwyg'] = array(
			'tab'           => 'content',
			'label'         => esc_html__( 'Text editor', 'bricks' ),
			'type'          => 'editor',
			'group'         => 'content',
			'inlineEditing' => array(
				'selector' => '.text-editor',
				'toolbar'  => true,
			),
			'default'       => '<h4>Conditional content</h4><p>Edit me. Aenean commodo ligula egget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>',
			'required'      => array( 'shortcodeContent', '=', 'wysiwyg' ),
		);
	}


	/**
	 * Render element output.
	 */
	public function render() {

		echo "<div {$this->render_attributes( '_root' )}>";

		echo BricksExtras\Helpers::maybe_preview() && isset( $this->settings['builderHidden'] ) ? '<div class="x_hide_in_builder">' : '';

		$shortcode_content = isset( $this->settings['shortcodeContent'] ) ? $this->settings['shortcodeContent'] : 'template';

		$output = '';

		$template_id   = isset( $this->settings['templateId'] ) ? intval( $this->settings['templateId'] ) : false;
		$template      = do_shortcode( '[bricks_template id="' . $template_id . '"]' );
		$inner_content = '';

		if ( 'template' === $shortcode_content ) {
			$inner_content = $template;
		} elseif ( 'wysiwyg' === $shortcode_content ) {
			if ( isset( $this->settings['shortcodeWysiwyg'] ) ) {
				$content       = $this->settings['shortcodeWysiwyg'];
				$content       = $this->render_dynamic_data( $content );
				$inner_content = \Bricks\Helpers::parse_editor_content( $content );
			}
		} elseif ( method_exists( '\Bricks\Frontend', 'render_children' ) ) {
				$inner_content = \Bricks\Frontend::render_children( $this );
		}

		$shortcode_wrap = array();
		$full_shortcode = isset( $this->settings['fullShortcode'] ) ? wp_kses_post( bricks_render_dynamic_data( $this->settings['fullShortcode'] ) ) : false;

		preg_match( '/\[([^\s\]]{1,})[^\]]*\]/i', $full_shortcode, $shortcode_wrap );

		$output = '';

		if ( $full_shortcode ) {
			$output = ! empty( $shortcode_wrap ) ? do_shortcode( $shortcode_wrap[0] . $inner_content . '[/' . $shortcode_wrap[1] . ']' ) : '';
		} else {
			$output = $inner_content;
		}
		echo $output;
		echo BricksExtras\Helpers::maybe_preview() && isset( $this->settings['builderHidden'] ) ? '</div>' : '';

		echo '</div>';
	}
}
