Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
BlockAttributes
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 registerAudioAttribute
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 registerMarkerAttribute
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 renderBlock
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5/**
6 * BeyondWords support for Gutenberg blocks.
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   3.7.0
11 * @since   4.0.0 Renamed from BlockAudioAttribute.php to BlockAttributes.php to support multiple attributes
12 */
13
14namespace Beyondwords\Wordpress\Component\Post\BlockAttributes;
15
16use Beyondwords\Wordpress\Component\Post\PostContentUtils;
17use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
18use Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI\PlayerUI;
19
20/**
21 * BlockAttributes
22 *
23 * @since 3.7.0
24 * @since 4.0.0 Renamed from BlockAudioAttribute to BlockAttributes to support multiple attributes
25 */
26class BlockAttributes
27{
28    /**
29     * Init.
30     *
31     * @since 4.0.0
32     */
33    public function init()
34    {
35        add_filter('register_block_type_args', array($this, 'registerAudioAttribute'));
36        add_filter('register_block_type_args', array($this, 'registerMarkerAttribute'));
37        add_filter('render_block', array($this, 'renderBlock'), 10, 2);
38    }
39
40    /**
41     * Register "Audio" attribute for Gutenberg blocks.
42     */
43    public function registerAudioAttribute($args)
44    {
45        // Setup attributes if needed.
46        if (! isset($args['attributes'])) {
47            $args['attributes'] = array();
48        }
49
50        if (! array_key_exists('beyondwordsAudio', $args['attributes'])) {
51            $args['attributes']['beyondwordsAudio'] = array(
52                'type' => 'boolean',
53                'default' => true,
54            );
55        }
56
57        return $args;
58    }
59
60    /**
61     * Register "Segment marker" attribute for Gutenberg blocks.
62     */
63    public function registerMarkerAttribute($args)
64    {
65        // Setup attributes if needed.
66        if (! isset($args['attributes'])) {
67            $args['attributes'] = array();
68        }
69
70        if (! array_key_exists('beyondwordsMarker', $args['attributes'])) {
71            $args['attributes']['beyondwordsMarker'] = array(
72                'type' => 'string',
73                'default' => '',
74            );
75        }
76
77        return $args;
78    }
79
80    /**
81     * Render block as HTML.
82     *
83     * Performs some checks and then attempts to add data-beyondwords-marker
84     * attribute to the root element of Gutenberg blocks.
85     *
86     * @since 4.0.0
87     * @since 4.2.2 Rename method to renderBlock.
88     *
89     * @param string $blockContent The block content (HTML).
90     * @param string $block        The full block, including name and attributes.
91     *
92     * @return string Block Content (HTML).
93     */
94    public function renderBlock($blockContent, $block)
95    {
96        // Skip adding marker if player UI is disabled
97        if (get_option('beyondwords_player_ui', PlayerUI::ENABLED) === PlayerUI::DISABLED) {
98            return $blockContent;
99        }
100
101        // Skip adding marker if no content ID exists
102        if (! PostMetaUtils::getContentId(get_the_ID())) {
103            return $blockContent;
104        }
105
106        $marker = $block['attrs']['beyondwordsMarker'] ?? '';
107
108        return PostContentUtils::addMarkerAttribute(
109            $blockContent,
110            $marker
111        );
112    }
113}