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

Controller::filterBlockGeneratedClassname()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 2
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