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 ); } } /** * Check visible permissions for fb field or section. * * @param $fb_item array Section or Field config. * @param string $entity_name * @param string $context * * @return bool * @see es_property_get_field_info() */ function es_is_visible( $fb_item, $entity_name = 'property', $context = 'field' ) { $is_visible = true; if ( empty( $fb_item ) ) { $is_visible = false; } else { if ( $context == 'section' && es_is_entity_default_section_deactivated( $fb_item['machine_name'], $entity_name ) ) { $is_visible = false; } else if ( $context == 'field' && es_is_entity_default_field_deactivated( $fb_item['machine_name'], $entity_name ) ) { $is_visible = false; } else if ( isset( $fb_item['is_visible'] ) ) { if ( ! $fb_item['is_visible'] ) { $is_visible = false; } else { if ( ! empty( $fb_item['is_visible_for'] ) ) { $visible_for = maybe_unserialize( $fb_item['is_visible_for'] ); if ( in_array( 'agents', $visible_for ) && current_user_can( 'agent' ) ) { $is_visible = true; } else if ( in_array( 'sellers', $visible_for ) && current_user_can( 'seller' ) ) { // ✅ ADDED SELLER SUPPORT $is_visible = true; } else if ( in_array( 'all_users', $visible_for ) ) { $is_visible = true; } else if ( in_array( 'admin', $visible_for ) && current_user_can('administrator') ) { $is_visible = true; } else if ( in_array( 'authenticated_users', $visible_for ) && is_user_logged_in() ) { $is_visible = true; } else { $is_visible = false; } } } } } return apply_filters( 'es_is_visible', $is_visible, $fb_item ); } /** * @param $entity_name * * @return string */ function es_get_entity_plural_name( $entity_name ) { $plural = null; switch ( $entity_name ) { case 'agent': $plural = 'agents'; break; case 'seller': // ✅ ADDED SELLER SUPPORT $plural = 'sellers'; break; case 'property': $plural = 'properties'; break; case 'agency': $plural = 'agencies'; break; } return apply_filters( 'es_get_entity_plural_name', $plural, $entity_name ); } /** * @param $entity_name * * @return bool */ function es_is_entity_contact_btn_enabled( $entity_name ) { return ests( "is_{$entity_name}_contact_form_enabled" ) && ests( "is_{$entity_name}_contact_button_enabled" ); } /** * Delete an attachment from a seller, agent, or other entity. * * @param $attachment_id int * @param $field string * @param $entity Es_Entity */ function es_entity_delete_attachment( $attachment_id, $field, $entity ) { $media_fields = wp_list_filter( es_get_entity_fields( $entity::get_entity_name() ), array( 'type' => 'media' ) ); if ( ! empty( $media_fields ) ) { global $wpdb; $force_delete = true; delete_post_meta( $entity->get_id(), $entity->get_entity_prefix() . $field, $attachment_id ); foreach ( $media_fields as $media_field => $config ) { if ( $force_delete ) { if ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_value='%s' AND meta_key='%s'", $attachment_id, $entity->get_entity_prefix() . $media_field ) ) ) { $force_delete = false; } } } if ( $force_delete ) { wp_delete_attachment( $attachment_id, true ); } } else { wp_delete_attachment( $attachment_id, true ); } } Real Estate Investing – The Basics of Making It a Profit - Realzify