<?php
/**
 * QrCdr Fonts class
 *
 * Defines the svg fonts.
 *
 * @since  1.0.0
 *
 * @category  WordPress_Plugin
 * @package   QrCdr
 * @author    Nicola Franchini
 * @link      https://veno.es
 */

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

if ( ! class_exists( 'QrCdr_Fonts' ) ) {
	/**
	 * QrCdr_Fonts class
	 */
	class QrCdr_Fonts {
		/**
		 * Holds an instance of the object
		 *
		 * @var instance
		 */
		protected static $instance = null;

		/**
		 * Cache for the processed fonts list.
		 *
		 * @var array|null
		 */
		protected $fonts = null;

		/**
		 * Returns the running object
		 *
		 * @return QrCdr_Fonts
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Get default fonts scanning the directory
		 *
		 * @return array
		 */
		public function get_default_fonts() {
			$qrcdr_fonts = array();
			$qrcdr_fontdir = dirname( __DIR__ ) . '/lib/fonts';
			$qrcdr_getfonts = glob( $qrcdr_fontdir . '/*.svg' );

			if ( is_array( $qrcdr_getfonts ) ) {
				foreach ( $qrcdr_getfonts as $qrcdr_font ) {
					$basename = basename( $qrcdr_font );
					$qrcdr_fonts[ $basename ] = esc_html( basename( $qrcdr_font, '.svg' ) );
				}
			}
			return $qrcdr_fonts;
		}

		/**
		 * Get available fonts (filtered and cached)
		 *
		 * @return array
		 */
		public function get_fonts() {
			// Se la lista è già stata generata e filtrata durante questa sessione, caricala dalla memoria.
			if ( null === $this->fonts ) {
				$this->fonts = apply_filters( 'qrcdr_available_fonts', $this->get_default_fonts() );
			}
			return $this->fonts;
		}
	}
}
