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 '' . esc_attr( $alt ) . ''; } 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'; ?>
ID ) ); $tagline = esc_html( get_post_meta( $post->ID, '_tagline', true ) ); $address = esc_html( get_post_meta( $post->ID, '_address', true ) ); $zip = esc_html( get_post_meta( $post->ID, '_zip', true ) ); $latitude = esc_attr( get_post_meta( $post->ID, '_map_lat', true ) ); $longitude = esc_attr( get_post_meta( $post->ID, '_map_lon', true ) ); $phone = esc_html( get_post_meta( $post->ID, '_phone', true ) ); $website = esc_url( get_post_meta( $post->ID, '_website', true ) ); $fax = esc_html( get_post_meta( $post->ID, '_fax', true ) ); $email = esc_html( get_post_meta( $post->ID, '_email', true ) ) ?: esc_html( get_the_author_meta( 'user_email' ) ); $author_name = esc_html( get_the_author_meta( 'display_name', $post->post_author ) ); $description = esc_html( get_the_excerpt( $post->ID ) ) ?: wp_trim_words( esc_html( strip_tags( $post->post_content ) ), 30 ); $date_published = esc_html( get_the_date( 'c', $post->ID ) ); // ISO 8601 format $date_modified = esc_html( get_the_modified_date( 'c', $post->ID ) ); // ISO 8601 format // Only get rating data if review system is enabled $enable_review = AD()->Helper->get_setting( 'enable_review' ); $rating_value = 0; $rating_count = 0; if ( ! empty( $enable_review ) ) { $rating_value = AD()->Helper->get_post_average_ratings( $post->ID ) ?: 0; $rating_count = adqs_get_parent_reviews_count( $post->ID ) ?: 0; } // Get featured image if available $image_url = has_post_thumbnail( $post->ID ) ? esc_url( get_the_post_thumbnail_url( $post->ID, 'full' ) ) : null; // Build the schema JSON array $schemaJson = [ '@context' => 'https://schema.org', '@type' => 'LocalBusiness', 'name' => $headline, ]; // Optional fields based on the selected schema options if ( in_array( 'headline', $options ) ) { $schemaJson['headline'] = $headline; } if ( in_array( 'tagline', $options ) && ! empty( $tagline ) ) { $schemaJson['tagline'] = $tagline; } if ( in_array( 'description', $options ) && ! empty( $description ) ) { $schemaJson['description'] = $description; } if ( in_array( 'image', $options ) && ! empty( $image_url ) ) { $schemaJson['image'] = $image_url; } // Address and postal code if ( in_array( 'address', $options ) && ( ! empty( $address ) || ! empty( $zip ) ) ) { $schemaJson['address'] = [ '@type' => 'PostalAddress', ]; if ( ! empty( $address ) ) { $schemaJson['address']['streetAddress'] = $address; } if ( ! empty( $zip ) ) { $schemaJson['address']['postalCode'] = $zip; } } // Geolocation if ( in_array( 'geo', $options ) && ! empty( $latitude ) && ! empty( $longitude ) ) { $schemaJson['geo'] = [ '@type' => 'GeoCoordinates', 'latitude' => floatval( $latitude ), 'longitude' => floatval( $longitude ), ]; } // Phone and fax if ( in_array( 'telephone', $options ) && ! empty( $phone ) ) { $schemaJson['telephone'] = $phone; } if ( in_array( 'fax', $options ) && ! empty( $fax ) ) { $schemaJson['faxNumber'] = $fax; } // Website and email if ( in_array( 'website', $options ) && ! empty( $website ) ) { $schemaJson['url'] = $website; } if ( in_array( 'email', $options ) && ! empty( $email ) ) { $schemaJson['email'] = $email; } // Author information if ( in_array( 'author', $options ) && ! empty( $author_name ) ) { $schemaJson['author'] = [ '@type' => 'Person', 'name' => $author_name, ]; } // Ratings information if ( in_array( 'rating', $options ) && $rating_value > 0 && $rating_count > 0 ) { $schemaJson['aggregateRating'] = [ '@type' => 'AggregateRating', 'ratingValue' => floatval( $rating_value ), 'reviewCount' => intval( $rating_count ), ]; } // Date information if ( in_array( 'date', $options ) ) { if ( ! empty( $date_published ) ) { $schemaJson['datePublished'] = $date_published; } if ( ! empty( $date_modified ) ) { $schemaJson['dateModified'] = $date_modified; } } // Output the structured data securely ?> query_vars['orderby'] ) && $query->query_vars['orderby'] === 'ad_listing_count' ) { $dynamic_post_types = adqs_get_directory_post_types(); if ( empty( $dynamic_post_types ) ) { return; } // Sanitize order direction (ASC or DESC only) $order = strtoupper( $query->query_vars['order'] ?? 'DESC' ); if ( ! in_array( $order, [ 'ASC', 'DESC' ], true ) ) { $order = 'DESC'; } // Build safe post types placeholders $post_types_placeholders = implode( ',', array_fill( 0, count( $dynamic_post_types ), '%s' ) ); // Prepare the subquery safely $subquery = $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_author = {$wpdb->users}.ID AND {$wpdb->posts}.post_type IN ($post_types_placeholders) AND {$wpdb->posts}.post_status = 'publish'", $dynamic_post_types ); $query->query_orderby = 'ORDER BY post_count ' . $order; $query->query_fields .= ", ($subquery) AS post_count"; } } }