Custom Property Import Script
-
I’m helping out a client with their site. They are having issues with a script which is responsible (in conjunction with a CRON job) for importing latest properties.
The issue they’re having is that the script is re-importing every image multiple times, what it should do is:
check database for new properties
get any live properties and content related to them
delete any dormant properties and their related content (eg: if a property sold)This is the script which is meant to do this:
<?php function import_properties() { if ($_SERVER['REQUEST_URI'] == '/import-properties' || $_SERVER['REQUEST_URI'] == '/import-properties?XDEBUG_SESSION_START=netbeans-xdebug' || $_SERVER['REQUEST_URI'] == "/site-previews/hmproperty/import-properties") { $addedProperties = importProperties(); removeOldProperties($addedProperties); exit(0); } } add_action('init', 'import_properties'); function getProperties() { $ch = curl_init(DATA_SRC_URL); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-type: application/json')); curl_setopt_array($ch, $options); $result = curl_exec($ch); $result = json_decode($result); return $result->properties; } function setTitle($address) { return implode(", ", array_filter(array( $address->line_1, $address->line_2, $address->town_city, $address->county_state_province, $address->zip_postcode ), 'filter_empty_address_line' ) ); } function filter_empty_address_line($line) { return (!(empty($line))); } function createPage($data) { $page['post_type'] = 'property'; $page['post_content'] = $data->description; $page['post_parent'] = false; $page['post_author'] = "davids"; $page['post_status'] = 'publish'; $page['post_title'] = $data->title . " (" . $data->internal_ref . ")"; $postID = wp_insert_post($page); $ignore = array("leven_photos", "leven_floorplans", "leven_brochures", "leven_description", "leven_title"); foreach ($data as $k => $v) { $k = "leven_" . $k; if (!(in_array($k, $ignore))) { if (is_array($v) || is_object($v)) { $multi = ""; if ($k == "leven_address") { foreach ($v as $address_line => $line) { add_post_meta($postID, "leven_address_" . $address_line, $line); } add_post_meta($postID, "leven_address_full", html_entity_decode($data->title, ENT_QUOTES)); //used for search } else { foreach ($v as $complex_meta) { if (is_object($complex_meta)) { $multi .= $complex_meta->description . ","; } else { $multi = $complex_meta; } } add_post_meta($postID, $k, rtrim($multi, ",")); } } else { add_post_meta($postID, $k, $v); } } } setPrice($data, $postID); //check price is monthly and set if not. setGallery($data, $postID); setFloorplan($data, $postID); setBrochure($data, $postID); return $postID; } function delete_post_children($post_id) { global $wpdb; $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'"); foreach ($ids as $id) { wp_delete_attachment($id, true); unlink(get_attached_file($id)); } wp_delete_attachment(get_post_meta($post_id, 'leven_floorplan_attachment_id', true), true); wp_delete_attachment(get_post_meta($post_id, 'leven_epc_attachment_id', true), true); } function setGallery($data, $postID) { $c = 0; foreach ($data->photos as $image) { if ($image->epc > 0) { setEPC($image, $postID); } else { $image = getRemoteMedia($image->title, $image->url); $attach_id = setImage($image, $postID); if ($c == 0) add_post_meta($postID, '_thumbnail_id', $attach_id, true); $c++; } } } function setEPC($image, $postID) { $image = getRemoteMedia($image->title, $image->url); add_post_meta($postID, 'leven_epc_attachment_id', setImage($image, "EPC Graph", "exclude")); } function setFloorplan($data, $postID) { foreach ($data->floorplans as $image) { $image = getRemoteMedia($image->title, $image->url); add_post_meta($postID, 'leven_floorplan_attachment_id', setImage($image, "Floorplan", "exclude"), true); } } function setBrochure($data, $postID) { foreach ($data->brochures as $pdf) { $doc = getRemoteMedia($pdf->title, $pdf->url, $postID, false); add_post_meta($postID, 'leven_brochure_attachment_url', $postID . "/" . $pdf->title); } } function getRemoteMedia($name, $url, $postID = false, $isImage = true) { $name = ($name == "" ? rand(1000, 9999) : $name); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $result = curl_exec($ch); preg_match("/\.[a-zA-Z]{3,4}$/", $url, $ext); if ($isImage) { $fullpath = get_stylesheet_directory() . DS . 'temp' . DS . basename($name) . $ext[0]; } else { $fullpath = wp_upload_dir(); $fpath = $fullpath['basedir'] . DS . "brochures" . DS . $postID; if (!(file_exists($fpath))) { mkdir($fpath); } $fullpath = $fpath . DS . $name; } if (file_exists($fullpath)) { unlink($fullpath); } $fp = fopen($fullpath, 'w'); fwrite($fp, $result); fclose($fp); return $fullpath; } function setImage($filename, $property_id, $title = false, $caption = false) { require_once(ABSPATH . '/wp-admin/includes/file.php'); require_once(ABSPATH . '/wp-admin/includes/media.php'); require_once(ABSPATH . '/wp-admin/includes/image.php'); $file = array( 'name' => basename($filename), 'type' => 'image/jpeg', 'tmp_name' => $filename, 'error' => 0, 'size' => filesize($filename) ); return media_handle_sideload($file, $property_id); } function setPrice($data, $post_id) { $price = (float)(intval($data->price)); $price_period = $data->rent_type->description; $prefix = ""; if (strtolower($price_period) == 'weekly') { add_post_meta($post_id, $prefix . 'leven_price_weekly', $price); // convert from per week to per month, 52 / 12 = 4.3(3) // round to the nearest whole pound $price = intval(ceil(($price * 4.33) / 1.0) * 1.0); $price_period = 'monthly'; update_post_meta($post_id, $prefix . 'leven_price', $price); update_post_meta($post_id, $prefix . 'leven_rent_type', $price_period); } else { //convert monthly to weekly update_post_meta($post_id, $prefix . 'leven_price', $price); update_post_meta($post_id, $prefix . 'leven_rent_type', $price_period); // convert from per month to per week 12/52 = 0.230769231 // round to the nearest 50 $price = intval(ceil(($price * 0.230769231) / 1.0) * 1.0); add_post_meta($post_id, $prefix . 'leven_price_weekly', $price); } } function importProperties() { if (!($properties = getProperties())) { die('Feed offline?'); } $tracker = array(); //tracks what post ID's have been added/modified. foreach ($properties as $property) { set_time_limit(1800); $property->title = setTitle($property->address); $existingPage = get_page_by_title($property->title . " (" . $property->internal_ref . ")", OBJECT_K, 'property'); if (!(null == $existingPage)) { delete_post_children($existingPage->ID); wp_delete_post($existingPage->ID, true); } $tracker[] = createPage($property); } return $tracker; } function removeOldProperties($ignoreList) { global $wpdb; $ignoreList = implode(",", $ignoreList); $q = "DELETE FROM " . $wpdb->prefix . "posts WHERE post_type = 'property' AND id NOT IN({$ignoreList});"; $wpdb->query($q); $q = " DELETE FROM " . $wpdb->prefix . "postmeta WHERE post_id NOT IN (SELECT id FROM " . $wpdb->prefix . "posts);"; $wpdb->query($q); } ?>
[Moderator Note: No bumping. If it’s that urgent, consider hiring someone instead.]
- The topic ‘Custom Property Import Script’ is closed to new replies.