-
Notifications
You must be signed in to change notification settings - Fork 13
/
Onboarding.php
443 lines (405 loc) · 13.7 KB
/
Onboarding.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* User onboarding.
*
* @package Genesis\CustomBlocks
* @copyright Copyright(c) 2020, Genesis Custom Blocks
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*/
namespace Genesis\CustomBlocks\Admin;
use Genesis\CustomBlocks\ComponentAbstract;
use Genesis\CustomBlocks\Blocks\Block;
/**
* Class Onboarding
*/
class Onboarding extends ComponentAbstract {
/**
* The transient for whether to show the welcome notice.
*
* @var string
*/
const SHOW_WELCOME_TRANSIENT = 'genesis_custom_blocks_show_welcome';
/**
* Option name.
*
* @var string
*/
const OPTION_NAME = 'genesis_custom_blocks_example_post_id';
/**
* Register any hooks that this component needs.
*/
public function register_hooks() {
add_action( 'current_screen', [ $this, 'admin_notices' ] );
}
/**
* Runs during plugin activation.
*/
public function plugin_activation() {
$this->add_dummy_data();
$this->prepare_welcome_notice();
}
/**
* Prepare onboarding notices.
*/
public function admin_notices() {
$example_post_id = get_option( self::OPTION_NAME );
if ( ! $example_post_id ) {
return;
}
$screen = get_current_screen();
$slug = genesis_custom_blocks()->get_post_type_slug();
if ( ! is_object( $screen ) ) {
return;
}
$post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
/*
* On the edit post screen, editing the Example Block.
*/
if ( $slug === $screen->id && 'post' === $screen->base && $post_id === $example_post_id ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'genesis_custom_blocks_before_fields_list', [ $this, 'show_add_to_post_notice' ] );
}
if ( 'draft' !== get_post_status( $example_post_id ) ) {
return;
}
/*
* On the plugins screen, immediately after activating Genesis Custom Blocks.
*/
if ( 'plugins' === $screen->id && 'true' === get_transient( self::SHOW_WELCOME_TRANSIENT ) ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'admin_notices', [ $this, 'show_welcome_notice' ] );
}
/*
* On the All Blocks screen, when a draft Example Block exists.
*/
if ( "edit-{$slug}" === $screen->id ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'admin_notices', [ $this, 'show_edit_block_notice' ] );
}
/*
* On the edit post screen, editing the draft Example Block.
*/
if ( $slug === $screen->id && 'post' === $screen->base && $post_id === $example_post_id ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'edit_form_advanced', [ $this, 'show_add_fields_notice' ] );
add_action( 'edit_form_before_permalink', [ $this, 'show_publish_notice' ] );
add_action(
'add_meta_boxes',
function() use ( $slug ) {
remove_meta_box( 'block_template', $slug, 'normal' );
},
20
);
}
}
/**
* Prepare the welcome notice on plugin activation.
*
* We can't hook into admin_notices at this point, so instead we set a short
* transient, and check that transient during the next page load.
*/
public function prepare_welcome_notice() {
set_transient( self::SHOW_WELCOME_TRANSIENT, 'true', 1 );
}
/**
* Enqueue scripts and styles used by the onboarding screens.
*
* @return void
*/
public function enqueue_scripts() {
wp_enqueue_style(
'genesis-custom-blocks-onboarding-css',
$this->plugin->get_url( 'css/admin.onboarding.css' ),
[],
$this->plugin->get_version()
);
}
/**
* Render the Welcome message.
*/
public function show_welcome_notice() {
$example_post_id = get_option( self::OPTION_NAME );
if ( ! $example_post_id ) {
return;
}
?>
<div class="genesis-custom-blocks-welcome genesis-custom-blocks-notice notice is-dismissible">
<h2><span role="image" aria-label="<?php esc_attr_e( 'Waving hand emoji', 'genesis-custom-blocks' ); ?>">👋</span> <?php esc_html_e( 'Hi, and welcome!', 'genesis-custom-blocks' ); ?></h2>
<p class="intro"><?php esc_html_e( 'Genesis Custom Blocks makes it easy to build your own blocks for the WordPress editor.', 'genesis-custom-blocks' ); ?></p>
<p><strong><?php esc_html_e( 'Want to see how it\'s done?', 'genesis-custom-blocks' ); ?></strong> <?php esc_html_e( 'Here\'s one we prepared earlier.', 'genesis-custom-blocks' ); ?></p>
<?php
edit_post_link(
__( 'Let\'s get started!', 'genesis-custom-blocks' ),
'<p>',
'</p>',
$example_post_id,
'button button--white button_cta'
);
?>
<p class="ps"><?php esc_html_e( 'P.S. We don\'t like to nag. This message won\'t be shown again.', 'genesis-custom-blocks' ); ?></p>
<p class="ps">
<?php
esc_html_e( 'Prefer to look at the docs?', 'genesis-custom-blocks' );
printf(
' <a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
'https://developer.wpengine.com/genesis-custom-blocks',
esc_html__( 'Start here', 'genesis-custom-blocks' )
);
?>
</p>
</div>
<?php
}
/**
* Render the Edit Your First Block message.
*/
public function show_edit_block_notice() {
$example_post_id = get_option( self::OPTION_NAME );
if ( ! $example_post_id ) {
return;
}
?>
<div class="genesis-custom-blocks-edit-block genesis-custom-blocks-notice notice">
<h2><span role="image" aria-label="<?php esc_attr_e( 'Scientist emoji', 'genesis-custom-blocks' ); ?>">👩</span> <span role="image" aria-label="<?php esc_attr_e( 'Stethescope emoji', 'genesis-custom-blocks' ); ?>">🔬</span> <?php echo esc_html_e( 'Ready to begin?', 'genesis-custom-blocks' ); ?></h2>
<p class="intro">
<?php
echo wp_kses_post(
sprintf(
// translators: Placeholders are <strong> html tags.
__( 'We created this %1$sExample Block%2$s to show you just how easy it is to get started.', 'genesis-custom-blocks' ),
'<strong>',
'</strong>'
)
);
?>
</p>
<p>
<?php
echo wp_kses_post(
sprintf(
// translators: Placeholders are <strong> and <a> html tags.
__( 'You can %1$sEdit%2$s the block to learn more, or just %3$sTrash%4$s it to dismiss this message.', 'genesis-custom-blocks' ),
'<strong>',
'</strong>',
'<a href="' . get_delete_post_link( $example_post_id ) . '" class="trash">',
'</a>'
)
);
?>
</p>
<p>
<?php
esc_html_e( 'Learn more:', 'genesis-custom-blocks' );
printf(
' <a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
'https://developer.wpengine.com/genesis-custom-blocks/get-started/',
esc_html__( 'Get Started', 'genesis-custom-blocks' )
);
?>
</p>
</div>
<?php
}
/**
* Render the Add Fields message.
*/
public function show_add_fields_notice() {
$post = get_post();
$block = new Block( $post->ID );
/*
* We add 4 fields to our Example Block in add_dummy_data().
*/
if ( count( $block->fields ) > 4 ) {
return;
}
?>
<div class="genesis-custom-blocks-add-fields genesis-custom-blocks-notice">
<h2><span role="image" aria-label="<?php esc_attr_e( 'Eyeglass emoji', 'genesis-custom-blocks' ); ?>">🧐</span> <?php esc_html_e( 'Try adding a field.', 'genesis-custom-blocks' ); ?></h2>
<p><?php esc_html_e( 'Fields let you define the options you see when adding your block to a post or page.', 'genesis-custom-blocks' ); ?></p>
<p><?php esc_html_e( 'There are lots of different field types that let you build powerfully dynamic custom blocks.', 'genesis-custom-blocks' ); ?></p>
<p>
<?php
esc_html_e( 'Learn more:', 'genesis-custom-blocks' );
printf(
' <a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
'https://developer.wpengine.com/genesis-custom-blocks/fields/',
esc_html__( 'Field Types', 'genesis-custom-blocks' )
);
?>
</p>
</div>
<?php
}
/**
* Render the Add Fields message.
*/
public function show_publish_notice() {
$block = new Block( get_the_ID() );
/**
* We add 4 fields to our Example Block in add_dummy_data().
*/
if ( count( $block->fields ) > 4 ) {
return;
}
?>
<div class="genesis-custom-blocks-publish genesis-custom-blocks-notice">
<h2><span role="image" aria-label="<?php esc_attr_e( 'Test tube emoji', 'genesis-custom-blocks' ); ?>">🧪</span> <?php esc_html_e( 'Time to experiment!', 'genesis-custom-blocks' ); ?></h2>
<ol class="intro">
<li><?php esc_html_e( 'Choose an icon', 'genesis-custom-blocks' ); ?></li>
<li><?php esc_html_e( 'Change the category', 'genesis-custom-blocks' ); ?></li>
<li><?php esc_html_e( 'Investigate a few different field types', 'genesis-custom-blocks' ); ?></li>
</ol>
<p>
<?php
echo wp_kses_post(
sprintf(
// translators: Placeholders are <strong> html tags.
__( 'When you\'re ready, save your block by pressing %1$sPublish%2$s.', 'genesis-custom-blocks' ),
'<strong>',
'</strong>'
)
);
?>
</p>
<p>
<?php
esc_html_e( 'Learn more:', 'genesis-custom-blocks' );
printf(
' <a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
'https://developer.wpengine.com/genesis-custom-blocks/get-started/',
esc_html__( 'Get Started', 'genesis-custom-blocks' )
);
?>
</p>
</div>
<?php
}
/**
* Render the Add to Post message.
*/
public function show_add_to_post_notice() {
$post = get_post();
if ( ! $post || ! isset( $post->post_name ) || empty( $post->post_name ) ) {
return;
}
if ( 'publish' !== $post->post_status ) {
return;
}
$template = genesis_custom_blocks()->locate_template( "blocks/block-{$post->post_name}.php", '', true );
if ( ! $template ) {
return;
}
?>
<div class="genesis-custom-blocks-add-to-block genesis-custom-blocks-notice notice notice-large is-dismissible">
<h2>🚀 <?php esc_html_e( 'Only one thing left to do!', 'genesis-custom-blocks' ); ?></h2>
<p class="intro"><?php esc_html_e( 'You\'ve created a new block, and added a block template. Well done!', 'genesis-custom-blocks' ); ?></p>
<p><?php esc_html_e( 'All that\'s left is to add your block to a post.', 'genesis-custom-blocks' ); ?></p>
<a href="<?php echo esc_attr( admin_url( 'post-new.php' ) ); ?>" class="button">
<?php esc_html_e( 'Add New Post', 'genesis-custom-blocks' ); ?>
</a>
</div>
<?php
/*
* After we've shown the Add to Post message once, we can delete the option. This will
* ensure that no further onboarding messages are shown.
*/
delete_option( self::OPTION_NAME );
}
/**
* Create a dummy starter block when the plugin is activated for the first time.
*/
public function add_dummy_data() {
/*
* Check if there are any block posts already added, and if so, bail.
* Note: wp_count_posts() does not work here.
*/
$blocks = get_posts(
[
'post_type' => genesis_custom_blocks()->get_post_type_slug(),
'numberposts' => '1',
'post_status' => 'any',
'fields' => 'ids',
]
);
if ( count( $blocks ) > 0 ) {
return;
}
$categories = get_block_categories( get_post() );
$example_post_id = wp_insert_post(
[
'post_title' => __( 'Example Block', 'genesis-custom-blocks' ),
'post_name' => 'example-block',
'post_status' => 'draft',
'post_type' => genesis_custom_blocks()->get_post_type_slug(),
'post_content' => wp_json_encode(
[
'genesis-custom-blocks\/example-block' => [
'name' => 'example-block',
'title' => __( 'Example Block', 'genesis-custom-blocks' ),
'icon' => 'genesis_custom_blocks',
'category' => isset( $categories[0] ) ? $categories[0] : [],
'keywords' => [
__( 'sample', 'genesis-custom-blocks' ), // translators: A keyword, used for search.
__( 'tutorial', 'genesis-custom-blocks' ), // translators: A keyword, used for search.
__( 'template', 'genesis-custom-blocks' ), // translators: A keyword, used for search.
],
'fields' => [
'title' => [
'name' => 'title',
'label' => __( 'Title', 'genesis-custom-blocks' ),
'control' => 'text',
'type' => 'string',
'location' => 'editor',
'order' => 0,
'help' => __( 'The primary display text', 'genesis-custom-blocks' ),
'default' => '',
'placeholder' => '',
'maxlength' => null,
],
'description' => [
'name' => 'description',
'label' => __( 'Description', 'genesis-custom-blocks' ),
'control' => 'textarea',
'type' => 'string',
'location' => 'editor',
'order' => 1,
'help' => '',
'default' => '',
'placeholder' => '',
'maxlength' => null,
'number_rows' => 4,
],
'button-text' => [
'name' => 'button-text',
'label' => __( 'Button Text', 'genesis-custom-blocks' ),
'control' => 'text',
'type' => 'string',
'location' => 'editor',
'order' => 2,
'help' => __( 'A Call-to-Action', 'genesis-custom-blocks' ),
'default' => '',
'placeholder' => '',
'maxlength' => null,
],
'button-link' => [
'name' => 'button-link',
'label' => __( 'Button Link', 'genesis-custom-blocks' ),
'control' => 'url',
'type' => 'string',
'location' => 'editor',
'order' => 3,
'help' => __( 'The destination URL', 'genesis-custom-blocks' ),
'default' => '',
'placeholder' => '',
],
],
],
]
),
]
);
if ( ! is_wp_error( $example_post_id ) ) {
update_option( self::OPTION_NAME, $example_post_id );
}
}
}