PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.3.4
FileBird – WordPress Media Library Folders & File Manager v6.3.4
6.5.8 6.5.7 6.5.5 6.5.4 trunk 4.3.1 5.1.6 5.3 5.3.2 5.4.3 5.4.4 5.4.5 5.5 5.5.3 5.5.5 5.5.8 5.5.8.1 5.6 5.6.1 5.6.2 5.6.3 5.6.4 6.2.3 6.2.5 6.3 All 36 releases
filebird / includes / Utils / Vite.php

Vite.php in FileBird – WordPress Media Library Folders & File Manager 6.3.4, at includes/Utils/Vite.php

157 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FileBird\Utils;
4
5 class Vite {
6 const HOST = 'https://localhost:3000/';
7 const SCRIPT_HANDLE = 'module/filebird/vite';
8 const CLIENT_SCRIPT_HANDLE = 'module/filebird/vite-client';
9
10 public static function base_path() {
11 return NJFB_PLUGIN_URL . 'assets/dist/';
12 }
13
14 /**
15 * Enqueues the Vite script and its dependencies.
16 *
17 * @param string $script The name of the script to enqueue.
18 * @return void
19 */
20 public static function enqueue_vite( string $script = 'main.tsx' ) {
21 self::enqueue_preload( $script );
22 // self::css_tag( $script );
23 $script_handle = self::register( $script );
24 add_filter(
25 'script_loader_tag',
26 function ( $tag, $handle, $src ) {
27 if ( strpos( $handle, 'module/filebird/' ) !== false ) {
28 $str = "type='module'";
29 $str .= NJFB_DEVELOPMENT ? ' crossorigin' : '';
30 $tag = '<script ' . $str . ' src="' . esc_url( $src ) . '" id="' . esc_attr( $handle ) . '-js"></script>';
31 }
32 return $tag;
33 },
34 10,
35 3
36 );
37
38 add_filter(
39 'script_loader_src',
40 function( $src, $handle ) {
41 if ( strpos( $handle, self::SCRIPT_HANDLE ) !== false && strpos( $src, '?ver=' ) ) {
42 return remove_query_arg( 'ver', $src );
43 }
44
45 return $src;
46 },
47 10,
48 2
49 );
50
51 return $script_handle;
52 }
53
54 public static function enqueue_preload( $script ) {
55 add_action(
56 'admin_head',
57 function() use ( $script ) {
58 if ( NJFB_DEVELOPMENT ) {
59 echo '<script type="module">
60 import RefreshRuntime from "' . esc_url( self::HOST ) . '@react-refresh"
61 RefreshRuntime.injectIntoGlobalHook(window)
62 window.$RefreshReg$ = () => {}
63 window.$RefreshSig$ = () => (type) => type
64 window.__vite_plugin_react_preamble_installed__ = true
65 </script>';
66 } else {
67 foreach ( self::imports_urls( $script ) as $url ) {
68 echo ( '<link rel="modulepreload" href="' . esc_url( $url ) . '">' );
69 }
70 }
71 }
72 );
73 }
74
75 public static function register( $entry ) {
76 $url = NJFB_DEVELOPMENT ? self::HOST . $entry : self::asset_url( $entry );
77
78 if ( ! $url ) {
79 return '';
80 }
81
82 // wp_enqueue_script( self::CLIENT_SCRIPT_HANDLE, self::HOST . '@vite/client', array(), NJFB_VERSION, false );
83 wp_enqueue_script( "module/filebird/$entry", $url, false, true, NJFB_DEVELOPMENT ? true : false );
84
85 return "module/filebird/$entry";
86 }
87
88 private static function css_tag( string $entry ): string {
89 // not needed on dev, it's inject by Vite
90 if ( NJFB_DEVELOPMENT ) {
91 return '';
92 }
93
94 $tags = '';
95 foreach ( self::css_urls( $entry ) as $key => $url ) {
96 wp_enqueue_style( "filebird/$key", $url, array(), NJFB_VERSION );
97 }
98 return $tags;
99 }
100
101
102 // Helpers to locate files
103
104 private static function get_manifest(): array {
105 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
106 $content = file_get_contents( NJFB_PLUGIN_PATH . 'assets/dist/manifest.json' );
107
108 return json_decode( $content, true );
109 }
110
111 private static function asset_url( string $entry ): string {
112 $manifest = self::get_manifest();
113
114 return isset( $manifest[ $entry ] )
115 ? self::base_path() . $manifest[ $entry ]['file']
116 : self::base_path() . $entry;
117 }
118
119 private static function get_public_url_base() {
120 return NJFB_DEVELOPMENT ? '/dist/' : self::base_path();
121 }
122
123 private static function imports_urls( string $entry ): array {
124 $urls = array();
125 $manifest = self::get_manifest();
126
127 if ( ! empty( $manifest[ $entry ]['imports'] ) ) {
128 foreach ( $manifest[ $entry ]['imports'] as $imports ) {
129 $urls[] = self::get_public_url_base() . $manifest[ $imports ]['file'];
130 }
131 }
132 return $urls;
133 }
134
135 private static function css_urls( string $entry ): array {
136 $urls = array();
137 $manifest = self::get_manifest();
138
139 if ( ! empty( $manifest[ $entry ]['css'] ) ) {
140 foreach ( $manifest[ $entry ]['css'] as $file ) {
141 $urls[ "filebird_entry_$file" ] = self::get_public_url_base() . $file;
142 }
143 }
144
145 if ( ! empty( $manifest[ $entry ]['imports'] ) ) {
146 foreach ( $manifest[ $entry ]['imports'] as $imports ) {
147 if ( ! empty( $manifest[ $imports ]['css'] ) ) {
148 foreach ( $manifest[ $imports ]['css'] as $css ) {
149 $urls[ "filebird_imports_$css" ] = self::get_public_url_base() . $css;
150 }
151 }
152 }
153 }
154 return $urls;
155 }
156 }
157