-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathembed-react-build.php
More file actions
executable file
·176 lines (148 loc) · 4.47 KB
/
embed-react-build.php
File metadata and controls
executable file
·176 lines (148 loc) · 4.47 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Plugin Name: Embed React Build
* Plugin URI: https://github.com/nurullah/embed-react-build
* Description: This plugin allows the ReactJS build to work embedded in wordpress.
* Version: 1.0.3
* Author: Nurullah Sevinctekin
* Author URI: https://github.com/nurullah/
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
// do not allow direct access to the file.
if (! defined( 'ABSPATH' )) exit;
class EmbedReactBuild {
public $name;
public $version;
function __construct() {
$this->name = $this->plugin_data( 'name' );
$this->version = $this->plugin_data( 'version' );
add_shortcode( 'embed_react_build', array( $this, 'shortcode_callback' ) );
}
/**
* Shortcode callback.
*
* @param array $attr Shortcode attributes
* @return void
*/
public function shortcode_callback( $attr ) {
extract( shortcode_atts( array(
'application_id' => 'root',
'url' => '',
'staging_url' => ''
), $attr ) );
// Production URL
$url = rtrim( $url, '/' );
// Staging URL
if ( isset($_GET['test']) && isset($staging_url) ) {
$url = rtrim( $staging_url, '/' );
}
// Manifest URL
$manifest_url = $url . '/manifest.json';
// validate the url.
if (! wp_http_validate_url( $url )) {
return 'The Build URL is not validated.';
}
// get dependencies.
$dependencies = $this->get_entrypoints( $url );
if (! $dependencies['success']) {
return $dependencies['message'];
}
// add dependencies to the page.
foreach( $dependencies['data'] as $key => $values ) {
foreach( $values as $dependency ) {
$path = array_keys( $dependency )[0];
$path_array = explode( '/', $path );
$file = end( $path_array );
$filename = implode( array_slice( explode( '.', $file), 0, -1 ) );
// enqueue variables
$handle = "react-$application_id-$filename";
$src = array_values( $dependency )[0];
// add dependencies to the page by type
switch ( $key ) {
case 'css':
wp_enqueue_style( $handle, $src );
break;
case 'js':
wp_enqueue_script( $handle, $src );
break;
default:
break;
}
}
}
// load manifest.json after runtime.
wp_add_inline_script(
"react-$application_id-runtime-main",
<<<EOL
var manifest=document.createElement("link");
manifest.rel="manifest";
manifest.href="$manifest_url"
document.head.append(manifest);
EOL
);
return "<div id=\"$application_id\"></div>";
}
/**
* Retrieves the dependencies needed for the React application to stand up.
*
* @param string $url The URL of React build.
* @return array
*/
public function get_entrypoints( $url ) {
// validate the assets url.
$assets_url = $url . '/asset-manifest.json';
if (! wp_http_validate_url( $assets_url )) {
return array(
'success' => false,
'message' => 'The Build URL containing the `asset-manifest.json` is not validated.'
);
}
// get build assets.
$response = wp_remote_get( $assets_url );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response) !== 200 ) {
return array(
'success' => false,
'message' => 'File `asset-manifest.json` not found at Build URL.'
);
}
// convert response body from json to object.
$assets = json_decode( $response['body'], true );
// prepare the entrypoints.
$entrypoints = [];
foreach( $assets['entrypoints'] as $path ) {
$path_array = explode( '/', $path );
$file = explode( '.', end( $path_array ) );
// file info
$filename = array_slice( $file, 0, -1 );
$extension = end( $file );
$file_url = array_filter( $assets['files'], function($value) use ($path) {
if (preg_match( "#/.*\/($path)$#", $value ))
return true;
}, );
// return the dependency.
$entrypoints[ $extension ][] = $file_url;
}
return array(
'success' => true,
'data' => $entrypoints
);
}
/**
* Retrieves the information in the plugin header.
*
* @param string $key
* @return array
*/
public function plugin_data( $key ) {
return get_file_data(
__FILE__,
array(
'name' => 'Plugin Name',
'version' => 'Version'
),
'plugin'
)[ $key ];
}
}
new EmbedReactBuild();