add-contact-to-list.php
8 months ago
add-tag-to-contact.php
8 months ago
create-list.php
8 months ago
create-tag.php
8 months ago
delete-contact.php
8 months ago
delete-list.php
8 months ago
delete-tag.php
8 months ago
get-all-lists.php
8 months ago
get-all-segments.php
8 months ago
get-all-tags.php
8 months ago
get-contact-by-email.php
8 months ago
get-contacts-by-list.php
8 months ago
get-contacts-by-tag.php
8 months ago
remove-contact-from-list.php
8 months ago
remove-tag-from-contact.php
8 months ago
add-contact-to-list.php
386 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AddContactToList. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category AddContactToList |
| 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\MailerPress\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * AddContactToList |
| 22 | * |
| 23 | * @category AddContactToList |
| 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 AddContactToList extends AutomateAction { |
| 31 | |
| 32 | /** |
| 33 | * Integration type. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | public $integration = 'MailerPress'; |
| 38 | |
| 39 | /** |
| 40 | * Action name. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | public $action = 'add_contact_to_list'; |
| 45 | |
| 46 | use SingletonLoader; |
| 47 | |
| 48 | /** |
| 49 | * Register a action. |
| 50 | * |
| 51 | * @param array $actions actions. |
| 52 | * |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'Add contact to list', 'suretriggers' ), |
| 58 | 'action' => $this->action, |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | return $actions; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Action listener. |
| 66 | * |
| 67 | * @param int $user_id user_id. |
| 68 | * @param int $automation_id automation_id. |
| 69 | * @param array $fields fields. |
| 70 | * @param array $selected_options selectedOptions. |
| 71 | * @return array|void |
| 72 | * @throws Exception Exception. |
| 73 | */ |
| 74 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 75 | // Get form data. |
| 76 | $email = isset( $selected_options['email'] ) ? sanitize_email( $selected_options['email'] ) : ''; |
| 77 | $first_name = isset( $selected_options['first_name'] ) ? sanitize_text_field( $selected_options['first_name'] ) : ''; |
| 78 | $last_name = isset( $selected_options['last_name'] ) ? sanitize_text_field( $selected_options['last_name'] ) : ''; |
| 79 | $subscription_status = isset( $selected_options['subscription_status'] ) ? sanitize_text_field( $selected_options['subscription_status'] ) : 'subscribed'; |
| 80 | $lists = isset( $selected_options['lists'] ) ? $selected_options['lists'] : []; |
| 81 | $tags = isset( $selected_options['tags'] ) ? $selected_options['tags'] : []; |
| 82 | |
| 83 | // Convert string to array if needed. |
| 84 | if ( is_string( $lists ) ) { |
| 85 | $lists = [ $lists ]; |
| 86 | } |
| 87 | if ( is_string( $tags ) ) { |
| 88 | $tags = [ $tags ]; |
| 89 | } |
| 90 | if ( empty( $email ) ) { |
| 91 | return [ |
| 92 | 'status' => 'error', |
| 93 | 'message' => 'Email is required.', |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | if ( ! is_email( $email ) ) { |
| 98 | return [ |
| 99 | 'status' => 'error', |
| 100 | 'message' => 'Please enter a valid email address.', |
| 101 | ]; |
| 102 | } |
| 103 | |
| 104 | try { |
| 105 | global $wpdb; |
| 106 | |
| 107 | // MailerPress table names. |
| 108 | $contact_table = $wpdb->prefix . 'mailerpress_contact'; |
| 109 | $contact_lists_table = $wpdb->prefix . 'mailerpress_contact_lists'; |
| 110 | $contact_tags_table = $wpdb->prefix . 'mailerpress_contact_tags'; |
| 111 | |
| 112 | // Check if contact already exists. |
| 113 | $existing_contact = $wpdb->get_row( |
| 114 | $wpdb->prepare( |
| 115 | "SELECT * FROM {$wpdb->prefix}mailerpress_contact WHERE email = %s", |
| 116 | |
| 117 | ) |
| 118 | ); |
| 119 | |
| 120 | if ( $existing_contact ) { |
| 121 | // Update existing contact. |
| 122 | $wpdb->update( |
| 123 | $contact_table, |
| 124 | [ |
| 125 | 'first_name' => $first_name, |
| 126 | 'last_name' => $last_name, |
| 127 | 'subscription_status' => $subscription_status, |
| 128 | 'updated_at' => current_time( 'mysql' ), |
| 129 | ], |
| 130 | [ 'contact_id' => $existing_contact->contact_id ], |
| 131 | [ '%s', '%s', '%s', '%s' ], |
| 132 | [ '%d' ] |
| 133 | ); |
| 134 | |
| 135 | $contact_id = $existing_contact->contact_id; |
| 136 | |
| 137 | // Fire action hook. |
| 138 | do_action( 'mailerpress_contact_updated', $contact_id ); |
| 139 | } else { |
| 140 | // Create new contact. |
| 141 | $unsubscribe_token = wp_generate_uuid4(); |
| 142 | |
| 143 | $result = $wpdb->insert( |
| 144 | $contact_table, |
| 145 | [ |
| 146 | 'email' => $email, |
| 147 | 'first_name' => $first_name, |
| 148 | 'last_name' => $last_name, |
| 149 | 'subscription_status' => $subscription_status, |
| 150 | 'created_at' => current_time( 'mysql' ), |
| 151 | 'updated_at' => current_time( 'mysql' ), |
| 152 | 'unsubscribe_token' => $unsubscribe_token, |
| 153 | 'opt_in_source' => '', |
| 154 | 'opt_in_details' => '', |
| 155 | 'access_token' => bin2hex( openssl_random_pseudo_bytes( 32 ) ), |
| 156 | ], |
| 157 | [ '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ] |
| 158 | ); |
| 159 | |
| 160 | if ( false === $result ) { |
| 161 | return [ |
| 162 | 'status' => 'error', |
| 163 | 'message' => 'Failed to create contact.', |
| 164 | ]; |
| 165 | } |
| 166 | |
| 167 | $contact_id = $wpdb->insert_id; |
| 168 | |
| 169 | // Fire action hook. |
| 170 | do_action( 'mailerpress_contact_created', $contact_id ); |
| 171 | } |
| 172 | |
| 173 | // Add contact to lists. |
| 174 | if ( ! empty( $lists ) ) { |
| 175 | foreach ( $lists as $list ) { |
| 176 | $list_id = 0; |
| 177 | |
| 178 | // Check if list is an ID or name. |
| 179 | if ( is_array( $list ) ) { |
| 180 | if ( isset( $list['value'] ) && ! empty( $list['value'] ) ) { |
| 181 | $list_id = absint( $list['value'] ); |
| 182 | } elseif ( isset( $list['id'] ) && ! empty( $list['id'] ) ) { |
| 183 | $list_id = absint( $list['id'] ); |
| 184 | } elseif ( isset( $list['name'] ) && ! empty( $list['name'] ) ) { |
| 185 | // Search for list by name. |
| 186 | $existing_list = $wpdb->get_row( |
| 187 | $wpdb->prepare( |
| 188 | "SELECT list_id FROM {$wpdb->prefix}mailerpress_lists WHERE name = %s", |
| 189 | sanitize_text_field( $list['name'] ) |
| 190 | ) |
| 191 | ); |
| 192 | |
| 193 | if ( $existing_list ) { |
| 194 | $list_id = $existing_list->list_id; |
| 195 | } else { |
| 196 | // Create new list. |
| 197 | $new_list_result = $wpdb->insert( |
| 198 | $wpdb->prefix . 'mailerpress_lists', |
| 199 | [ |
| 200 | 'name' => sanitize_text_field( $list['name'] ), |
| 201 | 'description' => '', |
| 202 | 'created_at' => current_time( 'mysql' ), |
| 203 | 'updated_at' => current_time( 'mysql' ), |
| 204 | ], |
| 205 | [ '%s', '%s', '%s', '%s' ] |
| 206 | ); |
| 207 | |
| 208 | if ( $new_list_result ) { |
| 209 | $list_id = $wpdb->insert_id; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } elseif ( is_numeric( $list ) ) { |
| 214 | $list_id = absint( $list ); |
| 215 | } elseif ( is_string( $list ) ) { |
| 216 | // Search for list by name. |
| 217 | $existing_list = $wpdb->get_row( |
| 218 | $wpdb->prepare( |
| 219 | "SELECT list_id FROM {$wpdb->prefix}mailerpress_lists WHERE name = %s", |
| 220 | sanitize_text_field( $list ) |
| 221 | ) |
| 222 | ); |
| 223 | |
| 224 | if ( $existing_list ) { |
| 225 | $list_id = $existing_list->list_id; |
| 226 | } else { |
| 227 | // Create new list. |
| 228 | $new_list_result = $wpdb->insert( |
| 229 | $wpdb->prefix . 'mailerpress_lists', |
| 230 | [ |
| 231 | 'name' => sanitize_text_field( $list ), |
| 232 | 'description' => '', |
| 233 | 'created_at' => current_time( 'mysql' ), |
| 234 | 'updated_at' => current_time( 'mysql' ), |
| 235 | ], |
| 236 | [ '%s', '%s', '%s', '%s' ] |
| 237 | ); |
| 238 | |
| 239 | if ( $new_list_result ) { |
| 240 | $list_id = $wpdb->insert_id; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | if ( $list_id > 0 ) { |
| 246 | // Check if already in list. |
| 247 | $existing_contact_list = $wpdb->get_row( |
| 248 | $wpdb->prepare( |
| 249 | "SELECT * FROM {$wpdb->prefix}mailerpress_contact_lists WHERE contact_id = %d AND list_id = %d", |
| 250 | $contact_id, |
| 251 | $list_id |
| 252 | ) |
| 253 | ); |
| 254 | |
| 255 | if ( ! $existing_contact_list ) { |
| 256 | $wpdb->insert( |
| 257 | $contact_lists_table, |
| 258 | [ |
| 259 | 'contact_id' => $contact_id, |
| 260 | 'list_id' => $list_id, |
| 261 | ], |
| 262 | [ '%d', '%d' ] |
| 263 | ); |
| 264 | |
| 265 | // Fire action hook. |
| 266 | do_action( 'mailerpress_contact_list_added', $contact_id, $list_id ); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Add contact to tags. |
| 273 | if ( ! empty( $tags ) ) { |
| 274 | foreach ( $tags as $tag ) { |
| 275 | $tag_id = 0; |
| 276 | |
| 277 | // Check if tag is an ID or name. |
| 278 | if ( is_array( $tag ) ) { |
| 279 | if ( isset( $tag['value'] ) && ! empty( $tag['value'] ) ) { |
| 280 | $tag_id = absint( $tag['value'] ); |
| 281 | } elseif ( isset( $tag['id'] ) && ! empty( $tag['id'] ) ) { |
| 282 | $tag_id = absint( $tag['id'] ); |
| 283 | } elseif ( isset( $tag['name'] ) && ! empty( $tag['name'] ) ) { |
| 284 | // Search for tag by name. |
| 285 | $existing_tag = $wpdb->get_row( |
| 286 | $wpdb->prepare( |
| 287 | "SELECT tag_id FROM {$wpdb->prefix}mailerpress_tags WHERE name = %s", |
| 288 | sanitize_text_field( $tag['name'] ) |
| 289 | ) |
| 290 | ); |
| 291 | |
| 292 | if ( $existing_tag ) { |
| 293 | $tag_id = $existing_tag->tag_id; |
| 294 | } else { |
| 295 | // Create new tag. |
| 296 | $new_tag_result = $wpdb->insert( |
| 297 | $wpdb->prefix . 'mailerpress_tags', |
| 298 | [ |
| 299 | 'name' => sanitize_text_field( $tag['name'] ), |
| 300 | ], |
| 301 | [ '%s' ] |
| 302 | ); |
| 303 | |
| 304 | if ( $new_tag_result ) { |
| 305 | $tag_id = $wpdb->insert_id; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } elseif ( is_numeric( $tag ) ) { |
| 310 | $tag_id = absint( $tag ); |
| 311 | } elseif ( is_string( $tag ) ) { |
| 312 | // Search for tag by name. |
| 313 | $existing_tag = $wpdb->get_row( |
| 314 | $wpdb->prepare( |
| 315 | "SELECT tag_id FROM {$wpdb->prefix}mailerpress_tags WHERE name = %s", |
| 316 | sanitize_text_field( $tag ) |
| 317 | ) |
| 318 | ); |
| 319 | |
| 320 | if ( $existing_tag ) { |
| 321 | $tag_id = $existing_tag->tag_id; |
| 322 | } else { |
| 323 | // Create new tag. |
| 324 | $new_tag_result = $wpdb->insert( |
| 325 | $wpdb->prefix . 'mailerpress_tags', |
| 326 | [ |
| 327 | 'name' => sanitize_text_field( $tag ), |
| 328 | ], |
| 329 | [ '%s' ] |
| 330 | ); |
| 331 | |
| 332 | if ( $new_tag_result ) { |
| 333 | $tag_id = $wpdb->insert_id; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | if ( $tag_id > 0 ) { |
| 339 | // Check if already has tag. |
| 340 | $existing_contact_tag = $wpdb->get_row( |
| 341 | $wpdb->prepare( |
| 342 | "SELECT * FROM {$wpdb->prefix}mailerpress_contact_tags WHERE contact_id = %d AND tag_id = %d", |
| 343 | $contact_id, |
| 344 | $tag_id |
| 345 | ) |
| 346 | ); |
| 347 | |
| 348 | if ( ! $existing_contact_tag ) { |
| 349 | $wpdb->insert( |
| 350 | $contact_tags_table, |
| 351 | [ |
| 352 | 'contact_id' => $contact_id, |
| 353 | 'tag_id' => $tag_id, |
| 354 | ], |
| 355 | [ '%d', '%d' ] |
| 356 | ); |
| 357 | |
| 358 | // Fire action hook. |
| 359 | do_action( 'mailerpress_contact_tag_added', $contact_id, $tag_id ); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return [ |
| 366 | 'contact_id' => $contact_id, |
| 367 | 'email' => $email, |
| 368 | 'first_name' => $first_name, |
| 369 | 'last_name' => $last_name, |
| 370 | 'subscription_status' => $subscription_status, |
| 371 | 'lists_added' => $lists, |
| 372 | 'tags_added' => $tags, |
| 373 | 'success' => true, |
| 374 | ]; |
| 375 | |
| 376 | } catch ( Exception $e ) { |
| 377 | return [ |
| 378 | 'status' => 'error', |
| 379 | 'message' => $e->getMessage(), |
| 380 | ]; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | AddContactToList::get_instance(); |
| 386 |