| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords support for Gutenberg blocks. |
| 7 |
* |
| 8 |
* @package BeyondWords\Editor\Components |
| 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 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace BeyondWords\Editor\Components; |
| 16 |
|
| 17 |
/** |
| 18 |
* BlockAttributes |
| 19 |
* |
| 20 |
* @since 3.7.0 |
| 21 |
* @since 4.0.0 Renamed from BlockAudioAttribute to BlockAttributes to support multiple attributes. |
| 22 |
* @since 6.0.0 Stop adding beyondwordsMarker attribute to blocks. |
| 23 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 24 |
*/ |
| 25 |
defined( 'ABSPATH' ) || exit; |
| 26 |
|
| 27 |
class BlockAttributes { |
| 28 |
|
| 29 |
/** |
| 30 |
* Init. |
| 31 |
* |
| 32 |
* @since 4.0.0 |
| 33 |
* @since 6.0.0 Make static and remove renderBlock registration. |
| 34 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 35 |
*/ |
| 36 |
public static function init() { |
| 37 |
add_filter( 'register_block_type_args', [ self::class, 'register_audio_attribute'] ); |
| 38 |
add_filter( 'register_block_type_args', [ self::class, 'register_marker_attribute'] ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Register "Audio" attribute for Gutenberg blocks. |
| 43 |
* |
| 44 |
* @since 6.0.0 Make static. |
| 45 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 46 |
*/ |
| 47 |
public static function register_audio_attribute( $args ) { |
| 48 |
if ( ! isset( $args['attributes'] ) ) { |
| 49 |
$args['attributes'] = []; |
| 50 |
} |
| 51 |
|
| 52 |
if ( ! array_key_exists( 'beyondwordsAudio', $args['attributes'] ) ) { |
| 53 |
$args['attributes']['beyondwordsAudio'] = [ |
| 54 |
'type' => 'boolean', |
| 55 |
'default' => true, |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
return $args; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register "Segment marker" attribute for Gutenberg blocks. |
| 64 |
* |
| 65 |
* @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 66 |
* |
| 67 |
* @deprecated This attribute is no longer used as of 6.0.0, but kept for backward compatibility. |
| 68 |
* |
| 69 |
* @since 6.0.0 Make static. |
| 70 |
*/ |
| 71 |
public static function register_marker_attribute( $args ) { |
| 72 |
if ( ! isset( $args['attributes'] ) ) { |
| 73 |
$args['attributes'] = []; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! array_key_exists( 'beyondwordsMarker', $args['attributes'] ) ) { |
| 77 |
$args['attributes']['beyondwordsMarker'] = [ |
| 78 |
'type' => 'string', |
| 79 |
'default' => '', |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
return $args; |
| 84 |
} |
| 85 |
} |
| 86 |
|