| 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_filter( 'editable_roles', array( __CLASS__, 'remove_ph_contact_role_from_dropdown' ) ); |
| 30 |
|
| 31 |
add_action( 'init', array( __CLASS__, 'listen_for_logout' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Listen for logout parameter |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public static function listen_for_logout( $user_id ) { |
| 39 |
|
| 40 |
if ( isset($_GET['logout']) && $_GET['logout'] == 1 ) |
| 41 |
{ |
| 42 |
wp_logout(); |
| 43 |
|
| 44 |
// For now redirect to homepage with filter |
| 45 |
wp_redirect( apply_filters( 'property_logout_redirect_url', home_url( '/' ) ) ); |
| 46 |
exit; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* When user is registered ensure they're also entered as a contact |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public static function user_register( $user_id ) { |
| 55 |
|
| 56 |
remove_action( 'save_post', array( __CLASS__, 'save_post' ), 10 ); |
| 57 |
|
| 58 |
// Shouldn't ever come through this route |
| 59 |
|
| 60 |
add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 ); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* When user profiles are updated, ensure the contact record is kept up-to-date also |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public static function profile_update( $user_id, $old_user_data ) { |
| 69 |
|
| 70 |
remove_action( 'save_post', array( __CLASS__, 'save_post' ), 10 ); |
| 71 |
|
| 72 |
// Shouldn't ever come through this route |
| 73 |
|
| 74 |
add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 3 ); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* When saving contacts in Property Hive, make sure user is updated also |
| 80 |
* |
| 81 |
* @param int $post_id The post ID. |
| 82 |
* @param post $post The post object. |
| 83 |
* @param bool $update Whether this is an existing post being updated or not. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public static function save_post( $post_id, $post, $update ) { |
| 88 |
|
| 89 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
| 90 |
return; |
| 91 |
|
| 92 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 93 |
return; |
| 94 |
|
| 95 |
if ( ! current_user_can( 'edit_post', $post_id ) ) |
| 96 |
return; |
| 97 |
|
| 98 |
if ( false !== wp_is_post_revision( $post_id ) ) |
| 99 |
return; |
| 100 |
|
| 101 |
if ( get_post_type($post_id) != 'contact' ) |
| 102 |
return; |
| 103 |
|
| 104 |
if ( get_post_status($post_id) != 'publish' ) |
| 105 |
return; |
| 106 |
|
| 107 |
// Check if associated user exists |
| 108 |
$contact = new PH_Contact( $post_id ); |
| 109 |
|
| 110 |
if ( $contact->email_address != '' ) |
| 111 |
{ |
| 112 |
// No associated user. Need to create one |
| 113 |
$userdata = array( |
| 114 |
'user_login' => $contact->email_address, |
| 115 |
'user_email' => $contact->email_address, |
| 116 |
'display_name' => get_the_title( $post_id ), |
| 117 |
); |
| 118 |
|
| 119 |
if ( $contact->user_id == '' ) |
| 120 |
{ |
| 121 |
$userdata['role'] = 'property_hive_contact'; |
| 122 |
$userdata['user_pass'] = NULL; // When creating a user, `user_pass` is expected. |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
// contact already associated with a user. Update user |
| 127 |
$userdata['ID'] = $contact->user_id; |
| 128 |
} |
| 129 |
|
| 130 |
$user_id = wp_insert_user( $userdata ); |
| 131 |
|
| 132 |
if ( ! is_wp_error( $user_id ) ) |
| 133 |
{ |
| 134 |
update_post_meta( $post_id, '_user_id', $user_id ); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
// Something went wrong when inserting the user |
| 139 |
var_dump($user_id); |
| 140 |
die(); |
| 141 |
} |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
// Contact doesn't have an email address |
| 146 |
|
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public static function pre_user_query( $user_search ) |
| 151 |
{ |
| 152 |
global $wpdb; |
| 153 |
|
| 154 |
$user_search->query_where .= " AND NOT EXISTS ( |
| 155 |
SELECT |
| 156 |
{$wpdb->usermeta}.user_id |
| 157 |
FROM |
| 158 |
$wpdb->usermeta |
| 159 |
WHERE |
| 160 |
{$wpdb->usermeta}.meta_key = 'wp_capabilities' |
| 161 |
AND |
| 162 |
{$wpdb->usermeta}.meta_value LIKE '%property_hive_contact%' |
| 163 |
AND |
| 164 |
$wpdb->usermeta.user_id = {$wpdb->users}.ID |
| 165 |
) "; |
| 166 |
|
| 167 |
return $user_search; |
| 168 |
} |
| 169 |
|
| 170 |
public static function remove_ph_contact_role_from_dropdown( $all_roles ) |
| 171 |
{ |
| 172 |
unset($all_roles['property_hive_contact']); |
| 173 |
|
| 174 |
return $all_roles; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
PH_User_Contacts::init(); |