enqueue_frontend_assets(); return; } global $post; if ( ! $post ) { return; } // Check if page contains our shortcode. if ( has_shortcode( $post->post_content, 'aca_my_account' ) ) { $this->enqueue_frontend_assets(); } } /** * Enqueue frontend assets. */ public function enqueue_frontend_assets() { // Check if user is logged in. if ( ! is_user_logged_in() ) { return; } $asset_file = ACA_PLUGIN_DIR . 'build/index.asset.php'; if ( ! file_exists( $asset_file ) ) { // Development fallback. $assets = array( 'dependencies' => array( 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n', 'wp-data', 'wp-hooks', 'wp-notices', 'wp-compose', 'wp-url', ), 'version' => ACA_VERSION, ); } else { $assets = include $asset_file; } $dependencies = array_values( array_diff( $assets['dependencies'], array( 'react-jsx-runtime' ) ) ); if ( ! in_array( 'wp-element', $dependencies, true ) ) { $dependencies[] = 'wp-element'; } // Enqueue main script. wp_enqueue_script( 'aca-frontend', ACA_PLUGIN_URL . 'build/index.js', $dependencies, $assets['version'], true ); // Set up api-fetch with nonce middleware. wp_add_inline_script( 'wp-api-fetch', sprintf( 'wp.apiFetch.use( wp.apiFetch.createNonceMiddleware( "%s" ) );', wp_create_nonce( 'wp_rest' ) ), 'after' ); // Localize script. wp_localize_script( 'aca-frontend', 'acaSettings', $this->get_localized_data() ); // Set script translations. wp_set_script_translations( 'aca-frontend', 'advanced-customer-account', ACA_PLUGIN_DIR . 'languages' ); // Enqueue WordPress components styles. wp_enqueue_style( 'wp-components' ); // Enqueue dashicons. wp_enqueue_style( 'dashicons' ); // Enqueue main stylesheet. $css_file = ACA_PLUGIN_DIR . 'build/index.css'; if ( file_exists( $css_file ) ) { wp_enqueue_style( 'aca-frontend', ACA_PLUGIN_URL . 'build/index.css', array( 'wp-components' ), filemtime( $css_file ) ); } // Enqueue custom styles. $custom_css_file = ACA_PLUGIN_DIR . 'build/style-index.css'; if ( file_exists( $custom_css_file ) ) { wp_enqueue_style( 'aca-frontend-custom', ACA_PLUGIN_URL . 'build/style-index.css', array( 'aca-frontend' ), filemtime( $custom_css_file ) ); } // Inject dynamic brand color variables scoped strictly to #aca-root and .aca-app. $settings = get_option( 'aca_settings', array() ); $primary_color = ! empty( $settings['primary_color'] ) ? sanitize_hex_color( $settings['primary_color'] ) : '#5304c1'; if ( ! $primary_color ) { $primary_color = '#5304c1'; } $hex = ltrim( $primary_color, '#' ); if ( strlen( $hex ) === 3 ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } $r = hexdec( substr( $hex, 0, 2 ) ); $g = hexdec( substr( $hex, 2, 2 ) ); $b = hexdec( substr( $hex, 4, 2 ) ); $primary_light = sprintf( 'rgba(%d, %d, %d, 0.85)', $r, $g, $b ); $primary_dark = sprintf( '#%02x%02x%02x', max( 0, $r - 35 ), max( 0, $g - 35 ), max( 0, $b - 35 ) ); $primary_bg = sprintf( 'rgba(%d, %d, %d, 0.12)', $r, $g, $b ); $primary_glow = sprintf( 'rgba(%d, %d, %d, 0.25)', $r, $g, $b ); $dynamic_css = " #aca-root, .aca-app { --aca-color-primary: {$primary_color}; --aca-color-primary-light: {$primary_light}; --aca-color-primary-dark: {$primary_dark}; --aca-color-primary-bg: {$primary_bg}; --aca-color-primary-glow: {$primary_glow}; } "; wp_add_inline_style( 'aca-frontend', $dynamic_css ); } /** * Get localized data for frontend. * * @return array */ private function get_localized_data() { $customer_id = get_current_user_id(); $customer = new WC_Customer( $customer_id ); $settings = get_option( 'aca_settings', array() ); return array( 'restUrl' => esc_url_raw( rest_url( 'aca/v1/' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'userId' => $customer_id, 'siteUrl' => esc_url( home_url() ), 'shopUrl' => esc_url( wc_get_page_permalink( 'shop' ) ), 'cartUrl' => esc_url( wc_get_cart_url() ), 'checkoutUrl' => esc_url( wc_get_checkout_url() ), 'logoutUrl' => esc_url( wc_logout_url() ), 'currency' => get_woocommerce_currency(), 'currencySymbol' => html_entity_decode( get_woocommerce_currency_symbol() ), 'dateFormat' => get_option( 'date_format' ), 'user' => array( 'displayName' => $customer->get_display_name(), 'email' => $customer->get_email(), 'avatarUrl' => get_avatar_url( $customer_id, array( 'size' => 96 ) ), ), 'menuItems' => class_exists( 'ACA_Menu_API' ) ? ACA_Menu_API::get_merged_menu_items() : array(), 'settings' => array( 'primaryColor' => isset( $settings['primary_color'] ) ? $settings['primary_color'] : '#5304c1', 'enableDarkMode' => isset( $settings['enable_dark_mode'] ) ? (bool) $settings['enable_dark_mode'] : false, ), 'i18n' => array( 'dashboard' => __( 'Dashboard', 'advanced-customer-account' ), 'orders' => __( 'Orders', 'advanced-customer-account' ), 'addresses' => __( 'Addresses', 'advanced-customer-account' ), 'profile' => __( 'Account Details', 'advanced-customer-account' ), 'downloads' => __( 'Downloads', 'advanced-customer-account' ), 'coupons' => __( 'My Coupons', 'advanced-customer-account' ), 'offers' => __( 'Exclusive Offers', 'advanced-customer-account' ), 'logout' => __( 'Logout', 'advanced-customer-account' ), 'loading' => __( 'Loading...', 'advanced-customer-account' ), 'error' => __( 'Something went wrong.', 'advanced-customer-account' ), 'noOrders' => __( 'No orders yet.', 'advanced-customer-account' ), 'viewOrder' => __( 'View Order', 'advanced-customer-account' ), 'reorder' => __( 'Reorder', 'advanced-customer-account' ), 'saveChanges' => __( 'Save Changes', 'advanced-customer-account' ), 'cancel' => __( 'Cancel', 'advanced-customer-account' ), 'copy' => __( 'Copy', 'advanced-customer-account' ), 'copied' => __( 'Copied!', 'advanced-customer-account' ), 'applyCoupon' => __( 'Apply to Cart', 'advanced-customer-account' ), 'addToCart' => __( 'Add to Cart', 'advanced-customer-account' ), ), ); } }