add-contact.php
2 months ago
add-email-to-sequence.php
10 months ago
add-event.php
10 months ago
add-lists-to-contact.php
9 months ago
add-note-to-company.php
9 months ago
add-note.php
5 months ago
add-tag-to-contact.php
10 months ago
attach-subscribers-company.php
10 months ago
create-campaign.php
4 months ago
create-company.php
10 months ago
create-email-template.php
9 months ago
create-list.php
9 months ago
create-sequence.php
9 months ago
create-tag.php
2 weeks ago
delete-campaign.php
9 months ago
delete-company.php
9 months ago
delete-list.php
9 months ago
delete-sequence.php
9 months ago
delete-tag.php
9 months ago
detach-subscribers-company.php
10 months ago
get-all-lists.php
9 months ago
get-all-tags.php
9 months ago
get-company-notes.php
9 months ago
get-contact-notes.php
9 months ago
get-contacts-who-clicked-email.php
9 months ago
get-contacts-who-did-not-open-email.php
9 months ago
get-contacts-who-opened-email.php
9 months ago
get-contacts-who-unsubscribed-email.php
9 months ago
list-contacts-by-list-id.php
7 months ago
list-email-templates.php
10 months ago
list-sequences.php
10 months ago
remove-contact.php
10 months ago
remove-subscriber-from-sequence.php
9 months ago
remove-tag-from-contact.php
10 months ago
retrieve-contact-by-email.php
10 months ago
retrieve-contact-by-id.php
10 months ago
retrieve-contact-by-list-ids.php
10 months ago
retrieve-contact-by-segment.php
10 months ago
retrieve-contact-by-status.php
10 months ago
retrieve-contact-by-tag-ids.php
10 months ago
retrieve-contact-by-user-id.php
10 months ago
send-email-by-campaign.php
4 months ago
send-email-by-template.php
10 months ago
send-email-to-list-contacts.php
4 months ago
update-contact-status.php
9 months ago
update-template.php
10 months ago
send-email-to-list-contacts.php
334 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SendEmailToListContacts. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SendEmailToListContacts |
| 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 | * SendEmailToListContacts |
| 23 | * |
| 24 | * @category SendEmailToListContacts |
| 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 SendEmailToListContacts 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_to_list_contacts'; |
| 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', '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 | // Remove backslash escaping from email body to prevent image URLs |
| 242 | // from being wrapped with %5C%22 (URL-encoded \") in the campaign. |
| 243 | $email_body = isset( $selected_options['email_body'] ) ? wp_unslash( $selected_options['email_body'] ) : ''; |
| 244 | |
| 245 | $args['body'] = wp_json_encode( |
| 246 | [ |
| 247 | 'title' => $response_context['title'], |
| 248 | 'email_body' => $email_body, |
| 249 | 'email_subject' => $selected_options['email_subject'], |
| 250 | 'settings' => [ |
| 251 | 'mailer_settings' => [ |
| 252 | 'from_name' => $selected_options['from_name'], |
| 253 | 'is_custom' => 'yes', |
| 254 | 'from_email' => $selected_options['from_email'], |
| 255 | 'reply_to_name' => '', |
| 256 | 'reply_to_email' => '', |
| 257 | ], |
| 258 | 'subscribers' => $tags_lists, |
| 259 | 'excludedSubscribers' => null, |
| 260 | 'sending_filter' => 'list_tag', |
| 261 | 'dynamic_segment' => [ |
| 262 | 'id' => '', |
| 263 | 'slug' => '', |
| 264 | ], |
| 265 | 'advanced_filters' => [ |
| 266 | [], |
| 267 | ], |
| 268 | ], |
| 269 | ] |
| 270 | ); |
| 271 | } |
| 272 | /** |
| 273 | * |
| 274 | * Ignore line |
| 275 | * |
| 276 | * @phpstan-ignore-next-line |
| 277 | */ |
| 278 | $settings_request = wp_remote_request( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/' . $response_context['id'], $args ); |
| 279 | $settings_response_code = wp_remote_retrieve_response_code( $settings_request ); |
| 280 | $settings_response_body = wp_remote_retrieve_body( $settings_request ); |
| 281 | $settings_context = json_decode( $settings_response_body, true ); |
| 282 | if ( 200 !== $settings_response_code ) { |
| 283 | return $settings_context; |
| 284 | } |
| 285 | if ( ! empty( $args['body'] ) ) { |
| 286 | $args = [ |
| 287 | 'headers' => $header_data, |
| 288 | 'sslverify' => false, |
| 289 | 'body' => $args['body'], |
| 290 | ]; |
| 291 | } |
| 292 | $contact_body_data = [ |
| 293 | 'subscribers' => $tags_lists, |
| 294 | 'sending_filter' => 'list_tag', |
| 295 | ]; |
| 296 | $contact_body = wp_json_encode( $contact_body_data ); |
| 297 | if ( $contact_body ) { |
| 298 | $check_estimated_contacts = wp_remote_post( |
| 299 | $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/estimated-contacts', |
| 300 | [ |
| 301 | 'headers' => $header_data, |
| 302 | 'sslverify' => false, |
| 303 | 'body' => $contact_body, |
| 304 | ] |
| 305 | ); |
| 306 | $contacts = wp_remote_retrieve_body( $check_estimated_contacts ); |
| 307 | $contacts_context = json_decode( $contacts, true ); |
| 308 | if ( is_array( $contacts_context ) && 0 == $contacts_context['count'] ) { |
| 309 | return [ |
| 310 | 'status' => 'error', |
| 311 | 'message' => __( 'No contacts found based on your selection!!', 'suretriggers' ), |
| 312 | |
| 313 | ]; |
| 314 | } |
| 315 | } |
| 316 | /** |
| 317 | * |
| 318 | * Ignore line |
| 319 | * |
| 320 | * @phpstan-ignore-next-line |
| 321 | */ |
| 322 | $final_request = wp_remote_post( $selected_options['wordpress_url'] . '/wp-json/fluent-crm/v2/campaigns/' . $settings_context['campaign']['id'] . '/schedule', $args ); |
| 323 | $final_response_body = wp_remote_retrieve_body( $final_request ); |
| 324 | $final_context = json_decode( $final_response_body, true ); |
| 325 | if ( is_wp_error( $final_request ) ) { |
| 326 | return $final_request->errors; |
| 327 | } |
| 328 | return $final_context; |
| 329 | } |
| 330 | |
| 331 | } |
| 332 | |
| 333 | SendEmailToListContacts::get_instance(); |
| 334 |