init_hooks(); } /** * Get the single instance of the Fonts_Loader class. * * @since 1.0.0 * @access public * * @return Fonts_Loader The single instance. */ public static function get_instance() { if ( is_null( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } /** * Initialize WordPress hooks. * * @since 1.0.0 * @access private * * @return void */ private function init_hooks() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_fonts' ), 9999 ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_fonts' ), 9999 ); add_action( 'acumenpopup_render_block', array( $this, 'collect_block_fonts' ) ); } /** * Collect font families from a rendered block. * * Scans block attributes for font family settings and * adds them to the fonts collection. * * @since 1.0.0 * @access public * * @param array $block The block data including attributes. * * @return void */ public function collect_block_fonts( $block ) { if ( ! isset( $block['attrs'] ) || ! is_array( $block['attrs'] ) ) { return; } $attributes = $block['attrs']; foreach ( $attributes as $key => $value ) { // Check for font family attributes. if ( ! empty( $value ) && 0 === strpos( $key, 'acumenpopup_' ) && false !== strpos( $key, 'FontFamily' ) ) { self::$all_fonts[] = sanitize_text_field( $value ); } } // Trigger font loading after collection. $this->enqueue_fonts(); } /** * Enqueue collected Google Fonts. * * Processes the collected fonts and enqueues them from Google Fonts API. * * @since 1.0.0 * @access public * * @return void */ public function enqueue_fonts() { if ( empty( self::$all_fonts ) || ! is_array( self::$all_fonts ) ) { return; } // Get unique fonts. $fonts = array_filter( array_unique( self::$all_fonts ) ); if ( empty( $fonts ) ) { return; } // Font weight variations to include. $font_weights = ':100,200,300,400,500,600,700,800,900'; foreach ( $fonts as $font ) { // Skip system fonts. if ( in_array( $font, $this->system_fonts, true ) || empty( $font ) ) { continue; } $font_slug = str_replace( ' ', '+', trim( $font ) ); $font_url = apply_filters( 'acumenpopup_google_font_url', add_query_arg( array( 'family' => $font_slug . $font_weights ), 'https://fonts.googleapis.com/css' ) ); $font_handle = 'acumenpopup-font-' . sanitize_title( $font ); // Register and enqueue the font. wp_register_style( $font_handle, esc_url( $font_url ), array(), ACUMENPOPUP_VERSION, 'all' ); wp_enqueue_style( $font_handle ); } } /** * Add a font to the collection manually. * * @since 1.0.0 * @access public * * @param string $font_family The font family name to add. * * @return void */ public function add_font( $font_family ) { if ( ! empty( $font_family ) ) { self::$all_fonts[] = sanitize_text_field( $font_family ); } } /** * Get all collected fonts. * * @since 1.0.0 * @access public * * @return array Array of collected font families. */ public function get_fonts() { return array_filter( array_unique( self::$all_fonts ) ); } /** * Clear all collected fonts. * * @since 1.0.0 * @access public * * @return void */ public function clear_fonts() { self::$all_fonts = array(); } /** * Prevent cloning of the instance. * * @since 1.0.0 * @access public * * @return void */ public function __clone() { _doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning is forbidden.', 'acumen-popup' ), '1.0.0' ); } /** * Prevent unserializing of the instance. * * @since 1.0.0 * @access public * * @return void */ public function __wakeup() { _doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing is forbidden.', 'acumen-popup' ), '1.0.0' ); } }