AssetsService.php
2 years ago
AssetsServiceProvider.php
3 years ago
BlockAssetsLoadService.php
3 years ago
PreloadService.php
3 years ago
ScriptsService.php
2 years ago
StylesService.php
3 years ago
PreloadService.php
138 lines
| 1 | <?php |
| 2 | namespace SureCart\WordPress\Assets; |
| 3 | |
| 4 | use SureCart\Support\Arrays; |
| 5 | |
| 6 | /** |
| 7 | * Handles the preloading functionality for components. |
| 8 | */ |
| 9 | class PreloadService { |
| 10 | /** |
| 11 | * The stats file path. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $stats_file; |
| 16 | |
| 17 | /** |
| 18 | * Components to preload. |
| 19 | * |
| 20 | * @var array |
| 21 | */ |
| 22 | protected $components = []; |
| 23 | |
| 24 | /** |
| 25 | * Get the stats file path |
| 26 | * |
| 27 | * @param string $stats_file The stats file path. |
| 28 | */ |
| 29 | public function __construct( $stats_file ) { |
| 30 | $this->stats_file = $stats_file; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Bootstrap the preload. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function bootstrap() { |
| 39 | // add preload tags to head, footer as fallback. |
| 40 | add_action( 'wp_head', [ $this, 'renderComponents' ] ); |
| 41 | add_action( 'wp_footer', [ $this, 'renderComponents' ] ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the bundle stats file. |
| 46 | * |
| 47 | * @return object|false; |
| 48 | */ |
| 49 | protected function getStatsFile() { |
| 50 | if ( ! file_exists( $this->stats_file ) ) { |
| 51 | return false; |
| 52 | } |
| 53 | return wp_json_file_decode( $this->stats_file, [ 'associative' => true ] ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the filenames from the stats. |
| 58 | * |
| 59 | * @param array $components An array of component names. |
| 60 | * @param string $format The format we are using. |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function getFileNames( $components, $format = 'esmBrowser' ) { |
| 65 | $json = $this->getStatsFile(); |
| 66 | $entries = $json['formats'][ $format ] ?? []; |
| 67 | |
| 68 | $set = array_map( |
| 69 | function( $element ) use ( $entries ) { |
| 70 | $files = []; |
| 71 | $collection_bundles = array_filter( |
| 72 | $entries, |
| 73 | function( $entry ) use ( $element ) { |
| 74 | return in_array( $element, $entry['components'], true ); |
| 75 | } |
| 76 | ); |
| 77 | |
| 78 | foreach ( $collection_bundles as $bundle ) { |
| 79 | $files = array_merge( [ $bundle['fileName'] ], $bundle['imports'] ); |
| 80 | } |
| 81 | |
| 82 | return $files; |
| 83 | }, |
| 84 | $components |
| 85 | ); |
| 86 | |
| 87 | return array_unique( Arrays::flatten( $set ) ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Render the components |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function renderComponents() { |
| 96 | if ( ! empty( $this->components ) ) { |
| 97 | $this->renderTag( $this->components ); |
| 98 | $this->components = []; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Render the preload tags. |
| 104 | * |
| 105 | * @param array $components An array of components. |
| 106 | * @param string $format The format. |
| 107 | * @param string $path The path to the component javascript file. |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public function renderTag( $components, $format = 'esmBrowser', $path = 'dist/components/surecart/' ) { |
| 112 | if ( empty( $components ) ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $names = $this->getFileNames( $components, $format ); |
| 117 | |
| 118 | if ( empty( $names ) ) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | foreach ( $names as $name ) { |
| 123 | echo "<link rel='preload' href='" . esc_url( trailingslashit( \SureCart::core()->assets()->getUrl() ) . trailingslashit( $path ) . $name ) . "' as='script' crossorigin />\n"; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Add preload components. |
| 129 | * |
| 130 | * @param array $components Component names. |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function add( $components ) { |
| 135 | $this->components = array_filter( array_unique( array_merge( $this->components, $components ) ) ); |
| 136 | } |
| 137 | } |
| 138 |