Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 5 additions & 48 deletions src/Component/Post/BlockAttributes/BlockAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@

namespace Beyondwords\Wordpress\Component\Post\BlockAttributes;

use Beyondwords\Wordpress\Component\Post\PostContentUtils;
use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
use Beyondwords\Wordpress\Component\Settings\Fields\PlayerUI\PlayerUI;

/**
* BlockAttributes
*
* @since 3.7.0
* @since 4.0.0 Renamed from BlockAudioAttribute to BlockAttributes to support multiple attributes
* @since 4.0.0 Renamed from BlockAudioAttribute to BlockAttributes to support multiple attributes.
* @since 6.0.0 Stop adding beyondwordsMarker attribute to blocks.
*/
class BlockAttributes
{
/**
* Init.
*
* @since 4.0.0
* @since 6.0.0 Make static.
* @since 6.0.0 Make static and remove renderBlock registration.
*/
public static function init()
{
add_filter('register_block_type_args', [self::class, 'registerAudioAttribute']);
add_filter('register_block_type_args', [self::class, 'registerMarkerAttribute']);
add_filter('render_block', [self::class, 'renderBlock'], 10, 2);
}

/**
Expand Down Expand Up @@ -63,6 +59,8 @@ public static function registerAudioAttribute($args)
/**
* Register "Segment marker" attribute for Gutenberg blocks.
*
* @deprecated This attribute is no longer used as of 6.0.0, but kept for backward compatibility.
*
* @since 6.0.0 Make static.
*/
public static function registerMarkerAttribute($args)
Expand All @@ -81,45 +79,4 @@ public static function registerMarkerAttribute($args)

return $args;
}

/**
* Render block as HTML.
*
* Performs some checks and then attempts to add data-beyondwords-marker
* attribute to the root element of Gutenberg blocks.
*
* @since 4.0.0
* @since 4.2.2 Rename method to renderBlock.
* @since 6.0.0 Make static and update for Magic Embed.
*
* @param string $blockContent The block content (HTML).
* @param string $block The full block, including name and attributes.
*
* @return string Block Content (HTML).
*/
public static function renderBlock($blockContent, $block)
{
// Skip adding marker if player UI is disabled
if (get_option(PlayerUI::OPTION_NAME) === PlayerUI::DISABLED) {
return $blockContent;
}

$postId = get_the_ID();

if (! $postId) {
return $blockContent;
}

// Skip adding marker if no content exists
if (! PostMetaUtils::hasContent($postId)) {
return $blockContent;
}

$marker = $block['attrs']['beyondwordsMarker'] ?? '';

return PostContentUtils::addMarkerAttribute(
$blockContent,
$marker
);
}
}
37 changes: 10 additions & 27 deletions src/Component/Post/BlockAttributes/addAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
import { addFilter } from '@wordpress/hooks';

/**
* External dependencies
* Internal dependencies
*/
import getBlockMarkerAttribute from './helpers/getBlockMarkerAttribute';
import { isBeyondwordsSupportedBlock } from './isBeyondwordsSupportedBlock';

/**
* Register custom block attributes for BeyondWords.
*
* @since 4.0.4 Remove settings.attributes undefined check, to match official docs.
* @since 6.0.1 Skip internal/UI blocks to prevent breaking the block inserter.
*
* @param {Object} settings Settings for the block.
* @param {string} name Block name.
*
* @return {Object} settings Modified settings.
*/
function addAttributes( settings ) {
function addAttributes( settings, name ) {
// Only add attributes to content blocks
if ( ! isBeyondwordsSupportedBlock( name ) ) {
return settings;
}

return {
...settings,
attributes: {
Expand All @@ -39,27 +46,3 @@ addFilter(
'beyondwords/beyondwords-block-attributes',
addAttributes
);

/**
* Set a unique BeyondWords marker for each block that doesn't already have one.
*
* @param {Object} attributes Attributes for the block.
*
* @return {Object} attributes Modified attributes.
*/
function setMarkerAttribute( attributes ) {
const marker = getBlockMarkerAttribute( attributes );

attributes = {
...attributes,
beyondwordsMarker: marker,
};

return attributes;
}

addFilter(
'blocks.getBlockAttributes',
'beyondwords/set-marker-attribute',
setMarkerAttribute
);
97 changes: 39 additions & 58 deletions src/Component/Post/BlockAttributes/addControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,40 @@ import { InspectorControls, BlockControls } from '@wordpress/block-editor';
import {
PanelBody,
PanelRow,
TextControl,
ToggleControl,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useEffect } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';

/**
* External dependencies
*/
import getBlockMarkerAttribute from './helpers/getBlockMarkerAttribute';

/**
* Internal dependencies
*/
import BlockAttributesCheck from './check';
import { isBeyondwordsSupportedBlock } from './isBeyondwordsSupportedBlock';

/**
* Add BeyondWords controls to Gutenberg Blocks.
*
* @since 6.0.1 Skip internal/UI blocks to prevent breaking the block inserter.
*
* @param {Function} BlockEdit Block edit component.
*
* @return {Function} BlockEdit Modified block edit component.
*/
const withBeyondwordsBlockControls = createHigherOrderComponent(
( BlockEdit ) => {
return ( props ) => {
const { attributes, setAttributes } = props;
const { name } = props;

useEffect( () => {
setAttributes( {
beyondwordsMarker: getBlockMarkerAttribute( attributes ),
} );
}, [] );
// Skip blocks that shouldn't have controls
// Do this check BEFORE accessing attributes to avoid unnecessary processing
if ( ! isBeyondwordsSupportedBlock( name ) ) {
return <BlockEdit { ...props } />;
}

const { beyondwordsAudio, beyondwordsMarker } = attributes;
const { attributes, setAttributes } = props;
const { beyondwordsAudio } = attributes;

const icon = !! beyondwordsAudio
? 'controls-volumeon'
Expand All @@ -57,56 +53,41 @@ const withBeyondwordsBlockControls = createHigherOrderComponent(
? __( 'Audio processing enabled', 'speechkit' )
: __( 'Audio processing disabled', 'speechkit' );

const toggleBeyondwordsAudio = () =>
const toggleBeyondwordsAudio = () => {
setAttributes( { beyondwordsAudio: ! beyondwordsAudio } );
};

return (
<>
<BlockEdit { ...props } />

<BlockAttributesCheck>
<InspectorControls>
<PanelBody
icon="controls-volumeon"
title={ __( 'BeyondWords', 'speechkit' ) }
initialOpen={ true }
>
<PanelRow>
<ToggleControl
label={ toggleLabel }
checked={ !! beyondwordsAudio }
onChange={ toggleBeyondwordsAudio }
__nextHasNoMarginBottom
/>
</PanelRow>
{ !! beyondwordsAudio && (
<PanelRow>
<TextControl
label={ __(
'Segment marker',
'speechkit'
) }
value={ beyondwordsMarker }
disabled
readOnly
__nextHasNoMarginBottom
/>
</PanelRow>
) }
</PanelBody>
</InspectorControls>

<BlockControls>
<ToolbarGroup>
<ToolbarButton
icon={ icon }
label={ buttonLabel }
className="components-toolbar__control"
onClick={ toggleBeyondwordsAudio }
<InspectorControls>
<PanelBody
icon="controls-volumeon"
title={ __( 'BeyondWords', 'speechkit' ) }
initialOpen={ true }
>
<PanelRow>
<ToggleControl
label={ toggleLabel }
checked={ !! beyondwordsAudio }
onChange={ toggleBeyondwordsAudio }
__nextHasNoMarginBottom
/>
</ToolbarGroup>
</BlockControls>
</BlockAttributesCheck>
</PanelRow>
</PanelBody>
</InspectorControls>

<BlockControls>
<ToolbarGroup>
<ToolbarButton
icon={ icon }
label={ buttonLabel }
className="components-toolbar__control"
onClick={ toggleBeyondwordsAudio }
/>
</ToolbarGroup>
</BlockControls>
</>
);
};
Expand Down

This file was deleted.

Loading
Loading