Animations.php
7 months ago
BackButton.php
7 months ago
Carousel.php
8 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
8 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
8 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
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
GravityFormsInline.php
190 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Gravity Forms Inline Layout module for FrontBlocks. |
| 4 | * |
| 5 | * @package FrontBlocks |
| 6 | * @author Closemarketing |
| 7 | * @copyright 2025 Closemarketing |
| 8 | * @version 1.0 |
| 9 | */ |
| 10 | |
| 11 | namespace FrontBlocks\Frontend; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * GravityFormsInline class. |
| 17 | * |
| 18 | * Adds inline layout option for Gravity Forms blocks. |
| 19 | * |
| 20 | * @since 1.2.2 |
| 21 | */ |
| 22 | class GravityFormsInline { |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | $this->init_hooks(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Initialize hooks. |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | private function init_hooks() { |
| 37 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99 ); |
| 38 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 39 | add_filter( 'render_block', array( $this, 'add_inline_attributes_to_gf_block' ), 10, 2 ); |
| 40 | add_action( 'init', array( $this, 'register_custom_attributes' ), 5 ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Enqueue scripts and styles. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function enqueue_scripts() { |
| 49 | wp_enqueue_style( |
| 50 | 'frontblocks-gf-inline', |
| 51 | FRBL_PLUGIN_URL . 'assets/gravityforms-inline/frontblocks-gf-inline.css', |
| 52 | array(), |
| 53 | FRBL_VERSION |
| 54 | ); |
| 55 | |
| 56 | wp_enqueue_script( |
| 57 | 'frontblocks-gf-inline-runtime', |
| 58 | FRBL_PLUGIN_URL . 'assets/gravityforms-inline/frontblocks-gf-inline.js', |
| 59 | array(), |
| 60 | FRBL_VERSION, |
| 61 | true |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Enqueue block editor assets. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function enqueue_block_editor_assets() { |
| 71 | // Enqueue CSS for editor preview. |
| 72 | wp_enqueue_style( |
| 73 | 'frontblocks-gf-inline-editor', |
| 74 | FRBL_PLUGIN_URL . 'assets/gravityforms-inline/frontblocks-gf-inline.css', |
| 75 | array(), |
| 76 | FRBL_VERSION |
| 77 | ); |
| 78 | |
| 79 | wp_enqueue_script( |
| 80 | 'frontblocks-gf-inline-editor-js', |
| 81 | FRBL_PLUGIN_URL . 'assets/gravityforms-inline/frontblocks-gf-inline-option.js', |
| 82 | array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-i18n', 'wp-hooks', 'wp-block-editor', 'wp-compose', 'wp-api-fetch' ), |
| 83 | FRBL_VERSION, |
| 84 | false |
| 85 | ); |
| 86 | |
| 87 | // Set script translations for JavaScript. |
| 88 | wp_set_script_translations( |
| 89 | 'frontblocks-gf-inline-editor-js', |
| 90 | 'frontblocks' |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Add inline layout attributes to Gravity Forms block. |
| 96 | * |
| 97 | * @param string $block_content Block content. |
| 98 | * @param array $block Block attributes. |
| 99 | * @return string |
| 100 | */ |
| 101 | public function add_inline_attributes_to_gf_block( $block_content, $block ) { |
| 102 | // Only process Gravity Forms blocks. |
| 103 | if ( ! isset( $block['blockName'] ) || 'gravityforms/form' !== $block['blockName'] ) { |
| 104 | return $block_content; |
| 105 | } |
| 106 | |
| 107 | $attrs = $block['attrs'] ?? array(); |
| 108 | $inline_enabled = isset( $attrs['frblGfInlineEnabled'] ) ? (bool) $attrs['frblGfInlineEnabled'] : false; |
| 109 | $inline_gap = isset( $attrs['frblGfInlineGap'] ) ? (int) $attrs['frblGfInlineGap'] : 10; |
| 110 | |
| 111 | // Add inline class and attributes if enabled. |
| 112 | if ( $inline_enabled ) { |
| 113 | // Try multiple approaches to add the class. |
| 114 | // Method 1: Add to wp-block-gravityforms-form wrapper. |
| 115 | if ( strpos( $block_content, 'wp-block-gravityforms-form' ) !== false ) { |
| 116 | $block_content = str_replace( |
| 117 | 'wp-block-gravityforms-form', |
| 118 | 'wp-block-gravityforms-form frontblocks-gf-inline', |
| 119 | $block_content |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | // Method 2: Add data attributes to any div with wp-block-gravityforms-form. |
| 124 | $block_content = preg_replace( |
| 125 | '/(<div[^>]*class="[^"]*wp-block-gravityforms-form[^"]*")/i', |
| 126 | '$1 data-gf-inline-enabled="true" data-gf-inline-gap="' . esc_attr( $inline_gap ) . '"', |
| 127 | $block_content, |
| 128 | 1 |
| 129 | ); |
| 130 | |
| 131 | // Method 3: Add wrapper with inline class if not already wrapped. |
| 132 | if ( strpos( $block_content, 'frontblocks-gf-inline' ) === false && ! empty( $block_content ) ) { |
| 133 | $block_content = '<div class="frontblocks-gf-inline-wrapper" data-gf-inline-gap="' . esc_attr( $inline_gap ) . '">' . $block_content . '</div>'; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return $block_content; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Register custom attributes for blocks. |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | public function register_custom_attributes() { |
| 146 | // Register attributes from frontend side. |
| 147 | add_action( |
| 148 | 'enqueue_block_editor_assets', |
| 149 | array( $this, 'add_inline_script_for_attributes' ) |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Add inline script for block attributes. |
| 155 | * |
| 156 | * @return void |
| 157 | */ |
| 158 | public function add_inline_script_for_attributes() { |
| 159 | wp_add_inline_script( |
| 160 | 'wp-blocks', |
| 161 | " |
| 162 | wp.hooks.addFilter( |
| 163 | 'blocks.registerBlockType', |
| 164 | 'frontblocks/gf-inline-attributes', |
| 165 | function( settings, name ) { |
| 166 | if ( name !== 'gravityforms/form' ) { |
| 167 | return settings; |
| 168 | } |
| 169 | |
| 170 | // Add our custom attributes. |
| 171 | settings.attributes = { |
| 172 | ...settings.attributes, |
| 173 | frblGfInlineEnabled: { |
| 174 | type: 'boolean', |
| 175 | default: false |
| 176 | }, |
| 177 | frblGfInlineGap: { |
| 178 | type: 'number', |
| 179 | default: 10 |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | return settings; |
| 184 | } |
| 185 | ); |
| 186 | " |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 |