| 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 |
use Beyondwords\Wordpress\Core\CoreUtils; |
| 20 |
|
| 21 |
/** |
| 22 |
* BlockAttributes setup |
| 23 |
* |
| 24 |
* @since 3.7.0 |
| 25 |
* @since 4.0.0 Renamed from BlockAudioAttribute to BlockAttributes to support multiple attributes |
| 26 |
*/ |
| 27 |
class BlockAttributes |
| 28 |
{ |
| 29 |
/** |
| 30 |
* Constructor |
| 31 |
*/ |
| 32 |
public function __construct() |
| 33 |
{ |
| 34 |
add_filter('register_block_type_args', array($this, 'registerAudioAttribute')); |
| 35 |
add_filter('register_block_type_args', array($this, 'registerMarkerAttribute')); |
| 36 |
add_filter('render_block', array($this, 'renderBlock'), 10, 2); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register "Audio" attribute for Gutenberg blocks. |
| 41 |
*/ |
| 42 |
public function registerAudioAttribute($args) |
| 43 |
{ |
| 44 |
// Setup attributes if needed. |
| 45 |
if (! isset($args['attributes'])) { |
| 46 |
$args['attributes'] = array(); |
| 47 |
} |
| 48 |
|
| 49 |
if (! array_key_exists('beyondwordsAudio', $args['attributes'])) { |
| 50 |
$args['attributes']['beyondwordsAudio'] = array( |
| 51 |
'type' => 'boolean', |
| 52 |
'default' => true, |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
return $args; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Register "Segment marker" attribute for Gutenberg blocks. |
| 61 |
*/ |
| 62 |
public function registerMarkerAttribute($args) |
| 63 |
{ |
| 64 |
// Setup attributes if needed. |
| 65 |
if (! isset($args['attributes'])) { |
| 66 |
$args['attributes'] = array(); |
| 67 |
} |
| 68 |
|
| 69 |
if (! array_key_exists('beyondwordsMarker', $args['attributes'])) { |
| 70 |
$args['attributes']['beyondwordsMarker'] = array( |
| 71 |
'type' => 'string', |
| 72 |
'default' => '', |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
return $args; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Render block as HTML. |
| 81 |
* |
| 82 |
* Performs some checks and then attempts to add data-beyondwords-marker |
| 83 |
* attribute to the root element of Gutenberg blocks. |
| 84 |
* |
| 85 |
* @since 4.0.0 |
| 86 |
* @since 4.2.2 Rename method to renderBlock. |
| 87 |
* |
| 88 |
* @param string $blockContent The block content (HTML). |
| 89 |
* @param string $block The full block, including name and attributes. |
| 90 |
* |
| 91 |
* @return string Block Content (HTML). |
| 92 |
*/ |
| 93 |
public function renderBlock($blockContent, $block) |
| 94 |
{ |
| 95 |
// Skip adding marker if player UI is disabled |
| 96 |
if (get_option('beyondwords_player_ui', PlayerUI::ENABLED) === PlayerUI::DISABLED) { |
| 97 |
return $blockContent; |
| 98 |
} |
| 99 |
|
| 100 |
// Skip adding marker if no content ID exists |
| 101 |
if (! PostMetaUtils::getContentId(get_the_ID())) { |
| 102 |
return $blockContent; |
| 103 |
} |
| 104 |
|
| 105 |
$marker = $block['attrs']['beyondwordsMarker'] ?? ''; |
| 106 |
|
| 107 |
return PostContentUtils::addMarkerAttribute( |
| 108 |
$blockContent, |
| 109 |
$marker |
| 110 |
); |
| 111 |
} |
| 112 |
} |
| 113 |
|