Test Failed
Push — main ( 2f7161...bda96f )
by Paul
11:42 queued 05:44
created

BlocksController::removeLegacyWidgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Commands\EnqueuePublicAssets;
6
use GeminiLabs\SiteReviews\Commands\RegisterBlocks;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Style;
9
10
class BlocksController extends AbstractController
11
{
12
    /**
13
     * @param bool|string[] $blockTypes
14
     *
15
     * @return bool|string[]
16
     *
17
     * @filter allowed_block_types_all
18
     */
19
    public function filterAllowedBlockTypes($blockTypes, \WP_Block_Editor_Context $context)
20
    {
21
        $postType = Arr::get($context, 'post.post_type');
22
        return glsr()->post_type !== $postType
23
            ? $blockTypes
24
            : [];
25
    }
26
27
    /**
28
     * @param array[] $categories
29
     *
30
     * @filter block_categories_all
31
     */
32
    public function filterBlockCategories(array $categories): array
33
    {
34
        $categories[] = [
35
            'icon' => null,
36
            'slug' => glsr()->id,
37
            'title' => glsr()->name,
38
        ];
39
        return $categories;
40
    }
41
42
    /**
43
     * @filter use_block_editor_for_post_type
44
     */
45
    public function filterUseBlockEditor(bool $useBlockEditor, string $postType): bool
46
    {
47
        return glsr()->post_type !== $postType ? $useBlockEditor : false;
48
    }
49
50
    /**
51
     * @action init
52
     */
53
    public function registerAssets(): void
54
    {
55
        global $pagenow;
56
        wp_register_style(
57
            glsr()->id.'/blocks',
58
            glsr(Style::class)->stylesheetUrl('blocks'),
59
            ['wp-edit-blocks'],
60
            glsr()->version
61
        );
62
        wp_add_inline_style(glsr()->id.'/blocks', (new EnqueuePublicAssets())->inlineStyles());
63
        $handle = glsr()->id.'/blocks';
64
        $url = glsr()->url('assets/scripts/'.glsr()->id.'-blocks.js');
65
        $deps = [
66
            glsr()->id.'/admin',
67
            'wp-block-editor',
68
            'wp-blocks',
69
            'wp-i18n',
70
            'wp-element',
71
        ];
72
        wp_register_script($handle, $url, $deps, glsr()->version, [
73
            'strategy' => 'defer',
74
        ]);
75
    }
76
77
    /**
78
     * @action init
79
     */
80
    public function registerBlocks(): void
81
    {
82
        $this->execute(new RegisterBlocks());
83
    }
84
85
    /**
86
     * @param string[] $widgets
87
     *
88
     * @filter widget_types_to_hide_from_legacy_widget_block
89
     */
90
    public function removeLegacyWidgets(array $widgets): array
91
    {
92
        array_push($widgets, 'glsr_site-review', 'glsr_site-reviews', 'glsr_site-reviews-form', 'glsr_site-reviews-summary');
93
        return $widgets;
94
    }
95
}
96