cookie_handler = CookiesHandler::getInstance();
// Listing Archive Query Customize.
add_action( 'pre_get_posts', [ $this, 'listing_archive_query_customize' ] );
// Save the meta data.
add_action( 'wp', [ $this, 'save_meta_data' ] );
// Listing Review Hooked
$this->comment_review_hooked();
add_filter( 'get_avatar', [ $this, 'customize_default_avatar' ], 10, 5 );
add_action( 'adqs_tax_lists', [ $this, 'tax_lists_html' ], 10, 3 );
add_action( 'adqs_ad_search_tax', [ $this, 'ad_search_tax' ] );
add_action( 'wp_head', [ $this, 'schema_markup' ] );
add_action( 'pre_user_query', [ $this, 'order_users_by_post_count' ] );
}
/**
* listing archive query customize
*
* @param \WP_Query $query
* @return void
*/
public function listing_archive_query_customize( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
$post_types = adqs_get_directory_post_types();
$is_post_type_archive = ! empty( $post_types ) ? $query->is_post_type_archive( $post_types ) : false;
$taxonomies = adqs_get_directory_taxonomies();
$is_taxonomy_archive = ! empty( $taxonomies ) ? $query->is_tax( $taxonomies ) : false;
if ( $is_post_type_archive || $is_taxonomy_archive || $query->is_author ) {
$search = ! empty( $_GET['ls'] ) ? sanitize_text_field( $_GET['ls'] ) : '';
// Handle author pages (including search on author pages)
if ( $query->is_author ) {
// Check if this is a directory listing request
$is_directory_request = false;
// Check if URL contains 'directory' - this is the primary indicator
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], '/directory' ) !== false ) {
$is_directory_request = true;
}
// Only check for 'listings' parameter if URL doesn't already contain 'directory'
elseif ( ! empty( $_GET['listings'] ) && $_GET['listings'] === 'yes' ) {
$is_directory_request = true;
}
if ( $is_directory_request ) {
// For directory author pages, show all dynamic post types
if ( ! empty( $post_types ) ) {
$query->set( 'post_type', $post_types );
}
} else {
// For normal author pages, don't modify the query - let WordPress handle it
// This will show regular posts, pages, etc.
return;
}
}
if ( ! empty( $search ) ) {
$query->set( 's', $search );
}
$query->set( 'posts_per_page', apply_filters( 'adqs_listing_archive_per_page', 6 ) );
/**
* Short By
*/
if ( ! empty( adqs_listing_query_sort_by() ) ) {
$sortByArgs = adqs_listing_query_sort_by();
if ( isset( $sortByArgs['meta_key'] ) && ! empty( $sortByArgs['meta_key'] ) ) {
$query->set( 'meta_key', $sortByArgs['meta_key'] );
}
if ( isset( $sortByArgs['orderby'] ) && ! empty( $sortByArgs['orderby'] ) ) {
$query->set( 'orderby', $sortByArgs['orderby'] );
}
if ( isset( $sortByArgs['order'] ) && ! empty( $sortByArgs['order'] ) ) {
$query->set( 'order', $sortByArgs['order'] );
}
}
}
}
/**
* get post average ratings
*
* @param [type] $id
* @return void
*/
public function save_meta_data() {
// Check if we're on a singular page
if ( ! is_singular() ) {
return;
}
// Get current post type
$current_post_type = get_post_type();
// Dynamically check if current post type is a dynamic post type
$dynamic_post_types = adqs_get_directory_post_types();
$is_dynamic_post_type = in_array( $current_post_type, $dynamic_post_types );
if ( ! $is_dynamic_post_type ) {
return;
}
$this->save_views_count( get_the_ID() );
// Only update ratings if review system is enabled
$enable_review = AD()->Helper->get_setting( 'enable_review' );
if ( ! empty( $enable_review ) ) {
$this->update_avg_ratings( get_the_ID() );
}
}
/**
* Update View Count By IP Address
*
* @param [type] $post_id
* @return void
*/
public function save_views_count( $postID = null ) {
// Initialize $postID if not provided
$postID = ! empty( $postID ) ? $postID : get_the_ID();
if ( empty( $postID ) ) {
return; // Exit if no post ID is available
}
// Retrieve view_ids from cookie, or initialize an empty array
$view_ids = $this->cookie_handler->has( 'view_counted' ) ? $this->cookie_handler->get( 'view_counted' ) : [];
// If the post ID is already in the cookie, do nothing
if ( in_array( $postID, ( $view_ids ?? [] ) ) ) {
return;
}
$count_key = '_view_count';
$count = absint( get_post_meta( $postID, $count_key, true ) );
// Increment the count (whether it's 0 or existing)
++$count;
update_post_meta( $postID, $count_key, $count );
// Add the post ID to the view_ids and save it to the cookie
$view_ids[] = $postID;
$this->cookie_handler->set( 'view_counted', $view_ids, 86400 ); // 24 hours
}
/**
* customize default avatar() {
*
* @return string
*/
public function customize_default_avatar( $avatar, $id_or_email, $size, $default_value, $alt ) {
$userId = is_object( $id_or_email ) ? $id_or_email->user_id : $id_or_email;
if ( ! empty( get_user_meta( $userId ?? 0, 'profile_picture', true ) ) ) {
return '';
}
return $avatar;
}
// ajax search html
public function tax_lists_html( $taxonomy = null, $saved_data = [], $directory_context = [] ) {
// If no taxonomy provided, get the category taxonomy for the current post type
if ( empty( $taxonomy ) ) {
$current_post_type = get_post_type();
$taxonomy = adqs_get_taxonomy_for_post_type( $current_post_type, 'categories' );
if ( empty( $taxonomy ) ) {
return ''; // Fallback
}
}
// Check if the taxonomy exists, if not return empty
if ( ! taxonomy_exists( $taxonomy ) ) {
return '';
}
$taxClass = ( strpos( $taxonomy, 'categories' ) !== false || strpos( $taxonomy, 'category' ) !== false ) ? 'categories' : 'locations';
?>