<?php
/**
 * Plugin Name:       OrderCarry Order Automation for WooCommerce
 * Description:       Send every WooCommerce order to Google Sheets, a webhook or a custom email. Unlimited orders, no monthly fees, no task limits.
 * Version:           1.0.3
 * Author:            Houssem Remili
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       ordercarry-order-automation-for-woocommerce
 * Domain Path:       /languages
 * Requires at least: 6.4
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * WC requires at least: 8.3
 * WC tested up to:   10.6
 *
 * Removed from the free build by Freemius, so wordpress.org never ships
 * locked code (guideline 5, no trialware). Every reference to these classes
 * is class_exists() guarded, so the free version runs without them.
 *
 * The compiled German goes too. wordpress.org builds and delivers those from
 * translate.wordpress.org, and bundling a copy is flagged in review; Freemius
 * gets no language packs, so the Pro build has to carry its own. The .pot
 * stays in both -- it is a template for translators, not a translation.
 *
  *
 * @package Ordsync
 */

defined( 'ABSPATH' ) || exit;

/*
 * Freemius's documented guard against the free and Pro zip both being
 * active at once. ordsync_fs() already existing means a sibling copy of
 * this plugin loaded first in this same request; the only safe thing left
 * to do is register OUR basename with it and stop -- redeclaring every
 * constant, class and hook a second time would fatal with "Cannot
 * redeclare class Ordsync_Plugin". set_basename() is what lets Freemius
 * then auto-deactivate the redundant copy.
 */
if ( function_exists( 'ordsync_fs' ) ) {

	ordsync_fs()->set_basename( true, __FILE__ );

} else {

	define( 'ORDSYNC_VERSION', '1.0.0' );
	define( 'ORDSYNC_FILE', __FILE__ );
	define( 'ORDSYNC_PATH', plugin_dir_path( __FILE__ ) );
	define( 'ORDSYNC_URL', plugin_dir_url( __FILE__ ) );
	define( 'ORDSYNC_SLUG', 'ordercarry-order-automation-for-woocommerce' );

	// Freemius must initialise before anything else touches licensing.
	require_once ORDSYNC_PATH . 'includes/freemius-init.php';
	ordsync_fs();

	if ( ordsync_fs() ) {
		ordsync_fs()->set_basename( false, __FILE__ );
	}

	/**
	 * Tell WooCommerce we speak HPOS and Blocks.
	 * Must run before WooCommerce boots.
	 */
	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 );
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
			}
		}
	);

	/**
	 * Boot.
	 */
	add_action(
		'plugins_loaded',
		function () {
			if ( ! class_exists( 'WooCommerce' ) ) {
				add_action(
					'admin_notices',
					function () {
						if ( ! current_user_can( 'activate_plugins' ) ) {
							return;
						}
						echo '<div class="notice notice-error"><p>';
						esc_html_e(
							'OrderCarry requires WooCommerce to be installed and active.',
							'ordercarry-order-automation-for-woocommerce'
						);
						echo '</p></div>';
					}
				);
				return;
			}

			require_once ORDSYNC_PATH . 'includes/class-ordsync-plugin.php';
			Ordsync_Plugin::instance();
		}
	);

	/**
	 * On activation, make sure a signing secret exists.
	 */
	register_activation_hook(
		__FILE__,
		function () {
			require_once ORDSYNC_PATH . 'includes/class-ordsync-settings.php';
			Ordsync_Settings::maybe_create_secret();
		}
	);

} // end function_exists( 'ordsync_fs' ) guard
