'Nonce verification failed' ] );
}
if ( ! current_user_can( 'read' ) ) {
wp_send_json_error( [ 'message' => 'Do not have any permission' ] );
}
$postid = isset( $_POST['listid'] ) ? absint( $_POST['listid'] ) : 0;
if ( ! $postid ) {
wp_send_json_error( [ 'message' => 'Invalid post ID' ] );
}
$post = get_post( $postid );
if ( ! $post ) {
wp_send_json_error( [ 'message' => 'Post not found' ] );
}
// Check if post type is one of our dynamic post types
$dynamic_post_types = adqs_get_directory_post_types();
if ( ! in_array( $post->post_type, $dynamic_post_types ) ) {
wp_send_json_error( [ 'message' => 'You can only delete listings of supported types' ] );
}
if ( (int) $post->post_author !== get_current_user_id() ) {
wp_send_json_error( [ 'message' => 'You do not have permission to delete this listing' ] );
}
$delete = wp_delete_post( $postid, true );
if ( $delete ) {
wp_send_json_success( [ 'message' => 'Listing deleted successfully' ] );
} else {
wp_send_json_error( [ 'message' => 'Failed to delete listing' ] );
}
}
/**
* Method remove_fav_listing
*
* @return void
*/
public function remove_fav_listing() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
}
if ( ! is_user_logged_in() ) {
wp_send_json_error( [ 'message' => 'Not allowed' ] );
}
$user_id = get_current_user_id();
$listing_id = absint( $_POST['postid'] );
$user_fav_list = ! empty( get_user_meta( $user_id, 'adqs_user_fav_list', true ) ) ? get_user_meta( $user_id, 'adqs_user_fav_list', true ) : [];
if ( in_array( $listing_id, $user_fav_list ) ) {
foreach ( array_keys( $user_fav_list, $listing_id ) as $key ) {
unset( $user_fav_list[ $key ] );
}
} else {
$user_fav_list[] = $listing_id;
}
update_user_meta( $user_id, 'adqs_user_fav_list', $user_fav_list );
wp_send_json_success();
}
/**
* Method get_user_fav_list
*
* @return void
*/
public function get_user_fav_list() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
return;
}
if ( ! is_user_logged_in() ) {
wp_send_json_error( [ 'message' => 'Not allowed' ] );
return;
}
$fav_listing_ids = ! empty( get_user_meta( get_current_user_id(), 'adqs_user_fav_list', true ) ) ? get_user_meta( get_current_user_id(), 'adqs_user_fav_list', true ) : [];
if ( empty( $fav_listing_ids ) ) {
wp_send_json_success( [] );
return;
}
$args = [
'post_type' => adqs_get_directory_post_types(),
'posts_per_page' => 10,
'post__in' => $fav_listing_ids,
];
$listing = new \WP_Query( $args );
$fav_content = [];
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) :
$listing->the_post();
$post = get_post( get_the_ID() );
$dir_type_id = 0;
if ( $post ) {
$dir_type_id = adqs_get_directory_id_by_post_type( $post->post_type );
}
$dir_term = get_term( $dir_type_id, ADQS_LISTING_TYPE_TAX );
ob_start();
adqs_get_template_part( 'global/price' );
$price = ob_get_clean();
$fav_content[] = [
'list_id' => get_the_ID(),
'feat_img' => get_default_preview_image( get_the_ID() ),
'title' => get_the_title(),
'permalink' => get_the_permalink(),
'dir_type' => $dir_term->name ?? '',
'price' => $price,
];
endwhile;
endif;
wp_reset_postdata();
wp_send_json_success( $fav_content );
return; // Unreachable but satisfies linter
}
/**
* Method manage_fav_listing
*
* @return void
*/
public function manage_fav_listing() {
if ( ! check_ajax_referer( 'adqs___grid_page', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
return;
}
if ( ! is_user_logged_in() ) {
wp_send_json_error( [ 'message' => 'Permission not allowed' ] );
return;
}
$user_id = get_current_user_id();
$listing_id = absint( $_POST['postid'] );
$user_fav_list = ! empty( get_user_meta( $user_id, 'adqs_user_fav_list', true ) ) ? get_user_meta( $user_id, 'adqs_user_fav_list', true ) : [];
if ( in_array( $listing_id, $user_fav_list ) ) {
foreach ( array_keys( $user_fav_list, $listing_id ) as $key ) {
unset( $user_fav_list[ $key ] );
}
} else {
$user_fav_list[] = $listing_id;
}
update_user_meta( $user_id, 'adqs_user_fav_list', $user_fav_list );
wp_send_json_success( $user_fav_list );
return; // Unreachable but satisfies linter
}
/**
* Method get_front_userdata
*
* @return void
*/
public function get_front_userdata() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
return;
}
$current_user = wp_get_current_user();
// Get user data
$user_data = [
'display_name' => $current_user->display_name,
'first_name' => $current_user->first_name,
'user_name' => $current_user->user_login,
'last_name' => $current_user->last_name,
'email' => $current_user->user_email,
'contactEmail' => get_user_meta( $current_user->ID, 'adqs_listing_conatct_email', true ),
'phone' => get_user_meta( $current_user->ID, 'adqs_phone', true ),
'website' => esc_url( $current_user->user_url ),
'address' => get_user_meta( $current_user->ID, 'adqs_address_info', true ),
'fb_url' => get_user_meta( $current_user->ID, 'adqs_facebook_profile', true ),
'twitter_url' => get_user_meta( $current_user->ID, 'adqs_twitter_profile', true ),
'linkedin_url' => get_user_meta( $current_user->ID, 'adqs_linked_profile', true ),
'instagram_url' => get_user_meta( $current_user->ID, 'adqs_instagram_profile', true ),
'description' => get_user_meta( $current_user->ID, 'description', true ),
'profile_picture' => get_user_meta( $current_user->ID, 'profile_picture', true ),
];
wp_send_json_success( $user_data );
return; // Unreachable but satisfies linter
}
/**
* Method user_profile_update
*
* @return void
*/
public function user_profile_update() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
}
$user_id = get_current_user_id();
if ( ! current_user_can( 'edit_user', $user_id ) ) {
wp_send_json_error( [ 'message' => 'You are not allowed to do this operation' ] );
}
$current_user = wp_get_current_user();
// Update basic user information
$user_data = [
'ID' => $current_user->ID,
'display_name' => isset( $_POST['dname'] ) ? sanitize_text_field( $_POST['dname'] ) : '',
'first_name' => isset( $_POST['fname'] ) ? sanitize_text_field( $_POST['fname'] ) : '',
'last_name' => isset( $_POST['lname'] ) ? sanitize_text_field( $_POST['lname'] ) : '',
'user_email' => isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '',
'user_url' => isset( $_POST['website'] ) ? esc_url( $_POST['website'] ) : '',
];
$user_id = wp_update_user( $user_data );
// Update additional user meta
$meta_data = [
'adqs_listing_conatct_email' => isset( $_POST['contactEmail'] ) ? sanitize_email( $_POST['contactEmail'] ) : '',
'adqs_phone' => isset( $_POST['phone'] ) ? sanitize_text_field( $_POST['phone'] ) : '',
'adqs_address_info' => isset( $_POST['address'] ) ? sanitize_text_field( $_POST['address'] ) : '',
'adqs_facebook_profile' => isset( $_POST['fburl'] ) ? sanitize_text_field( $_POST['fburl'] ) : '',
'adqs_twitter_profile' => isset( $_POST['twitterurl'] ) ? sanitize_text_field( $_POST['twitterurl'] ) : '',
'adqs_instagram_profile' => isset( $_POST['instaurl'] ) ? sanitize_text_field( $_POST['instaurl'] ) : '',
'adqs_linked_profile' => isset( $_POST['linkedinurl'] ) ? sanitize_text_field( $_POST['linkedinurl'] ) : '',
'description' => isset( $_POST['description'] ) ? sanitize_textarea_field( $_POST['description'] ) : '',
];
foreach ( $meta_data as $meta_key => $meta_value ) {
update_user_meta( $current_user->ID, $meta_key, $meta_value );
}
// Handle password update
$new_password = isset( $_POST['new_password'] ) ? $_POST['new_password'] : '';
$confirm_password = isset( $_POST['confirm_password'] ) ? $_POST['confirm_password'] : '';
if ( ! empty( $new_password ) && $new_password === $confirm_password ) {
// Validate password strength (minimum 8 characters)
if ( strlen( $new_password ) < 8 ) {
wp_send_json_error( [ 'message' => esc_html__( 'Password must be at least 8 characters long.', 'adirectory' ) ] );
}
wp_set_password( $new_password, $current_user->ID );
} elseif ( ! empty( $new_password ) && $new_password !== $confirm_password ) {
wp_send_json_error( [ 'message' => esc_html__( 'Passwords do not match.', 'adirectory' ) ] );
}
if ( ! empty( $_FILES['profileFile']['name'] ) ) {
// Validate file type
$allowed_types = [ 'image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp' ];
$file_type = $_FILES['profileFile']['type'];
if ( ! in_array( $file_type, $allowed_types ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Only image files (JPG, PNG, GIF, WebP) are allowed.', 'adirectory' ) ] );
}
// Validate file size (2MB max)
$max_size = 2 * 1024 * 1024; // 2MB in bytes
if ( $_FILES['profileFile']['size'] > $max_size ) {
wp_send_json_error( [ 'message' => esc_html__( 'File size must be less than 2MB.', 'adirectory' ) ] );
}
$upload_overrides = [ 'test_form' => false ];
$movefile = wp_handle_upload( $_FILES['profileFile'], $upload_overrides );
if ( $movefile && ! isset( $movefile['error'] ) ) {
$profile_picture_url = $movefile['url'];
update_user_meta( $current_user->ID, 'profile_picture', $profile_picture_url );
} else {
wp_send_json_error(
[
'message' => esc_html__( 'Error uploading file. Please try again.', 'adirectory' ),
]
);
}
}
wp_send_json_success();
}
/**
* Method qsd_user_dash_get_listings
*
* @return void
*/
public function qsd_user_dash_get_listings() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
return;
}
$author_id = absint( $_POST['author_id'] );
if ( get_current_user_id() !== $author_id ) {
wp_send_json_error( [ 'message' => 'You are not allowed to do this operation' ] );
return;
}
$paged = isset( $_POST['cuurent_page'] ) ? sanitize_text_field( $_POST['cuurent_page'] ) : 1;
$searchq = isset( $_POST['searchq'] ) ? sanitize_text_field( $_POST['searchq'] ) : false;
$directory_type = isset( $_POST['directory_type'] ) ? sanitize_text_field( $_POST['directory_type'] ) : '';
if ( empty( $author_id ) ) {
wp_send_json_error( [ 'message' => 'Author id missing' ] );
}
$post_status_key = isset( $_POST['post_status_key'] ) ? sanitize_text_field( $_POST['post_status_key'] ) : 'all-listings';
$post_status = '';
$posts_per_page = 6;
switch ( $post_status_key ) {
case 'published-listing':
$post_status = 'publish';
break;
case 'pending-listing':
$post_status = 'pending';
break;
case 'expired-listing':
$post_status = 'expired';
break;
case 'all-listing':
default:
// For 'all-listing', we'll use an array of all possible post statuses
// This includes the standard WordPress statuses plus the custom 'expired' status
$post_status = [ 'publish', 'pending', 'draft', 'private', 'expired' ];
break;
}
// Get post types based on directory type filter
if ( empty( $directory_type ) ) {
// If no directory type specified, get all directory types
$post_types = adqs_get_directory_post_types();
} else {
$post_type = adqs_get_post_type_by_directory( $directory_type );
$post_types = $post_type ? [ $post_type ] : adqs_get_directory_post_types();
}
$args = [
'post_type' => $post_types,
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'author' => $author_id,
];
// Only add post_status if it's not empty
if ( ! empty( $post_status ) ) {
$args['post_status'] = $post_status;
}
if ( $searchq ) {
$args['s'] = $searchq;
}
$meta_query = [];
if ( count( $meta_query ) > 0 ) {
if ( count( $meta_query ) > 1 ) {
$meta_query['relation'] = 'AND';
}
$args['meta_query'] = $meta_query;
}
$listings = new \WP_Query( $args );
$total_listing = $listings->found_posts;
$total_pages = ceil( $total_listing / $posts_per_page );
$listings_data = [];
if ( $listings->have_posts() ) {
while ( $listings->have_posts() ) {
$listings->the_post();
$post = get_post( get_the_ID() );
$listing_type_id = 0;
if ( $post ) {
$listing_type_id = adqs_get_directory_id_by_post_type( $post->post_type );
}
$pricing_order = get_post_meta( get_the_ID(), 'adqs_pricing_order_no', true );
$directory_term = get_term_by( 'id', $listing_type_id, 'adqs_listing_types' );
$directory_name = ! empty( $directory_term->name ) ? $directory_term->name : '';
$default_expiry = get_term_meta( $listing_type_id, '_qsd_term_default_expiry', true ) ? get_term_meta( $listing_type_id, '_qsd_term_default_expiry', true ) : 15;
$edit_query_args = [
'postid' => (int) get_the_ID(),
];
if ( ! empty( $pricing_order ) ) {
$edit_query_args['adqs_listing_type'] = (int) $listing_type_id;
$edit_query_args['adqs_order'] = (int) $pricing_order;
}
$edit_permalink = add_query_arg( $edit_query_args, esc_url( adqs_get_permalink_by_key( 'adqs_add_listing' ) ) );
$post_date = get_the_date( 'Y-m-d', get_the_ID() );
$default_expiry = date( 'Y-m-d', strtotime( $post_date . ' + ' . $default_expiry . ' days' ) );
$value = AD()->Helper->meta_val( get_the_ID(), '_expiry_date', '' );
if ( ! empty( $value ) ) {
$value = adqs_convert_expiry_date( get_the_ID() );
$default_expiry = wp_date( 'Y/m/d \a\t h:i a', strtotime( $value ) );
}
ob_start();
adqs_get_template_part( 'global/price' );
$price = ob_get_clean();
// Get post status (including 'expired' status)
$post_status = get_post_status( get_the_ID() );
$status_class = ( $post_status === 'expired' ) ? 'expire' : $post_status;
// Get expiry information
$expiry_date = adqs_convert_expiry_date( get_the_ID() );
$days_until_expiry = adqs_get_days_until_expiry( get_the_ID() );
$is_expiring_soon = adqs_is_listing_expiring_soon( get_the_ID() );
$is_expired = adqs_is_listing_expired( get_the_ID() );
$never_expire = get_post_meta( get_the_ID(), '_expiry_never', true ) === 'yes';
// Format expiry display
$expiry_display = '';
if ( $never_expire ) {
$expiry_display = esc_html__( 'Never Expires', 'adirectory' );
} elseif ( $is_expired ) {
$expiry_display = esc_html__( 'Expired', 'adirectory' );
} elseif ( $days_until_expiry !== false ) {
if ( $days_until_expiry === 0 ) {
$expiry_display = esc_html__( 'Expires Today', 'adirectory' );
} elseif ( $days_until_expiry === 1 ) {
$expiry_display = esc_html__( 'Expires Tomorrow', 'adirectory' );
} else {
$expiry_display = sprintf(
/* translators: %d: number of days */
esc_html__( 'Expires in %d days', 'adirectory' ),
$days_until_expiry
);
}
}
$listings_data[] = [
'id' => get_the_ID(),
'title' => get_the_title(),
'permalink' => get_the_permalink(),
'price' => $price,
'dir_type' => $directory_name,
'feat_img' => get_default_preview_image( get_the_ID(), 'thumbnail' ),
'status' => $status_class,
'default_expiry' => $never_expire ? esc_html__( 'No Expiry', 'adirectory' ) : $default_expiry,
'edit_permalink' => $edit_permalink,
'expiry_date' => $never_expire ? esc_html__( 'No Expiry', 'adirectory' ) : ( $expiry_date ? wp_date( get_option( 'date_format' ), strtotime( $expiry_date ) ) : '' ),
'expiry_date_raw' => $expiry_date,
'days_until_expiry' => $days_until_expiry,
'is_expiring_soon' => $is_expiring_soon,
'is_expired' => $is_expired,
'never_expire' => $never_expire,
'expiry_display' => $expiry_display,
];
}
}
wp_reset_postdata();
wp_send_json_success(
[
'listings' => $listings_data,
'total_pages' => $total_pages,
]
);
return; // Unreachable but satisfies linter
}
/**
* Method user_dash_get_pricing_package
*
* @return void
*/
public function user_dash_get_pricing_package() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
return;
}
$author_id = absint( $_POST['author_id'] );
$paged = isset( $_POST['current_page'] ) ? absint( $_POST['current_page'] ) : 1;
$searchq = isset( $_POST['searchq'] ) ? sanitize_text_field( $_POST['searchq'] ) : false;
$args = [];
if ( ! empty( $searchq ) ) {
$args['s'] = $searchq;
}
if ( empty( $author_id ) ) {
wp_send_json_error( [ 'message' => 'Author id missing' ] );
}
$status = isset( $_POST['status'] ) ? sanitize_text_field( $_POST['status'] ) : null;
$posts_per_page = 6;
$offset = ( $paged - 1 ) * $posts_per_page;
$allStatus = [];
if ( ! function_exists( 'adp_get_order_by_user_id' ) || ! function_exists( 'adp_get_order_count_by_user_id' ) ) {
wp_send_json_success(
[
'all_status' => [],
'packages' => [],
'total_pages' => 0,
]
);
return;
}
$getStatus = adp_get_order_by_user_id( $author_id, 'status', null, true );
foreach ( $getStatus as $s ) {
if ( ! in_array( $s->status, $allStatus ) ) {
$allStatus[] = $s->status ?? '';
}
}
$query_args = array_merge(
$args,
[
'posts_per_page' => $posts_per_page,
'offset' => $offset,
]
);
$pricingPackage = adp_get_order_by_user_id( $author_id, 'id,pricing_id,directory_id,price_payment,payment_type,status,order_date', $status, true, $query_args );
$total_listing = adp_get_order_count_by_user_id( $author_id, $status, $args );
$total_pages = ceil( $total_listing / $posts_per_page );
$packages_data = [];
if ( ! empty( $pricingPackage ) ) {
foreach ( $pricingPackage as $package ) {
$getPricing = function_exists( 'adp_get_query_pricing' ) ? adp_get_query_pricing( $package->pricing_id, 'name' ) : null;
$view_page = get_option( 'adp_view_page_id' );
$permalink = ! empty( $view_page ) ? get_permalink( absint( $view_page ) ) . 'ad-pricing/' . $package->pricing_id : '';
$order_permalink = ( ! empty( $view_page ) && function_exists( 'adp_get_order_page_url' ) ) ? adp_get_order_page_url( absint( $view_page ), $package->id ) : '';
$directory = get_term_by( 'term_id', absint( $package->directory_id ?? 0 ), 'adqs_listing_types' );
$directory_permalink = ! empty( $view_page ) ? add_query_arg( 'directory_type', $directory->slug ?? '', get_permalink( absint( $view_page ) ) ) : '';
$reg_listing = get_user_meta( $author_id, "adp_order_reg_listing_{$package->id}", true );
$fea_listing = get_user_meta( $author_id, "adp_order_fea_listing_{$package->id}", true );
$order_expire = get_user_meta( $author_id, "adp_order_expire_{$package->id}", true );
if ( ( $reg_listing !== 'unlimited' ) && ! empty( $reg_listing ) ) {
$reg_listing = esc_html__( 'Regular ', 'adirectory' ) . ' ' . $reg_listing;
}
if ( ( $fea_listing !== 'unlimited' ) && ! empty( $reg_listing ) ) {
$fea_listing = esc_html__( 'Featured ', 'adirectory' ) . ' ' . $fea_listing;
}
if ( ( $order_expire !== 'never_expire' ) && ! empty( $order_expire ) ) {
$order_expire = wp_date( 'F d, Y', strtotime( $order_expire ) );
}
if ( ( $order_expire === 'never_expire' ) ) {
$order_expire = esc_html__( 'Never ', 'adirectory' );
}
$remain_listing = [
'reg_listing' => $reg_listing,
'fea_listing' => $fea_listing,
];
$price = adqs_get_price_ws( $package->price_payment ?? '' );
if ( function_exists( 'adp_get_query_order_meta' ) && function_exists( 'adp_get_currency' ) ) {
if ( ! empty( adp_get_query_order_meta( $package->id, 'adqsp_currency_switch', true ) ) ) {
$currency_switch = adp_get_query_order_meta( $package->id, 'adqsp_currency_switch', true );
if ( adp_get_currency( $currency_switch, 'postion' ) === 'before' ) {
$price = esc_html( adp_get_currency( $currency_switch, 'symbol' ) ) . ( $package->price_payment ?? '' );
}
if ( adp_get_currency( $currency_switch, 'postion' ) === 'after' ) {
$price = ( $package->price_payment ?? '' ) . esc_html( adp_get_currency( $currency_switch, 'symbol' ) );
}
}
}
$packages_data[] = [
'order_id' => $package->id ?? '',
'package_name' => $getPricing->name ?? '',
'plan_type' => ucwords( $directory->name ?? '' ),
'permalink' => $permalink,
'order_permalink' => $order_permalink,
'directory_permalink' => $directory_permalink,
'price' => $price,
'remain_listing' => $remain_listing,
'payment_type' => ucwords( str_replace( '_', ' ', $package->payment_type ?? '' ) ),
'status' => $package->status ?? '',
'order_date' => wp_date( 'F d, Y', strtotime( $package->order_date ) ),
'order_expire' => $order_expire,
];
}
}
wp_send_json_success(
[
'all_status' => $allStatus,
'packages' => $packages_data,
'total_pages' => $total_pages,
]
);
return; // Unreachable but satisfies linter
}
/**
* Method submit_ajax_review_comment
*
* @return void
*/
public function submit_ajax_review_comment() {
check_ajax_referer( 'adqs___directory_frontend', 'security' );
// Check if review system is enabled
$enable_review = AD()->Helper->get_setting( 'enable_review' );
if ( empty( $enable_review ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Review system is disabled. Reviews cannot be submitted.', 'adirectory' ) ] );
}
global $comment;
$errorMessage = false;
$alreadyHasComment = false;
$comment_html = '';
$email = sanitize_email( $_POST['email'] ?? '' );
$comment_post_ID = absint( sanitize_text_field( $_POST['comment_post_ID'] ?? 0 ) );
// Check if the listing has pricing package permission for customer reviews
if ( function_exists( 'adqs_listing_has_pricing_permission' ) ) {
if ( ! adqs_listing_has_pricing_permission( $comment_post_ID, 'customer_reviews' ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Customer Reviews feature is not available for this listing.', 'adirectory' ) ] );
}
}
// Check if only logged-in users can submit reviews
$only_logged_in_reviews = (bool) AD()->Helper->get_setting( 'enable_review_comment_only_logged_in' );
if ( $only_logged_in_reviews && ! is_user_logged_in() ) {
wp_send_json_error( [ 'message' => esc_html__( 'You must be logged in to submit a review.', 'adirectory' ) ] );
}
$hasComment = (int) get_comments(
[
'author_email' => $email,
'post_id' => $comment_post_ID,
'status' => 'approve',
'count' => true,
]
);
if ( ! empty( $email ) && ( $hasComment > 0 ) ) {
$alreadyHasComment = true;
} else {
$author_id = get_post_field( 'post_author', $comment_post_ID );
// Clear author review count cache
AD()->Helper->clear_author_review_cache( $author_id );
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
if ( is_wp_error( $comment ) ) {
$errorMessage = '
' . esc_html__( 'Comment Submission Failure', 'adirectory' ) . '
';
} else {
$this->update_avg_ratings( $comment_post_ID );
// Send email notification for new review
if ( $comment && ! is_wp_error( $comment ) ) {
$this->send_review_notification( $comment, $comment_post_ID );
}
}
/*
* Set Cookies
*/
$user = wp_get_current_user();
do_action( 'set_comment_cookies', $comment, $user );
}
// Ensure variables are defined even if no comment HTML is produced
$comment_html = isset( $comment_html ) ? $comment_html : '';
$avgRatings_html = '';
if ( ! empty( $comment ) ) :
ob_start();
adqs_get_template_part( 'single-listing/review' );
$comment_html = ob_get_clean();
// avgRatings_html is already initialized above; now capture rendered content
ob_start();
$Helper = AD()->Helper;
$avgRatings = (float) $Helper->get_post_average_ratings( $comment_post_ID );
$countReview = adqs_get_parent_reviews_count( $comment_post_ID );
if ( ! empty( $countReview ) ) :
?>
get_review_rating_html( $avgRatings ); ?>
1 ? esc_html__( 'Reviews', 'adirectory' ) : esc_html__( 'Review', 'adirectory' );
echo esc_html( $countReview ) . ' ' . esc_html( $reviewText );
?>
$comment_html,
'avgRatings_html' => $avgRatings_html,
'alreadyHasComment' => $alreadyHasComment,
'errorMessage' => $errorMessage,
]
);
return; // Unreachable but satisfies linter
}
/**
* check has next comment
*
* @since 1.0.0
* @return bool
*/
public function has_next_review_comment( $comment_args ) {
if ( isset( $comment_args['paged'] ) ) {
$comment_args['paged'] = $comment_args['paged'] + 1;
$comments = get_comments( $comment_args );
if ( ! empty( $comments ) ) {
return true;
}
}
return false;
}
/**
* show more review comment
*
* @since 1.0.0
* @return void
*/
public function show_more_review_comment() {
check_ajax_referer( 'adqs___directory_frontend', 'security' );
// Check if review system is enabled
$enable_review = AD()->Helper->get_setting( 'enable_review' );
if ( empty( $enable_review ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Review system is disabled.', 'adirectory' ) ] );
}
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
$per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 3;
$current_page = isset( $_POST['current_page'] ) ? absint( $_POST['current_page'] ) : 1;
$current_page = $current_page + 1;
$comment_args = [
'post_id' => $post_id,
'number' => $per_page,
'paged' => $current_page,
'parent' => 0,
'status' => 'approve',
];
$comments = get_comments( $comment_args );
global $comment, $comment_id, $comment_author_id;
ob_start();
if ( ! empty( $comments ) ) :
foreach ( $comments as $comment ) :
$comment_id = get_comment_ID();
$comment_author_id = ! empty( $comment->user_id ) ? $comment->user_id : 0;
adqs_get_template_part( 'single-listing/review' );
endforeach;
endif;
$comment_html = ob_get_clean();
wp_send_json_success(
[
'comment_html' => $comment_html,
'get_current_page' => $current_page,
'has_next' => $this->has_next_review_comment( $comment_args ),
]
);
return; // Unreachable but satisfies linter
}
/**
* Method listing_contact_owner
*
* @return void
*/
public function listing_contact_owner() {
check_ajax_referer( 'adqs___directory_frontend', 'security' );
$listing_id = ! empty( $_POST['adqs_listing_id'] ) ? absint( $_POST['adqs_listing_id'] ) : 0;
$auth_id = ! empty( $_POST['adqs_co'] ) ? absint( $_POST['adqs_co'] ) : 0;
if ( ! $listing_id || ! $auth_id ) {
wp_send_json_error( __( 'Invalid listing or author ID.', 'adirectory' ) );
wp_die();
}
// Check if the listing has pricing package permission for contact owner
if ( function_exists( 'adqs_listing_has_pricing_permission' ) ) {
if ( ! adqs_listing_has_pricing_permission( $listing_id, 'contact_owner' ) ) {
wp_send_json_error( __( 'Contact Listing Owner feature is not available for this listing.', 'adirectory' ) );
wp_die();
}
}
$to = get_the_author_meta( 'user_email', $auth_id );
if ( get_the_author_meta( 'adqs_listing_conatct_email', $auth_id ) ) {
$to = get_the_author_meta( 'adqs_listing_conatct_email', $auth_id );
}
$subject = esc_html__( 'For listing', 'adirectory' ) . ' : ' . wp_strip_all_tags( get_the_title( $listing_id ) );
if ( ! $to ) {
wp_send_json_error( __( 'No email found for the author.', 'adirectory' ) );
wp_die();
}
// Validate recipient email
if ( ! is_email( $to ) ) {
wp_send_json_error( __( 'Invalid recipient email address.', 'adirectory' ) );
wp_die();
}
// Retrieve sender's name and email from POST
$sender_name = sanitize_text_field( $_POST['adqs_ca_name'] ?? '' );
$sender_email = sanitize_email( $_POST['adqs_ca_email'] ?? '' );
$sender_message = sanitize_textarea_field( $_POST['adqs_ca_msg'] ?? '' );
// Basic validation
if ( empty( $sender_name ) ) {
wp_send_json_error( __( 'Name is required.', 'adirectory' ) );
wp_die();
}
if ( ! $sender_email || ! is_email( $sender_email ) ) {
wp_send_json_error( __( 'Please enter a valid email address.', 'adirectory' ) );
wp_die();
}
if ( empty( $sender_message ) ) {
wp_send_json_error( __( 'Message is required.', 'adirectory' ) );
wp_die();
}
// Build email body with better formatting
ob_start();
?>
',
'Reply-To: ' . esc_html( $sender_name ) . ' <' . esc_html( $sender_email ) . '>',
];
// Send the email with fallback
$send_mail = $this->send_email_with_fallback( $to, $subject, $body, $headers, $site_email, $sender_name, $sender_email );
if ( ! $send_mail ) {
// Get detailed error information for better user feedback
global $phpmailer;
$error_message = __( 'Failed to send email. Please try again.', 'adirectory' );
if ( isset( $phpmailer ) && ! empty( $phpmailer->ErrorInfo ) ) {
$phpmailer_error = $phpmailer->ErrorInfo;
// Provide user-friendly error messages based on common issues
if ( strpos( $phpmailer_error, 'Could not instantiate mail function' ) !== false ) {
$error_message = __( 'Email service is not configured on this server. Please contact the administrator.', 'adirectory' );
} elseif ( strpos( $phpmailer_error, 'SMTP Error' ) !== false ) {
$error_message = __( 'SMTP server error. Please try again later.', 'adirectory' );
} elseif ( strpos( $phpmailer_error, 'Connection refused' ) !== false ) {
$error_message = __( 'Cannot connect to email server. Please try again later.', 'adirectory' );
} elseif ( strpos( $phpmailer_error, 'Authentication failed' ) !== false ) {
$error_message = __( 'Email authentication failed. Please contact the administrator.', 'adirectory' );
} elseif ( strpos( $phpmailer_error, 'Invalid address' ) !== false ) {
$error_message = __( 'Invalid email address. Please check your email and try again.', 'adirectory' );
}
}
wp_send_json_error( $error_message );
} else {
wp_send_json_success( [ 'send_mail' => true ] );
}
wp_die();
}
/**
* Send email with fallback to PHP mail function
*
* @param string $to Recipient email
* @param string $subject Email subject
* @param string $body Email body (HTML)
* @param array $headers Email headers
* @param string $from_email From email address
* @param string $sender_name Sender name
* @param string $sender_email Sender email
* @return bool Success status
*/
private function send_email_with_fallback( $to, $subject, $body, $headers, $from_email, $sender_name, $sender_email ) {
// Check if we're in local development environment
$is_local = (
strpos( $_SERVER['HTTP_HOST'], 'localhost' ) !== false ||
strpos( $_SERVER['HTTP_HOST'], '127.0.0.1' ) !== false ||
strpos( $_SERVER['HTTP_HOST'], '.local' ) !== false
);
// In local development, skip wp_mail() entirely to avoid file generation
// and use only PHP mail() with proper headers
if ( $is_local ) {
$php_headers = [
'From: ' . $from_email,
'Reply-To: ' . $sender_name . ' <' . $sender_email . '>',
'Content-Type: text/html; charset=UTF-8',
];
$php_mail_result = mail( $to, $subject, $body, implode( "\r\n", $php_headers ) );
return $php_mail_result;
}
// For production, try wp_mail first (most reliable)
$wp_mail_result = wp_mail( $to, $subject, $body, $headers );
if ( $wp_mail_result ) {
return true;
}
// If wp_mail failed, try PHP mail() as single fallback
$php_headers = [
'From: ' . $from_email,
'Reply-To: ' . $sender_name . ' <' . $sender_email . '>',
'Content-Type: text/html; charset=UTF-8',
];
$php_mail_result = mail( $to, $subject, $body, implode( "\r\n", $php_headers ) );
return $php_mail_result;
}
/**
* Method ajax_filters_listings
*
* @return void
*/
public function ajax_tax_filters_listings() {
check_ajax_referer( 'adqs_ajax_filter', 'security' );
$view_type = '';
$form_data = isset( $_REQUEST['form_data'] ) ? wp_unslash( $_REQUEST['form_data'] ) : '';
$base_url = isset( $_REQUEST['base_url'] ) ? sanitize_url( $_REQUEST['base_url'] ) : '';
$paged = isset( $_REQUEST['paged'] ) ? absint( sanitize_text_field( $_REQUEST['paged'] ) ) : 1;
$per_page = isset( $_REQUEST['per_page'] ) ? (int) sanitize_text_field( $_REQUEST['per_page'] ) : 8;
$pagination_type = isset( $_REQUEST['pagination_type'] ) ? sanitize_text_field( $_REQUEST['pagination_type'] ) : 'pagination';
$tax_type = isset( $_REQUEST['tax_type'] ) ? sanitize_text_field( $_REQUEST['tax_type'] ) : 'categories';
$tax_name = isset( $_REQUEST['tax_name'] ) ? sanitize_text_field( $_REQUEST['tax_name'] ) : '';
$order = isset( $_REQUEST['order'] ) ? sanitize_text_field( $_REQUEST['order'] ) : '';
$orderby = isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : '';
$ajax_filter = isset( $_REQUEST['ajax_filter'] ) ? sanitize_text_field( $_REQUEST['ajax_filter'] ) : '';
$uniqId = isset( $_REQUEST['uniq_id'] ) ? sanitize_text_field( $_REQUEST['uniq_id'] ) : '';
$terms = isset( $_REQUEST['terms'] ) ? wp_unslash( $_REQUEST['terms'] ) : [];
parse_str( $form_data, $datas );
$directory_types = adqs_get_directories();
$active_type = sanitize_text_field( $datas['directory_type'] ?? '' );
// Taxonomies are now static and shared across all post types
$category_taxonomy = 'adqs_category';
$location_taxonomy = 'adqs_location';
$tax_name = $tax_type === 'categories' ? $category_taxonomy : $location_taxonomy;
$per_page = (int) $per_page;
$args = [
'taxonomy' => $tax_name,
'hide_empty' => true,
'number' => $per_page,
'order' => $order,
'orderby' => $orderby,
];
// Use the already fetched directory types instead of calling the function again
$all_listing_type = $directory_types;
$allTermsIds = [];
// Get custom_directory directly from request
$custom_directory_param = isset( $_REQUEST['custom_directory'] ) ? sanitize_text_field( $_REQUEST['custom_directory'] ) : '';
$custom_directory = ! empty( $custom_directory_param ) ? explode( ',', $custom_directory_param ) : [];
// Clean up array values
$custom_directory = array_map( 'trim', $custom_directory );
// Determine which directories to filter by
$directories_to_filter = [];
// If a specific directory type is selected via URL parameter, use only that directory
if ( ! empty( $active_type ) && empty( $terms ) ) {
$directories_to_filter = [ $active_type ];
}
// If "All" is selected (empty active_type) and custom_directory is specified, use custom directories
elseif ( empty( $active_type ) && ! empty( $custom_directory ) && empty( $terms ) ) {
$directories_to_filter = $custom_directory;
}
// Filter terms by the determined directories
if ( ! empty( $directories_to_filter ) && empty( $terms ) ) {
// Convert directory slugs to post types using proper function
$directory_post_types = [];
foreach ( $directories_to_filter as $dir_slug ) {
$post_type = adqs_get_post_type_by_directory( trim( $dir_slug ) );
if ( ! empty( $post_type ) && post_type_exists( $post_type ) ) {
$directory_post_types[] = $post_type;
}
}
if ( ! empty( $directory_post_types ) ) {
// Query posts from these specific post types
$posts_query = new \WP_Query(
[
'post_type' => $directory_post_types,
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => [
[
'taxonomy' => $tax_name,
'operator' => 'EXISTS',
],
],
]
);
if ( $posts_query->have_posts() ) {
// Get all term IDs associated with these posts
$term_ids = [];
foreach ( $posts_query->posts as $post_id ) {
$post_terms = wp_get_post_terms( $post_id, $tax_name, [ 'fields' => 'ids' ] );
if ( ! is_wp_error( $post_terms ) && ! empty( $post_terms ) ) {
$term_ids = array_merge( $term_ids, $post_terms );
}
}
$allTermsIds = array_unique( $term_ids );
}
wp_reset_postdata();
}
}
// If specific terms are provided via the terms parameter, use them (overrides directory filtering)
if ( ! empty( $terms ) ) {
$terms = $terms[ $tax_name ] ?? '';
$terms = adqs_covertTerm_slug_to_id( $terms, $tax_name );
$allTermsIds = array_map( 'absint', explode( ',', $terms ) );
}
if ( ! empty( $datas['tax_search'] ?? '' ) ) {
$args['search'] = $datas['tax_search'];
}
$allTermsIds = array_unique( array_filter( $allTermsIds ) );
if ( ! empty( $allTermsIds ) ) {
$args['include'] = $allTermsIds;
}
$pagination_args = [];
if ( ( $pagination_type === 'pagination' ) && $per_page > 0 ) {
$page = $paged ? $paged : 1;
$args['offset'] = ( $page - 1 ) * $per_page;
// Pagination
$totalArgs = [
'taxonomy' => $tax_name,
'hide_empty' => true,
];
// If we have specific terms, count only those
if ( ! empty( $allTermsIds ) ) {
$totalArgs['include'] = $allTermsIds;
}
if ( ! empty( $datas['tax_search'] ?? '' ) ) {
$totalArgs['search'] = $datas['tax_search'];
}
$total_terms = wp_count_terms( $totalArgs );
$pagination_args = [
'base' => trailingslashit( $base_url ) . '%_%',
'format' => 'page/%#%/',
'current' => $page,
'total' => ceil( $total_terms / $per_page ),
'show_all' => false,
'type' => 'list',
'prev_next' => true,
'prev_text' => '',
'next_text' => '',
];
}
$get_category_terms = get_terms( $args );
// Determine template slug based on taxonomy type
$template_slug = 'taxonomy';
if ( $tax_type === 'locations' ) {
$template_slug = 'taxonomy-locations';
}
ob_start();
adqs_get_template_part(
'content',
$template_slug,
compact(
'all_listing_type',
'get_category_terms',
'tax_name',
'active_type',
'per_page',
'pagination_args',
'pagination_type',
'uniqId',
'ajax_filter',
)
);
$results_html = ob_get_clean();
// Return the results as JSON
wp_send_json_success(
[
'html_content' => $results_html,
]
);
wp_die();
} // end
/**
* Method ajax_filters_listings
*
* @return void
*/
public function ajax_filters_listings() {
check_ajax_referer( 'adqs_ajax_filter', 'security' );
$view_type = '';
$form_data = isset( $_REQUEST['form_data'] ) ? wp_unslash( $_REQUEST['form_data'] ) : '';
$base_url = isset( $_REQUEST['base_url'] ) ? sanitize_url( $_REQUEST['base_url'] ) : '';
$filter_layout = isset( $_REQUEST['filter_layout'] ) ? sanitize_text_field( $_REQUEST['filter_layout'] ) : 'top';
$paged = isset( $_REQUEST['paged'] ) ? absint( sanitize_text_field( $_REQUEST['paged'] ) ) : 1;
$setting_per_page = ! empty( AD()->Helper->get_setting( 'listing_per_page' ) ) ? absint( AD()->Helper->get_setting( 'listing_per_page' ) ) : 6;
$per_page = isset( $_REQUEST['per_page'] ) ? (int) sanitize_text_field( $_REQUEST['per_page'] ) : $setting_per_page;
$pagination_type = isset( $_REQUEST['pagination_type'] ) ? sanitize_text_field( $_REQUEST['pagination_type'] ) : 'pagination';
$directory_type = isset( $_REQUEST['directory_type'] ) ? sanitize_text_field( $_REQUEST['directory_type'] ) : '';
$custom_directory = isset( $_REQUEST['custom_directory'] ) ? sanitize_text_field( $_REQUEST['custom_directory'] ) : '';
$view_type = isset( $_REQUEST['view_type'] ) ? sanitize_text_field( $_REQUEST['view_type'] ) : 'grid';
$sort_by = isset( $_REQUEST['sort_by'] ) ? sanitize_text_field( $_REQUEST['sort_by'] ) : '';
$categories = isset( $_REQUEST['categories'] ) ? sanitize_text_field( $_REQUEST['categories'] ) : '';
$locations = isset( $_REQUEST['locations'] ) ? sanitize_text_field( $_REQUEST['locations'] ) : '';
$tags = isset( $_REQUEST['tags'] ) ? sanitize_text_field( $_REQUEST['tags'] ) : '';
$rating = isset( $_REQUEST['rating'] ) ? sanitize_text_field( $_REQUEST['rating'] ) : '';
$display_listings = isset( $_REQUEST['display_listings'] ) ? sanitize_text_field( $_REQUEST['display_listings'] ) : '';
$uniqId = isset( $_REQUEST['uniq_id'] ) ? sanitize_text_field( $_REQUEST['uniq_id'] ) : '';
parse_str( $form_data, $datas );
// Determine post types: URL param > widget setting > all
if ( ! empty( $directory_type ) ) {
$post_type = adqs_get_post_type_by_directory( $directory_type );
$post_types = ! empty( $post_type ) ? [ $post_type ] : adqs_get_directory_post_types();
} elseif ( ! empty( $custom_directory ) ) {
$directory_slugs = array_filter( array_map( 'trim', explode( ',', $custom_directory ) ) );
$post_types = [];
foreach ( $directory_slugs as $slug ) {
$post_type = adqs_get_post_type_by_directory( $slug );
if ( ! empty( $post_type ) && post_type_exists( $post_type ) ) {
$post_types[] = $post_type;
}
}
if ( empty( $post_types ) ) {
$post_types = adqs_get_directory_post_types();
}
} else {
$post_types = adqs_get_directory_post_types();
}
$queryArgs = [
'post_type' => $post_types,
'posts_per_page' => $per_page,
'post_status' => 'publish',
];
if ( ( $pagination_type === 'pagination' ) ) {
$queryArgs['paged'] = $paged;
}
$notFiltersKeys = [ 'view_type', 'sort_by' ];
// Pass directory context for proper filtering
if ( ! empty( $directory_type ) ) {
$queryArgs['directory_type'] = $directory_type;
}
if ( ! empty( $custom_directory ) ) {
$queryArgs['custom_directory'] = $custom_directory;
}
if ( ! empty( $categories ) ) {
$queryArgs['categories'] = $categories;
}
if ( ! empty( $locations ) ) {
$queryArgs['locations'] = $locations;
}
if ( ! empty( $tags ) ) {
$queryArgs['tags'] = adqs_covertTerm_slug_to_id( $tags );
}
if ( ! empty( $rating ) ) {
$queryArgs['rating'] = $rating;
}
if ( ! empty( $display_listings ) ) {
$queryArgs['display_listings'] = $display_listings;
}
if ( ! empty( $datas ) && is_array( $datas ) ) {
foreach ( $datas as $name => $value ) {
if ( ! in_array( $name, $notFiltersKeys ) ) {
// Handle arrays properly (for checkboxes)
if ( is_array( $value ) ) {
$queryArgs[ $name ] = array_map( 'sanitize_text_field', $value );
} else {
$queryArgs[ $name ] = sanitize_text_field( $value ?? '' );
}
}
}
}
$query_args = adqs_listing_query_filter_args( $queryArgs );
/**
* Short By
*/
if ( isset( $datas['sort_by'] ) ) {
$sort_by = $datas['sort_by'];
}
$sortByArgs = adqs_listing_query_sort_by( $sort_by );
if ( ! empty( $sortByArgs ) ) {
$query_args = array_merge( $query_args, $sortByArgs );
}
// Add parsed form data to query args for filters
$query_args['bh_status'] = $datas['bh_status'] ?? '';
$query_args['bh_days'] = $datas['bh_days'] ?? '';
$query_args['bh_open'] = $datas['bh_open'] ?? '';
$query_args['bh_close'] = $datas['bh_close'] ?? '';
$query_args = apply_filters( 'adqs_listing_query_ajax_filter_args', $query_args, $queryArgs );
$listings_query = new \WP_Query( $query_args );
if ( isset( $datas['view_type'] ) ) {
$view_type = $datas['view_type'];
}
ob_start();
adqs_get_template_part( 'grid/gridlist', 'view', compact( 'listings_query', 'view_type', 'base_url', 'paged', 'pagination_type', 'uniqId', 'filter_layout' ) );
// Collect the results
$results_html = ob_get_clean();
// Return the results as JSON
wp_send_json_success(
[
'html_content' => $results_html,
]
);
wp_die();
} // end
public function ajax_mapview_filters_listings() {
// Match frontend localized nonce from grid-page-script
check_ajax_referer( 'adqs___grid_page', 'security' );
$directory_type = isset( $_REQUEST['directory_type'] ) ? sanitize_text_field( $_REQUEST['directory_type'] ) : '';
$custom_directory = isset( $_REQUEST['custom_directory'] ) ? sanitize_text_field( $_REQUEST['custom_directory'] ) : '';
$ajax_filter = true;
ob_start();
adqs_get_template_part( 'grid/map', 'view', compact( 'directory_type', 'custom_directory', 'ajax_filter' ) );
$results_html = ob_get_clean();
wp_send_json_success(
[
'results_html' => $results_html,
]
);
}
public function ajax_change_taxonomy() {
check_ajax_referer( 'adqs_ajax_filter', 'security' );
$directory_type = isset( $_REQUEST['directory_type'] ) ? sanitize_text_field( $_REQUEST['directory_type'] ) : '';
$custom_directory = isset( $_REQUEST['custom_directory'] ) ? sanitize_text_field( $_REQUEST['custom_directory'] ) : '';
// Taxonomies are now static and shared across all post types
$categories_taxonomy = 'adqs_category';
$locations_taxonomy = 'adqs_location';
$defaultOptions = [
'has_input' => true,
'has_children' => true,
'has_name' => false,
'value_type' => 'slug',
];
// Determine which directories to filter by
$filter_directories = [];
if ( ! empty( $directory_type ) ) {
// A specific directory is selected via URL - show only its terms
$filter_directories = [ $directory_type ];
} elseif ( ! empty( $custom_directory ) ) {
// Use the directories from widget settings
$custom_dir_array = array_filter( array_map( 'trim', explode( ',', $custom_directory ) ) );
if ( ! empty( $custom_dir_array ) ) {
$filter_directories = $custom_dir_array;
}
}
// Add directory filtering if directories are specified
if ( ! empty( $filter_directories ) ) {
$directory_post_types = [];
foreach ( $filter_directories as $dir_slug ) {
$post_type = adqs_get_post_type_by_directory( $dir_slug );
if ( ! empty( $post_type ) && post_type_exists( $post_type ) ) {
$directory_post_types[] = $post_type;
}
}
if ( ! empty( $directory_post_types ) ) {
$defaultOptions['filter_by_post_types'] = $directory_post_types;
}
}
ob_start();
adqs_render_tax_lists(
[
'taxonomy' => $categories_taxonomy,
'hide_empty' => true,
],
$defaultOptions
);
$categories_html = ob_get_clean();
ob_start();
adqs_render_tax_lists(
[
'taxonomy' => $locations_taxonomy,
'hide_empty' => true,
],
$defaultOptions
);
$locations_html = ob_get_clean();
wp_send_json_success(
[
'categories' => ! empty( $categories_html ) ? $categories_html : '',
'locations' => ! empty( $locations_html ) ? $locations_html : '',
]
);
}
public function ajax_change_filters_custom_fields() {
check_ajax_referer( 'adqs_ajax_filter', 'security' );
$directory_type = isset( $_REQUEST['directory_type'] ) ? sanitize_text_field( $_REQUEST['directory_type'] ) : '';
// If directory_type is empty, it means "All" directories - return empty filter fields
if ( empty( $directory_type ) ) {
wp_send_json_success(
[
'filter_fields' => '',
]
);
return;
}
ob_start();
$dir_term = get_term_by( 'slug', $directory_type, 'adqs_listing_types' );
$dir_id = ( $dir_term && ! is_wp_error( $dir_term ) ) ? $dir_term->term_id : 0;
$allMetaFields = adqs_metafields_get_search_filters( $dir_id );
if ( ! empty( $allMetaFields ) ) {
do_action( 'adqs_ajax_custom_filter_fields', $allMetaFields );
}
$filter_fields_html = ob_get_clean();
wp_send_json_success(
[
'filter_fields' => ! empty( $filter_fields_html ) ? $filter_fields_html : '',
]
);
}
public function ajax_agents_filters() {
check_ajax_referer( 'adqs_agent_ajax_filter', 'security' );
$page_url = isset( $_REQUEST['page_url'] ) ? sanitize_text_field( $_REQUEST['page_url'] ) : '';
$directory_type = isset( $_REQUEST['directory_type'] ) ? sanitize_text_field( $_REQUEST['directory_type'] ) : '';
$per_page = isset( $_REQUEST['per_page'] ) ? sanitize_text_field( $_REQUEST['per_page'] ) : '';
$directory_dropdown = isset( $_REQUEST['directory_dropdown'] ) ? sanitize_text_field( $_REQUEST['directory_dropdown'] ) : '';
$ag_sort_by = isset( $_REQUEST['ag_sort_by'] ) ? sanitize_text_field( $_REQUEST['ag_sort_by'] ) : '';
$ag_verify = isset( $_REQUEST['ag_verify'] ) ? sanitize_text_field( $_REQUEST['ag_verify'] ) : '';
$agent_search = isset( $_REQUEST['agent_search'] ) ? sanitize_text_field( $_REQUEST['agent_search'] ) : '';
$current_page = isset( $_REQUEST['current_page'] ) ? sanitize_text_field( $_REQUEST['current_page'] ) : '';
$ajax_filter = true;
ob_start();
adqs_get_template_part(
'content',
'agents',
compact( 'per_page', 'directory_dropdown', 'directory_type', 'ajax_filter', 'ag_sort_by', 'ag_verify', 'agent_search', 'current_page', 'page_url' )
);
$html_content = ob_get_clean();
wp_send_json_success(
[
'html_content' => ! empty( $html_content ) ? $html_content : '',
]
);
}
/**
* Method submit_review_reply
* Handle AJAX submission of review replies
*
* @return void
*/
public function submit_review_reply() {
check_ajax_referer( 'adqs___directory_frontend', 'security' );
// Check if review system is enabled
$enable_review = AD()->Helper->get_setting( 'enable_review' );
if ( empty( $enable_review ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Review system is disabled. Replies cannot be submitted.', 'adirectory' ) ] );
}
$parent_comment_id = isset( $_POST['parent_comment_id'] ) ? absint( $_POST['parent_comment_id'] ) : 0;
$reply_text = isset( $_POST['reply_comment'] ) ? sanitize_textarea_field( $_POST['reply_comment'] ) : '';
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
// Validate input
if ( empty( $parent_comment_id ) || empty( $reply_text ) || empty( $post_id ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Missing required fields.', 'adirectory' ) ] );
}
// Check if reply functionality is enabled
$enable_replies = AD()->Helper->get_setting( 'enable_review_comment_reply' );
if ( ! $enable_replies ) {
wp_send_json_error( [ 'message' => esc_html__( 'Reply functionality is disabled.', 'adirectory' ) ] );
}
// If only logged-in users can reply, enforce login
$only_logged_in_replies = (bool) AD()->Helper->get_setting( 'enable_review_comment_reply_only_logged_in' );
if ( $only_logged_in_replies && ! is_user_logged_in() ) {
wp_send_json_error( [ 'message' => esc_html__( 'You must be logged in to post a reply.', 'adirectory' ) ] );
}
// Verify parent comment exists and belongs to the post
$parent_comment = get_comment( $parent_comment_id );
if ( ! $parent_comment || $parent_comment->comment_post_ID != $post_id ) {
wp_send_json_error( [ 'message' => esc_html__( 'Invalid parent comment.', 'adirectory' ) ] );
}
// Check if comments are open for this post
if ( ! comments_open( $post_id ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Comments are closed for this listing.', 'adirectory' ) ] );
}
// Prepare comment data
$comment_data = [
'comment_post_ID' => $post_id,
'comment_parent' => $parent_comment_id,
'comment_content' => $reply_text,
'comment_type' => 'comment',
'comment_approved' => 1, // Auto-approve replies for now
];
// Set author information
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$comment_data['user_id'] = $current_user->ID;
$comment_data['comment_author'] = $current_user->display_name;
$comment_data['comment_author_email'] = $current_user->user_email;
} else {
// For guest users, we need to get name and email from the form
$comment_data['comment_author'] = isset( $_POST['reply_author'] ) ? sanitize_text_field( $_POST['reply_author'] ) : esc_html__( 'Guest', 'adirectory' );
$comment_data['comment_author_email'] = isset( $_POST['reply_email'] ) ? sanitize_email( $_POST['reply_email'] ) : '';
}
// Insert the reply
$reply_id = wp_insert_comment( $comment_data );
if ( is_wp_error( $reply_id ) ) {
wp_send_json_error( [ 'message' => esc_html__( 'Failed to submit reply.', 'adirectory' ) ] );
}
// Get the reply object for display
$reply = get_comment( $reply_id );
// Send email notification for new reply
$this->send_reply_notification( $reply, $parent_comment, $post_id );
// Generate HTML for the new reply
ob_start();
?>
esc_html__( 'Reply submitted successfully.', 'adirectory' ),
'reply_html' => $reply_html,
'reply_id' => $reply_id,
'parent_comment_id' => $parent_comment_id,
]
);
}
/**
* Send email notification for new review
*
* @param \WP_Comment|\WP_Error $comment The comment object
* @param int $post_id The post ID
* @return void
*/
private function send_review_notification( $comment, $post_id ) {
// Check if comment is valid
if ( is_wp_error( $comment ) || ! $comment || ! is_object( $comment ) ) {
return;
}
// Get the post author email
$post_author_id = get_post_field( 'post_author', $post_id );
$post_author_email = get_the_author_meta( 'user_email', $post_author_id );
// Get listing details
$listing_title = get_the_title( $post_id );
$listing_url = get_permalink( $post_id );
// Get review details
$reviewer_name = $comment->comment_author;
$review_content = $comment->comment_content;
$review_rating = get_comment_meta( $comment->comment_ID, 'adqs_review_rating', true );
// Prepare email data
$email_data = [
'listing_title' => $listing_title,
'listing_url' => $listing_url,
'reviewer_name' => $reviewer_name,
'review_content' => $review_content,
'review_rating' => $review_rating,
'listing_author_email' => $post_author_email,
];
// Send email to listing author
$email_sender = new \ADQS_Directory\EmailSender();
$email_sender->send_mail( 'new_review_submitted', 'user', $email_data );
// Send email to admin
$email_sender->send_mail( 'new_review_submitted', 'admin', $email_data );
}
/**
* Send email notification for new reply
*
* @param WP_Comment|array|null $reply The reply comment object
* @param WP_Comment|array|null $parent_comment The parent comment object
* @param int $post_id The post ID
* @return void
*/
private function send_reply_notification( $reply, $parent_comment, $post_id ) {
// Check if reply and parent comment are valid
if ( ! $reply || ! $parent_comment || ! is_object( $reply ) || ! is_object( $parent_comment ) ) {
return;
}
// Get the post author email
$post_author_id = get_post_field( 'post_author', $post_id );
$post_author_email = get_the_author_meta( 'user_email', $post_author_id );
// Get listing details
$listing_title = get_the_title( $post_id );
$listing_url = get_permalink( $post_id );
// Get reply details
$reply_author_name = $reply->comment_author;
$reply_content = $reply->comment_content;
// Get original reviewer details
$original_reviewer_name = $parent_comment->comment_author;
// Prepare email data
$email_data = [
'listing_title' => $listing_title,
'listing_url' => $listing_url,
'reply_author_name' => $reply_author_name,
'reply_content' => $reply_content,
'original_reviewer_name' => $original_reviewer_name,
'listing_author_email' => $post_author_email,
];
// Send email to listing author
$email_sender = new \ADQS_Directory\EmailSender();
$email_sender->send_mail( 'new_reply_submitted', 'user', $email_data );
// Send email to admin
$email_sender->send_mail( 'new_reply_submitted', 'admin', $email_data );
}
/**
* Get directory types for user dashboard filter
*
* @return void
*/
public function adqs_get_directory_types() {
if ( ! check_ajax_referer( '__qs_directory_userdash', 'security' ) ) {
wp_send_json_error( [ 'message' => 'Nonce verification failed' ] );
}
$directories = adqs_get_directories();
if ( empty( $directories ) ) {
wp_send_json_success( [] );
}
$directory_data = array_map(
function ( $directory ) {
$dir_type_icon = get_term_meta( $directory->term_id, 'adqs_directory_icon', true );
$dir_type_img = get_term_meta( $directory->term_id, 'adqs_directory_img', true );
return [
'id' => $directory->term_id,
'name' => $directory->name,
'slug' => $directory->slug,
'icon' => $dir_type_icon,
'image' => $dir_type_img,
];
},
$directories
);
wp_send_json_success( $directory_data );
}
}