Location
1 month ago
Script
1 month ago
Assets.php
1 month ago
Enqueueable.php
1 month ago
Enqueueables.php
1 month ago
Location.php
1 month ago
Script.php
1 month ago
Style.php
1 month ago
Script.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Asset; |
| 4 | |
| 5 | use AC\Asset\Script\Inline\Position; |
| 6 | |
| 7 | class Script extends Enqueueable |
| 8 | { |
| 9 | |
| 10 | protected bool $in_footer; |
| 11 | |
| 12 | protected array $templates = []; |
| 13 | |
| 14 | public function __construct( |
| 15 | string $handle, |
| 16 | ?Location $location = null, |
| 17 | array $dependencies = [], |
| 18 | bool $in_footer = false |
| 19 | ) { |
| 20 | parent::__construct($handle, $location, $dependencies); |
| 21 | |
| 22 | $this->in_footer = $in_footer; |
| 23 | } |
| 24 | |
| 25 | protected function is_registered(): bool |
| 26 | { |
| 27 | return wp_script_is($this->get_handle(), 'registered'); |
| 28 | } |
| 29 | |
| 30 | public function is_in_footer(): bool |
| 31 | { |
| 32 | return $this->in_footer; |
| 33 | } |
| 34 | |
| 35 | public function register(): void |
| 36 | { |
| 37 | if ( ! $this->location instanceof Location) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | $version = $this->get_version(); |
| 42 | |
| 43 | wp_register_script( |
| 44 | $this->get_handle(), |
| 45 | $this->location->get_url(), |
| 46 | $this->dependencies, |
| 47 | $version !== null |
| 48 | ? (string)$version |
| 49 | : null, |
| 50 | $this->is_in_footer() |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | public function enqueue(): void |
| 55 | { |
| 56 | if (wp_script_is($this->get_handle())) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if ( ! $this->is_registered()) { |
| 61 | $this->register(); |
| 62 | } |
| 63 | |
| 64 | wp_enqueue_script($this->get_handle()); |
| 65 | |
| 66 | if ($this->templates) { |
| 67 | // Allows JS frameworks to use PHP templates |
| 68 | $this->add_inline( |
| 69 | sprintf( |
| 70 | 'window.addEventListener("DOMContentLoaded",function(){document.body.insertAdjacentHTML("beforeend", %s);});', |
| 71 | json_encode(implode('', $this->templates)) |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public function localize(string $name, Script\Localize\Translation $translation): self |
| 78 | { |
| 79 | if ( ! $this->is_registered()) { |
| 80 | $this->register(); |
| 81 | } |
| 82 | |
| 83 | wp_localize_script($this->handle, $name, $translation->get_translation()); |
| 84 | |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | public function add_inline(string $data, ?Position $position = null): self |
| 89 | { |
| 90 | if ( ! $this->is_registered()) { |
| 91 | $this->register(); |
| 92 | } |
| 93 | |
| 94 | wp_add_inline_script($this->handle, $data, (string)($position ?? Position::after())); |
| 95 | |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | public function add_inline_variable(string $name, $data): self |
| 100 | { |
| 101 | return $this->add_inline( |
| 102 | sprintf('var %s = %s;', $name, json_encode($data)), |
| 103 | Position::before() |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | public function add_template(string $id, string $html): self |
| 108 | { |
| 109 | $this->templates[] = sprintf('<template id="%s">%s</template>', esc_attr($id), $html); |
| 110 | |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | } |