Skip to content

Commit d0e3877

Browse files
committed
Move all editor settings to PHP
1 parent 5b598a2 commit d0e3877

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

inc/Services/Editor.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public function boot( Service_Container $container ): void {
4848
*/
4949
$this->register_custom_block_styles();
5050

51+
/**
52+
* Customize theme.json settings
53+
*/
54+
add_filter( 'wp_theme_json_data_theme', [ $this, 'filter_theme_json_theme' ], 10, 1 );
55+
5156
/**
5257
* Load editor JS for ADMIN
5358
*/
@@ -154,6 +159,27 @@ private function style(): void {
154159
add_editor_style( 'dist/' . $file );
155160
}
156161

162+
/**
163+
* Theme.json settings
164+
* See https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/
165+
*
166+
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
167+
*
168+
* return WP_Theme_JSON_Data
169+
*/
170+
public function filter_theme_json_theme( $theme_json ): \WP_Theme_JSON_Data {
171+
$custom_theme_json = [
172+
'version' => 2,
173+
'settings' => [
174+
'typography' => [
175+
'dropCap' => false,
176+
],
177+
],
178+
];
179+
180+
return $theme_json->update_with( $custom_theme_json );
181+
}
182+
157183
/**
158184
* Editor script
159185
*/
@@ -173,6 +199,23 @@ public function admin_editor_script(): void {
173199
$asset_data['version'],
174200
true
175201
);
202+
203+
$this->assets_tools->add_inline_script( 'theme-admin-editor-script', 'const BFFEditorSettings = ' . json_encode( array(
204+
'disableAllBlocksStyles' => apply_filters( 'bff/editor/disable_all_blocks_styles', [
205+
'core/separator',
206+
'core/quote',
207+
'core/pullquote',
208+
'core/table',
209+
'core/image'
210+
] ),
211+
'disabledBlocksStyles' => apply_filters( 'bff/editor/disabled_blocks_styles', [
212+
// 'core/button' => [ 'outline' ]
213+
] ),
214+
'allowedBlocksVariations' => apply_filters( 'bff/editor/allowed_blocks_variations', [
215+
'core/embed' => [ 'youtube', 'vimeo', 'dailymotion' ],
216+
] ),
217+
) ), 'before' );
218+
176219
$this->assets_tools->enqueue_script( 'theme-admin-editor-script' );
177220
}
178221

inc/Tools/Assets.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public function register_script( string $handle, string $src, array $deps = [],
2525
return \wp_register_script( $handle, \get_theme_file_uri( $src ), $deps, $ver, $in_footer );
2626
}
2727

28+
/**
29+
* Add inline script to a registered script.
30+
*
31+
* @param string $handle
32+
* @param string $data
33+
* @param string $position
34+
*
35+
* @return bool
36+
*/
37+
public function add_inline_script( string $handle, string $data, string $position = 'after' ): bool {
38+
return \wp_add_inline_script( $handle, $data, $position );
39+
}
40+
2841
/**
2942
* @param string $handle
3043
*/

src/js/editor.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/*global BFFEditorSettings*/
2+
13
import lazySizes from 'lazysizes'
24
import 'lazysizes/plugins/native-loading/ls.native-loading'
35
import 'lazysizes/plugins/object-fit/ls.object-fit'
@@ -15,14 +17,23 @@ lazySizes.cfg.nativeLoading = {
1517

1618
// Native Gutenberg
1719
domReady(() => {
18-
unregisterBlockStyle('core/separator', ['wide', 'dots'])
19-
// whitelist core embeds
20-
const allowedEmbedVariants = ['youtube', 'vimeo', 'dailymotion']
21-
getBlockVariations('core/embed').forEach((variant) => {
22-
if (!allowedEmbedVariants.includes(variant.name)) {
23-
unregisterBlockVariation('core/embed', variant.name)
24-
}
25-
})
20+
// Disable specific block styles
21+
if (BFFEditorSettings.disabledBlocksStyles) {
22+
Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach(([block, styles]) => {
23+
unregisterBlockStyle(block, styles)
24+
})
25+
}
26+
27+
// Allow blocks variations
28+
if (BFFEditorSettings.allowedBlocksVariations) {
29+
Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach(([block, variations]) => {
30+
getBlockVariations(block).forEach((variant) => {
31+
if (!variations.includes(variant.name)) {
32+
unregisterBlockVariation(block, variant.name)
33+
}
34+
})
35+
})
36+
}
2637
})
2738

2839
// ACF Blocks
@@ -31,22 +42,9 @@ if (window.acf) {
3142
}
3243

3344
addFilter('blocks.registerBlockType', 'beapi-framework', function (settings, name) {
34-
if (name === 'core/paragraph') {
35-
settings.example.attributes.dropCap = false
36-
}
37-
38-
if (name === 'core/separator' || name === 'core/quote' || name === 'core/pullquote' || name === 'core/table') {
39-
// remove custom styles
40-
settings.styles = []
41-
}
42-
43-
if (name === 'core/image') {
44-
// remove custom styles
45+
// Disable all styles
46+
if (BFFEditorSettings.disableAllBlocksStyles && BFFEditorSettings.disableAllBlocksStyles.includes(name)) {
4547
settings.styles = []
46-
// set default aligment for images to null
47-
settings.attributes.align = {
48-
type: 'string',
49-
}
5048
}
5149

5250
return settings

0 commit comments

Comments
 (0)