-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock-editor.php
557 lines (471 loc) · 14.8 KB
/
block-editor.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
<?php
add_action( 'after_setup_theme', 'mrw_block_editor_theme_support', 11 );
/**
* Make modifications to editor that can be made using default add_theme_support() calls
*/
function mrw_block_editor_theme_support() {
/*
* Remove pixel-based font sizing
*
* Override with remove_theme_support( 'disable-custom-font-sizes' ) hooked to after_setup_theme with priority 12+
*
* @see https://developer.wordpress.org/block-editor/developers/themes/theme-support/#disabling-custom-font-sizes
*/
add_theme_support( 'disable-custom-font-sizes' );
/*
* Remove custom color picker
*
* Override with remove_theme_support( 'disable-custom-colors' ) hooked to after_setup_theme with priority 12+
*
* @see https://developer.wordpress.org/block-editor/developers/themes/theme-support/#disabling-custom-colors-in-block-color-palettes
*/
add_theme_support( 'disable-custom-colors' );
/*
* Remove the default color palette if the theme has not explicitly registered a palette
*
* Override this with mrw_block_editor_color_palette filter
*
* @see https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-color-palettes
*/
$theme_palette = get_theme_support( 'editor-color-palette' );
/**
* See if the Block Editor Colors plugin is activated. In that case, let it do its thing
*
* @var bool
*
* @see https://wordpress.org/plugins/block-editor-colors/
*/
$block_editor_colors_plugin_is_active = in_array( 'block-editor-colors/plugin.php', (array) get_option( 'active_plugins', array() ), true );
/**
* mrw_block_editor_hide_color_palette filter
* @since 2.0.0
*/
$hide_palette = apply_filters(
'mrw_block_editor_hide_color_palette',
empty( $theme_palette )
);
if( $hide_palette && ! $block_editor_colors_plugin_is_active ) {
add_theme_support( 'editor-color-palette' );
}
/*
* Hide the custom gradient builder
*
* Override with remove_theme_support( 'disable-custom-gradients' ) hooked to after_setup_theme with priority 12+
*
* @see https://developer.wordpress.org/block-editor/developers/themes/theme-support/#disabling-custom-gradients
*/
add_theme_support( 'disable-custom-gradients' );
/*
* Hide default gradient presets if the theme has not explicitly registered any
*
* Override this with mrw_block_editor_hide_gradient_presets filter
*
* @see https://developer.wordpress.org/block-editor/developers/themes/theme-support/#block-gradient-presets
*/
$theme_gradients = get_theme_support( 'editor-gradient-presets' );
/**
* mrw_block_editor_hide_gradient_presets filter
* @since 2.1.0
*/
$hide_gradients = apply_filters(
'mrw_block_editor_hide_gradient_presets',
empty( $theme_gradients )
);
if( $hide_gradients ) {
add_theme_support( 'editor-gradient-presets', array() );
}
/*
* Remove core-registered block patterns
*
* Override with add_theme_support( 'core-block-patterns' ) hooked to after_setup_theme with priority 12+
*
* @see https://make.wordpress.org/core/2020/07/16/block-patterns-in-wordpress-5-5/
*/
remove_theme_support( 'core-block-patterns' );
}
add_action( 'plugins_loaded', 'mrw_hide_block_directory' );
/**
* Hide the block directory
*
* @see https://github.com/WordPress/gutenberg/issues/23961#issuecomment-666683997
*/
function mrw_hide_block_directory() {
if( in_array( 'block-directory', mrw_hidden_block_editor_settings() ) ) {
remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' );
remove_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets_block_directory' );
}
}
/**
* Define default hidden blocks and allow them to be filtered
*
* Note: Blocks are hidden from the inserter rather than unregistered to avoid editor crashing when a hidden block is in the page content
*
* @return array slugs of all hidden core blocks
*/
function mrw_hidden_blocks() {
/**
* mrw_hidden_core_blocks filter
*
* Allows showing all hidden "Core" blocks by removing them from the hidden list. Use __return_empty_array to unhide all blocks.
*
* @since 2.5.0
*/
$hidden_core_blocks = apply_filters( 'mrw_hidden_core_blocks', array(
'core/audio',
'core/code',
'core/nextpage',
'core/preformatted',
'core/shortcode',
'core/spacer',
'core/table',
'core/verse',
'core/video',
) );
/**
* mrw_hidden_widget_blocks filter
*
* Allows showing all hidden "Widget" blocks by removing them from the hidden list. Use __return_empty_array to unhide all blocks.
*
* @since 2.5.0
*/
$hidden_widgets = apply_filters( 'mrw_hidden_widget_blocks', array(
'core/archives',
'core/calendar',
'core/categories',
'core/latest-comments',
'core/rss',
'core/search',
'core/tag-cloud',
) );
/**
* mrw_hidden_query_blocks filter
*
* Allows showing all hidden "Query" and "Post" blocks by removing them from the hidden list. Use __return_empty_array to unhide all blocks.
*
* @since 2.5.0
*/
$hidden_query_blocks = apply_filters( 'mrw_hidden_query_blocks', array(
'core/avatar',
'core/query',
'core/query-title',
'core/post-title',
'core/post-content',
'core/post-date',
'core/post-excerpt',
'core/post-featured-image',
'core/post-terms',
'core/post-author',
'core/post-author-biography',
'core/read-more',
'core/term-description',
) );
/**
* mrw_hidden_site_blocks filter
*
* Allows showing all hidden "Site"/"Theme" blocks by removing them from the hidden list. Use __return_empty_array to unhide all blocks.
*
* @since 2.5.0
*/
$hidden_site_blocks = apply_filters( 'mrw_hidden_site_blocks', array(
'core/comments-query-loop',
'core/loginout',
'core/page-list',
'core/site-logo',
'core/site-tagline',
'core/site-title',
'core/navigation',
'core/post-comments',
'core/post-comments-form',
'core/post-navigation-link',
) );
$hidden_blocks = array_merge(
$hidden_core_blocks,
$hidden_widgets,
$hidden_query_blocks,
$hidden_site_blocks,
);
// attempt to detect if More Tag button was previously made available in the classic editor before overriding the More Block
$mce_buttons = array_merge(
apply_filters( 'mce_buttons', array() ),
apply_filters( 'mce_buttons_2', array() )
);
if( ! in_array( 'wp_more', $mce_buttons ) ) {
$hidden_blocks[] = 'core/more';
}
/**
* mrw_hidden_blocks filter
* @since 2.3.0
*/
return apply_filters( 'mrw_hidden_blocks', $hidden_blocks );
}
/**
* Return list of hidden embeds
*/
function mrw_hidden_embeds() {
$hidden_embeds = array(
'amazon-kindle',
'animoto',
'cloudup',
'collegehumor',
'crowdsignal',
'dailymotion',
'hulu',
'mixcloud',
'polldaddy',
'reverbnation',
'smugmug',
'speaker',
'videopress',
'wolfram-cloud',
'wordpress-tv',
);
/**
* mrw_hidden_embeds filter
* @since 2.4.0
*/
return apply_filters( 'mrw_hidden_embeds', $hidden_embeds );
}
add_action( 'jetpack_register_gutenberg_extensions', 'mrw_jetpack_hidden_blocks', 99 );
/**
* Hidden Jetpack Blocks
*
* @see https://developer.jetpack.com/hooks/jetpack_register_gutenberg_extensions/
*/
function mrw_jetpack_hidden_blocks() {
if ( ! class_exists( 'Jetpack_Gutenberg' ) ) {
return;
}
$jetpack_hidden_block_reason = 'Hidden by MRW Simplified Editor. Use mrw_jetpack_hidden_blocks filter to restore.';
/**
* mrw_jetpack_hidden_blocks filter
* @since 2.3.0
*/
$mrw_jetpack_hidden_blocks = apply_filters(
'mrw_jetpack_hidden_blocks',
array(
'markdown',
'rating-star',
'repeat-visitor',
'opentable',
'revue',
'eventbrite',
'gif',
'calendly',
'send-a-message', // required for whatsapp-button
'whatsapp-button',
)
);
foreach( $mrw_jetpack_hidden_blocks as $block ) {
Jetpack_Gutenberg::set_extension_unavailable(
'jetpack/' . $block,
$jetpack_hidden_block_reason
);
}
}
/**
* Define default hidden block style and allow them to be filtered
*
* @return array keyed array where keys are a block slug and value is an array containing all block style to remove
*/
function mrw_hidden_block_styles() {
$hidden_styles = array(
'core/image' => array( 'default', 'circle-mask', 'rounded' ),
'core/button' => array( 'default', 'fill', 'squared', 'outline' ),
'core/quote' => array( 'default', 'large' ),
'core/separator' => array( 'default', 'wide', 'dots' ),
'core/pullquote' => array( 'default', 'solid-color' ),
'core/table' => array( 'default', 'stripes' ),
'core/social-links' => array( 'default', 'logos-only', 'pill-shape' ),
);
/**
* mrw_hidden_block_styles filter
* @since 2.3.0
*/
return apply_filters( 'mrw_hidden_block_styles', $hidden_styles );
}
/**
* Return filtered array of Block Editor Features/Settings to hide
*
* @return array every option to hide via CSS or JS
*/
function mrw_hidden_block_editor_settings() {
$hidden_block_editor_settings = array(
'block-directory',
'border',
'border-radius',
'default-color-palette',
'default-style-variation',
'drop-cap',
'duotone',
'font-weight',
'font-style',
'heading-1',
'heading-5',
'heading-6',
'image-dimensions',
'image-file-upload',
'image-url',
'letter-spacing',
'line-height',
'new-tabs',
'padding',
'pullquote-border',
'spacing',
'text-decoration',
'text-transform',
);
/**
* mrw_hidden_core_blocks filter
* @since 2.3.0
*/
return apply_filters( 'mrw_hidden_block_editor_settings', $hidden_block_editor_settings );
}
if( is_wp_version_compatible( '5.8' ) ) {
add_filter( 'block_editor_settings_all', 'mrw_block_editor_settings', 99, 2 );
} else {
add_filter( 'block_editor_settings', 'mrw_block_editor_settings', 10, 2 );
}
/**
* Make changes to editor settings, accounting for plugin filters, via the core block_editor_settings filter
*
* @param array $editor_settings default editor settings
* @return array modified settings
*
* @see https://github.com/joppuyo/remove-drop-cap/blob/v1.1.0/remove-drop-cap.php#L22
*/
function mrw_block_editor_settings( $editor_settings, $context ) {
$hidden_settings = mrw_hidden_block_editor_settings();
/* Border */
if( in_array( 'border', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['border'] = [];
}
/* Border Radius, Button */
if( in_array( 'border-radius', $hidden_settings) ) {
/* WordPress < 6.0 */
$editor_settings['__experimentalFeatures']['blocks']['core/button']['border']['customRadius'] = false;
/* WordPress 6.0+ */
$editor_settings['__experimentalFeatures']['blocks']['core/button']['border']['radius'] = false;
}
/* Default Color Pallete */
if( in_array( 'default-color-palette', $hidden_settings ) ) {
$editor_settings['__experimentalFeatures']['color']['defaultPalette'] = false;
}
/* Drop Cap */
if( in_array( 'drop-cap', $hidden_settings ) ) {
$editor_settings['__experimentalFeatures']['typography']['dropCap'] = false;
}
/* Duotone */
if( in_array( 'duotone', $hidden_settings ) ) {
$editor_settings['__experimentalFeatures']['color']['duotone'] = null;
$editor_settings['__experimentalFeatures']['color']['customDuotone'] = false;
}
/* Font Style */
if( in_array( 'font-style', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['typography']['fontStyle'] = false;
}
/* Font Weight */
if( in_array( 'font-weight', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['typography']['fontWeight'] = false;
}
/* Gap and Margin */
if( in_array( 'spacing', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['spacing'] = [];
}
/* Letter Spacing */
if( in_array( 'letter-spacing', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['typography']['letterSpacing'] = false;
}
/* Line Height */
if( in_array( 'line-height', $hidden_settings) ) {
$editor_settings['enableCustomLineHeight'] = false;
}
/* Padding */
if( in_array( 'padding', $hidden_settings) ) {
$editor_settings['enableCustomSpacing'] = false;
}
/* Pullquote Border */
if( in_array( 'pullquote-border', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['blocks']['core/pullquote']['border'] = [];
}
/* Text Decoration */
if( in_array( 'text-decoration', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['typography']['textDecoration'] = false;
}
/* Text Transform */
if( in_array( 'text-transform', $hidden_settings) ) {
$editor_settings['__experimentalFeatures']['typography']['textTransform'] = false;
}
return $editor_settings;
}
/**
* Prepare all plugin options (after being filtered) for use in JavaScript
*
* @return array all hidden blocks, block styles, and block editor settings
*/
function mrw_block_editor_js_config() {
$js_options = array();
// Note: using array_values to ensure this is passed as an array and not an object
/*==============================
= Blocks =
==============================*/
$js_options['hiddenBlocks'] = array_values( mrw_hidden_blocks() );
/*========================================
= Embed Variations =
========================================*/
$js_options['hiddenEmbeds'] = array_values( mrw_hidden_embeds() );
/*====================================
= Block Styles =
======================================*/
$hidden_style_variations = array();
foreach ( mrw_hidden_block_styles() as $block => $styles ) {
$hidden_styles[$block] = array_values( $styles );
}
$js_options['hiddenStyles'] = $hidden_styles;
/*================================
= Features =
================================*/
$js_options['hiddenSettings'] = array_values( mrw_hidden_block_editor_settings() );
return $js_options;
}
add_action( 'enqueue_block_editor_assets', 'mrw_block_editor_assets' );
/**
* Enqueue CSS and JS files that modify/hide block editor settings in the admin
*/
function mrw_block_editor_assets() {
wp_enqueue_style(
'mrw-block-editor-css',
plugins_url( 'css/block-editor.css', dirname(__FILE__) ),
array(),
'2.0.0'
);
wp_register_script(
'mrw-block-editor-js',
plugins_url( 'js/block-editor.js', dirname(__FILE__) ),
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
'2.0.0'
);
wp_localize_script(
'mrw-block-editor-js',
'mrwEditorOptions',
mrw_block_editor_js_config()
);
wp_enqueue_script( 'mrw-block-editor-js' );
}
add_action( 'admin_body_class', 'mrw_block_editor_settings_admin_classes' );
/**
* Apply body classes to admin for each features that are hidden via CSS
*
* @param array $classes list of hidden features
* @return array
*/
function mrw_block_editor_settings_admin_classes( $classes ) {
$current_screen = get_current_screen();
if( isset( $current_screen->is_block_editor ) && (bool) $current_screen->is_block_editor ) {
$hidden_block_editor_settings = mrw_hidden_block_editor_settings();
$prefix = ' mrw-block-editor-no-';
foreach( $hidden_block_editor_settings as $setting ) {
$classes .= $prefix . sanitize_title_with_dashes( $setting );
}
}
return $classes;
}