-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass-contact-updated-trigger.php
More file actions
278 lines (249 loc) · 7.08 KB
/
class-contact-updated-trigger.php
File metadata and controls
278 lines (249 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* Contact Updated Trigger for FluentCRM
*
* @package FluentCRM
* @since 1.1.0
*/
use FluentCrm\App\Services\Funnel\BaseTrigger;
use FluentCrm\App\Services\Funnel\FunnelHelper;
use FluentCrm\App\Services\Funnel\FunnelProcessor;
use FluentCrm\Framework\Support\Arr;
/**
* Class Contact_Updated_Trigger
*
* Triggers automations when a standard field is updated on a contact.
*
* @since 1.1.0
*/
class Contact_Updated_Trigger extends BaseTrigger {
/**
* Get things started.
*
* @since 1.1.0
*/
public function __construct() {
$this->{'triggerName'} = 'fluentcrm_contact_updated';
$this->{'priority'} = 15;
$this->{'actionArgNum'} = 3;
parent::__construct();
}
/**
* Defines the trigger.
*
* @since 1.1.0
*
* @return array The trigger definition.
*/
public function getTrigger() {
return array(
'category' => __( 'CRM', 'fluentcampaign-pro' ),
'label' => __( 'A standard field is updated', 'fluentcampaign-pro' ),
'description' => __( 'This funnel will start when a standard field is updated on a contact.', 'fluentcampaign-pro' ),
);
}
/**
* Get the funnel setting defaults.
*
* @since 1.1.0
*
* @return array The funnel setting defaults.
*/
public function getFunnelSettingsDefaults() {
return array(
'field_name' => 'any',
'update_type' => 'any',
'field_value' => '',
'run_multiple' => 'no',
);
}
/**
* Adds the settings fields.
*
* @since 1.1.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
* @return array The settings fields.
*/
public function getSettingsFields( $funnel ) {
$standard_fields = array(
'hash',
'prefix',
'first_name',
'last_name',
'user_id',
'company_id',
'email',
'status', // pending / subscribed / bounced / unsubscribed; Default: subscriber.
'contact_type', // lead / customer.
'address_line_1',
'address_line_2',
'postal_code',
'city',
'state',
'country',
'phone',
'timezone',
'date_of_birth',
'source',
'life_time_value',
'last_activity',
'total_points',
'latitude',
'longitude',
'ip',
'created_at',
'updated_at',
'avatar',
);
$options = array();
foreach ( $standard_fields as $field ) {
$options[] = array(
'id' => $field,
'title' => ucfirst( str_replace( '_', ' ', $field ) ),
);
}
return array(
'title' => __( 'Field Updated', 'fluentcampaign-pro' ),
'sub_title' => __( 'This Funnel will start when the selected standard field is updated for a contact.', 'fluentcampaign-pro' ),
'fields' => array(
'field_name' => array(
'type' => 'select',
'options' => $options,
'label' => __( 'Field', 'fluentcampaign-pro' ),
),
),
);
}
/**
* Get the defaults for the funnel.
*
* @since 1.1.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
* @return array The funnel condition defaults.
*/
public function getFunnelConditionDefaults( $funnel ) {
return array(
'update_type' => 'any',
'run_multiple' => 'no',
);
}
/**
* Get the conditional fields for the funnel.
*
* @since 1.1.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
* @return array The condition fields.
*/
public function getConditionFields( $funnel ) {
return array(
'update_type' => array(
'type' => 'radio',
'label' => __( 'If the field changes to?', 'fluentcampaign-pro' ),
'options' => array(
array(
'id' => 'any',
'title' => __( 'Any Value', 'fluentcampaign-pro' ),
),
array(
'id' => 'specific',
'title' => __( 'A Specific Value', 'fluentcampaign-pro' ),
),
),
),
'field_value' => array(
'type' => 'input-text',
'label' => __( 'Field Value', 'fluentcampaign-pro' ),
'help' => __( 'Enter the field value that must match to trigger the automation.', 'fluentcampaign-pro' ),
'dependency' => array(
'depends_on' => 'update_type',
'operator' => '=',
'value' => 'specific',
),
),
'field_empty' => array(
'type' => 'radio',
'label' => __( 'Can the field be empty ?', 'fluentcampaign-pro' ),
'options' => array(
array(
'id' => 'yes',
'title' => __( 'Yes', 'fluentcampaign-pro' ),
),
array(
'id' => 'no',
'title' => __( 'No', 'fluentcampaign-pro' ),
),
),
'help' => __( 'If the field can be empty, the automation will run. Otherwise it will be skipped.', 'fluentcampaign-pro' ),
),
'run_multiple' => array(
'type' => 'yes_no_check',
'label' => '',
'check_label' => __( 'Restart the Automation Multiple times for a contact for this event. (Only enable if you want to restart automation for the same contact)', 'fluentcampaign-pro' ),
'inline_help' => __( 'If you enable, then it will restart the automation for a contact even if the contact is already in the automation. Otherwise, It will just skip if already exist.', 'fluentcampaign-pro' ),
),
);
}
/**
* Handle the action.
*
* @since 1.1.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
* @param array $original_args The original arguments.
*/
public function handle( $funnel, $original_args ) {
$subscriber = $original_args[0];
$updated_data = $original_args[1];
$will_process = $this->isProcessable( $funnel, $subscriber, $updated_data );
$will_process = apply_filters( 'fluentcrm_funnel_will_process_' . $this->{'triggerName'}, $will_process, $funnel, $subscriber, $original_args );
if ( ! $will_process ) {
return;
}
( new FunnelProcessor() )->startFunnelSequence(
$funnel,
array(),
array( 'source_trigger_name' => $this->{'triggerName'} ),
$subscriber
);
}
/**
* Determines if the funnel should be processed.
*
* @since 1.1.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
* @param object $subscriber The subscriber.
* @param array $updated_data The updated data.
* @return bool Whether the funnel should be processed.
*/
private function isProcessable( $funnel, $subscriber, $updated_data ) {
$field = Arr::get( $funnel->settings, 'field_name' );
if ( 'any' === $field ) {
return true; // Always process if 'any' is the field name.
}
if ( ! array_key_exists( $field, $updated_data ) ) {
return false;
}
$update_type = Arr::get( $funnel->conditions, 'update_type' );
if ( 'specific' === $update_type && Arr::get( $funnel->conditions, 'field_value' ) !== $updated_data[ $field ] ) {
return false;
}
$field_empty = Arr::get( $funnel->conditions, 'field_empty');
// Check if the updated data field is empty
if ( 'no' === $field_empty && empty( $updated_data[ $field ] )) {
return false;
}
if ( $subscriber && FunnelHelper::ifAlreadyInFunnel( $funnel->id, $subscriber->id ) ) {
if ( 'yes' === Arr::get( $funnel->conditions, 'run_multiple' ) ) {
FunnelHelper::removeSubscribersFromFunnel( $funnel->id, array( $subscriber->id ) );
} else {
return false;
}
}
return true;
}
}
new Contact_Updated_Trigger();