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
8 months ago
add-note-to-company.php
8 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
8 months ago
create-list.php
8 months ago
create-sequence.php
8 months ago
create-tag.php
2 weeks ago
delete-campaign.php
8 months ago
delete-company.php
8 months ago
delete-list.php
8 months ago
delete-sequence.php
8 months ago
delete-tag.php
8 months ago
detach-subscribers-company.php
10 months ago
get-all-lists.php
8 months ago
get-all-tags.php
8 months ago
get-company-notes.php
8 months ago
get-contact-notes.php
8 months ago
get-contacts-who-clicked-email.php
8 months ago
get-contacts-who-did-not-open-email.php
8 months ago
get-contacts-who-opened-email.php
8 months ago
get-contacts-who-unsubscribed-email.php
8 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
8 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
8 months ago
update-template.php
10 months ago
send-email-by-template.php
360 lines
| 1 | <?php |
| 2 | /** |
| 3 | * SendEmailByTemplate. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category SendEmailByTemplate |
| 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 Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * SendEmailByTemplate |
| 22 | * |
| 23 | * @category SendEmailByTemplate |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | class SendEmailByTemplate extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'FluentCRM'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'fluentcrm_send_email_by_template'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register a action. |
| 50 | * |
| 51 | * @param array $actions actions. |
| 52 | * @return array |
| 53 | */ |
| 54 | public function register( $actions ) { |
| 55 | $actions[ $this->integration ][ $this->action ] = [ |
| 56 | 'label' => __( 'Send Email By Template', 'suretriggers' ), |
| 57 | 'action' => $this->action, |
| 58 | 'function' => [ $this, 'action_listener' ], |
| 59 | ]; |
| 60 | return $actions; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Action listener. |
| 65 | * |
| 66 | * @param int $user_id user_id. |
| 67 | * @param int $automation_id automation_id. |
| 68 | * @param array $fields fields. |
| 69 | * @param array $selected_options selectedOptions. |
| 70 | * |
| 71 | * @return array|void|mixed |
| 72 | * |
| 73 | * @throws Exception Exception. |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | // Check if FluentCRM is active. |
| 77 | if ( ! class_exists( '\FluentCrm\App\Models\Template' ) || ! class_exists( '\FluentCrm\App\Models\Campaign' ) ) { |
| 78 | return [ |
| 79 | 'status' => 'error', |
| 80 | 'message' => __( 'FluentCRM is not installed or activated.', 'suretriggers' ), |
| 81 | |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | $template_id = isset( $selected_options['template_id'] ) ? absint( $selected_options['template_id'] ) : 0; |
| 86 | $selected_list = $selected_options['list_id']; |
| 87 | $selected_tag = $selected_options['tag_id']; |
| 88 | |
| 89 | if ( ! $template_id ) { |
| 90 | return [ |
| 91 | 'status' => 'error', |
| 92 | 'message' => __( 'Template ID is required.', 'suretriggers' ), |
| 93 | |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | // Get the template. |
| 98 | $template = \FluentCrm\App\Models\Template::find( $template_id ); |
| 99 | |
| 100 | if ( ! $template ) { |
| 101 | return [ |
| 102 | 'status' => 'error', |
| 103 | 'message' => __( 'Template not found.', 'suretriggers' ), |
| 104 | |
| 105 | ]; |
| 106 | } |
| 107 | |
| 108 | $tags_lists = []; |
| 109 | |
| 110 | // Initialize with original values. |
| 111 | $tags_lists[] = [ |
| 112 | 'list' => $selected_list, |
| 113 | 'tag' => $selected_tag, |
| 114 | ]; |
| 115 | |
| 116 | // Process arrays if needed. |
| 117 | if ( is_array( $selected_list ) && is_array( $selected_tag ) ) { |
| 118 | $max_count = max( count( $selected_list ), count( $selected_tag ) ); |
| 119 | $tags_lists = []; |
| 120 | for ( $i = 0; $i < $max_count; $i++ ) { |
| 121 | $tags_lists[] = [ |
| 122 | 'list' => isset( $selected_list[ $i ]['value'] ) ? $selected_list[ $i ]['value'] : '', |
| 123 | 'tag' => isset( $selected_tag[ $i ]['value'] ) ? $selected_tag[ $i ]['value'] : '', |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | $campaign_title = isset( $selected_options['campaign_title'] ) ? sanitize_text_field( $selected_options['campaign_title'] ) : 'Campaign from template ' . $template->post_title . ' - ' . gmdate( 'Y-m-d H:i:s' ); |
| 129 | |
| 130 | $email_subject = isset( $selected_options['email_subject'] ) && ! empty( $selected_options['email_subject'] ) ? |
| 131 | sanitize_text_field( $selected_options['email_subject'] ) : get_post_meta( $template->ID, '_email_subject', true ); |
| 132 | |
| 133 | $from_name = isset( $selected_options['from_name'] ) ? sanitize_text_field( $selected_options['from_name'] ) : ''; |
| 134 | $from_email = isset( $selected_options['from_email'] ) ? sanitize_email( $selected_options['from_email'] ) : ''; |
| 135 | $reply_to_name = isset( $selected_options['reply_to_name'] ) ? sanitize_text_field( $selected_options['reply_to_name'] ) : ''; |
| 136 | $reply_to_email = isset( $selected_options['reply_to_email'] ) ? sanitize_email( $selected_options['reply_to_email'] ) : ''; |
| 137 | |
| 138 | $campaign_data = [ |
| 139 | 'title' => $campaign_title, |
| 140 | 'status' => 'draft', |
| 141 | 'template_id' => $template_id, |
| 142 | 'email_subject' => $email_subject, |
| 143 | 'email_pre_header' => '', |
| 144 | 'email_body' => $template->post_content, |
| 145 | 'settings' => [ |
| 146 | 'mailer_settings' => [ |
| 147 | 'from_name' => $from_name, |
| 148 | 'from_email' => $from_email, |
| 149 | 'reply_to_name' => $reply_to_name, |
| 150 | 'reply_to_email' => $reply_to_email, |
| 151 | 'is_custom' => 'yes', |
| 152 | ], |
| 153 | 'subscribers' => $tags_lists, |
| 154 | 'excludedSubscribers' => null, |
| 155 | 'sending_filter' => 'list_tag', |
| 156 | 'dynamic_segment' => [ |
| 157 | 'id' => '', |
| 158 | 'slug' => '', |
| 159 | ], |
| 160 | 'advanced_filters' => [ |
| 161 | [], |
| 162 | ], |
| 163 | ], |
| 164 | 'design_template' => get_post_meta( $template->ID, '_design_template', true ), |
| 165 | ]; |
| 166 | |
| 167 | $api_username = isset( $selected_options['api_username'] ) ? $selected_options['api_username'] : ''; |
| 168 | $api_password = isset( $selected_options['api_password'] ) ? $selected_options['api_password'] : ''; |
| 169 | $wordpress_url = isset( $selected_options['wordpress_url'] ) ? $selected_options['wordpress_url'] : site_url(); |
| 170 | |
| 171 | $header_data = [ |
| 172 | 'Content-Type' => 'application/json', |
| 173 | 'Authorization' => 'Basic ' . base64_encode( $api_username . ':' . $api_password ), |
| 174 | ]; |
| 175 | |
| 176 | $body_data = wp_json_encode( [ 'title' => $campaign_title ] ); |
| 177 | if ( false === $body_data ) { |
| 178 | $body_data = '{"title":"' . esc_js( $campaign_title ) . '"}'; |
| 179 | } |
| 180 | |
| 181 | $args = [ |
| 182 | 'headers' => $header_data, |
| 183 | 'sslverify' => false, |
| 184 | 'body' => $body_data, |
| 185 | ]; |
| 186 | |
| 187 | $new_campaign_request = wp_remote_post( $wordpress_url . '/wp-json/fluent-crm/v2/campaigns', $args ); |
| 188 | $response_code = wp_remote_retrieve_response_code( $new_campaign_request ); |
| 189 | $new_campaign_response_body = wp_remote_retrieve_body( $new_campaign_request ); |
| 190 | $response_context = json_decode( $new_campaign_response_body, true ); |
| 191 | |
| 192 | if ( 200 !== $response_code ) { |
| 193 | |
| 194 | $error_message = __( 'Failed to create campaign.', 'suretriggers' ); |
| 195 | |
| 196 | switch ( $response_code ) { |
| 197 | case 422: |
| 198 | $messages = []; |
| 199 | if ( is_array( $response_context ) ) { |
| 200 | foreach ( $response_context as $field_errors ) { |
| 201 | foreach ( $field_errors as $msg ) { |
| 202 | $messages[] = $msg; |
| 203 | } |
| 204 | } |
| 205 | $error_message = implode( ', ', $messages ); |
| 206 | } |
| 207 | break; |
| 208 | default: |
| 209 | $error_message = __( 'Failed to create campaign.', 'suretriggers' ); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | return [ |
| 214 | 'status' => 'error', |
| 215 | 'message' => __( 'Failed to create campaign.', 'suretriggers' ), |
| 216 | |
| 217 | ]; |
| 218 | } |
| 219 | |
| 220 | $settings_data = wp_json_encode( |
| 221 | [ |
| 222 | 'title' => $campaign_title, |
| 223 | 'email_body' => $template->post_content, |
| 224 | 'email_subject' => $email_subject, |
| 225 | 'settings' => [ |
| 226 | 'mailer_settings' => [ |
| 227 | 'from_name' => $from_name, |
| 228 | 'is_custom' => 'yes', |
| 229 | 'from_email' => $from_email, |
| 230 | 'reply_to_name' => $reply_to_name, |
| 231 | 'reply_to_email' => $reply_to_email, |
| 232 | ], |
| 233 | 'subscribers' => $tags_lists, |
| 234 | 'excludedSubscribers' => null, |
| 235 | 'sending_filter' => 'list_tag', |
| 236 | 'dynamic_segment' => [ |
| 237 | 'id' => '', |
| 238 | 'slug' => '', |
| 239 | ], |
| 240 | 'advanced_filters' => [ |
| 241 | [], |
| 242 | ], |
| 243 | ], |
| 244 | ] |
| 245 | ); |
| 246 | |
| 247 | if ( false === $settings_data ) { |
| 248 | return [ |
| 249 | 'status' => 'error', |
| 250 | 'message' => __( 'Failed to encode campaign settings data.', 'suretriggers' ), |
| 251 | |
| 252 | ]; |
| 253 | } |
| 254 | |
| 255 | $args = [ |
| 256 | 'headers' => $header_data, |
| 257 | 'sslverify' => false, |
| 258 | 'method' => 'PUT', |
| 259 | 'body' => $settings_data, |
| 260 | ]; |
| 261 | |
| 262 | if ( ! is_array( $response_context ) || ! isset( $response_context['id'] ) ) { |
| 263 | return [ |
| 264 | 'status' => 'error', |
| 265 | 'message' => __( 'Invalid campaign response: missing ID.', 'suretriggers' ), |
| 266 | |
| 267 | ]; |
| 268 | } |
| 269 | |
| 270 | $settings_request = wp_remote_request( $wordpress_url . '/wp-json/fluent-crm/v2/campaigns/' . $response_context['id'], $args ); |
| 271 | $settings_response_code = wp_remote_retrieve_response_code( $settings_request ); |
| 272 | $settings_response_body = wp_remote_retrieve_body( $settings_request ); |
| 273 | $settings_context = json_decode( $settings_response_body, true ); |
| 274 | |
| 275 | if ( 200 !== $settings_response_code ) { |
| 276 | return [ |
| 277 | 'status' => 'error', |
| 278 | 'message' => __( 'Failed to update campaign settings.', 'suretriggers' ), |
| 279 | |
| 280 | ]; |
| 281 | } |
| 282 | |
| 283 | $contact_body_data = [ |
| 284 | 'subscribers' => $tags_lists, |
| 285 | 'sending_filter' => 'list_tag', |
| 286 | ]; |
| 287 | $contact_body = wp_json_encode( $contact_body_data ); |
| 288 | |
| 289 | if ( false === $contact_body ) { |
| 290 | return [ |
| 291 | 'status' => 'error', |
| 292 | 'message' => __( 'Failed to encode contact data.', 'suretriggers' ), |
| 293 | |
| 294 | ]; |
| 295 | } |
| 296 | |
| 297 | $check_estimated_contacts = wp_remote_post( |
| 298 | $wordpress_url . '/wp-json/fluent-crm/v2/campaigns/estimated-contacts', |
| 299 | [ |
| 300 | 'headers' => $header_data, |
| 301 | 'sslverify' => false, |
| 302 | 'body' => $contact_body, |
| 303 | ] |
| 304 | ); |
| 305 | $contacts = wp_remote_retrieve_body( $check_estimated_contacts ); |
| 306 | $contacts_context = json_decode( $contacts, true ); |
| 307 | |
| 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 | $scheduled_at = isset( $selected_options['scheduled_at'] ) ? sanitize_text_field( $selected_options['scheduled_at'] ) : gmdate( 'Y-m-d H:i:s' ); |
| 317 | |
| 318 | $schedule_data = wp_json_encode( [ 'scheduled_at' => $scheduled_at ] ); |
| 319 | if ( false === $schedule_data ) { |
| 320 | $schedule_data = '{"scheduled_at":"' . esc_js( $scheduled_at ) . '"}'; |
| 321 | } |
| 322 | |
| 323 | if ( ! is_array( $settings_context ) || ! isset( $settings_context['campaign'] ) || ! isset( $settings_context['campaign']['id'] ) ) { |
| 324 | return [ |
| 325 | 'status' => 'error', |
| 326 | 'message' => __( 'Invalid campaign ID.', 'suretriggers' ), |
| 327 | |
| 328 | ]; |
| 329 | } |
| 330 | |
| 331 | $final_request = wp_remote_post( |
| 332 | $wordpress_url . '/wp-json/fluent-crm/v2/campaigns/' . $settings_context['campaign']['id'] . '/schedule', |
| 333 | [ |
| 334 | 'headers' => $header_data, |
| 335 | 'sslverify' => false, |
| 336 | 'body' => $schedule_data, |
| 337 | ] |
| 338 | ); |
| 339 | |
| 340 | $final_response_body = wp_remote_retrieve_body( $final_request ); |
| 341 | $final_context = json_decode( $final_response_body, true ); |
| 342 | |
| 343 | if ( is_wp_error( $final_request ) ) { |
| 344 | return [ |
| 345 | 'status' => 'error', |
| 346 | 'message' => __( 'Failed to schedule campaign.', 'suretriggers' ), |
| 347 | |
| 348 | ]; |
| 349 | } |
| 350 | |
| 351 | if ( is_array( $final_context ) ) { |
| 352 | $final_context['template_id'] = $template_id; |
| 353 | } |
| 354 | |
| 355 | return $final_context; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | SendEmailByTemplate::get_instance(); |
| 360 |