-
Notifications
You must be signed in to change notification settings - Fork 3
/
extending-gutenberg.php
178 lines (164 loc) · 4.91 KB
/
extending-gutenberg.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Plugin Name: The WordPress SlotFill System
* Description: Plugin of Slot Fill Examples.
* Version: 1.0.0
* Author: Ryan Welcher
* Author URI: www.ryanwelcher.com
* Plugin URI: https://github.com/ryanwelcher/the-gutenberg-slotfill-system
* Textdomain: gutenberg-slot-fill-system
*
* @package GutenbergSlotFillSystem
*/
namespace GutenbergSlotFillSystem;
/**
* Enqueue the SlotFill Examples.
*/
\add_action(
'enqueue_block_editor_assets',
function() {
$current_page = get_current_screen();
// Enqueue the slots for the edit post screen.
$edit_post_examples_asset_path = plugin_dir_path( __FILE__ ) . 'build/edit-post.asset.php';
if (
file_exists( $edit_post_examples_asset_path )
&& 'post' === $current_page->base
) {
$edit_post_examples_assets = require_once $edit_post_examples_asset_path;
\wp_enqueue_script(
'gutenberg-slot-fill-system-edit-post-examples', // Handle.
plugin_dir_url( __FILE__ ) . 'build/edit-post.js',
$edit_post_examples_assets['dependencies'],
$edit_post_examples_assets['version'],
true
);
}
$portal_example_asset_path = plugin_dir_path( __FILE__ ) . 'build/portal.asset.php';
if (
file_exists( $portal_example_asset_path )
&& 'post' === $current_page->base
) {
$edit_post_examples_assets = require_once $portal_example_asset_path;
\wp_enqueue_script(
'gutenberg-slot-fill-system-portal', // Handle.
plugin_dir_url( __FILE__ ) . 'build/portal.js',
$edit_post_examples_assets['dependencies'],
$edit_post_examples_assets['version'],
true
);
}
// Enqueue the examples for the common slots
$common_examples_asset_path = plugin_dir_path( __FILE__ ) . 'build/common.asset.php';
if ( file_exists( $common_examples_asset_path ) ) {
$common_examples_assets = require_once $common_examples_asset_path;
\wp_enqueue_script(
'gutenberg-slot-fill-system-common-examples', // Handle.
plugin_dir_url( __FILE__ ) . 'build/common.js',
$common_examples_assets['dependencies'],
$common_examples_assets['version'],
true
);
}
// Enqueue the unified slots for WordPress 6.6.
$conditional_examples_asset_path = plugin_dir_path( __FILE__ ) . 'build/conditional.asset.php';
if ( file_exists( $conditional_examples_asset_path ) ) {
$conditional_examples_assets = require_once $conditional_examples_asset_path;
\wp_enqueue_script(
'gutenberg-slot-fill-system-conditional-examples', // Handle.
plugin_dir_url( __FILE__ ) . 'build/conditional.js',
$conditional_examples_assets['dependencies'],
$conditional_examples_assets['version'],
true
);
}
}
);
/**
* Create a new Dashboard Widget.
*/
\add_action(
'wp_dashboard_setup',
function() {
\wp_add_dashboard_widget(
'extending_gutenberg_dashboard_widget',
'Custom SlotFill System',
function() {
echo '<div id="extending-gutenberg-dashboard"></div>';
}
);
}
);
/**
* Enqueue our JS on the dashboard page.
*
* @param string $hook The hook associated with the screen.
*/
\add_action(
'admin_enqueue_scripts',
function( $hook ) {
if ( 'index.php' === $hook ) {
$dashboard_asset_path = plugin_dir_path( __FILE__ ) . 'build/dashboard-widget.asset.php';
if ( file_exists( $dashboard_asset_path ) ) {
$dashboard_assets = require_once $dashboard_asset_path;
wp_enqueue_script(
'gutenberg-slot-fill-system-dashboard-widget',
plugin_dir_url( __FILE__ ) . '/build/dashboard-widget.js',
$dashboard_assets['dependencies'],
$dashboard_assets['version'],
true
);
}
}
// Localize some data we need for the script.
$user = \wp_get_current_user();
\wp_localize_script(
'gutenberg-slot-fill-system-dashboard-widget',
'EB_DASH',
array(
'user' => array( 'display_name' => $user->display_name ),
'pluginAssetPath' => plugin_dir_url( __FILE__ ),
)
);
}
);
// Register a custom page for our custom SlotFill examples.
\add_action(
'admin_menu',
function() {
add_menu_page(
__( 'Custom Slot Examples', 'gutenberg-slot-fill-system' ),
__( 'Custom Slot Examples', 'gutenberg-slot-fill-system' ),
'manage_options',
'gutenberg-slot-fill-system',
function() {
?>
<div id="gutenberg-slot-fill-system-custom-slots">
<?php esc_html_e( 'Requires JavaScript', 'gutenberg-slot-fill-system' ); ?>
</div>
<?php
}
);
}
);
/**
* Enqueue our script on the settings page,
*/
add_action(
'admin_enqueue_scripts',
function( $suffix ) {
$asset_file_page = plugin_dir_path( __FILE__ ) . 'build/custom-slots.asset.php';
if ( file_exists( $asset_file_page ) && 'toplevel_page_gutenberg-slot-fill-system' === $suffix ) {
$assets = require_once $asset_file_page;
wp_enqueue_script(
'gutenberg-slot-fill-system-custom-slots',
plugin_dir_url( __FILE__ ) . 'build/custom-slots.js',
$assets['dependencies'],
$assets['version'],
true
);
foreach ( $assets['dependencies'] as $style ) {
wp_enqueue_style( $style );
}
}
}
);