| 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 |
class PH_User_Contacts { |
| 17 |
|
| 18 |
/** |
| 19 |
* Hook in methods |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
//add_action( 'user_register', array( __CLASS__, 'user_register' ), 10, 1 ); |
| 23 |
//add_action( 'profile_update', array( __CLASS__, 'profile_update' ), 10, 2 ); |
| 24 |
//add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 ); |
| 25 |
|
| 26 |
// Hide users with role 'property_hive_contact' by excluding them from user queries |
| 27 |
//add_action( 'pre_user_query', array( __CLASS__, 'pre_user_query' ) ); |
| 28 |
|
| 29 |
add_action( 'init', array( __CLASS__, 'listen_for_logout' ) ); |
| 30 |
|
| 31 |
add_action( 'template_redirect', array( __CLASS__, 'redirect_to_my_account_if_logged_in' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Return to my account page if accessing login or register page and already logged in |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public static function redirect_to_my_account_if_logged_in() { |
| 39 |
|
| 40 |
if ( is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) || isset($_GET['et_fb']) ) |
| 41 |
{ |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$login_page_id = get_option( 'propertyhive_applicant_login_page_id', '' ); |
| 46 |
$register_page_id = get_option( 'propertyhive_applicant_registration_page_id', '' ); |
| 47 |
|
| 48 |
if ( |
| 49 |
is_user_logged_in() && |
| 50 |
!empty(get_queried_object_id()) && |
| 51 |
( |
| 52 |
( !empty($login_page_id) && get_queried_object_id() == $login_page_id ) || |
| 53 |
( !empty($register_page_id) && get_queried_object_id() == $register_page_id ) |
| 54 |
) |
| 55 |
) |
| 56 |
{ |
| 57 |
$my_account_page_id = get_option( 'propertyhive_my_account_page_id', '' ); |
| 58 |
if ( !empty($my_account_page_id) ) |
| 59 |
{ |
| 60 |
wp_redirect( get_permalink($my_account_page_id) ); |
| 61 |
exit(); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Listen for logout parameter |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public static function listen_for_logout( $user_id ) { |
| 71 |
|
| 72 |
if ( isset($_GET['logout']) && $_GET['logout'] == 1 ) |
| 73 |
{ |
| 74 |
wp_logout(); |
| 75 |
|
| 76 |
// For now redirect to homepage with filter |
| 77 |
wp_redirect( apply_filters( 'property_logout_redirect_url', home_url( '/' ) ) ); |
| 78 |
exit; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* When user is registered ensure they're also entered as a contact |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public static function user_register( $user_id ) { |
| 87 |
|
| 88 |
remove_action( 'save_post', array( __CLASS__, 'save_post' ), 10 ); |
| 89 |
|
| 90 |
// Shouldn't ever come through this route |
| 91 |
|
| 92 |
add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* When user profiles are updated, ensure the contact record is kept up-to-date also |
| 98 |
* @return array |
| 99 |
*/ |
| 100 |
public static function profile_update( $user_id, $old_user_data ) { |
| 101 |
|
| 102 |
remove_action( 'save_post', array( __CLASS__, 'save_post' ), 10 ); |
| 103 |
|
| 104 |
// Shouldn't ever come through this route |
| 105 |
|
| 106 |
add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 ); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* When saving contacts in Property Hive, make sure user is updated also |
| 112 |
* |
| 113 |
* @param int $post_id The post ID. |
| 114 |
* @param post $post The post object. |
| 115 |
* @param bool $update Whether this is an existing post being updated or not. |
| 116 |
* |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public static function save_post( $post_id, $post, $update ) { |
| 120 |
|
| 121 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
| 122 |
return; |
| 123 |
|
| 124 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 125 |
return; |
| 126 |
|
| 127 |
if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 128 |
return; |
| 129 |
|
| 130 |
if ( false !== wp_is_post_revision( $post_id ) ) |
| 131 |
return; |
| 132 |
|
| 133 |
if ( get_post_type($post_id) != 'contact' ) |
| 134 |
return; |
| 135 |
|
| 136 |
if ( get_post_status($post_id) != 'publish' ) |
| 137 |
return; |
| 138 |
|
| 139 |
// Check if associated user exists |
| 140 |
$contact = new PH_Contact( $post_id ); |
| 141 |
|
| 142 |
if ( $contact->email_address != '' ) |
| 143 |
{ |
| 144 |
$display_name = get_the_title( $post_id ); |
| 145 |
|
| 146 |
// No associated user. Need to create one |
| 147 |
$userdata = array( |
| 148 |
'user_login' => $contact->email_address, |
| 149 |
'user_email' => $contact->email_address, |
| 150 |
'display_name' => $display_name, |
| 151 |
); |
| 152 |
|
| 153 |
if ( !empty($display_name) ) |
| 154 |
{ |
| 155 |
$name_parts = explode( ' ', $display_name ); |
| 156 |
|
| 157 |
if ( count($name_parts) > 1 ) |
| 158 |
{ |
| 159 |
$userdata['last_name'] = array_pop($name_parts); |
| 160 |
$userdata['first_name'] = implode(' ', $name_parts); |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
$userdata['last_name'] = $display_name; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if ( $contact->user_id == '' ) |
| 169 |
{ |
| 170 |
$userdata['role'] = 'property_hive_contact'; |
| 171 |
$userdata['user_pass'] = NULL; // When creating a user, `user_pass` is expected. |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
// contact already associated with a user. Update user |
| 176 |
$userdata['ID'] = $contact->user_id; |
| 177 |
} |
| 178 |
|
| 179 |
$user_id = wp_insert_user( $userdata ); |
| 180 |
|
| 181 |
if ( ! is_wp_error( $user_id ) ) |
| 182 |
{ |
| 183 |
update_post_meta( $post_id, '_user_id', $user_id ); |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
// Something went wrong when inserting the user |
| 188 |
var_dump($user_id); |
| 189 |
die(); |
| 190 |
} |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
// Contact doesn't have an email address |
| 195 |
|
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
public static function pre_user_query( $user_search ) |
| 200 |
{ |
| 201 |
global $wpdb; |
| 202 |
|
| 203 |
$user_search->query_where .= " AND NOT EXISTS ( |
| 204 |
SELECT |
| 205 |
{$wpdb->usermeta}.user_id |
| 206 |
FROM |
| 207 |
$wpdb->usermeta |
| 208 |
WHERE |
| 209 |
{$wpdb->usermeta}.meta_key = 'wp_capabilities' |
| 210 |
AND |
| 211 |
{$wpdb->usermeta}.meta_value LIKE '%property_hive_contact%' |
| 212 |
AND |
| 213 |
$wpdb->usermeta.user_id = {$wpdb->users}.ID |
| 214 |
) "; |
| 215 |
|
| 216 |
return $user_search; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
PH_User_Contacts::init(); |