<?php
/**
* Plugin Name: Respacio Houzez Import
* Plugin URI: https://respacio.com
* Description: Import properties from the Respacio CRM.
* Version: 1.2.1
* Author: Respacio
* Author URI: https://respacio.com
* Text Domain: respacio-houzez-import
* Domain Path: /languages
* Since: 1.0
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
**/

// Exit if accessed directly.
if(! defined('ABSPATH')){ 
	exit; 
}

/**
 * loading composer autoload first
 */
$autoloadFile = __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (!file_exists($autoloadFile)) return;
require_once $autoloadFile;

use RespacioHouzezImport\ui;
use RespacioHouzezImport\ajax;
use RespacioHouzezImport\corn;
use RespacioHouzezImport\image;
use RespacioHouzezImport\cpt;
use RespacioHouzezImport\gravity;
use RespacioHouzezImport\cf7;
use RespacioHouzezImport\forminator;
use RespacioHouzezImport\wpforms;
use RespacioHouzezImport\export;

/**
 * making sure to execute the plugin after `plugin_loaded` hook
 */
add_action('plugin_loaded', function () {

	/**
	 * always define your constant as early as possible
	 * so from a single point we can control dynamic variable data
	 */
	if (!defined('RHIMO_PLUGIN_NAME')) define( 'RHIMO_PLUGIN_NAME', 'Respacio Houzez Import' );
	if (!defined('RHIMO_THEME_TYPE')) define( 'RHIMO_THEME_TYPE', '1' );
	if (!defined('RHIMO_API_BASE_URL')) define('RHIMO_API_BASE_URL', 'https://crm.respacio.com/ws/properties' );
	if (!defined('RHIMO_FEED_URL')) define('RHIMO_FEED_URL', 'https://crm.respacio.com/ws/properties/sync_properties_json' );
	if (!defined('RHIMO_PROPERTY_WEB_URL')) define('RHIMO_PROPERTY_WEB_URL', 'https://crm.respacio.com/ws/properties/sync_property_web_url' );
	if (!defined('MODE_OLD')) define('MODE_OLD', false);
	
	/**
	 * to add more form support edit the constant array
	 * also add supported class file with same name and class name as KEY.
	 */
	if (!defined('supportedForms')) define('supportedForms',[ 'cf7'=>'Contact 7', 'gravity'=>'Gravity Forms', /*'forminator' => 'Forminator',*/ 'wpforms' => 'WPForms' ]);
	
	

	ui::activate();
	ajax::activate();
	image::activate();
	corn::activate();
	cpt::configure();
	/** TODO: Remove below if no usage found*/
	include('includes/admin-page.php');
	gravity::onSubmit();
	cf7::onSubmit();
	forminator::onSubmit();
	wpforms::onSubmit();
});


//load text domain
function respacio_text_domain(){
	load_plugin_textdomain( 'respacio-houzez-import', false, dirname(plugin_basename(__FILE__)) . '/languages' );
}

add_action( 'init', 'respacio_text_domain' );

//plugin activation
function respacio_houzez_import_activate(){
	global $wpdb;

	$table = $wpdb->prefix . "property_images";

	// Check if table exists before querying it
	if ($wpdb->get_var("SHOW TABLES LIKE '$table'") === $table) {
		$db_test = $wpdb->get_col("SHOW COLUMNS FROM $table");

		$column = array_search('sequence', $db_test);

		if ($column === false || $column === '') {
			$wpdb->query("DROP TABLE IF EXISTS $table");
		}
	}

	//generating xml file before send it when API entered 
	$get_xml_link = get_option('xmlFileLink');
	if(!$get_xml_link){
		$xmlLink = export::xmlExportUrl();
		update_option('xmlFileLink', $xmlLink);
	}

	//flush rewrite rules only after any CPTs or rewrites
	flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'respacio_houzez_import_activate' );


//plugin deactivation
function respacio_houzez_import_deactivate(){
	flush_rewrite_rules( );
}

register_deactivation_hook( __FILE__, 'respacio_houzez_import_deactivate' );

//image format admin notice
$selected_image_formats = get_option('respacio_save_image_formats_list2');

if(empty($selected_image_formats)){

	add_action('admin_notices', 'respacio_houzez_image_format_admin_notice');

	function respacio_houzez_image_format_admin_notice() {
		$image_format_url = admin_url('admin.php?page=respacio_houzez_import');
		
		$message = __(
			'Urgent: You have not selected which image versions your website uses in the Respacio plugin. <strong>Respacio plugin creates 13 versions of each property image</strong> to cater to all types of websites.', 
			'respacio-houzez-import'
		);		
		$message .= ' ';
		$message .= sprintf( __(
			'You can select them <a style="color:blue" href="%s">here</a>, then click the "Image" tab to choose the image types your site needs so it won’t create unnecessary versions.', 
			'respacio-houzez-import'), $image_format_url );
		?>
		<div class="notice notice-warning">
			<p style="font-size:medium"><?php echo $message; ?></p>
		</div>
		<?php
	}

}