PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / trunk
FrontBlocks for Gutenberg/GeneratePress vtrunk
trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / includes / Frontend / GravityFormsInline.php
frontblocks / includes / Frontend Last commit date
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
GravityFormsInline.php
197 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( 'init', array( $this, 'register_scripts' ) );
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 * Register frontend scripts and styles for conditional enqueueing.
45 *
46 * @return void
47 */
48 public function register_scripts() {
49 wp_register_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_register_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 // Enqueue frontend assets only when the GF inline feature is active.
114 if ( ! wp_style_is( 'frontblocks-gf-inline', 'enqueued' ) ) {
115 wp_enqueue_style( 'frontblocks-gf-inline' );
116 }
117 if ( ! wp_script_is( 'frontblocks-gf-inline-runtime', 'enqueued' ) ) {
118 wp_enqueue_script( 'frontblocks-gf-inline-runtime' );
119 }
120 // Try multiple approaches to add the class.
121 // Method 1: Add to wp-block-gravityforms-form wrapper.
122 if ( strpos( $block_content, 'wp-block-gravityforms-form' ) !== false ) {
123 $block_content = str_replace(
124 'wp-block-gravityforms-form',
125 'wp-block-gravityforms-form frontblocks-gf-inline',
126 $block_content
127 );
128 }
129
130 // Method 2: Add data attributes to any div with wp-block-gravityforms-form.
131 $block_content = preg_replace(
132 '/(<div[^>]*class="[^"]*wp-block-gravityforms-form[^"]*")/i',
133 '$1 data-gf-inline-enabled="true" data-gf-inline-gap="' . esc_attr( $inline_gap ) . '"',
134 $block_content,
135 1
136 );
137
138 // Method 3: Add wrapper with inline class if not already wrapped.
139 if ( strpos( $block_content, 'frontblocks-gf-inline' ) === false && ! empty( $block_content ) ) {
140 $block_content = '<div class="frontblocks-gf-inline-wrapper" data-gf-inline-gap="' . esc_attr( $inline_gap ) . '">' . $block_content . '</div>';
141 }
142 }
143
144 return $block_content;
145 }
146
147 /**
148 * Register custom attributes for blocks.
149 *
150 * @return void
151 */
152 public function register_custom_attributes() {
153 // Register attributes from frontend side.
154 add_action(
155 'enqueue_block_editor_assets',
156 array( $this, 'add_inline_script_for_attributes' )
157 );
158 }
159
160 /**
161 * Add inline script for block attributes.
162 *
163 * @return void
164 */
165 public function add_inline_script_for_attributes() {
166 wp_add_inline_script(
167 'wp-blocks',
168 "
169 wp.hooks.addFilter(
170 'blocks.registerBlockType',
171 'frontblocks/gf-inline-attributes',
172 function( settings, name ) {
173 if ( name !== 'gravityforms/form' ) {
174 return settings;
175 }
176
177 // Add our custom attributes.
178 settings.attributes = {
179 ...settings.attributes,
180 frblGfInlineEnabled: {
181 type: 'boolean',
182 default: false
183 },
184 frblGfInlineGap: {
185 type: 'number',
186 default: 10
187 }
188 };
189
190 return settings;
191 }
192 );
193 "
194 );
195 }
196 }
197