Animations.php
1 month ago
BackButton.php
7 months ago
BeforeAfter.php
1 month ago
BlockPatterns.php
4 months ago
Carousel.php
1 week ago
ColumnsSameHeight.php
1 week ago
ContainerEdgeAlignment.php
4 weeks ago
Counter.php
1 month ago
DownloadButton.php
1 week ago
Events.php
4 weeks ago
FaqSchema.php
1 week ago
FluidTypography.php
4 weeks ago
Gallery.php
8 months ago
GravityFormsInline.php
1 month ago
Headline.php
4 weeks ago
InsertPost.php
1 month ago
ProductCategories.php
4 weeks ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
4 weeks ago
StackedImages.php
4 months ago
StickyColumn.php
4 weeks ago
SvgUpload.php
4 weeks ago
Testimonials.php
8 months ago
TextAnimation.php
1 month ago
UserText.php
4 weeks ago
ContainerEdgeAlignment.php
180 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 | add_filter( 'register_block_type_args', array( $this, 'register_native_block_attributes' ), 10, 2 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Enqueue editor assets. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function enqueue_editor_assets() { |
| 40 | wp_enqueue_script( |
| 41 | 'frbl-edge-alignment-option', |
| 42 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.js', |
| 43 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-hooks', 'wp-compose', 'wp-block-editor' ), |
| 44 | FRBL_VERSION, |
| 45 | true |
| 46 | ); |
| 47 | |
| 48 | // Set script translations for JavaScript. |
| 49 | wp_set_script_translations( |
| 50 | 'frbl-edge-alignment-option', |
| 51 | 'frontblocks' |
| 52 | ); |
| 53 | |
| 54 | wp_enqueue_style( |
| 55 | 'frbl-edge-alignment-editor', |
| 56 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css', |
| 57 | array(), |
| 58 | FRBL_VERSION |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Register frontend assets for conditional enqueueing. |
| 64 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | public function register_frontend_assets() { |
| 68 | wp_register_style( |
| 69 | 'frbl-edge-alignment', |
| 70 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css', |
| 71 | array(), |
| 72 | FRBL_VERSION |
| 73 | ); |
| 74 | |
| 75 | wp_register_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 | * Register frblEdgeAlignment attribute server-side for native blocks. |
| 86 | * |
| 87 | * @param array $args Block type args. |
| 88 | * @param string $block_type Block type name. |
| 89 | * @return array |
| 90 | */ |
| 91 | public function register_native_block_attributes( $args, $block_type ) { |
| 92 | $native_blocks = array( 'core/group', 'core/columns' ); |
| 93 | if ( ! in_array( $block_type, $native_blocks, true ) ) { |
| 94 | return $args; |
| 95 | } |
| 96 | |
| 97 | if ( ! isset( $args['attributes'] ) ) { |
| 98 | $args['attributes'] = array(); |
| 99 | } |
| 100 | |
| 101 | $args['attributes']['frblEdgeAlignment'] = array( |
| 102 | 'type' => 'string', |
| 103 | 'default' => '', |
| 104 | ); |
| 105 | |
| 106 | return $args; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add edge alignment classes to blocks. |
| 111 | * |
| 112 | * @param string $block_content Block content. |
| 113 | * @param array $block Block data. |
| 114 | * @return string Modified block content. |
| 115 | */ |
| 116 | public function add_edge_alignment_classes( $block_content, $block ) { |
| 117 | $supported_blocks = array( |
| 118 | 'generateblocks/container', |
| 119 | 'generateblocks/element', |
| 120 | 'core/group', |
| 121 | 'core/columns', |
| 122 | ); |
| 123 | |
| 124 | if ( ! in_array( $block['blockName'], $supported_blocks, true ) ) { |
| 125 | return $block_content; |
| 126 | } |
| 127 | |
| 128 | // Check if edge alignment attribute exists. |
| 129 | if ( empty( $block['attrs']['frblEdgeAlignment'] ) ) { |
| 130 | return $block_content; |
| 131 | } |
| 132 | |
| 133 | // Get the edge alignment value. |
| 134 | $edge_alignment = $block['attrs']['frblEdgeAlignment']; |
| 135 | |
| 136 | // Determine which class to add. |
| 137 | $class_string = ''; |
| 138 | |
| 139 | if ( 'left' === $edge_alignment ) { |
| 140 | $class_string = 'frbl-edge-left'; |
| 141 | } elseif ( 'right' === $edge_alignment ) { |
| 142 | $class_string = 'frbl-edge-right'; |
| 143 | } |
| 144 | |
| 145 | // If no valid alignment, return. |
| 146 | if ( empty( $class_string ) ) { |
| 147 | return $block_content; |
| 148 | } |
| 149 | |
| 150 | // Enqueue frontend assets only when an edge-aligned block is detected. |
| 151 | if ( ! wp_style_is( 'frbl-edge-alignment', 'enqueued' ) ) { |
| 152 | wp_enqueue_style( 'frbl-edge-alignment' ); |
| 153 | } |
| 154 | if ( ! wp_script_is( 'frbl-edge-alignment-js', 'enqueued' ) ) { |
| 155 | wp_enqueue_script( 'frbl-edge-alignment-js' ); |
| 156 | } |
| 157 | |
| 158 | // Add class to the block. |
| 159 | // Find the first occurrence of class=" and add our class. |
| 160 | if ( false !== strpos( $block_content, 'class="' ) ) { |
| 161 | $block_content = preg_replace( |
| 162 | '/class="/', |
| 163 | 'class="' . esc_attr( $class_string ) . ' ', |
| 164 | $block_content, |
| 165 | 1 |
| 166 | ); |
| 167 | } else { |
| 168 | // If no class attribute exists, add one after the first tag opening. |
| 169 | $block_content = preg_replace( |
| 170 | '/^<(\w+)/', |
| 171 | '<$1 class="' . esc_attr( $class_string ) . '"', |
| 172 | $block_content, |
| 173 | 1 |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | return $block_content; |
| 178 | } |
| 179 | } |
| 180 |