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