}>|null */ private static ?array $manifest = null; /** * Whether frontend JS should be served as bundles. */ public static function is_active(): bool { if ( FrontendAssetOptimizer::debugging() ) { return false; } /** * Filters whether the frontend JS chain is served as concatenated bundles. * Return false to keep per-file (still minified) loading. * * @param bool $bundle Default true. */ if ( ! apply_filters( 'adpresso_bundle_frontend_js', true ) ) { return false; } $manifest = self::manifest(); if ( $manifest === [] ) { return false; } $root = plugin_dir_path( ADPRESSO_FILE ); foreach ( array_keys( $manifest ) as $bundle ) { if ( ! is_file( $root . self::BUILD_DIR . $bundle . '.js' ) ) { return false; } } return true; } /** * Registers the bundle handles plus a src=false alias for every member handle. */ public static function register( Assets $assets ): void { foreach ( self::manifest() as $bundle => $spec ) { $assets->register_script( $bundle, self::BUILD_DIR . $bundle . '.js', $spec['deps'], true ); foreach ( array_keys( $spec['members'] ) as $member_handle ) { // in_footer:true - without it the alias sits in the head group and drags its bundle + adpresso-main-frontend up with it. wp_register_script( 'adpresso-' . $member_handle, false, [ 'adpresso-' . $bundle ], ADPRESSO_VERSION, true ); } } } /** * @return array}> */ public static function manifest(): array { if ( self::$manifest !== null ) { return self::$manifest; } $path = plugin_dir_path( ADPRESSO_FILE ) . self::MANIFEST; $raw = is_file( $path ) ? file_get_contents( $path ) : ''; $data = $raw !== '' ? json_decode( $raw, true ) : null; self::$manifest = is_array( $data['bundles'] ?? null ) ? $data['bundles'] : []; return self::$manifest; } /** * Test helper - clears the cached manifest. */ public static function reset(): void { self::$manifest = null; } }