| 1 |
<?php |
| 2 |
/** |
| 3 |
* Submit Form |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Divi\Triggers\Submit_Form |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_Divi_Submit_Form extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'divi'; |
| 15 |
public $trigger = 'divi_submit_form'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register the trigger |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
|
| 24 |
automatorwp_register_trigger( $this->trigger, array( |
| 25 |
'integration' => $this->integration, |
| 26 |
'label' => __( 'User submits a form', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User submits <strong>a form</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Form name. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User submits %1$s %2$s time(s)', 'automatorwp' ), '{form_id}', '{times}' ), |
| 30 |
/* translators: %1$s: Form name. */ |
| 31 |
'log_label' => sprintf( __( 'User submits %1$s', 'automatorwp' ), '{form_id}' ), |
| 32 |
'action' => 'et_pb_contact_form_submit', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 999, |
| 35 |
'accepted_args' => 3, |
| 36 |
'options' => array( |
| 37 |
'form_id' => array( |
| 38 |
'from' => 'form_id', |
| 39 |
'default' => __( 'any form', 'automatorwp' ), |
| 40 |
'fields' => array( |
| 41 |
'form_id' => array( |
| 42 |
'name' => __( 'Form ID:', 'automatorwp' ), |
| 43 |
'type' => 'text', |
| 44 |
'default' => '' |
| 45 |
) |
| 46 |
) |
| 47 |
), |
| 48 |
'times' => automatorwp_utilities_times_option(), |
| 49 |
), |
| 50 |
'tags' => array_merge( |
| 51 |
array( |
| 52 |
'form_field:FIELD_ID' => array( |
| 53 |
'label' => __( 'Form field value', 'automatorwp' ), |
| 54 |
'type' => 'text', |
| 55 |
'preview' => __( 'Form field value, replace "FIELD_ID" by the field id', 'automatorwp' ), |
| 56 |
), |
| 57 |
), |
| 58 |
automatorwp_utilities_times_tag() |
| 59 |
) |
| 60 |
) ); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Trigger listener |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* |
| 69 |
* @param array $fields_values |
| 70 |
* @param bool $et_contact_error |
| 71 |
* @param array $contact_form_info |
| 72 |
*/ |
| 73 |
public function listener( $fields_values, $et_contact_error, $contact_form_info ) { |
| 74 |
|
| 75 |
$user_id = get_current_user_id(); |
| 76 |
|
| 77 |
// Login is required |
| 78 |
if ( $user_id === 0 ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Bail if submission has an error |
| 83 |
if ( $et_contact_error === true ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Bail if form does not have an ID |
| 88 |
if ( ! isset( $contact_form_info['contact_form_unique_id'] ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$form_id = $contact_form_info['contact_form_unique_id']; |
| 93 |
$post_id = $contact_form_info['post_id']; // Post where form is located |
| 94 |
|
| 95 |
$form_fields = automatorwp_divi_get_form_fields_values( $fields_values ); |
| 96 |
|
| 97 |
// Trigger submit form event |
| 98 |
automatorwp_trigger_event( array( |
| 99 |
'trigger' => $this->trigger, |
| 100 |
'user_id' => $user_id, |
| 101 |
'form_id' => $form_id, |
| 102 |
'form_fields' => $form_fields, |
| 103 |
) ); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* User deserves check |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
* |
| 112 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 113 |
* @param stdClass $trigger The trigger object |
| 114 |
* @param int $user_id The user ID |
| 115 |
* @param array $event Event information |
| 116 |
* @param array $trigger_options The trigger's stored options |
| 117 |
* @param stdClass $automation The trigger's automation object |
| 118 |
* |
| 119 |
* @return bool True if user deserves trigger, false otherwise |
| 120 |
*/ |
| 121 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 122 |
|
| 123 |
// Don't deserve if form ID is not received |
| 124 |
if( ! isset( $event['form_id'] ) ) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
// Don't deserve if form ID doesn't match with the trigger option |
| 129 |
if( ! empty( $trigger_options['form_id'] ) && $event['form_id'] !== $trigger_options['form_id'] ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
return $deserves_trigger; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Register the required hooks |
| 139 |
* |
| 140 |
* @since 1.0.0 |
| 141 |
*/ |
| 142 |
public function hooks() { |
| 143 |
|
| 144 |
// Log meta data |
| 145 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 146 |
|
| 147 |
// Log fields |
| 148 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 149 |
|
| 150 |
parent::hooks(); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Trigger custom log meta |
| 155 |
* |
| 156 |
* @since 1.0.0 |
| 157 |
* |
| 158 |
* @param array $log_meta Log meta data |
| 159 |
* @param stdClass $trigger The trigger object |
| 160 |
* @param int $user_id The user ID |
| 161 |
* @param array $event Event information |
| 162 |
* @param array $trigger_options The trigger's stored options |
| 163 |
* @param stdClass $automation The trigger's automation object |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 168 |
|
| 169 |
// Bail if action type don't match this action |
| 170 |
if( $trigger->type !== $this->trigger ) { |
| 171 |
return $log_meta; |
| 172 |
} |
| 173 |
|
| 174 |
$log_meta['form_fields'] = ( isset( $event['form_fields'] ) ? $event['form_fields'] : array() ); |
| 175 |
|
| 176 |
return $log_meta; |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Action custom log fields |
| 182 |
* |
| 183 |
* @since 1.0.0 |
| 184 |
* |
| 185 |
* @param array $log_fields The log fields |
| 186 |
* @param stdClass $log The log object |
| 187 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 188 |
* |
| 189 |
* @return array |
| 190 |
*/ |
| 191 |
public function log_fields( $log_fields, $log, $object ) { |
| 192 |
|
| 193 |
// Bail if log is not assigned to an trigger |
| 194 |
if( $log->type !== 'trigger' ) { |
| 195 |
return $log_fields; |
| 196 |
} |
| 197 |
|
| 198 |
// Bail if trigger type don't match this trigger |
| 199 |
if( $object->type !== $this->trigger ) { |
| 200 |
return $log_fields; |
| 201 |
} |
| 202 |
|
| 203 |
$log_fields['form_fields'] = array( |
| 204 |
'name' => __( 'Fields Submitted', 'automatorwp' ), |
| 205 |
'desc' => __( 'Information about the fields values sent on this form submission.', 'automatorwp' ), |
| 206 |
'type' => 'text', |
| 207 |
); |
| 208 |
|
| 209 |
return $log_fields; |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
new AutomatorWP_Divi_Submit_Form(); |