-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
client-assets.php
979 lines (872 loc) · 30.8 KB
/
client-assets.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<?php
/**
* Functions to register client-side assets (scripts and stylesheets) for the
* Gutenberg editor plugin.
*
* @package gutenberg
*/
if ( ! defined( 'ABSPATH' ) ) {
die( 'Silence is golden.' );
}
/**
* Retrieves the root plugin path.
*
* @return string Root path to the gutenberg plugin.
*
* @since 0.1.0
*/
function gutenberg_dir_path() {
return plugin_dir_path( dirname( __FILE__ ) );
}
/**
* Retrieves a URL to a file in the gutenberg plugin.
*
* @param string $path Relative path of the desired file.
*
* @return string Fully qualified URL pointing to the desired file.
*
* @since 0.1.0
*/
function gutenberg_url( $path ) {
return plugins_url( $path, dirname( __FILE__ ) );
}
/**
* Registers a script according to `wp_register_script`. Honors this request by
* reassigning internal dependency properties of any script handle already
* registered by that name. It does not deregister the original script, to
* avoid losing inline scripts which may have been attached.
*
* @since 4.1.0
*
* @param string $handle Name of the script. Should be unique.
* @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
* @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
* @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
* as a query string for cache busting purposes. If version is set to false, a version
* number is automatically added equal to current installed WordPress version.
* If set to null, no version is added.
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
* Default 'false'.
*/
function gutenberg_override_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
global $wp_scripts;
$script = $wp_scripts->query( $handle, 'registered' );
if ( $script ) {
/*
* In many ways, this is a reimplementation of `wp_register_script` but
* bypassing consideration of whether a script by the given handle had
* already been registered.
*/
// See: `_WP_Dependency::__construct` .
$script->src = $src;
$script->deps = $deps;
$script->ver = $ver;
/*
* The script's `group` designation is an indication of whether it is
* to be printed in the header or footer. The behavior here defers to
* the arguments as passed. Specifically, group data is not assigned
* for a script unless it is designated to be printed in the footer.
*/
// See: `wp_register_script` .
unset( $script->extra['group'] );
if ( $in_footer ) {
$script->add_data( 'group', 1 );
}
} else {
wp_register_script( $handle, $src, $deps, $ver, $in_footer );
}
}
/**
* Registers a style according to `wp_register_style`. Honors this request by
* deregistering any style by the same handler before registration.
*
* @since 4.1.0
*
* @param string $handle Name of the stylesheet. Should be unique.
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
* @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
* as a query string for cache busting purposes. If version is set to false, a version
* number is automatically added equal to current installed WordPress version.
* If set to null, no version is added.
* @param string $media Optional. The media for which this stylesheet has been defined.
* Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
* '(orientation: portrait)' and '(max-width: 640px)'.
*/
function gutenberg_override_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
wp_deregister_style( $handle );
wp_register_style( $handle, $src, $deps, $ver, $media );
}
/**
* Registers all the WordPress packages scripts that are in the standardized
* `build/` location.
*
* @since 4.5.0
*/
function gutenberg_register_packages_scripts() {
$packages_dependencies = include dirname( __FILE__ ) . '/packages-dependencies.php';
foreach ( $packages_dependencies as $handle => $dependencies ) {
// Remove `wp-` prefix from the handle to get the package's name.
$package_name = strpos( $handle, 'wp-' ) === 0 ? substr( $handle, 3 ) : $handle;
$path = "build/$package_name/index.js";
gutenberg_override_script(
$handle,
gutenberg_url( $path ),
array_merge( $dependencies, array( 'wp-polyfill' ) ),
filemtime( gutenberg_dir_path() . $path ),
true
);
}
}
/**
* Registers common scripts and styles to be used as dependencies of the editor
* and plugins.
*
* @since 0.1.0
*/
function gutenberg_register_scripts_and_styles() {
global $wp_scripts;
gutenberg_register_vendor_scripts();
gutenberg_register_packages_scripts();
// Add nonce middleware which accounts for the absence of the heartbeat
// listener. This relies on API Fetch implementation running middlewares in
// order of last added, and that the original nonce middleware would defer
// to an X-WP-Nonce header already being present. This inline script should
// be removed once the following Core ticket is resolved in assigning the
// nonce received from heartbeat to the created middleware.
//
// See: https://core.trac.wordpress.org/ticket/46107 .
// See: https://github.com/WordPress/gutenberg/pull/13451 .
global $wp_scripts;
if ( isset( $wp_scripts->registered['wp-api-fetch'] ) ) {
$wp_scripts->registered['wp-api-fetch']->deps[] = 'wp-hooks';
}
wp_add_inline_script(
'wp-api-fetch',
sprintf(
implode(
"\n",
array(
'( function() {',
' var nonceMiddleware = wp.apiFetch.createNonceMiddleware( "%s" );',
' wp.apiFetch.use( nonceMiddleware );',
' wp.hooks.addAction(',
' "heartbeat.tick",',
' "core/api-fetch/create-nonce-middleware",',
' function( response ) {',
' if ( response[ "rest_nonce" ] ) {',
' nonceMiddleware.nonce = response[ "rest_nonce" ];',
' }',
' }',
' )',
'} )();',
)
),
( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' )
),
'after'
);
// TEMPORARY: Core does not (yet) provide persistence migration from the
// introduction of the block editor.
wp_add_inline_script(
'wp-data',
implode(
"\n",
array(
'( function() {',
' var userId = ' . get_current_user_ID() . ';',
' var storageKey = "WP_DATA_USER_" + userId;',
' wp.data.plugins.persistence.__unstableMigrate( { storageKey: storageKey } );',
'} )()',
)
)
);
// Editor Styles.
// This empty stylesheet is defined to ensure backward compatibility.
gutenberg_override_style( 'wp-blocks', false );
$fonts_url = '';
/*
* Translators: Use this to specify the proper Google Font name and variants
* to load that is supported by your language. Do not translate.
* Set to 'off' to disable loading.
*/
$font_family = _x( 'Noto Serif:400,400i,700,700i', 'Google Font Name and Variants', 'gutenberg' );
if ( 'off' !== $font_family ) {
$query_args = array(
'family' => urlencode( $font_family ),
);
$fonts_url = esc_url_raw( add_query_arg( $query_args, 'https://fonts.googleapis.com/css' ) );
}
gutenberg_override_style(
'wp-editor-font',
$fonts_url,
array(),
null
);
gutenberg_override_style(
'wp-editor',
gutenberg_url( 'build/editor/style.css' ),
array( 'wp-components', 'wp-editor-font', 'wp-nux' ),
filemtime( gutenberg_dir_path() . 'build/editor/style.css' )
);
wp_style_add_data( 'wp-editor', 'rtl', 'replace' );
gutenberg_override_style(
'wp-edit-post',
gutenberg_url( 'build/edit-post/style.css' ),
array( 'wp-components', 'wp-editor', 'wp-edit-blocks', 'wp-block-library', 'wp-nux' ),
filemtime( gutenberg_dir_path() . 'build/edit-post/style.css' )
);
wp_style_add_data( 'wp-edit-post', 'rtl', 'replace' );
gutenberg_override_style(
'wp-components',
gutenberg_url( 'build/components/style.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/components/style.css' )
);
wp_style_add_data( 'wp-components', 'rtl', 'replace' );
gutenberg_override_style(
'wp-block-library',
gutenberg_url( 'build/block-library/style.css' ),
current_theme_supports( 'wp-block-styles' ) ? array( 'wp-block-library-theme' ) : array(),
filemtime( gutenberg_dir_path() . 'build/block-library/style.css' )
);
wp_style_add_data( 'wp-block-library', 'rtl', 'replace' );
gutenberg_override_style(
'wp-format-library',
gutenberg_url( 'build/format-library/style.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/format-library/style.css' )
);
wp_style_add_data( 'wp-format-library', 'rtl', 'replace' );
gutenberg_override_style(
'wp-edit-blocks',
gutenberg_url( 'build/block-library/editor.css' ),
array(
'wp-components',
'wp-editor',
'wp-block-library',
// Always include visual styles so the editor never appears broken.
'wp-block-library-theme',
),
filemtime( gutenberg_dir_path() . 'build/block-library/editor.css' )
);
wp_style_add_data( 'wp-edit-blocks', 'rtl', 'replace' );
gutenberg_override_style(
'wp-nux',
gutenberg_url( 'build/nux/style.css' ),
array( 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/nux/style.css' )
);
wp_style_add_data( 'wp-nux', 'rtl', 'replace' );
gutenberg_override_style(
'wp-block-library-theme',
gutenberg_url( 'build/block-library/theme.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/block-library/theme.css' )
);
wp_style_add_data( 'wp-block-library-theme', 'rtl', 'replace' );
gutenberg_override_style(
'wp-list-reusable-blocks',
gutenberg_url( 'build/list-reusable-blocks/style.css' ),
array( 'wp-components' ),
filemtime( gutenberg_dir_path() . 'build/list-reusable-blocks/style.css' )
);
wp_style_add_data( 'wp-list-reusable-block', 'rtl', 'replace' );
gutenberg_override_style(
'wp-edit-widgets',
gutenberg_url( 'build/edit-widgets/style.css' ),
array(),
filemtime( gutenberg_dir_path() . 'build/edit-widgets/style.css' )
);
wp_style_add_data( 'wp-edit-widgets', 'rtl', 'replace' );
if ( defined( 'GUTENBERG_LIVE_RELOAD' ) && GUTENBERG_LIVE_RELOAD ) {
$live_reload_url = ( GUTENBERG_LIVE_RELOAD === true ) ? 'http://localhost:35729/livereload.js' : GUTENBERG_LIVE_RELOAD;
wp_enqueue_script(
'gutenberg-live-reload',
$live_reload_url
);
}
}
add_action( 'wp_enqueue_scripts', 'gutenberg_register_scripts_and_styles', 5 );
add_action( 'admin_enqueue_scripts', 'gutenberg_register_scripts_and_styles', 5 );
/**
* Registers vendor JavaScript files to be used as dependencies of the editor
* and plugins.
*
* This function is called from a script during the plugin build process, so it
* should not call any WordPress PHP functions.
*
* @since 0.1.0
*/
function gutenberg_register_vendor_scripts() {
/*
* This function is kept as an empty stub, in case Gutenberg should need to
* explicitly provide a version newer than that provided by core.
*/
}
/**
* Retrieves a unique and reasonably short and human-friendly filename for a
* vendor script based on a URL and the script handle.
*
* @param string $handle The name of the script.
* @param string $src Full URL of the external script.
*
* @return string Script filename suitable for local caching.
*
* @since 0.1.0
*/
function gutenberg_vendor_script_filename( $handle, $src ) {
$filename = basename( $src );
$match = preg_match(
'/^'
. '(?P<ignore>.*?)'
. '(?P<suffix>\.min)?'
. '(?P<extension>\.js)'
. '(?P<extra>.*)'
. '$/',
$filename,
$filename_pieces
);
$prefix = $handle;
$suffix = $match ? $filename_pieces['suffix'] : '';
$hash = substr( md5( $src ), 0, 8 );
return "${prefix}${suffix}.${hash}.js";
}
/**
* Registers a vendor script from a URL, preferring a locally cached version if
* possible, or downloading it if the cached version is unavailable or
* outdated.
*
* @param string $handle Name of the script.
* @param string $src Full URL of the external script.
* @param array $deps Optional. An array of registered script handles this
* script depends on.
*
* @since 0.1.0
*/
function gutenberg_register_vendor_script( $handle, $src, $deps = array() ) {
if ( defined( 'GUTENBERG_LOAD_VENDOR_SCRIPTS' ) && ! GUTENBERG_LOAD_VENDOR_SCRIPTS ) {
return;
}
$filename = gutenberg_vendor_script_filename( $handle, $src );
if ( defined( 'GUTENBERG_LIST_VENDOR_ASSETS' ) && GUTENBERG_LIST_VENDOR_ASSETS ) {
echo "$src|$filename\n";
return;
}
$full_path = gutenberg_dir_path() . 'vendor/' . $filename;
$needs_fetch = (
defined( 'GUTENBERG_DEVELOPMENT_MODE' ) && GUTENBERG_DEVELOPMENT_MODE && (
! file_exists( $full_path ) ||
time() - filemtime( $full_path ) >= DAY_IN_SECONDS
)
);
if ( $needs_fetch ) {
// Determine whether we can write to this file. If not, don't waste
// time doing a network request.
// @codingStandardsIgnoreStart
$f = @fopen( $full_path, 'a' );
// @codingStandardsIgnoreEnd
if ( ! $f ) {
// Failed to open the file for writing, probably due to server
// permissions. Enqueue the script directly from the URL instead.
gutenberg_override_script( $handle, $src, $deps, null );
return;
}
fclose( $f );
$response = wp_remote_get( $src );
if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
$f = fopen( $full_path, 'w' );
fwrite( $f, wp_remote_retrieve_body( $response ) );
fclose( $f );
} elseif ( ! filesize( $full_path ) ) {
// The request failed. If the file is already cached, continue to
// use this file. If not, then unlink the 0 byte file, and enqueue
// the script directly from the URL.
gutenberg_override_script( $handle, $src, $deps, null );
unlink( $full_path );
return;
}
}
gutenberg_override_script(
$handle,
gutenberg_url( 'vendor/' . $filename ),
$deps,
null
);
}
/**
* Assigns a default editor template with a default block by post format, if
* not otherwise assigned for a new post of type "post".
*
* @param array $settings Default editor settings.
* @param WP_Post $post Post being edited.
*
* @return array Filtered block editor settings.
*/
function gutenberg_default_post_format_template( $settings, $post ) {
// Only assign template for new posts without explicitly assigned template.
$is_new_post = 'auto-draft' === $post->post_status;
if ( $is_new_post && ! isset( $settings['template'] ) && 'post' === $post->post_type ) {
switch ( get_post_format() ) {
case 'audio':
$default_block_name = 'core/audio';
break;
case 'gallery':
$default_block_name = 'core/gallery';
break;
case 'image':
$default_block_name = 'core/image';
break;
case 'quote':
$default_block_name = 'core/quote';
break;
case 'video':
$default_block_name = 'core/video';
break;
}
if ( isset( $default_block_name ) ) {
$settings['template'] = array( array( $default_block_name ) );
}
}
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_default_post_format_template', 10, 2 );
/**
* Retrieve a stored autosave that is newer than the post save.
*
* Deletes autosaves that are older than the post save.
*
* @param WP_Post $post Post object.
* @return WP_Post|boolean The post autosave. False if none found.
*/
function gutenberg_get_autosave_newer_than_post_save( $post ) {
// Add autosave data if it is newer and changed.
$autosave = wp_get_post_autosave( $post->ID );
if ( ! $autosave ) {
return false;
}
// Check if the autosave is newer than the current post.
if (
mysql2date( 'U', $autosave->post_modified_gmt, false ) > mysql2date( 'U', $post->post_modified_gmt, false )
) {
return $autosave;
}
// If the autosave isn't newer, remove it.
wp_delete_post_revision( $autosave->ID );
return false;
}
/**
* Loads Gutenberg Locale Data.
*/
function gutenberg_load_locale_data() {
// Prepare Jed locale data.
$locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
wp_add_inline_script(
'wp-i18n',
'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );'
);
}
/**
* Retrieve The available image sizes for a post
*
* @return array
*/
function gutenberg_get_available_image_sizes() {
$size_names = apply_filters(
'image_size_names_choose',
array(
'thumbnail' => __( 'Thumbnail', 'gutenberg' ),
'medium' => __( 'Medium', 'gutenberg' ),
'large' => __( 'Large', 'gutenberg' ),
'full' => __( 'Full Size', 'gutenberg' ),
)
);
$all_sizes = array();
foreach ( $size_names as $size_slug => $size_name ) {
$all_sizes[] = array(
'slug' => $size_slug,
'name' => $size_name,
);
}
return $all_sizes;
}
/**
* Extends block editor settings to include Gutenberg's `editor-styles.css` as
* taking precedent those styles shipped with core.
*
* @param array $settings Default editor settings.
*
* @return array Filtered editor settings.
*/
function gutenberg_extend_block_editor_styles( $settings ) {
$editor_styles_file = gutenberg_dir_path() . 'build/editor/editor-styles.css';
/*
* If, for whatever reason, the built editor styles do not exist, avoid
* override and fall back to the default.
*/
if ( ! file_exists( $editor_styles_file ) ) {
return $settings;
}
if ( empty( $settings['styles'] ) ) {
$settings['styles'] = array();
} else {
/*
* The styles setting is an array of CSS strings, so there is no direct
* way to find the default styles. To maximize stability, load (again)
* the default styles from disk and find its place in the array.
*
* See: https://github.com/WordPress/wordpress-develop/blob/5.0.3/src/wp-admin/edit-form-blocks.php#L168-L175
*/
$default_styles = file_get_contents(
ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
);
/*
* Iterate backwards from the end of the array since the preferred
* insertion point in case not found is prepended as first entry.
*/
for ( $i = count( $settings['styles'] ) - 1; $i >= 0; $i-- ) {
if ( isset( $settings['styles'][ $i ]['css'] ) &&
$default_styles === $settings['styles'][ $i ]['css'] ) {
break;
}
}
}
$editor_styles = array(
'css' => file_get_contents( $editor_styles_file ),
);
// Substitute default styles if found. Otherwise, prepend to setting array.
if ( isset( $i ) && $i >= 0 ) {
$settings['styles'][ $i ] = $editor_styles;
} else {
array_unshift( $settings['styles'], $editor_styles );
}
return $settings;
}
add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' );
/**
* Scripts & Styles.
*
* Enqueues the needed scripts and styles when visiting the top-level page of
* the Gutenberg editor.
*
* @since 0.1.0
*
* @param string $hook Screen name.
*/
function gutenberg_editor_scripts_and_styles( $hook ) {
global $wp_meta_boxes;
// Enqueue heartbeat separately as an "optional" dependency of the editor.
// Heartbeat is used for automatic nonce refreshing, but some hosts choose
// to disable it outright.
wp_enqueue_script( 'heartbeat' );
wp_enqueue_script( 'wp-edit-post' );
wp_enqueue_script( 'wp-format-library' );
wp_enqueue_style( 'wp-format-library' );
global $post;
// Set initial title to empty string for auto draft for duration of edit.
// Otherwise, title defaults to and displays as "Auto Draft".
$is_new_post = 'auto-draft' === $post->post_status;
// Set the post type name.
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$rest_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
$preload_paths = array(
'/',
'/wp/v2/types?context=edit',
'/wp/v2/taxonomies?per_page=-1&context=edit',
'/wp/v2/themes?status=active',
sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ),
sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
array( '/wp/v2/media', 'OPTIONS' ),
array( '/wp/v2/blocks', 'OPTIONS' ),
);
/**
* Preload common data by specifying an array of REST API paths that will be preloaded.
*
* Filters the array of paths that will be preloaded.
*
* @param array $preload_paths Array of paths to preload
* @param object $post The post resource data.
*/
$preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
// Ensure the global $post remains the same after
// API data is preloaded. Because API preloading
// can call the_content and other filters, callbacks
// can unexpectedly modify $post resulting in issues
// like https://github.com/WordPress/gutenberg/issues/7468.
$backup_global_post = $post;
$preload_data = array_reduce(
$preload_paths,
'rest_preload_api_request',
array()
);
// Restore the global $post as it was before API preloading.
$post = $backup_global_post;
wp_add_inline_script(
'wp-api-fetch',
sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data ) ),
'after'
);
wp_add_inline_script(
'wp-blocks',
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $post ) ) ),
'after'
);
// Assign initial edits, if applicable. These are not initially assigned
// to the persisted post, but should be included in its save payload.
if ( $is_new_post ) {
// Override "(Auto Draft)" new post default title with empty string,
// or filtered value.
$initial_edits = array(
'title' => $post->post_title,
'content' => $post->post_content,
'excerpt' => $post->post_excerpt,
);
} else {
$initial_edits = null;
}
gutenberg_load_locale_data();
// Preload server-registered block schemas.
wp_add_inline_script(
'wp-blocks',
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . json_encode( get_block_editor_server_block_settings() ) . ');'
);
// Get admin url for handling meta boxes.
$meta_box_url = admin_url( 'post.php' );
$meta_box_url = add_query_arg(
array(
'post' => $post->ID,
'action' => 'edit',
'meta-box-loader' => true,
'_wpnonce' => wp_create_nonce( 'meta-box-loader' ),
),
$meta_box_url
);
wp_localize_script( 'wp-editor', '_wpMetaBoxUrl', $meta_box_url );
// Initialize the editor.
$gutenberg_theme_support = get_theme_support( 'gutenberg' );
$align_wide = get_theme_support( 'align-wide' );
$color_palette = current( (array) get_theme_support( 'editor-color-palette' ) );
$font_sizes = current( (array) get_theme_support( 'editor-font-sizes' ) );
if ( ! empty( $gutenberg_theme_support ) ) {
wp_enqueue_script( 'wp-deprecated' );
wp_add_inline_script( 'wp-deprecated', 'wp.deprecated( "`gutenberg` theme support", { plugin: "Gutenberg", version: "5.2", alternative: "`align-wide` theme support" } );' );
}
/**
* Filters the allowed block types for the editor, defaulting to true (all
* block types supported).
*
* @param bool|array $allowed_block_types Array of block type slugs, or
* boolean to enable/disable all.
* @param object $post The post resource data.
*/
$allowed_block_types = apply_filters( 'allowed_block_types', true, $post );
// Get all available templates for the post/page attributes meta-box.
// The "Default template" array element should only be added if the array is
// not empty so we do not trigger the template select element without any options
// besides the default value.
$available_templates = wp_get_theme()->get_page_templates( get_post( $post->ID ) );
$available_templates = ! empty( $available_templates ) ? array_merge(
array(
'' => apply_filters( 'default_page_template_title', __( 'Default template', 'gutenberg' ), 'rest-api' ),
),
$available_templates
) : $available_templates;
// Media settings.
$max_upload_size = wp_max_upload_size();
if ( ! $max_upload_size ) {
$max_upload_size = 0;
}
// Editor Styles.
global $editor_styles;
$styles = array(
array(
'css' => file_get_contents(
ABSPATH . WPINC . '/css/dist/editor/editor-styles.css'
),
),
);
/* Translators: Use this to specify the CSS font family for the default font */
$locale_font_family = esc_html_x( 'Noto Serif', 'CSS Font Family for Editor Font', 'gutenberg' );
$styles[] = array(
'css' => "body { font-family: '$locale_font_family' }",
);
if ( $editor_styles && current_theme_supports( 'editor-styles' ) ) {
foreach ( $editor_styles as $style ) {
if ( filter_var( $style, FILTER_VALIDATE_URL ) ) {
$styles[] = array(
'css' => file_get_contents( $style ),
);
} else {
$file = get_theme_file_path( $style );
if ( file_exists( $file ) ) {
$styles[] = array(
'css' => file_get_contents( $file ),
'baseURL' => get_theme_file_uri( $style ),
);
}
}
}
}
// Lock settings.
$user_id = wp_check_post_lock( $post->ID );
if ( $user_id ) {
/**
* Filters whether to show the post locked dialog.
*
* Returning a falsey value to the filter will short-circuit displaying the dialog.
*
* @since 3.6.0
*
* @param bool $display Whether to display the dialog. Default true.
* @param WP_Post $post Post object.
* @param WP_User|bool $user The user id currently editing the post.
*/
if ( apply_filters( 'show_post_locked_dialog', true, $post, $user_id ) ) {
$locked = true;
}
$user_details = null;
if ( $locked ) {
$user = get_userdata( $user_id );
$user_details = array(
'name' => $user->display_name,
);
$avatar = get_avatar( $user_id, 64 );
if ( $avatar ) {
if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) ) {
$user_details['avatar'] = $matches[1];
}
}
}
$lock_details = array(
'isLocked' => $locked,
'user' => $user_details,
);
} else {
// Lock the post.
$active_post_lock = wp_set_post_lock( $post->ID );
$lock_details = array(
'isLocked' => false,
'activePostLock' => esc_attr( implode( ':', $active_post_lock ) ),
);
}
$editor_settings = array(
'alignWide' => $align_wide || ! empty( $gutenberg_theme_support[0]['wide-images'] ), // Backcompat. Use `align-wide` outside of `gutenberg` array.
'availableTemplates' => $available_templates,
'allowedBlockTypes' => $allowed_block_types,
'disableCustomColors' => get_theme_support( 'disable-custom-colors' ),
'disableCustomFontSizes' => get_theme_support( 'disable-custom-font-sizes' ),
'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title', 'gutenberg' ), $post ),
'bodyPlaceholder' => apply_filters( 'write_your_story', __( 'Start writing or type / to choose a block', 'gutenberg' ), $post ),
'isRTL' => is_rtl(),
'autosaveInterval' => 10,
'maxUploadFileSize' => $max_upload_size,
'allowedMimeTypes' => get_allowed_mime_types(),
'styles' => $styles,
'imageSizes' => gutenberg_get_available_image_sizes(),
'richEditingEnabled' => user_can_richedit(),
// Ideally, we'd remove this and rely on a REST API endpoint.
'postLock' => $lock_details,
'postLockUtils' => array(
'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ),
'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
),
// Whether or not to load the 'postcustom' meta box is stored as a user meta
// field so that we're not always loading its assets.
'enableCustomFields' => (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
);
$post_autosave = gutenberg_get_autosave_newer_than_post_save( $post );
if ( $post_autosave ) {
$editor_settings['autosave'] = array(
'editLink' => get_edit_post_link( $post_autosave->ID ),
);
}
if ( false !== $color_palette ) {
$editor_settings['colors'] = $color_palette;
}
if ( false !== $font_sizes ) {
$editor_settings['fontSizes'] = $font_sizes;
}
if ( ! empty( $post_type_object->template ) ) {
$editor_settings['template'] = $post_type_object->template;
$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
}
$current_screen = get_current_screen();
$core_meta_boxes = array();
// Make sure the current screen is set as well as the normal core metaboxes.
if ( isset( $current_screen->id ) && isset( $wp_meta_boxes[ $current_screen->id ]['normal']['core'] ) ) {
$core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
}
// Check if the Custom Fields meta box has been removed at some point.
if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
unset( $editor_settings['enableCustomFields'] );
}
/**
* Filters the settings to pass to the block editor.
*
* @since 3.7.0
*
* @param array $editor_settings Default editor settings.
* @param WP_Post $post Post being edited.
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
$init_script = <<<JS
( function() {
window._wpLoadBlockEditor = new Promise( function( resolve ) {
wp.domReady( function() {
resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
} );
} );
Object.defineProperty( window, '_wpLoadGutenbergEditor', {
get: function() {
// TODO: Hello future maintainer. In removing this deprecation,
// ensure also to check whether `wp-editor`'s dependencies in
// `package-dependencies.php` still require `wp-deprecated`.
wp.deprecated( '`window._wpLoadGutenbergEditor`', {
plugin: 'Gutenberg',
version: '5.2',
alternative: '`window._wpLoadBlockEditor`',
hint: 'This is a private API, not intended for public use. It may be removed in the future.'
} );
return window._wpLoadBlockEditor;
}
} );
} )();
JS;
$script = sprintf(
$init_script,
$post->post_type,
$post->ID,
wp_json_encode( $editor_settings ),
wp_json_encode( $initial_edits )
);
wp_add_inline_script( 'wp-edit-post', $script );
/**
* Scripts
*/
wp_enqueue_media(
array(
'post' => $post->ID,
)
);
wp_tinymce_inline_scripts();
wp_enqueue_editor();
/**
* Styles
*/
wp_enqueue_style( 'wp-edit-post' );
/**
* Fires after block assets have been enqueued for the editing interface.
*
* Call `add_action` on any hook before 'admin_enqueue_scripts'.
*
* In the function call you supply, simply use `wp_enqueue_script` and
* `wp_enqueue_style` to add your functionality to the Gutenberg editor.
*
* @since 0.4.0
*/
do_action( 'enqueue_block_editor_assets' );
}