PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.1
YayMail – WooCommerce Email Customizer v4.4.1
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / src / Utils / YayMailViteApp.php

YayMailViteApp.php in YayMail – WooCommerce Email Customizer 4.4.1, at src/Utils/YayMailViteApp.php

178 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\Utils;
4
5 use YayMail\Utils\SingletonTrait;
6
7 /**
8 * YayMail Vite App enqueue logic
9 */
10 class YayMailViteApp {
11 use SingletonTrait;
12
13 private $entries = [];
14 private $manifest = [];
15 private $preload_module_files = [];
16 private $style_files = [];
17
18 protected function __construct() {
19 add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ], 99 );
20 add_action( 'admin_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ], 99 );
21 }
22
23 private function get_plugin_url() {
24 if ( Helpers::is_yaymail_lite_and_yay_wp_pro_active() ) {
25 return YAYMAIL_WP_PLUGIN_URL;
26 }
27 return YAYMAIL_PLUGIN_URL;
28 }
29
30 public function enqueue_entry( string $key = 'yaymail-main.tsx', array $deps = [] ) {
31 if ( isset( $this->entries[ $key ] ) ) {
32 return;
33 }
34 $this->entries[ $key ] = [
35 'key' => $key,
36 'deps' => $deps,
37 ];
38 }
39
40 public function wp_enqueue_scripts() {
41 if ( empty( $this->entries ) ) {
42 return;
43 }
44
45 if ( ! YAYMAIL_IS_DEVELOPMENT ) {
46 $this->load_manifest();
47 foreach ( $this->entries as $entry_opts ) {
48 $this->load_entry( $entry_opts['key'] );
49 }
50 }
51
52 $this->register_entry_modules();
53
54 add_action( 'admin_head', [ $this, 'register_preload_modules' ] );
55 add_action( 'wp_head', [ $this, 'register_preload_modules' ] );
56
57 $this->register_styles();
58 }
59
60 private function load_entry( $entry_key ) {
61 $entry_options = $this->get_module_opts( $entry_key );
62 if ( null === $entry_options ) {
63 return;
64 }
65
66 if ( isset( $entry_options['css'] ) ) {
67 $this->load_styles( $entry_options['css'] );
68 }
69
70 if ( isset( $entry_options['imports'] ) ) {
71 foreach ( $entry_options['imports'] as $import_key ) {
72 $import_module_opt = $this->get_module_opts( $import_key );
73
74 if ( ! in_array( $import_module_opt['file'], $this->preload_module_files, true ) ) {
75 $this->preload_module_files[] = $import_module_opt['file'];
76 }
77
78 if ( isset( $import_module_opt['css'] ) ) {
79 $this->load_styles( $import_module_opt['css'] );
80 }
81 }
82 }
83 }
84
85 private function register_entry_modules() {
86 add_filter(
87 'script_loader_tag',
88 function ( $tag, $handle, $src ) {
89 if ( strpos( $handle, 'module/yaymail/' ) !== false ) {
90 $str = "type='module'";
91 $str .= YAYMAIL_IS_DEVELOPMENT ? ' crossorigin' : '';
92 $tag = '<script ' . $str . ' src="' . $src . '" id="' . $handle . '-js"></script>';
93 }
94 return $tag;
95 },
96 10,
97 3
98 );
99
100 foreach ( $this->entries as $entry_key => $entry_opts ) {
101 if ( YAYMAIL_IS_DEVELOPMENT ) {
102 wp_register_script( "module/yaymail/$entry_key", "http://localhost:3000/$entry_key", $entry_opts['deps'], null, true );// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
103
104 } else {
105 $url = $this->get_plugin_url() . 'assets/dist/yaymail/' . $this->get_module_opts( $entry_key )['file'];
106 wp_register_script( "module/yaymail/$entry_key", $url, $entry_opts['deps'], time(), true );
107 }
108
109 wp_enqueue_script( "module/yaymail/$entry_key" );
110 }
111
112 do_action( 'yaymail_after_enqueue_scripts', $this->entries );
113 }
114
115 public function register_preload_modules() {
116 if ( YAYMAIL_IS_DEVELOPMENT ) {
117 echo '<script type="module">
118 import RefreshRuntime from "http://localhost:3000/@react-refresh"
119 RefreshRuntime.injectIntoGlobalHook(window)
120 window.$RefreshReg$ = () => {}
121 window.$RefreshSig$ = () => (type) => type
122 window.__vite_plugin_react_preamble_installed__ = true
123 </script>';
124
125 } else {
126 foreach ( $this->preload_module_files as $file ) {
127 echo ( '<link rel="modulepreload" href="' . esc_attr( $this->get_plugin_url() . 'assets/dist/yaymail/' . $file ) . '">' );
128 }
129 }
130 }
131
132 private function register_styles() {
133 if ( YAYMAIL_IS_DEVELOPMENT ) {
134 return;
135 }
136
137 foreach ( $this->style_files as $file ) {
138 wp_register_style( 'yay/' . $file, $this->get_plugin_url() . 'assets/dist/yaymail/' . $file, [], yaymail_version() );
139 wp_enqueue_style( 'yay/' . $file );
140 }
141 }
142
143 private function load_manifest() {
144 global $wp_filesystem;
145 require_once ABSPATH . '/wp-admin/includes/file.php';
146 WP_Filesystem();
147
148 if ( Helpers::is_yaymail_lite_and_yay_wp_pro_active() ) {
149 $plugin_path = YAYMAIL_WP_PLUGIN_PATH;
150 } else {
151 $plugin_path = YAYMAIL_PLUGIN_PATH;
152 }
153
154 $content = $wp_filesystem->get_contents( $plugin_path . 'assets/dist/yaymail/manifest.json' );
155
156 // when $wp_filesystem is not available, use file_get_contents
157 if ( ! $content ) {
158 $content = file_get_contents( $plugin_path . 'assets/dist/yaymail/manifest.json' );
159 }
160
161 $this->manifest = json_decode( $content, true );
162 }
163
164 private function get_module_opts( $key ) {
165 return $this->manifest[ $key ] ?? null;
166 }
167
168 private function load_styles( array $style_files ) {
169 if ( empty( $style_files ) ) {
170 return;
171 }
172 foreach ( $style_files as $file ) {
173 if ( ! in_array( $file, $this->style_files, true ) ) {
174 $this->style_files[] = $file;
175 }
176 }
177 }
178 }