| 1 |
<?php |
| 2 |
/** |
| 3 |
* ElementorPro Compatibility for form |
| 4 |
* |
| 5 |
* @package WPFunnels\Compatibility\Plugin |
| 6 |
*/ |
| 7 |
namespace WPFunnels\Compatibility\Plugin; |
| 8 |
|
| 9 |
use WPFunnels\Wpfnl_functions; |
| 10 |
use WPFunnels\Traits\SingletonTrait; |
| 11 |
use WPFunnels\Optin\Optin_Record; |
| 12 |
|
| 13 |
/** |
| 14 |
* Elementor Pro Compatibility for form |
| 15 |
* |
| 16 |
* @package WPFunnels\Compatibility\ElementorPro |
| 17 |
*/ |
| 18 |
class ElementorPro extends PluginCompatibility{ |
| 19 |
use SingletonTrait; |
| 20 |
|
| 21 |
/** |
| 22 |
* Filters/Hook from Elementor Pro |
| 23 |
* to initiate the necessary updates. |
| 24 |
* |
| 25 |
* @since 3.4.9 |
| 26 |
*/ |
| 27 |
public function init() { |
| 28 |
add_action( 'elementor_pro/forms/new_record', array( $this, 'after_submit_elementor_form'), 10,2 ); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* This function is triggered after submitting an Elementor Pro form. |
| 34 |
* It retrieves the form fields, maps them to the required format, |
| 35 |
* Creates a new Optin Record, and triggers an action to process the form data. |
| 36 |
* |
| 37 |
* @since 3.4.9 |
| 38 |
* |
| 39 |
* @param object $record The Elementor Pro form record object. |
| 40 |
* @param object $handler The Elementor Pro form handler object. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function after_submit_elementor_form( $record, $handler ) { |
| 45 |
// Get the ID of the current post or page |
| 46 |
$step_id = get_the_ID(); |
| 47 |
|
| 48 |
if( !$step_id ){ |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$funnel_id = Wpfnl_functions::get_funnel_id_from_step( $step_id ); |
| 53 |
|
| 54 |
if( !$funnel_id ){ |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Get the 'fields' data from the form submission record |
| 59 |
$fields = $record->get( 'fields' ); |
| 60 |
|
| 61 |
// Map the fields using the map_fields method |
| 62 |
$mapped_value = $this->map_fields( $fields ); |
| 63 |
|
| 64 |
// Create a new instance of the Optin_Record class with the mapped values |
| 65 |
$record = new Optin_Record( $mapped_value); |
| 66 |
|
| 67 |
/** |
| 68 |
* Submit & process form data |
| 69 |
*/ |
| 70 |
// Start output buffering |
| 71 |
ob_start(); |
| 72 |
|
| 73 |
// Trigger the 'wpfunnels/after_optin_submit' action, passing along the step ID, a 'notification' string, an empty string, the record, and the mapped values |
| 74 |
do_action( 'wpfunnels/after_optin_submit', $step_id, 'redirect_to', 'next_step', $record, $mapped_value ); |
| 75 |
|
| 76 |
// Clean (erase) the output buffer and turn off output buffering |
| 77 |
ob_get_clean(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Maps Elementor form fields to a standardized format. |
| 83 |
* |
| 84 |
* This function takes an array of Elementor form fields and maps them to a standardized |
| 85 |
* format for further processing. It handles specific field types such as name, email, |
| 86 |
* message, telephone, and URL. |
| 87 |
* |
| 88 |
* @param array $fields An array of Elementor form fields. |
| 89 |
* |
| 90 |
* @return array An array of mapped fields in a standardized format. Returns an empty array if input is invalid. |
| 91 |
* |
| 92 |
* @since 3.4.9 |
| 93 |
*/ |
| 94 |
private function map_fields( $fields ) { |
| 95 |
// Check if $fields is an array and not empty |
| 96 |
if( is_array( $fields ) && !empty( $fields ) ){ |
| 97 |
// Initialize an empty array to store the mapped fields |
| 98 |
$map_fields = array(); |
| 99 |
|
| 100 |
// Iterate over each field in the $fields array |
| 101 |
foreach( $fields as $key=>$field ){ |
| 102 |
// If the key is 'name' and the 'value' is set, map the 'value' to 'first_name' and 'last_name' in the $map_fields array |
| 103 |
if( 'name' === $key && isset( $field['value'] ) ){ |
| 104 |
$map_fields['first_name'] = $field['value']; |
| 105 |
$map_fields['last_name'] = ''; |
| 106 |
} |
| 107 |
// If the key is 'email' and the 'value' is set, map the 'value' to 'email' in the $map_fields array |
| 108 |
if( 'email' === $key && isset( $field['value'] ) ){ |
| 109 |
$map_fields['email'] = $field['value']; |
| 110 |
} |
| 111 |
// If the key is 'message' and the 'value' is set, map the 'value' to 'message' in the $map_fields array |
| 112 |
if( 'message' === $key && isset( $field['value'] ) ){ |
| 113 |
$map_fields['message'] = $field['value']; |
| 114 |
} |
| 115 |
// If the 'type' is set and is 'tel', map the 'value' to 'phone' in the $map_fields array |
| 116 |
if( isset( $field['type'] ) && 'tel' === $field['type'] ){ |
| 117 |
$map_fields['phone'] = $field['value']; |
| 118 |
} |
| 119 |
// If the 'type' is set and is 'url', map the 'value' to 'web-url' in the $map_fields array |
| 120 |
if( isset( $field['type'] ) && 'url' === $field['type'] ){ |
| 121 |
$map_fields['web-url'] = $field['value']; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Return the mapped fields |
| 126 |
return $map_fields; |
| 127 |
} |
| 128 |
// If $fields is not an array or is empty, return an empty array |
| 129 |
return []; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Check if Elementor Pro is activated |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
* @since 3.4.9 |
| 138 |
*/ |
| 139 |
public function maybe_activate() |
| 140 |
{ |
| 141 |
// Check if the constant 'ELEMENTOR_PRO_VERSION' is defined |
| 142 |
// Return true if it is defined, false otherwise |
| 143 |
return defined('ELEMENTOR_PRO_VERSION'); |
| 144 |
} |
| 145 |
} |
| 146 |
|