-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleadcapture-form.php
More file actions
485 lines (427 loc) · 14.1 KB
/
leadcapture-form.php
File metadata and controls
485 lines (427 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* Plugin Name: LeadCapture Form
* Plugin URI: https://github.com/SilverAssist/leadcapture-form
* Description: WordPress plugin that embeds LeadCapture.io forms via shortcode, Gutenberg block, and Elementor widget with lazy loading and popup trigger support.
* Version: 1.0.1
* Author: Silver Assist
* Author URI: http://silverassist.com/
* Text Domain: leadcapture-form
* Domain Path: /languages
* Requires PHP: 8.2
* Update URI: https://github.com/SilverAssist/leadcapture-form
* License: Polyform Noncommercial License 1.0.0
* License URI: https://polyformproject.org/licenses/noncommercial/1.0.0/
*
* @package LeadCaptureForm
* @version 1.0.1
* @author Silver Assist
*/
namespace LeadCaptureForm;
// Import WordPress core classes.
use WP_Post;
// Import PHP standard classes.
use Exception;
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define plugin constants.
define( 'LEADCAPTURE_FORM_VERSION', '1.0.0' );
define( 'LEADCAPTURE_FORM_FILE', __FILE__ );
define( 'LEADCAPTURE_FORM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'LEADCAPTURE_FORM_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'LEADCAPTURE_FORM_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
/**
* Main plugin class using Singleton pattern.
*
* Handles the core functionality of the LeadCapture Form plugin,
* including shortcode registration, script/style loading, and form rendering.
*
* @since 1.0.0
* @package LeadCaptureForm
*/
class LeadCapture_Form {
/**
* Single instance of the plugin.
*
* @since 1.0.0
* @var LeadCapture_Form|null
* @access private
* @static
*/
private static ?LeadCapture_Form $instance = null;
/**
* Private constructor to prevent direct instantiation.
*
* @since 1.0.0
* @access private
*/
private function __construct() {
$this->init();
}
/**
* Prevent object cloning.
*
* @since 1.0.0
* @access private
*/
private function __clone(): void {
}
/**
* Prevent object unserialization.
*
* @since 1.0.0
* @access public
* @throws Exception Always thrown to prevent unserialization.
*/
public function __wakeup(): void {
throw new Exception( 'Cannot unserialize singleton' );
}
/**
* Get the single instance of the plugin.
*
* Implements the Singleton pattern to ensure only one instance
* of the plugin exists throughout the WordPress lifecycle.
*
* @since 1.0.0
* @access public
* @static
* @return LeadCapture_Form The single instance of the plugin.
*/
public static function get_instance(): LeadCapture_Form {
if ( self::$instance === null ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Initialize the plugin.
*
* Sets up hooks, loads dependencies, and registers the shortcode.
* Called from the constructor.
*
* @since 1.0.0
* @access private
* @return void
*/
private function init(): void {
// Load necessary files.
$this->load_dependencies();
// WordPress hooks.
add_action( 'init', array( $this, 'init_plugin' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Register shortcode.
add_shortcode( 'leadcapture_form', array( $this, 'render_shortcode' ) );
}
/**
* Load plugin dependencies.
*
* Include additional PHP files from the includes directory.
* Loads the Gutenberg block handler, Elementor widgets loader, and updater system.
*
* @since 1.0.0
* @access private
* @return void
*/
private function load_dependencies(): void {
// Load Composer autoloader for external packages.
if ( file_exists( LEADCAPTURE_FORM_PLUGIN_PATH . 'vendor/autoload.php' ) ) {
require_once LEADCAPTURE_FORM_PLUGIN_PATH . 'vendor/autoload.php';
}
// Load Gutenberg block handler.
require_once LEADCAPTURE_FORM_PLUGIN_PATH . 'includes/LeadCaptureFormBlock.php';
// Load Elementor widgets loader (only if Elementor is active).
if ( \did_action( 'elementor/loaded' ) || \class_exists( '\\Elementor\\Plugin' ) ) {
require_once LEADCAPTURE_FORM_PLUGIN_PATH . 'includes/elementor/WidgetsLoader.php';
}
// Load updater system (only in admin).
if ( \is_admin() ) {
require_once LEADCAPTURE_FORM_PLUGIN_PATH . 'includes/LeadCaptureFormUpdater.php';
require_once LEADCAPTURE_FORM_PLUGIN_PATH . 'includes/LeadCaptureFormAdmin.php';
}
}
/**
* Initialize plugin after WordPress is loaded.
*
* Loads the plugin textdomain for internationalization support,
* initializes the Gutenberg block handler, sets up Elementor integration,
* and initializes the updater system.
* This method is called on the "init" hook.
*
* @since 1.0.0
* @access public
* @return void
*/
public function init_plugin(): void {
// Load textdomain for translations.
\load_plugin_textdomain(
'leadcapture-form',
false,
dirname( LEADCAPTURE_FORM_PLUGIN_BASENAME ) . '/languages'
);
// Initialize Gutenberg block.
if ( \class_exists( 'LeadCaptureForm\\Block\\LeadCaptureFormBlock' ) ) {
Block\LeadCaptureFormBlock::get_instance();
}
// Initialize Elementor widgets loader.
if ( \class_exists( 'LeadCaptureForm\\Elementor\\WidgetsLoader' ) ) {
Elementor\WidgetsLoader::get_instance();
}
// Initialize updater system (only in admin).
if ( \is_admin() && \class_exists( 'LeadCaptureForm\\LeadCaptureFormUpdater' ) ) {
// Public repository - no authentication required.
$updater = new LeadCaptureFormUpdater( __FILE__, 'SilverAssist/leadcapture-form' );
// Initialize admin page.
if ( \class_exists( 'LeadCaptureForm\\LeadCaptureFormAdmin' ) ) {
new LeadCaptureFormAdmin( $updater );
}
}
}
/**
* Load scripts and styles.
*
* Conditionally enqueues CSS and JavaScript files only when the shortcode
* is present on the current page or when Elementor widgets are detected.
* Also localizes script with global settings.
*
* @since 1.0.0
* @access public
* @global WP_Post $post The current post object.
* @return void
*/
public function enqueue_scripts(): void {
// Register CSS.
wp_register_style(
'leadcapture-form-css',
LEADCAPTURE_FORM_PLUGIN_URL . 'assets/css/leadcapture-form.css',
array(),
LEADCAPTURE_FORM_VERSION
);
// Register JavaScript.
wp_register_script(
'leadcapture-form-js',
LEADCAPTURE_FORM_PLUGIN_URL . 'assets/js/leadcapture-form.js',
array(),
LEADCAPTURE_FORM_VERSION,
true
);
global $post;
$should_load_scripts = false;
$shortcode_instances = array();
// Check if shortcode is present in post content.
if ( is_a( $post, WP_Post::class ) && has_shortcode( $post->post_content, 'leadcapture_form' ) ) {
$should_load_scripts = true;
$shortcode_instances = $this->extract_shortcode_instances( $post->post_content );
}
// Check if Elementor widgets are present.
if ( ! $should_load_scripts && $this->has_elementor_widgets() ) {
$should_load_scripts = true;
}
if ( $should_load_scripts ) {
wp_enqueue_style( 'leadcapture-form-css' );
wp_enqueue_script( 'leadcapture-form-js' );
// Localize script with global settings.
wp_localize_script(
'leadcapture-form-js',
'leadCaptureFormSettings',
array(
'instances' => $shortcode_instances,
'pixelScriptUrl' => 'https://api.useleadbot.com/lead-bots/get-pixel-script.js',
)
);
}
}
/**
* Render the shortcode.
*
* Processes shortcode attributes and generates HTML output for the form container.
* Supports two modes: embed (inline form) and popup trigger (button click).
*
* @since 1.0.0
* @access public
* @param array|string $atts {
* Shortcode attributes.
*
* @type string $form-token Required. The LeadCapture.io form token (e.g., GLFT-XXXX).
* @type string $mode Optional. Display mode: "embed" or "popup". Default "embed".
* @type string $trigger-class Optional. CSS class for popup trigger (provided by LeadCapture.io).
* @type string $height Optional. Placeholder height for embed mode (e.g., "600px").
* }
* @return string HTML output for the shortcode.
*/
public function render_shortcode( $atts ): string {
// Default attributes.
$atts = shortcode_atts(
array(
'form-token' => '',
'mode' => 'embed',
'trigger-class' => '',
'height' => '',
),
$atts,
'leadcapture_form'
);
// Validate form token.
$form_token = \sanitize_text_field( $atts['form-token'] ?? '' );
if ( empty( $form_token ) ) {
return '<div class="leadcapture-form-error">' .
esc_html__( 'Error: The form-token parameter is required.', 'leadcapture-form' ) .
'</div>';
}
$mode = \sanitize_text_field( $atts['mode'] ?? 'embed' );
$trigger_class = \sanitize_text_field( $atts['trigger-class'] ?? '' );
$height = \sanitize_text_field( $atts['height'] ?? '' );
// Create unique ID for this shortcode instance.
$instance_id = 'leadcapture-form-' . \wp_generate_uuid4();
// Generate form HTML using output buffering.
ob_start();
if ( $mode === 'popup' && ! empty( $trigger_class ) ) {
// Popup mode: the pixel script handles the popup via trigger class.
// The script is loaded lazily; the trigger class activates the popup.
?>
<div class="leadcapture-form-container leadcapture-popup-mode"
id="<?php echo \esc_attr( $instance_id ); ?>"
data-form-token="<?php echo \esc_attr( $form_token ); ?>"
data-mode="popup"
data-trigger-class="<?php echo \esc_attr( $trigger_class ); ?>">
</div>
<?php
} else {
// Embed mode: inline form with placeholder animation.
?>
<div class="leadcapture-form-container leadcapture-embed-mode"
id="<?php echo \esc_attr( $instance_id ); ?>"
data-form-token="<?php echo \esc_attr( $form_token ); ?>"
data-mode="embed"
data-height="<?php echo \esc_attr( $height ); ?>">
<div class="leadcapture-form-wrapper">
<!-- Placeholder with pulse animation. -->
<div class="leadcapture-form-placeholder">
<div class="leadcapture-pulse-animation"></div>
</div>
<!-- Form container: LeadCapture.io populates divs with class 'leadforms-embd-form'. -->
<div class="leadcapture-form-content" style="display: none;">
<div class="leadforms-embd-form"><!-- LeadCapture.io renders here. --></div>
</div>
</div>
</div>
<?php
}
return ob_get_clean();
}
/**
* Extract shortcode instances from post content.
*
* Parses the post content to find all instances of the leadcapture_form shortcode
* and extracts their attributes for JavaScript configuration.
*
* @since 1.0.0
* @access private
* @param string $content The post content to parse.
* @return array Array of shortcode instances with their configurations.
*/
private function extract_shortcode_instances( string $content ): array {
$instances = array();
// Pattern to find leadcapture_form shortcodes.
$pattern = '/\[leadcapture_form\s+([^\]]*)\]/';
if ( preg_match_all( $pattern, $content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $index => $match ) {
// Parse shortcode attributes.
$atts = shortcode_parse_atts( $match[1] );
if ( $atts ) {
$form_token = \sanitize_text_field( $atts['form-token'] ?? '' );
$mode = \sanitize_text_field( $atts['mode'] ?? 'embed' );
$trigger_class = \sanitize_text_field( $atts['trigger-class'] ?? '' );
$height = \sanitize_text_field( $atts['height'] ?? '' );
if ( ! empty( $form_token ) ) {
$instances[] = array(
'form_token' => $form_token,
'mode' => $mode,
'trigger_class' => $trigger_class,
'height' => $height,
'index' => $index,
);
}
}
}
}
return $instances;
}
/**
* Check if Elementor LeadCapture widgets are present on the current page.
*
* Searches for Elementor data to detect if any LeadCapture form widgets are active.
* This is used to determine if scripts should be loaded when shortcodes aren't present.
*
* @since 1.0.0
* @access private
* @return bool True if Elementor widgets are detected, false otherwise.
*/
private function has_elementor_widgets(): bool {
// Early return if Elementor is not active.
if ( ! class_exists( '\\Elementor\\Plugin' ) ) {
return false;
}
global $post;
if ( ! is_a( $post, WP_Post::class ) ) {
return false;
}
// Check if this is an Elementor page.
$elementor_data = get_post_meta( $post->ID, '_elementor_data', true );
if ( empty( $elementor_data ) ) {
return false;
}
// Parse Elementor data (it's stored as JSON).
$elementor_data = json_decode( $elementor_data, true );
if ( ! is_array( $elementor_data ) ) {
return false;
}
// Recursively search for our widget in the Elementor data.
return $this->search_elementor_data_for_widget( $elementor_data, 'leadcapture-form' );
}
/**
* Recursively search Elementor data for specific widget type.
*
* Searches through the nested Elementor data structure to find widgets
* of a specific type (widget name).
*
* @since 1.0.0
* @access private
* @param array $data The Elementor data array to search.
* @param string $widget_name The widget name to search for.
* @return bool True if widget is found, false otherwise.
*/
private function search_elementor_data_for_widget( array $data, string $widget_name ): bool {
foreach ( $data as $element ) {
if ( ! is_array( $element ) ) {
continue;
}
// Check if this element is our widget.
if ( isset( $element['widgetType'] ) && $element['widgetType'] === $widget_name ) {
return true;
}
// Recursively search in elements (for sections, columns, etc.).
if ( isset( $element['elements'] ) && is_array( $element['elements'] ) ) {
if ( $this->search_elementor_data_for_widget( $element['elements'], $widget_name ) ) {
return true;
}
}
}
return false;
}
}
/**
* Initialize the plugin.
*
* Factory function to get the singleton instance of the plugin.
* This function is called on the "plugins_loaded" hook.
*
* @since 1.0.0
* @return LeadCapture_Form The single instance of the plugin.
*/
function leadcapture_form_init(): LeadCapture_Form {
return LeadCapture_Form::get_instance();
}
// Initialize the plugin when WordPress is ready.
\add_action( 'plugins_loaded', 'LeadCaptureForm\\leadcapture_form_init' );