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