add_action( 'wp_ajax_es_get_select2_users', 'es_ajax_get_select2_users' );
/**
* Return agent and seller users for select2 field.
*
* @return void
*/
function es_ajax_get_select2_users() {
if ( check_ajax_referer( 'get_select2_users' ) && current_user_can( 'manage_options' ) ) {
$args = array(
'role__in' => array( 'agent', 'seller' ), // Include both roles
'number' => 10,
);
if ( $s = es_get( 's' ) ) {
$args['search'] = sprintf( '*%s*', $s );
}
$users = get_users( $args );
$result[] = array( 'id' => '', 'text' => '' );
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
$user = es_get_user_entity( $user->ID );
$role = user_can( $user->get_id(), 'agent' ) ? 'Agent' : 'Seller';
$result[] = array(
'id' => $user->get_id(),
'text' => $user->get_full_name() . " ($role)"
);
}
}
$new_result['results'] = $result;
wp_die( json_encode( $new_result ) );
}
}
/**
* Ajax function to retrieve agent or seller information.
*/
function es_ajax_get_user_info() {
if ( check_ajax_referer( 'es_get_user_info' ) ) {
$user_id = intval( es_post( 'user_id' ) );
if ( $user_id ) {
if ( user_can( $user_id, 'agent' ) ) {
$user_data = es_get_agent( $user_id );
} elseif ( user_can( $user_id, 'seller' ) ) {
$user_data = es_get_seller( $user_id );
} else {
wp_die( json_encode( es_error_ajax_response( __( 'Invalid user role.', 'es' ) ) ) );
}
wp_die( json_encode( es_success_ajax_response( $user_data ) ) );
} else {
wp_die( json_encode( es_error_ajax_response( __( 'Invalid user ID.', 'es' ) ) ) );
}
}
}
add_action( 'wp_ajax_es_get_user_info', 'es_ajax_get_user_info' );
/**
* Add item to wishlist via ajax.
*
* @return string
*/
function es_ajax_wishlist_action() {
$post_id = es_clean( filter_input( INPUT_POST, 'post_id' ) );
$entity = es_clean( filter_input( INPUT_POST, 'entity' ) );
if ( $post_id ) {
$wishlist = es_get_wishlist_instance( $entity );
if ( $wishlist->has( $post_id ) ) {
$wishlist->remove( $post_id );
wp_die( json_encode( es_success_ajax_response( __( 'Item successfully removed from wishlist', 'es' ) ) ) );
} else {
$wishlist->add( $post_id );
wp_die( json_encode( es_success_ajax_response( __( 'Item successfully added to wishlist', 'es' ) ) ) );
}
} else {
wp_die( json_encode( es_error_ajax_response( __( 'Something wrong. Please contact support.', 'es' ) ) ) );
}
}
add_action( 'wp_ajax_es_wishlist_action', 'es_ajax_wishlist_action' );
add_action( 'wp_ajax_nopriv_es_wishlist_action', 'es_ajax_wishlist_action' );
/**
* Ajax function to get properties associated with a seller or agent.
*/
function es_ajax_get_user_properties() {
if ( check_ajax_referer( 'es_get_user_properties' ) ) {
$user_id = intval( es_post( 'user_id' ) );
if ( $user_id ) {
$query_args = array(
'post_type' => 'properties',
'author' => $user_id,
'post_status' => 'publish'
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) {
ob_start();
echo '
';
while ( $query->have_posts() ) {
$query->the_post();
es_load_template( 'front/property/content-archive.php', array(
'ignore_wrapper' => true,
'target_blank' => 'target="_blank"',
) );
}
echo "
";
wp_reset_postdata();
$content = ob_get_clean();
$response = array(
'status' => 'success',
'content' => $content,
);
wp_die( json_encode( $response ) );
} else {
wp_die( json_encode( es_error_ajax_response( __( 'No properties found.', 'es' ) ) ) );
}
} else {
wp_die( json_encode( es_error_ajax_response( __( 'Invalid user ID.', 'es' ) ) ) );
}
}
}
add_action( 'wp_ajax_es_get_user_properties', 'es_ajax_get_user_properties' );
add_action( 'wp_ajax_nopriv_es_get_user_properties', 'es_ajax_get_user_properties' );
if ( ! function_exists( 'es_get_the_property' ) ) {
/**
* Return property instance in loop.
*
* @param int $post
*
* @return Es_Property
*/
function es_get_the_property( $post = 0 ) {
$post = get_post( $post );
return es_get_property( $post->ID );
}
}
if ( ! function_exists( 'es_get_the_user' ) ) {
/**
* Return user (Agent or Seller) instance in loop.
*
* @param int $post
*
* @return Es_User_Post
*/
function es_get_the_user( $post = 0 ) {
$post = get_post( $post );
if ( user_can( $post->post_author, 'agent' ) ) {
return es_get_agent( $post->ID );
} elseif ( user_can( $post->post_author, 'seller' ) ) {
return es_get_seller( $post->ID );
} else {
return null;
}
}
}
if ( ! function_exists( 'es_get_the_agent' ) ) {
/**
* Return agent instance in loop.
*
* @param int $post
*
* @return Es_Agent_Post
*/
function es_get_the_agent( $post = 0 ) {
$post = get_post( $post );
return es_get_agent( $post->ID );
}
}
if ( ! function_exists( 'es_get_the_seller' ) ) {
/**
* Return seller instance in loop.
*
* @param int $post
*
* @return Es_Seller_Post
*/
function es_get_the_seller( $post = 0 ) {
$post = get_post( $post );
return es_get_seller( $post->ID );
}
}
if ( ! function_exists( 'es_get_the_user_avatar_url' ) ) {
/**
* Return user (Agent or Seller) avatar URL.
*
* @param int $post_id
* @param string $size
*
* @return mixed|void
*/
function es_get_the_user_avatar_url( $post_id = 0, $size = 'thumbnail' ) {
$post_id = $post_id ? $post_id : get_the_ID();
if ( user_can( $post_id, 'agent' ) ) {
return es_get_the_agent_avatar_url( $post_id, $size );
} elseif ( user_can( $post_id, 'seller' ) ) {
return es_get_the_seller_avatar_url( $post_id, $size );
}
return null;
}
}
if ( ! function_exists( 'es_get_the_seller_avatar_url' ) ) {
/**
* Return seller avatar URL.
*
* @param int $post_id
* @param string $size
*
* @return mixed|void
*/
function es_get_the_seller_avatar_url( $post_id = 0, $size = 'thumbnail' ) {
$post_id = $post_id ? $post_id : get_the_ID();
if ( has_post_thumbnail( $post_id ) ) {
$img = get_the_post_thumbnail_url( $post_id, $size );
} else {
$img = es_seller_get_default_image_url();
}
return apply_filters( 'es_get_the_seller_avatar_url', $img, $post_id, $size );
}
}
if ( ! function_exists( 'es_get_the_user_config' ) ) {
/**
* Get config for agent or seller.
*
* @param int $post_id
*
* @return mixed|void
*/
function es_get_the_user_config( $post_id = 0 ) {
$post_id = $post_id ? $post_id : get_the_ID();
$config = array();
$entity = es_get_entity_by_id( $post_id );
if ( $entity && ! empty( $entity->user_id ) ) {
foreach ( $entity->user_id as $user_id ) {
if ( user_can( $user_id, 'agent' ) ) {
$user = es_get_agent( $user_id );
$config[ $user_id ] = array(
'id' => $user_id,
'value' => $user_id,
'title' => get_the_title( $user_id ),
'subtitle' => $user->position,
'image' => es_get_the_agent_avatar( $user_id ),
);
} elseif ( user_can( $user_id, 'seller' ) ) {
$user = es_get_seller( $user_id );
$config[ $user_id ] = array(
'id' => $user_id,
'value' => $user_id,
'title' => get_the_title( $user_id ),
'subtitle' => 'Seller', // Customize subtitle for sellers
'image' => es_get_the_seller_avatar( $user_id ),
);
}
}
}
return apply_filters( 'es_get_the_user_config', $config, $post_id );
}
}
13 Must Reads for Real Estate Investors (Sept. 15, 2023) – Wealth Management - Realzify