| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* WP ERP Integration |
| 5 |
* |
| 6 |
* @package weForms\Integrations |
| 7 |
*/ |
| 8 |
class WeForms_Integration_ERP extends WeForms_Abstract_Integration { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize the plugin |
| 12 |
*/ |
| 13 |
function __construct() { |
| 14 |
$this->id = 'erp'; |
| 15 |
$this->title = __( 'WP ERP', 'weforms' ); |
| 16 |
$this->icon = WEFORMS_ASSET_URI . '/images/icon-weforms.png'; |
| 17 |
|
| 18 |
$this->settings_fields = array( |
| 19 |
'enabled' => false, |
| 20 |
'group' => [], |
| 21 |
'stage' => 'subscriber', |
| 22 |
'fields' => array( |
| 23 |
'email' => '', |
| 24 |
'first_name' => '', |
| 25 |
'last_name' => '' |
| 26 |
) |
| 27 |
); |
| 28 |
|
| 29 |
add_action( 'weforms_entry_submission', array( $this, 'subscribe_user' ), 10, 4 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Check if the dependency met |
| 34 |
* |
| 35 |
* @return boolean |
| 36 |
*/ |
| 37 |
public function has_dependency() { |
| 38 |
return function_exists( 'erp_crm_get_contact_groups' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Subscribe a user when a form is submitted |
| 43 |
* |
| 44 |
* @param int $entry_id |
| 45 |
* @param int $form_id |
| 46 |
* @param int $page_id |
| 47 |
* @param array $form_settings |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function subscribe_user( $entry_id, $form_id, $page_id, $form_settings ) { |
| 52 |
|
| 53 |
if ( ! $this->has_dependency() ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$integration = weforms_is_integration_active( $form_id, $this->id ); |
| 58 |
|
| 59 |
if ( false === $integration ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if ( empty( $integration->fields->email ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$email = WeForms_Notification::replace_field_tags( $integration->fields->email, $entry_id ); |
| 68 |
|
| 69 |
if ( empty( $email ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$first_name = WeForms_Notification::replace_name_tag( $integration->fields->first_name, $entry_id ); |
| 74 |
$last_name = WeForms_Notification::replace_name_tag( $integration->fields->last_name, $entry_id ); |
| 75 |
|
| 76 |
$contact_id = erp_insert_people( array( |
| 77 |
'first_name' => $first_name, |
| 78 |
'last_name' => $last_name, |
| 79 |
'email' => $email, |
| 80 |
'type' => 'contact', |
| 81 |
) ); |
| 82 |
|
| 83 |
if ( is_wp_error( $contact_id ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$contact = new \WeDevs\ERP\CRM\Contact( absint( $contact_id ), 'contact' ); |
| 88 |
$contact->update_meta( 'life_stage', $integration->stage ); |
| 89 |
|
| 90 |
if ( $integration->group ) { |
| 91 |
foreach ($integration->group as $group_id) { |
| 92 |
|
| 93 |
$hash = sha1( microtime() . 'erp-subscription-form' . $group_id . $contact_id ); |
| 94 |
|
| 95 |
erp_crm_create_new_contact_subscriber( array( |
| 96 |
'group_id' => $group_id, |
| 97 |
'user_id' => $contact_id, |
| 98 |
'status' => 'subscribe', |
| 99 |
'hash' => $hash |
| 100 |
) ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |