PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.4
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.4
6.0.4 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 All 35 releases
automatorwp / integrations / newsletter / includes / triggers / anonymous-subscribe-list.php

anonymous-subscribe-list.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.4, at integrations/newsletter/includes/triggers/anonymous-subscribe-list.php

322 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Anonymous Subscribe List
4 *
5 * @package AutomatorWP\Integrations\Newsletter\Triggers\Anonymous_Subscribe_List
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_Newsletter_Anonymous_Subscribe_List extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'newsletter';
15 public $trigger = 'newsletter_anonymous_subscribe_list';
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 subscribes to a list', 'automatorwp' ),
28 'select_option' => __( 'Guest subscribes to a <strong>list</strong>', 'automatorwp' ),
29 /* translators: %1$s: List title. %2$s: Number of times. */
30 'edit_label' => sprintf( __( 'Guest subscribes to %1$s %2$s time(s)', 'automatorwp' ), '{list}', '{times}' ),
31 /* translators: %1$s: List title. */
32 'log_label' => sprintf( __( 'Guest subscribes to %1$s', 'automatorwp' ), '{list}' ),
33 'action' => 'newsletter_user_post_subscribe',
34 'function' => array( $this, 'listener' ),
35 'priority' => 10,
36 'accepted_args' => 1,
37 'options' => array(
38 'list' => array(
39 'from' => 'list',
40 'fields' => array(
41 'list' => array(
42 'name' => __( 'List:', 'automatorwp' ),
43 'type' => 'select',
44 'classes' => 'automatorwp-selector',
45 'options_cb' => array( $this, 'lists_options_cb' ),
46 'default' => 'any'
47 )
48 )
49 ),
50 'times' => automatorwp_utilities_times_option(),
51 ),
52 'tags' => array_merge(
53 array(
54 'newsletter_user_email' => array(
55 'label' => __( 'User email', 'automatorwp' ),
56 'type' => 'text',
57 'preview' => __( '[email protected]', 'automatorwp' ),
58 ),
59 ),
60 automatorwp_utilities_post_tags(),
61 automatorwp_utilities_times_tag()
62 )
63 ) );
64
65 }
66
67 /**
68 * Get lists options
69 *
70 * @since 1.0.0
71 *
72 * @return array
73 */
74 public function lists_options_cb() {
75
76 $options = array(
77 'any' => __( 'any list', 'automatorwp' ),
78 );
79
80 if ( class_exists( '\Newsletter' ) ) {
81
82 $lists = \Newsletter::instance()->get_lists();
83
84 if ( ! empty( $lists ) ) {
85 foreach ( $lists as $list ) {
86 $options['list_' . $list->id] = $list->name;
87 }
88 }
89
90 }
91
92 return $options;
93
94 }
95
96 /**
97 * Trigger listener
98 *
99 * @since 1.0.0
100 *
101 * @param object $user
102 */
103 public function listener( $user ) {
104
105 global $wpdb;
106
107 $user_id = $user->id;
108 $user_email = $user->email;
109
110 // Get the last user log
111 $logs_table = $wpdb->prefix . 'newsletter_user_logs';
112 $log = $wpdb->get_row( "SELECT MAX(id), data FROM {$logs_table} WHERE user_id = {$user_id} AND source = 'subscribe'" );
113
114 // Bail if log not found
115 if ( $log === null ) {
116 return;
117 }
118
119 // Check if there is the log data
120 if ( ! isset( $log->data ) && $log->data !== null ) {
121 return;
122 }
123
124 if ( $log->data === null ) {
125 return;
126 }
127
128 // Get the lists subscribed
129 $lists = json_decode( $log->data, true );
130
131 foreach( $lists as $list_id => $status ) {
132
133 // Possible statuses are:
134 // 1 - Subscribed
135 // 0 - Not subscribed
136 // C - Confirmed
137
138 // Bail if user not subscribed to this list
139 if( $status !== '1' ) {
140 continue;
141 }
142
143 // Trigger the list subscribed
144 automatorwp_trigger_event( array(
145 'trigger' => $this->trigger,
146 'user_id' => $user_id,
147 'user_email' => $user_email,
148 'list_id' => $list_id,
149 ) );
150 }
151
152 }
153
154 /**
155 * User deserves check
156 *
157 * @since 1.0.0
158 *
159 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
160 * @param stdClass $trigger The trigger object
161 * @param int $user_id The user ID
162 * @param array $event Event information
163 * @param array $trigger_options The trigger's stored options
164 * @param stdClass $automation The trigger's automation object
165 *
166 * @return bool True if user deserves trigger, false otherwise
167 */
168 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
169
170 // Don't deserve if post is not received
171 if( ! isset( $event['list_id'] ) ) {
172 return false;
173 }
174
175 // Shorthand
176 $list_id = $trigger_options['list'];
177
178 // Don't deserve if list doesn't match with the trigger option
179 if( $list_id !== 'any' && $list_id !== $event['list_id'] ) {
180 return false;
181 }
182
183 return $deserves_trigger;
184
185 }
186
187 /**
188 * Register the required hooks
189 *
190 * @since 1.0.0
191 */
192 public function hooks() {
193
194 // Log meta data
195 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
196
197 // Log fields
198 add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
199
200 // Trigger tags replacements
201 add_filter( 'automatorwp_trigger_tags_replacements', array( $this, 'tags_replacements' ), 10, 4 );
202
203 parent::hooks();
204 }
205
206 /**
207 * Trigger custom log meta
208 *
209 * @since 1.0.0
210 *
211 * @param array $log_meta Log meta data
212 * @param stdClass $trigger The trigger object
213 * @param int $user_id The user ID
214 * @param array $event Event information
215 * @param array $trigger_options The trigger's stored options
216 * @param stdClass $automation The trigger's automation object
217 *
218 * @return array
219 */
220 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
221
222 // Bail if action type don't match this action
223 if( $trigger->type !== $this->trigger ) {
224 return $log_meta;
225 }
226
227 $log_meta['user_email'] = ( isset( $event['user_email'] ) ? $event['user_email'] : '' );
228 $log_meta['list_id'] = ( isset( $event['list_id'] ) ? $event['list_id'] : '' );
229
230 return $log_meta;
231
232 }
233
234 /**
235 * Action custom log fields
236 *
237 * @since 1.0.0
238 *
239 * @param array $log_fields The log fields
240 * @param stdClass $log The log object
241 * @param stdClass $object The trigger/action/automation object attached to the log
242 *
243 * @return array
244 */
245 public function log_fields( $log_fields, $log, $object ) {
246
247 // Bail if log is not assigned to an trigger
248 if( $log->type !== 'trigger' ) {
249 return $log_fields;
250 }
251
252 // Bail if trigger type don't match this trigger
253 if( $object->type !== $this->trigger ) {
254 return $log_fields;
255 }
256
257 $log_fields['user_email'] = array(
258 'name' => __( 'User email', 'automatorwp' ),
259 'desc' => __( 'Email used to get subscribed.', 'automatorwp' ),
260 'type' => 'text',
261 );
262
263 $log_fields['list_id'] = array(
264 'name' => __( 'List subscribed', 'automatorwp' ),
265 'desc' => __( 'List subscribed.', 'automatorwp' ),
266 'type' => 'text',
267 'before_field' => array( $this, 'list_display_cb' ),
268 );
269
270 return $log_fields;
271
272 }
273
274 /**
275 * Callback used to render list on logs
276 *
277 * @since 1.0.0
278 *
279 * @param array $field_args
280 * @param CMB2_Field $field
281 */
282 public function list_display_cb( $field_args, $field ) {
283
284 $list_id = $value = $field->value();
285
286 $lists = $this->lists_options_cb();
287
288 echo ( isset( $lists[$list_id] ) ? $lists[$list_id] : sprintf( __( 'List with ID %s not found.', 'automatorwp' ), $list_id ) );
289
290 }
291
292 /**
293 * Filter to setup custom trigger tags replacements
294 *
295 * Note: Post and times tags replacements are already passed
296 *
297 * @since 1.0.0
298 *
299 * @param array $replacements The trigger replacements
300 * @param stdClass $trigger The trigger object
301 * @param int $user_id The user ID
302 * @param stdClass $log The last trigger log object
303 *
304 * @return array
305 */
306 public function tags_replacements( $replacements, $trigger, $user_id, $log ) {
307
308 // Bail if trigger type don't match this trigger
309 if( $trigger->type !== $this->trigger ) {
310 return $replacements;
311 }
312
313 // Times replacement by default
314 $replacements['newsletter_user_email'] = ct_get_object_meta( $log->id, 'user_email', true );
315
316 return $replacements;
317
318 }
319
320 }
321
322 new AutomatorWP_Newsletter_Anonymous_Subscribe_List();