'; var_dump( $data ); echo ''; } /** * check data * * @param [type] $data * @param [type] $key * @return bool */ public function check_data( $data, $key ) { return ( isset( $data[ $key ] ) && ! empty( $data[ $key ] ) ) ? true : false; } /** * Required input attributes * * @param [type] $data * @return void */ public function required( $data ) { echo $this->check_data( $data, 'is_required' ) ? 'required="required"' : ''; } public function required_html( $data ) { echo $this->check_data( $data, 'is_required' ) ? ' *' : ''; } /** * is admin only */ public function admin_view( $data ) { return $this->check_data( $data, 'admin_view' ) ? current_user_can( 'manage_options' ) : true; } /** * is hidden field */ public function is_hidden( $data ) { return $this->check_data( $data, 'is_hidden' ) ? $data['is_hidden'] : false; } /** * Get Data */ public function get_data( $data = [], $key = '', $default = '' ) { return $this->check_data( $data, $key ) ? $data[ $key ] : $default; } /** * Meta Value for adding */ public function meta_val( $post_id = 0, $meta_key = '', $default = '' ) { $value = ! empty( get_post_meta( $post_id, $meta_key, true ) ) ? get_post_meta( $post_id, $meta_key, true ) : $default; return apply_filters( 'adqs_helper_listing_metadata', $value, $post_id, $meta_key ); } /** * Meta Post Data */ public function post_data( $post = [], $meta_key = '', $default = '' ) { return isset( $post[ $meta_key ] ) ? $post[ $meta_key ] : $default; } /** * Get File name From Folder */ public function file_name_from_folder( $path = '', $ext = 'php' ) { $filenames = glob( $path ); $filenames = array_map( function ( $path ) use ( $ext ) { return basename( $path, ".{$ext}" ); }, $filenames ); return $filenames; } /** * Text to slug generate * * @param [type] $str * @param string $delimiter * @return string */ public function text_to_slug( $str, $delimiter = '-' ) { $slug = strtolower( trim( preg_replace( '/[\s-]+/', $delimiter, preg_replace( '/[^A-Za-z0-9-]+/', $delimiter, preg_replace( '/[&]/', 'and', preg_replace( '/[\']/', '', iconv( 'UTF-8', 'ASCII//TRANSLIT', $str ) ) ) ) ), $delimiter ) ); return $slug; } /** * determine Video Url Type * * @param [type] $url * @return array */ public function determineVideoUrlType( $url ) { if ( empty( $url ) ) { return [ 'video_type' => 'none', 'video_id' => '', 'file_ext' => '', ]; } // YouTube Regex $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/'; $has_match_youtube = preg_match( $yt_rx, $url, $yt_matches ); // Vimeo Regex $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/'; $has_match_vimeo = preg_match( $vm_rx, $url, $vm_matches ); // WordPress supported video extensions $supported_ext = [ 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv' ]; // Extract extension $ext = strtolower( pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_EXTENSION ) ); // Check direct file if ( in_array( $ext, $supported_ext, true ) ) { return [ 'video_type' => 'file', 'video_id' => '', 'file_ext' => $ext, ]; } // YouTube match if ( $has_match_youtube ) { return [ 'video_type' => 'youtube', 'video_id' => $yt_matches[5] ?? '', 'file_ext' => '', ]; } // Vimeo match if ( $has_match_vimeo ) { return [ 'video_type' => 'vimeo', 'video_id' => $vm_matches[5] ?? '', 'file_ext' => '', ]; } // Not supported return [ 'video_type' => 'none', 'video_id' => '', 'file_ext' => '', ]; } /** * Get review rating * * @return $maxPrice */ public function get_author_ratings( $author_id = 0 ) { // Check if review system is enabled and author_id is valid $enable_review = $this->get_setting( 'enable_review' ); if ( empty( $enable_review ) || empty( $author_id ) ) { return 0; } global $wpdb; $cache_key = "adqs_authlrRatings_{$author_id}"; $authlrRatings = wp_cache_get( $cache_key, 'adqs_cache' ); if ( $authlrRatings === false ) { // Get all dynamic post types $dynamic_post_types = adqs_get_directory_post_types(); if ( ! empty( $dynamic_post_types ) ) { $post_types_placeholders = implode( ',', array_fill( 0, count( $dynamic_post_types ), '%s' ) ); // Calculate average rating from actual comment ratings $query = $wpdb->prepare( "SELECT AVG(CAST(cm.meta_value AS DECIMAL(3,1))) FROM $wpdb->comments c INNER JOIN $wpdb->commentmeta cm ON c.comment_ID = cm.comment_id INNER JOIN $wpdb->posts p ON c.comment_post_ID = p.ID WHERE cm.meta_key = 'adqs_review_rating' AND cm.meta_value > 0 AND c.comment_approved = '1' AND c.comment_parent = 0 AND p.post_type IN ($post_types_placeholders) AND p.post_status = 'publish' AND p.post_author = %d", array_merge( $dynamic_post_types, [ $author_id ] ) ); $authlrRatings = $wpdb->get_var( $query ); } else { $authlrRatings = 0; } wp_cache_set( $cache_key, $authlrRatings, 'adqs_cache' ); } $authlrRatings = $authlrRatings ? round( $authlrRatings, 1 ) : 0; $current_rating = get_user_meta( $author_id, 'adqs_agent_rating', true ); if ( $authlrRatings !== $current_rating ) { update_user_meta( $author_id, 'adqs_agent_rating', $authlrRatings ); } return $authlrRatings; } /** * Get review count * * @return $maxPrice */ public function get_author_review_count( $author_id = 0 ) { // Check if review system is enabled and author_id is valid $enable_review = $this->get_setting( 'enable_review' ); if ( empty( $enable_review ) || empty( $author_id ) ) { return 0; } global $wpdb; $authlrReviewCount = wp_cache_get( "adqs_authlrReviewCount_{$author_id}", 'adqs_cache' ); if ( $authlrReviewCount === false ) { // Get all dynamic post types $dynamic_post_types = adqs_get_directory_post_types(); if ( ! empty( $dynamic_post_types ) ) { $post_types_placeholders = implode( ',', array_fill( 0, count( $dynamic_post_types ), '%s' ) ); $query = $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID IN(SELECT ID FROM $wpdb->posts WHERE post_type IN ($post_types_placeholders) AND post_status='publish' AND post_author=%d) AND comment_parent = 0 AND comment_approved = '1'", array_merge( $dynamic_post_types, [ $author_id ] ) ); $authlrReviewCount = $wpdb->get_var( $query ); } else { $authlrReviewCount = 0; } wp_cache_set( "adqs_authlrReviewCount_{$author_id}", $authlrReviewCount, 'adqs_cache' ); } $authlrReviewCount = ! empty( $authlrReviewCount ) ? $authlrReviewCount : 0; $current_review = get_user_meta( $author_id, 'adqs_agent_review', true ); if ( $authlrReviewCount !== $current_review ) { update_user_meta( $author_id, 'adqs_agent_review', $authlrReviewCount ); } return $authlrReviewCount; } /** * Clear author review count cache * * @param int $author_id The author ID * @return void */ public function clear_author_review_cache( $author_id = 0 ) { if ( empty( $author_id ) ) { return; } wp_cache_delete( "adqs_authlrReviewCount_{$author_id}", 'adqs_cache' ); wp_cache_delete( "adqs_authlrRatings_{$author_id}", 'adqs_cache' ); // Also clear the user meta to force recalculation delete_user_meta( $author_id, 'adqs_agent_review' ); delete_user_meta( $author_id, 'adqs_agent_rating' ); // Force immediate recalculation $this->get_author_review_count( $author_id ); $this->get_author_ratings( $author_id ); } /** * Get Max Price * * @return $maxPrice */ public function get_maxPrice() { global $wpdb; $maxPrice = wp_cache_get( 'adqs_maxPrice', 'adqs_cache' ); if ( $maxPrice === false ) { // Get all dynamic post types $dynamic_post_types = adqs_get_directory_post_types(); if ( ! empty( $dynamic_post_types ) ) { $post_types_placeholders = implode( ',', array_fill( 0, count( $dynamic_post_types ), '%s' ) ); $maxPrice = $wpdb->get_var( $wpdb->prepare( "SELECT MAX(CAST(meta_value AS DECIMAL(10, 0))) AS max_numeric_price FROM {$wpdb->postmeta} WHERE meta_key = '_price' AND meta_value REGEXP '^[0-9]+\.?[0-9]*$' AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type IN ($post_types_placeholders) AND post_status='publish')", $dynamic_post_types ) ); } else { $maxPrice = 0; } wp_cache_set( 'adqs_maxPrice', $maxPrice, 'adqs_cache' ); } return ! empty( $maxPrice ) ? $maxPrice : false; } /** * Price * * @param [type] $post_id * @return string */ public function get_price( $post_id ) { $priceRangeTypes = apply_filters( 'adqs_meta_price_range', [ 'skimming' => esc_html__( 'High ($$$$)', 'adirectory' ), 'moderate' => esc_html__( 'Expensive ($$$)', 'adirectory' ), 'economy' => esc_html__( 'Moderate ($$)', 'adirectory' ), 'bellow_economy' => esc_html__( 'Cheap ($)', 'adirectory' ), ] ); $value_price = $this->meta_val( $post_id, '_price' ) ?? ''; $value_price_sub = $this->meta_val( $post_id, '_price_sub' ); $value_price_range = $this->meta_val( $post_id, '_price_range' ); $value_price_type = $this->meta_val( $post_id, '_price_type' ); if ( $value_price_type === '_price_range' ) { return $priceRangeTypes[ $value_price_range ] ?? ''; } else { $value_price = apply_filters( 'adqs_listing_price', $value_price ); $value_price_sub = ! empty( $value_price_sub ) ? "{$value_price_sub}" : ''; $price = adqs_get_price_ws( $value_price ); return join( ' ', array_filter( [ $price, $value_price_sub ] ) ); } } /** * Comment Review Html * * @param integer $ratings * @return void */ public function get_review_rating_html( $ratings = 0 ) { $ratings = ! empty( $ratings ) ? floatval( $ratings ) : 0; $stars = '
'; for ( $i = 1; $i <= 5; $i++ ) { if ( $i <= $ratings ) { $stars .= ''; } elseif ( $i - $ratings < 1 ) { $stars .= ''; } else { $stars .= ''; } } $stars .= '
'; echo wp_kses_post( $stars ); } /** * get post average ratings * * @param [type] $id * @return void */ public function get_post_average_ratings( $id = 0 ) { $avg_ratings = get_post_meta( $id, 'adqs_avg_ratings', true ); return ! empty( $avg_ratings ) ? $avg_ratings : 0; } /** * Post View Count * * @param [type] $post_id * @return int */ public function get_view_count( $post_id ) { $value = $this->meta_val( $post_id, '_view_count' ); return $value; } /** * Get pluck terms * * @param integer $post_id * @param string $taxonomy * @param boolean $column * @return array */ public function get_pluck_terms( $post_id = 0, $taxonomy = '', $field = '', $index_key = null ) { $post_terms = get_the_terms( $post_id, $taxonomy ); return wp_list_pluck( $post_terms, $field, $index_key ); } /** * Use to convert large positive numbers in to short form like 1K+, 100K+, 199K+, 1M+, 10M+, 1B+ etc * * @param $n * @return string */ public function get_number_short_format( $n = 0, $after = '+' ) { $n = ! empty( $n ) ? absint( $n ) : 0; if ( $n >= 0 && $n < 1000 ) { // 1 - 999 $n_format = floor( $n ); $suffix = ''; } elseif ( $n >= 1000 && $n < 1000000 ) { // 1k-999k $n_format = floor( $n / 1000 ); $suffix = "K{$after}"; } elseif ( $n >= 1000000 && $n < 1000000000 ) { // 1m-999m $n_format = floor( $n / 1000000 ); $suffix = "M{$after}"; } elseif ( $n >= 1000000000 && $n < 1000000000000 ) { // 1b-999b $n_format = floor( $n / 1000000000 ); $suffix = "B{$after}"; } elseif ( $n >= 1000000000000 ) { // 1t+ $n_format = floor( $n / 1000000000000 ); $suffix = "T{$after}"; } return ! empty( $n_format . $suffix ) ? $n_format . $suffix : 0; } /** * Get Settings Data * * @param string $key * @return string|boolean|array */ public function get_setting( $key = '' ) { $settings = get_option( 'adqs_admin_settings' ); return ( isset( $settings[ $key ] ) && ! empty( $settings[ $key ] ) ) ? $settings[ $key ] : ''; } }