Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 137
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Wordpress_User_Integration
0.00% covered (danger)
0.00%
0 / 136
0.00% covered (danger)
0.00%
0 / 3
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 init_hooks
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 wordpress_profile_update
0.00% covered (danger)
0.00%
0 / 134
0.00% covered (danger)
0.00%
0 / 1
552
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 *
6 * WordPress User related integrations
7 *
8 */
9namespace Automattic\JetpackCRM;
10
11// block direct access
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14class Wordpress_User_Integration {
15
16    /**
17     * Init
18     */
19    public function __construct() {
20
21        // register hooks
22        $this->init_hooks();
23    }
24
25    /*
26     * Initialise hooks
27    */
28    public function init_hooks() {
29
30        add_action( 'profile_update', array( $this, 'wordpress_profile_update' ), 10, 3 );
31    }
32
33    /*
34     * Catches WordPress user profile updates and updates any related Contacts
35     * Works for all WP users and related systems (e.g. WooSync)
36     * https://developer.wordpress.org/reference/hooks/profile_update/
37     *
38     * @param int $user_id (WordPress User ID)
39     * @param WP_User Object $old_user_data
40     * @param array $new_user_data (This is a raw db insert array)
41     *
42    */
43    public function wordpress_profile_update( $user_id, $old_user_data, $new_user_data ) {
44
45        global $zbs, $wp_query;
46
47        // detect whether these changes are coming from Profile page or Woo my account page:
48        $change_via_woo_account = false;
49
50        // here we use a relatively hacky approach to retrieve the posted page
51        // ... though it seems the best approach
52        if ( is_object( $wp_query ) && isset( $wp_query->queried_object ) && isset( $wp_query->queried_object->post_content ) ) {
53
54            if ( str_contains( $wp_query->queried_object->post_content, '[woocommerce_my_account]' ) ) {
55
56                $change_via_woo_account = true;
57
58            }
59        }
60
61        // This var will be defined as JPCRM_PROFILE_UPDATE_CHANGES, which will allow WooSync's save_my_account_crm_field_changes to sidestep repeating changes
62        $profile_update_changes = array();
63
64        // retrieve id's
65        $old_user_contact_id = false;
66        $new_user_contact_id = false;
67        $email_conflict      = false;
68        if ( isset( $old_user_data->data->user_email ) ) {
69
70            // check if we have contacts with emails
71            $old_user_contact_id = $zbs->DAL->contacts->getContact(
72                -1,
73                array(
74
75                    'email'       => $old_user_data->data->user_email,
76                    'onlyID'      => true,
77                    'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_CONTACT ),
78
79                )
80            );
81
82        }
83
84        if ( isset( $new_user_data['user_email'] ) ) {
85
86            $new_user_contact_id = $zbs->DAL->contacts->getContact(
87                -1,
88                array(
89
90                    'email'       => $new_user_data['user_email'],
91                    'onlyID'      => true,
92                    'ignoreowner' => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_CONTACT ),
93
94                )
95            );
96
97        }
98
99        // Email change (For Client Portal Users & WooSync originated contacts)
100        if ( $old_user_data->data->user_email != $new_user_data['user_email'] ) {
101
102            // if we had a contact with this users email...
103            if ( $old_user_contact_id > 0 ) {
104
105                // if we don't already have a contact with this new email (no collision)
106                if ( ! $new_user_contact_id ) {
107
108                    // update contacts email address:
109                    $zbs->DAL->contacts->update_contact_email( $old_user_contact_id, $new_user_data['user_email'] );
110
111                    // flag so woosync doesn't also update
112                    $profile_update_changes[] = 'email';
113
114                    // Add log noting the email change
115                    $note_type         = 'contact_changed_details_via_wpprofile';
116                    $short_description = __( 'Email changed', 'zero-bs-crm' );
117                    if ( $change_via_woo_account ) {
118                        $note_type = 'contact_changed_details_via_woomyacc';
119                    }
120                    $zbs->DAL->logs->addUpdateLog(
121                        array(
122
123                            'id'    => -1,
124                            'owner' => -1,
125                            'data'  => array(
126
127                                'objtype'   => ZBS_TYPE_CONTACT,
128                                'objid'     => $old_user_contact_id,
129                                'type'      => $note_type, // contact_changed_details_via_wpprofile or contact_changed_details_via_woomyacc
130                                'shortdesc' => $short_description,
131                                'longdesc'  => sprintf( __( 'Contact email changed from `%1$s` to `%2$s`', 'zero-bs-crm' ), $old_user_data->data->user_email, $new_user_data['user_email'] ),
132
133                            ),
134
135                        )
136                    );
137
138                } else {
139
140                    // the email this contact has changed their email to is already in use
141                    // on another contact :o
142
143                    // flag so that we don't also update the new users details below
144                    $email_conflict = true;
145
146                    // Add a log to both contacts.
147                    $short_description = __( 'Attempted email Change', 'zero-bs-crm' );
148                    $zbs->DAL->logs->addUpdateLog(
149                        array(
150
151                            'id'    => -1,
152                            'owner' => -1,
153                            'data'  => array(
154
155                                'objtype'   => ZBS_TYPE_CONTACT,
156                                'objid'     => $old_user_contact_id,
157                                'type'      => 'contact_change_details_attempt',
158                                'shortdesc' => $short_description,
159                                'longdesc'  => sprintf( __( 'There was an attempt to change this contacts email from `%1$s` to `%2$s`, unfortunately this was not possible because another contact (#%3$d) already uses that email address.', 'zero-bs-crm' ), $old_user_data->data->user_email, $new_user_data['user_email'], $new_user_contact_id ),
160
161                            ),
162
163                        )
164                    );
165                    $zbs->DAL->logs->addUpdateLog(
166                        array(
167
168                            'id'    => -1,
169                            'owner' => -1,
170                            'data'  => array(
171
172                                'objtype'   => ZBS_TYPE_CONTACT,
173                                'objid'     => $new_user_contact_id,
174                                'type'      => 'contact_change_details_attempt',
175                                'shortdesc' => $short_description,
176                                'longdesc'  => sprintf( __( 'There was an attempt to change another contact\'s (#%1$d) email from `%2$s` to `%3$s`, this was not possible because this contact already uses that email address.', 'zero-bs-crm' ), $old_user_contact_id, $old_user_data->data->user_email, $new_user_data['user_email'] ),
177
178                            ),
179
180                        )
181                    );
182
183                }
184            }
185        }
186
187        // First and last names are available in $new_user_data but not in old.
188        // Here we brutally updated these, which in some cases may be further updated by `save_my_account_crm_field_changes()` in WooSync
189        // ... if changes posted through Woo My Account, and fname/lname specified as editable fields.
190
191        // if we have a contact to op, and this isn't a conflicting situation:
192        if ( isset( $old_user_contact_id ) && $old_user_contact_id > 0 && ! $email_conflict ) {
193
194            // discern if changes
195            $changes                   = array();
196            $detail_change_description = '';
197
198            // retrieve existing
199            $contact = $zbs->DAL->contacts->getContact(
200                $old_user_contact_id,
201                array(
202                    'withCustomFields' => false,
203                    'fields'           => array( 'zbsc_fname', 'zbsc_lname' ),
204                    'ignoreowner'      => true,
205                )
206            );
207
208            // first name
209            if ( isset( $new_user_data['first_name'] ) ) {
210
211                $new_first_name = $new_user_data['first_name'];
212                if ( $contact['fname'] != $new_first_name ) {
213
214                    // add limitedFields change
215                    $changes[] = array(
216                        'key'  => 'zbsc_fname',
217                        'val'  => $new_first_name,
218                        'type' => '%s',
219                    );
220
221                    // add to log
222                    $detail_change_description .= '<br>' . sprintf( __( 'First name changed from `%1$s` to `%2$s`', 'zero-bs-crm' ), $contact['fname'], $new_first_name );
223
224                    // flag so woosync doesn't also update
225                    $profile_update_changes[] = 'fname';
226
227                }
228            }
229
230            // last name
231            if ( isset( $new_user_data['last_name'] ) ) {
232
233                $new_last_name = $new_user_data['last_name'];
234                if ( $contact['lname'] != $new_last_name ) {
235
236                    // add limitedFields change
237                    $changes[] = array(
238                        'key'  => 'zbsc_lname',
239                        'val'  => $new_last_name,
240                        'type' => '%s',
241                    );
242
243                    // add to log
244                    $detail_change_description .= '<br>' . sprintf( __( 'Last name changed from `%1$s` to `%2$s`', 'zero-bs-crm' ), $contact['lname'], $new_last_name );
245
246                    // flag so woosync doesn't also update
247                    $profile_update_changes[] = 'lname';
248
249                }
250            }
251
252            // any changes?
253            if ( count( $changes ) > 0 ) {
254
255                // update contact
256                $zbs->DAL->contacts->addUpdateContact(
257                    array(
258                        'id'            => $old_user_contact_id,
259                        'limitedFields' => $changes,
260                    )
261                );
262
263                // Add log noting the email change
264                $note_type         = 'contact_changed_details_via_wpprofile';
265                $short_description = __( 'Contact name change', 'zero-bs-crm' );
266                if ( $change_via_woo_account ) {
267                    $note_type = 'contact_changed_details_via_woomyacc';
268                }
269                $zbs->DAL->logs->addUpdateLog(
270                    array(
271
272                        'id'    => -1,
273                        'owner' => -1,
274                        'data'  => array(
275
276                            'objtype'   => ZBS_TYPE_CONTACT,
277                            'objid'     => $old_user_contact_id,
278                            'type'      => $note_type,
279                            'shortdesc' => $short_description,
280                            'longdesc'  => __( 'Contact details changed:', 'zero-bs-crm' ) . $detail_change_description,
281
282                        ),
283
284                    )
285                );
286            }
287        }
288
289        // if posted from woo my account, define any changes so that woosync doesn't repeat them
290        if ( ! defined( 'JPCRM_PROFILE_UPDATE_CHANGES' ) && $change_via_woo_account && count( $profile_update_changes ) > 0 ) {
291            define( 'JPCRM_PROFILE_UPDATE_CHANGES', $profile_update_changes );
292        }
293    }
294}