| 1 |
<?php |
| 2 |
/** |
| 3 |
* Extend_Attributes |
| 4 |
* |
| 5 |
* Extends block metadata to include custom attributes for the plugin. |
| 6 |
* Adds 'customCss', 'blockClientId', and plugin-specific styles attributes to all blocks. |
| 7 |
* |
| 8 |
* @package gutenify |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace gutenify; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Extend_Attributes |
| 17 |
* |
| 18 |
* Adds additional attributes to block metadata for enhanced customization. |
| 19 |
*/ |
| 20 |
class Extend_Attributes { |
| 21 |
|
| 22 |
/** |
| 23 |
* Register the extend_attributes method on the 'block_type_metadata' action. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
add_action( 'block_type_metadata', array( $this, 'extend_attributes' ), 1 ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Extend block attributes if not already set. |
| 31 |
* |
| 32 |
* Adds: |
| 33 |
* - 'customCss': For custom CSS input. |
| 34 |
* - 'blockClientId': For storing the block's unique client ID. |
| 35 |
* - '<plugin>Styles': For plugin-specific styles. |
| 36 |
* |
| 37 |
* @param array $metadata Block metadata array. |
| 38 |
* @return array Modified block metadata. |
| 39 |
*/ |
| 40 |
public function extend_attributes( $metadata ) { |
| 41 |
$constants = Helpers::plugin_constants(); |
| 42 |
$plugin_main_camel_case_name = $constants['plugin_main_camel_case_name']; |
| 43 |
|
| 44 |
// Add customCss attribute if not present. |
| 45 |
if ( ! isset( $metadata['attributes']['customCss'] ) ) { |
| 46 |
$metadata['attributes']['customCss'] = array( |
| 47 |
'type' => 'string', |
| 48 |
'default' => '', |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
// Add blockClientId attribute if not present. |
| 53 |
if ( ! isset( $metadata['attributes']['blockClientId'] ) ) { |
| 54 |
$metadata['attributes']['blockClientId'] = array( |
| 55 |
'type' => 'string', |
| 56 |
'default' => '', |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
// Add plugin-specific styles attribute if not present. |
| 61 |
if ( ! isset( $metadata['attributes'][ $plugin_main_camel_case_name . 'Styles' ] ) ) { |
| 62 |
$metadata['attributes'][ $plugin_main_camel_case_name . 'Styles' ] = array( |
| 63 |
'type' => 'string', |
| 64 |
'default' => '', |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
// Add animation settings attribute if not present. |
| 69 |
if ( ! isset( $metadata['attributes']['animationsSettings'] ) ) { |
| 70 |
$metadata['attributes']['animationsSettings'] = array( |
| 71 |
'type' => 'object', |
| 72 |
'default' => array( |
| 73 |
'duration' => 1000, |
| 74 |
), |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
return $metadata; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Initialize the attribute extender. |
| 83 |
new Extend_Attributes(); |
| 84 |
|