PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.35
OttoKit: All-in-One Automation Platform v1.1.35
1.1.37 1.1.36 1.1.35 1.1.34 1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / fluentcrm / actions / send-email-by-campaign.php
suretriggers / src / Integrations / fluentcrm / actions Last commit date
add-contact.php 3 weeks ago add-email-to-sequence.php 11 months ago add-event.php 11 months ago add-lists-to-contact.php 10 months ago add-note-to-company.php 10 months ago add-note.php 6 months ago add-tag-to-contact.php 11 months ago attach-subscribers-company.php 11 months ago create-campaign.php 5 months ago create-company.php 3 weeks ago create-email-template.php 10 months ago create-list.php 10 months ago create-sequence.php 10 months ago create-tag.php 1 month ago delete-campaign.php 10 months ago delete-company.php 10 months ago delete-list.php 10 months ago delete-sequence.php 10 months ago delete-tag.php 10 months ago detach-subscribers-company.php 11 months ago get-all-lists.php 10 months ago get-all-tags.php 10 months ago get-company-notes.php 10 months ago get-contact-notes.php 10 months ago get-contacts-who-clicked-email.php 10 months ago get-contacts-who-did-not-open-email.php 10 months ago get-contacts-who-opened-email.php 10 months ago get-contacts-who-unsubscribed-email.php 10 months ago list-contacts-by-list-id.php 8 months ago list-email-templates.php 11 months ago list-sequences.php 11 months ago remove-contact.php 11 months ago remove-subscriber-from-sequence.php 10 months ago remove-tag-from-contact.php 11 months ago retrieve-contact-by-email.php 11 months ago retrieve-contact-by-id.php 11 months ago retrieve-contact-by-list-ids.php 11 months ago retrieve-contact-by-segment.php 11 months ago retrieve-contact-by-status.php 11 months ago retrieve-contact-by-tag-ids.php 11 months ago retrieve-contact-by-user-id.php 11 months ago send-email-by-campaign.php 5 months ago send-email-by-template.php 11 months ago send-email-to-list-contacts.php 5 months ago update-contact-status.php 10 months ago update-template.php 11 months ago
send-email-by-campaign.php
355 lines
1 <?php
2 /**
3 * SendEmailByCampaign.
4 * php version 5.6
5 *
6 * @category SendEmailByCampaign
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\FluentCRM\Actions;
15
16 use DateTime;
17 use Exception;
18 use SureTriggers\Integrations\AutomateAction;
19 use SureTriggers\Traits\SingletonLoader;
20
21 /**
22 * SendEmailByCampaign
23 *
24 * @category SendEmailByCampaign
25 * @package SureTriggers
26 * @author BSF <username@example.com>
27 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
28 * @link https://www.brainstormforce.com/
29 * @since 1.0.0
30 */
31 class SendEmailByCampaign extends AutomateAction {
32
33
34 /**
35 * Integration type.
36 *
37 * @var string
38 */
39 public $integration = 'FluentCRM';
40
41 /**
42 * Action name.
43 *
44 * @var string
45 */
46 public $action = 'fluentcrm_send_email_by_campaign';
47
48 use SingletonLoader;
49
50 /**
51 * Register a action.
52 *
53 * @param array $actions actions.
54 * @return array
55 */
56 public function register( $actions ) {
57
58 $actions[ $this->integration ][ $this->action ] = [
59 'label' => __( 'Send Email By Campaign', 'suretriggers' ),
60 'action' => $this->action,
61 'function' => [ $this, 'action_listener' ],
62 ];
63 return $actions;
64 }
65
66 /**
67 * Action listener.
68 *
69 * @param int $user_id user_id.
70 * @param int $automation_id automation_id.
71 * @param array $fields fields.
72 * @param array $selected_options selectedOptions.
73 *
74 * @return array|void|mixed
75 *
76 * @throws Exception Exception.
77 */
78 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
79
80 $username = $selected_options['api_username'];
81 $password = $selected_options['api_password'];
82
83 $selected_list = $selected_options['list_id'];
84 $selected_tag = $selected_options['tag_id'];
85
86 $tags_lists[] = [
87 'list' => $selected_list,
88 'tag' => $selected_tag,
89 ];
90
91 if ( is_array( $selected_list ) && is_array( $selected_tag ) ) {
92 $max_count = max( count( $selected_list ), count( $selected_tag ) );
93 $tags_lists = [];
94 for ( $i = 0; $i < $max_count; $i++ ) {
95 $tags_lists[] = [
96 'list' => $selected_list[ $i ]['value'] ? $selected_list[ $i ]['value'] : '',
97 'tag' => $selected_tag[ $i ]['value'] ? $selected_tag[ $i ]['value'] : '',
98 ];
99 }
100 }
101
102 $header_data = [
103 'Content-Type' => 'application/json',
104 'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
105 ];
106
107 // Check if selected list not exists then return error.
108 if ( 'all' != $selected_list ) {
109 $args = [
110 'headers' => $header_data,
111 'sslverify' => false,
112 ];
113 if ( is_array( $selected_list ) ) {
114 $all_lists_request = wp_remote_get( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/lists/', $args );
115 $all_lists_response_body = wp_remote_retrieve_body( $all_lists_request );
116 $all_list_response_context = json_decode( $all_lists_response_body, true );
117 if ( is_array( $all_list_response_context ) && ! empty( $all_list_response_context['lists'] ) ) {
118 $list_ids = array_map(
119 function ( $list ) {
120 return $list['id'];
121 },
122 $all_list_response_context['lists']
123 );
124 $filtered_lists = array_filter(
125 $selected_list,
126 function ( $list ) {
127 return 'all' !== $list['value'];
128 }
129 );
130 $ids = array_column( $filtered_lists, 'value' );
131 if ( ! empty( array_diff( $ids, $list_ids ) ) ) {
132 return [
133 'status' => 'error',
134 'message' => __( "Selected List doesn't exists!!", 'suretriggers' ),
135
136 ];
137 }
138 }
139 } else {
140 $list_request = wp_remote_get( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/lists/' . $selected_list, $args );
141 $list_response_body = wp_remote_retrieve_body( $list_request );
142 $list_response_context = json_decode( $list_response_body, true );
143 if ( '' == $list_response_context ) {
144 return [
145 'status' => 'error',
146 'message' => __( "Selected List doesn't exists!!", 'suretriggers' ),
147
148 ];
149 }
150 }
151 }
152
153 // Check if selected tag not exists then return error.
154 if ( 'all' != $selected_tag ) {
155 $args = [
156 'headers' => $header_data,
157 'sslverify' => false,
158 ];
159 if ( is_array( $selected_tag ) ) {
160 $all_tags_request = wp_remote_get( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/tags/', $args );
161 $all_tags_response_body = wp_remote_retrieve_body( $all_tags_request );
162 $all_tag_response_context = json_decode( $all_tags_response_body, true );
163 if ( is_array( $all_tag_response_context ) && ! empty( $all_tag_response_context['tags']['data'] ) ) {
164 $tag_ids = array_map(
165 function ( $tag ) {
166 return $tag['id'];
167 },
168 $all_tag_response_context['tags']['data']
169 );
170 $filtered_tags = array_filter(
171 $selected_tag,
172 function ( $tag ) {
173 return 'all' !== $tag['value'];
174 }
175 );
176 $ids = array_column( $filtered_tags, 'value' );
177 if ( ! empty( array_diff( $ids, $tag_ids ) ) ) {
178 return [
179 'status' => 'error',
180 'message' => __( "Selected Tag doesn't exists!!", 'suretriggers' ),
181
182 ];
183 }
184 }
185 } else {
186 $tags_response = wp_remote_get( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/tags/' . $selected_tag, $args );
187 $tags_body = wp_remote_retrieve_body( $tags_response );
188 $tags_context = json_decode( $tags_body, true );
189 if ( is_array( $tags_context ) && '' == $tags_context['tag'] ) {
190 return [
191 'status' => 'error',
192 'message' => __( "Selected Tag doesn't exists!!", 'suretriggers' ),
193
194 ];
195 }
196 }
197 }
198 // Check if selected campaign not exists then create new campaign.
199 $args = [
200 'headers' => $header_data,
201 'sslverify' => false,
202 ];
203 $request = wp_remote_get( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/' . $selected_options['campaign'], $args );
204 $response_code = wp_remote_retrieve_response_code( $request );
205 $response_body = wp_remote_retrieve_body( $request );
206 $response_context = json_decode( $response_body, true );
207 if ( ! empty( $response_context ) && is_array( $response_context ) && isset( $response_context['campaign'] ) ) {
208 $response_context = $response_context['campaign'];
209 }
210 // Campaign not exists, so create new one.
211 if ( 404 === $response_code ) {
212 $args = [
213 'headers' => $header_data,
214 'sslverify' => false,
215 'body' => wp_json_encode( [ 'title' => $selected_options['campaign'] ] ),
216 ];
217 /**
218 *
219 * Ignore line
220 *
221 * @phpstan-ignore-next-line
222 */
223 $new_campaign_request = wp_remote_post( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns', $args );
224 $response_code = wp_remote_retrieve_response_code( $new_campaign_request );
225 $new_campaign_response_body = wp_remote_retrieve_body( $new_campaign_request );
226 $response_context = json_decode( $new_campaign_response_body, true );
227 if ( 200 !== $response_code ) {
228 return $response_context;
229 }
230 } elseif ( 200 !== $response_code ) {
231 return $response_context;
232 }
233 // Prepare email body.
234 $args = array_merge(
235 $args,
236 [
237 'method' => 'PUT',
238 ]
239 );
240 if ( is_array( $response_context ) ) {
241 // Use campaign values if email body, subject, from name, and from email are not provided.
242 // Remove backslash escaping from email body to prevent image URLs
243 // from being wrapped with %5C%22 (URL-encoded \") in the campaign.
244 $email_body = isset( $selected_options['email_body'] ) && ! empty( $selected_options['email_body'] ) ?
245 wp_unslash( $selected_options['email_body'] ) : ( isset( $response_context['email_body'] ) ? $response_context['email_body'] : '' );
246
247 $email_subject = isset( $selected_options['email_subject'] ) && ! empty( $selected_options['email_subject'] ) ?
248 $selected_options['email_subject'] : ( isset( $response_context['email_subject'] ) ? $response_context['email_subject'] : '' );
249
250 $from_name = isset( $selected_options['from_name'] ) && ! empty( $selected_options['from_name'] ) ?
251 $selected_options['from_name'] : ( isset( $response_context['settings']['mailer_settings']['from_name'] ) ?
252 $response_context['settings']['mailer_settings']['from_name'] : '' );
253
254 $from_email = isset( $selected_options['from_email'] ) && ! empty( $selected_options['from_email'] ) ?
255 $selected_options['from_email'] : ( isset( $response_context['settings']['mailer_settings']['from_email'] ) ?
256 $response_context['settings']['mailer_settings']['from_email'] : '' );
257
258 $reply_to_name = isset( $selected_options['reply_to_name'] ) && ! empty( $selected_options['reply_to_name'] ) ?
259 $selected_options['reply_to_name'] : ( isset( $response_context['settings']['mailer_settings']['reply_to_name'] ) ?
260 $response_context['settings']['mailer_settings']['reply_to_name'] : '' );
261
262 $reply_to_email = isset( $selected_options['reply_to_email'] ) && ! empty( $selected_options['reply_to_email'] ) ?
263 $selected_options['reply_to_email'] : ( isset( $response_context['settings']['mailer_settings']['reply_to_email'] ) ?
264 $response_context['settings']['mailer_settings']['reply_to_email'] : '' );
265
266 $args['body'] = wp_json_encode(
267 [
268 'title' => $response_context['title'],
269 'email_body' => $email_body,
270 'email_subject' => $email_subject,
271 'settings' => [
272 'mailer_settings' => [
273 'from_name' => $from_name,
274 'is_custom' => 'yes',
275 'from_email' => $from_email,
276 'reply_to_name' => $reply_to_name,
277 'reply_to_email' => $reply_to_email,
278 ],
279 'subscribers' => $tags_lists,
280 'excludedSubscribers' => null,
281 'sending_filter' => 'list_tag',
282 'dynamic_segment' => [
283 'id' => '',
284 'slug' => '',
285 ],
286 'advanced_filters' => [
287 [],
288 ],
289 ],
290 ]
291 );
292 }
293 /**
294 *
295 * Ignore line
296 *
297 * @phpstan-ignore-next-line
298 */
299 $settings_request = wp_remote_request( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/' . $response_context['id'], $args );
300 $settings_response_code = wp_remote_retrieve_response_code( $settings_request );
301 $settings_response_body = wp_remote_retrieve_body( $settings_request );
302 $settings_context = json_decode( $settings_response_body, true );
303 if ( 200 !== $settings_response_code ) {
304 return $settings_context;
305 }
306 if ( ! empty( $args['body'] ) ) {
307 $args = [
308 'headers' => $header_data,
309 'sslverify' => false,
310 'body' => $args['body'],
311 ];
312 }
313 $contact_body_data = [
314 'subscribers' => $tags_lists,
315 'sending_filter' => 'list_tag',
316 ];
317 $contact_body = wp_json_encode( $contact_body_data );
318 if ( $contact_body ) {
319 $check_estimated_contacts = wp_remote_post(
320 $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/estimated-contacts',
321 [
322 'headers' => $header_data,
323 'sslverify' => false,
324 'body' => $contact_body,
325 ]
326 );
327 $contacts = wp_remote_retrieve_body( $check_estimated_contacts );
328 $contacts_context = json_decode( $contacts, true );
329 if ( is_array( $contacts_context ) && 0 == $contacts_context['count'] ) {
330 return [
331 'status' => 'error',
332 'message' => __( 'No contacts found based on your selection!!', 'suretriggers' ),
333
334 ];
335 }
336 }
337 /**
338 *
339 * Ignore line
340 *
341 * @phpstan-ignore-next-line
342 */
343 $final_request = wp_remote_post( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/' . $settings_context['campaign']['id'] . '/schedule', $args );
344 $final_response_body = wp_remote_retrieve_body( $final_request );
345 $final_context = json_decode( $final_response_body, true );
346 if ( is_wp_error( $final_request ) ) {
347 return $final_request->errors;
348 }
349 return $final_context;
350 }
351
352 }
353
354 SendEmailByCampaign::get_instance();
355