/
plugin.php
68 lines (58 loc) · 2.03 KB
/
plugin.php
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
<?php
namespace Automattic\Chatrix;
use function Automattic\Chatrix\Block\register as register_block;
use function Automattic\Chatrix\Popup\register as register_popup;
use function Automattic\Chatrix\Sessions\init_logout;
const SCRIPT_HANDLE_CONFIG = 'chatrix-config';
const SCRIPT_HANDLE_APP = 'chatrix-app';
const SCRIPT_HANDLE_LOGOUT = 'chatrix-logout';
const CONFIG_VARIABLE = 'ChatrixConfig';
const LOGOUT_COOKIE_NAME = 'chatrix-logout';
function main() {
init_logout();
register_scripts();
register_block();
register_popup();
}
function register_scripts() {
add_action(
'init',
function () {
$json_data = wp_json_encode(
array(
'rootUrl' => root_url() . '/iframe/',
)
);
// Enqueue script for global configuration.
wp_register_script( SCRIPT_HANDLE_CONFIG, '', array(), automattic_chatrix_version(), true );
wp_enqueue_script( SCRIPT_HANDLE_CONFIG );
wp_add_inline_script( SCRIPT_HANDLE_CONFIG, 'window.' . CONFIG_VARIABLE . " = $json_data;" );
// Note we don't enqueue the SCRIPT_HANDLE_APP script yet. It will be enqueued whenever SCRIPT_HANDLE_APP
// is specified as a dependency of another script.
wp_register_script(
SCRIPT_HANDLE_APP,
root_url() . '/app.iife.js',
array( 'wp-element' ),
automattic_chatrix_version(),
true
);
// filter 'chatrix_load_logout_script' exists to limit when logout script is loaded.
// possible optimisation example:
// add_filter( 'chatrix_load_logout_script', function() { return isset( $_COOKIE[ \Automattic\Chatrix\LOGOUT_COOKIE_NAME ] ); } );
// to limit it when the logout cookie is set but can only do so when no page caching is involved for the page.
if ( ! is_user_logged_in() && apply_filters( 'chatrix_load_logout_script', true ) ) {
wp_register_script(
SCRIPT_HANDLE_LOGOUT,
root_url() . '/logout.iife.js',
array(),
automattic_chatrix_version(),
false
);
wp_enqueue_script( SCRIPT_HANDLE_LOGOUT );
}
}
);
}
function root_url(): string {
return plugins_url( 'build', __DIR__ );
}