images
7 years ago
getresponse.php
7 months ago
hustle-get-response-api.php
3 years ago
hustle-get-response-form-hooks.php
7 months ago
hustle-get-response-form-settings.php
7 months ago
hustle-get-response.php
3 years ago
hustle-get-response-form-hooks.php
420 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Get_Response_Form_Hooks class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Hustle_Get_Response_Form_Hooks |
| 10 | * Define the form hooks that are used by Get_Response |
| 11 | * |
| 12 | * @since 4.0 |
| 13 | */ |
| 14 | class Hustle_Get_Response_Form_Hooks extends Hustle_Provider_Form_Hooks_Abstract { |
| 15 | |
| 16 | /** |
| 17 | * Existing contact ID. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | private $existing_member; |
| 22 | |
| 23 | /** |
| 24 | * Add Get_Response data to entry. |
| 25 | * |
| 26 | * @since 4.0 |
| 27 | * |
| 28 | * @param array $submitted_data Submitted data. |
| 29 | * @return array |
| 30 | * @throws Exception Required fields are missed. |
| 31 | */ |
| 32 | public function add_entry_fields( $submitted_data ) { |
| 33 | |
| 34 | $addon = $this->addon; |
| 35 | $module_id = $this->module_id; |
| 36 | $form_settings_instance = $this->form_settings_instance; |
| 37 | |
| 38 | /** |
| 39 | * Filter submitted form data to be processed |
| 40 | * |
| 41 | * @since 4.0 |
| 42 | * |
| 43 | * @param array $submitted_data |
| 44 | * @param int $module_id current module_id |
| 45 | * @param Hustle_Get_Response_Form_Settings $form_settings_instance |
| 46 | */ |
| 47 | $submitted_data = apply_filters( |
| 48 | 'hustle_provider_get_response_form_submitted_data', |
| 49 | $submitted_data, |
| 50 | $module_id, |
| 51 | $form_settings_instance |
| 52 | ); |
| 53 | $addon_setting_values = $form_settings_instance->get_form_settings_values(); |
| 54 | |
| 55 | try { |
| 56 | $global_multi_id = $addon_setting_values['selected_global_multi_id']; |
| 57 | $api_key = $addon->get_setting( 'api_key', '', $global_multi_id ); |
| 58 | $api = $addon::api( $api_key ); |
| 59 | |
| 60 | if ( empty( $submitted_data['email'] ) ) { |
| 61 | throw new Exception( __( 'Required Field "email" was not filled by the user.', 'hustle' ) ); |
| 62 | } |
| 63 | |
| 64 | $list_id = $addon_setting_values['list_id']; |
| 65 | |
| 66 | $submitted_data = $this->check_legacy( $submitted_data ); |
| 67 | |
| 68 | $email = $submitted_data['email']; |
| 69 | |
| 70 | $name = array(); |
| 71 | if ( ! empty( $submitted_data['name'] ) ) { |
| 72 | $name['name'] = $submitted_data['name']; |
| 73 | } |
| 74 | if ( ! empty( $submitted_data['first_name'] ) ) { |
| 75 | $name['first_name'] = $submitted_data['first_name']; |
| 76 | } |
| 77 | if ( ! empty( $submitted_data['last_name'] ) ) { |
| 78 | $name['last_name'] = $submitted_data['last_name']; |
| 79 | } |
| 80 | |
| 81 | $new_data = array( |
| 82 | 'email' => $email, |
| 83 | 'dayOfCycle' => apply_filters( 'hustle_optin_get_response_cycle', '0' ), |
| 84 | 'campaign' => array( |
| 85 | 'campaignId' => $list_id, |
| 86 | ), |
| 87 | 'ipAddress' => Opt_In_Geo::get_user_ip(), |
| 88 | ); |
| 89 | |
| 90 | if ( count( $name ) ) { |
| 91 | $new_data['name'] = implode( ' ', $name ); |
| 92 | } |
| 93 | |
| 94 | // Extra fields. |
| 95 | $extra_data = array_diff_key( |
| 96 | $submitted_data, |
| 97 | array( |
| 98 | 'email' => '', |
| 99 | 'name' => '', |
| 100 | 'first_name' => '', |
| 101 | 'last_name' => '', |
| 102 | ) |
| 103 | ); |
| 104 | $extra_data = array_filter( $extra_data ); |
| 105 | $is_sent = false; |
| 106 | $member_status = __( 'Member could not be subscribed.', 'hustle' ); |
| 107 | |
| 108 | if ( ! empty( $extra_data ) ) { |
| 109 | $new_data['customFieldValues'] = array(); |
| 110 | |
| 111 | $cf = $api->get_custom_fields(); |
| 112 | |
| 113 | if ( is_wp_error( $cf ) ) { |
| 114 | throw new Exception( $cf->get_error_message() ); |
| 115 | } |
| 116 | |
| 117 | $custom_fields = wp_list_pluck( $cf, 'name', 'customFieldId' ); |
| 118 | $phone_fields = array_filter( |
| 119 | wp_list_pluck( $cf, 'type', 'name' ), |
| 120 | function ( $field_name ) { |
| 121 | return 'phone' === $field_name; |
| 122 | } |
| 123 | ); |
| 124 | $module = Hustle_Module_Model::new_instance( $module_id ); |
| 125 | $form_fields = $module->get_form_fields(); |
| 126 | |
| 127 | foreach ( $extra_data as $key => $value ) { |
| 128 | $key = strtolower( str_replace( array( '-', '_' ), '', $key ) ); |
| 129 | |
| 130 | if ( in_array( $key, $custom_fields, true ) ) { |
| 131 | $custom_field_id = array_search( $key, $custom_fields, true ); |
| 132 | } else { |
| 133 | $type = isset( $form_fields[ $key ] ) ? $this->get_field_type( $form_fields[ $key ]['type'] ) : 'text'; |
| 134 | $custom_field = array( |
| 135 | 'name' => $key, |
| 136 | 'type' => $type, |
| 137 | 'hidden' => false, |
| 138 | 'values' => array(), |
| 139 | ); |
| 140 | |
| 141 | $custom_field_id = $api->add_custom_field( $custom_field ); |
| 142 | if ( is_wp_error( $custom_field_id ) ) { |
| 143 | throw new Exception( $custom_field_id->get_error_message() ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if ( in_array( $key, $phone_fields, true ) ) { |
| 148 | $value = $this->filter_phone_number( $value ); |
| 149 | |
| 150 | if ( empty( $value ) ) { |
| 151 | // If the phone number is probably wrong than we skip sending it to GetResponse. |
| 152 | $details = __( 'Member was added without the phone number.', 'hustle' ); |
| 153 | continue; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | $new_data['customFieldValues'][] = array( |
| 158 | 'customFieldId' => $custom_field_id, |
| 159 | 'value' => array( $value ), |
| 160 | ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Fires before adding subscriber |
| 166 | * |
| 167 | * @since 4.0.2 |
| 168 | * |
| 169 | * @param int $module_id |
| 170 | * @param array $submitted_data |
| 171 | * @param object $form_settings_instance |
| 172 | */ |
| 173 | do_action( |
| 174 | 'hustle_provider_get_response_before_add_subscriber', |
| 175 | $module_id, |
| 176 | $submitted_data, |
| 177 | $form_settings_instance |
| 178 | ); |
| 179 | |
| 180 | if ( $this->existing_member ) { |
| 181 | $res = $api->update_contact( $this->existing_member, $new_data ); |
| 182 | } else { |
| 183 | $res = $api->subscribe( $new_data ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Fires after adding subscriber |
| 188 | * |
| 189 | * @since 4.0.2 |
| 190 | * |
| 191 | * @param int $module_id |
| 192 | * @param array $submitted_data |
| 193 | * @param mixed $res |
| 194 | * @param object $form_settings_instance |
| 195 | */ |
| 196 | do_action( |
| 197 | 'hustle_provider_get_response_after_add_subscriber', |
| 198 | $module_id, |
| 199 | $submitted_data, |
| 200 | $res, |
| 201 | $form_settings_instance |
| 202 | ); |
| 203 | |
| 204 | if ( is_wp_error( $res ) ) { |
| 205 | $error_code = $res->get_error_code(); |
| 206 | $error_message = $res->get_error_message( $error_code ); |
| 207 | |
| 208 | if ( preg_match( '%Conflict%', $error_message ) ) { |
| 209 | $details = __( 'This email address has already subscribed.', 'hustle' ); |
| 210 | } else { |
| 211 | $details = $res->get_error_message(); |
| 212 | } |
| 213 | } else { |
| 214 | $details = ( ! empty( $details ) ) ? $details : __( 'Successfully added or updated member on Get_Response list', 'hustle' ); |
| 215 | $is_sent = true; |
| 216 | $details = $details; |
| 217 | $member_status = __( 'OK', 'hustle' ); |
| 218 | } |
| 219 | |
| 220 | $entry_fields = array( |
| 221 | array( |
| 222 | 'name' => 'status', |
| 223 | 'value' => array( |
| 224 | 'is_sent' => $is_sent, |
| 225 | 'description' => $details, |
| 226 | 'member_status' => $member_status, |
| 227 | ), |
| 228 | ), |
| 229 | ); |
| 230 | } catch ( Exception $e ) { |
| 231 | $entry_fields = $this->exception( $e ); |
| 232 | } |
| 233 | |
| 234 | if ( ! empty( $addon_setting_values['list_name'] ) ) { |
| 235 | $entry_fields[0]['value']['list_name'] = $addon_setting_values['list_name']; |
| 236 | } |
| 237 | |
| 238 | $entry_fields = apply_filters( |
| 239 | 'hustle_provider_' . $addon->get_slug() . '_entry_fields', |
| 240 | $entry_fields, |
| 241 | $module_id, |
| 242 | $submitted_data, |
| 243 | $form_settings_instance |
| 244 | ); |
| 245 | |
| 246 | return $entry_fields; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Unsubscribe |
| 251 | * |
| 252 | * @param string $email Email. |
| 253 | */ |
| 254 | public function unsubscribe( $email ) { |
| 255 | $addon = $this->addon; |
| 256 | $form_settings_instance = $this->form_settings_instance; |
| 257 | $addon_setting_values = $form_settings_instance->get_form_settings_values(); |
| 258 | $list_id = $addon_setting_values['list_id']; |
| 259 | $global_multi_id = $addon_setting_values['selected_global_multi_id']; |
| 260 | try { |
| 261 | $api = $addon::api( $addon->get_setting( 'api_key', '', $global_multi_id ) ); |
| 262 | $api->delete_email( $list_id, $email ); |
| 263 | } catch ( Exception $e ) { |
| 264 | Opt_In_Utils::maybe_log( $addon->get_slug(), 'unsubscribtion is failed', $e->getMessage() ); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Check whether the email is already subscribed. |
| 270 | * |
| 271 | * @since 4.0 |
| 272 | * |
| 273 | * @param array $submitted_data Submitted data. |
| 274 | * @param bool $allow_subscribed Allow already subscribed. |
| 275 | * @return bool |
| 276 | */ |
| 277 | public function on_form_submit( $submitted_data, $allow_subscribed = true ) { |
| 278 | |
| 279 | $is_success = true; |
| 280 | $module_id = $this->module_id; |
| 281 | $form_settings_instance = $this->form_settings_instance; |
| 282 | $addon = $this->addon; |
| 283 | $addon_setting_values = $form_settings_instance->get_form_settings_values(); |
| 284 | |
| 285 | if ( empty( $submitted_data['email'] ) ) { |
| 286 | return __( 'Required Field "email" was not filled by the user.', 'hustle' ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Filter submitted form data to be processed |
| 291 | * |
| 292 | * @since 4.0 |
| 293 | * |
| 294 | * @param array $submitted_data |
| 295 | * @param int $module_id current module_id |
| 296 | * @param Hustle_Get_Response_Form_Settings $form_settings_instance |
| 297 | */ |
| 298 | $submitted_data = apply_filters( |
| 299 | 'hustle_provider_get_response_form_submitted_data_before_validation', |
| 300 | $submitted_data, |
| 301 | $module_id, |
| 302 | $form_settings_instance |
| 303 | ); |
| 304 | |
| 305 | // triggers exception if not found. |
| 306 | $global_multi_id = $addon_setting_values['selected_global_multi_id']; |
| 307 | $api_key = $addon->get_setting( 'api_key', '', $global_multi_id ); |
| 308 | $api = $addon::api( $api_key ); |
| 309 | $args = array( |
| 310 | 'email' => $submitted_data['email'], |
| 311 | 'list_id' => $addon_setting_values['list_id'], |
| 312 | ); |
| 313 | $this->existing_member = $this->get_subscriber( $api, $args ); |
| 314 | |
| 315 | if ( $this->existing_member && ! $allow_subscribed ) { |
| 316 | $is_success = self::ALREADY_SUBSCRIBED_ERROR; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Return `true` if success, or **(string) error message** on fail |
| 321 | * |
| 322 | * @since 4.0 |
| 323 | * |
| 324 | * @param bool $is_success |
| 325 | * @param int $module_id current module_id |
| 326 | * @param array $submitted_data |
| 327 | * @param Hustle_Get_Response_Form_Settings $form_settings_instance |
| 328 | */ |
| 329 | $is_success = apply_filters( |
| 330 | 'hustle_provider_get_response_form_submitted_data_after_validation', |
| 331 | $is_success, |
| 332 | $module_id, |
| 333 | $submitted_data, |
| 334 | $form_settings_instance |
| 335 | ); |
| 336 | |
| 337 | // process filter. |
| 338 | if ( true !== $is_success ) { |
| 339 | // only update `submit_form_error_message` when not empty. |
| 340 | if ( ! empty( $is_success ) ) { |
| 341 | $this->submit_form_error_message = (string) $is_success; |
| 342 | } |
| 343 | return $is_success; |
| 344 | } |
| 345 | |
| 346 | return true; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Get subscriber for providers |
| 351 | * |
| 352 | * This method is to be inherited |
| 353 | * And extended by child classes. |
| 354 | * |
| 355 | * Make use of the property `$subscriber` |
| 356 | * Method to omit double api calls |
| 357 | * |
| 358 | * @since 4.0.2 |
| 359 | * |
| 360 | * @param object $api Api. |
| 361 | * @param mixed $data Data. |
| 362 | * @return mixed array/object API response on queried subscriber |
| 363 | */ |
| 364 | protected function get_subscriber( $api, $data ) { |
| 365 | $key = md5( wp_json_encode( $data ) ); |
| 366 | if ( empty( $this->subscriber ) && ! isset( $this->subscriber[ $key ] ) ) { |
| 367 | $this->subscriber[ $key ] = $api->get_contact( $data ); |
| 368 | } |
| 369 | return $this->subscriber[ $key ]; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Get supported fields |
| 374 | * |
| 375 | * This method is to be inherited |
| 376 | * and extended by child classes. |
| 377 | * |
| 378 | * List the fields supported by the |
| 379 | * provider |
| 380 | * |
| 381 | * @since 4.1 |
| 382 | * |
| 383 | * @param string $type hustle field type. |
| 384 | * @return string Api field type |
| 385 | */ |
| 386 | protected function get_field_type( $type ) { |
| 387 | |
| 388 | switch ( $type ) { |
| 389 | case 'datepicker': |
| 390 | $type = 'date'; |
| 391 | break; |
| 392 | case 'number': |
| 393 | case 'url': |
| 394 | break; |
| 395 | default: |
| 396 | $type = 'text'; |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | return $type; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Checks if phone number is proably correct. |
| 405 | * |
| 406 | * @param string $value Value. |
| 407 | * @return string |
| 408 | */ |
| 409 | private function filter_phone_number( $value ) { |
| 410 | // We remove unnsesary letters. |
| 411 | $value = preg_replace( '/[^+0-9]/', '', $value ); |
| 412 | |
| 413 | if ( strpos( $value, '+' ) === false || ! ( strlen( $value ) >= 5 && strlen( $value ) < 16 ) ) { |
| 414 | return null; |
| 415 | } |
| 416 | |
| 417 | return $value; |
| 418 | } |
| 419 | } |
| 420 |