| 1 |
<?php |
| 2 |
namespace WPMapBlock; |
| 3 |
|
| 4 |
class Assets |
| 5 |
{ |
| 6 |
public static function init() |
| 7 |
{ |
| 8 |
$self = new self(); |
| 9 |
add_action('init', [$self, 'register_block_assets']); |
| 10 |
} |
| 11 |
|
| 12 |
public function register_block_assets() |
| 13 |
{ |
| 14 |
// Register block script for frontend. |
| 15 |
$frontend_dependencies = include_once WPMAPBLOCK_ASSETS_DIR_PATH . 'dist/wpmapblock-frontend.core.min.asset.php'; |
| 16 |
wp_register_style( |
| 17 |
'wp-map-block-stylesheets', |
| 18 |
WPMAPBLOCK_ASSETS_URI . 'dist/wpmapblock-frontend.core.min.css', |
| 19 |
is_admin() ? array('wp-editor') : null, |
| 20 |
$frontend_dependencies['version'] |
| 21 |
); |
| 22 |
wp_register_script( |
| 23 |
'wp-map-block-frontend-js', // Handle. |
| 24 |
WPMAPBLOCK_ASSETS_URI . 'dist/wpmapblock-frontend.core.min.js', |
| 25 |
$frontend_dependencies['dependencies'], |
| 26 |
$frontend_dependencies['version'], |
| 27 |
true |
| 28 |
); |
| 29 |
|
| 30 |
// Register block editor styles for backend. |
| 31 |
$backend_dependencies = include_once WPMAPBLOCK_ASSETS_DIR_PATH . 'dist/wpmapblock.core.min.asset.php'; |
| 32 |
wp_register_style( |
| 33 |
'wp-map-block-editor-css', |
| 34 |
WPMAPBLOCK_ASSETS_URI . 'dist/wpmapblock.core.min.css', |
| 35 |
array('wp-edit-blocks'), |
| 36 |
$backend_dependencies['version'] |
| 37 |
); |
| 38 |
|
| 39 |
// Register block editor script for backend. |
| 40 |
wp_register_script( |
| 41 |
'wp-map-block-js', // Handle. |
| 42 |
WPMAPBLOCK_ASSETS_URI . 'dist/wpmapblock.core.min.js', |
| 43 |
$backend_dependencies['dependencies'], |
| 44 |
$backend_dependencies['version'], |
| 45 |
true |
| 46 |
); |
| 47 |
|
| 48 |
// WP Localized globals. Use dynamic PHP stuff in JavaScript via `wpmapblockGlobal` object. |
| 49 |
wp_localize_script( |
| 50 |
'wp-map-block-js', |
| 51 |
'wpmapblockGlobal', // Array containing dynamic data for a JS Global. |
| 52 |
[ |
| 53 |
'pluginDirPath' => plugin_dir_path(__DIR__), |
| 54 |
'pluginDirUrl' => plugin_dir_url(__DIR__), |
| 55 |
// Add more data here that you want to access from `wpmapblockGlobal` object. |
| 56 |
] |
| 57 |
); |
| 58 |
wp_set_script_translations( 'wp-map-block-js', 'wp-map-block', WPMAPBLOCK_ROOT_DIR_PATH . 'languages/' ); |
| 59 |
} |
| 60 |
} |
| 61 |
|