PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
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 / contact-form-7 / includes / triggers / anonymous-submit-form.php

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

215 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Anonymous Submit Form
4 *
5 * @package AutomatorWP\Integrations\Contact_Form_7\Triggers\Anonymous_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_Contact_Form_7_Anonymous_Submit_Form extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'contact_form_7';
15 public $trigger = 'contact_form_7_anonymous_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 'anonymous' => true,
27 'label' => __( 'Guest submits a form', 'automatorwp' ),
28 'select_option' => __( 'Guest submits <strong>a form</strong>', 'automatorwp' ),
29 /* translators: %1$s: Post title. */
30 'edit_label' => sprintf( __( 'Guest submits %1$s', 'automatorwp' ), '{post}' ),
31 /* translators: %1$s: Post title. */
32 'log_label' => sprintf( __( 'Guest submits %1$s', 'automatorwp' ), '{post}' ),
33 'action' => 'wpcf7_submit',
34 'function' => array( $this, 'listener' ),
35 'priority' => 10,
36 'accepted_args' => 2,
37 'options' => array(
38 'post' => automatorwp_utilities_post_option( array(
39 'name' => __( 'Form:', 'automatorwp' ),
40 'option_none_label' => __( 'any form', 'automatorwp' ),
41 'post_type' => 'wpcf7_contact_form'
42 ) ),
43 'times' => automatorwp_utilities_times_option(),
44 ),
45 'tags' => array_merge(
46 array(
47 'form_field:FIELD_NAME' => array(
48 'label' => __( 'Form field value', 'automatorwp' ),
49 'type' => 'text',
50 'preview' => __( 'Form field value, replace "FIELD_NAME" by the field name', 'automatorwp' ),
51 ),
52 ),
53 automatorwp_utilities_post_tags(),
54 automatorwp_utilities_times_tag()
55 )
56 ) );
57
58 }
59
60 /**
61 * Trigger listener
62 *
63 * @since 1.0.0
64 *
65 * @param WPCF7_ContactForm $form
66 * @param array $result
67 */
68 public function listener( $form, $result ) {
69
70 // Check if form was correctly filled
71 if( ! in_array( $result['status'], array( 'mail_sent', 'mail_failed' ) ) ) {
72 return;
73 }
74
75 $form_id = ( version_compare( WPCF7_VERSION, '4.8', '<' ) ) ? $form->id : $form->id();
76 $user_id = get_current_user_id();
77
78 // Bail if user is logged in
79 if ( $user_id !== 0 ) {
80 return;
81 }
82
83 // Check if form submission is correctly setup
84 $submission = WPCF7_Submission::get_instance();
85
86 if ( ! $submission ) {
87 return;
88 }
89
90 // Check if posted data is correctly setup
91 $posted_data = $submission->get_posted_data();
92
93 if ( ! $posted_data ) {
94 return;
95 }
96
97 $form_fields = automatorwp_contact_form_7_get_form_fields_values( $form, $submission );
98
99 // Trigger submit form event
100 automatorwp_trigger_event( array(
101 'trigger' => $this->trigger,
102 'post_id' => $form_id,
103 'form_fields' => $form_fields,
104 ) );
105
106 }
107
108 /**
109 * Anonymous deserves check
110 *
111 * @since 1.0.0
112 *
113 * @param bool $deserves_trigger True if anonymous deserves trigger, false otherwise
114 * @param stdClass $trigger The trigger object
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 anonymous deserves trigger, false otherwise
120 */
121 public function anonymous_deserves_trigger( $deserves_trigger, $trigger, $event, $trigger_options, $automation ) {
122
123 // Don't deserve if post is not received
124 if( ! isset( $event['post_id'] ) ) {
125 return false;
126 }
127
128 // Don't deserve if post doesn't match with the trigger option
129 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
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_Contact_Form_7_Anonymous_Submit_Form();