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
← All changes | integrations/mailchimp/includes/actions/user-subscribe.php +212 -0 5.7.2 → 6.0.3 View file →
@@ -1,0 +1,212 @@
1 +<?php
2 +/**
3 + * User Subscribe
4 + *
5 + * @package AutomatorWP\Integrations\Mailchimp\Actions\User_Subscribe
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_Mailchimp_User_Subscribe extends AutomatorWP_Integration_Action {
13 +
14 + public $integration = 'mailchimp';
15 + public $action = 'mailchimp_user_subscribe';
16 +
17 + /**
18 + * Register the trigger
19 + *
20 + * @since 1.0.0
21 + */
22 + public function register() {
23 +
24 + automatorwp_register_action( $this->action, array(
25 + 'integration' => $this->integration,
26 + 'label' => __( 'Subscribe user to an audience', 'automatorwp' ),
27 + 'select_option' => __( 'Subscribe <strong>user</strong> to an <strong>audience</strong>', 'automatorwp' ),
28 + /* translators: %1$s: Audience. */
29 + 'edit_label' => sprintf( __( 'Subscribe user to %1$s', 'automatorwp' ), '{audience}' ),
30 + /* translators: %1$s: Audience. */
31 + 'log_label' => sprintf( __( 'Subscribe user to %1$s', 'automatorwp' ), '{audience}' ),
32 + 'options' => array(
33 + 'audience' => automatorwp_utilities_ajax_selector_option( array(
34 + 'field' => 'audience',
35 + 'option_default' => __( 'an audience', 'automatorwp' ),
36 + 'name' => __( 'Audience:', 'automatorwp' ),
37 + 'action_cb' => 'automatorwp_mailchimp_get_lists',
38 + 'options_cb' => 'automatorwp_mailchimp_options_cb_lists',
39 + 'default' => ''
40 + ) ),
41 + ),
42 + ) );
43 +
44 + }
45 +
46 + /**
47 + * Action execution function
48 + *
49 + * @since 1.0.0
50 + *
51 + * @param stdClass $action The action object
52 + * @param int $user_id The user ID
53 + * @param array $action_options The action's stored options (with tags already passed)
54 + * @param stdClass $automation The action's automation object
55 + */
56 + public function execute( $action, $user_id, $action_options, $automation ) {
57 +
58 + $this->result = '';
59 +
60 + // Shorthand
61 + $user = get_user_by ( 'ID', $user_id );
62 + $list_id = $action_options['audience'];
63 +
64 + // Get object to connect to API
65 + $mailchimp = automatorwp_mailchimp_get_authorization();
66 +
67 + // Bail if no authorization
68 + if ( ! $mailchimp ){
69 + return;
70 + }
71 +
72 + try {
73 +
74 + $response = $mailchimp->lists->addListMember($list_id, [
75 + "email_address" => $user->user_email,
76 + "status" => "subscribed",
77 + "merge_fields" => [
78 + "FNAME" => $user->first_name,
79 + "LNAME" => $user->last_name
80 + ]
81 + ]);
82 +
83 + $this->result = __( 'User added', 'automatorwp' );
84 +
85 + } catch (\GuzzleHttp\Exception\ClientException $e) {
86 +
87 + $response = $e->getResponse();
88 + $responseBodyAsString = $response->getBody()->getContents();
89 + $decoded = json_decode($responseBodyAsString);
90 + $this->result = $decoded->title;
91 + return;
92 +
93 + }
94 +
95 + }
96 +
97 + /**
98 + * Register required hooks
99 + *
100 + * @since 1.0.0
101 + */
102 + public function hooks() {
103 +
104 + // Configuration notice
105 + add_filter( 'automatorwp_automation_ui_after_item_label', array( $this, 'configuration_notice' ), 10, 2 );
106 +
107 + // Log meta data
108 + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 );
109 +
110 + // Log fields
111 + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 );
112 +
113 + parent::hooks();
114 +
115 + }
116 +
117 + /**
118 + * Configuration notice
119 + *
120 + * @since 1.0.0
121 + *
122 + * @param stdClass $object The trigger/action object
123 + * @param string $item_type The object type (trigger|action)
124 + */
125 + public function configuration_notice( $object, $item_type ) {
126 +
127 + // Bail if action type don't match this action
128 + if( $item_type !== 'action' ) {
129 + return;
130 + }
131 +
132 + if( $object->type !== $this->action ) {
133 + return;
134 + }
135 +
136 + // Warn user if the authorization has not been setup from settings
137 + if( ! automatorwp_mailchimp_get_authorization() ) : ?>
138 + <div class="automatorwp-notice-warning" style="margin-top: 10px; margin-bottom: 0;">
139 + <?php echo sprintf(
140 + __( 'You need to configure the <a href="%s" target="_blank">Mailchimp settings</a> to get this action to work.', 'automatorwp' ),
141 + get_admin_url() . 'admin.php?page=automatorwp_settings&tab=opt-tab-mailchimp'
142 + ); ?>
143 + <?php echo sprintf(
144 + __( '<a href="%s" target="_blank">Documentation</a>', 'automatorwp' ),
145 + 'https://automatorwp.com/docs/mailchimp/'
146 + ); ?>
147 + </div>
148 + <?php endif;
149 +
150 + }
151 +
152 + /**
153 + * Action custom log meta
154 + *
155 + * @since 1.0.0
156 + *
157 + * @param array $log_meta Log meta data
158 + * @param stdClass $action The action object
159 + * @param int $user_id The user ID
160 + * @param array $action_options The action's stored options (with tags already passed)
161 + * @param stdClass $automation The action's automation object
162 + *
163 + * @return array
164 + */
165 + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) {
166 +
167 + // Bail if action type don't match this action
168 + if( $action->type !== $this->action ) {
169 + return $log_meta;
170 + }
171 +
172 + // Store the action's result
173 + $log_meta['result'] = $this->result;
174 +
175 + return $log_meta;
176 + }
177 +
178 + /**
179 + * Action custom log fields
180 + *
181 + * @since 1.0.0
182 + *
183 + * @param array $log_fields The log fields
184 + * @param stdClass $log The log object
185 + * @param stdClass $object The trigger/action/automation object attached to the log
186 + *
187 + * @return array
188 + */
189 + public function log_fields( $log_fields, $log, $object ) {
190 +
191 + // Bail if log is not assigned to an action
192 + if( $log->type !== 'action' ) {
193 + return $log_fields;
194 + }
195 +
196 + // Bail if action type don't match this action
197 + if( $object->type !== $this->action ) {
198 + return $log_fields;
199 + }
200 +
201 + $log_fields['result'] = array(
202 + 'name' => __( 'Result:', 'automatorwp' ),
203 + 'type' => 'text',
204 + );
205 +
206 + return $log_fields;
207 + }
208 +
209 +
210 +}
211 +
212 +new AutomatorWP_Mailchimp_User_Subscribe();