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