-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-field-updated-trigger.php
210 lines (181 loc) · 5.5 KB
/
class-field-updated-trigger.php
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
<?php
use FluentCrm\App\Services\Funnel\BaseTrigger;
use FluentCrm\App\Services\Funnel\FunnelHelper;
use FluentCrm\App\Services\Funnel\FunnelProcessor;
use FluentCrm\Framework\Support\Arr;
class Field_Updated_Trigger extends BaseTrigger {
/**
* Get things started.
*
* @since 1.0.0
*/
public function __construct() {
$this->{'triggerName'} = 'fluentcrm_contact_custom_data_updated';
$this->{'priority'} = 15;
$this->{'actionArgNum'} = 3;
parent::__construct();
}
/**
* Defines the trigger.
*
* @since 1.0.0
*/
public function getTrigger() {
return array(
'category' => __( 'CRM', 'fluentcampaign-pro' ),
'label' => __( 'A field is updated', 'fluentcampaign-pro' ),
'description' => __( 'This funnel will start when a custom field is updated on a contact.', 'fluentcampaign-pro' ),
);
}
/**
* Get the funnel setting defaults.
*
* @since 1.0.0
*/
public function getFunnelSettingsDefaults() {
return array(
'field_name' => '',
'update_type' => 'any',
'field_value' => '',
'run_muliple' => 'no',
);
}
/**
* Adds the settings fields.
*
* @since 1.0.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
*/
public function getSettingsFields( $funnel ) {
$options = array();
foreach ( fluentcrm_get_custom_contact_fields() as $field ) {
$options[] = array(
'id' => $field['slug'],
'title' => $field['label'],
);
}
return array(
'title' => __( 'Field Updated', 'fluentcampaign-pro' ),
'sub_title' => __( 'This Funnel will start when the selected custom field is updated for a contact.', 'fluentcampaign-pro' ),
'fields' => array(
'field_name' => array(
'type' => 'select',
'options' => $options,
'label' => __( 'Field', 'fluentcampaign-pro' ),
'placeholder' => __( 'Select a field', 'fluentcampaign-pro' ),
),
),
);
}
/**
* Get the defaults for the funnel.
*
* @since 1.0.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
*/
public function getFunnelConditionDefaults( $funnel ) {
return array(
'update_type' => 'any',
'run_multiple' => 'no',
);
}
/**
* Get the conditional fields for the funnel.
*
* @since 1.0.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
*/
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',
),
),
'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.0.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[1];
$updated_data = $original_args[2];
$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
);
}
/**
* Is the action processable?
*
* @since 1.0.0
*
* @param FluentCrm\App\Models\Funnel $funnel The funnel.
* @param FluentCrm\App\Models\Subscriber $subscriber The subscriber.
* @param array $updated_data The updated custom fields.
* @return bool Whether or not the action is processable.
*/
private function isProcessable( $funnel, $subscriber, $updated_data ) {
$field = Arr::get( $funnel->settings, 'field_name' );
// Check if the correct field was updated.
if ( ! array_key_exists( $field, $updated_data ) ) {
return false;
}
$update_type = Arr::get( $funnel->conditions, 'update_type' );
// Check if we're looking for a specific value match.
if ( 'specific' === $update_type && Arr::get( $funnel->conditions, 'field_value' ) !== $updated_data[ $field ] ) {
return false;
}
// check run_only_once.
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;
}
}