PluginProbe
BeyondWords – AI audio for publishers / 4.7.0
BeyondWords – AI audio for publishers v4.7.0
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Component / Post / BlockAttributes / BlockAttributes.php

BlockAttributes.php in BeyondWords – AI audio for publishers 4.7.0, at src/Component/Post/BlockAttributes/BlockAttributes.php

114 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(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
14 namespace Beyondwords\Wordpress\Component\Post\BlockAttributes;
15
16 use Beyondwords\Wordpress\Component\Post\PostContentUtils;
17 use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
18 use Beyondwords\Wordpress\Component\Settings\PlayerUI\PlayerUI;
19
20 /**
21 * BlockAttributes setup
22 *
23 * @since 3.7.0
24 * @since 4.0.0 Renamed from BlockAudioAttribute to BlockAttributes to support multiple attributes
25 */
26 class 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 }
114