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
get-contact-by-email.php
252 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GetContactByEmail. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category GetContactByEmail |
| 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 | * GetContactByEmail |
| 22 | * |
| 23 | * @category GetContactByEmail |
| 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 GetContactByEmail 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 = 'get_contact_by_email'; |
| 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' => __( 'Get contact by email', '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 | $include_lists = isset( $selected_options['include_lists'] ) ? (bool) $selected_options['include_lists'] : false; |
| 78 | $include_tags = isset( $selected_options['include_tags'] ) ? (bool) $selected_options['include_tags'] : false; |
| 79 | $include_custom_fields = isset( $selected_options['include_custom_fields'] ) ? (bool) $selected_options['include_custom_fields'] : false; |
| 80 | |
| 81 | if ( empty( $email ) ) { |
| 82 | return [ |
| 83 | 'status' => 'error', |
| 84 | 'message' => 'Email address is required.', |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | if ( ! is_email( $email ) ) { |
| 89 | return [ |
| 90 | 'status' => 'error', |
| 91 | 'message' => 'Please enter a valid email address.', |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | global $wpdb; |
| 97 | |
| 98 | // Get contact by email. |
| 99 | $contact = $wpdb->get_row( |
| 100 | $wpdb->prepare( |
| 101 | "SELECT contact_id, email, first_name, last_name, subscription_status, opt_in_source, opt_in_details, unsubscribe_token, access_token, created_at, updated_at |
| 102 | FROM {$wpdb->prefix}mailerpress_contact |
| 103 | WHERE email = %s", |
| 104 | |
| 105 | ) |
| 106 | ); |
| 107 | |
| 108 | if ( ! $contact ) { |
| 109 | return [ |
| 110 | 'status' => 'error', |
| 111 | 'message' => 'Contact not found with the provided email address.', |
| 112 | ]; |
| 113 | } |
| 114 | |
| 115 | // Format basic contact data. |
| 116 | $contact_data = [ |
| 117 | 'contact_id' => (int) $contact->contact_id, |
| 118 | 'email' => $contact->email, |
| 119 | 'first_name' => $contact->first_name, |
| 120 | 'last_name' => $contact->last_name, |
| 121 | 'subscription_status' => $contact->subscription_status, |
| 122 | 'opt_in_source' => $contact->opt_in_source, |
| 123 | 'opt_in_details' => $contact->opt_in_details, |
| 124 | 'unsubscribe_token' => $contact->unsubscribe_token, |
| 125 | 'access_token' => $contact->access_token, |
| 126 | 'created_at' => $contact->created_at, |
| 127 | 'updated_at' => $contact->updated_at, |
| 128 | ]; |
| 129 | |
| 130 | // Get lists if requested. |
| 131 | if ( $include_lists ) { |
| 132 | $lists = $wpdb->get_results( |
| 133 | $wpdb->prepare( |
| 134 | "SELECT l.list_id, l.name, l.description, l.created_at, l.updated_at |
| 135 | FROM {$wpdb->prefix}mailerpress_lists l |
| 136 | INNER JOIN {$wpdb->prefix}mailerpress_contact_lists cl ON l.list_id = cl.list_id |
| 137 | WHERE cl.contact_id = %d |
| 138 | ORDER BY l.name ASC", |
| 139 | $contact->contact_id |
| 140 | ) |
| 141 | ); |
| 142 | |
| 143 | $formatted_lists = []; |
| 144 | if ( $lists ) { |
| 145 | foreach ( $lists as $list ) { |
| 146 | $formatted_lists[] = [ |
| 147 | 'list_id' => (int) $list->list_id, |
| 148 | 'name' => $list->name, |
| 149 | 'description' => $list->description, |
| 150 | 'created_at' => $list->created_at, |
| 151 | 'updated_at' => $list->updated_at, |
| 152 | ]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | $contact_data['lists'] = $formatted_lists; |
| 157 | } |
| 158 | |
| 159 | // Get tags if requested. |
| 160 | if ( $include_tags ) { |
| 161 | $tags = $wpdb->get_results( |
| 162 | $wpdb->prepare( |
| 163 | "SELECT t.tag_id, t.name |
| 164 | FROM {$wpdb->prefix}mailerpress_tags t |
| 165 | INNER JOIN {$wpdb->prefix}mailerpress_contact_tags ct ON t.tag_id = ct.tag_id |
| 166 | WHERE ct.contact_id = %d |
| 167 | ORDER BY t.name ASC", |
| 168 | $contact->contact_id |
| 169 | ) |
| 170 | ); |
| 171 | |
| 172 | $formatted_tags = []; |
| 173 | if ( $tags ) { |
| 174 | foreach ( $tags as $tag ) { |
| 175 | $formatted_tags[] = [ |
| 176 | 'tag_id' => (int) $tag->tag_id, |
| 177 | 'name' => $tag->name, |
| 178 | ]; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | $contact_data['tags'] = $formatted_tags; |
| 183 | } |
| 184 | |
| 185 | // Get custom fields if requested. |
| 186 | if ( $include_custom_fields ) { |
| 187 | $custom_fields_table = $wpdb->prefix . 'mailerpress_contact_custom_fields'; |
| 188 | $table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $custom_fields_table ) ); |
| 189 | |
| 190 | if ( $table_exists ) { |
| 191 | $custom_fields = $wpdb->get_results( |
| 192 | $wpdb->prepare( |
| 193 | "SELECT field_key, field_value |
| 194 | FROM {$wpdb->prefix}mailerpress_contact_custom_fields |
| 195 | WHERE contact_id = %d |
| 196 | ORDER BY field_key ASC", |
| 197 | $contact->contact_id |
| 198 | ) |
| 199 | ); |
| 200 | |
| 201 | $formatted_custom_fields = []; |
| 202 | if ( $custom_fields ) { |
| 203 | foreach ( $custom_fields as $field ) { |
| 204 | $formatted_custom_fields[ $field->field_key ] = $field->field_value; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | $contact_data['custom_fields'] = $formatted_custom_fields; |
| 209 | } else { |
| 210 | $contact_data['custom_fields'] = []; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Get some additional stats. |
| 215 | $stats = []; |
| 216 | |
| 217 | // Count lists. |
| 218 | $list_count = $wpdb->get_var( |
| 219 | $wpdb->prepare( |
| 220 | "SELECT COUNT(*) FROM {$wpdb->prefix}mailerpress_contact_lists WHERE contact_id = %d", |
| 221 | $contact->contact_id |
| 222 | ) |
| 223 | ); |
| 224 | $stats['list_count'] = (int) $list_count; |
| 225 | |
| 226 | // Count tags. |
| 227 | $tag_count = $wpdb->get_var( |
| 228 | $wpdb->prepare( |
| 229 | "SELECT COUNT(*) FROM {$wpdb->prefix}mailerpress_contact_tags WHERE contact_id = %d", |
| 230 | $contact->contact_id |
| 231 | ) |
| 232 | ); |
| 233 | $stats['tag_count'] = (int) $tag_count; |
| 234 | |
| 235 | $contact_data['stats'] = $stats; |
| 236 | |
| 237 | return [ |
| 238 | 'contact' => $contact_data, |
| 239 | 'success' => true, |
| 240 | ]; |
| 241 | |
| 242 | } catch ( Exception $e ) { |
| 243 | return [ |
| 244 | 'status' => 'error', |
| 245 | 'message' => $e->getMessage(), |
| 246 | ]; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | GetContactByEmail::get_instance(); |
| 252 |