|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2022 Jan T. Sott |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to |
| 8 | + * deal in the Software without restriction, including without limitation the |
| 9 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | + * DEALINGS IN THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Idleberg\WordpressViteAssets; |
| 26 | + |
| 27 | +use Idleberg\ViteManifest\ViteManifest; |
| 28 | + |
| 29 | +class WordpressViteAssets |
| 30 | +{ |
| 31 | + private $vm; |
| 32 | + |
| 33 | + public function __construct(string $manifestFile) |
| 34 | + { |
| 35 | + $this->vm = new ViteManifest($manifestFile); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Writes tags for entries specified in the manifest to the page header |
| 40 | + * |
| 41 | + * @param array|string $entry |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function addAction(array|string $entries, int $priority = 0): string |
| 45 | + { |
| 46 | + if (!function_exists('add_action')) { |
| 47 | + throw new \Exception("WordPress function add_action() not found"); |
| 48 | + } |
| 49 | + |
| 50 | + $entries = is_array($entries) ? $entries : [$entries]; |
| 51 | + |
| 52 | + add_action('wp_head', function () { |
| 53 | + foreach($entries as $entry) { |
| 54 | + $scriptTag = $this->getScriptTag($entry); |
| 55 | + |
| 56 | + if ($scriptTag) { |
| 57 | + echo $scriptTag . PHP_EOL; |
| 58 | + } |
| 59 | + |
| 60 | + foreach($this->getPreloadTags($entry) as $preloadTag) { |
| 61 | + echo $preloadTag . PHP_EOL; |
| 62 | + } |
| 63 | + |
| 64 | + foreach($this->getStyleTags($entry) as $styleTag) { |
| 65 | + echo $styleTag . PHP_EOL; |
| 66 | + } |
| 67 | + } |
| 68 | + }, $priority); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the script tag for an entry in the manifest |
| 73 | + * |
| 74 | + * @param string $entry |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + public function getScriptTag(string $entry): string |
| 78 | + { |
| 79 | + $url = $this->vm->getEntrypoint($entry); |
| 80 | + |
| 81 | + if (!$url) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + return "<script type=\"module\" src=\"{$url['src']}\" crossorigin integrity=\"{$url['integrity']}\"></script>"; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the style tags for an entry in the manifest |
| 90 | + * |
| 91 | + * @param string $entry |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function getStyleTags(string $entry): array |
| 95 | + { |
| 96 | + return array_map(function ($url) { |
| 97 | + return "<link rel=\"stylesheet\" href=\"{$url['href']}\" crossorigin integrity=\"{$url['integrity']}\" />"; |
| 98 | + }, $this->vm->getStyles($entry)); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the preload tags for an entry in the manifest |
| 103 | + * |
| 104 | + * @param string $entry |
| 105 | + * @return array |
| 106 | + */ |
| 107 | + public function getPreloadTags(string $entry): array |
| 108 | + { |
| 109 | + return array_map(function($import) { |
| 110 | + return "<link rel=\"modulepreload\" href=\"{$import['href']}\">"; |
| 111 | + }, $this->vm->getImports($entry)); |
| 112 | + } |
| 113 | +} |
0 commit comments