PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.1
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.1
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / ws-form / includes / triggers / submit-form.php

submit-form.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.1, at integrations/ws-form/includes/triggers/submit-form.php

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