<?php
/**
 * Plugin Name:       Posten Bring Checkout
 * Plugin URI:        https://wordpress.org/plugins/posten-bring-checkout/
 * Description:       Posten Bring Checkout plugin for WooCommerce
 * Version:           1.1.50
 * Author:            Posten Bring AS
 * Author URI:        https://www.postenbring.no/en
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       posten-bring-checkout
 * Domain Path:       /languages
 * Requires Plugins:  woocommerce
 * Requires at least: 6.3
 * Requires PHP:      7.4
 * WC requires at least: 8.3
 * WC tested up to: 9.3.3
 */

use Automattic\WooCommerce\Utilities\FeaturesUtil;
use PostenBringCheckout\includes\Activation;
use PostenBringCheckout\includes\Main;

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

$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'woocommerce/woocommerce.php';

if (in_array( $plugin_path, wp_get_active_and_valid_plugins() )
	|| in_array( $plugin_path, wp_get_active_network_plugins() )) {

	$plugin_data = get_file_data( __FILE__, array( 'version' => 'version' ) );
	define( 'POSTEN_BRING_CHECKOUT_VERSION', $plugin_data['version'] );

	/**
	 * Activation callback
	 *
	 * Will run the necessary activation tasks
	 *
	 * @since 0.0.1
	 *
	 * @see Activation::activate()
	 */
	function posten_bring_checkout_activate_plugin(): void {
		require_once plugin_dir_path( __FILE__ ) . '/includes/Activation.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/CheckoutApi.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/CredentialsService.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/models/Credentials.php';
		Activation::activate();
	}

	/**
	 * Deactivation callback
	 *
	 * Will run the necessary deactivation tasks
	 *
	 * @since 0.0.1
	 *
	 * @see Activation::deactivate()
	 */
	function posten_bring_checkout_deactivate_plugin(): void {
		require_once plugin_dir_path( __FILE__ ) . '/includes/Activation.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/CheckoutApi.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/CredentialsService.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/models/Credentials.php';
		Activation::deactivate();
	}

	/**
	 * Uninstall callback
	 *
	 * Will run the necessary uninstallation tasks
	 *
	 * @since 0.0.1
	 *
	 * @see Activation::uninstall()
	 */
	function posten_bring_checkout_uninstall_plugin(): void {
		require_once plugin_dir_path( __FILE__ ) . '/includes/Activation.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/CheckoutApi.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/CredentialsService.php';
		require_once plugin_dir_path( __FILE__ ) . '/admin/bring/models/Credentials.php';
		Activation::uninstall();
	}

	register_activation_hook(
		__FILE__,
		'posten_bring_checkout_activate_plugin'
	);

	register_deactivation_hook(
		__FILE__,
		'posten_bring_checkout_deactivate_plugin'
	);

	register_uninstall_hook(
		__FILE__,
		'posten_bring_checkout_uninstall_plugin'
	);

	require plugin_dir_path( __FILE__ ) . '/includes/Main.php';

	/**
	 * Main function
	 *
	 * This function will initialize the plugin and connect to all the required hooks.
	 *
	 * @since 0.0.1
	 *
	 * @see Main::run()
	 */
	function posten_bring_checkout_run(): void {
		$plugin = new Main();
		$plugin->run();
	}

	function posten_bring_checkout_load_integrations(): void {
		if ( class_exists( 'WC_Integration' ) ) {
			require_once plugin_dir_path(__FILE__) . '/admin/bring/integration/PostenBringCheckoutIntegration.php';
			add_filter( 'woocommerce_integrations', 'posten_bring_checkout_load_integration' );
		}
	}

	function posten_bring_checkout_load_integration( $integrations ) {
		array_unshift( $integrations, 'PostenBringCheckout\admin\bring\integration\PostenBringCheckoutIntegration' );
		return $integrations;
	}

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

	add_action( 'woocommerce_init', 'posten_bring_checkout_run' );
	add_action( 'plugins_loaded', 'posten_bring_checkout_load_integrations' );
}
