PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.8
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.8
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / integrations / erp / class-integration-erp.php

class-integration-erp.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.4.8, at includes/integrations/erp/class-integration-erp.php

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