Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

Commit 64307c1

Browse files
committed
✨ Reduce overhead
1 parent 31bd254 commit 64307c1

3 files changed

Lines changed: 269 additions & 105 deletions

File tree

src/BaseServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace TinyPixel\BlockCompose;
44

5-
use Roots\Acorn\ServiceProvider;
5+
use function \Roots\config;
6+
use \Roots\Acorn\ServiceProvider;
67

78
class BaseServiceProvider extends ServiceProvider
89
{

src/BlockComposeServiceProvider.php

Lines changed: 260 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use \TinyPixel\BlockCompose\BaseServiceProvider;
66

7+
use function \Roots\asset;
8+
use function \Roots\config;
9+
710
class BlockComposeServiceProvider extends BaseServiceProvider
811
{
912
/**
@@ -19,64 +22,79 @@ public function register()
1922
*/
2023
public function boot()
2124
{
22-
$this->processEditorScripts(
23-
collect(config('editor.block_editor_scripts'))
24-
);
25-
26-
$this->processScripts(
27-
collect(config('editor.scripts'))
28-
);
29-
30-
$this->processEditorStyles(
31-
collect(config('editor.block_editor_styles'))
32-
);
33-
34-
$this->processStyles(
35-
collect(config('editor.styles'))
36-
);
37-
38-
$this->registerBlockCategories(
39-
collect(config('editor.block_categories'))
40-
);
41-
42-
add_theme_support('editor-color-palette', collect(config('editor.color_palette'))->toArray());
25+
$this->readConfig();
26+
$this->setAssets();
27+
$this->setOptions();
28+
add_action('init', [$this, 'setReusableBlockOptions']);
29+
add_action('rest_init', [$this, 'setEndpoints']);
30+
}
4331

44-
if (config('editor.disable_color_palette')==true) {
45-
add_theme_support('disable-custom-colors');
46-
}
32+
public function setOptions()
33+
{
34+
! $this->categories->isEmpty() ? $this->setCategories() : null;
35+
! $this->color_palette->isEmpty() ? $this->setColorPalette() : null;
36+
! $this->font_sizes->isEmpty() ? $this->setFontSizes() : null;
4737

48-
add_theme_support('editor-font-sizes', collect(config('editor.font_sizes'))->toArray());
38+
$this->disable_font_sizes ? $this->disableCustomFontSizes() : null;
39+
$this->disable_custom_colors ? $this->disableCustomColors() : null;
40+
$this->supports_wide_align ? $this->enableWideAlign() : null;
41+
$this->uses_default_styles ? $this->enableDefaultStyles() : null;
42+
$this->supports_editor_styles ? $this->enableEditorStyle() : null;
43+
$this->supports_dark_styles ? $this->enableDarkEditorStyle() : null;
44+
$this->responsive_embeds ? $this->enableResponsiveEmbeds() : null;
45+
$this->expose_to_graphql ? $this->enableGraphQL() : null;
46+
$this->script_debug ? $this->enableScriptDebug() : null;
47+
}
4948

50-
if (config('editor.disable_color_palette')==true) {
51-
add_theme_support('disable-custom-font-sizes');
52-
}
49+
public function setAssets()
50+
{
51+
! $this->editor_scripts->isEmpty()
52+
? add_action('init', [$this, 'setEditorScripts'])
53+
: null;
5354

54-
if (config('editor.supports_wide_alignments')==true) {
55-
add_theme_support('align-wide');
56-
}
55+
! $this->editor_styles->isEmpty()
56+
? add_action('enqueue_block_editor_assets', [$this, 'setEditorStyles'])
57+
: null;
5758

58-
if (config('editor.default_block_styles')==true) {
59-
add_theme_support('wp-block-styles');
60-
}
59+
! $this->styles->isEmpty()
60+
? add_action('wp_enqueue_scripts', [$this, 'setStyles'])
61+
: null;
6162

62-
if (config('editor.editor_styles')==true) {
63-
add_theme_support('editor-styles');
64-
}
63+
! $this->scripts->isEmpty()
64+
? add_action('wp_enqueue_scripts', [$this, 'setScripts'])
65+
: null;
66+
}
6567

66-
if (config('editor.dark_editor_styles')==true) {
67-
add_theme_support('dark-editor-style');
68-
}
6968

70-
if (config('responsive_embeds')==true) {
71-
add_theme_support('responsive-embeds');
72-
}
69+
/**
70+
* Read configuration file
71+
*/
72+
public function readConfig()
73+
{
74+
$this->editor_scripts = collect(config('editor.block_editor_scripts'));
75+
$this->editor_plugins = collect(config('editor.editor_plugin_scripts'));
76+
$this->scripts = collect(config('editor.scripts'));
77+
$this->styles = collect(config('editor.styles'));
78+
$this->editor_styles = collect(config('editor.block_editor_styles'));
79+
$this->categories = collect(config('editor.block_categories'));
80+
$this->color_palette = collect(config('editor.color_palette'));
81+
$this->font_sizes = collect(config('editor.font_sizes'));
7382

74-
add_action('admin_print_scripts', function () {
75-
if (\Roots\config('editor.debug')==true) {
76-
global $wp_scripts;
77-
dd($wp_scripts);
78-
}
79-
});
83+
$this->disable_font_sizes = config('editor.disable_custom_font_sizes');
84+
$this->disable_custom_colors = config('editor.disable_custom_color_palette');
85+
$this->supports_wide_align = config('editor.supports_wide_alignments');
86+
$this->uses_default_styles = config('editor.default_block_styles');
87+
$this->supports_editor_styles = config('editor.editor_styles');
88+
$this->supports_dark_styles = config('editor.dark_editor_styles');
89+
$this->responsive_embeds = config('editor.responsive_embeds');
90+
$this->script_debug = config('editor.debug');
91+
$this->reusable_unlock = config('editor.reusable_blocks_unlock');
92+
$this->reusable_icon = config('editor.reusable_blocks_icon');
93+
$this->reusable_block_labels = config('editor.reusable_blocks_labels');
94+
$this->reusable_custom_fields = config('editor.reusable_blocks_enable_custom_fields');
95+
$this->reusable_cap_type = config('editor.reusable_blocks_capability_type');
96+
$this->reusable_caps = config('editor.reusable_blocks_capabilities');
97+
$this->expose_to_graphql = config('editor.reusable_blocks_expose_to_graphql');
8098
}
8199

82100
/**
@@ -89,51 +107,81 @@ public function withBound()
89107
}
90108
}
91109

92-
public function processEditorScripts($scripts)
110+
/**
111+
* wp register script calls
112+
*/
113+
public function setEditorScripts()
93114
{
94-
add_action('init', function () use ($scripts) {
95-
$scripts->each(function ($script) {
96-
wp_register_script(
97-
$script['name'],
98-
asset($script['file'])->uri(),
99-
['wp-editor', 'wp-element', 'wp-blocks'],
100-
'',
101-
null,
102-
true,
103-
);
104-
});
115+
$deps = [
116+
'wp-editor',
117+
'wp-element',
118+
'wp-blocks'
119+
];
120+
121+
$plugin_deps = [
122+
'wp-editor',
123+
'wp-element',
124+
'wp-plugins',
125+
'wp-dom-ready',
126+
'wp-edit-post'
127+
];
128+
129+
$this->editor_scripts->each(function ($script) use ($deps) {
130+
wp_register_script(
131+
$script['name'],
132+
asset($script['file'])->uri(),
133+
$deps,
134+
'',
135+
null,
136+
true
137+
);
138+
});
139+
140+
$this->editor_plugins->each(function ($script) use ($plugin_deps) {
141+
wp_enqueue_script(
142+
$script['name'],
143+
asset($script['file'])->uri(),
144+
$plugin_deps,
145+
'',
146+
null,
147+
true
148+
);
105149
});
106150
}
107151

108-
public function processEditorStyles($styles)
152+
/**
153+
* wp enqueue style calls
154+
*/
155+
public function setEditorStyles()
109156
{
110-
add_action('enqueue_block_editor_assets', function () use ($styles) {
111-
$styles->each(function ($style) {
112-
wp_enqueue_style(
113-
$style['name'],
114-
asset($style['file'])->uri(),
115-
false,
116-
null,
117-
'all',
118-
);
119-
});
157+
$this->editor_styles->each(function ($style) {
158+
// wp_enqueue_style($style['name'], asset($style['file'])->uri(), false, null, 'all');
120159
});
121160
}
122161

123-
public function processScripts()
162+
/**
163+
* Enqueue frontend scripts
164+
*/
165+
public function setScripts()
124166
{
125167
// ...
126168
}
127169

128-
public function processStyles()
170+
/**
171+
* Enqueue frontend styles
172+
*/
173+
public function setStyles()
129174
{
130175
// ...
131176
}
132177

133-
public function registerBlockCategories($newCategories)
178+
/**
179+
* Add inserter categories
180+
*/
181+
public function setCategories()
134182
{
135-
add_filter('block_categories', function ($categories, $post) use ($newCategories) {
136-
collect(config('editor.block_categories'))->each(function ($category) {
183+
add_filter('block_categories', function ($categories, $post) {
184+
$this->categories->each(function ($category) {
137185
$categories[] = [
138186
'slug' => $category['slug'],
139187
'title' => $category['title'],
@@ -144,4 +192,139 @@ public function registerBlockCategories($newCategories)
144192
return $categories;
145193
}, 10, 2);
146194
}
195+
196+
/**
197+
* Set color palette
198+
*/
199+
public function setColorPalette()
200+
{
201+
add_theme_support('editor-color-palette', $this->color_palette->toArray());
202+
}
203+
204+
/**
205+
* Set font sizes
206+
*/
207+
public function setFontSizes()
208+
{
209+
add_theme_support('editor-font-sizes', $this->font_sizes->toArray());
210+
}
211+
212+
/**
213+
* Disable custom font sizes
214+
*/
215+
public function disableCustomFontSizes()
216+
{
217+
add_theme_support('disable-custom-font-sizes');
218+
}
219+
220+
/**
221+
* Disable custom colors
222+
*/
223+
public function disableCustomColors()
224+
{
225+
add_theme_support('disable-custom-colors');
226+
}
227+
228+
/**
229+
* Enable special alignment options
230+
*/
231+
public function enableWideAlign()
232+
{
233+
add_theme_support('align-wide');
234+
}
235+
236+
/**
237+
* Enable default styles
238+
*/
239+
public function enableDefaultStyles()
240+
{
241+
add_theme_support('wp-block-styles');
242+
}
243+
244+
/**
245+
* Enable editor style
246+
*/
247+
public function enableEditorStyle()
248+
{
249+
add_theme_support('editor-styles');
250+
}
251+
252+
/**
253+
* Enable dark editor style
254+
*/
255+
public function enableDarkEditorStyle()
256+
{
257+
add_theme_support('dark-editor-style');
258+
}
259+
260+
/**
261+
* Enable responsive embeds
262+
*/
263+
public function enableResponsiveEmbeds()
264+
{
265+
add_theme_support('responsive-embeds');
266+
}
267+
268+
/**
269+
* Set reusable block options
270+
*/
271+
public function setReusableBlockOptions()
272+
{
273+
$this->type = get_post_type_object('wp_block');
274+
275+
if ($this->reusable_unlock==true) {
276+
$this->type->_builtin = false;
277+
$this->type->show_in_menu = true;
278+
}
279+
280+
if ($this->reusable_icon) {
281+
$this->type->menu_icon = $this->reusable_icon;
282+
}
283+
284+
if ($this->reusable_block_labels) {
285+
$this->type->labels = (object) array_merge(
286+
$this->reusable_block_labels,
287+
(array) $this->type->labels
288+
);
289+
}
290+
291+
if ($this->reusable_custom_fields==true) {
292+
$this->type->supports = [
293+
'title',
294+
'editor',
295+
'custom-fields',
296+
];
297+
}
298+
299+
if ($this->reusable_cap_type) {
300+
$this->type->capability_type = $this->reusable_cap_type;
301+
}
302+
303+
if ($this->reusable_caps) {
304+
$this->type->capabilities = $this->reusable_caps;
305+
}
306+
}
307+
308+
public function enableScriptDebug()
309+
{
310+
add_action('admin_print_scripts', function () {
311+
if ($this->script_debug==true) {
312+
global $wp_scripts;
313+
dd($wp_scripts);
314+
}
315+
});
316+
}
317+
318+
public function enableGraphQL()
319+
{
320+
add_filter('register_post_type_args', function ($args, $post_type) {
321+
if ('wp_block' === $post_type) {
322+
$args['show_in_graphql'] = true;
323+
$args['graphql_single_name'] = $this->reusable_block_labels['singular_name'];
324+
$args['graphql_plural_name'] = $this->reusable_block_labels['name'];
325+
}
326+
327+
return $args;
328+
}, 10, 2);
329+
}
147330
}

0 commit comments

Comments
 (0)