Current File : /home/n742ef5/royalanteam.com/mls/fe.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once(dirname(__FILE__) . '/constant.php');
require_once(ABSPATH . 'wp-load.php');

global $wpdb;
$table_name = 'properties';
$mls_key_field = 'ListingKey';

echo "Property features update started\n";
$propertiesData = $wpdb->get_results("SELECT * FROM $table_name WHERE flag = 1 AND post_id IS NOT NULL", ARRAY_A);
echo "Properties For Features Update" . count($propertiesData);
$skipp=true;
$i=0;
foreach ($propertiesData as $record) {$i++;
    $json_data = json_decode($record['otherjson'], true);
    $record = array_merge($record, $json_data);
    $post_id = $record['post_id'];
    if($post_id==2909982){
        $skipp=false;
    }
    echo " count $i post ID: $post_id\n";
    if($skipp){
        continue;
    }
    $addition_features_keys = array(

        'GarageParkingSpaces' => 'Garage spaces',
        'GarageType' => 'Garage type',
        'ParkingTotal' => 'Parking',
        'HeatType' => 'Heating',
        'Cooling' => 'Cooling',
        'FireplacesTotal' => 'Fireplace',
        'HeatSource' => 'Heat Source',
        'Furnished' => 'Furnished',
        'KitchensTotal' => 'Kitchens',
        'DaysOnMarket' => 'Days On Market',
        'DenFamilyroomYN' => 'Family Room',
        'TelephoneYNA' => 'Telephone',
        'ExteriorFeatures' => 'ExteriorFeatures',
        'PropertyFeatures' => 'PropertyFeatures',
        'Water' => 'Water',
        'WaterBodyName' => 'WaterBodyName',
        'WaterBodyType' => 'WaterBodyType',
        'WaterFrontageFt' => 'WaterFrontageFt',
        'LotWidth' => 'Lot Width',
        'LotDepth' => 'Lot Depth',
        'ConstructionMaterials' => 'ConstructionMaterials',
        'ShorelineExposure' => 'Shoreline Exposure',
        'ParkingMonthlyCost' => 'Parking Monthly Cost',
        'ParkingSpaces' => 'Parking Spaces',
        'ParkingFeatures' => 'ParkingFeatures',
        'AccessToProperty' => 'Access To Property',
        'PrivateEntranceYN' => 'Private Entrance',
        'LotIrregularities' => 'Lot Irregularities',
        'TaxLegalDescription' => 'Tax Legal Description',
        'Sewer' => 'Sewer',
        'Sewage' => 'Sewage',
        'ParcelOfTiedLand' => 'Parcel Of TiedLand',
        'SpecialDesignation' => 'Special Designation',
        'Zoning' => 'Zoning',
        'Roof' => 'Roof',
        'WashroomsType1Pcs' => 'Washrooms Type1Pcs',
        'WclWashroomsType2Pcsoset_p2' => 'Washrooms Type2Pcs',
        'WashroomsType3Pcs' => 'Washrooms Type3Pcs',
        'WashroomsType4Pcs' => 'Washrooms Type4Pcs',
        'WashroomsType5Pcs' => 'Washrooms Type5Pcs',
        'WashroomsType1Level' => 'Washrooms Type1Level',
        'WashroomsType2Level' => 'Washrooms Type2Level',
        'WashroomsType3Level' => 'Washrooms Type3Level',
        'WashroomsType4Level' => 'Washrooms Type4Level',
        'WashroomsType5Level' => 'Washrooms Type5Level',
        'WashroomsType1' => 'WashroomsType1',
        'WashroomsType2' => 'WashroomsType2',
        'WashroomsType3' => 'WashroomsType3',
        'WashroomsType4' => 'WashroomsType4',
        'WashroomsType5' => 'WashroomsType5',
        'ChattelsYN' => 'Chattels',
        'PoolFeatures' => 'PoolFeatures',
        'CraneYN' => 'Crane',
        'FarmFeatures' => 'FarmFeatures',
        'SecurityFeatures' => 'SecurityFeatures',
        'FireplaceFeatures' => 'FireplaceFeatures',
        'WaterfrontFeatures' => 'WaterfrontFeatures',
        'LaundryFeatures' => 'LaundryFeatures',
        'DriveInLevelShippingDoorsHeightFeet' => 'DriveInLevelShippingDoorsHeightFeet',
        'DriveInLevelShippingDoors' => 'DriveInLevelShippingDoors',
        'DriveInLevelShippingDoorsWidthFeet' => 'DriveInLevelShippingDoorsWidthFeet',
        'DoubleManShippingDoorsHeightFeet' => 'DoubleManShippingDoorsHeightFeet',
        'DoubleManShippingDoors' => 'DoubleManShippingDoors',
        'DoubleManShippingDoorsWidthFeet' => 'DoubleManShippingDoorsWidthFeet',
        'GradeLevelShippingDoorsHeightFeet' => 'GradeLevelShippingDoorsHeightFeet',
        'GradeLevelShippingDoors' => 'GradeLevelShippingDoors',
        'GradeLevelShippingDoorsWidthFeet' => 'GradeLevelShippingDoorsWidthFeet',
        'TruckLevelShippingDoorsHeightFeet' => 'TruckLevelShippingDoorsHeightFeet',
        'TruckLevelShippingDoors' => 'TruckLevelShippingDoors',
        'TruckLevelShippingDoorsWidthFeet' => 'TTruckLevelShippingDoorsWidthFeet',
        'TrailerParkingSpots' => 'TrailerParkingSpots',
        'Utilities' => 'Utilities',
        'WaterSource' => 'WaterSupplyTypes',
        'BaySizeWidthFeet' => 'BaySizeWidthFeet',
        'BaySizeLengthFeet' => 'BaySizeLengthFeet',
        'WaterfrontAccessory' => 'WaterfrontAccessory',
        'WaterDeliveryFeature' => 'WaterDeliveryFeature',
        'LockerNumber' => 'Locker #',
        'LotFeatures' => 'LotFeatures',
        'AccessibilityFeatures' => 'AccessibilityFeatures',
        'CommunityFeatures' => 'CommunityFeatures',
    );
    $feature_tax = array();
    if (isset($record['HeatType']) && $record['HeatType'] != '' && strtolower($record['HeatType']) != 'null') {
        $feature_tax['Heat Included'] = 1;
    }
    if (isset($record['CableYNA']) && $record['CableYNA'] != '' && strtolower($record['CableYNA']) != 'null') {
        $feature_tax['Cable TV Included'] = 1;
    }
    if (isset($record['ElevatorYN']) && $record['ElevatorYN'] != '' && strtolower($record['ElevatorYN']) != 'null') {
        $feature_tax['Elevator'] = 1;
    }
    if (isset($record['Furnished']) && $record['Furnished'] != '' && strtolower($record['Furnished']) != 'null' && $record['Furnished'] !="Unfurnished" ) {
        $feature_tax['Furnished'] = 1;
    }
    if (isset($record['WaterfrontYN']) && $record['WaterfrontYN'] != '' && strtolower($record['WaterfrontYN']) != 'null') {
        $feature_tax['Waterfront'] = 1;
    }
    if (isset($record['ChattelsYN']) && $record['ChattelsYN'] != '' && strtolower($record['ChattelsYN']) != 'null') {
        $feature_tax['Chattels'] = 1;
    }
    if (isset($record['FireplaceYN']) && $record['FireplaceYN'] != '' && strtolower($record['FireplaceYN']) != 'null') {
        $feature_tax['Fireplace'] = 1;
    }
    if (isset($record['CraneYN']) && $record['CraneYN'] != '' && strtolower($record['CraneYN']) != 'null') {
        $feature_tax['Crane'] = 1;
    }
    if (isset($record['GarageYN']) && $record['GarageYN'] != '' && strtolower($record['GarageYN']) != 'null') {
        $feature_tax['Garage'] = 1;
    }
    if (isset($record['SpaYN']) && $record['SpaYN'] != '' && strtolower($record['SpaYN']) != 'null') {
        $feature_tax['Spa'] = 1;
    }
    if (isset($record['BasementYN']) && $record['BasementYN'] != '' && strtolower($record['BasementYN']) != 'null') {
        $feature_tax['Basement'] = 1;
    }
    if (isset($record['CentralVacuumYN']) && $record['CentralVacuumYN'] != '' && strtolower($record['CentralVacuumYN']) != 'null') {
        $feature_tax['CentralVacuum'] = 1;
    }
    if (isset($record['PropertyFeatures']) && is_array($record['PropertyFeatures'])) {
        foreach ($record['PropertyFeatures'] as $feature) {
            if (!empty($feature) && strtolower($feature) != 'null' && strtolower($feature) != 'none') {
                $feature_tax[$feature] = 1;
            }
        }
    }
    if (isset($record['Sewer']) && is_array($record['Sewer']) && !empty($record['Sewer']) && $record['Sewer'][0] !== 'None') {
        $feature_tax['Sewer'] = 1;
    }

    foreach ($feature_tax as $key => $value) {
        $all_tax[] = array('name' => 'features', 'value' => $key);
    }
    foreach ($all_tax as $term_tax) {
        $taxonomy = $term_tax['name'];
        $term_value = trim($term_tax['value']);
        if (empty($term_value)) {
            continue;
        }

        $term_info = term_exists($term_value, $taxonomy);

        if (is_array($term_info)) {
            $term_id = $term_info['term_id'];
        } else {
            $term_info = wp_insert_term(
                $term_value,
                $taxonomy,
                array(
                    'description' => $term_value,
                )
            );
            if (is_wp_error($term_info)) {
                error_log('Error inserting term: ' . print_r($term_info->get_error_messages(), true));
                continue;
            }
            $term_id = $term_info['term_id'];
        }
      

        // Assign the term to the post
        wp_set_object_terms($post_id, intval($term_id), $taxonomy, true);
    }


    $add_count = 0;
    foreach ($addition_features_keys as $feature_key => $feature_title) {
        if (isset($record[$feature_key])) {
            if (is_array($record[$feature_key])) {
                $record[$feature_key] = implode(', ', $record[$feature_key]);
            }
            if (!empty($record[$feature_key])) {
                if ($record[$feature_key] === 'Y') {
                    $record[$feature_key] = 'Yes';
                } elseif ($record[$feature_key] === 'N') {
                    $record[$feature_key] = 'No';
                }
                $post_meta_data['estate_additional_features_' . $add_count . '_estate_additional_feature_name'] = $feature_title;
                $post_meta_data['estate_additional_features_' . $add_count . '_estate_additional_feature_value'] = $record[$feature_key];
                $post_meta_data['_estate_additional_features_' . $add_count . '_estate_additional_feature_name'] = 'myhome_estate_additional_feature_name';
                $post_meta_data['_estate_additional_features_' . $add_count . '_estate_additional_feature_value'] = 'myhome_estate_additional_feature_value';
                $add_count++;
            }
        }
    }

    insertOrUpdatePostMetaData($post_meta_data, $post_id);
    echo "Updated post ID: $post_id\n";
}
echo "Property features update completed\n";

function insertOrUpdatePostMetaData($meta_data, $post_id)
{
    global $wpdb;
    if (!is_array($meta_data) || empty($meta_data)) {
        error_log("Error: meta_data must be a non-empty array.");
        return false;
    }
    try {
        foreach ($meta_data as $meta_key => $meta_value) {
            $meta_key_sanitized = esc_sql($meta_key);
            $meta_value_sanitized = esc_sql($meta_value);
            $existing_meta = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT meta_id FROM {$wpdb->prefix}postmeta WHERE post_id = %d AND meta_key = %s",
                    $post_id,
                    $meta_key_sanitized
                )
            );


            if ($existing_meta) {
                $update_query = $wpdb->prepare(
                    "UPDATE {$wpdb->prefix}postmeta SET meta_value = %s WHERE post_id = %d AND meta_key = %s",
                    $meta_value_sanitized,
                    $post_id,
                    $meta_key_sanitized
                );
                $result = $wpdb->query($update_query);

                if ($result === false) {
                    $error_message = $wpdb->last_error;
                    error_log("Error updating post meta data for post ID $post_id: $error_message");
                    return false;
                }
            } else {

                $insert_query = $wpdb->prepare(
                    "INSERT INTO {$wpdb->prefix}postmeta (post_id, meta_key, meta_value) VALUES (%d, %s, %s)",
                    $post_id,
                    $meta_key_sanitized,
                    $meta_value_sanitized
                );
                $result = $wpdb->query($insert_query);

                if ($result === false) {
                    $error_message = $wpdb->last_error;
                    error_log("Error inserting post meta data for post ID $post_id: $error_message");
                    return false;
                }
            }
        }

    
        echo "MetaData added or updated for post ID $post_id\n";
        return true;
    } catch (Exception $e) {

        error_log("Exception while inserting/updating post meta data: " . $e->getMessage());
        return false;
    }
}