-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwordpress-plugin.php
97 lines (87 loc) · 2.34 KB
/
wordpress-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
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
<?php
/**
* Plugin Name: PLUGIN_NAME
*/
/**
* Include the autoloader
*/
add_action('plugins_loaded', function () {
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
include __DIR__ . '/vendor/autoload.php';
}
});
/**
* Initialize plugin
*/
add_action('init', 'wordpress_plugin', 1);
/**
* Static accessor for plugin container
*
* @return array
*/
function wordpress_plugin()
{
static $container;
if (!$container) {
$container = [];
/**
* Action runs when PLUGIN_NAME is initialized
*
* @param array $container Plugin container
*/
do_action('wordpress_plugin', $container);
}
return $container;
}
/** Init admin UI after plugin loads */
add_action('wordpress_plugin', function () {
//Register assets
add_action('init', function () {
wordpress_plugin_register_asset('wordpress-plugin-admin');
});
//Register block
wordpress_plugin_register_asset('wordpress-plugin-blocks');
register_block_type('wordpress-plugin/block', array(
'editor_script' => 'wordpress-plugin-blocks',
));
//Enqueue admin assets on admin page only
add_action('admin_enqueue_scripts', function ($hook) {
if ('toplevel_page_custompage' != $hook) {
return;
}
wp_enqueue_script('wordpress-plugin-admin');
});
//Register menu page
add_action('admin_menu', function () {
add_menu_page(
__('PLUGIN_NAME', 'wordpress-plugin'),
__('PLUGIN_NAME', 'wordpress-plugin'),
'manage_options',
'wordpress-plugin-admin',
function () {
wp_enqueue_script('wordpress-plugin-admin');
echo '<div id="wordpress-plugin-admin"></div>';
}
);
});
});
/**
* Register an asset
**
* @since 0.0.1
* @param string $handle
*/
function wordpress_plugin_register_asset($handle)
{
$_handle = str_replace('wordpress-plugin-', '', $handle);
if (file_exists(__DIR__ . "/build/$_handle.asset.php")) {
// automatically load dependencies and version
$assets = include __DIR__ . "/build/$_handle.asset.php";
wp_register_script(
$handle,
plugins_url("/build/$_handle.js", __FILE__),
$assets['dependencies'],
$assets['version']
);
}
}