forked from greatkhanjoy/WPReact-Plugin-With-Vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-vite.php
More file actions
106 lines (90 loc) · 3.27 KB
/
wp-vite.php
File metadata and controls
106 lines (90 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Plugin Name: WP Vite And React
* Description: WP Vite And React
* Author URI: https://greatkhanjoy.me
* Plugin URI: https://greatkhanjoy.me
* Version: 1.0.0
* Author: Greatkhanjoy
* Text Domain: wp-vite-react
* Domain Path: /i18n
*/
class WPViteReact
{
function __construct()
{
// add_action('init',[$this,'initialize']);
add_action('admin_enqueue_scripts', [$this, 'loadAssets']);
add_action('wp_enqueue_scripts', [$this, 'loadAssets']);
add_action('admin_menu', [$this, 'adminMenu']);
add_filter('script_loader_tag', [$this, 'loadScriptAsModule'], 10, 3);
add_filter('script_loader_tag', [$this, 'loadScriptAsModuleTwo'], 10, 3);
add_shortcode('wp_vite_react', [$this, 'wp_vite_react_render_shortcode']);
}
// function shortocode render()
public function wp_vite_react_render_shortcode()
{
wp_enqueue_script('wp-vite-react-core');
wp_enqueue_style('wp-vite-react-script');
// wp_enqueue_style('wp-vite-react-style');
include_once(plugin_dir_path(__FILE__) . "/inc/frontend.php");
}
// function load script as module
function loadScriptAsModule($tag, $handle, $src)
{
if ('wp-vite-react-core' !== $handle) {
return $tag;
}
$tag = '<script type="module" src="' . esc_url($src) . '"></script>';
return $tag;
}
// function load script as module
function loadScriptAsModuleTwo($tag, $handle, $src)
{
if ('wp-vite-react-script' !== $handle) {
return $tag;
}
$tag = '
<script type="module" crossorigin >
import RefreshRuntime from "' . esc_url($src) . '";
RefreshRuntime.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
window.__vite_plugin_react_preamble_installed__ = true;
</script>';
return $tag;
}
// Add admin menu
function adminMenu()
{
add_menu_page('WP React', 'WP React', 'manage_options', 'admin/admin.php', [$this, 'loadAdminPage'], 'dashicons-vault', 6);
}
// Admin page render
function loadAdminPage()
{
wp_enqueue_script('wp-vite-react-core');
wp_enqueue_script('wp-vite-react-script');
// wp_enqueue_style('wp-vite-react-style');
$pluginUrl = plugin_dir_url(__FILE__);
wp_localize_script('wp-vite-react-core', 'wpvitereact', [
'url' => $pluginUrl,
'nonce' => wp_create_nonce('wp_rest'),
]);
include_once(plugin_dir_path(__FILE__) . "/inc/admin.php");
}
// Load assets for admin and frontend
function loadAssets()
{
// wp_register_script('wp-vite-react-core', plugins_url('dist/assets/index-0340b01b.js', __FILE__), [], time(), true);
// wp_register_style('wp-vite-react-style', plugins_url('dist/assets/index-f25b5597.css', __FILE__), [], time(), 'all');
wp_register_script('wp-vite-react-core', 'http://localhost:5173/src/main.jsx', ['wp-vite-react-script'], time(), true);
wp_register_script(
'wp-vite-react-script',
'http://localhost:5173/@react-refresh',
[],
null,
true
);
}
}
new WPViteReact();