Animations.php
3 months ago
BackButton.php
9 months ago
BeforeAfter.php
3 months ago
BlockPatterns.php
6 months ago
Carousel.php
2 months ago
ColumnsSameHeight.php
2 months ago
ContainerEdgeAlignment.php
1 week ago
CookieNotice.php
1 week ago
Counter.php
3 months ago
DownloadButton.php
2 months ago
Events.php
2 months ago
FaqSchema.php
2 months ago
FluidTypography.php
2 months ago
Gallery.php
1 week ago
GravityFormsInline.php
3 months ago
Headline.php
2 months ago
InsertPost.php
3 months ago
Maintenance.php
1 week ago
ProductCategories.php
2 months ago
ReadingProgress.php
9 months ago
ReadingTime.php
1 week ago
ScrollTop.php
1 week ago
ShapeAnimations.php
1 week ago
StackedImages.php
6 months ago
StickyColumn.php
2 months ago
SvgUpload.php
2 months ago
Testimonials.php
10 months ago
TextAnimation.php
3 months ago
UserText.php
2 months ago
ContainerEdgeAlignment.php
210 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_action( 'enqueue_block_assets', array( $this, 'enqueue_editor_style' ) ); |
| 31 | add_filter( 'render_block', array( $this, 'add_edge_alignment_classes' ), 10, 2 ); |
| 32 | add_filter( 'register_block_type_args', array( $this, 'register_native_block_attributes' ), 10, 2 ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Enqueue editor assets. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function enqueue_editor_assets() { |
| 41 | wp_enqueue_script( |
| 42 | 'frbl-edge-alignment-option', |
| 43 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.js', |
| 44 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-hooks', 'wp-compose', 'wp-block-editor' ), |
| 45 | FRBL_VERSION, |
| 46 | true |
| 47 | ); |
| 48 | |
| 49 | // Set script translations for JavaScript. |
| 50 | wp_set_script_translations( |
| 51 | 'frbl-edge-alignment-option', |
| 52 | 'frontblocks' |
| 53 | ); |
| 54 | |
| 55 | // Inspector panel rules render in the parent admin document, not the |
| 56 | // iframed canvas, so this stylesheet stays on |
| 57 | // enqueue_block_editor_assets rather than moving to |
| 58 | // enqueue_block_assets like the canvas stylesheet below. |
| 59 | wp_enqueue_style( |
| 60 | 'frbl-edge-alignment-panel', |
| 61 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment-panel.css', |
| 62 | array(), |
| 63 | FRBL_VERSION |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Enqueue the canvas/frontend style on both the frontend and the block |
| 69 | * editor's iframed canvas. |
| 70 | * |
| 71 | * Hooked on enqueue_block_assets rather than enqueue_block_editor_assets: |
| 72 | * the editor canvas is rendered in an iframe, and a style enqueued via the |
| 73 | * editor-only hook is appended to the parent wp-admin document instead of |
| 74 | * that iframe, which WordPress now flags as an incorrect registration. |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public function enqueue_editor_style() { |
| 79 | // Frontend enqueueing is handled conditionally in add_edge_alignment_classes(). |
| 80 | if ( ! is_admin() ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | wp_enqueue_style( |
| 85 | 'frbl-edge-alignment-editor', |
| 86 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css', |
| 87 | array(), |
| 88 | FRBL_VERSION |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Register frontend assets for conditional enqueueing. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public function register_frontend_assets() { |
| 98 | wp_register_style( |
| 99 | 'frbl-edge-alignment', |
| 100 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css', |
| 101 | array(), |
| 102 | FRBL_VERSION |
| 103 | ); |
| 104 | |
| 105 | wp_register_script( |
| 106 | 'frbl-edge-alignment-js', |
| 107 | FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment-frontend.js', |
| 108 | array(), |
| 109 | FRBL_VERSION, |
| 110 | true |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Register frblEdgeAlignment attribute server-side for native blocks. |
| 116 | * |
| 117 | * @param array $args Block type args. |
| 118 | * @param string $block_type Block type name. |
| 119 | * @return array |
| 120 | */ |
| 121 | public function register_native_block_attributes( $args, $block_type ) { |
| 122 | $native_blocks = array( 'core/group', 'core/columns' ); |
| 123 | if ( ! in_array( $block_type, $native_blocks, true ) ) { |
| 124 | return $args; |
| 125 | } |
| 126 | |
| 127 | if ( ! isset( $args['attributes'] ) ) { |
| 128 | $args['attributes'] = array(); |
| 129 | } |
| 130 | |
| 131 | $args['attributes']['frblEdgeAlignment'] = array( |
| 132 | 'type' => 'string', |
| 133 | 'default' => '', |
| 134 | ); |
| 135 | |
| 136 | return $args; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Add edge alignment classes to blocks. |
| 141 | * |
| 142 | * @param string $block_content Block content. |
| 143 | * @param array $block Block data. |
| 144 | * @return string Modified block content. |
| 145 | */ |
| 146 | public function add_edge_alignment_classes( $block_content, $block ) { |
| 147 | $supported_blocks = array( |
| 148 | 'generateblocks/container', |
| 149 | 'generateblocks/element', |
| 150 | 'core/group', |
| 151 | 'core/columns', |
| 152 | ); |
| 153 | |
| 154 | if ( ! in_array( $block['blockName'], $supported_blocks, true ) ) { |
| 155 | return $block_content; |
| 156 | } |
| 157 | |
| 158 | // Check if edge alignment attribute exists. |
| 159 | if ( empty( $block['attrs']['frblEdgeAlignment'] ) ) { |
| 160 | return $block_content; |
| 161 | } |
| 162 | |
| 163 | // Get the edge alignment value. |
| 164 | $edge_alignment = $block['attrs']['frblEdgeAlignment']; |
| 165 | |
| 166 | // Determine which class to add. |
| 167 | $class_string = ''; |
| 168 | |
| 169 | if ( 'left' === $edge_alignment ) { |
| 170 | $class_string = 'frbl-edge-left'; |
| 171 | } elseif ( 'right' === $edge_alignment ) { |
| 172 | $class_string = 'frbl-edge-right'; |
| 173 | } |
| 174 | |
| 175 | // If no valid alignment, return. |
| 176 | if ( empty( $class_string ) ) { |
| 177 | return $block_content; |
| 178 | } |
| 179 | |
| 180 | // Enqueue frontend assets only when an edge-aligned block is detected. |
| 181 | if ( ! wp_style_is( 'frbl-edge-alignment', 'enqueued' ) ) { |
| 182 | wp_enqueue_style( 'frbl-edge-alignment' ); |
| 183 | } |
| 184 | if ( ! wp_script_is( 'frbl-edge-alignment-js', 'enqueued' ) ) { |
| 185 | wp_enqueue_script( 'frbl-edge-alignment-js' ); |
| 186 | } |
| 187 | |
| 188 | // Add class to the block. |
| 189 | // Find the first occurrence of class=" and add our class. |
| 190 | if ( false !== strpos( $block_content, 'class="' ) ) { |
| 191 | $block_content = preg_replace( |
| 192 | '/class="/', |
| 193 | 'class="' . esc_attr( $class_string ) . ' ', |
| 194 | $block_content, |
| 195 | 1 |
| 196 | ); |
| 197 | } else { |
| 198 | // If no class attribute exists, add one after the first tag opening. |
| 199 | $block_content = preg_replace( |
| 200 | '/^<(\w+)/', |
| 201 | '<$1 class="' . esc_attr( $class_string ) . '"', |
| 202 | $block_content, |
| 203 | 1 |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | return $block_content; |
| 208 | } |
| 209 | } |
| 210 |