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 '