class-user-profile.php
4.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WPF_User_Profile {
public function __construct() {
// User profile display / edit
add_action( 'show_user_profile', array( $this, 'user_profile' ));
add_action( 'edit_user_profile', array( $this, 'user_profile' ));
// Scripts
add_action( 'admin_enqueue_scripts', array( $this, 'user_profile_enqueue_scripts' ) );
// AJAX
add_action( 'wp_ajax_resync_contact', array( $this, 'resync_contact' ));
// Updates
add_action( 'user_register', array( $this, 'user_profile_update' ));
add_action( 'profile_update', array( $this, 'user_profile_update' ));
// Filters for posted data from internal forms
add_filter( 'wpf_user_register', array( $this, 'filter_form_fields' ), 10, 2 );
add_filter( 'wpf_user_update', array( $this, 'filter_form_fields' ), 10, 2 );
}
/**
* Updates the contact record in the CRM when a profile is edited in the backend
*
* @access public
* @return void
*/
public function user_profile_update( $user_id ) {
if(wp_fusion()->settings->get('push') == true)
wp_fusion()->user->push_user_meta( $user_id );
}
/**
* Resynchronize local user ID with IS contact record
*
* @access public
* @return mixed
*/
public function resync_contact() {
$user_id = $_POST['user_id'];
// Force reset contact ID and search for new match
$contact_id = wp_fusion()->user->get_contact_id($user_id, true);
// If no contact found
if(empty($contact_id))
wp_send_json_error();
// Force reset tags and search for new tags
$user_tags = wp_fusion()->user->get_tags($user_id, true);
$response = array(
'contact_id' => $contact_id,
'user_tags' => $user_tags
);
do_action('wpf_resync_contact', $user_id);
// Return the result to the script and die
echo json_encode($response);
wp_die();
}
/**
* Enqueue scripts pn user profile page
*
* @access public
* @return void
*/
public function user_profile_enqueue_scripts($hook) {
if ( 'user-edit.php' == $hook || 'profile.php' == $hook) {
wp_enqueue_script( 'wpf-options', WPF_DIR_URL . '/assets/js/wpf-admin.js', array('jquery'));
}
}
/**
* Filters registration data before sending to the CRM (internal add / edit fields)
*
* @access public
* @return array Registration data
*/
public function filter_form_fields( $post_data, $user_id ) {
global $pagenow;
if($pagenow == 'profile.php' || $pagenow == 'user-edit.php' || $pagenow == 'user-new.php') {
if(isset( $post_data['email'] )) {
$post_data['user_email'] = $post_data['email'];
unset($post_data['email']);
$post_data['user_url'] = $post_data['url'];
unset($post_data['url']);
if(isset($post_data['pass1-text'])) {
$post_data['user_pass'] = $post_data['pass1-text'];
unset($post_data['pass1-text']);
}
}
if(isset($post_data['user_password'])) {
$post_data['user_pass'] = $post_data['user_password'];
unset($post_data['user_password']);
}
}
return $post_data;
}
/**
* Adds fields to user profile
*
* @access public
* @return void
*/
public function user_profile($user) { ?>
<h3>WP Fusion</h3>
<table class="form-table">
<tr>
<th><label for="contact_id"><?php echo wp_fusion()->crm->name ?> Contact ID</label></th>
<td id="contact-id">
<?php if(wp_fusion()->user->get_contact_id($user->ID)) : ?>
<?php echo wp_fusion()->user->get_contact_id($user->ID); ?>
<?php else : ?>
No <?php echo wp_fusion()->crm->name ?> contact record found.
<?php endif; ?>
</td>
</tr>
<?php if(wp_fusion()->user->get_contact_id($user->ID)) : ?>
<tr id="wpf-tags-row">
<th><label for="wpf_tags"><?php echo wp_fusion()->crm->name ?> Tags</label></th>
<td id="wpf-tags-td">
<?php $user_tags = wp_fusion()->user->get_tags($user->ID) ?>
<?php if(!empty($user_tags)) { ?>
<select disabled multiple="multiple" id="wpf_tags" class="select4-wpf-tags">
<?php foreach($user_tags as $id) { ?>
<?php // if(empty($label)) $label = wp_fusion()->user->sync_single_tag($id, $user->ID); ?>
<option selected value="<?php echo $id; ?>"><?php echo wp_fusion()->user->get_tag_label($id) ?></option>
<?php } ?>
</select>
<p class="description">These tags are currently applied to the user in <?php echo wp_fusion()->crm->name ?>.</p>
<?php } else { ?>
No tags applied.
<?php } ?>
</td>
</tr>
<?php endif; ?>
<tr>
<th><label for="resync_contact">Resync Contact</label></th>
<td>
<a id="resync-contact" class="button button-default" data-user_id="<?php echo $user->ID ?>">Resync Contact</a>
<p class="description">If the contact ID or tags aren't in sync, click here to reset the local data and load from the <?php echo wp_fusion()->crm->name ?> contact record.</p>
</td>
</tr>
</table>
<?php
}
}
new WPF_User_Profile;