<?php
/**
 * Plugin Name: Alsendo Connect
 * Plugin URI: https://alsendo.com/partners/
 * Description: Send parcels via Ecolet, Zaslat, Apaczka directly from your shop panel
 * Product: Alsendo Connect
 * Version: 1.1.2
 * Tested up to: 6.9
 * Requires at least: 5.3
 * Requires PHP: 7.4
 * Author: iLabs LTD
 * Author URI: https://iLabs.dev
 * Text Domain: alsendo-connect
 * Domain Path: /lang/
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 *
 * Copyright 2025 iLabs LTD
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

//require_once __DIR__ . '/src/ShutdownHandler.php';
//\Inspire_Labs\Alsendo_Connect\ShutdownHandler::register( __FILE__ );

require_once __DIR__ . '/vendor/autoload.php';
require_once 'dependencies.php';


function alsendo_connect(): \Inspire_Labs\Alsendo_Connect\Plugin {
	static $instance = null;
	if ( null === $instance ) {
		$instance = new \Inspire_Labs\Alsendo_Connect\Plugin();
	}
	return $instance;
}

define( 'ALSENDO_CONNECT_PLUGIN_DIR', __DIR__ );
define( 'ALSENDO_CONNECT_PLUGIN_FILE', __FILE__ );
define(
	'ALSENDO_CONNECT_PLUGIN_PATH',
	plugin_dir_path( __FILE__ )
);

//define( 'ALSENDO_CONNECT_REQUEST_DEBUG', false );
//define( 'ALSENDO_CONNECT_DEBUG_MODE', true );


add_action(
	'after_setup_theme',
	function () {
		if (
			( function_exists( 'is_plugin_active' ) && is_plugin_active( 'woocommerce/woocommerce.php' ) )
			|| in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true )
			|| ( defined( 'WC_PLUGIN_FILE' ) && defined( 'WC_VERSION' ) )
		) {

			if ( alsendo_connect_system_check() && alsendo_connect_conflict_plugin_cjeck() ) {

				$config = array(
					'__FILE__'    => __FILE__,
					'slug'        => 'alsendo_connect',
					'lang_dir'    => 'lang',
					'text_domain' => 'alsendo-connect',
				);

				$plugin = alsendo_connect();
				$plugin->execute( $config );
				$plugin->plugins_loaded_hooks();

			}
		} else {
			add_action( 'admin_notices', 'alsendo_connect_plugin_needed_notice' );
			return;
		}
	}
);

add_action(
	'before_woocommerce_init',
	function () {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
				'custom_order_tables',
				__FILE__,
				true
			);
		}
	}
);



function alsendo_connect_plugin_needed_notice() {
	$message = sprintf(
	/* translators: Placeholders: %1$s and %3$s are <strong> tags. %2$s and %4$s are text */
		'%1$s %2$s %3$s %4$s',
		'<strong>',
		esc_html__( 'Alsendo Connect', 'alsendo-connect' ),
		'</strong>',
		esc_html__( 'requires WooCommerce to function.', 'alsendo-connect' )
	);
	printf( '<div class="error"><p>%s</p></div>', wp_kses_post( $message ) );
}


function alsendo_connect_system_check() {

	if ( \PHP_VERSION_ID < 70400 ) {
		\add_action(
			'admin_notices',
			function () {
				echo "<div class='notice notice-error error'><p><strong style='color: red;'>Alsendo Connect: ";
				echo esc_html__(
					'PHP version is older than 7.4 so this plugin will not work. Please contact your host and ask them to upgrade.',
					'alsendo-connect'
				);
				echo '</strong></p></div>';
			}
		);

		return false;
	}

	return true;
}


function alsendo_connect_conflict_plugin_cjeck() {

	if ( in_array( 'apaczka-pl/apaczka-pl.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {

		\add_action(
			'admin_notices',
			function () {
				echo "<div class='notice notice-error error'><p><strong style='color: red;'>Alsendo Connect: ";
				echo esc_html__(
					'Disable plugin Apaczka.pl before using Alsendo Connect plugin to avoid conflicts.',
					'alsendo-connect'
				);
				echo '</strong></p></div>';
			}
		);

		return false;
	}

	return true;
}

add_filter(
	'plugin_action_links_' . plugin_basename( __FILE__ ),
	function ( $links ) {
		$plugin_links = array(
			'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=alsendo_connect_settings_general' ) ) . '">' . esc_html__( 'Settings', 'alsendo-connect' ) . '</a>',
		);

		return array_merge( $links, $plugin_links );
	}
);

/**
 * On deactivation delete cache data
 */
register_deactivation_hook( __FILE__, 'alsendo_connect_clear_service_cache' );
function alsendo_connect_clear_service_cache() {
	delete_option( 'alsendo_connect_apaczka_service_structure_timestamp' );
	delete_option( 'alsendo_connect_apaczka_service_structure' );
	delete_option( 'alsendo_connect_zaslat_address_book_timestamp' );
	delete_option( 'alsendo_connect_zaslat_address_book' );
	delete_option( 'alsendo_connect_ecolet_service_structure_timestamp' );
	delete_option( 'alsendo_connect_ecolet_service_structure' );
	$shipping_alsendo = new \Inspire_Labs\Alsendo_Connect\Shipping_Method_Alsendo_Connect();
	if ( is_object( $shipping_alsendo ) ) {
		$shipping_alsendo->clear_zones_cache();
	}
}
