PluginProbe
Property Hive / 2.3.0
Property Hive v2.3.0
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-user-contacts.php

class-ph-user-contacts.php in Property Hive 2.3.0, at includes/class-ph-user-contacts.php

239 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * Handle users as contacts and vice versa
9 *
10 * @class PH_User_Contacts
11 * @version 1.0.0
12 * @package PropertyHive/Classes/
13 * @category Class
14 * @author PropertyHive
15 */
16 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Preserve the existing public PH_User_Contacts extension-compatible class name.
17 class PH_User_Contacts {
18
19 /**
20 * Hook in methods
21 */
22 public static function init() {
23 //add_action( 'user_register', array( __CLASS__, 'user_register' ), 10, 1 );
24 //add_action( 'profile_update', array( __CLASS__, 'profile_update' ), 10, 2 );
25 //add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 );
26
27 // Hide users with role 'property_hive_contact' by excluding them from user queries
28 //add_action( 'pre_user_query', array( __CLASS__, 'pre_user_query' ) );
29
30 add_action( 'init', array( __CLASS__, 'listen_for_logout' ) );
31
32 add_action( 'template_redirect', array( __CLASS__, 'redirect_to_my_account_if_logged_in' ) );
33 }
34
35 /**
36 * Return to my account page if accessing login or register page and already logged in
37 * @return void
38 */
39 public static function redirect_to_my_account_if_logged_in() {
40
41 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only Divi builder indicator only bypasses the account-page redirect; it performs no state change.
42 if ( is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) || isset($_GET['et_fb']) )
43 {
44 return;
45 }
46
47 $login_page_id = get_option( 'propertyhive_applicant_login_page_id', '' );
48 $register_page_id = get_option( 'propertyhive_applicant_registration_page_id', '' );
49
50 if (
51 is_user_logged_in() &&
52 !empty(get_queried_object_id()) &&
53 (
54 ( !empty($login_page_id) && get_queried_object_id() == $login_page_id ) ||
55 ( !empty($register_page_id) && get_queried_object_id() == $register_page_id )
56 )
57 )
58 {
59 $my_account_page_id = get_option( 'propertyhive_my_account_page_id', '' );
60 if ( !empty($my_account_page_id) )
61 {
62 wp_safe_redirect( get_permalink($my_account_page_id) );
63 exit();
64 }
65 }
66 }
67
68 /**
69 * Listen for logout parameter
70 * @return void
71 */
72 public static function listen_for_logout( $user_id = 0 ) {
73
74 if ( ! isset( $_GET['logout'] ) || ! is_string( $_GET['logout'] ) || $_GET['logout'] !== '1' ) {
75 return;
76 }
77 if ( ! isset( $_GET['_wpnonce'] ) || ! is_string( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'log-out' ) ) {
78 // Cached/legacy links must ask for confirmation without logging out first.
79 // Keep the confirmation on our route so the existing post-logout redirect filter still runs.
80 $confirmation_url = static function( $url ) {
81 return wp_nonce_url( add_query_arg( 'logout', '1', home_url( '/' ) ), 'log-out' );
82 };
83 add_filter( 'logout_url', $confirmation_url, PHP_INT_MAX );
84 // Core wp_nonce_ays reads redirect_to before calling logout_url. This route
85 // uses its own redirect hook and must not forward arbitrary request values.
86 $_REQUEST['redirect_to'] = '';
87 try {
88 wp_nonce_ays( 'log-out' );
89 } finally {
90 remove_filter( 'logout_url', $confirmation_url, PHP_INT_MAX );
91 }
92 return;
93 }
94
95 wp_logout();
96
97 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Existing public property_logout_redirect_url hook must remain available to installed extensions.
98 wp_safe_redirect( apply_filters( 'property_logout_redirect_url', home_url( '/' ) ) );
99 exit;
100 }
101
102 /**
103 * When user is registered ensure they're also entered as a contact
104 * @return array
105 */
106 public static function user_register( $user_id ) {
107
108 remove_action( 'save_post', array( __CLASS__, 'save_post' ), 10 );
109
110 // Shouldn't ever come through this route
111
112 add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 );
113
114 }
115
116 /**
117 * When user profiles are updated, ensure the contact record is kept up-to-date also
118 * @return array
119 */
120 public static function profile_update( $user_id, $old_user_data ) {
121
122 remove_action( 'save_post', array( __CLASS__, 'save_post' ), 10 );
123
124 // Shouldn't ever come through this route
125
126 add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 );
127
128 }
129
130 /**
131 * When saving contacts in Property Hive, make sure user is updated also
132 *
133 * @param int $post_id The post ID.
134 * @param post $post The post object.
135 * @param bool $update Whether this is an existing post being updated or not.
136 *
137 * @return array
138 */
139 public static function save_post( $post_id, $post, $update ) {
140
141 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
142 return;
143
144 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
145 return;
146
147 if ( ! current_user_can( 'edit_post', $post_id ) )
148 return;
149
150 if ( false !== wp_is_post_revision( $post_id ) )
151 return;
152
153 if ( get_post_type($post_id) != 'contact' )
154 return;
155
156 if ( get_post_status($post_id) != 'publish' )
157 return;
158
159 // Check if associated user exists
160 $contact = new PH_Contact( $post_id );
161
162 if ( $contact->email_address != '' )
163 {
164 $display_name = get_the_title( $post_id );
165
166 // No associated user. Need to create one
167 $userdata = array(
168 'user_login' => $contact->email_address,
169 'user_email' => $contact->email_address,
170 'display_name' => $display_name,
171 );
172
173 if ( !empty($display_name) )
174 {
175 $name_parts = explode( ' ', $display_name );
176
177 if ( count($name_parts) > 1 )
178 {
179 $userdata['last_name'] = array_pop($name_parts);
180 $userdata['first_name'] = implode(' ', $name_parts);
181 }
182 else
183 {
184 $userdata['last_name'] = $display_name;
185 }
186 }
187
188 if ( $contact->user_id == '' )
189 {
190 $userdata['role'] = 'property_hive_contact';
191 $userdata['user_pass'] = NULL; // When creating a user, `user_pass` is expected.
192 }
193 else
194 {
195 // contact already associated with a user. Update user
196 $userdata['ID'] = $contact->user_id;
197 }
198
199 $user_id = wp_insert_user( $userdata );
200
201 if ( ! is_wp_error( $user_id ) )
202 {
203 update_post_meta( $post_id, '_user_id', $user_id );
204 }
205 else
206 {
207 // Something went wrong when inserting the user
208 wp_die( esc_html( implode( ' ', $user_id->get_error_messages() ) ) );
209 }
210 }
211 else
212 {
213 // Contact doesn't have an email address
214
215 }
216 }
217
218 public static function pre_user_query( $user_search )
219 {
220 global $wpdb;
221
222 $user_search->query_where .= " AND NOT EXISTS (
223 SELECT
224 {$wpdb->usermeta}.user_id
225 FROM
226 $wpdb->usermeta
227 WHERE
228 {$wpdb->usermeta}.meta_key = 'wp_capabilities'
229 AND
230 {$wpdb->usermeta}.meta_value LIKE '%property_hive_contact%'
231 AND
232 $wpdb->usermeta.user_id = {$wpdb->users}.ID
233 ) ";
234
235 return $user_search;
236 }
237 }
238
239 PH_User_Contacts::init();