init_hooks(); } /** * Get the single instance of the Assets class. * * @since 1.0.0 * @access public * * @return Assets The single instance. */ public static function get_instance() { if ( is_null( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } /** * Initialize WordPress hooks for asset enqueueing. * * @since 1.0.0 * @access private * * @return void */ private function init_hooks() { // Editor Assets (priority 2 to load early). add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ), 2 ); // Frontend + Editor Assets. add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ) ); } /** * Enqueue assets for both frontend and editor. * * Loads Font Awesome, Pristine validation library, and global styles. * * @since 1.0.0 * @access public * * @return void */ public function enqueue_block_assets() { // Font Awesome for icon library. wp_enqueue_style( 'acumenpopup-fontawesome', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'assets/css/fontawesome.css', array(), ACUMENPOPUP_VERSION, 'all' ); // Register Pristine validation library for form block viewScript. wp_register_script( 'acumenpopup-blocks-form-validation', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'assets/js/pristine.min.js', array(), ACUMENPOPUP_VERSION, true ); // Localize AJAX settings for frontend form submissions. wp_localize_script( 'acumenpopup-blocks-form-validation', 'acumenpopupGLocalize', array( 'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ), 'nonce' => wp_create_nonce( 'acumenpopup_form_submit' ), ) ); // Global styles. wp_enqueue_style( 'acumenpopup-global-styles', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'assets/css/global.css', array(), ACUMENPOPUP_VERSION, 'all' ); // Frontend only scripts. if ( ! is_admin() ) { wp_enqueue_script( 'acumenpopup-popup-triggers', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'assets/js/popup-triggers.js', array(), ACUMENPOPUP_VERSION, true ); wp_localize_script( 'acumenpopup-popup-triggers', 'acumenpopupTracker', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'acumenpopup_tracker' ), ) ); } } /** * Enqueue assets for the block editor only. * * Loads modules and global editor scripts/styles. * * @since 1.0.0 * @access public * * @return void */ public function enqueue_editor_assets() { // Only load in admin context. if ( ! is_admin() ) { return; } // Load modules script. $this->enqueue_modules_script(); // Load global script. $this->enqueue_global_script(); } /** * Enqueue the modules script and styles. * * @since 1.0.0 * @access private * * @return void */ private function enqueue_modules_script() { $asset_file = trailingslashit( ACUMENPOPUP_PLUGIN_DIR ) . 'build/modules/index.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $dependencies = require $asset_file; wp_enqueue_script( 'acumenpopup-modules-script', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'build/modules/index.js', $dependencies['dependencies'], $dependencies['version'], false ); // Localize script with theme fonts and admin email. wp_localize_script( 'acumenpopup-modules-script', 'acumenpopupParams', array( 'theme_fonts' => $this->get_theme_fonts(), 'admin_email' => sanitize_email( get_option( 'admin_email' ) ), ) ); } /** * Enqueue the global script and styles. * * @since 1.0.0 * @access private * * @return void */ private function enqueue_global_script() { $asset_file = trailingslashit( ACUMENPOPUP_PLUGIN_DIR ) . 'build/global/index.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $dependencies = require $asset_file; wp_enqueue_script( 'acumenpopup-global-script', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'build/global/index.js', $dependencies['dependencies'], ACUMENPOPUP_VERSION, false ); wp_enqueue_style( 'acumenpopup-global-style', trailingslashit( ACUMENPOPUP_URL_FILE ) . 'build/global/index.css', array(), ACUMENPOPUP_VERSION, 'all' ); } /** * Get theme fonts from global settings. * * Retrieves font families defined in the theme's global settings * and returns them sorted with system fonts first. * * @since 1.0.0 * @access public * * @return array Array of font family names. */ public function get_theme_fonts() { $global_settings = wp_get_global_settings(); $global_fonts = isset( $global_settings['typography']['fontFamilies'] ) ? $global_settings['typography']['fontFamilies'] : array(); if ( empty( $global_fonts ) ) { return array(); } $theme_fonts = array(); $custom_fonts = array(); // Collect theme fonts. if ( isset( $global_fonts['theme'] ) && ! empty( $global_fonts['theme'] ) ) { foreach ( $global_fonts['theme'] as $font ) { if ( isset( $font['name'] ) ) { $theme_fonts[] = sanitize_text_field( $font['name'] ); } } } // Collect custom fonts. if ( isset( $global_fonts['custom'] ) && ! empty( $global_fonts['custom'] ) ) { foreach ( $global_fonts['custom'] as $font ) { if ( isset( $font['name'] ) ) { $custom_fonts[] = sanitize_text_field( $font['name'] ); } } } // Merge theme and custom fonts. $final_fonts = array_merge( $theme_fonts, $custom_fonts ); // Separate system fonts. $system_fonts = array_filter( $final_fonts, function ( $font ) { return false !== strpos( strtolower( $font ), 'system' ); } ); // Return with system fonts at the top. return array_values( array_merge( $system_fonts, array_diff( $final_fonts, $system_fonts ) ) ); } /** * 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' ); } }