gh-add-tag-to-contact.php
11 months ago
gh-create-contact.php
9 months ago
gh-remove-tag-from-contact.php
11 months ago
gh-create-contact.php
330 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GhCreateContact. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category GhCreateContact |
| 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\Groundhogg\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use SureTriggers\Integrations\AutomateAction; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | /** |
| 21 | * GhCreateContact |
| 22 | * |
| 23 | * @category GhCreateContact |
| 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 GhCreateContact extends AutomateAction { |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'Groundhogg'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'gh_create_contact'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | |
| 57 | $actions[ $this->integration ][ $this->action ] = [ |
| 58 | 'label' => __( 'Create/Update Contact', 'suretriggers' ), |
| 59 | 'action' => $this->action, |
| 60 | 'function' => [ $this, 'action_listener' ], |
| 61 | ]; |
| 62 | return $actions; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Action listener. |
| 67 | * |
| 68 | * @param int $user_id user_id. |
| 69 | * @param int $automation_id automation_id. |
| 70 | * @param array $fields fields. |
| 71 | * @param array $selected_options selectedOptions. |
| 72 | * @return array |
| 73 | * @throws Exception Exception. |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | $email = sanitize_email( $selected_options['email'] ); |
| 77 | $api_key = $selected_options['token']; |
| 78 | $public_key = $selected_options['public_key']; |
| 79 | $first_name = $selected_options['first_name']; |
| 80 | $last_name = $selected_options['last_name']; |
| 81 | $optin_status = $selected_options['optin_status']; |
| 82 | $custom_fields = $selected_options['custom_fields']; |
| 83 | // Extract new fields from selected_options or meta object. |
| 84 | $primary_phone = isset( $selected_options['primary_phone'] ) ? $selected_options['primary_phone'] : ''; |
| 85 | $primary_phone_extension = isset( $selected_options['primary_phone_extension'] ) ? $selected_options['primary_phone_extension'] : ''; |
| 86 | $street_address_1 = isset( $selected_options['street_address_1'] ) ? $selected_options['street_address_1'] : ''; |
| 87 | $street_address_2 = isset( $selected_options['street_address_2'] ) ? $selected_options['street_address_2'] : ''; |
| 88 | $city = isset( $selected_options['city'] ) ? $selected_options['city'] : ''; |
| 89 | $postal_zip = isset( $selected_options['postal_zip'] ) ? $selected_options['postal_zip'] : ''; |
| 90 | $region = isset( $selected_options['region'] ) ? $selected_options['region'] : ''; |
| 91 | $country = isset( $selected_options['country'] ) ? $selected_options['country'] : ''; |
| 92 | $lead_source = isset( $selected_options['lead_source'] ) ? $selected_options['lead_source'] : ''; |
| 93 | $source_page = isset( $selected_options['source_page'] ) ? $selected_options['source_page'] : ''; |
| 94 | $custom_meta = isset( $selected_options['custom_meta'] ) ? $selected_options['custom_meta'] : ''; |
| 95 | $birthday = isset( $selected_options['birthday'] ) ? $selected_options['birthday'] : ''; |
| 96 | $ip_address = isset( $selected_options['ip_address'] ) ? $selected_options['ip_address'] : ''; |
| 97 | $mobile_phone = isset( $selected_options['mobile_phone'] ) ? $selected_options['mobile_phone'] : ''; |
| 98 | $gravatar = isset( $selected_options['gravatar'] ) ? $selected_options['gravatar'] : ''; |
| 99 | $age = isset( $selected_options['age'] ) ? $selected_options['age'] : ''; |
| 100 | |
| 101 | |
| 102 | // Check if fields exist in meta object from custom_fields if not found directly. |
| 103 | if ( ! empty( $custom_fields ) ) { |
| 104 | foreach ( $custom_fields as $field ) { |
| 105 | if ( isset( $field['custom_field_key'] ) && isset( $field['custom_field_value'] ) ) { |
| 106 | $key = $field['custom_field_key']; |
| 107 | $value = $field['custom_field_value']; |
| 108 | |
| 109 | // Map meta fields to direct fields if direct fields are empty. |
| 110 | switch ( $key ) { |
| 111 | case 'primary_phone': |
| 112 | if ( empty( $primary_phone ) ) { |
| 113 | $primary_phone = $value; |
| 114 | } |
| 115 | break; |
| 116 | case 'primary_phone_extension': |
| 117 | if ( empty( $primary_phone_extension ) ) { |
| 118 | $primary_phone_extension = $value; |
| 119 | } |
| 120 | break; |
| 121 | case 'street_address_1': |
| 122 | if ( empty( $street_address_1 ) ) { |
| 123 | $street_address_1 = $value; |
| 124 | } |
| 125 | break; |
| 126 | case 'street_address_2': |
| 127 | if ( empty( $street_address_2 ) ) { |
| 128 | $street_address_2 = $value; |
| 129 | } |
| 130 | break; |
| 131 | case 'city': |
| 132 | if ( empty( $city ) ) { |
| 133 | $city = $value; |
| 134 | } |
| 135 | break; |
| 136 | case 'postal_zip': |
| 137 | if ( empty( $postal_zip ) ) { |
| 138 | $postal_zip = $value; |
| 139 | } |
| 140 | break; |
| 141 | case 'region': |
| 142 | if ( empty( $region ) ) { |
| 143 | $region = $value; |
| 144 | } |
| 145 | break; |
| 146 | case 'country': |
| 147 | if ( empty( $country ) ) { |
| 148 | $country = $value; |
| 149 | } |
| 150 | break; |
| 151 | case 'lead_source': |
| 152 | if ( empty( $lead_source ) ) { |
| 153 | $lead_source = $value; |
| 154 | } |
| 155 | break; |
| 156 | case 'source_page': |
| 157 | if ( empty( $source_page ) ) { |
| 158 | $source_page = $value; |
| 159 | } |
| 160 | break; |
| 161 | case 'custom_meta': |
| 162 | if ( empty( $custom_meta ) ) { |
| 163 | $custom_meta = $value; |
| 164 | } |
| 165 | break; |
| 166 | case 'birthday': |
| 167 | if ( empty( $birthday ) ) { |
| 168 | $birthday = $value; |
| 169 | } |
| 170 | break; |
| 171 | case 'ip_address': |
| 172 | if ( empty( $ip_address ) ) { |
| 173 | $ip_address = $value; |
| 174 | } |
| 175 | break; |
| 176 | case 'mobile_phone': |
| 177 | if ( empty( $mobile_phone ) ) { |
| 178 | $mobile_phone = $value; |
| 179 | } |
| 180 | break; |
| 181 | case 'gravatar': |
| 182 | if ( empty( $gravatar ) ) { |
| 183 | $gravatar = $value; |
| 184 | } |
| 185 | break; |
| 186 | case 'age': |
| 187 | if ( empty( $age ) ) { |
| 188 | $age = $value; |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if ( is_email( $email ) ) { |
| 197 | // Make a single response array. |
| 198 | $response_array = []; |
| 199 | |
| 200 | // Build http request param. |
| 201 | $request_args = [ |
| 202 | 'data' => [ |
| 203 | 'email' => $email, |
| 204 | 'first_name' => $first_name, |
| 205 | 'last_name' => $last_name, |
| 206 | 'optin_status' => $optin_status, |
| 207 | ], |
| 208 | ]; |
| 209 | |
| 210 | // Add gravatar and age to data section if provided. |
| 211 | if ( ! empty( $gravatar ) ) { |
| 212 | $request_args['data']['gravatar'] = $gravatar; |
| 213 | } |
| 214 | if ( ! empty( $age ) ) { |
| 215 | $request_args['data']['age'] = $age; |
| 216 | } |
| 217 | |
| 218 | // Prepare meta fields array. |
| 219 | $meta_fields = []; |
| 220 | |
| 221 | // Add custom_fields to meta. |
| 222 | if ( ! empty( $custom_fields ) ) { |
| 223 | foreach ( $custom_fields as $key => $value ) { |
| 224 | $meta_fields[ $value['custom_field_key'] ] = $value['custom_field_value']; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // Add direct fields to meta if provided. |
| 229 | if ( ! empty( $primary_phone ) ) { |
| 230 | $meta_fields['primary_phone'] = $primary_phone; |
| 231 | } |
| 232 | if ( ! empty( $primary_phone_extension ) ) { |
| 233 | $meta_fields['primary_phone_extension'] = $primary_phone_extension; |
| 234 | } |
| 235 | if ( ! empty( $street_address_1 ) ) { |
| 236 | $meta_fields['street_address_1'] = $street_address_1; |
| 237 | } |
| 238 | if ( ! empty( $street_address_2 ) ) { |
| 239 | $meta_fields['street_address_2'] = $street_address_2; |
| 240 | } |
| 241 | if ( ! empty( $city ) ) { |
| 242 | $meta_fields['city'] = $city; |
| 243 | } |
| 244 | if ( ! empty( $postal_zip ) ) { |
| 245 | $meta_fields['postal_zip'] = $postal_zip; |
| 246 | } |
| 247 | if ( ! empty( $region ) ) { |
| 248 | $meta_fields['region'] = $region; |
| 249 | } |
| 250 | if ( ! empty( $country ) ) { |
| 251 | $meta_fields['country'] = $country; |
| 252 | } |
| 253 | if ( ! empty( $lead_source ) ) { |
| 254 | $meta_fields['lead_source'] = $lead_source; |
| 255 | } |
| 256 | if ( ! empty( $source_page ) ) { |
| 257 | $meta_fields['source_page'] = $source_page; |
| 258 | } |
| 259 | if ( ! empty( $custom_meta ) ) { |
| 260 | $meta_fields['custom_meta'] = $custom_meta; |
| 261 | } |
| 262 | if ( ! empty( $birthday ) ) { |
| 263 | $meta_fields['birthday'] = $birthday; |
| 264 | } |
| 265 | if ( ! empty( $ip_address ) ) { |
| 266 | $meta_fields['ip_address'] = $ip_address; |
| 267 | } |
| 268 | if ( ! empty( $mobile_phone ) ) { |
| 269 | $meta_fields['mobile_phone'] = $mobile_phone; |
| 270 | } |
| 271 | |
| 272 | // Add meta to request if we have any fields. |
| 273 | if ( ! empty( $meta_fields ) ) { |
| 274 | $request_args['meta'] = $meta_fields; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | $args = [ |
| 279 | 'headers' => [ |
| 280 | 'Content-Type' => 'application/json', |
| 281 | 'Gh-Token' => $api_key, |
| 282 | 'Gh-Public-Key' => $public_key, |
| 283 | ], |
| 284 | 'sslverify' => false, |
| 285 | 'data_format' => 'body', |
| 286 | 'body' => wp_json_encode( $request_args ), |
| 287 | ]; |
| 288 | |
| 289 | /** |
| 290 | * |
| 291 | * Ignore line |
| 292 | * |
| 293 | * @phpstan-ignore-next-line |
| 294 | */ |
| 295 | $request = wp_remote_post( get_rest_url() . 'gh/v4/contacts/', $args ); |
| 296 | $response_code = wp_remote_retrieve_response_code( $request ); |
| 297 | $response_body = wp_remote_retrieve_body( $request ); |
| 298 | $response = $response_body; |
| 299 | |
| 300 | if ( 200 !== $response_code ) { |
| 301 | $response = json_decode( $response_body ); |
| 302 | if ( is_object( $response ) ) { |
| 303 | if ( property_exists( $response, 'code' ) && property_exists( $response, 'message' ) ) { |
| 304 | $error_code = $response->code; |
| 305 | $error_message = $response->message; |
| 306 | $response_array = [ |
| 307 | 'status' => 'error', |
| 308 | 'code' => $error_code, |
| 309 | 'message' => $error_message, |
| 310 | ]; |
| 311 | } |
| 312 | } |
| 313 | } else { |
| 314 | $response = json_decode( $response, true ); |
| 315 | $response_array = (array) $response; |
| 316 | } |
| 317 | |
| 318 | return $response_array; |
| 319 | } else { |
| 320 | return [ |
| 321 | 'status' => 'error', |
| 322 | 'message' => 'Enter valid email', |
| 323 | ]; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | |
| 329 | GhCreateContact::get_instance(); |
| 330 |