class-base.php
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
class WPF_CRM_Base {
/**
* Contains an array of installed APIs and their details
*
* @var available_crms
*/
public $available_crms = array();
/**
* Contains the class object for the currently active CRM
*
* @var crm
*/
public $crm;
public function __construct() {
$configured_crms = wp_fusion()->get_crms();
foreach( $configured_crms as $slug => $classname ) {
if(class_exists($classname)) {
$crm = new $classname();
if(wp_fusion()->settings->get('crm') == $slug) {
$this->crm = $crm;
$this->crm->init();
}
$this->available_crms[$slug] = array( 'name' => $crm->name );
if(isset($crm->menu_name)) {
$this->available_crms[$slug]['menu_name'] = $crm->menu_name;
} else {
$this->available_crms[$slug]['menu_name'] = $crm->name;
}
}
}
add_filter( 'wpf_configure_settings', array( $this, 'configure_settings' ) );
// AJAX CRM connection and sync
add_action( 'wp_ajax_wpf_sync' , array( $this, 'sync' ) );
add_action( 'wp_ajax_wpf_sync_users', array( $this, 'sync_users' ) );
// API debugging class
if (defined('WPF_DEBUG'))
$this->debug();
// Sets up "turbo" mode
$this->turbo();
}
/**
* Adds the available CRMs to the select dropdown on the setup page
*
* @access public
* @since 1.0
* @return array
*/
public function configure_settings( $settings ) {
$settings['crm']['choices'] = $this->get_crms_for_select();
return $settings;
}
/**
* Returns the slug and menu name of each CRM for select fields
*
* @access public
* @since 1.0
* @return array
*/
public function get_crms_for_select() {
foreach( $this->available_crms as $slug => $data ) {
$select_array[$slug] = $data['menu_name'];
}
return $select_array;
}
/**
* Perform initial app sync
*
* @access public
* @return JSON response
*/
public function sync() {
if(wp_fusion()->crm->sync() == true) {
wp_send_json_success();
} else {
wp_send_json_error();
}
}
/**
* Perform initial app sync
*
* @access public
* @return JSON response
*/
public function sync_users() {
$offset = $_POST['offset'];
$number = $_POST['number'];
if(wp_fusion()->user->sync_users($offset, $number) == true) {
wp_send_json_success();
} else {
wp_send_json_error();
}
}
/**
* Maps local fields to CRM field names
*
* @access public
* @return array
*/
public function map_meta_fields( $user_data ) {
$update_data = array();
foreach (wp_fusion()->settings->get('contact_fields') as $field => $field_data) {
// If field exists in form and sync is active
if(isset($user_data[$field]) && $field_data['active'] == true) {
$value = apply_filters( 'wpf_format_field_value', $user_data[$field], $field_data['type'], $field_data['crm_field'] );
if(!empty($value))
$update_data[$field_data['crm_field']] = $value;
}
}
return $update_data;
}
/**
* Enables debugging class
*
* @access public
*/
public function debug() {
require_once WPF_DIR_PATH . 'includes/crms/class-debug.php';
$this->crm = new WPF_CRM_Debug($this->crm);
}
/**
* Enables turbo mode
*
* @access public
*/
public function turbo() {
require_once WPF_DIR_PATH . 'includes/crms/class-queue.php';
$this->crm = new WPF_CRM_Queue($this->crm);
}
}