Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
4 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
6 months ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
4 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
7 months ago
StackedImages.php
4 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
ContainerEdgeAlignment.php
141 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( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 29 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_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 | * Enqueue frontend assets. |
| 63 | * |
| 64 | * @return void |
| 65 | */ |
| 66 | public function enqueue_frontend_assets() { |
| 67 | wp_enqueue_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 | // Add inline script to calculate margins dynamically. |
| 75 | wp_enqueue_script( |
| 76 | 'frbl-edge-alignment-js', |
| 77 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment-frontend.js', |
| 78 | array(), |
| 79 | FRBL_VERSION, |
| 80 | true |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Add edge alignment classes to blocks. |
| 86 | * |
| 87 | * @param string $block_content Block content. |
| 88 | * @param array $block Block data. |
| 89 | * @return string Modified block content. |
| 90 | */ |
| 91 | public function add_edge_alignment_classes( $block_content, $block ) { |
| 92 | // Only process GenerateBlocks container blocks (support both old and new versions). |
| 93 | if ( 'generateblocks/container' !== $block['blockName'] && 'generateblocks/element' !== $block['blockName'] ) { |
| 94 | return $block_content; |
| 95 | } |
| 96 | |
| 97 | // Check if edge alignment attribute exists. |
| 98 | if ( empty( $block['attrs']['frblEdgeAlignment'] ) ) { |
| 99 | return $block_content; |
| 100 | } |
| 101 | |
| 102 | // Get the edge alignment value. |
| 103 | $edge_alignment = $block['attrs']['frblEdgeAlignment']; |
| 104 | |
| 105 | // Determine which class to add. |
| 106 | $class_string = ''; |
| 107 | |
| 108 | if ( 'left' === $edge_alignment ) { |
| 109 | $class_string = 'frbl-edge-left'; |
| 110 | } elseif ( 'right' === $edge_alignment ) { |
| 111 | $class_string = 'frbl-edge-right'; |
| 112 | } |
| 113 | |
| 114 | // If no valid alignment, return. |
| 115 | if ( empty( $class_string ) ) { |
| 116 | return $block_content; |
| 117 | } |
| 118 | |
| 119 | // Add class to the block. |
| 120 | // Find the first occurrence of class=" and add our class. |
| 121 | if ( false !== strpos( $block_content, 'class="' ) ) { |
| 122 | $block_content = preg_replace( |
| 123 | '/class="/', |
| 124 | 'class="' . esc_attr( $class_string ) . ' ', |
| 125 | $block_content, |
| 126 | 1 |
| 127 | ); |
| 128 | } else { |
| 129 | // If no class attribute exists, add one after the first tag opening. |
| 130 | $block_content = preg_replace( |
| 131 | '/^<(\w+)/', |
| 132 | '<$1 class="' . esc_attr( $class_string ) . '"', |
| 133 | $block_content, |
| 134 | 1 |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | return $block_content; |
| 139 | } |
| 140 | } |
| 141 |