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