-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsge-mapbox-block.php
83 lines (71 loc) · 2.11 KB
/
vsge-mapbox-block.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Plugin Name: vsge-mapbox-block
* Version: 1.1.4
* Description: VSGE - mapbox block
* Author: codekraft
* Text Domain: vsge-mapbox-block
* Domain Path: /languages
*/
define('VSGE_MB_PLUGIN_DIR', __DIR__);
define('VSGE_MB_PLUGIN_URL', \plugin_dir_url(__FILE__));
/**
* Loads the text domain for the vsge-mapbox-block plugin.
*/
function vsge_mapbox_block_i18n() {
\load_plugin_textdomain( 'vsge-mapbox-block', false, VSGE_MB_PLUGIN_DIR . '/languages' );
}
\add_action( 'init', 'vsge_mapbox_block_i18n' );
/**
* Get the MapBox token from the already defined constants and return it.
*
* @return string
*/
function vsge_get_token(): string
{
if (defined('MAPBOX_TOKEN')) {
return \apply_filters('vsge_mapbox_block_key', MAPBOX_TOKEN);
}
return 'no token provided';
}
add_action('init', function () {
register_block_type(VSGE_MB_PLUGIN_DIR . '/build', [
"script" => "vsge-mapbox-vendor",
"viewScript" => "vsge-mapbox-frontend",
"editorScript" => "vsge-mapbox-editor",
]);
});
add_action('enqueue_block_assets', function () {
$fe_assets = include VSGE_MB_PLUGIN_DIR . '/build/frontend.asset.php';
$editor_assets = include VSGE_MB_PLUGIN_DIR . '/build/editor.asset.php';
$vendor_assets = include VSGE_MB_PLUGIN_DIR . '/build/vendor.asset.php';
wp_register_script(
'vsge-mapbox-vendor',
VSGE_MB_PLUGIN_URL . 'build/vendor.js',
$vendor_assets['dependencies'],
$vendor_assets['version'],
true
);
wp_localize_script('vsge-mapbox-vendor',
'mapboxBlockData',
array(
'siteurl' => get_option('siteurl'),
'accessToken' => vsge_get_token(),
'language' => get_locale()
)
);
wp_register_script(
'vsge-mapbox-frontend',
VSGE_MB_PLUGIN_URL . 'build/frontend.js',
$fe_assets['dependencies'],
$fe_assets['version']
);
wp_set_script_translations( 'vsge-mapbox-frontend', 'vsge-mapbox-block', VSGE_MB_PLUGIN_DIR . '/languages' );
wp_register_script(
'vsge-mapbox-editor',
VSGE_MB_PLUGIN_URL . 'build/editor.js',
$editor_assets['dependencies'],
$editor_assets['version']
);
wp_set_script_translations( 'vsge-mapbox-editor', 'vsge-mapbox-block', VSGE_MB_PLUGIN_DIR . '/languages' );
});