Created
November 25, 2016 18:38
-
-
Save danboh/1311158a9510153aa3e19d28ed67c54f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace wptrebrets\inc; | |
class Save { | |
protected $post; | |
protected $photos; | |
protected $mls; | |
protected $upload_dir; | |
protected $feed; | |
protected $class; | |
/** | |
* Takes a Feed object from a completed search and saves it | |
* | |
* @param Feed $feed | |
*/ | |
public function __construct(Feed $feed) | |
{ | |
$this->upload_dir = wp_upload_dir(); | |
$this->feed = $feed; | |
$this->mls = $feed->mls; | |
$this->class = $feed->class; | |
self::posts($feed->get()); | |
} | |
/** | |
* Uses the filename and MLS number to generate a directory to save the files to, this returns the path | |
* | |
* @param array|object $property | |
* @return string | |
*/ | |
public function getDirectory($property) | |
{ | |
//Get the path to the upload directory and create it if it isn't there | |
$first_letter = substr($property, 0, 1); | |
$numeric = substr($property, 1); | |
$dir = $this->upload_dir['basedir'] . '/wptreb/' . $first_letter . '/' . $numeric; | |
if (!file_exists($dir)) { | |
mkdir($dir, 0777, true); | |
} | |
return $dir; | |
} | |
/** | |
* Uses the filename and MLS number to get the URL to the stored images | |
* | |
* @param array|object $property | |
* @return string | |
*/ | |
public function getDirectoryURL($property) | |
{ | |
//Get the URL to the upload directory | |
$first_letter = substr($property, 0, 1); | |
$numeric = substr($property, 1); | |
$upload_dir = wp_upload_dir(); | |
$dir = $upload_dir['url'] . '/wptreb/' . $first_letter . '/' . $numeric; | |
return $dir; | |
} | |
/** | |
* Loops through the images and stores them on the server - at the same time it links the images to the post so you | |
* later use them for a gallery/slideshow | |
* | |
* @param $photos | |
* @param $id | |
* @param $property | |
*/ | |
public function photos($photos, $id, $property) | |
{ | |
$all_photos = array(); | |
$dir = self::getDirectory($property); | |
$n = 1; | |
$first = true; | |
global $wpdb; | |
$no_photo_found_title = "no-photo-found"; | |
$no_photo_found = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $no_photo_found_title . "'" ); | |
//error_log(print_r($photos,true)); | |
if(count($photos)>0){ | |
//Here we get the photos and store them in directories separated by the first letter of the property | |
foreach ($photos as $key => $photo) { | |
if($photo->getError()){ | |
//No photo yet | |
if($no_photo_found) { | |
add_post_meta($id, 'REAL_HOMES_property_images', $no_photo_found); | |
add_post_meta($id, '_thumbnail_id', $no_photo_found); | |
} | |
}else{ | |
$filename = $property.'-'.$n.'.jpg'; | |
if (!file_exists($dir.'/'.$n.'.jpg')) { | |
file_put_contents($dir.'/'.$property.'-'.$n.'.jpg', $photo->getContent()); | |
self::addPhotoToWordPress($filename, $dir, $id, $property, $first); | |
$first = false; | |
} | |
$all_photos[$property][] = $dir.'/'. $filename; | |
$n++; | |
if($no_photo_found){ | |
delete_post_meta($id, 'REAL_HOMES_property_images', $no_photo_found); | |
} | |
} | |
} | |
}else{ | |
if($no_photo_found) { | |
add_post_meta($id, 'REAL_HOMES_property_images', $no_photo_found); | |
add_post_meta($id, '_thumbnail_id', $no_photo_found); | |
} | |
} | |
} | |
/** | |
* Saves the posts into WordPress | |
* | |
* @param array|object $post | |
*/ | |
public function posts($post) | |
{ | |
if (is_array($post)) { | |
//get post data | |
$property_formatted = array(); | |
//loop through properties found | |
foreach ($post as $property) { | |
//reassign fields to ones that look good | |
$property_formatted['REAL_HOMES_property_price'] = $property['Lp_dol']; | |
$property_formatted['REAL_HOMES_property_bedrooms'] = isset($property['Br']) ? $property['Br'] : ''; | |
$property_formatted['REAL_HOMES_property_bathrooms'] = $property['Bath_tot']; | |
$property_formatted['REAL_HOMES_property_garage'] = isset($property['Gar_spaces']) ? $property['Gar_spaces'] : ''; | |
$property_formatted['REAL_HOMES_property_id'] = $property['Ml_num']; | |
$property_formatted['REAL_HOMES_property_size'] = isset($property['Sqft']) ? $property['Sqft'] : (isset($property['Tot_area']) ? $property['Tot_area'] : ''); | |
$property_formatted['REAL_HOMES_property_size_postfix'] = 'Sq Ft'; | |
$property_formatted['REAL_HOMES_property_address'] = (isset($property['Apt_num']) ? ((strlen($property['Apt_num']) > 0) ? $property['Apt_num'] .'-' : ( isset($property['Unit_num']) ? ((strlen($property['Unit_num']) > 0) ? $property['Unit_num'].'-' : '' ) : '' ) ) : '') . $property['Addr'] . (isset($property['Municipality']) ? ', '.$property['Municipality'] : ''); | |
$property_formatted['REAL_HOMES_tour_video_url'] = $property['Tour_url']; | |
$property_formatted['REAL_HOMES_gallery_slider_type'] = 'thumb-on-bottom'; | |
$property_formatted['REAL_HOMES_property_map'] = '0'; | |
$property_formatted['REAL_HOMES_featured'] = '0'; | |
$property_formatted['REAL_HOMES_agents'] = get_page_by_title(wptrebrets_get_option('default_agent'),OBJECT,'agent')->ID; | |
if(isset($property['Zip'])){ | |
global $wpdb; | |
$latlong = $wpdb->get_results("SELECT * FROM postal_codes where postal_code = '". trim($property['Zip']) ."'"); | |
if(!empty($latlong)) { | |
$property_formatted['REAL_HOMES_property_location'] = $latlong[0]->postal_code_latitude. ',' . $latlong[0]->postal_code_longitude; | |
} | |
}else{ | |
global $wpdb; | |
$latlong = $wpdb->get_results("select * from city_postal_codes where city like '%". $property['Municipality'] ."%'"); | |
if(!empty($latlong)) { | |
$property_formatted['REAL_HOMES_property_location'] = $latlong[0]->latitude. ',' . $latlong[0]->longitude; | |
}else{ | |
$latlong = $wpdb->get_results("select * from city_postal_codes where city like '%". $property['Area'] ."%'"); | |
if(!empty($latlong)) { | |
$property_formatted['REAL_HOMES_property_location'] = $latlong[0]->latitude . ',' . $latlong[0]->longitude; | |
}else{ | |
//If no ZIP Code, city is not found and area is not found then default is Toronto as location | |
$property_formatted['REAL_HOMES_property_location'] = '43.650,-79.380'; | |
} | |
} | |
} | |
//Else: potentially ask google for address if there's no ZIP code | |
//property additional details | |
$property_formatted['REAL_HOMES_additional_details'] = array ( | |
'Type' => (isset($property['Prop_type']) ? $property['Prop_type'] : ''), | |
'Style' => (isset($property['Style']) ? $property['Style'] : ''), | |
'Utilities' => (isset($property['Utilities']) ? $property['Utilities'] : ''), | |
'Maintenance' => (isset($property['Maint']) ? $property['Maint'] : ''), | |
'Year Built' => $property['Yr_built'], | |
'Kitchens' => (isset($property['Num_kit']) ? $property['Num_kit'] : ''), | |
'Exterior' => (isset($property['Constr1_out']) ? (isset($property['Constr2_out']) ? $property['Constr1_out'].'/'.$property['Constr2_out'] : '' ) : ''), | |
'Zoning' => $property['Zoning'], | |
'Garage Type' => $property['Gar_type'], | |
'Basement' => $property['Bsmt1_out'], | |
'A/C' => $property['A_c'], | |
'Heat' => $property['Heating'], | |
'Hydro Included?' => (isset($property['Hydro_inc']) ? $property['Hydro_inc'] : ''), | |
'Hydro Expense' => (isset($property['Hydro_exp']) ? $property['Hydro_exp'] : ''), | |
'Water' => (isset($property['Water']) ? $property['Water'] : ''), | |
'Water Expense' => (isset($property['Water_exp']) ? $property['Water_exp'] : ''), | |
'Taxes' => (isset($property['Taxes']) ? (isset($property['Yr']) ? $property['Taxes'].'/'.$property['Yr'] : '' ) : ''), | |
'Days on the Market' => $property['Dom'], | |
); | |
$property_formatted['full_dump'] = $property; | |
$update_check = ''; | |
//This will check for old properties and see if they need to be updated | |
$update_check = CheckOld::data($property['Ml_num'], $property['Timestamp_sql'], $property['Status']); | |
if (is_array($update_check)) { | |
if (isset($update_check['update'])) { | |
$date_added = get_the_time('U', $update_check['update']); | |
$pix_updated_date = strtotime($property['Pix_updt']); | |
$update = new Update($property_formatted['REAL_HOMES_property_id'], $update_check['update'], get_post_meta($update_check['update'], 'wptrebs_photos', true),$this->class); | |
$updated_post = $update->posts($property); | |
// if(!is_wp_error($updated_post)){ | |
// error_log('INFO: '.$this->class.' Property '.$property_formatted['REAL_HOMES_property_id'].' has been updated'); | |
// }else{ | |
// error_log('ERROR: could not update '.$this->class.' property. '.$updated_post->get_error_code().': '.$updated_post->get_error_message()); | |
// } | |
if(is_wp_error($updated_post)){ | |
error_log('ERROR: could not update '.$this->class.' property. '.$updated_post->get_error_code().': '.$updated_post->get_error_message()); | |
} | |
//check to see if there's new images available since the property was added | |
if($date_added < $pix_updated_date){ | |
$photos = $this->feed->photos($property_formatted['REAL_HOMES_property_id']); | |
//Get the photos | |
self::photos($photos, $updated_post, $property_formatted['REAL_HOMES_property_id']); | |
} | |
} elseif (isset($update_check['new'])) { | |
//set up arguments before entering post to wp | |
$post_args = array( | |
'post_title' => $property_formatted['REAL_HOMES_property_id']. " - " .$property_formatted['REAL_HOMES_property_address'], | |
'post_content' => $property['Ad_text'] . ((strlen($property['Extras']) > 0) ? '<br><br><strong>Extras: </strong>'.$property['Extras'] : '') . '<br><br>' . $property['Rltr'], | |
'post_status' => 'publish', | |
'post_type' => 'property', | |
'comment_status' => 'closed' | |
); | |
//insert post and return new post id | |
$posted_property = wp_insert_post($post_args); | |
//error_log('INFO: New '.$this->class.' Property '.$property_formatted['REAL_HOMES_property_id'].' created'); | |
//check that the post was inserted without errors | |
if(!is_wp_error($posted_property)){ | |
//Update Property Features Taxonomy (only present in Condo and Residential classes) | |
if($this->class != 'Commercial'){ | |
for ($i = 1; $i <= 6; $i++) { | |
$term = term_exists($property['Prop_feat'.$i.'_out'], 'property-feature'); | |
if ($term !== 0 && $term !== null) { | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-feature' ); | |
}else { | |
//Term's name cannot be empty | |
if (strlen($property['Prop_feat' . $i . '_out']) > 0){ | |
$term = wp_insert_term( | |
$property['Prop_feat' . $i . '_out'], // the term | |
'property-feature', // the taxonomy | |
array( | |
'slug' => sanitize_title_with_dashes($property['Prop_feat' . $i . '_out']) | |
) | |
); | |
wp_set_post_terms($posted_property, array($term['term_id']), 'property-feature'); | |
} | |
} | |
} | |
} | |
//Update Property Area | |
$area_term = term_exists($property['Area'], 'property-city'); | |
if ($area_term !== 0 && $area_term !== null) { | |
wp_set_post_terms($posted_property, array($area_term['term_id']), 'property-city'); | |
} else { | |
$area_term = wp_insert_term( | |
$property['Area'], // the term | |
'property-city', // the taxonomy | |
array( | |
'slug' => sanitize_title_with_dashes($property['Area']) | |
) | |
); | |
wp_set_post_terms($posted_property, array($area_term['term_id']), 'property-city'); | |
} | |
//Update Property City Taxonomy | |
$term = term_exists($property['Municipality'], 'property-city'); | |
if ($term !== 0 && $term !== null) { | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-city' ); | |
}else{ | |
$term = wp_insert_term( | |
$property['Municipality'], // the term | |
'property-city', // the taxonomy | |
array( | |
'parent' => $area_term['term_id'], | |
'slug' => sanitize_title_with_dashes($property['Municipality']) | |
) | |
); | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-city' ); | |
} | |
//Update Property Status Taxonomy | |
$term = term_exists($property['S_r'], 'property-status'); | |
if ($term !== 0 && $term !== null) { | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-status' ); | |
}else{ | |
$term = wp_insert_term( | |
$property['S_r'], // the term | |
'property-status', // the taxonomy | |
array( | |
'slug' => sanitize_title_with_dashes($property['S_r']) | |
) | |
); | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-status' ); | |
} | |
//Update Property Type Taxonomy | |
$type_term = term_exists($this->class, 'property-type'); | |
if ($type_term !== 0 && $type_term !== null) { | |
wp_set_post_terms( $posted_property, array($type_term['term_id']), 'property-type' ); | |
}else{ | |
$type_term = wp_insert_term( | |
$this->class, // the term | |
'property-type', // the taxonomy | |
array( | |
'slug' => sanitize_title_with_dashes($this->class) | |
) | |
); | |
wp_set_post_terms( $posted_property, array($type_term['term_id']), 'property-type' ); | |
} | |
//Add Property Type Child if Property is Commercial | |
if($this->class == 'Commercial'){ | |
$term = term_exists($property['Prop_type'], 'property-type'); | |
if ($term !== 0 && $term !== null) { | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-type' ); | |
}else{ | |
$term = wp_insert_term( | |
$property['Prop_type'], // the term | |
'property-type', // the taxonomy | |
array( | |
'parent' => $type_term['term_id'], | |
'slug' => sanitize_title_with_dashes($property['Prop_type']) | |
) | |
); | |
wp_set_post_terms( $posted_property, array($term['term_id']), 'property-type' ); | |
} | |
} | |
//add post meta using the new post id and good looking array | |
foreach ($property_formatted as $key => $value) { | |
if (!is_null($value)) { | |
add_post_meta($posted_property, $key, $value, true) || update_post_meta($posted_property, $key, $value); | |
} | |
} | |
$photos = $this->feed->photos($property_formatted['REAL_HOMES_property_id']); | |
//Get the photos | |
self::photos($photos, $posted_property, $property_formatted['REAL_HOMES_property_id']); | |
} | |
else{ | |
error_log('ERROR: could not create new '.$this->class.' property. '.$posted_property->get_error_code .'-'. $posted_property->get_error_message()); | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Links the picture to a WordPress post | |
* | |
* @param $filename | |
* @param $dir | |
* @param $parent_post_id | |
* @param $mls | |
* @param $first | |
*/ | |
protected function addPhotoToWordPress($filename, $dir, $parent_post_id, $mls, $first) | |
{ | |
// $filename should be the path to a file in the upload directory. | |
$filename = $dir . '/' . $filename; | |
// Check the type of tile. We'll use this as the 'post_mime_type'. | |
$filetype = wp_check_filetype( basename( $filename ), null ); | |
// Get the path to the upload directory. | |
$wp_upload_dir = $dir; | |
// Prepare an array of post data for the attachment. | |
$attachment = array( | |
'guid' => self::getDirectoryURL($mls) .'/'. basename( $filename ), | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
// Insert the attachment. | |
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id ); | |
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
// Generate the metadata for the attachment, and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
//Add the first image as thumbnail and banner | |
if ($first == true) { | |
add_post_meta($parent_post_id, '_thumbnail_id', $attach_id); | |
add_post_meta($parent_post_id, 'REAL_HOMES_page_banner_image', $attach_id); | |
} | |
//Insert the image id (attachment id) information as postmeta for the property | |
add_post_meta($parent_post_id, 'REAL_HOMES_property_images', $attach_id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment