/
fcrm-add-user-to-list.php
157 lines (132 loc) · 4.08 KB
/
fcrm-add-user-to-list.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
<?php
namespace Uncanny_Automator;
/**
* Class FCRM_ADD_USER_TO_LIST
*
* @package Uncanny_Automator
*/
class FCRM_ADD_USER_TO_LIST {
/**
* Integration code
*
* @var string
*/
public static $integration = 'FCRM';
/**
* @var string
*/
private $trigger_code;
/**
* @var string
*/
private $trigger_meta;
/**
* Set up Automator trigger constructor.
*/
public function __construct() {
$this->trigger_code = 'FCRMUSERLIST';
$this->trigger_meta = 'FCRMLIST';
$this->define_trigger();
}
/**
* Define and register the trigger by pushing it into the Automator object
*/
public function define_trigger() {
$trigger = array(
'author' => Automator()->get_author_name( $this->trigger_code ),
'support_link' => Automator()->get_author_support_link( $this->trigger_code, 'integration/fluentcrm/' ),
'integration' => self::$integration,
'code' => $this->trigger_code,
/* translators: Logged-in trigger - Fluent Forms */
'sentence' => sprintf( esc_attr_x( 'A user is added to {{a list:%1$s}}', 'Fluent Forms', 'uncanny-automator' ), $this->trigger_meta ),
/* translators: Logged-in trigger - Fluent Forms */
'select_option_name' => esc_attr_x( 'A user is added to {{a list}}', 'Fluent Forms', 'uncanny-automator' ),
'action' => 'fluentcrm_contact_added_to_lists',
'priority' => 20,
'accepted_args' => 2,
'validation_function' => array( $this, 'contact_added_to_lists' ),
'options_callback' => array( $this, 'load_options' ),
);
Automator()->register->trigger( $trigger );
}
/**
* @return array[]
*/
public function load_options() {
return Automator()->utilities->keep_order_of_options(
array(
'options' => array(
Automator()->helpers->recipe->fluent_crm->options->fluent_crm_lists(),
),
)
);
}
/**
* @param $attached_list_ids
* @param $subscriber
*
* @return void
*/
public function contact_added_to_lists( $attached_list_ids, $subscriber ) {
$user_id = $subscriber->user_id;
if ( 0 === $user_id ) {
// There is no wp user associated with the subscriber
return;
}
$list_ids = Automator()
->helpers
->recipe
->fluent_crm
->get_attached_list_ids( $attached_list_ids );
if ( empty( $list_ids ) ) {
// sanity check
return;
}
$matched_recipes = Automator()
->helpers
->recipe
->fluent_crm
->match_single_condition( $list_ids, 'int', $this->trigger_meta, $this->trigger_code );
if ( ! empty( $matched_recipes ) ) {
foreach ( $matched_recipes as $matched_recipe ) {
if ( ! Automator()->is_recipe_completed( $matched_recipe->recipe_id, $user_id ) ) {
$args = array(
'code' => $this->trigger_code,
'meta' => $this->trigger_meta,
'recipe_to_match' => $matched_recipe->recipe_id,
'ignore_post_id' => true,
'user_id' => $user_id,
);
$result = Automator()->maybe_add_trigger_entry( $args, false );
if ( $result ) {
foreach ( $result as $r ) {
if ( true === $r['result'] ) {
if ( isset( $r['args'] ) && isset( $r['args']['get_trigger_id'] ) ) {
$insert = array(
'user_id' => $user_id,
'trigger_id' => (int) $r['args']['trigger_id'],
'trigger_log_id' => $r['args']['get_trigger_id'],
'meta_key' => $this->trigger_meta,
'meta_value' => maybe_serialize( $matched_recipe->matched_value ),
'run_number' => $r['args']['run_number'],
);
Automator()->insert_trigger_meta( $insert );
$insert = array(
'user_id' => $user_id,
'trigger_id' => (int) $r['args']['trigger_id'],
'trigger_log_id' => $r['args']['get_trigger_id'],
'meta_key' => 'subscriber_id',
'meta_value' => $subscriber->id,
'run_number' => $r['args']['run_number'],
);
Automator()->insert_trigger_meta( $insert );
}
Automator()->maybe_trigger_complete( $r['args'] );
}
}
}
}
}
}
}
}