profile_url = $profile_url; $this->scrapper = new LFZW_Fr_Scrapper(); $this->encoded_zuid = get_option('lfzw_encoded_zuid'); } /** * Listings getter method * Fetches and formats all listings. * * @since 1.0.0 * @return array All listings */ public function get_listings() { $listings = $this->fetch_active_listings(); if(!empty($listings)) { return $this->format_listings($listings); } else { throw new \Exception( "Listings not found.", 404 ); } } /** * Utility method for imagee. * * @since 1.0.0 * @return string url */ function convert_zillow_image_url($url, $newSuffix = 'cc_ft_576') { $parts = explode('-', $url); if (count($parts) > 1) { $base = $parts[0]; return $base . '-' . $newSuffix . '.webp'; } return $url; } /** * This method converts all listings data into a fixed format accepted by listings template ensuring template remains independent of fetched data and logic * * @since 1.0.0 * @param array $listings * @return array All formatted listings */ private function format_listings( $listings ) { $formatted_listings = array(); foreach ( $listings as $listing ) { $list_id = isset( $listing['zpid'] ) ? sanitize_text_field( $listing['zpid'] ) : ''; $list_ttl = $list_adrs = $list_city = $list_state = $list_zip = ''; if ( isset( $listing['address'] ) ) { $address = $listing['address']; $list_ttl = isset( $address['line1'] ) ? sanitize_text_field( $address['line1'] ) : ''; $list_adrs = isset( $address['line2'] ) ? sanitize_text_field( $address['line2'] ) : ''; $list_city = isset( $address['city'] ) ? sanitize_text_field( $address['city'] ) : ''; $list_state = isset( $address['stateOrProvince'] ) ? sanitize_text_field( $address['stateOrProvince'] ) : ''; $list_zip = isset( $address['postalCode'] ) ? sanitize_text_field( $address['postalCode'] ) : ''; } $list_beds = isset( $listing['bedrooms'] ) ? absint( $listing['bedrooms'] ) : ''; $list_baths = isset( $listing['bathrooms'] ) ? absint( $listing['bathrooms'] ) : ''; $list_img = isset( $listing['primary_photo_url'] ) ? esc_url_raw( $this->convert_zillow_image_url( $listing['primary_photo_url'] ) ) : ''; $list_lat = isset( $listing['latitude'] ) ? floatval( $listing['latitude'] ) : ''; $list_long = isset( $listing['longitude'] ) ? floatval( $listing['longitude'] ) : ''; $list_url = isset( $listing['listing_url'] ) ? $listing['listing_url'] : ''; if ( $list_url && strpos( $list_url, 'http' ) !== 0 ) { $list_url = 'https://www.zillow.com' . $list_url; } $list_url = esc_url_raw( $list_url ); $list_price = isset( $listing['price'] ) ? sanitize_text_field( $listing['price'] ) : ''; $data = array( 'list_id' => $list_id, 'title' => $list_ttl, 'address' => $list_adrs, 'city' => $list_city, 'state' => $list_state, 'zip' => $list_zip, 'beds' => $list_beds, 'baths' => $list_baths, 'image' => $list_img, 'lat' => $list_lat, 'long' => $list_long, 'list_url' => $list_url, 'price' => $list_price, ); $formatted_listings[] = $data; } return $formatted_listings; } /** * Fetch and return active listings for the specified Zillow user. * * @since 1.0.0 * @return void */ private function fetch_active_listings( ) { if(!$this->encoded_zuid) { throw new \Exception( "Error getting the listings. Please contact the site owner." ); } $page = 1; $all_listings = array(); $listings_url = "https://private-zillow.p.rapidapi.com/agent/for_sale?encodedZuid={$this->encoded_zuid}&page={$page}"; try { $response = $this->scrapper->make_request( $listings_url ); $data = json_decode( $response, true ); $page_listings = isset( $data['forSaleListings'] ) && is_array( $data['forSaleListings'] ) ? $data['forSaleListings'] : []; if ( count( $page_listings ) > 0 ) { $all_listings = array_merge( $all_listings, $page_listings ); if (count($all_listings) > 3) { $all_listings = array_slice($all_listings, 0, 3); } } } catch ( Exception $e ) { error_log( "Error fetching page $page: " . $e->getMessage() ); } return $all_listings; } }