get_listings(); $list_ids = []; if (!empty($all_active_listings) && is_array($all_active_listings)) { global $wpdb; $listings_table_name = $wpdb->prefix . 'lfzw_listings'; $details_table_name = $wpdb->prefix . 'lfzw_details'; $images_table_name = $wpdb->prefix . 'lfzw_images'; $wpdb->query('SET foreign_key_checks = 0'); $wpdb->query("TRUNCATE TABLE {$images_table_name}"); $wpdb->query("TRUNCATE TABLE {$details_table_name}"); $wpdb->query("TRUNCATE TABLE {$listings_table_name}"); $wpdb->query('SET foreign_key_checks = 1'); // Save all the listing foreach ($all_active_listings as $listing) { $list_ids[] = $listing['list_id']; try { $wpdb->insert($listings_table_name, [ 'list_id' => $listing['list_id'], 'title' => $listing['title'], 'address' => $listing['address'], 'city' => $listing['city'], 'state' => $listing['state'], 'zip' => $listing['zip'], 'beds' => $listing['beds'], 'baths' => $listing['baths'], 'image' => $listing['image'], 'latitude' => $listing['lat'], 'longitude' => $listing['long'], 'list_url' => $listing['list_url'], 'price' => $listing['price'], 'details' => json_encode([]) ]); } catch ( Exception $e ) { error_log( "Error saving listing -" . $listing['list_id'] . ": " . $e->getMessage() ); } } } else { throw new \Exception("No listings found!", 404); } return $list_ids; } /** * Fetches the listings details and saves in DB * * @since 1.0.0 * @return void */ public static function save_listing_details($list_id) { require_once LFZW_Fr_Main::$plugin_dir . 'includes/core/class-lfzw-fr-details.php'; try { global $wpdb; $details_table_name = $wpdb->prefix . 'lfzw_details'; $images_table_name = $wpdb->prefix . 'lfzw_images'; if($list_id) { // Save the details $details_scrapper = new LFZW_Fr_Details($list_id); $details = $details_scrapper->get_data(); $wpdb->insert($details_table_name, [ 'list_id' => $list_id, 'about' => $details['desc'], 'area' => $details['area'], 'details_url' => $details['details_url'], 'contact_info' => json_encode($details['contact_info']), 'metadata' => json_encode($details['metadata']) ]); // Save the images if (!empty($details['images'])) { foreach ($details['images'] as $image_url) { $wpdb->insert( $images_table_name, [ 'list_id' => $list_id, 'image_url' => $image_url, 'image_alt' => $details['address'] ], ); } } } else { throw new \Exception("Listing ID is missing!", 400); } } catch ( Exception $e ) { error_log( "Error saving details -" . $list_id . ": " . $e->getMessage() ); } } }