images
2 years ago
convertkit.php
2 years ago
hustle-convertkit-api.php
2 years ago
hustle-convertkit-form-hooks.php
2 years ago
hustle-convertkit-form-settings.php
2 years ago
hustle-convertkit.php
2 years ago
hustle-convertkit-form-hooks.php
347 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_ConvertKit_Form_Hooks class |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class Hustle_ConvertKit_Form_Hooks |
| 10 | * Define the form hooks that are used by ConvertKit |
| 11 | * |
| 12 | * @since 4.0 |
| 13 | */ |
| 14 | class Hustle_ConvertKit_Form_Hooks extends Hustle_Provider_Form_Hooks_Abstract { |
| 15 | |
| 16 | /** |
| 17 | * Add ConvertKit data to entry. |
| 18 | * |
| 19 | * @since 4.0 |
| 20 | * |
| 21 | * @param array $submitted_data Submitted data. |
| 22 | * @return array |
| 23 | * @throws Exception Required fields are missed. |
| 24 | */ |
| 25 | public function add_entry_fields( $submitted_data ) { |
| 26 | |
| 27 | $addon = $this->addon; |
| 28 | $module_id = $this->module_id; |
| 29 | $module = new Hustle_Module_Model( $module_id ); |
| 30 | if ( is_wp_error( $module ) ) { |
| 31 | return; |
| 32 | } |
| 33 | $form_settings_instance = $this->form_settings_instance; |
| 34 | |
| 35 | /** |
| 36 | * Filter submitted form data to be processed |
| 37 | * |
| 38 | * @since 4.0 |
| 39 | * |
| 40 | * @param array $submitted_data |
| 41 | * @param int $module_id current module_id |
| 42 | * @param Hustle_ConvertKit_Form_Settings $form_settings_instance |
| 43 | */ |
| 44 | $submitted_data = apply_filters( |
| 45 | 'hustle_provider_convertkit_form_submitted_data', |
| 46 | $submitted_data, |
| 47 | $module_id, |
| 48 | $form_settings_instance |
| 49 | ); |
| 50 | |
| 51 | $addon_setting_values = $form_settings_instance->get_form_settings_values(); |
| 52 | |
| 53 | try { |
| 54 | $global_multi_id = $addon_setting_values['selected_global_multi_id']; |
| 55 | $api_key = $addon->get_setting( 'api_key', '', $global_multi_id ); |
| 56 | $api_secret = $addon->get_setting( 'api_secret', '', $global_multi_id ); |
| 57 | $api = $addon::api( $api_key, $api_secret ); |
| 58 | |
| 59 | if ( empty( $submitted_data['email'] ) ) { |
| 60 | throw new Exception( __( 'Required Field "email" was not filled by the user.', 'hustle' ) ); |
| 61 | } |
| 62 | |
| 63 | $list_id = $addon_setting_values['list_id']; |
| 64 | $submitted_data = $this->check_legacy( $submitted_data ); |
| 65 | $existing_member = $this->get_subscriber( |
| 66 | $api, |
| 67 | array( |
| 68 | 'email' => $submitted_data['email'], |
| 69 | 'list_id' => $list_id, |
| 70 | ) |
| 71 | ); |
| 72 | $is_sent = false; |
| 73 | $member_status = __( 'Member could not be subscribed.', 'hustle' ); |
| 74 | |
| 75 | // deal with custom fields first. |
| 76 | $custom_fields = array( |
| 77 | 'ip_address' => array( |
| 78 | 'label' => 'IP Address', |
| 79 | ), |
| 80 | ); |
| 81 | // Extra fields. |
| 82 | $additional_fields = array_diff_key( |
| 83 | $submitted_data, |
| 84 | array( |
| 85 | 'email' => '', |
| 86 | 'first_name' => '', |
| 87 | ) |
| 88 | ); |
| 89 | $additional_fields = array_filter( $additional_fields ); |
| 90 | $subscribe_data_fields = array(); |
| 91 | |
| 92 | if ( $additional_fields && is_array( $additional_fields ) && count( $additional_fields ) > 0 ) { |
| 93 | foreach ( $additional_fields as $field_name => $value ) { |
| 94 | $meta_key = 'cv_field_' . $field_name; |
| 95 | $meta_value = $module->get_meta( $meta_key ); |
| 96 | |
| 97 | if ( ! $meta_value || $meta_value !== $field_name ) { |
| 98 | $custom_fields[ $field_name ] = array( |
| 99 | 'label' => $field_name, |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | if ( isset( $submitted_data[ $field_name ] ) ) { |
| 104 | $subscribe_data_fields[ $field_name ] = $submitted_data[ $field_name ]; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if ( ! $addon->maybe_create_custom_fields( $global_multi_id, $custom_fields ) ) { |
| 110 | $details = __( 'Unable to add custom field.', 'hustle' ); |
| 111 | } else { |
| 112 | // subscription. |
| 113 | $name = isset( $submitted_data['first_name'] ) ? $submitted_data['first_name'] : ''; |
| 114 | |
| 115 | $subscribe_data = array( |
| 116 | 'api_key' => $api_key, |
| 117 | 'first_name' => $name, |
| 118 | 'email' => $submitted_data['email'], |
| 119 | 'fields' => array( |
| 120 | 'ip_address' => Opt_In_Geo::get_user_ip(), |
| 121 | ), |
| 122 | ); |
| 123 | $subscribe_data['fields'] = wp_parse_args( $subscribe_data_fields, $subscribe_data['fields'] ); |
| 124 | |
| 125 | /** |
| 126 | * Fires before adding subscriber |
| 127 | * |
| 128 | * @since 4.0.2 |
| 129 | * |
| 130 | * @param int $module_id |
| 131 | * @param array $submitted_data |
| 132 | * @param object $form_settings_instance |
| 133 | */ |
| 134 | do_action( |
| 135 | 'hustle_provider_convertkit_before_add_subscriber', |
| 136 | $module_id, |
| 137 | $submitted_data, |
| 138 | $form_settings_instance |
| 139 | ); |
| 140 | |
| 141 | if ( false !== $existing_member ) { |
| 142 | $res = $api->update_subscriber( $existing_member, $subscribe_data ); |
| 143 | } else { |
| 144 | $forms = $api->get_forms(); |
| 145 | $lists = is_array( $forms ) ? wp_list_pluck( $forms, 'id' ) : array(); |
| 146 | if ( in_array( (int) $list_id, $lists, true ) ) { |
| 147 | $res = $api->subscribe( $list_id, $subscribe_data ); |
| 148 | } else { |
| 149 | $res = new WP_Error( 'convertkit_list_doesnt_exist', __( 'ConvertKit list doesn\'t exist.', 'hustle' ) ); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Fires after adding subscriber |
| 155 | * |
| 156 | * @since 4.0.2 |
| 157 | * |
| 158 | * @param int $module_id |
| 159 | * @param array $submitted_data |
| 160 | * @param mixed $res |
| 161 | * @param object $form_settings_instance |
| 162 | */ |
| 163 | do_action( |
| 164 | 'hustle_provider_convertkit_after_add_subscriber', |
| 165 | $module_id, |
| 166 | $submitted_data, |
| 167 | $res, |
| 168 | $form_settings_instance |
| 169 | ); |
| 170 | |
| 171 | if ( is_wp_error( $res ) ) { |
| 172 | $details = $res->get_error_message(); |
| 173 | } elseif ( empty( $res ) ) { |
| 174 | $details = __( 'Something went wrong', 'hustle' ); |
| 175 | } else { |
| 176 | $is_sent = true; |
| 177 | $details = __( 'Successfully added or updated member on ConvertKit list', 'hustle' ); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | if ( ! empty( $res->subscription->state ) ) { |
| 182 | $member_status = $res->subscription->state; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | $entry_fields = array( |
| 187 | array( |
| 188 | 'name' => 'status', |
| 189 | 'value' => array( |
| 190 | 'is_sent' => $is_sent, |
| 191 | 'description' => $details, |
| 192 | 'member_status' => $member_status, |
| 193 | ), |
| 194 | ), |
| 195 | ); |
| 196 | } catch ( Exception $e ) { |
| 197 | $entry_fields = $this->exception( $e ); |
| 198 | } |
| 199 | |
| 200 | if ( ! empty( $addon_setting_values['list_name'] ) ) { |
| 201 | $entry_fields[0]['value']['list_name'] = $addon_setting_values['list_name']; |
| 202 | } |
| 203 | |
| 204 | $entry_fields = apply_filters( |
| 205 | 'hustle_provider_convertkit_entry_fields', |
| 206 | $entry_fields, |
| 207 | $module_id, |
| 208 | $submitted_data, |
| 209 | $form_settings_instance |
| 210 | ); |
| 211 | |
| 212 | return $entry_fields; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Unsubscribe |
| 217 | * |
| 218 | * @param string $email Email. |
| 219 | */ |
| 220 | public function unsubscribe( $email ) { |
| 221 | $addon = $this->addon; |
| 222 | $form_settings_instance = $this->form_settings_instance; |
| 223 | $addon_setting_values = $form_settings_instance->get_form_settings_values(); |
| 224 | $list_id = $addon_setting_values['list_id']; |
| 225 | $global_multi_id = $addon_setting_values['selected_global_multi_id']; |
| 226 | $api_key = $addon->get_setting( 'api_key', '', $global_multi_id ); |
| 227 | $api_secret = $addon->get_setting( 'api_secret', '', $global_multi_id ); |
| 228 | try { |
| 229 | $api = $addon::api( $api_key, $api_secret ); |
| 230 | $api->delete_email( $list_id, $email ); |
| 231 | } catch ( Exception $e ) { |
| 232 | Opt_In_Utils::maybe_log( $addon->get_slug(), 'unsubscribtion is failed', $e->getMessage() ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Check whether the email is already subscribed. |
| 238 | * |
| 239 | * @since 4.0 |
| 240 | * |
| 241 | * @param array $submitted_data Submitted data. |
| 242 | * @param bool $allow_subscribed Allow already subscribed. |
| 243 | * @return bool |
| 244 | */ |
| 245 | public function on_form_submit( $submitted_data, $allow_subscribed = true ) { |
| 246 | |
| 247 | $is_success = true; |
| 248 | $module_id = $this->module_id; |
| 249 | $form_settings_instance = $this->form_settings_instance; |
| 250 | $addon = $this->addon; |
| 251 | $addon_setting_values = $form_settings_instance->get_form_settings_values(); |
| 252 | |
| 253 | if ( empty( $submitted_data['email'] ) ) { |
| 254 | return __( 'Required Field "email" was not filled by the user.', 'hustle' ); |
| 255 | } |
| 256 | |
| 257 | if ( ! $allow_subscribed ) { |
| 258 | |
| 259 | /** |
| 260 | * Filter submitted form data to be processed |
| 261 | * |
| 262 | * @since 4.0 |
| 263 | * |
| 264 | * @param array $submitted_data |
| 265 | * @param int $module_id current module_id |
| 266 | * @param Hustle_ConvertKit_Form_Settings $form_settings_instance |
| 267 | */ |
| 268 | $submitted_data = apply_filters( |
| 269 | 'hustle_provider_convertkit_form_submitted_data_before_validation', |
| 270 | $submitted_data, |
| 271 | $module_id, |
| 272 | $form_settings_instance |
| 273 | ); |
| 274 | |
| 275 | // triggers exception if not found. |
| 276 | $global_multi_id = $addon_setting_values['selected_global_multi_id']; |
| 277 | $api_key = $addon->get_setting( 'api_key', '', $global_multi_id ); |
| 278 | $api_secret = $addon->get_setting( 'api_secret', '', $global_multi_id ); |
| 279 | $api = $addon::api( $api_key, $api_secret ); |
| 280 | $list_id = $addon_setting_values['list_id']; |
| 281 | |
| 282 | $existing_member = $this->get_subscriber( |
| 283 | $api, |
| 284 | array( |
| 285 | 'email' => $submitted_data['email'], |
| 286 | 'list_id' => $list_id, |
| 287 | ) |
| 288 | ); |
| 289 | |
| 290 | if ( $existing_member ) { |
| 291 | $is_success = self::ALREADY_SUBSCRIBED_ERROR; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Return `true` if success, or **(string) error message** on fail |
| 297 | * |
| 298 | * @since 4.0 |
| 299 | * |
| 300 | * @param bool $is_success |
| 301 | * @param int $module_id current module_id |
| 302 | * @param array $submitted_data |
| 303 | * @param Hustle_ConvertKit_Form_Settings $form_settings_instance |
| 304 | */ |
| 305 | $is_success = apply_filters( |
| 306 | 'hustle_provider_convertkit_form_submitted_data_after_validation', |
| 307 | $is_success, |
| 308 | $module_id, |
| 309 | $submitted_data, |
| 310 | $form_settings_instance |
| 311 | ); |
| 312 | |
| 313 | // process filter. |
| 314 | if ( true !== $is_success ) { |
| 315 | // only update `submit_form_error_message` when not empty. |
| 316 | if ( ! empty( $is_success ) ) { |
| 317 | $this->submit_form_error_message = (string) $is_success; |
| 318 | } |
| 319 | return $is_success; |
| 320 | } |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Get subscriber for providers |
| 327 | * |
| 328 | * This method is to be inherited |
| 329 | * And extended by child classes. |
| 330 | * |
| 331 | * Make use of the property `$subscriber` |
| 332 | * Method to omit double api calls |
| 333 | * |
| 334 | * @since 4.0.2 |
| 335 | * |
| 336 | * @param object $api Api. |
| 337 | * @param mixed $data Data. |
| 338 | * @return mixed array/object API response on queried subscriber |
| 339 | */ |
| 340 | protected function get_subscriber( $api, $data ) { |
| 341 | if ( empty( $this->subscriber ) && ! isset( $this->subscriber[ md5( $data['email'] ) ] ) ) { |
| 342 | $this->subscriber[ md5( $data['email'] ) ] = $api->is_form_subscriber( $data['email'], $data['list_id'] ); |
| 343 | } |
| 344 | return $this->subscriber[ md5( $data['email'] ) ]; |
| 345 | } |
| 346 | } |
| 347 |