<?php
/**
 * Theme Info Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Shared;

defined( 'ABSPATH' ) || exit;

use Errigal\Shared\Errigal_Validator as Validator;
use WP_Theme;

/**
 * This class is in charge of providing theme info functions.
 *
 * @since 1.1.0
 */
class Errigal_Theme_Info {
	/**
	 * Indicates theme is parent theme.
	 *
	 * @since 1.1.0
	 *
	 * @var bool
	 */
	public const IS_PARENT = true;

	/**
	 * Theme name.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const THEME_NAME = 'Name';

	/**
	 * Theme URI.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const THEME_URI = 'ThemeURI';

	/**
	 * Theme author URI.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const THEME_AUTHOR_URI = 'AuthorURI';

	/**
	 * Theme version.
	 *
	 * @since 1.1.0
	 *
	 * @var string
	 */
	private const THEME_VERSION = 'Version';

	/**
	 * Regular expression: Theme version.
	 *
	 * @since 1.2.0
	 *
	 * @var string
	 */
	private const REGEX_VERSION = '/[^0-9.]/';

	/**
	 * Gets theme name.
	 *
	 * @since 1.1.0
	 *
	 * @param bool $is_parent Optional. Whether to force getting parent theme name. Default false.
	 * @return string
	 */
	public static function get_theme_name( $is_parent = false ) {
		return esc_html( self::get_theme_property( self::THEME_NAME, $is_parent ) );
	}

	/**
	 * Gets theme URI.
	 *
	 * @since 1.1.0
	 *
	 * @param bool $is_parent Optional. Whether to force getting parent theme URI. Default false.
	 * @return string
	 */
	public static function get_theme_uri( $is_parent = false ) {
		return esc_url( self::get_theme_property( self::THEME_URI, $is_parent ) );
	}

	/**
	 * Gets theme author URI.
	 *
	 * @since 1.1.0
	 *
	 * @param bool $is_parent Optional. Whether to force getting parent theme author URI. Default false.
	 * @return string
	 */
	public static function get_theme_author_uri( $is_parent = false ) {
		return esc_url( self::get_theme_property( self::THEME_AUTHOR_URI, $is_parent ) );
	}

	/**
	 * Gets theme version.
	 *
	 * @since 1.1.0
	 *
	 * @param bool $is_parent Optional. Whether to force getting parent theme version. Default false.
	 * @return string
	 */
	public static function get_theme_version( $is_parent = false ) {
		return preg_replace(
			self::REGEX_VERSION,
			'',
			self::get_theme_property( self::THEME_VERSION, $is_parent )
		);
	}

	/**
	 * Gets theme property.
	 *
	 * @since 1.1.0
	 *
	 * @param string $offset    Property to get.
	 * @param bool   $is_parent Optional. Whether to force getting parent theme property. Default false.
	 * @return string
	 */
	private static function get_theme_property( $offset, $is_parent = false ) {
		if ( ! Validator::is_non_empty_string( $offset ) ) {
			return '';
		}

		$theme = self::get_theme( $is_parent );

		if ( ! self::is_wp_theme( $theme ) ) {
			return '';
		}

		return trim( $theme->get( $offset ) );
	}

	/**
	 * Gets theme.
	 *
	 * @since 1.1.0
	 *
	 * @param bool $is_parent Optional. Whether to force getting parent theme. Default false.
	 * @return WP_Theme
	 */
	private static function get_theme( $is_parent = false ) {
		if ( ! is_child_theme() || self::IS_PARENT !== $is_parent ) {
			return wp_get_theme();
		}

		$parent = wp_get_theme()->parent();

		if ( ! self::is_wp_theme( $parent ) ) {
			return wp_get_theme();
		}

		return $parent;
	}

	/**
	 * Determines whether $theme is instance of WP_Theme.
	 *
	 * @since 1.1.0
	 *
	 * @param WP_Theme $theme Theme object.
	 * @return bool
	 */
	private static function is_wp_theme( $theme ) {
		return $theme instanceof WP_Theme;
	}
}
