user = $user; // is guest. $this->is_guest = $this->user->ID ? false : true; // Set customer object. if ( $this->user->ID ) { $this->is_customer = true; $customer = WPSC_Customer::get_by_email( $this->user->user_email ); if ( $customer->id ) { $this->customer = $customer; } else { $this->customer = WPSC_Customer::insert( array( 'user' => $this->user->ID, 'name' => $this->user->display_name, 'email' => $this->user->user_email, ) ); } } elseif ( $email ) { $this->is_customer = true; $this->customer = WPSC_Customer::get_by_email( $email ); } // Set agent object. $agent = WPSC_Agent::get_by_user_id( $this->user->ID ); if ( $agent->id && $agent->is_active ) { $this->is_agent = true; $this->agent = $agent; } // set leval. if ( WPSC_Functions::is_site_admin() ) { $this->level = 'admin'; } elseif ( $this->is_agent ) { $this->level = 'agent'; } elseif ( $this->is_customer ) { $this->level = 'customer'; } else { $this->level = 'none'; } } /** * Load current wpsc user * * @return void */ public static function load_current_user() { global $current_user; // wp logged-in user. $email = $current_user && $current_user->ID ? $current_user->user_email : ''; if ( $email ) { self::$current_user = new WPSC_Current_User( $email ); self::$login_type = 'registered'; return; } // guest login. $gs = get_option( 'wpsc-gs-general' ); $login_auth = isset( $_COOKIE['wpsc_guest_login_auth'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['wpsc_guest_login_auth'] ) ) : ''; $login_auth = $login_auth ? json_decode( $login_auth ) : false; if ( ! $login_auth ) { self::$current_user = new WPSC_Current_User(); return; } $login_auth->email = $login_auth->email ? sanitize_email( $login_auth->email ) : ''; if ( ! $login_auth->email ) { self::$current_user = new WPSC_Current_User(); return; } if ( $login_auth && self::validate_guest_login( $login_auth ) ) { self::$current_user = new WPSC_Current_User( $login_auth->email ); return; } self::$current_user = new WPSC_Current_User(); } /** * Change current user * * @param string $email - email string. * * @return string */ public static function change_current_user( $email ) { $current_user = new WPSC_Current_User( $email ); self::$current_user = $current_user; return self::$current_user; } /** * Return ticket list filters for the user. * * @return array */ public function get_tl_filters() { $filters = array( 'default' => array(), 'saved' => array(), ); // default filters. $default_filters = get_option( $this->is_agent ? 'wpsc-atl-default-filters' : 'wpsc-ctl-default-filters' ); foreach ( $default_filters as $index => $filter ) { // exclude if current user does not have access to deleted filter. if ( $index === 'deleted' && ! $this->agent->has_cap( 'dtt-access' ) ) { continue; } // exclude if filter is not enabled. if ( ! $filter['is_enable'] ) { continue; } $filters['default'][ $index ] = $filter; } // saved filters. $filters['saved'] = $this->get_saved_filters(); // return filters. return $filters; } /** * Return all saved filters for current user * * @return array */ public function get_saved_filters() { $saved_filters = ! $this->is_guest && $this->user->ID ? get_user_meta( $this->user->ID, get_current_blog_id() . '-wpsc-tl-saved-filters', true ) : array(); return $saved_filters ? $saved_filters : array(); } /** * Return attachment auth for URLs created in rest api * * @return string */ public function get_attachment_auth() { $now = new DateTime(); $diff = new DateInterval( 'PT1H' ); $auth = get_user_meta( $this->user->ID, get_current_blog_id() . '-wpsc-rest-attachment-auth', true ); if ( $auth ) { $dt = new DateTime( $auth['date'] ); if ( $now < $dt->add( $diff ) ) { return $auth['key']; } } $auth = array( 'key' => WPSC_Functions::get_random_string( 12 ), 'date' => $now->format( 'Y-m-d H:i:s' ), ); update_user_meta( $this->user->ID, get_current_blog_id() . '-wpsc-rest-attachment-auth', $auth ); return $auth['key']; } /** * Get ticket list items * * @return array */ public function get_tl_list_items() { return $this->is_agent ? get_option( 'wpsc-atl-list-items' ) : get_option( 'wpsc-ctl-list-items' ); } /** * Get default orderby * * @return array */ public function get_tl_default_settings() { return $this->is_agent ? get_option( 'wpsc-tl-ms-agent-view' ) : get_option( 'wpsc-tl-ms-customer-view' ); } /** * Return system query for the current user for ticket list * * @param array $filters - filters. * @return array */ public function get_tl_system_query( $filters ) { $current_user = self::$current_user; $adv_setting = get_option( 'wpsc-ms-advanced-settings' ); if ( $adv_setting['public-mode'] && ! $current_user->is_agent ) { return $filters; } $system_query = array( 'relation' => 'OR' ); $system_query[] = array( 'slug' => 'customer', 'compare' => '=', 'val' => $this->customer->id, ); if ( $this->is_agent ) { if ( $this->agent->has_cap( 'view-assigned-me' ) ) { $system_query[] = array( 'slug' => 'assigned_agent', 'compare' => '=', 'val' => $this->agent->id, ); } if ( $this->agent->has_cap( 'view-unassigned' ) ) { $system_query[] = array( 'slug' => 'assigned_agent', 'compare' => '=', 'val' => '', ); } if ( $this->agent->has_cap( 'view-assigned-others' ) ) { $system_query[] = array( 'slug' => 'assigned_agent', 'compare' => 'NOT IN', 'val' => array( $this->agent->id, '' ), ); } } return apply_filters( 'wpsc_tl_current_user_system_query', $system_query, $filters, $this ); } /** * Return system query for the current user for ticket list * * @param array $filters - filters. * @return array */ public function get_atl_system_query( $filters ) { $system_query = array( 'relation' => 'OR' ); $system_query[] = array( 'slug' => 'customer', 'compare' => '=', 'val' => $this->customer->id, ); if ( $this->is_agent ) { if ( $this->agent->has_cap( 'at-assigned-me' ) ) { $system_query[] = array( 'slug' => 'assigned_agent', 'compare' => '=', 'val' => $this->agent->id, ); } if ( $this->agent->has_cap( 'at-unassigned' ) ) { $system_query[] = array( 'slug' => 'assigned_agent', 'compare' => '=', 'val' => '', ); } if ( $this->agent->has_cap( 'at-assigned-others' ) ) { $system_query[] = array( 'slug' => 'assigned_agent', 'compare' => 'NOT IN', 'val' => array( $this->agent->id, '' ), ); } } return apply_filters( 'wpsc_atl_current_user_system_query', $system_query, $filters, $this ); } /** * Check login for default login form * * @return void */ public static function check_user_login() { if ( check_ajax_referer( 'wpsc_default_login', '_ajax_nonce', false ) != 1 ) { wp_send_json_error( 'Unauthorized request!', 401 ); } WPSC_MS_Recaptcha::validate( 'submit_login' ); $username = isset( $_POST['username'] ) ? sanitize_text_field( wp_unslash( $_POST['username'] ) ) : ''; if ( ! $username ) { wp_send_json_error( 'Bad request', 400 ); } $password = isset( $_POST['password'] ) ? $_POST['password'] : ''; // phpcs:ignore if ( ! $password ) { wp_send_json_error( 'Bad request', 400 ); } $remember_me = isset( $_POST['remember_me'] ) ? true : false; $user = wp_signon( array( 'user_login' => $username, 'user_password' => $password, 'remember' => $remember_me, ) ); if ( is_wp_error( $user ) ) { $auth_errors = array( 'incorrect_password', 'invalid_username', 'empty_username', 'empty_password', ); $code = $user->get_error_code(); if ( in_array( $code, $auth_errors, true ) ) { wp_send_json_error( array( 'code' => 'invalid_login', 'message' => __( 'Invalid username or password.', 'supportcandy' ), ), 401 ); } wp_send_json_error( array( 'code' => $code, 'message' => wp_strip_all_tags( $user->get_error_message() ), ), 400 ); } wp_send_json_success(); } /** * Get user registration * * @return void */ public static function get_user_registration() { $page_settings = get_option( 'wpsc-gs-page-settings' ); $recaptcha = get_option( 'wpsc-recaptcha-settings' ); $tc = get_option( 'wpsc-term-and-conditions' ); $gdpr = get_option( 'wpsc-gdpr-settings' ); if ( $page_settings['user-registration'] !== 'default' ) { wp_send_json_error( __( 'Unauthorized', 'supportcandy' ), 401 ); }?>