| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form Triggers |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Triggers; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Form_Triggers |
| 16 |
*/ |
| 17 |
class Form_Triggers extends Trigger { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'wpforms'; |
| 24 |
$this->group = __( 'Forms', 'wpbot-automator' ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get available sub-triggers |
| 29 |
*/ |
| 30 |
public static function get_sub_triggers() { |
| 31 |
return array( |
| 32 |
'wpforms_submit' => array( |
| 33 |
'title' => __( 'WPForms Submitted', 'wpbot-automator' ), |
| 34 |
'hook' => 'wpforms_process_complete', |
| 35 |
), |
| 36 |
'fluentform_submit' => array( |
| 37 |
'title' => __( 'Fluent Forms Submitted', 'wpbot-automator' ), |
| 38 |
'hook' => 'fluentform_submission_inserted', |
| 39 |
), |
| 40 |
'gravityforms_submit' => array( |
| 41 |
'title' => __( 'Gravity Forms Submitted', 'wpbot-automator' ), |
| 42 |
'hook' => 'gform_after_submission', |
| 43 |
), |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register the triggers |
| 49 |
*/ |
| 50 |
public function register() { |
| 51 |
// error_log( 'WPbot Automator - Registering Form Triggers' ); |
| 52 |
add_action( 'wpforms_process_complete', array( $this, 'handle_wpforms' ), 10, 4 ); |
| 53 |
// CF7 is handled by the dedicated CF7 trigger class. |
| 54 |
// add_action( 'wpcf7_mail_sent', array( $this, 'handle_cf7' ), 10, 1 ); |
| 55 |
// error_log( 'WPbot Automator - CF7 hooked' ); |
| 56 |
//error_log( 'WPbot Automator - WPForms hooked' ); |
| 57 |
add_action( 'fluentform_submission_inserted', array( $this, 'handle_fluentform' ), 10, 3 ); |
| 58 |
// error_log( 'WPbot Automator - Fluent hooked' ); |
| 59 |
add_action( 'gform_after_submission', array( $this, 'handle_gravityforms' ), 10, 2 ); |
| 60 |
// error_log( 'WPbot Automator - Gravity hooked' ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Handle WPForms. |
| 65 |
*/ |
| 66 |
public function handle_wpforms( $fields, $entry, $form_data, $entry_id ) { |
| 67 |
$data = array( |
| 68 |
'form_id' => $form_data['id'], |
| 69 |
'form_name' => $form_data['settings']['form_title'], |
| 70 |
'fields' => $fields, |
| 71 |
'entry_id' => $entry_id, |
| 72 |
); |
| 73 |
|
| 74 |
// Expose each WPForms field value as a direct {token} using the field label. |
| 75 |
// e.g. a field labelled "Email" → {email}, "First Name" → {first_name}. |
| 76 |
foreach ( $fields as $field ) { |
| 77 |
if ( ! isset( $field['name'], $field['value'] ) || ! is_scalar( $field['value'] ) ) { |
| 78 |
continue; |
| 79 |
} |
| 80 |
$safe = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $field['name'] ) ); |
| 81 |
$safe = trim( $safe, '_' ); |
| 82 |
if ( $safe && ! isset( $data[ $safe ] ) ) { |
| 83 |
$data[ $safe ] = $field['value']; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
$this->run( array( |
| 88 |
'sub_id' => 'wpforms_submit', |
| 89 |
'data' => $data, |
| 90 |
) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Handle Contact Form 7. |
| 95 |
*/ |
| 96 |
public function handle_cf7( $contact_form ) { |
| 97 |
$full_trigger_id = $this->id; |
| 98 |
\WPbot_Automator\Engine\Workflow_Runner::run_workflows_for_trigger( $full_trigger_id, $contact_form ); |
| 99 |
$submission = \WPCF7_Submission::get_instance(); |
| 100 |
if ( $submission ) { |
| 101 |
$this->run( array( |
| 102 |
'sub_id' => 'cf7_submit', |
| 103 |
'data' => array( |
| 104 |
'form_id' => $contact_form->id(), |
| 105 |
'form_name' => $contact_form->title(), |
| 106 |
'fields' => $submission->get_posted_data(), |
| 107 |
), |
| 108 |
) ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Handle Fluent Forms. |
| 114 |
*/ |
| 115 |
public function handle_fluentform( $insert_id, $raw_data, $form ) { |
| 116 |
$data = array( |
| 117 |
'form_id' => $form->id, |
| 118 |
'form_name' => $form->title, |
| 119 |
'fields' => $raw_data, |
| 120 |
'entry_id' => $insert_id, |
| 121 |
); |
| 122 |
|
| 123 |
// FluentForms submitted data is a flat key→value array. |
| 124 |
if ( is_array( $raw_data ) ) { |
| 125 |
foreach ( $raw_data as $key => $val ) { |
| 126 |
if ( is_scalar( $val ) && '' !== $val ) { |
| 127 |
$safe = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $key ) ); |
| 128 |
$safe = trim( $safe, '_' ); |
| 129 |
if ( $safe && ! isset( $data[ $safe ] ) ) { |
| 130 |
$data[ $safe ] = $val; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$this->run( array( |
| 137 |
'sub_id' => 'fluentform_submit', |
| 138 |
'data' => $data, |
| 139 |
) ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Handle Gravity Forms. |
| 144 |
*/ |
| 145 |
public function handle_gravityforms( $entry, $form ) { |
| 146 |
$data = array( |
| 147 |
'form_id' => $form['id'], |
| 148 |
'form_name' => $form['title'], |
| 149 |
'fields' => $entry, |
| 150 |
); |
| 151 |
|
| 152 |
// Gravity Forms entry is a flat field_id → value array; also expose via label. |
| 153 |
if ( is_array( $form['fields'] ) ) { |
| 154 |
foreach ( $form['fields'] as $field ) { |
| 155 |
$field_id = $field['id'] ?? null; |
| 156 |
if ( ! $field_id || ! isset( $entry[ $field_id ] ) || ! is_scalar( $entry[ $field_id ] ) ) { |
| 157 |
continue; |
| 158 |
} |
| 159 |
$safe = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $field['label'] ?? '' ) ); |
| 160 |
$safe = trim( $safe, '_' ); |
| 161 |
if ( $safe && ! isset( $data[ $safe ] ) ) { |
| 162 |
$data[ $safe ] = $entry[ $field_id ]; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$this->run( array( |
| 168 |
'sub_id' => 'gravityforms_submit', |
| 169 |
'data' => $data, |
| 170 |
) ); |
| 171 |
} |
| 172 |
} |
| 173 |
|