Resources.php
2.37 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
<?php
namespace WPML\LIB\WP\App;
use WPML\FP\Fns;
use WPML\FP\Obj;
use WPML\LIB\WP\Nonce;
use WPML\LIB\WP\WordPress;
class Resources {
/**
* Enqueue a JavaScript application file from the dist directory.
*
* @param string $app
* @param string $pluginBaseUrl
* @param string $pluginBasePath
* @param string $version
* @param null|string $domain
* @param null|string[] $localize
*
* @return void
*/
public static function enqueue( $app, $pluginBaseUrl, $pluginBasePath, $version, $domain = null, $localize = null ) {
static::enqueueWithDeps( $app, $pluginBaseUrl, $pluginBasePath, $version, $domain, $localize, [] );
}
/**
* Enqueue a JavaScript application file from the dist directory, with dependencies.
*
* @param string $app
* @param string $pluginBaseUrl
* @param string $pluginBasePath
* @param string $version
* @param null|string $domain
* @param null|string[] $localize
* @param null|string[] $dependencies
*
* @return void
*/
public static function enqueueWithDeps( $app, $pluginBaseUrl, $pluginBasePath, $version, $domain = null, $localize = null, $dependencies = [] ) {
$handle = sprintf(
'wpml-%s-ui',
str_replace( '/', '_', $app )
);
wp_register_script(
$handle,
"$pluginBaseUrl/dist/js/$app/app.js",
$dependencies,
$version
);
if ( $localize ) {
if ( isset( $localize['data']['endpoint'] ) ) {
$localize['data']['nonce'] = Nonce::create( $localize['data']['endpoint'] );
}
if ( isset( $localize['data']['endpoints'] ) ) {
$localize = Obj::over(
Obj::lensPath( [ 'data', 'endpoints' ] ),
Fns::map( function ( $endpoint ) {
return [
'endpoint' => $endpoint,
'nonce' => Nonce::create( $endpoint )
];
} ),
$localize
);
}
wp_localize_script( $handle, $localize['name'], $localize['data'] );
}
wp_enqueue_script( $handle );
if ( file_exists( "$pluginBasePath/dist/css/$app/styles.css" ) ) {
wp_enqueue_style(
$handle,
"$pluginBaseUrl/dist/css/$app/styles.css",
[],
$version
);
}
if ( $domain && WordPress::versionCompare( '>=', '5.0.0' ) ) {
$rootPath = $domain === 'wpml-translation-management' && defined( 'WPML_PLUGIN_PATH' )
? WPML_PLUGIN_PATH
: $pluginBasePath;
wp_set_script_translations( $handle, $domain, "$rootPath/locale/jed" );
}
}
}