Animations.php
1 month ago
BackButton.php
7 months ago
BeforeAfter.php
1 month ago
BlockPatterns.php
4 months ago
Carousel.php
1 month ago
ContainerEdgeAlignment.php
1 month ago
Counter.php
1 month ago
Events.php
7 months ago
FaqSchema.php
1 month ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
1 month ago
Headline.php
1 month ago
InsertPost.php
1 month ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
1 month ago
StackedImages.php
4 months ago
StickyColumn.php
1 month ago
Testimonials.php
8 months ago
TextAnimation.php
1 month ago
ContainerEdgeAlignment.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Container Edge Alignment |
| 4 | * |
| 5 | * Adds custom controls to GenerateBlocks Container block to remove padding |
| 6 | * from left or right side, creating an edge-to-edge effect on one side. |
| 7 | * |
| 8 | * @package FrontBlocks |
| 9 | * @author Closemarketing |
| 10 | * @copyright 2025 Closemarketing |
| 11 | * @version 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace FrontBlocks\Frontend; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Container Edge Alignment class. |
| 20 | * |
| 21 | * @since 1.0.0 |
| 22 | */ |
| 23 | class ContainerEdgeAlignment { |
| 24 | /** |
| 25 | * Constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | add_action( 'init', array( $this, 'register_frontend_assets' ) ); |
| 29 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 30 | add_filter( 'render_block', array( $this, 'add_edge_alignment_classes' ), 10, 2 ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Enqueue editor assets. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function enqueue_editor_assets() { |
| 39 | wp_enqueue_script( |
| 40 | 'frbl-edge-alignment-option', |
| 41 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.js', |
| 42 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-hooks', 'wp-compose', 'wp-block-editor' ), |
| 43 | FRBL_VERSION, |
| 44 | true |
| 45 | ); |
| 46 | |
| 47 | // Set script translations for JavaScript. |
| 48 | wp_set_script_translations( |
| 49 | 'frbl-edge-alignment-option', |
| 50 | 'frontblocks' |
| 51 | ); |
| 52 | |
| 53 | wp_enqueue_style( |
| 54 | 'frbl-edge-alignment-editor', |
| 55 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css', |
| 56 | array(), |
| 57 | FRBL_VERSION |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Register frontend assets for conditional enqueueing. |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function register_frontend_assets() { |
| 67 | wp_register_style( |
| 68 | 'frbl-edge-alignment', |
| 69 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css', |
| 70 | array(), |
| 71 | FRBL_VERSION |
| 72 | ); |
| 73 | |
| 74 | wp_register_script( |
| 75 | 'frbl-edge-alignment-js', |
| 76 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment-frontend.js', |
| 77 | array(), |
| 78 | FRBL_VERSION, |
| 79 | true |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Add edge alignment classes to blocks. |
| 85 | * |
| 86 | * @param string $block_content Block content. |
| 87 | * @param array $block Block data. |
| 88 | * @return string Modified block content. |
| 89 | */ |
| 90 | public function add_edge_alignment_classes( $block_content, $block ) { |
| 91 | // Only process GenerateBlocks container blocks (support both old and new versions). |
| 92 | if ( 'generateblocks/container' !== $block['blockName'] && 'generateblocks/element' !== $block['blockName'] ) { |
| 93 | return $block_content; |
| 94 | } |
| 95 | |
| 96 | // Check if edge alignment attribute exists. |
| 97 | if ( empty( $block['attrs']['frblEdgeAlignment'] ) ) { |
| 98 | return $block_content; |
| 99 | } |
| 100 | |
| 101 | // Get the edge alignment value. |
| 102 | $edge_alignment = $block['attrs']['frblEdgeAlignment']; |
| 103 | |
| 104 | // Determine which class to add. |
| 105 | $class_string = ''; |
| 106 | |
| 107 | if ( 'left' === $edge_alignment ) { |
| 108 | $class_string = 'frbl-edge-left'; |
| 109 | } elseif ( 'right' === $edge_alignment ) { |
| 110 | $class_string = 'frbl-edge-right'; |
| 111 | } |
| 112 | |
| 113 | // If no valid alignment, return. |
| 114 | if ( empty( $class_string ) ) { |
| 115 | return $block_content; |
| 116 | } |
| 117 | |
| 118 | // Enqueue frontend assets only when an edge-aligned block is detected. |
| 119 | if ( ! wp_style_is( 'frbl-edge-alignment', 'enqueued' ) ) { |
| 120 | wp_enqueue_style( 'frbl-edge-alignment' ); |
| 121 | } |
| 122 | if ( ! wp_script_is( 'frbl-edge-alignment-js', 'enqueued' ) ) { |
| 123 | wp_enqueue_script( 'frbl-edge-alignment-js' ); |
| 124 | } |
| 125 | |
| 126 | // Add class to the block. |
| 127 | // Find the first occurrence of class=" and add our class. |
| 128 | if ( false !== strpos( $block_content, 'class="' ) ) { |
| 129 | $block_content = preg_replace( |
| 130 | '/class="/', |
| 131 | 'class="' . esc_attr( $class_string ) . ' ', |
| 132 | $block_content, |
| 133 | 1 |
| 134 | ); |
| 135 | } else { |
| 136 | // If no class attribute exists, add one after the first tag opening. |
| 137 | $block_content = preg_replace( |
| 138 | '/^<(\w+)/', |
| 139 | '<$1 class="' . esc_attr( $class_string ) . '"', |
| 140 | $block_content, |
| 141 | 1 |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | return $block_content; |
| 146 | } |
| 147 | } |
| 148 |