Test Failed
Push — develop ( 85bacd...65d39e )
by Paul
08:58 queued 21s
created

Controller   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 31
c 1
b 0
f 0
dl 0
loc 105
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A filterAllowedBlockTypes() 0 6 2
A filterUseBlockEditor() 0 3 2
A registerBlocks() 0 6 1
A removeLegacyWidgets() 0 4 1
A enqueueBlockEditorAssets() 0 14 1
A filterBlockCategories() 0 7 1
A enqueueBlockAssets() 0 15 4
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Gutenberg;
4
5
use GeminiLabs\SiteReviews\Commands\EnqueuePublicAssets;
6
use GeminiLabs\SiteReviews\Controllers\AbstractController;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Integrations\Gutenberg\Blocks\SiteReviewBlock;
9
use GeminiLabs\SiteReviews\Integrations\Gutenberg\Blocks\SiteReviewsBlock;
10
use GeminiLabs\SiteReviews\Integrations\Gutenberg\Blocks\SiteReviewsFormBlock;
11
use GeminiLabs\SiteReviews\Integrations\Gutenberg\Blocks\SiteReviewsSummaryBlock;
12
use GeminiLabs\SiteReviews\Modules\Assets\AssetJs;
13
use GeminiLabs\SiteReviews\Modules\Style;
14
15
class Controller extends AbstractController
16
{
17
    /**
18
     * Ensure block styles are loaded on post types with blocks disabled.
19
     * 
20
     * @action wp_enqueue_scripts
21
     */
22
    public function enqueueBlockAssets(): void
23
    {
24
        if (!glsr()->filterBool('assets/css', true)) {
25
            return;
26
        }
27
        $blocks = [
28
            'core/button', 'core/search',
29
        ];
30
        $registry = \WP_Block_Type_Registry::get_instance();
31
        foreach ($blocks as $block) {
32
            if (!$registry->is_registered($block)) {
33
                continue;
34
            }
35
            $handles = $registry->get_registered($block)->style_handles;
36
            array_walk($handles, fn ($handle) => wp_enqueue_style($handle));
37
        }
38
    }
39
40
    /**
41
     * The CSS registered here will not load in the site editor unless it contains the .wp-block selector.
42
     *
43
     * @see https://github.com/WordPress/gutenberg/issues/41821
44
     *
45
     * @action enqueue_block_editor_assets
46
     */
47
    public function enqueueBlockEditorAssets(): void
48
    {
49
        // $dependencies = glsr()->filterArray('enqueue/public/dependencies', []);
50
        // wp_enqueue_script(glsr()->id, glsr(AssetJs::class)->url(), $dependencies, glsr(AssetJs::class)->version(), [
51
        //     'in_footer' => true,
52
        //     'strategy' => 'defer',
53
        // ]);
54
        wp_enqueue_style(
55
            glsr()->id.'/blocks',
56
            glsr(Style::class)->stylesheetUrl('blocks'),
57
            ['wp-edit-blocks'],
58
            glsr()->version
59
        );
60
        wp_add_inline_style(glsr()->id.'/blocks', (new EnqueuePublicAssets())->inlineStyles());
61
    }
62
63
    /**
64
     * @param bool|string[] $blockTypes
65
     *
66
     * @return bool|string[]
67
     *
68
     * @filter allowed_block_types_all
69
     */
70
    public function filterAllowedBlockTypes($blockTypes, \WP_Block_Editor_Context $context)
71
    {
72
        $postType = Arr::get($context, 'post.post_type');
73
        return glsr()->post_type !== $postType
74
            ? $blockTypes
75
            : [];
76
    }
77
78
    /**
79
     * @param array[] $categories
80
     *
81
     * @filter block_categories_all
82
     */
83
    public function filterBlockCategories(array $categories): array
84
    {
85
        $categories[] = [
86
            'slug' => glsr()->id,
87
            'title' => glsr()->name,
88
        ];
89
        return $categories;
90
    }
91
92
    /**
93
     * @filter use_block_editor_for_post_type
94
     */
95
    public function filterUseBlockEditor(bool $useBlockEditor, string $postType): bool
96
    {
97
        return glsr()->post_type !== $postType ? $useBlockEditor : false;
98
    }
99
100
    /**
101
     * @action init
102
     */
103
    public function registerBlocks(): void
104
    {
105
        glsr(SiteReviewBlock::class)->register();
106
        glsr(SiteReviewsBlock::class)->register();
107
        glsr(SiteReviewsFormBlock::class)->register();
108
        glsr(SiteReviewsSummaryBlock::class)->register();
109
    }
110
111
    /**
112
     * @param string[] $widgets
113
     *
114
     * @filter widget_types_to_hide_from_legacy_widget_block
115
     */
116
    public function removeLegacyWidgets(array $widgets): array
117
    {
118
        array_push($widgets, 'glsr_site-review', 'glsr_site-reviews', 'glsr_site-reviews-form', 'glsr_site-reviews-summary');
119
        return $widgets;
120
    }
121
}
122