Skip to content

Commit 9cbf635

Browse files
authored
Merge pull request #405 from BeAPI/feat/editor-settings-php
Move all editor settings to PHP
2 parents af482b7 + 54f8a67 commit 9cbf635

File tree

5 files changed

+2201
-2857
lines changed

5 files changed

+2201
-2857
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,25 @@ You can launch a bundle report with the following command :
109109
```bash
110110
yarn bundle-report
111111
```
112+
113+
## WordPress Editor (Gutenberg)
114+
115+
### Customize blocks
116+
117+
The `bff_editor_custom_settings` filter allow you to customize blocks styles and variations. For example:
118+
119+
```php
120+
add_filter( 'bff_editor_custom_settings', 'customize_editor_settings', 10, 1 );
121+
function customize_editor_settings( $settings ) {
122+
// Disable all block styles for Separator block
123+
$settings[ 'disableAllBlocksStyles' ] = [ 'core/separator' ];
124+
125+
// Disable specific block style for Button block
126+
$settings[ 'disabledBlocksStyles' ] = [ 'core/button' => [ 'outline' ] ];
127+
128+
// Allow only YouTube variation for Embed block
129+
$settings[ 'allowedBlocksVariations' ] = [ 'core/embed' => [ 'youtube' ] ];
130+
131+
return $settings;
132+
}
133+
```

inc/Services/Editor.php

+52
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( \WP_Theme_JSON_Data $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,32 @@ public function admin_editor_script(): void {
173199
$asset_data['version'],
174200
true
175201
);
202+
203+
$this->assets_tools->add_inline_script(
204+
'theme-admin-editor-script',
205+
'const BFFEditorSettings = ' . wp_json_encode(
206+
apply_filters(
207+
'bff_editor_custom_settings',
208+
[
209+
'disableAllBlocksStyles' => [
210+
'core/separator',
211+
'core/quote',
212+
'core/pullquote',
213+
'core/table',
214+
'core/image',
215+
],
216+
'disabledBlocksStyles' => [
217+
// 'core/button' => [ 'outline' ]
218+
],
219+
'allowedBlocksVariations' => [
220+
'core/embed' => [ 'youtube', 'vimeo', 'dailymotion' ],
221+
],
222+
]
223+
)
224+
),
225+
'before'
226+
);
227+
176228
$this->assets_tools->enqueue_script( 'theme-admin-editor-script' );
177229
}
178230

inc/Tools/Assets.php

+13
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

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/* global BFFEditorSettings */
2+
3+
/* Customize BFFEditorSettings in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */
4+
15
import lazySizes from 'lazysizes'
26
import 'lazysizes/plugins/native-loading/ls.native-loading'
37
import 'lazysizes/plugins/object-fit/ls.object-fit'
@@ -15,14 +19,23 @@ lazySizes.cfg.nativeLoading = {
1519

1620
// Native Gutenberg
1721
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-
})
22+
// Disable specific block styles
23+
if (BFFEditorSettings.disabledBlocksStyles) {
24+
Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach(([block, styles]) => {
25+
unregisterBlockStyle(block, styles)
26+
})
27+
}
28+
29+
// Allow blocks variations
30+
if (BFFEditorSettings.allowedBlocksVariations) {
31+
Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach(([block, variations]) => {
32+
getBlockVariations(block).forEach((variant) => {
33+
if (!variations.includes(variant.name)) {
34+
unregisterBlockVariation(block, variant.name)
35+
}
36+
})
37+
})
38+
}
2639
})
2740

2841
// ACF Blocks
@@ -31,22 +44,9 @@ if (window.acf) {
3144
}
3245

3346
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
47+
// Disable all styles
48+
if (BFFEditorSettings.disableAllBlocksStyles && BFFEditorSettings.disableAllBlocksStyles.includes(name)) {
4549
settings.styles = []
46-
// set default aligment for images to null
47-
settings.attributes.align = {
48-
type: 'string',
49-
}
5050
}
5151

5252
return settings

0 commit comments

Comments
 (0)