Current File : /home/n742ef5/royalanteam.com/mls_bkp/mls_pic.php |
<?php
ini_set('memory_limit', '1024M');
set_time_limit(0);
ini_set('max_execution_time', 7200);
ini_set('display_errors', 1);
ini_set("log_errors", 1);
//ini_set("error_log", "mls_resi_new.txt");
error_reporting(E_ALL);
date_default_timezone_set('America/Toronto');
echo "<pre>";
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$t_start = $mtime;
require_once("config/mls_config.php");
require_once("lib/phrets.php");
require_once("/home/n742ef5/royalanteam.com/wp-load.php");
//require_once("../wp-load.php");
global $wpdb;
$website_uri = site_url();
$date = date("Y-m-d", time());
$year = date("Y", time());
$month = date("m", time());
$path = $date;
$main_directory = '/home/n742ef5/royalanteam.com/';
$sub_directory = $year . "/" . $month;
$relative_path = "wp-content/uploads/" . $year . "/" . $month;
$full_path = $main_directory . "" . $relative_path;
$total_listings_group = 50;
$pic_limit = 25;
if (!is_dir($full_path))
mkdir($full_path, 0777, TRUE);
$message = '';
//$post_unique_query = "SELECT * FROM $wpdb->postmeta where ( meta_key='image_update_needed' and post_id = 102145 )" ;
$post_unique_query = "SELECT * FROM $wpdb->postmeta where ( meta_key='image_update_needed' and meta_value='1') ORDER BY meta_id DESC LIMIT $total_listings_group" ;
$result_unique = $wpdb->get_results($post_unique_query,ARRAY_A);
if ( $result_unique && count($result_unique) > 0) {
// MLS Login
$rets = new phRETS;
$rets->AddHeader("Accept", "*/*");
$rets->AddHeader("RETS-Version", "RETS/$rets_version");
$rets->AddHeader("User-Agent", 'UserAgent/1.0');
$rets->SetParam('debug_mode', false);
$rets->SetParam("offset_support", false);
$rets->SetParam("compression_enabled", true);
$connect = $rets->Connect($login_url, $username, $password);
if (!$connect) {
$error_details = $rets->Error();
$error_text = strip_tags($error_details['text']);
$error_type = strtoupper($error_details['type']);
echo "<center><span style='color:red;font-weight:bold;'>{$error_type} ({$error_details['code']}) {$error_text}</span></center>";
exit;
}
foreach ($result_unique as $post) {
//print_r($post);
$post_id = $post['post_id'];
echo "<br>Post : ".$post_id;
//exit;
$listing_key = get_post_meta($post_id, 'fave_mls_field_key_value');
$mls_id = $listing_key[0];
$sysid = $listing_key[0];
$photos = array();
$media = get_attached_media('', $post_id);
if (isset($media) && count($media) > 0) {
foreach ($media as $photo) {
$main_image_path = $photo->post_content_filtered;
error_log("Image " . $main_image_path . "<br>");
@unlink($main_image_path);
$thumbnail_image_src = wp_get_attachment_image_src($photo->ID, 'thumbnail');
$thumbnail_relative_url = str_replace($website_uri.'/', $main_directory, $thumbnail_image_src['0']);
@unlink($thumbnail_relative_url);
$image_post_id = $photo->ID;
$wp_delete_query = "DELETE FROM $wpdb->postmeta where post_id=$image_post_id";
$wpdb->query($wp_delete_query);
$wp_delete_query = "DELETE FROM $wpdb->posts where ID =$image_post_id";
$wpdb->query($wp_delete_query);
}
$wp_delete_query = "DELETE FROM $wpdb->postmeta where post_id=$post_id and meta_key in('_iwp_gallery','fave_property_images')";
$wpdb->query($wp_delete_query);
}
echo " : ID - ".$sysid;
if($sysid=='') {
echo " : Not sysid ";
continue;
}
$photos = $rets->GetObject('Property', 'Photo', $sysid,'*');
$pic_count = 0;
$images_array = array();
foreach ($photos as $photo) {
if ($pic_count < $pic_limit) {
if ($photo['Success'] == 1) {
$pic_count++;
$image_name = "Photo" . $mls_id . "-" . $pic_count . ".jpeg";
$image_full_path = $full_path . "/" . $image_name;
file_put_contents($image_full_path, $photo['Data']);
$recordpp['post_author'] = 1;
$recordpp['post_status'] = 'publish';
$recordpp['post_parent'] = $post_id;
$recordpp['post_mime_type'] = 'image/jpeg';
$recordpp['post_type'] = 'attachment';
$recordpp['post_content'] = '';
$recordpp['post_title'] = $mls_id . "-" . $pic_count;
$recordpp['post_name'] = $mls_id . "_" . $pic_count;
$recordpp['post_content_filtered'] = $full_path . "/" . $image_name;
$recordpp['guid'] = $website_uri . "/" . $relative_path . "/" . $image_name;
$photo_insert = $wpdb->insert( $wpdb->posts, $recordpp);
$photo_id = $wpdb->insert_id;
$images_array[] = $photo_id;
if ($pic_count == 1) {
$main_photo_id = $photo_id;
//$post_meta_data['fave_prop_slider_image'] = $main_photo_id;
$post_meta_data['_thumbnail_id'] = $main_photo_id;
echo "<br>Images downloaded for " . $post_id;
}
$iimage_data['meta_key'] = '_wp_attached_file';
$iimage_data['post_id'] = $photo_id;
$iimage_data['meta_value'] = $sub_directory . "/" . $image_name;
$image_meta = $wpdb->insert( $wpdb->postmeta, $iimage_data);
/*$image_pr_data['meta_key'] = 'fave_property_images';
$image_pr_data['meta_value'] = $photo_id;
$image_pr_data['post_id'] = $post_id;
$image_newmeta = $wpdb->insert( $wpdb->postmeta, $image_pr_data);
*/
$meta_data_thumb['width'] = '1366';
$meta_data_thumb['height'] = '768';
$meta_data_thumb['file'] = $sub_directory . "/" . $image_name;
$meta_data_thumb['image_meta'] = array(
'aperture' => 0,
'credit' => '',
'camera' => '',
'caption' => '',
'created_timestamp' => '',
'copyright' => '',
'focal_length' => 0,
'iso' => '',
'shutter_speed' => 0,
'title' => '',
'orientation' => '',
'keywords' => array()
);
$iiimage_data['meta_key'] = '_wp_attachment_metadata';
$iiimage_data['post_id'] = $photo_id;
$iiimage_data['meta_value'] = serialize($meta_data_thumb);
$image_meta = $wpdb->insert( $wpdb->postmeta, $iiimage_data);
}
}
}
if( $pic_count>0 ){
$post_meta_data['_estate_gallery'] = 'myhome_estate_gallery';
$post_meta_data['estate_gallery'] = $images_array;
$post_meta_data['image_update_needed'] = 0;
$post_meta_data['property_image_downloaded_time'] = date('Y-m-d H:i:s');
}
if( !empty($post_meta_data)){
foreach ($post_meta_data as $meta_key => $value) {
if (!add_post_meta($post_id, $meta_key, $value, true)) {
update_post_meta($post_id, $meta_key, $value);
}
}
}
$message .= $post_id.' - '.$sysid.' : '.$pic_count;
$my_post = array(
'ID' => $post_id
);
wp_update_post($my_post);
//exit;
}
}
$end_time = date('Y-m-d H:i:s');
$headers = 'From: No Reply <noreply@'.$_SERVER['HTTP_HOST'].'>' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
//$headers = 'From: '. $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
//$sent = wp_mail('msuman1610@gmail.com', 'Image Download '.$end_time, strip_tags($message), $headers);