<?php
/**
 * Theme.
 *
 * @package MadeByAura\Prime\Helpers
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Helpers;

defined( 'ABSPATH' ) || die();

/**
 * Theme.
 */
class Theme {
	/**
	 * Get theme information.
	 *
	 * @param string $key  Key.
	 * @return string
	 */
	public static function get_info( $key ) {
		static $info;

		if ( ! $info ) {
			$info['slug'] = get_template();
			$info['url']  = trailingslashit( get_template_directory_uri() );
			$info['path'] = trailingslashit( wp_normalize_path( get_template_directory() ) );

			if ( is_child_theme() ) {
				$info['version'] = wp_get_theme( wp_get_theme()->get( 'Template' ) )->get( 'Version' );
			} else {
				$info['version'] = wp_get_theme()->get( 'Version' );
			}

			// Child theme.
			$info['child_slug']    = get_stylesheet();
			$info['child_url']     = trailingslashit( get_stylesheet_directory_uri() );
			$info['child_path']    = trailingslashit( wp_normalize_path( get_stylesheet_directory() ) );
			$info['child_version'] = wp_get_theme()->get( 'Version' );
		}

		return $info[ $key ];
	}

	/**
	 * Wrapper for `view()`.
	 *
	 * @param string $path  Path of view.
	 * @param array  $args  Optional. Arguments passed to view.
	 */
	public static function view( $path, $args = [] ) {
		/**
		 * Dynamically filters whether to short-circuit view.
		 *
		 * @param bool   $disabled  Whether to short-circuit view. Default false.
		 * @param string $path      Path of view.
		 * @param array  $args      Arguments passed to view.
		 */
		$disabled = apply_filters( "aura_theme|view|disable|{$path}", false, $path, $args );

		/**
		 * Filters whether to short-circuit view.
		 *
		 * @param bool   $disabled  Whether to short-circuit view.
		 *                          Default false.
		 * @param string $path      Path of view.
		 * @param array  $args      Arguments passed to view.
		 */
		$disabled = apply_filters( 'aura_theme|view|disable', $disabled, $path, $args );

		// Do not proceed if view is disabled.
		if ( true === $disabled ) {
			return;
		}

		/**
		 * Dynamically filters arguments passed to view.
		 *
		 * @param array  $args  Arguments passed to view.
		 * @param string $path  Path of view.
		 */
		$args = apply_filters( "aura_theme|view|args|{$path}", $args, $path );

		/**
		 * Filters arguments passed to view.
		 *
		 * @param array  $args  Arguments passed to view.
		 * @param string $path  Path of view.
		 */
		$args = apply_filters( 'aura_theme|view|args', $args, $path );

		/**
		 * Dynamically filters whether to hide view.
		 *
		 * @param bool   $hidden  Whether to hide view. Default false.
		 * @param string $path    Path of view.
		 * @param array  $args    Arguments passed to view.
		 */
		$hidden = apply_filters( "aura_theme|view|hide|{$path}", false, $path, $args );

		/**
		 * Filters whether to short-circuit view.
		 *
		 * @param bool   $hidden  Whether to hide view. Default false.
		 * @param string $path    Path of view.
		 * @param array  $args    Arguments passed to view.
		 */
		$hidden = apply_filters( 'aura_theme|view|hide', $hidden, $path, $args );

		/**
		 * Dynamically fires before `get_template_part()` is called.
		 *
		 * @param string $path  Path of view.
		 * @param array  $args  Arguments passed to view.
		 */
		do_action( "aura_theme|view|before|{$path}", $path, $args );

		/**
		 * Fires before `get_template_part()` is called.
		 *
		 * @param string $path  Path of view.
		 * @param array  $args  Arguments passed to view.
		 */
		do_action( 'aura_theme|view|before', $path, $args );

		if ( ! $hidden ) {
			get_template_part( $path, null, $args );
		}

		/**
		 * Dynamically fires after `get_template_part()` is called.
		 *
		 * @param string $path  Path of view.
		 * @param array  $args  Arguments passed to view.
		 */
		do_action( "aura_theme|view|after|{$path}", $path, $args );

		/**
		 * Fires after `get_template_part()` is called.
		 *
		 * @param string $path  Path of view.
		 * @param array  $args  Arguments passed to view.
		 */
		do_action( 'aura_theme|view|after', $path, $args );
	}

	/**
	 * Wrapper for `get_template_part()`.
	 *
	 * Adds hooks to
	 *
	 * @param string $slug  Slug of template part.
	 * @param array  $args  Optional. Arguments passed to template part.
	 */
	public static function get_template_part( $slug, $args = [] ) {
		/**
		 * Dynamically filters whether to short-circuit template part.
		 *
		 * @param bool   $disabled  Whether to short-circuit template part.
		 *                          Default false
		 * @param string $slug      Slug of template part.
		 * @param array  $args      Arguments passed to template part.
		 */
		$disabled = apply_filters( "aura_theme|template_part|disable|{$slug}", false, $slug, $args );

		/**
		 * Filters whether to short-circuit template part.
		 *
		 * @param bool   $disabled  Whether to short-circuit template part.
		 *                          Default false.
		 * @param string $slug      Slug of template part.
		 * @param array  $args      Arguments passed to template part.
		 */
		$disabled = apply_filters( 'aura_theme|template_part|disable', $disabled, $slug, $args );

		// Do not proceed if template part is disabled.
		if ( true === $disabled ) {
			return;
		}

		/**
		 * Dynamically filters arguments passed to template part.
		 *
		 * @param array  $args  Arguments passed to template part.
		 * @param string $slug  Slug of template part.
		 */
		$args = apply_filters( "aura_theme|template_part|args|{$slug}", $args, $slug );

		/**
		 * Filters arguments passed to template part.
		 *
		 * @param array  $args  Arguments passed to template part.
		 * @param string $slug  Slug of template part.
		 */
		$args = apply_filters( 'aura_theme|template_part|args', $args, $slug );

		/**
		 * Dynamically filters whether to hide template part.
		 *
		 * @param bool   $hidden  Whether to hide template part. Default false.
		 * @param string $slug    Slug of template part.
		 * @param array  $args    Arguments passed to template part.
		 */
		$hidden = apply_filters( "aura_theme|template_part|hide|{$slug}", false, $slug, $args );

		/**
		 * Filters whether to short-circuit template part.
		 *
		 * @param bool   $hidden  Whether to hide template part. Default false.
		 * @param string $slug    Slug of template part.
		 * @param array  $args    Arguments passed to template part.
		 */
		$hidden = apply_filters( 'aura_theme|template_part|hide', $hidden, $slug, $args );

		/**
		 * Dynamically fires before `get_template_part()` is called.
		 *
		 * @param string $slug  Slug of template part.
		 * @param array  $args  Arguments passed to template part.
		 */
		do_action( "aura_theme|template_part|before|{$slug}", $slug, $args );

		/**
		 * Fires before `get_template_part()` is called.
		 *
		 * @param string $slug  Slug of template part.
		 * @param array  $args  Arguments passed to template part.
		 */
		do_action( 'aura_theme|template_part|before', $slug, $args );

		if ( ! $hidden ) {
			get_template_part( "template-parts/{$slug}", null, $args );
		}

		/**
		 * Dynamically fires after `get_template_part()` is called.
		 *
		 * @param string $slug  Slug of template part.
		 * @param array  $args  Arguments passed to template part.
		 */
		do_action( "aura_theme|template_part|after|{$slug}", $slug, $args );

		/**
		 * Fires after `get_template_part()` is called.
		 *
		 * @param string $slug  Slug of template part.
		 * @param array  $args  Arguments passed to template part.
		 */
		do_action( 'aura_theme|template_part|after', $slug, $args );
	}
}
