| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | * @package PropertyHive/Classes/ |
| 13 | 13 | * @category Class |
| 14 | 14 | * @author PropertyHive |
| 15 | 15 | */ |
| 16 | +// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Preserve the existing public PH_User_Contacts extension-compatible class name. | |
| 16 | 17 | class PH_User_Contacts { |
| 17 | 18 | |
| 18 | 19 | /** |
| 19 | 20 | * Hook in methods |
| @@ -25,30 +26,81 @@ | ||
| 25 | 26 | |
| 26 | 27 | // Hide users with role 'property_hive_contact' by excluding them from user queries |
| 27 | 28 | //add_action( 'pre_user_query', array( __CLASS__, 'pre_user_query' ) ); |
| 28 | 29 | |
| 29 | - //add_filter( 'editable_roles', array( __CLASS__, 'remove_ph_contact_role_from_dropdown' ) ); | |
| 30 | + add_action( 'init', array( __CLASS__, 'listen_for_logout' ) ); | |
| 30 | 31 | |
| 31 | - add_action( 'init', array( __CLASS__, 'listen_for_logout' ) ); | |
| 32 | + add_action( 'template_redirect', array( __CLASS__, 'redirect_to_my_account_if_logged_in' ) ); | |
| 32 | 33 | } |
| 33 | 34 | |
| 34 | 35 | /** |
| 35 | - * Listen for logout parameter | |
| 36 | + * Return to my account page if accessing login or register page and already logged in | |
| 36 | 37 | * @return void |
| 37 | 38 | */ |
| 38 | - public static function listen_for_logout( $user_id ) { | |
| 39 | + public static function redirect_to_my_account_if_logged_in() { | |
| 39 | 40 | |
| 40 | - if ( isset($_GET['logout']) && $_GET['logout'] == 1 ) | |
| 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']) ) | |
| 41 | 43 | { |
| 42 | - wp_logout(); | |
| 44 | + return; | |
| 45 | + } | |
| 43 | 46 | |
| 44 | - // For now redirect to homepage with filter | |
| 45 | - wp_redirect( apply_filters( 'property_logout_redirect_url', home_url( '/' ) ) ); | |
| 46 | - exit; | |
| 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 | + } | |
| 47 | 65 | } |
| 48 | 66 | } |
| 49 | 67 | |
| 50 | 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 | + /** | |
| 51 | 103 | * When user is registered ensure they're also entered as a contact |
| 52 | 104 | * @return array |
| 53 | 105 | */ |
| 54 | 106 | public static function user_register( $user_id ) { |
| @@ -108,15 +160,32 @@ | ||
| 108 | 160 | $contact = new PH_Contact( $post_id ); |
| 109 | 161 | |
| 110 | 162 | if ( $contact->email_address != '' ) |
| 111 | 163 | { |
| 164 | + $display_name = get_the_title( $post_id ); | |
| 165 | + | |
| 112 | 166 | // No associated user. Need to create one |
| 113 | 167 | $userdata = array( |
| 114 | 168 | 'user_login' => $contact->email_address, |
| 115 | 169 | 'user_email' => $contact->email_address, |
| 116 | - 'display_name' => get_the_title( $post_id ), | |
| 170 | + 'display_name' => $display_name, | |
| 117 | 171 | ); |
| 118 | 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 | + | |
| 119 | 188 | if ( $contact->user_id == '' ) |
| 120 | 189 | { |
| 121 | 190 | $userdata['role'] = 'property_hive_contact'; |
| 122 | 191 | $userdata['user_pass'] = NULL; // When creating a user, `user_pass` is expected. |
| @@ -135,10 +204,9 @@ | ||
| 135 | 204 | } |
| 136 | 205 | else |
| 137 | 206 | { |
| 138 | 207 | // Something went wrong when inserting the user |
| 139 | - var_dump($user_id); | |
| 140 | - die(); | |
| 208 | + wp_die( esc_html( implode( ' ', $user_id->get_error_messages() ) ) ); | |
| 141 | 209 | } |
| 142 | 210 | } |
| 143 | 211 | else |
| 144 | 212 | { |
| @@ -164,15 +232,8 @@ | ||
| 164 | 232 | $wpdb->usermeta.user_id = {$wpdb->users}.ID |
| 165 | 233 | ) "; |
| 166 | 234 | |
| 167 | 235 | 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 | 236 | } |
| 176 | 237 | } |
| 177 | 238 | |
| 178 | 239 | PH_User_Contacts::init(); |