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
create-campaign.php
212 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateCampaign. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateCampaign |
| 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 | use FluentCrm\App\Models\Campaign; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * CreateCampaign |
| 24 | * |
| 25 | * @category CreateCampaign |
| 26 | * @package SureTriggers |
| 27 | * @author BSF <username@example.com> |
| 28 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 29 | * @link https://www.brainstormforce.com/ |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | class CreateCampaign extends AutomateAction { |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Integration type. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $integration = 'FluentCRM'; |
| 41 | |
| 42 | /** |
| 43 | * Action name. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $action = 'fluentcrm_create_campaign'; |
| 48 | |
| 49 | use SingletonLoader; |
| 50 | |
| 51 | /** |
| 52 | * Register a action. |
| 53 | * |
| 54 | * @param array $actions actions. |
| 55 | * @return array |
| 56 | */ |
| 57 | public function register( $actions ) { |
| 58 | |
| 59 | $actions[ $this->integration ][ $this->action ] = [ |
| 60 | 'label' => __( 'Create Campaign', 'suretriggers' ), |
| 61 | 'action' => $this->action, |
| 62 | 'function' => [ $this, 'action_listener' ], |
| 63 | ]; |
| 64 | return $actions; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Action listener. |
| 69 | * |
| 70 | * @param int $user_id user_id. |
| 71 | * @param int $automation_id automation_id. |
| 72 | * @param array $fields fields. |
| 73 | * @param array $selected_options selectedOptions. |
| 74 | * @return array |
| 75 | * @throws Exception Exception. |
| 76 | */ |
| 77 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 78 | if ( ! function_exists( 'FluentCrmApi' ) ) { |
| 79 | return [ |
| 80 | 'status' => 'error', |
| 81 | 'message' => __( 'FluentCRM is not active.', 'suretriggers' ), |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | if ( ! class_exists( 'FluentCrm\App\Models\Campaign' ) ) { |
| 86 | return [ |
| 87 | 'status' => 'error', |
| 88 | 'message' => __( 'FluentCRM Campaign model not available.', 'suretriggers' ), |
| 89 | ]; |
| 90 | } |
| 91 | |
| 92 | $campaign_name = isset( $selected_options['campaign_name'] ) ? sanitize_text_field( $selected_options['campaign_name'] ) : ''; |
| 93 | |
| 94 | if ( empty( $campaign_name ) ) { |
| 95 | return [ |
| 96 | 'status' => 'error', |
| 97 | 'message' => __( 'Campaign name is required.', 'suretriggers' ), |
| 98 | ]; |
| 99 | } |
| 100 | |
| 101 | // Optional email fields. |
| 102 | $email_subject = isset( $selected_options['email_subject'] ) ? sanitize_text_field( $selected_options['email_subject'] ) : ''; |
| 103 | $email_pre_header = isset( $selected_options['email_pre_header'] ) ? sanitize_text_field( $selected_options['email_pre_header'] ) : ''; |
| 104 | $html_content = isset( $selected_options['html_content'] ) ? wp_kses_post( $selected_options['html_content'] ) : ''; |
| 105 | |
| 106 | // Recipient lists/tags — paired by index, matching the send-email actions' convention. |
| 107 | $selected_list = isset( $selected_options['recipient_lists'] ) ? $selected_options['recipient_lists'] : 'all'; |
| 108 | $selected_tag = isset( $selected_options['recipient_tags'] ) ? $selected_options['recipient_tags'] : 'all'; |
| 109 | |
| 110 | $subscribers[] = [ |
| 111 | 'list' => is_numeric( $selected_list ) ? (string) $selected_list : $selected_list, |
| 112 | 'tag' => is_numeric( $selected_tag ) ? (string) $selected_tag : $selected_tag, |
| 113 | ]; |
| 114 | |
| 115 | if ( is_array( $selected_list ) && is_array( $selected_tag ) ) { |
| 116 | $max_count = max( count( $selected_list ), count( $selected_tag ) ); |
| 117 | $subscribers = []; |
| 118 | for ( $i = 0; $i < $max_count; $i++ ) { |
| 119 | $raw_list = isset( $selected_list[ $i ]['value'] ) ? $selected_list[ $i ]['value'] : ''; |
| 120 | $raw_tag = isset( $selected_tag[ $i ]['value'] ) ? $selected_tag[ $i ]['value'] : ''; |
| 121 | $subscribers[] = [ |
| 122 | 'list' => is_numeric( $raw_list ) ? (string) $raw_list : $raw_list, |
| 123 | 'tag' => is_numeric( $raw_tag ) ? (string) $raw_tag : $raw_tag, |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Excluded lists/tags — same pairing convention. |
| 129 | $excluded_list = isset( $selected_options['excluded_lists'] ) ? $selected_options['excluded_lists'] : ''; |
| 130 | $excluded_tag = isset( $selected_options['excluded_tags'] ) ? $selected_options['excluded_tags'] : ''; |
| 131 | |
| 132 | $excluded_subscribers = null; |
| 133 | if ( ! empty( $excluded_list ) || ! empty( $excluded_tag ) ) { |
| 134 | $excluded_subscribers[] = [ |
| 135 | 'list' => is_numeric( $excluded_list ) ? (string) $excluded_list : $excluded_list, |
| 136 | 'tag' => is_numeric( $excluded_tag ) ? (string) $excluded_tag : $excluded_tag, |
| 137 | ]; |
| 138 | |
| 139 | if ( is_array( $excluded_list ) && is_array( $excluded_tag ) ) { |
| 140 | $max_count = max( count( $excluded_list ), count( $excluded_tag ) ); |
| 141 | $excluded_subscribers = []; |
| 142 | for ( $i = 0; $i < $max_count; $i++ ) { |
| 143 | $raw_list = isset( $excluded_list[ $i ]['value'] ) ? $excluded_list[ $i ]['value'] : ''; |
| 144 | $raw_tag = isset( $excluded_tag[ $i ]['value'] ) ? $excluded_tag[ $i ]['value'] : ''; |
| 145 | $excluded_subscribers[] = [ |
| 146 | 'list' => is_numeric( $raw_list ) ? (string) $raw_list : $raw_list, |
| 147 | 'tag' => is_numeric( $raw_tag ) ? (string) $raw_tag : $raw_tag, |
| 148 | ]; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Build base campaign data — no settings here so boot() applies all FluentCRM defaults. |
| 154 | $campaign_data = [ |
| 155 | 'title' => $campaign_name, |
| 156 | ]; |
| 157 | |
| 158 | if ( ! empty( $email_subject ) ) { |
| 159 | $campaign_data['email_subject'] = $email_subject; |
| 160 | } |
| 161 | |
| 162 | if ( ! empty( $email_pre_header ) ) { |
| 163 | $campaign_data['email_pre_header'] = $email_pre_header; |
| 164 | } |
| 165 | |
| 166 | if ( ! empty( $html_content ) ) { |
| 167 | $campaign_data['email_body'] = $html_content; |
| 168 | } |
| 169 | |
| 170 | try { |
| 171 | $campaign = Campaign::create( $campaign_data ); |
| 172 | |
| 173 | if ( ! $campaign ) { |
| 174 | return [ |
| 175 | 'status' => 'error', |
| 176 | 'message' => __( 'Failed to create campaign.', 'suretriggers' ), |
| 177 | ]; |
| 178 | } |
| 179 | |
| 180 | // Merge subscriber settings into boot() defaults (mailer_settings, template_config etc.) |
| 181 | // using the same wp_parse_args pattern as FluentCRM's own draftRecipients(). |
| 182 | $subscriber_settings = [ |
| 183 | 'subscribers' => $subscribers, |
| 184 | 'excludedSubscribers' => $excluded_subscribers, |
| 185 | 'sending_filter' => 'list_tag', |
| 186 | ]; |
| 187 | $campaign->settings = wp_parse_args( $subscriber_settings, is_array( $campaign->settings ) ? $campaign->settings : [] ); |
| 188 | $campaign->save(); |
| 189 | |
| 190 | return [ |
| 191 | 'status' => 'success', |
| 192 | 'message' => sprintf( __( 'Campaign "%s" created successfully.', 'suretriggers' ), $campaign_name ), |
| 193 | 'campaign_id' => $campaign->id, |
| 194 | 'campaign_title' => $campaign->title, |
| 195 | 'campaign_slug' => $campaign->slug, |
| 196 | 'campaign_status' => $campaign->status, |
| 197 | 'campaign_type' => isset( $campaign->type ) ? $campaign->type : 'campaign', |
| 198 | 'created_at' => $campaign->created_at, |
| 199 | ]; |
| 200 | |
| 201 | } catch ( Exception $e ) { |
| 202 | return [ |
| 203 | 'status' => 'error', |
| 204 | 'message' => sprintf( __( 'Error creating campaign: %s', 'suretriggers' ), $e->getMessage() ), |
| 205 | ]; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
| 211 | CreateCampaign::get_instance(); |
| 212 |