Current File : /home/n742ef5/royalanteam.com/check-and-modify-estate.php
<?php
// Path to the Estate.php file
$estate_file = 'wp-content/plugins/myhome-core/legacy/includes/Estates/Estate.php';

// Check if file exists
if (!file_exists($estate_file)) {
    echo "Estate.php file not found.\n";
    exit;
}

// Read the file content
$content = file_get_contents($estate_file);

// Check if the file was modified in the last minute (for cron job)
$file_modified_time = filemtime($estate_file);
if (time() - $file_modified_time < 60) {
    echo "File was modified less than a minute ago. Skipping update.\n";
    exit;
}

// New constructor code (formatted with proper tabs)
$new_constructor_code = <<<'EOD'
	public function __construct(\WP_Post $post)
	{
		$this->post = $post;
		$this->meta = get_post_meta($post->ID);
		$this->prices = new Prices($this);
		$this->meta['gallery'] = array();
		$gallery_urls = get_post_meta($post->ID, 'estate_gallery_urls', true);
		if (isset($gallery_urls)) {
			$gallery_urls = json_decode($gallery_urls, true);
			$this->meta['gallery'] = is_array($gallery_urls) ? $gallery_urls : array();
		}
	}
EOD;

// Check if constructor is already updated
$constructor_check = '/public function __construct\s*\([^)]*\)\s*{[^}]*\$this->meta\[\'gallery\'\]\s*=\s*array\(\);/s';
if (preg_match($constructor_check, $content)) {
    echo "Constructor is already updated. Skipping constructor update.\n";
    $constructor_updated = true;
} else {
    // Regex to match the entire constructor block, including any trailing else and closing braces
    $constructor_pattern = '/public function __construct\s*\([^)]*\)\s*{(?:[^{}]*+|{[^{}]*+}*+)*?}\s*else\s*{[^}]*}\s*/s';

    $constructor_updated = false;
    if (preg_match($constructor_pattern, $content)) {
        $content = preg_replace($constructor_pattern, $new_constructor_code."\n", $content, 1);
        echo "Constructor has been updated.\n";
        $constructor_updated = true;
    } else {
        // Try fallback: match just the first function block (if no else remains)
        $constructor_pattern_simple = '/public function __construct\s*\([^)]*\)\s*{(?:[^{}]*+|{[^{}]*+}*+)*?}\s*/s';
        if (preg_match($constructor_pattern_simple, $content)) {
            $content = preg_replace($constructor_pattern_simple, $new_constructor_code."\n", $content, 1);
            echo "Constructor has been updated (simple pattern).\n";
            $constructor_updated = true;
        } else {
            echo "Constructor pattern not found.\n";
            exit;
        }
    }
}

// Only proceed if constructor was updated
if ($constructor_updated) {
    // New get_gallery_data method (formatted with proper tabs)
    $new_gallery_data_method = <<<'EOD'
	public function get_gallery_data($limit = 0)
	{
		if (empty($this->meta['gallery']) || !is_array($this->meta['gallery'])) {
			return array();
		}

		$gallery_data = array();
		$counter = 1;
		foreach ($this->meta['gallery'] as $image_url) {
			$image = array(
				'image' => esc_url($image_url),
				'alt' => '',
			);

			if (!empty($image['image'])) {
				$gallery_data[] = $image;
			}

			if ($limit > 0 && $counter == $limit) {
				break;
			}
			$counter++;
		}

		return apply_filters('myhome_estate_gallery_data', $gallery_data, $this);
	}
EOD;

    // New get_gallery method (formatted with proper tabs)
    $new_gallery_method = <<<'EOD'
	public function get_gallery()
	{
		$gallery_urls = get_post_meta($this->post->ID, 'estate_gallery_urls', true);

		if (!empty($gallery_urls)) {
			$gallery_urls = json_decode($gallery_urls, true);
			if (is_array($gallery_urls)) {
				$gallery = array();
				$i = 0;
				foreach ($gallery_urls as $url) {
					$i++;
					$gallery[] = array(
						'url' => $url,
						'ID' => $i,
						'alt' => ''
					);
				}
				return apply_filters('myhome_estate_gallery', $gallery, $this);
			}
		}
		$gallery = get_field('myhome_estate_gallery', $this->post->ID);
		if (!is_array($gallery)) {
			return array();
		}

		foreach ($gallery as $key => $image) {
			$gallery[$key]['srcset'] = \MyHomeCore\My_Home_Core()->images->get($image['ID'], 'myhome-standard');
		}

		return apply_filters('myhome_estate_gallery', $gallery, $this);
	}
EOD;

    // Check if get_gallery_data is already updated
    $gallery_data_check = '/public function get_gallery_data\s*\([^)]*\)\s*{[^}]*\$this->meta\[\'gallery\'\]/s';
    if (!preg_match($gallery_data_check, $content)) {
        // Replace get_gallery_data method
        $gallery_data_pattern = '/public function get_gallery_data\s*\([^)]*\)\s*{(?:[^{}]*+|{[^{}]*+}*+)*?}\s*/s';
        if (preg_match($gallery_data_pattern, $content)) {
            $content = preg_replace($gallery_data_pattern, $new_gallery_data_method."\n", $content, 1);
            echo "get_gallery_data method has been updated.\n";
        } else {
            echo "get_gallery_data method not found.\n";
        }
    } else {
        echo "get_gallery_data method is already updated. Skipping update.\n";
    }

    // Check if get_gallery is already updated
    $gallery_check = '/public function get_gallery\s*\([^)]*\)\s*{[^}]*\$gallery_urls\s*=\s*get_post_meta/s';
    if (!preg_match($gallery_check, $content)) {
        // Replace get_gallery method
        $gallery_pattern = '/public function get_gallery\s*\([^)]*\)\s*{(?:[^{}]*+|{[^{}]*+}*+)*?}\s*/s';
        if (preg_match($gallery_pattern, $content)) {
            $content = preg_replace($gallery_pattern, $new_gallery_method."\n", $content, 1);
            echo "get_gallery method has been updated.\n";
        } else {
            echo "get_gallery method not found.\n";
        }
    } else {
        echo "get_gallery method is already updated. Skipping update.\n";
    }

    // Write the modified content back to the file
    if (file_put_contents($estate_file, $content)) {
        echo "File has been successfully updated.\n";
    } else {
        echo "Error writing to file.\n";
    }
}