Test Failed
Push — develop ( 907276...bdb629 )
by Paul
10:55
created

Controller   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 12
eloc 25
c 3
b 0
f 1
dl 0
loc 90
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A filterAllowedBlockTypes() 0 6 2
A enqueueBlockEditorAssets() 0 3 1
A filterBlockCategories() 0 7 1
A filterBlockGeneratedClassname() 0 9 3
A filterUseBlockEditor() 0 3 2
A registerBlocks() 0 12 2
A removeLegacyWidgets() 0 4 1
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
13
class Controller extends AbstractController
14
{
15
    /**
16
     * @action enqueue_block_editor_assets
17
     */
18
    public function enqueueBlockEditorAssets(): void
19
    {
20
        $this->execute(new EnqueuePublicAssets());
21
    }
22
23
    /**
24
     * @param bool|string[] $blockTypes
25
     *
26
     * @return bool|string[]
27
     *
28
     * @filter allowed_block_types_all
29
     */
30
    public function filterAllowedBlockTypes($blockTypes, \WP_Block_Editor_Context $context)
31
    {
32
        $postType = Arr::get($context, 'post.post_type');
33
        return glsr()->post_type !== $postType
34
            ? $blockTypes
35
            : [];
36
    }
37
38
    /**
39
     * @param array[] $categories
40
     *
41
     * @filter block_categories_all
42
     */
43
    public function filterBlockCategories(array $categories): array
44
    {
45
        $categories[] = [
46
            'slug' => glsr()->id,
47
            'title' => glsr()->name,
48
        ];
49
        return $categories;
50
    }
51
52
    /**
53
     * @param string $className The current applied classname.
54
     * @param string $blockName The block name.
55
     *
56
     * @filter block_default_classname
57
     */
58
    public function filterBlockGeneratedClassname($className, $blockName): string
59
    {
60
        if ('site-reviews/review' === $blockName) {
61
            return 'wp-block-site-review';
62
        }
63
        if ('site-reviews/reviews' === $blockName) {
64
            return 'wp-block-site-reviews';
65
        }
66
        return $className;
67
    }
68
69
    /**
70
     * @filter use_block_editor_for_post_type
71
     */
72
    public function filterUseBlockEditor(bool $useBlockEditor, string $postType): bool
73
    {
74
        return glsr()->post_type !== $postType ? $useBlockEditor : false;
75
    }
76
77
    /**
78
     * @action init
79
     */
80
    public function registerBlocks(): void
81
    {
82
        if (function_exists('wp_register_block_metadata_collection')) { // WP 6.7
83
            wp_register_block_metadata_collection(
84
                glsr()->path('assets/blocks', false),
85
                glsr()->path('assets/blocks/blocks-manifest.php')
86
            );
87
        }
88
        glsr(SiteReviewBlock::class)->register();
89
        glsr(SiteReviewsBlock::class)->register();
90
        glsr(SiteReviewsFormBlock::class)->register();
91
        glsr(SiteReviewsSummaryBlock::class)->register();
92
    }
93
94
    /**
95
     * @param string[] $widgets
96
     *
97
     * @filter widget_types_to_hide_from_legacy_widget_block
98
     */
99
    public function removeLegacyWidgets(array $widgets): array
100
    {
101
        array_push($widgets, 'glsr_site-review', 'glsr_site-reviews', 'glsr_site-reviews-form', 'glsr_site-reviews-summary');
102
        return $widgets;
103
    }
104
}
105