<?php
/**
 * Echo widget fragments.
 *
 * @package SplendorStudio\Framework\Templates\Fragments
 *
 * @since   1.0.0
 */
class Fancify_Lite_Widget_Area_Fragments extends Fancify_Lite_Fragments {
	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 */
	function __construct() {
		$this->register_hook_callbacks();
	}

	/**
	 * Register callbacks for actions and filters
	 *
	 * @since 1.0.0
	 */
	function register_hook_callbacks() {
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_widget', array( $this, 'widget_badge' ), 5 );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_widget', array( $this, 'widget_title' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_widget', array( $this, 'widget_content' ), 15 );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_no_widget', array( $this, 'no_widget' ) );
		Fancify_Lite_Filters::add_filter( 'fancify_lite_widget_content_rss_output', array( $this, 'widget_rss_content' ) );
		Fancify_Lite_Filters::add_filter( 'fancify_lite_widget_content_attributes', array( $this, 'modify_widget_content_attributes' ) );
		Fancify_Lite_Filters::add_filter( 'fancify_lite_widget_content_categories_output', array( $this, 'modify_widget_count' ) );
		Fancify_Lite_Filters::add_filter( 'fancify_lite_widget_content_archives_output', array( $this, 'modify_widget_count' ) );
		Fancify_Lite_Filters::add_filter( 'fancify_lite_widget_content_categories_output', array( $this, 'remove_widget_dropdown_label' ) );
		Fancify_Lite_Filters::add_filter( 'fancify_lite_widget_content_archives_output', array( $this, 'remove_widget_dropdown_label' ) );
	}

	/**
	 * Echo widget badge.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function widget_badge() {
		if ( ! fancify_lite_get_widget( 'badge' ) ) {
			return;
		}

		fancify_lite_open_markup_e( 'fancify_lite_widget_badge' . fancify_lite_widget_subfilters(), 'div', 'class=fancify-panel-badge fancify-badge' );

			echo fancify_lite_widget_shortcodes( fancify_lite_get_widget( 'badge_content' ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped -- Echoes HTML output.

		fancify_lite_close_markup_e( 'fancify_lite_widget_badge' . fancify_lite_widget_subfilters(), 'div' );
	}

	/**
	 * Echo widget title.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function widget_title() {
		$w_title = fancify_lite_get_widget( 'title' );

		if ( ! $w_title || ! fancify_lite_get_widget( 'show_title' ) ) {
			return;
		}

		fancify_lite_open_markup_e( 'fancify_lite_widget_title' . fancify_lite_widget_subfilters(), 'h2', 'class=fancify-widget-title' );

			fancify_lite_open_markup_e('fancify_lite_widget_title_text', 'span', array('class' => 'fancify-widget-title-text'));
				fancify_lite_output_e('fancify_lite_widget_title_text', esc_html( $w_title ) );
			fancify_lite_close_markup_e('fancify_lite_widget_title_text', 'span');

			fancify_lite_open_markup_e( 'fancify_lite_widget_divider' . fancify_lite_widget_subfilters(), 'span', 'class=fancify-widget-divider' );
			fancify_lite_close_markup_e( 'fancify_lite_widget_divider' . fancify_lite_widget_subfilters(), 'span' );

		fancify_lite_close_markup_e( 'fancify_lite_widget_title' . fancify_lite_widget_subfilters(), 'h2' );
	}

	/**
	 * Echo widget content.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function widget_content() {
		fancify_lite_open_markup_e( 'fancify_lite_widget_content' . fancify_lite_widget_subfilters(), 'div' );

			fancify_lite_output_e( 'fancify_lite_widget_content' . fancify_lite_widget_subfilters(), fancify_lite_get_widget( 'content' ) );

		fancify_lite_close_markup_e( 'fancify_lite_widget_content' . fancify_lite_widget_subfilters(), 'div' );
	}

	/**
	 * Echo no widget content.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function no_widget() {
		// Only apply this notice to sidebar_primary and sidebar_secondary.
		if ( ! in_array( fancify_lite_get_widget_area( 'id' ), array( 'sidebar_primary', 'sidebar_secondary' ), true ) ) {
			return;
		}
		// Return if this is normal user
		if ( ! current_user_can( 'edit_theme_options' ) ) {
			return;
		}

		fancify_lite_open_markup_e( 'fancify_lite_no_widget_notice', 'p', array( 'class' => 'fancify-alert fancify-alert-warning' ) );

			fancify_lite_output_e(
				'fancify_lite_no_widget_notice_text',
				// translators: Name of the widget area.
				sprintf( esc_html__( '%s does not have any widget assigned! (This message only visible for admin)', 'fancify-lite' ), fancify_lite_get_widget_area( 'name' ) )
			);

		fancify_lite_close_markup_e( 'fancify_lite_no_widget_notice', 'p' );
	}

	/**
	 * Modify RSS widget content.
	 *
	 * @since 1.0.0
	 *
	 * @return string The RSS widget content.
	 */
	public function widget_rss_content() {
		$options = fancify_lite_get_widget( 'options' );

		return '<p><a class="fancify-button" href="' . fancify_lite_get( 'url', $options ) . '" target="_blank">' . esc_html__( 'Read feed', 'fancify-lite' ) . '</a><p>';
	}

	/**
	 * Modify core widgets content attributes, so they use the default UIKit styling.
	 *
	 * @since 1.0.0
	 *
	 * @param array $attributes The current widget attributes.
	 *
	 * @return array The modified widget attributes.
	 */
	public function modify_widget_content_attributes( $attributes ) {
		$type = fancify_lite_get_widget( 'type' );

		$target = array(
			'archives',
			'categories',
			'links',
			'meta',
			'pages',
			'recent-posts',
			'recent-comments',
			'nav_menu',
		);

		$current_class = isset( $attributes['class'] ) ? $attributes['class'] . ' ' : '';

		if ( in_array( fancify_lite_get_widget( 'type' ), $target, true ) ) {
			$attributes['class'] = esc_attr( $current_class ) . 'fancify-list';
		}

		if ( 'calendar' === $type ) {
			$attributes['class'] = esc_attr( $current_class ) . 'fancify-table fancify-table-condensed';
		}
		
		if ( 'tag_cloud' === $type ) {
			$attributes['class'] = esc_attr( $current_class ) . 'fancify-tag-cloud';
		}

		return $attributes;
	}

	/**
	 * Modify widget count.
	 *
	 * @since 1.0.0
	 *
	 * @param string $content The widget content.
	 *
	 * @return string The modified widget content.
	 */
	public function modify_widget_count( $content ) {
		$count = fancify_lite_output( 'fancify_lite_widget_count', '$1' );

		if ( true === fancify_lite_get( 'dropdown', fancify_lite_get_widget( 'options' ) ) ) {
			$output = $count;
		} else {
			$output  = fancify_lite_open_markup( 'fancify_lite_widget_count', 'span', 'class=fancify-count' );
			$output .= $count;
			$output .= fancify_lite_close_markup( 'fancify_lite_widget_count', 'span' );
		}

		// Keep closing tag to avoid overwriting the inline JavaScript.
		return preg_replace( '#>((\s|&nbsp;)\((.*)\))#', '>' . $output, $content );
	}

	/**
	 * Modify widget dropdown label.
	 *
	 * @since 1.0.0
	 *
	 * @param string $content The widget content.
	 *
	 * @return string The modified widget content.
	 */
	public function remove_widget_dropdown_label( $content ) {
		return preg_replace( '#<label([^>]*)class="screen-reader-text"(.*?)>(.*?)</label>#', '', $content );
	}

}

new Fancify_Lite_Widget_Area_Fragments;