Origin commit
This commit is contained in:
327
inc/user/class-opalestate-user-form-handler.php
Executable file
327
inc/user/class-opalestate-user-form-handler.php
Executable file
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Login processer
|
||||
*/
|
||||
class Opalestate_User_Form_Handler {
|
||||
|
||||
/**
|
||||
* Login processer
|
||||
*/
|
||||
public function __construct() {
|
||||
|
||||
add_action( 'init', [ $this, 'process_login' ] );
|
||||
add_action( 'init', [ $this, 'process_register' ] );
|
||||
|
||||
add_action( 'wp_ajax_opalestate_login_form', [ $this, 'process_login' ] );
|
||||
add_action( 'wp_ajax_opalestate_register_form', [ $this, 'process_register' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Login processer
|
||||
*/
|
||||
public static function process_login() {
|
||||
|
||||
$nonce_value = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( $_POST['_wpnonce'] ) : '';
|
||||
$nonce_value = isset( $_POST['opalestate-login-nonce'] ) ? sanitize_text_field( $_POST['opalestate-login-nonce'] ) : $nonce_value;
|
||||
|
||||
|
||||
/* verify wp nonce */
|
||||
if ( ! wp_verify_nonce( $nonce_value, 'opalestate-login' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
do_action( 'opalestate_user_proccessing_login_before' );
|
||||
|
||||
$credentials = [];
|
||||
$username = isset( $_POST['username'] ) ? sanitize_user( $_POST['username'] ) : '';
|
||||
$password = isset( $_POST['password'] ) ? sanitize_text_field( $_POST['password'] ) : '';
|
||||
|
||||
/* sanitize, allow hook process like block somebody =)))) */
|
||||
$validation = apply_filters( 'opalestate_validation_process_login_error', new WP_Error(), $username, $password );
|
||||
if ( $validation->get_error_code() ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . $validation->get_error_message() );
|
||||
}
|
||||
|
||||
/* validate username */
|
||||
if ( ! $username ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'Username is required.', 'opalestate-pro' ) );
|
||||
} else {
|
||||
|
||||
if ( is_email( $username ) ) {
|
||||
/* user object */
|
||||
$user = get_user_by( 'email', $username );
|
||||
if ( $user->user_login ) {
|
||||
$credentials['user_login'] = $user->user_login;
|
||||
} else {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'A user could not be found with this email address.',
|
||||
'opalestate-pro' ) );
|
||||
}
|
||||
} else {
|
||||
$credentials['user_login'] = $username;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* validate password if it empty */
|
||||
if ( ! $password ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'Password is required.', 'opalestate-pro' ) );
|
||||
}
|
||||
$credentials['user_password'] = $password;
|
||||
/* is rembemer me checkbox */
|
||||
$credentials['remember'] = isset( $_POST['remember'] );
|
||||
|
||||
/* signon user */
|
||||
$user = wp_signon( $credentials, is_ssl() );
|
||||
if ( is_wp_error( $user ) ) {
|
||||
throw new Exception( $user->get_error_message() );
|
||||
} else {
|
||||
|
||||
/* after signon successfully */
|
||||
do_action( 'opalestate_after_signon_successfully', $user );
|
||||
$redirect = opalestate_get_dashdoard_page_uri();
|
||||
|
||||
if ( ! empty( $_POST['redirect'] ) ) {
|
||||
$redirect = sanitize_text_field( $_POST['redirect'] );
|
||||
} elseif ( wp_get_referer() ) {
|
||||
$redirect = wp_get_referer();
|
||||
}
|
||||
|
||||
$redirect = apply_filters( 'opalestate_signon_redirect_url', $redirect );
|
||||
|
||||
if ( opalestate_is_ajax_request() ) {
|
||||
|
||||
opalestate_add_notice( 'success', esc_html__( 'Logged successfully, welcome back!', 'opalestate-pro' ) );
|
||||
ob_start();
|
||||
opalestate_print_notices();
|
||||
$message = ob_get_clean();
|
||||
|
||||
|
||||
wp_send_json( [
|
||||
'status' => true,
|
||||
'message' => $message,
|
||||
'redirect' => $redirect,
|
||||
] );
|
||||
|
||||
} else {
|
||||
wp_safe_redirect( $redirect );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
do_action( 'opalestate_user_proccessing_login_after' );
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
opalestate_add_notice( 'error', $e->getMessage() );
|
||||
}
|
||||
|
||||
if ( opalestate_is_ajax_request() ) {
|
||||
ob_start();
|
||||
opalestate_print_notices();
|
||||
$message = ob_get_clean();
|
||||
wp_send_json( [
|
||||
'status' => false,
|
||||
'message' => $message,
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register processer
|
||||
*/
|
||||
public function process_register() {
|
||||
if ( ! isset( $_POST['opalestate-register-nonce'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$nonce_value = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( $_POST['_wpnonce'] ) : '';
|
||||
$nonce_value = isset( $_POST['opalestate-register-nonce'] ) ? sanitize_text_field( $_POST['opalestate-register-nonce'] ) : $nonce_value;
|
||||
|
||||
/* verify wp nonce */
|
||||
if ( ! isset( $_POST['confirmed_register'] ) || ! wp_verify_nonce( $nonce_value, 'opalestate-register' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
do_action( 'opalestate_user_proccessing_register_before' );
|
||||
|
||||
$credentials = [];
|
||||
$username = isset( $_POST['username'] ) ? sanitize_user( $_POST['username'] ) : '';
|
||||
$email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
|
||||
$password = isset( $_POST['password'] ) ? sanitize_text_field( $_POST['password'] ) : '';
|
||||
$password1 = isset( $_POST['password1'] ) ? sanitize_text_field( $_POST['password1'] ) : '';
|
||||
|
||||
/* sanitize, allow hook process like block somebody =)))) */
|
||||
$validation = apply_filters( 'opalestate_validation_process_register_error', new WP_Error(), $username, $email );
|
||||
|
||||
/* sanitize */
|
||||
if ( $validation->get_error_code() ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . $validation->get_error_message() );
|
||||
}
|
||||
|
||||
/* validate username */
|
||||
if ( ! $username ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'Username is required.', 'opalestate-pro' ) );
|
||||
} else {
|
||||
$credentials['user_login'] = $username;
|
||||
}
|
||||
|
||||
/* validate email */
|
||||
if ( ! $email ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'Email is required.', 'opalestate-pro' ) );
|
||||
} else {
|
||||
$credentials['user_email'] = $email;
|
||||
}
|
||||
|
||||
/* validate password */
|
||||
if ( ! $password ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'Password is required.', 'opalestate-pro' ) );
|
||||
}
|
||||
if ( $password !== $password1 ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . esc_html__( 'Re-Password is not match.', 'opalestate-pro' ) );
|
||||
}
|
||||
$credentials['user_pass'] = $password;
|
||||
|
||||
|
||||
/* create new user */
|
||||
$user_id = opalestate_create_user( $credentials );
|
||||
|
||||
if ( is_wp_error( $user_id ) ) {
|
||||
throw new Exception( '<strong>' . esc_html__( 'ERROR', 'opalestate-pro' ) . ':</strong> ' . $user_id->get_error_message() );
|
||||
} else {
|
||||
|
||||
/* after register successfully */
|
||||
do_action( 'opalestate_after_register_successfully', $user_id );
|
||||
|
||||
$redirect = home_url();
|
||||
if ( opalestate_get_option( 'login_user' ) ) {
|
||||
wp_set_auth_cookie( $user_id );
|
||||
$redirect = opalestate_get_dashdoard_page_uri();
|
||||
} elseif ( ! empty( $_POST['redirect'] ) ) {
|
||||
$redirect = sanitize_text_field( $_POST['redirect'] );
|
||||
} elseif ( wp_get_referer() ) {
|
||||
$redirect = wp_get_referer();
|
||||
}
|
||||
|
||||
do_action( 'opalestate_user_proccessing_register_after' );
|
||||
|
||||
$redirect = apply_filters( 'opalestate_register_redirect_url', $redirect );
|
||||
|
||||
/* is ajax request */
|
||||
if ( opalestate_is_ajax_request() ) {
|
||||
wp_send_json( [ 'status' => true, 'redirect' => $redirect ] );
|
||||
} else {
|
||||
wp_safe_redirect( $redirect );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
opalestate_add_notice( 'error', $e->getMessage() );
|
||||
}
|
||||
|
||||
/* is ajax request */
|
||||
if ( opalestate_is_ajax_request() ) {
|
||||
ob_start();
|
||||
opalestate_print_notices();
|
||||
$message = ob_get_clean();
|
||||
wp_send_json( [
|
||||
'status' => false,
|
||||
'message' => $message,
|
||||
] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* process user doForgotPassword with username/password
|
||||
*
|
||||
* return Json Data with messsage and login status
|
||||
*/
|
||||
public function process_forgot_password() {
|
||||
|
||||
// First check the nonce, if it fails the function will break
|
||||
check_ajax_referer( 'ajax-pbr-lostpassword-nonce', 'security' );
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$account = sanitize_text_field( $_POST['user_login'] );
|
||||
|
||||
if ( empty( $account ) ) {
|
||||
$error = esc_html__( 'Enter an username or e-mail address.', 'opalestate-pro' );
|
||||
} else {
|
||||
if ( is_email( $account ) ) {
|
||||
if ( email_exists( $account ) ) {
|
||||
$get_by = 'email';
|
||||
} else {
|
||||
$error = esc_html__( 'There is no user registered with that email address.', 'opalestate-pro' );
|
||||
}
|
||||
} elseif ( validate_username( $account ) ) {
|
||||
if ( username_exists( $account ) ) {
|
||||
$get_by = 'login';
|
||||
} else {
|
||||
$error = esc_html__( 'There is no user registered with that username.', 'opalestate-pro' );
|
||||
}
|
||||
} else {
|
||||
$error = esc_html__( 'Invalid username or e-mail address.', 'opalestate-pro' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty ( $error ) ) {
|
||||
$random_password = wp_generate_password();
|
||||
|
||||
$user = get_user_by( $get_by, $account );
|
||||
|
||||
$update_user = wp_update_user( [ 'ID' => $user->ID, 'user_pass' => $random_password ] );
|
||||
|
||||
if ( $update_user ) {
|
||||
|
||||
$from = get_option( 'admin_email' ); // Set whatever you want like mail@yourdomain.com
|
||||
|
||||
if ( ! ( isset( $from ) && is_email( $from ) ) ) {
|
||||
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
|
||||
if ( substr( $sitename, 0, 4 ) == 'www.' ) {
|
||||
$sitename = substr( $sitename, 4 );
|
||||
}
|
||||
$from = 'do-not-reply@' . $sitename;
|
||||
}
|
||||
|
||||
$to = $user->user_email;
|
||||
$subject = esc_html__( 'Your new password', 'opalestate-pro' );
|
||||
$sender = 'From: ' . get_option( 'name' ) . ' <' . $from . '>' . "\r\n";
|
||||
|
||||
$message = esc_html__( 'Your new password is: ', 'opalestate-pro' ) . $random_password;
|
||||
|
||||
$headers[] = 'MIME-Version: 1.0' . "\r\n";
|
||||
$headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
|
||||
$headers[] = "X-Mailer: PHP \r\n";
|
||||
$headers[] = $sender;
|
||||
|
||||
$mail = wp_mail( $to, $subject, $message, $headers );
|
||||
if ( $mail ) {
|
||||
$success = esc_html__( 'Check your email address for you new password.', 'opalestate-pro' );
|
||||
} else {
|
||||
$error = esc_html__( 'System is unable to send you mail containg your new password.', 'opalestate-pro' );
|
||||
}
|
||||
} else {
|
||||
$error = esc_html__( 'Oops! Something went wrong while updating your account.', 'opalestate-pro' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $error ) ) {
|
||||
echo wp_send_json( [ 'status' => false, 'message' => ( $error ) ] );
|
||||
}
|
||||
|
||||
if ( ! empty( $success ) ) {
|
||||
echo wp_send_json( [ 'status' => false, 'message' => $success ] );
|
||||
}
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
new Opalestate_User_Form_Handler();
|
||||
222
inc/user/class-opalestate-user-search.php
Executable file
222
inc/user/class-opalestate-user-search.php
Executable file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
/**
|
||||
* $Desc$
|
||||
*
|
||||
* @version $Id$
|
||||
* @package opalestate
|
||||
* @author Opal Team <info@wpopal.com >
|
||||
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
|
||||
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* @website http://www.wpopal.com
|
||||
* @support http://www.wpopal.com/support/forum.html
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class OpalEstate_User_Search {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected $user_id = 0;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function get_instance() {
|
||||
static $_instance;
|
||||
if ( ! $_instance ) {
|
||||
$_instance = new self();
|
||||
}
|
||||
|
||||
return $_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function __construct() {
|
||||
add_action( 'init', [ $this, 'init' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set values when user logined in system
|
||||
*/
|
||||
public function init() {
|
||||
|
||||
global $current_user;
|
||||
wp_get_current_user();
|
||||
|
||||
$this->user_id = $current_user->ID;
|
||||
|
||||
add_filter( 'opalestate_management_user_menu', [ $this, 'dashboard_menu' ] );
|
||||
add_action( 'wp_ajax_opalestate_ajx_save_search', [ $this, 'do_save' ] );
|
||||
add_action( 'wp_ajax_nopriv_opalestate_ajx_save_search', [ $this, 'do_save' ] );
|
||||
|
||||
add_shortcode( 'opalestate_user_saved_search', [ $this, 'savedsearch_page' ] );
|
||||
|
||||
add_filter( 'opalestate_user_content_saved_search_page', [ $this, 'savedsearch_page' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function get_search_by_code( $code ) {
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$query = " SELECT * FROM " . $wpdb->prefix . "opalestate_usersearch WHERE code like %s ";
|
||||
|
||||
$items = $wpdb->get_results( $wpdb->prepare( $query, $code ) );
|
||||
|
||||
if ( isset( $items[0] ) ) {
|
||||
return $items[0];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function has_existed( $params ) {
|
||||
return $this->get_search_by_code( md5( $params ) );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function insert( $data ) {
|
||||
global $wpdb;
|
||||
|
||||
$args = [
|
||||
'name' => '',
|
||||
'params' => '',
|
||||
'code' => '',
|
||||
'user_id' => $this->user_id,
|
||||
];
|
||||
|
||||
$args = array_merge( $args, $data );
|
||||
$args['code'] = md5( $data['params'] );
|
||||
|
||||
$id = $wpdb->insert( $wpdb->prefix . 'opalestate_usersearch', $args );
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function install() {
|
||||
try {
|
||||
if ( ! function_exists( 'dbDelta' ) ) {
|
||||
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'opalestate_usersearch' . ' (
|
||||
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255),
|
||||
params VARCHAR(255),
|
||||
code VARCHAR(255),
|
||||
user_id INT(11) DEFAULT 0
|
||||
) ' . $charset_collate;
|
||||
dbDelta( $sql );
|
||||
|
||||
} catch ( Exception $e ) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function do_save() {
|
||||
if ( $this->user_id > 0 && isset( $_POST['params'] ) && isset( $_POST['name'] ) && ! empty( $_POST['name'] ) && ! empty( $_POST['params'] ) ) {
|
||||
if ( ! $this->has_existed( $_POST['params'] ) ) {
|
||||
$this->insert( [ 'name' => sanitize_text_field( $_POST['name'] ), 'params' => $_POST['params'] ] );
|
||||
$result = [ 'status' => true, 'message' => esc_html__( 'Saved this search successful.', 'opalestate-pro' ) ];
|
||||
} else {
|
||||
$result = [ 'status' => false, 'message' => esc_html__( 'You saved this search', 'opalestate-pro' ) ];
|
||||
}
|
||||
} else {
|
||||
$result = [ 'status' => false, 'message' => esc_html__( 'Please sign in to save this search.', 'opalestate-pro' ) ];
|
||||
}
|
||||
|
||||
echo json_encode( $result );
|
||||
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function do_delete( $id ) {
|
||||
|
||||
global $wpdb;
|
||||
if ( $this->user_id ) {
|
||||
$wpdb->delete( $wpdb->prefix . "opalestate_usersearch", [ 'id' => $id, 'user_id' => $this->user_id ], [ '%d' ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function get_list() {
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$query = " SELECT * FROM " . $wpdb->prefix . "opalestate_usersearch where user_id=" . $this->user_id;
|
||||
|
||||
return $wpdb->get_results( $query );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function is_saved() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function dashboard_menu( $menu ) {
|
||||
$menu['savedsearch'] = [
|
||||
'icon' => 'fa fa-search',
|
||||
'link' => 'saved_search',
|
||||
'title' => esc_html__( 'Saved Search', 'opalestate-pro' ),
|
||||
'id' => 0,
|
||||
];
|
||||
|
||||
return $menu;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function savedsearch_page() {
|
||||
if ( isset( $_GET['doaction'] ) && $_GET['doaction'] == 'delete' && isset( $_GET['id'] ) ) {
|
||||
$this->do_delete( absint( $_GET['id'] ) );
|
||||
}
|
||||
|
||||
return opalestate_load_template_path( 'user-search/content-savedsearch' );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function render_button() {
|
||||
echo opalestate_load_template_path( 'user-search/render-form' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( opalestate_options( 'enable_saved_usersearch', 'on' ) == 'on' ) {
|
||||
OpalEstate_User_Search::get_instance();
|
||||
}
|
||||
717
inc/user/class-opalestate-user.php
Executable file
717
inc/user/class-opalestate-user.php
Executable file
@@ -0,0 +1,717 @@
|
||||
<?php
|
||||
/**
|
||||
* OpalEstate_User
|
||||
*
|
||||
* @package opalestate
|
||||
* @author Opal Team <info@wpopal.com >
|
||||
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
|
||||
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* @website http://www.wpopal.com
|
||||
* @support http://www.wpopal.com/support/forum.html
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class OpalEstate_User {
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
public $current_user_id;
|
||||
|
||||
/**
|
||||
* @var mixed|void
|
||||
*/
|
||||
public $enable_extra_profile;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
public $roles;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
public $user_id;
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
public $new_attachmenet_ids;
|
||||
|
||||
/**
|
||||
* OpalEstate_User constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
define( "OPALESTATE_USER_PROFILE_PREFIX", 'opalestate_user_' );
|
||||
|
||||
$shortcodes = [
|
||||
'user_profile' => [ 'code' => 'user_profile', 'label' => esc_html__( 'User Profile', 'opalestate-pro' ) ],
|
||||
'myaccount' => [ 'code' => 'myaccount', 'label' => esc_html__( 'My Account', 'opalestate-pro' ) ],
|
||||
];
|
||||
|
||||
foreach ( $shortcodes as $shortcode ) {
|
||||
add_shortcode( 'opalestate_' . $shortcode['code'], [ $this, $shortcode['code'] ] );
|
||||
}
|
||||
$this->enable_extra_profile = opalestate_options( 'enable_extra_profile', 'on' );
|
||||
|
||||
|
||||
add_action( 'init', [ $this, 'process_frontend_submit' ], 99999 );
|
||||
add_action( 'cmb2_render_text_password', [ $this, 'cmb2_render_text_password' ], 10, 5 );
|
||||
|
||||
/**
|
||||
* Ajax action
|
||||
*/
|
||||
add_action( 'wp_ajax_opalestate_save_changepass', [ $this, 'save_change_password' ] );
|
||||
add_action( 'wp_ajax_nopriv_opalestate_save_changepass', [ $this, 'save_change_password' ] );
|
||||
|
||||
add_action( 'cmb2_after_init', [ $this, 'process_submission' ], 100000 );
|
||||
|
||||
/**
|
||||
* Check User Block Submission
|
||||
*/
|
||||
add_action( 'opalestate_submission_form_before', [ $this, 'show_message' ], 9 );
|
||||
add_action( 'opalestate_before_process_ajax_upload_file', [ $this, 'check_blocked' ] );
|
||||
add_action( 'opalestate_before_process_ajax_upload_user_avatar', [ $this, 'check_blocked' ] );
|
||||
add_action( 'opalestate_profile_form_process_before', [ $this, 'check_blocked' ] );
|
||||
add_action( 'opalestate_toggle_featured_property_before', [ $this, 'check_blocked' ] );
|
||||
|
||||
add_action( 'user_register', [ $this, 'on_create_user' ], 10, 1 );
|
||||
add_action( 'profile_update', [ $this, 'on_create_user' ], 10, 1 );
|
||||
add_action( 'opalestate_after_register_successfully', [ $this, 'on_regiser_user' ], 10, 1 );
|
||||
|
||||
add_action( 'init', [ $this, 'disable' ], 100000 );
|
||||
add_action( 'init', [ $this, 'init_user_management' ] );
|
||||
|
||||
add_action( 'wp_enqueue_scripts', [ $this, 'scripts_styles' ], 99 );
|
||||
|
||||
add_filter( 'pre_get_posts', [ $this, 'show_current_user_attachments' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* FrontEnd Submission
|
||||
*/
|
||||
public function show_current_user_attachments( $wp_query_obj ) {
|
||||
|
||||
global $current_user, $pagenow;
|
||||
|
||||
if ( ! is_a( $current_user, 'WP_User' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! in_array( $pagenow, [ 'upload.php', 'admin-ajax.php' ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $current_user->roles ) ) {
|
||||
if ( in_array( 'opalestate_agent', $current_user->roles ) || in_array( 'opalestate_agency', $current_user->roles ) ) {
|
||||
$wp_query_obj->set( 'author', $current_user->ID );
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
public function scripts_styles() {
|
||||
if ( isset( $_GET['tab'] ) ) {
|
||||
wp_register_style( 'opalesate-cmb2-front', OPALESTATE_PLUGIN_URL . 'assets/cmb2-front.css' );
|
||||
wp_enqueue_style( 'opalesate-cmb2-front' );
|
||||
wp_register_script(
|
||||
'opalestate-dashboard',
|
||||
OPALESTATE_PLUGIN_URL . 'assets/js/frontend/dashboard.js',
|
||||
[
|
||||
'jquery',
|
||||
],
|
||||
'1.0',
|
||||
true
|
||||
);
|
||||
wp_enqueue_script( 'opalestate-dashboard' );
|
||||
}
|
||||
}
|
||||
|
||||
public function disable() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
add_action( 'wp_before_admin_bar_render', [ $this, 'disable_profile_page' ] );
|
||||
add_action( 'admin_init', [ $this, 'disable_profile_page' ] );
|
||||
add_filter( 'show_admin_bar', '__return_false' );
|
||||
}
|
||||
}
|
||||
|
||||
public function init_user_management() {
|
||||
add_action( 'opalestate_user_content_profile_page', [ $this, 'user_profile' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function show_message_user_profile() {
|
||||
$user_id = isset( $_GET['user_id'] ) ? intval( $_GET['user_id'] ) : 0;
|
||||
$roles = opalestate_user_roles_by_user_id( $user_id );
|
||||
if ( $roles ):
|
||||
if ( in_array( 'opalestate_agency', $roles ) ):
|
||||
$agency_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
|
||||
if ( ! $agency_id ) {
|
||||
return;
|
||||
}
|
||||
$link = get_edit_post_link( $agency_id );
|
||||
?>
|
||||
<div id="message" class="updated fade">
|
||||
<p><?php echo sprintf( esc_html__( 'This user has role <strong>Opal Estate Agency</strong> and click here to <a target="_blank" href="%s">update Agency profile</a>',
|
||||
'opalestate-pro' ), $link ); ?></p>
|
||||
</div>
|
||||
<?php elseif ( in_array( 'opalestate_agent', $roles ) ) :
|
||||
$agent_id = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
|
||||
if ( ! $agent_id ) {
|
||||
return;
|
||||
}
|
||||
$link = get_edit_post_link( $agent_id );
|
||||
?>
|
||||
<div id="message" class="updated fade">
|
||||
<p><?php echo sprintf( esc_html__( 'This user has role <strong>Opal Estate Agent</strong> and click here to <a target="_blank" href="%s">update Agent profile</a>',
|
||||
'opalestate-pro' ), $link ); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php
|
||||
endif;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function on_regiser_user( $user_id ) {
|
||||
if ( isset( $_POST['role'] ) ) {
|
||||
$roles = opalestate_user_roles_by_user_id( $user_id );
|
||||
|
||||
// Fetch the WP_User object of our user.
|
||||
$u = new WP_User( $user_id );
|
||||
$u->remove_role( 'subscriber' );
|
||||
// Replace the current role with 'editor' role
|
||||
$u->set_role( sanitize_text_field( $_POST['role'] ) );
|
||||
|
||||
if ( $roles && in_array( $_POST['role'], $roles ) ) {
|
||||
$role = str_replace( 'opalestate_', '', sanitize_text_field( $_POST['role'] ) );
|
||||
do_action( 'opalestate_on_set_role_' . $role, $user_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function on_create_user( $user_id ) {
|
||||
if ( isset( $_POST['role'] ) ) {
|
||||
$roles = opalestate_user_roles_by_user_id( $user_id );
|
||||
|
||||
if ( $roles && in_array( $_POST['role'], $roles ) ) {
|
||||
$role = sanitize_text_field( str_replace( 'opalestate_', '', $_POST['role'] ) );
|
||||
do_action( 'opalestate_on_set_role_' . $role, $user_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function disable_profile_page() {
|
||||
|
||||
// Remove AdminBar Link
|
||||
if (
|
||||
'wp_before_admin_bar_render' === current_filter()
|
||||
&& ! current_user_can( 'manage_options' )
|
||||
) {
|
||||
return $GLOBALS['wp_admin_bar']->remove_menu( 'edit-profile', 'user-actions' );
|
||||
}
|
||||
|
||||
// Remove (sub)menu items
|
||||
// remove_menu_page( 'profile.php' );
|
||||
if ( function_exists( "remove_submenu_page" ) ) {
|
||||
remove_submenu_page( 'users.php', 'profile.php' );
|
||||
}
|
||||
// Deny access to the profile page and redirect upon try
|
||||
if (
|
||||
defined( 'IS_PROFILE_PAGE' )
|
||||
&& IS_PROFILE_PAGE
|
||||
&& ! current_user_can( 'manage_options' )
|
||||
) {
|
||||
// wp_redirect( admin_url() );
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function show_message() {
|
||||
if ( $this->is_blocked() ) {
|
||||
|
||||
echo apply_filters( 'opalestate_user_block_submission_message',
|
||||
'<div class="alert alert-danger">' . __( 'Your account was blocked to use the submission form, so you could not submit any property.', 'opalestate-pro' ) . '</div>' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function check_blocked() {
|
||||
$check = $this->is_blocked();
|
||||
if ( $check ) {
|
||||
$std = new stdClass();
|
||||
$std->status = false;
|
||||
$std->message = esc_html__( 'Your account is blocked, you could not complete this action', 'opalestate-pro' );
|
||||
$std->msg = $std->message;
|
||||
echo json_encode( $std );
|
||||
wp_die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function get_user_types() {
|
||||
|
||||
return apply_filters( 'opalestate_usertypes', [
|
||||
'none' => esc_html__( 'Subscriber', 'opalestate-pro' ),
|
||||
'opalestate_agent' => esc_html__( 'Agent', 'opalestate-pro' ),
|
||||
'opalestate_agency' => esc_html__( 'Agency', 'opalestate-pro' ),
|
||||
] );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function process_submission() {
|
||||
|
||||
global $current_user;
|
||||
// Verify Nonce
|
||||
$user_id = get_current_user_id();
|
||||
$check = $this->is_blocked();
|
||||
|
||||
$key = 'nonce_CMB2phpopalestate_user_front';
|
||||
|
||||
if ( ! isset( $_POST[ $key ] ) || empty( $_POST[ $key ] ) || ! is_user_logged_in() || $check ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->process_upload_files( 0 );
|
||||
|
||||
$prefix = OPALESTATE_USER_PROFILE_PREFIX;
|
||||
$post_id = $user_id;
|
||||
|
||||
$metaboxes = apply_filters( 'cmb2_meta_boxes', $this->front_edit_fields( [] ) );
|
||||
cmb2_get_metabox_form( $metaboxes[ $prefix . 'front' ], $post_id );
|
||||
$cmb = cmb2_get_metabox( $prefix . 'front', $post_id );
|
||||
|
||||
$sanitized_values = $cmb->get_sanitized_values( $_POST );
|
||||
$cmb->save_fields( $user_id, 'user', $sanitized_values );
|
||||
|
||||
$posts = [
|
||||
'first_name',
|
||||
'last_name',
|
||||
'description',
|
||||
];
|
||||
|
||||
foreach ( $posts as $post ) {
|
||||
if ( isset( $_POST[ $post ] ) ) {
|
||||
update_user_meta( $current_user->ID, $post, esc_attr( $_POST[ $post ] ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->new_attachmenet_ids ) {
|
||||
foreach ( $this->new_attachmenet_ids as $_id ) {
|
||||
delete_post_meta( $_id, '_pending_to_use_', 1 );
|
||||
}
|
||||
}
|
||||
|
||||
$this->remove_dirty_images( $user_id );
|
||||
|
||||
return opalestate_output_msg_json( true,
|
||||
__( 'The data updated successful, please wait for redirecting', 'opalestate-pro' ),
|
||||
[
|
||||
'heading' => esc_html__( 'Update Information', 'opalestate-pro' ),
|
||||
'redirect' => opalestate_get_user_management_page_uri( [ 'tab' => 'profile' ] ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove dirty images of current user
|
||||
*/
|
||||
public function remove_dirty_images( $user_id ) {
|
||||
|
||||
if ( isset( $_POST['remove_image_id'] ) && is_array( $_POST['remove_image_id'] ) && $_POST['remove_image_id'] ) {
|
||||
foreach ( $_POST['remove_image_id'] as $key => $value ) {
|
||||
$post = get_post( $value );
|
||||
if ( $post->post_author == $user_id ) {
|
||||
wp_delete_attachment( $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
private function get_field_name( $field ) {
|
||||
return OPALESTATE_USER_PROFILE_PREFIX . $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process upload images for properties
|
||||
*/
|
||||
public function upload_image( $submitted_file, $parent_id = 0 ) {
|
||||
return opalesate_upload_image( $submitted_file, $parent_id );
|
||||
}
|
||||
|
||||
|
||||
private function process_upload_files( $post_id ) {
|
||||
|
||||
//upload images for featured and gallery images
|
||||
if ( isset( $_FILES ) && ! empty( $_FILES ) ) {
|
||||
|
||||
///
|
||||
$fields = [
|
||||
$this->get_field_name( 'avatar_id' ),
|
||||
];
|
||||
|
||||
foreach ( $_FILES as $key => $value ) {
|
||||
// allow processing in fixed collection
|
||||
if ( in_array( $key, $fields ) ) {
|
||||
$ufile = $_FILES[ $key ];
|
||||
|
||||
/// /////
|
||||
if ( isset( $ufile['name'] ) && is_array( $ufile['name'] ) ) {
|
||||
$output = [];
|
||||
|
||||
foreach ( $ufile['name'] as $f_key => $f_value ) {
|
||||
$loop_file = [
|
||||
'name' => $ufile['name'][ $f_key ],
|
||||
'type' => $ufile['type'][ $f_key ],
|
||||
'tmp_name' => $ufile['tmp_name'][ $f_key ],
|
||||
'error' => $ufile['error'][ $f_key ],
|
||||
'size' => $ufile['size'][ $f_key ],
|
||||
];
|
||||
$new_atm = $this->upload_image( $loop_file, $post_id );
|
||||
if ( $new_atm ) {
|
||||
$_POST[ $key ] = isset( $_POST[ $key ] ) ? $_POST[ $key ] : [];
|
||||
$_POST[ $key ][ $new_atm['attachment_id'] ] = $new_atm['url'];
|
||||
$this->new_attachmenet_ids[ $new_atm['attachment_id'] ] = $new_atm['attachment_id'];
|
||||
}
|
||||
}
|
||||
|
||||
} ///
|
||||
elseif ( isset( $ufile['name'] ) ) {
|
||||
$new_atm = $this->upload_image( $ufile, $post_id );
|
||||
if ( $new_atm ) {
|
||||
$_POST[ $key ] = $new_atm['attachment_id'];
|
||||
|
||||
if ( preg_match( "#id#", $key ) ) {
|
||||
$_key = str_replace( "_id", "", $key );
|
||||
$_POST[ $_key ] = $new_atm['url'];
|
||||
}
|
||||
$this->new_attachmenet_ids[ $new_atm['attachment_id'] ] = $new_atm['attachment_id'];
|
||||
}
|
||||
}
|
||||
//// / //
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function is_blocked() {
|
||||
|
||||
global $current_user;
|
||||
// Verify Nonce
|
||||
$user_id = get_current_user_id();
|
||||
if ( $user_id <= 0 ) {
|
||||
return true;
|
||||
}
|
||||
$blocked = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'block_submission', true );
|
||||
|
||||
return $blocked;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function get_avatar_url( $user_id ) {
|
||||
|
||||
return get_avatar_url( $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function get_author_picture( $user_id ) {
|
||||
$avatar = get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'avatar', true );
|
||||
|
||||
if ( ! $avatar ) {
|
||||
$avatar = opalestate_get_image_avatar_placehold();
|
||||
}
|
||||
|
||||
return $avatar;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function shortcode_button() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function save_change_password() {
|
||||
global $current_user;
|
||||
|
||||
$nonce = 'nonce_CMB2phpopalestate_user_frontchangepass';
|
||||
if ( ! isset( $_POST[ $nonce ], $_POST['oldpassword'], $_POST['new_password'], $_POST['confirm_password'] ) || ! wp_verify_nonce( $_POST[ $nonce ], $nonce ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
do_action( 'opalestate_profile_form_process_before' );
|
||||
$output = new stdClass();
|
||||
$output->status = false;
|
||||
$output->message = esc_html__( 'Found a problem while updating', 'opalestate-pro' );
|
||||
|
||||
wp_get_current_user();
|
||||
|
||||
$userID = $current_user->ID;
|
||||
|
||||
$oldpassword = sanitize_text_field( $_POST['oldpassword'] );
|
||||
$new_password = sanitize_text_field( $_POST['new_password'] );
|
||||
$confirm_password = sanitize_text_field( $_POST['confirm_password'] );
|
||||
|
||||
if ( empty( $oldpassword ) || empty( $new_password ) || empty( $confirm_password ) ) {
|
||||
$output->message = esc_html__( 'Passwords fields are not empty', 'opalestate-pro' );
|
||||
echo json_encode( $output );
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( $new_password != $confirm_password ) {
|
||||
$output->message = esc_html__( 'New password is not same confirm password', 'opalestate-pro' );
|
||||
echo json_encode( $output );
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$user = get_user_by( 'id', $userID );
|
||||
if ( $user && wp_check_password( $oldpassword, $user->data->user_pass, $userID ) ) {
|
||||
wp_set_password( $new_password, $userID );
|
||||
$output->status = true;
|
||||
$output->message = esc_html__( 'Password Updated', 'opalestate-pro' );
|
||||
} else {
|
||||
$output->message = esc_html__( 'Old password is not correct', 'opalestate-pro' );
|
||||
}
|
||||
|
||||
echo json_encode( $output );
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines custom front end fields
|
||||
*
|
||||
* @access public
|
||||
* @param array $metaboxes
|
||||
* @return array
|
||||
*/
|
||||
public function front_edit_fields( array $metaboxes ) {
|
||||
$post_id = 0;
|
||||
$prefix = OPALESTATE_USER_PROFILE_PREFIX;
|
||||
global $current_user;
|
||||
|
||||
$default = [];
|
||||
|
||||
$user_roles = $current_user->roles;
|
||||
$user_role = array_shift( $user_roles );
|
||||
|
||||
$metabox = new Opalestate_User_MetaBox();
|
||||
|
||||
///
|
||||
if ( $this->get_member_id() ) {
|
||||
$fields = array_merge_recursive( $default,
|
||||
$metabox->get_front_base_field( $prefix )
|
||||
);
|
||||
} else {
|
||||
$fields = array_merge_recursive( $default,
|
||||
$metabox->get_front_base_field( $prefix ),
|
||||
$metabox->get_job_fields( $prefix ),
|
||||
$metabox->get_base_front_fields( $prefix ),
|
||||
$metabox->get_address_fields( $prefix )
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$metaboxes[ $prefix . 'front' ] = [
|
||||
'id' => $prefix . 'front',
|
||||
'title' => esc_html__( 'Name and Description', 'opalestate-pro' ),
|
||||
'object_types' => [ 'opalestate_property' ],
|
||||
'context' => 'normal',
|
||||
'object_types' => [ 'user' ], // Tells CMB2 to use user_meta vs post_meta
|
||||
'priority' => 'high',
|
||||
'show_names' => true,
|
||||
'cmb_styles' => false,
|
||||
'fields' => $fields,
|
||||
];
|
||||
|
||||
|
||||
$metaboxes[ $prefix . 'frontchangepass' ] = [
|
||||
'id' => $prefix . 'frontchangepass',
|
||||
'title' => esc_html__( 'Name and Description', 'opalestate-pro' ),
|
||||
'object_types' => [ 'opalestate_property' ],
|
||||
'context' => 'normal',
|
||||
'object_types' => [ 'user' ], // Tells CMB2 to use user_meta vs post_meta
|
||||
'priority' => 'high',
|
||||
'show_names' => true,
|
||||
'fields' => [
|
||||
[
|
||||
'id' => "oldpassword",
|
||||
'name' => esc_html__( 'Old Password', 'opalestate-pro' ),
|
||||
'type' => 'text_password',
|
||||
'attributes' => [
|
||||
'required' => 'required',
|
||||
],
|
||||
'description' => esc_html__( 'Please enter your old password', 'opalestate-pro' ),
|
||||
],
|
||||
[
|
||||
'id' => "new_password",
|
||||
'name' => esc_html__( 'New Password', 'opalestate-pro' ),
|
||||
'type' => 'text_password',
|
||||
'attributes' => [
|
||||
'required' => 'required',
|
||||
],
|
||||
'description' => esc_html__( 'Please enter your new password.', 'opalestate-pro' ),
|
||||
],
|
||||
[
|
||||
'id' => "confirm_password",
|
||||
'name' => esc_html__( 'Confirm Password', 'opalestate-pro' ),
|
||||
'type' => 'text_password',
|
||||
'attributes' => [
|
||||
'required' => 'required',
|
||||
],
|
||||
'description' => esc_html__( 'Please enter your confirm password.', 'opalestate-pro' ),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
return $metaboxes;
|
||||
}
|
||||
|
||||
public function cmb2_render_text_password( $field_args, $escaped_value, $object_id, $object_type, $field_type_object ) {
|
||||
echo $field_type_object->input( [ 'type' => 'password', 'class' => 'form-control' ] );
|
||||
}
|
||||
|
||||
|
||||
public function myaccount() {
|
||||
return opalestate_load_template_path( 'user/my-account' );
|
||||
}
|
||||
|
||||
/**
|
||||
* FrontEnd Submission
|
||||
*/
|
||||
public function user_profile() {
|
||||
|
||||
global $current_user;
|
||||
|
||||
if ( ! is_user_logged_in() ) {
|
||||
echo opalestate_load_template_path( 'parts/not-allowed' );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
|
||||
$metaboxes = apply_filters( 'cmb2_meta_boxes', $this->front_edit_fields( [] ) );
|
||||
|
||||
return opalestate_load_template_path( 'user/profile', [ 'metaboxes' => $metaboxes, 'user_id' => $user_id ] );
|
||||
|
||||
}
|
||||
|
||||
public function process_frontend_submit() {
|
||||
|
||||
if ( opalestate_options( 'enable_extra_profile', 'on' ) != 'on' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $current_user;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function update_data_agent_or_agency( $prefix ) {
|
||||
|
||||
global $current_user;
|
||||
|
||||
|
||||
$post_id = isset( $_POST['object_id'] ) && absint( $_POST['object_id'] ) ? absint( $_POST['object_id'] ) : 0;
|
||||
$user_id = get_current_user_id();
|
||||
$metaboxes = apply_filters( 'opalestate_before_render_profile_' . $_GET['tab'] . '_form', [], $post_id );
|
||||
$metaboxes = apply_filters( 'cmb2_meta_boxes', $metaboxes );
|
||||
|
||||
if ( isset( $metaboxes[ $prefix . 'front' ] ) ) {
|
||||
if ( ! empty( $post_id ) ) {
|
||||
$old_post = get_post( $post_id );
|
||||
$post_date = $old_post->post_date;
|
||||
} else {
|
||||
$post_date = '';
|
||||
}
|
||||
|
||||
$data = [
|
||||
'ID' => $post_id,
|
||||
'post_title' => $current_user->display_name,
|
||||
'post_author' => $user_id,
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'opalestate_agent',
|
||||
'post_date' => $post_date,
|
||||
'post_content' => wp_kses( $_POST[ $prefix . 'text' ], '<b><strong><i><em><h1><h2><h3><h4><h5><h6><pre><code><span><p>' ),
|
||||
];
|
||||
unset( $_POST[ $prefix . 'text' ] );
|
||||
|
||||
|
||||
if ( $post_id > 0 ) {
|
||||
$post_id = wp_update_post( $data, true );
|
||||
} else {
|
||||
$post_id = wp_insert_post( $data, true );
|
||||
}
|
||||
|
||||
$post = get_post( $post_id );
|
||||
|
||||
if ( empty( $post->post_content ) || empty( $post->post_title ) || ! has_post_thumbnail( $post_id ) ) {
|
||||
|
||||
$data['post_status'] = 'pending';
|
||||
$post_id = wp_update_post( $data, true );
|
||||
}
|
||||
update_user_meta( $user_id, $prefix . 'related_id', $post_id );
|
||||
cmb2_get_metabox_form( $metaboxes[ $prefix . 'front' ], $post_id );
|
||||
|
||||
return $post_id;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function get_member_id() {
|
||||
$user_id = get_current_user_id();
|
||||
|
||||
return get_user_meta( $user_id, OPALESTATE_USER_PROFILE_PREFIX . 'related_id', true );
|
||||
}
|
||||
}
|
||||
|
||||
new OpalEstate_User();
|
||||
29
inc/user/class-user-statistics.php
Executable file
29
inc/user/class-user-statistics.php
Executable file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
class OpalEstate_User_Statistics {
|
||||
public $user_id;
|
||||
|
||||
public function __construct () {
|
||||
$this->user_id = get_current_user_id();
|
||||
}
|
||||
|
||||
public function get_count_properties() {
|
||||
$query = Opalestate_Query::get_properties_by_user( array(), $this->user_id );
|
||||
return $query->found_posts;
|
||||
}
|
||||
|
||||
public function get_count_featured() {
|
||||
$query = Opalestate_Query::get_properties_by_user( array(
|
||||
'featured' => 1
|
||||
), $this->user_id );
|
||||
return $query->found_posts;
|
||||
}
|
||||
|
||||
public function get_count_pending_properties() {
|
||||
$query = Opalestate_Query::get_properties_by_user( array(
|
||||
'post_status' => 'pending'
|
||||
), $this->user_id );
|
||||
return $query->found_posts;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
261
inc/user/functions.php
Executable file
261
inc/user/functions.php
Executable file
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
function opalestate_submssion_list_page( $args = [] ) {
|
||||
return opalestate_get_user_management_page_uri( array('tab' => 'submission_list') );
|
||||
}
|
||||
|
||||
function opalestate_get_user_management_page_uri( $args = [] ) {
|
||||
|
||||
global $opalestate_options;
|
||||
|
||||
$uri = isset( $opalestate_options['user_management_page'] ) ? get_permalink( absint( $opalestate_options['user_management_page'] ) ) : get_bloginfo( 'url' );
|
||||
|
||||
if ( ! empty( $args ) ) {
|
||||
// Check for backward compatibility
|
||||
if ( is_string( $args ) ) {
|
||||
$args = str_replace( '?', '', $args );
|
||||
}
|
||||
$args = wp_parse_args( $args );
|
||||
$uri = add_query_arg( $args, $uri );
|
||||
}
|
||||
|
||||
return apply_filters( 'opalestate_user_management_page_uri', $uri );
|
||||
}
|
||||
|
||||
function opalestate_get_current_url( $args = [] ) {
|
||||
global $wp;
|
||||
if( isset($_GET['tab']) && $_GET['tab'] ) {
|
||||
$args['tab'] = $_GET['tab'];
|
||||
}
|
||||
$current_url = home_url( add_query_arg( $args, $wp->request ) );
|
||||
return $current_url;
|
||||
}
|
||||
|
||||
|
||||
function opalestate_get_user_tab_uri( $tab ) {
|
||||
$args['tab'] = $tab ;
|
||||
return opalestate_get_current_url( $args );
|
||||
}
|
||||
|
||||
|
||||
|
||||
function opalestate_management_show_content_page_tab() {
|
||||
|
||||
$tab = isset($_GET['tab']) && $_GET['tab'] ? sanitize_text_field( $_GET['tab'] ): 'dashboard';
|
||||
|
||||
$fnc = 'opalestate_user_content_'.$tab.'_page';
|
||||
|
||||
$content = apply_filters( $fnc, '' );
|
||||
|
||||
if( $content ) {
|
||||
echo $content;
|
||||
} else {
|
||||
if( function_exists( $fnc ) ) {
|
||||
$fnc();
|
||||
} else {
|
||||
opalestate_user_content_dashboard_page();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function opalestate_user_savedsearch_page( $args = [] ) {
|
||||
|
||||
$uri = get_permalink( opalestate_get_option( 'saved_link_page', '/' ) );
|
||||
|
||||
if ( ! empty( $args ) ) {
|
||||
// Check for backward compatibility
|
||||
if ( is_string( $args ) ) {
|
||||
$args = str_replace( '?', '', $args );
|
||||
}
|
||||
$args = wp_parse_args( $args );
|
||||
$uri = add_query_arg( $args, $uri );
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
|
||||
function opalestate_my_account_page( $id = false, $args = array() ) {
|
||||
|
||||
$page = get_permalink( opalestate_get_option( 'user_myaccount_page', '/' ) );
|
||||
if ( $id ) {
|
||||
$edit_page_id = opalestate_get_option( 'user_myaccount_page' );
|
||||
$page = $edit_page_id ? get_permalink( $edit_page_id ) : $page;
|
||||
$page = add_query_arg( 'id', $id, $page );
|
||||
}
|
||||
if( $args ){
|
||||
foreach( $args as $key => $value ) {
|
||||
$page = add_query_arg( $key, $value, $page );
|
||||
}
|
||||
}
|
||||
return $page;
|
||||
}
|
||||
|
||||
function opalestate_submssion_page( $id = false, $args = array() ) {
|
||||
|
||||
|
||||
$page = get_permalink( opalestate_get_option( 'submission_page', '/' ) );
|
||||
if ( $id ) {
|
||||
$edit_page_id = opalestate_get_option( 'submission_edit_page' );
|
||||
$page = $edit_page_id ? get_permalink( $edit_page_id ) : $page;
|
||||
$page = add_query_arg( 'id', $id, $page );
|
||||
}
|
||||
if( $args ){
|
||||
foreach( $args as $key => $value ) {
|
||||
$page = add_query_arg( $key, $value, $page );
|
||||
}
|
||||
}
|
||||
return $page;
|
||||
}
|
||||
|
||||
function opalestate_management_user_menu() {
|
||||
}
|
||||
|
||||
|
||||
function opalestate_management_user_menu_tabs() {
|
||||
|
||||
global $opalestate_options;
|
||||
$menu = [];
|
||||
|
||||
$menu['dashboard'] = [
|
||||
'icon' => 'fa fa-user',
|
||||
'link' => 'dashboard',
|
||||
'title' => esc_html__( 'Dashboard', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['profile_page'] ) ? $opalestate_options['profile_page'] : 0,
|
||||
];
|
||||
|
||||
$menu['profile'] = [
|
||||
'icon' => 'fa fa-user',
|
||||
'link' => 'profile',
|
||||
'title' => esc_html__( 'Personal Information', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['profile_page'] ) ? $opalestate_options['profile_page'] : 0,
|
||||
];
|
||||
|
||||
$menu['favorite'] = [
|
||||
'icon' => 'fa fa-heart',
|
||||
'link' => 'favorite',
|
||||
'title' => esc_html__( 'Favorite', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['favorite_page'] ) ? $opalestate_options['favorite_page'] : 0,
|
||||
];
|
||||
|
||||
$menu['reviews'] = [
|
||||
'icon' => 'fa fa-star',
|
||||
'link' => 'reviews',
|
||||
'title' => esc_html__( 'Reviews', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['reviews_page'] ) ? $opalestate_options['reviews_page'] : 0,
|
||||
];
|
||||
|
||||
$menu['reviews'] = [
|
||||
'icon' => 'fa fa-star',
|
||||
'link' => 'reviews',
|
||||
'title' => esc_html__( 'Reviews', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['reviews_page'] ) ? $opalestate_options['reviews_page'] : 0,
|
||||
];
|
||||
|
||||
if( opalestate_get_option('message_log') ) {
|
||||
$menu['messages'] = [
|
||||
'icon' => 'fa fa-envelope',
|
||||
'link' => 'messages',
|
||||
'title' => esc_html__( 'Messages', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['reviews_page'] ) ? $opalestate_options['reviews_page'] : 0,
|
||||
];
|
||||
}
|
||||
|
||||
$menu['submission'] = [
|
||||
'icon' => 'fa fa-upload',
|
||||
'link' => 'submission',
|
||||
'title' => esc_html__( 'Submit Property', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['submission_page'] ) ? $opalestate_options['submission_page'] : 0,
|
||||
];
|
||||
|
||||
$menu['myproperties'] = [
|
||||
'icon' => 'fa fa-building',
|
||||
'link' => 'submission_list',
|
||||
'title' => esc_html__( 'My Properties', 'opalestate-pro' ),
|
||||
'id' => isset( $opalestate_options['submission_list_page'] ) ? $opalestate_options['submission_list_page'] : 0,
|
||||
];
|
||||
|
||||
$menu = apply_filters( 'opalestate_management_user_menu', $menu );
|
||||
|
||||
$output = '<ul class="account-links nav-pills nav-stacked">';
|
||||
|
||||
global $post;
|
||||
|
||||
$uri = opalestate_get_user_management_page_uri();
|
||||
|
||||
foreach ( $menu as $key => $item ) {
|
||||
if( preg_match("#http#", $item['link']) ){
|
||||
$link = $item['link'];
|
||||
} else {
|
||||
$link = $uri . '?tab=' . $item['link'];
|
||||
}
|
||||
|
||||
$output .= '<li class="' . ( is_object( $post ) && $post->ID == $item['id'] ? 'active' : '' ) . '"><a href="' . $link . '"><i class="' . $item['icon'] . '"></i> ' . $item['title'] . '</a></li>';
|
||||
}
|
||||
|
||||
$output .= '<li><a href="' . wp_logout_url( home_url( '/' ) ) . '"> <i class="fa fa-unlock"></i> ' . esc_html__( 'Log out', 'opalestate-pro' ) . '</a></li>';
|
||||
|
||||
$output .= '</ul>';
|
||||
|
||||
echo $output;
|
||||
}
|
||||
|
||||
function opalestate_user_content_dashboard_page(){
|
||||
echo opalestate_load_template_path( 'user/dashboard' );
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'opalestate_create_user' ) ) {
|
||||
/**
|
||||
* create new wp user
|
||||
*/
|
||||
function opalestate_create_user( $credentials = [] ) {
|
||||
$cred = wp_parse_args( $credentials, [
|
||||
'user_login' => '',
|
||||
'user_email' => '',
|
||||
'user_pass' => '',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
] );
|
||||
|
||||
/* sanitize user email */
|
||||
$user_email = sanitize_email( $cred['user_email'] );
|
||||
if ( email_exists( $user_email ) ) {
|
||||
return new WP_Error( 'email-exists', esc_html__( 'An account is already registered with your email address. Please login.', 'opalestate-pro' ) );
|
||||
}
|
||||
|
||||
$username = sanitize_user( $cred['user_login'] );
|
||||
if ( ! $username || ! validate_username( $username ) ) {
|
||||
return new WP_Error( 'username-invalid', esc_html__( 'Please enter a valid account username.', 'opalestate-pro' ) );
|
||||
}
|
||||
/* if username exists */
|
||||
if ( username_exists( $username ) ) {
|
||||
return new WP_Error( 'username-exists', esc_html__( 'Username is already exists.', 'opalestate-pro' ) );
|
||||
}
|
||||
|
||||
/* password empty */
|
||||
if ( ! $cred['user_pass'] ) {
|
||||
return new WP_Error( 'password-empty', esc_html__( 'Password is requried.', 'opalestate-pro' ) );
|
||||
} else {
|
||||
$password = $cred['user_pass'];
|
||||
}
|
||||
|
||||
$user_data = apply_filters( 'opalestate_create_user_data', [
|
||||
'user_login' => $username,
|
||||
'user_pass' => $password,
|
||||
'user_email' => $user_email,
|
||||
] );
|
||||
|
||||
/* insert new wp user */
|
||||
$user_id = wp_insert_user( $user_data );
|
||||
if ( is_wp_error( $user_id ) ) {
|
||||
return new WP_Error( 'user-create-failed', $user_id->get_error_message() );
|
||||
}
|
||||
|
||||
/* allow hook like insert user meta. create new post type agent in opalmembership */
|
||||
do_action( 'opalmembership_create_new_user_successfully', $user_id, $user_data, $cred );
|
||||
|
||||
return $user_id;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user