Current File : /home/n742ef5/royalanteam.com/mls_bkp/mls_sold_new.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('default_charset', 'UTF-8');
error_reporting(E_ALL);
header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set('America/Toronto');
//ini_set("error_log", "/home3/sitwoweb/public_html/royalanteam/mls/mls_resi_new.txt");
//echo "dfdf";exit;
$plus_v = urlencode("+");
$mtime   = microtime();
$mtime   = explode(" ", $mtime);
$mtime   = $mtime[1] . $mtime[0];
$t_start = $mtime;

$to      = 'msuman1610@gmail.com';
$subject = 'Property Sold';

require_once("config/mls_config.php");
require_once("lib/phrets.php");
require_once("config/properties_config.php");
require_once("/home/n742ef5/royalanteam.com/wp-load.php");
global $wpdb;
$website_uri = site_url();
$website_uri2 = 'https://royalanteam.com';

//echo $website_uri;
//exit;
$message = "";
$message = '<html><head><title>RoyalTeam Sold Import</title></head><body>';
$timestamp_field = 'Timestamp_sql';
$file_name       = "mls_sold.php";
$curr_resource   = "Property";
$mls_key_field   = "Ml_num";
$curr_mls_id     = 1;
date_default_timezone_set('America/Toronto');
$curr_date          = date('Y-m-d H:i:s');
$cron_tablename     = 'properties_cron_log';
$tmp_tablename      = 'property_queue';
$pro_table = "rets_property_data_resi";
$last_success_query = "SELECT Ml_num FROM $pro_table where isActive=0 ORDER BY id DESC limit 5000";
$all_sold_post = $wpdb->get_results($last_success_query, ARRAY_A);
if (isset($all_sold_post)  && count($all_sold_post) < 5001) {
	foreach ($all_sold_post as $sold_post) {
		$mls_num = $sold_post['Ml_num'];
		$listing_key = $mls_num;
		echo "<br>" . $qn_query = "select post_id from $wpdb->postmeta where meta_key='fave_mls_field_key_value' and meta_value = '" . $listing_key . "' ";
		$pre_post = $wpdb->get_results($qn_query);
		if ($pre_post && !empty($pre_post)) {
			$post_id  = $pre_post[0]->post_id;
			//$post_id  = $each_posts['ID'];
			echo "<br>Post Id :" . $post_id;
			//continue;
			$media  = get_attached_media('', $post_id);
			if (isset($media) && count($media) > 0) {
				foreach ($media as $photo) {
					echo "<br>" . $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');
					if (strpos($thumbnail_image_src['0'], "https") !== false) {
						$thumbnail_relative_url = str_replace($website_uri2 . '/', $main_directory, $thumbnail_image_src['0']);
					} else {
						$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);

			$remove_query_meta = "DELETE FROM $wpdb->postmeta  where post_id = $post_id ";
			$result_remove     = $wpdb->query($remove_query_meta);

			$del_term = "DELETE FROM $wpdb->term_relationships where object_id = $post_id ";
			$wpdb->query($del_term);

			$remove_main   = "DELETE FROM $wpdb->posts where ID = $post_id ";
			$result_remove = $wpdb->query($remove_main);
		}
		$remove_main   = "DELETE FROM $pro_table where $mls_key_field = '$mls_num' ";
		$result_remove = $wpdb->query($remove_main);
	}
}