PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.5
YayMail – WooCommerce Email Customizer v4.4.5
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.5, at src/Utils/YayMailViteApp.php

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