-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwheel-meta-boxes.php
More file actions
99 lines (93 loc) · 2.35 KB
/
Copy pathwheel-meta-boxes.php
File metadata and controls
99 lines (93 loc) · 2.35 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
93
94
95
96
97
98
99
<?php
/**
* Plugin Name: Wheel meta boxes
*
* @package s8-wheel-meta-boxes
*/
namespace s8\WP\WheelMetaBoxes;
const version = '0.1';
/**
* Enqueue helper to always cache-bust in development (WP_DEBUG).
* @return array{string, string}
*/
function get_asset_src_and_version( string $path ): array {
$version = version;
if ( WP_DEBUG ) {
$mtime = filemtime( plugin_dir_path( __FILE__ ) . $path );
if ( $mtime !== false ) $version = "$mtime";
}
return [
plugins_url( $path, __FILE__ ),
$version
];
}
// Adds an SVG with image load event to dispatch a custom event letting the UI code know
// that the canvas is available and whether it’s iframed.
add_filter( 'block_editor_settings_all', __NAMESPACE__ . '\add_canvas_informant', 10 );
function add_canvas_informant( $settings ) {
if ( ! isset( $settings['styles'] ) ) {
$settings['styles'] = [];
}
$markup = <<<HTML
<svg>
<image
onload="
const sameWindow = window.parent === window;
(sameWindow ? window : window.parent).dispatchEvent(
new CustomEvent('editorcanvascomplete', { detail: ! sameWindow })
);
"
href="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
/>
</svg>
HTML;
$settings['styles'][] = [
'assets' => $markup,
// The 'svgs' type is new in WP 6.3.
'__unstableType' => 'svgs',
// These styles aren’t generated by global styles, so this must be false or it’s
// stripped out in gutenberg_get_block_editor_settings.
'isGlobalStyles' => false,
];
return $settings;
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\assets' );
function assets() {
// Bails unless on 'post' screen (avoiding site editor).
if ('post' !== get_current_screen()->base) return;
[ $src, $version ] = get_asset_src_and_version( 'wheel-meta-boxes.js' );
wp_enqueue_script(
's8-wheel-meta-boxes',
$src,
[
'wp-data',
'wp-editor',
'wp-edit-post',
'wp-preferences',
],
$version,
true,
);
[ $src, $version ] = get_asset_src_and_version( 'sidebar.js' );
wp_enqueue_script(
's8-wheel-meta-boxes-sidebar',
$src,
[
'wp-components',
'wp-compose',
'wp-data',
'wp-editor',
'wp-plugins',
'wp-preferences',
],
$version,
true
);
[ $src, $version ] = get_asset_src_and_version( 'sidebar.css' );
wp_enqueue_style(
's8-wheel-meta-boxes-sidebar',
$src,
[],
$version
);
}