<?php
/**
 * Theme setup and feature registration.
 *
 * @package Eronex
 * @since   0.0.1
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Eronex_Theme_Setup initial setup
 *
 * @package Eronex
 * @since   0.0.1
 */
if ( ! class_exists( 'Eronex_Theme_Setup' ) ) {

	/**
	 * Registers theme supports, menus, and related filters.
	 *
	 * @package Eronex
	 * @since   0.0.1
	 */
	class Eronex_Theme_Setup {

		/**
		 * Singleton instance.
		 *
		 * @since 0.0.1
		 * @var Eronex_Theme_Setup|null
		 */
		private static $instance;

		/**
		 * Returns the singleton instance.
		 *
		 * @since 0.0.1
		 *
		 * @return Eronex_Theme_Setup Instance of the class.
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Constructor. Hooks theme setup to after_setup_theme.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function __construct() {
			add_action( 'after_setup_theme', array( $this, 'setup_theme' ), 2 );
		}

		/**
		 * Registers theme supports, nav menus, and embed/WooCommerce settings.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function setup_theme() {
			/**
			 * Fires after the Eronex theme has loaded its setup routine.
			 *
			 * @since 0.0.1
			 */
			do_action( 'eronex_theme_loaded' );

			/**
			 * Set the content width in pixels, based on the theme's design.
			 * Matches the default `.eronex-container` max-width.
			 *
			 * @global int $content_width
			 */
			$GLOBALS['content_width'] = apply_filters( 'eronex_content_width', 1140 );

			/**
			 * Make theme available for translation
			 * Translations can be filed in the /languages/ directory
			 */
			load_theme_textdomain( 'eronex', ERONEX_DIR . 'languages' );

			/**
			 * Gutenberg / block editor
			 */
			add_theme_support( 'align-wide' );
			add_theme_support( 'editor-styles' );
			add_theme_support( 'wp-block-styles' );
			add_theme_support( 'responsive-embeds' );

			/**
			 * Load editor stylesheet so the block/classic editor matches the front end.
			 */
			add_editor_style( 'assets/css/editor-style.css' );

			//register nav menu
			register_nav_menus(
				array(
					'primary'   => __( 'Primary Menu', 'eronex' ),
					'hamburger' => __( 'Hamburger Menu', 'eronex' ),
				)
			);

			// Add default posts and comments RSS feed links to head
			add_theme_support( 'automatic-feed-links' );

			// Let WordPress manage the document title.
			add_theme_support( 'title-tag' );

			// Enable support for Post Thumbnails on posts and pages
			add_theme_support( 'post-thumbnails' );

			// Switch default core markup for search form, comment form, and comments
			add_theme_support(
				'html5',
				array(
					'navigation-widgets',
					'search-form',
					'comment-form',
					'comment-list',
					'gallery',
					'caption',
					'style',
					'script',
				)
			);

			// Post formats
			add_theme_support(
				'post-formats',
				array(
					'gallery',
					'image',
					'link',
					'quote',
					'video',
					'audio',
					'status',
					'aside',
				)
			);

			// Customize Selective Refresh Widgets
			add_theme_support( 'customize-selective-refresh-widgets' );

			/**
			 * Filters whether to wrap oEmbed output in a full-width responsive container.
			 *
			 * @since 0.0.1
			 *
			 * @param bool $enable Whether to enable the full-width oEmbed wrapper. Default true.
			 */
			if ( apply_filters( 'eronex_fullwidth_oembed', true ) ) {
				// Filters the oEmbed process to run the responsive_oembed_wrapper() function.
				add_filter( 'embed_oembed_html', array( $this, 'responsive_oembed_wrapper' ), 10, 3 );
			}

			// WooCommerce
			add_theme_support(
				'woocommerce',
				array(
					'thumbnail_image_width'         => 150,
					'single_image_width'            => 800,
					'gallery_thumbnail_image_width' => 100,
				)
			);

			add_theme_support( 'wc-product-gallery-zoom' );
			add_theme_support( 'wc-product-gallery-lightbox' );
			add_theme_support( 'wc-product-gallery-slider' );

			// Rank Math Breadcrumb
			/**
			 * Filters whether to declare Rank Math breadcrumb theme support.
			 *
			 * @since 0.0.1
			 *
			 * @param bool $enable Whether to add Rank Math breadcrumb support. Default true.
			 */
			if ( true === apply_filters( 'eronex_rank_math_theme_support', true ) ) {
				add_theme_support( 'rank-math-breadcrumbs' );
			}
		}

		/**
		 * Adds a responsive embed wrapper around oEmbed content.
		 *
		 * @since 0.0.1
		 *
		 * @param string $html           The oEmbed markup.
		 * @param string $url            The URL being embedded.
		 * @param array  $attr           An array of attributes.
		 * @param bool   $core_yt_block  Whether the oEmbed is being rendered by the core YouTube block.
		 * @return string Updated embed markup.
		 */
		public function responsive_oembed_wrapper( $html, $url, $attr, $core_yt_block = false ) {

			/**
			 * Filters whether to wrap allowed oEmbeds in the Eronex responsive container.
			 *
			 * @since 0.0.1
			 *
			 * @param bool $enable Whether to add the responsive oEmbed wrapper. Default true.
			 */
			$add_eronex_oembed_wrapper = apply_filters( 'eronex_responsive_oembed_wrapper_enable', true );

			/**
			 * Filters additional CSS class(es) for the Eronex oEmbed wrapper.
			 *
			 * @since 0.0.1
			 *
			 * @param string $class Extra CSS class string. Default empty.
			 */
			$eronex_embed_wrapper_class = apply_filters( 'eronex_embed_wrapper_class', '' );

			/**
			 * Filters the list of oEmbed hostnames that receive the full-width wrapper.
			 *
			 * @since 0.0.1
			 *
			 * @param string[] $providers Hostnames that should use the full-width wrapper.
			 */
			$allowed_providers = apply_filters(
				'eronex_allowed_fullwidth_oembed_providers',
				array(
					'vimeo.com',
					'youtube.com',
					'youtu.be',
					'wistia.com',
					'wistia.net',
					'spotify.com',
					'soundcloud.com',
					'animoto.com',
					'cloudup.com',
					'poll.fm',
					'dai.ly',
					'mixcloud.com',
					'pca.st',
					'reddit.com',
					'scribd.com',
					'slideshare.net',
					'speakerdeck.com',
					'tumblr.com',
					'videopress.com',
					'wordpress.org',
					'wordpress.tv',
					'imgur.com',
					'ted.com',
				)
			);

			if ( eronex_strposa( $url, $allowed_providers ) && $add_eronex_oembed_wrapper ) {
				if ( $core_yt_block ) {
					$embed_html = wp_oembed_get( $url );
					$html       = false !== $embed_html ? '<div class="wp-block-embed__wrapper"> <div class="eronex-oembed-container ' . esc_attr( $eronex_embed_wrapper_class ) . '" style="height: 100%;">' . $embed_html . '</div> </div>' : '';
				} else {
					$html = ( '' !== $html ) ? '<div class="eronex-oembed-container ' . esc_attr( $eronex_embed_wrapper_class ) . '" style="height: 100%;">' . $html . '</div>' : '';
				}
			} elseif ( '' === $html || trim( $html ) === $url ) {
				$embed_html = wp_oembed_get( $url, array( 'width' => 600 ) );
				$html       = $embed_html ? $embed_html : $url;
				wp_maybe_enqueue_oembed_host_js( $html );
			}

			return $html;
		}
	}
}

//Initiated Class
Eronex_Theme_Setup::get_instance();
