| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dynamic Block Classname Handler |
| 4 |
* |
| 5 |
* This file contains the `Dynamic_Block_Classname` class, which allows the dynamic addition |
| 6 |
* of CSS classnames or inline styles to Gutenberg blocks during rendering. |
| 7 |
* |
| 8 |
* @package gutenify |
| 9 |
* @subpackage Functions |
| 10 |
* @since 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace gutenify; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; // Prevent direct access to the file. |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Dynamic_Block_Classname. |
| 19 |
* |
| 20 |
* Handles dynamic addition of CSS classnames to Gutenberg blocks. |
| 21 |
*/ |
| 22 |
class Dynamic_Block_Classname { |
| 23 |
|
| 24 |
/** |
| 25 |
* Initializes the class by hooking into 'render_block'. |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
add_filter( 'render_block', array( __CLASS__, 'render_block' ), 10, 3 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Hook callback for modifying block output during rendering. |
| 33 |
* |
| 34 |
* @param string $block_content The original block HTML. |
| 35 |
* @param array $block The block settings. |
| 36 |
* @param object $instance The block instance (for reusable blocks). |
| 37 |
* @return string Modified block HTML after applying custom filters. |
| 38 |
*/ |
| 39 |
public static function render_block( $block_content, $block, $instance ) { |
| 40 |
// Skip processing for empty blocks or specific block types. |
| 41 |
if ( empty( $block['blockName'] ) || in_array( $block['blockName'], array( 'structured-content/faq-item', 'structured-content/faq' ), true ) ) { |
| 42 |
return $block_content; |
| 43 |
} |
| 44 |
|
| 45 |
// Generate a unique CSS class ID for this block. |
| 46 |
$block_id = wp_unique_id( 'gtfy-' ); |
| 47 |
|
| 48 |
$html_processor = new \WP_HTML_Tag_Processor( $block_content ); |
| 49 |
if ( $html_processor->next_tag() ) { |
| 50 |
$html_processor->add_class( $block_id ); |
| 51 |
} |
| 52 |
|
| 53 |
// Enqueue dynamic styles if the block is valid. |
| 54 |
if ( is_array( $block ) && ! empty( $block ) ) { |
| 55 |
self::enqueue_dyanmic_style( $block, $block_id, $instance ); |
| 56 |
} |
| 57 |
|
| 58 |
// Get the updated HTML content. |
| 59 |
$updated_html = $html_processor->get_updated_html(); |
| 60 |
|
| 61 |
// Apply general and block-specific filters before returning. |
| 62 |
$filter_prefix = 'gutenify_render_block'; |
| 63 |
$updated_html = apply_filters( $filter_prefix, $updated_html, $block, $instance, $block_id ); |
| 64 |
return apply_filters( $filter_prefix . '_' . $block['blockName'], $updated_html, $block, $instance, $block_id ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Enqueues dynamic styles for the block. |
| 69 |
* |
| 70 |
* @param array $block The block settings. |
| 71 |
* @param string $block_id Unique ID for the block instance. |
| 72 |
* @param object $instance The block instance. |
| 73 |
*/ |
| 74 |
public static function enqueue_dyanmic_style( $block, $block_id, $instance ) { |
| 75 |
if ( empty( $block['blockName'] ) ) { |
| 76 |
return false; // Exit early if the block has no name. |
| 77 |
} |
| 78 |
|
| 79 |
// Create a unique style handle based on the block name and block ID. |
| 80 |
$handle = 'gutenify_' . str_replace( '/', '_', $block['blockName'] ) . '_' . $block_id; |
| 81 |
|
| 82 |
// Register and enqueue an empty stylesheet for attaching inline styles. |
| 83 |
wp_register_style( $handle, false ); |
| 84 |
wp_enqueue_style( $handle ); |
| 85 |
|
| 86 |
// Initialize CSS variable. |
| 87 |
$css = ''; |
| 88 |
|
| 89 |
if ( ! in_array( $block['blockName'], array( 'gutenify/button' ), true ) ) { |
| 90 |
$css = self::spacing_style( $block, $block_id, $instance ); |
| 91 |
} |
| 92 |
|
| 93 |
// Add the generated inline styles to the registered handle. |
| 94 |
wp_add_inline_style( $handle, $css ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Generates dynamic spacing styles for the block. |
| 99 |
* |
| 100 |
* @param array $block The block settings. |
| 101 |
* @param string $block_id Unique ID for the block instance. |
| 102 |
* @param object $instance The block instance. |
| 103 |
* @return string CSS styles. |
| 104 |
*/ |
| 105 |
public static function spacing_style( $block, $block_id, $instance ) { |
| 106 |
// Initialize the CSS output variable. |
| 107 |
$css = ''; |
| 108 |
|
| 109 |
// Get default margin from instance attributes. |
| 110 |
$margin = ! empty( $instance->attributes['blockAdvanceOptions']['margin'] ) ? $instance->attributes['blockAdvanceOptions']['margin'] : array(); |
| 111 |
|
| 112 |
// Merge with block-level margin if provided. |
| 113 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['margin'] ) ) { |
| 114 |
$margin = wp_parse_args( $block['attrs']['blockAdvanceOptions']['margin'], $margin ); |
| 115 |
} |
| 116 |
|
| 117 |
// Generate margin CSS. |
| 118 |
$css .= Dynamic_Styles::get_spacing_with_media( '.' . $block_id, $margin, 'margin-' ); |
| 119 |
|
| 120 |
// Get default padding from instance attributes. |
| 121 |
$padding = ! empty( $instance->attributes['blockAdvanceOptions']['padding'] ) ? $instance->attributes['blockAdvanceOptions']['padding'] : array(); |
| 122 |
|
| 123 |
// Merge with block-level padding if provided. |
| 124 |
if ( ! empty( $block['attrs']['blockAdvanceOptions']['padding'] ) ) { |
| 125 |
$padding = wp_parse_args( $block['attrs']['blockAdvanceOptions']['padding'], $padding ); |
| 126 |
} |
| 127 |
|
| 128 |
// Generate padding CSS. |
| 129 |
$css .= Dynamic_Styles::get_spacing_with_media( '.' . $block_id, $padding, 'padding-' ); |
| 130 |
|
| 131 |
// Return the combined spacing styles. |
| 132 |
return $css; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// Initialize the class. |
| 137 |
Dynamic_Block_Classname::init(); |
| 138 |
|