class-userpro.php
3.93 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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( function_exists( 'userpro_init' ) ) {
class WPF_UserPro extends WPF_Integrations_Base {
/**
* Gets things started
*
* @access public
* @since 1.0
* @return void
*/
public function init() {
add_filter( 'userpro_form_validation', array($this, 'form_submitted'), 50, 2);
add_filter( 'userpro_before_value_is_displayed', array( $this, 'load_new_value' ), 99, 4);
add_filter( 'wpf_configure_settings', array( $this, 'register_settings' ), 10, 2 );
add_filter( 'wpf_configure_sections', array($this, 'configure_sections'), 10, 2 );
add_filter( 'wpf_meta_fields', array( $this, 'set_contact_field_names' ), 30 );
add_filter( 'wpf_user_register', array( $this, 'filter_form_fields'), 10, 2 );
add_filter( 'wpf_user_update', array( $this, 'filter_form_fields'), 10, 2 );
}
/**
* Add fields to settings page
*
* @access public
* @return array Settings
*/
public function register_settings( $settings, $options ) {
$settings['userpro_header'] = array(
'title' => __('UserPro Integration', 'wp-fusion' ),
'std' => 0,
'type' => 'heading',
'section' => 'integrations'
);
$settings['userpro_pull'] = array(
'title' => __('Pull', 'wp-fusion' ),
'desc' => __('Update the local profile data for a given user from the CRM before displaying. May slow down profile load times.', 'wp-fusion' ),
'std' => 0,
'type' => 'checkbox',
'section' => 'integrations'
);
return $settings;
}
/**
* Adds Integrations tab if not already present
*
* @access public
* @return void
*/
public function configure_sections($page, $options) {
if(!isset($page['sections']['integrations']))
$page['sections'] = wp_fusion()->settings->insert_setting_after('contact-fields', $page['sections'], array('integrations' => __('Integrations', 'wp-fusion' )));
return $page;
}
/**
* Set field labels from UM field labels
*
* @access public
* @return array Settings
*/
public function set_contact_field_names( $meta_fields ) {
$userpro_fields = get_option('userpro_fields');
foreach ((array)$userpro_fields as $key => $field) {
if(!is_array($field))
$field = array('label' => '', 'type' => 'text');
if(!isset($field['label']))
$field['label'] = '';
if(!isset($field['type']))
$field['type'] = 'text';
$meta_fields[$key] = array(
'label' => $field['label'],
'type' => $field['type']
);
}
return $meta_fields;
}
/**
* Filters registration data before sending to the CRM
*
* @access public
* @return array Registration data
*/
public function filter_form_fields( $post_data, $user_id ) {
$unique_id = '-' . $post_data['unique_id'];
$registration_data = array();
foreach($post_data as $key => $value) {
if(substr( $key, -strlen( $unique_id ) ) == $unique_id) {
// Trim the unique ID from the end of the string
$key = substr($key, 0, -strlen($unique_id));
$registration_data[$key] = $value;
}
}
return $registration_data;
}
/**
* Sends UserPro submitted form data to the CRM
*
* @access public
* @return array Errors
*/
public function form_submitted( $errors, $form ) {
if(empty($errors) && wp_fusion()->settings->get('push') == true )
wp_fusion()->user->push_user_meta( get_current_user_id(), $form);
return $errors;
}
/**
* Update UserPro form fields before displaying
*
* @access public
* @return void
*/
public function load_new_value( $value, $key, $array, $user_id ) {
if(wp_fusion()->settings->get('userpro_pull') == true) {
$new_value = wp_fusion()->user->pull_user_meta( $user_id, $key );
// If there's an error, still display the meta data.
if($new_value != false)
$value = $new_value;
}
return $value;
}
}
new WPF_UserPro;
}