<?php
/**
 * Plugin Name: Payments Square Connect – Square Hosted Checkout for WooCommerce, Forms & Pay Links
 * Description: Square hosted checkout for WooCommerce, PMPro memberships, WPForms, Fluent Forms & CF7. Apple Pay, Google Pay, zero fees.
 * Version: 1.3.33
 * Author: WPPayments
 * Text Domain: payments-connect-square
 * Domain Path: /languages
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 *
 * @package SHC4WC
 */

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

if ( defined( 'SHC4WC_VERSION' ) ) {
	return;
}

if ( ! function_exists( 'psc_fs' ) ) {
	/**
	 * Path to the bundled Freemius SDK bootstrap file.
	 *
	 * @return string
	 */
	function psc_fs_sdk_path() {
		return dirname( __FILE__ ) . '/vendor/freemius/start.php';
	}

	/**
	 * Whether the bundled Freemius SDK is present on disk.
	 *
	 * @return bool
	 */
	function psc_fs_sdk_exists() {
		return is_readable( psc_fs_sdk_path() );
	}

	/**
	 * Create a helper function for easy SDK access.
	 *
	 * @return Freemius|false Freemius instance, or false when the SDK is missing.
	 */
	function psc_fs() {
		global $psc_fs;

		if ( ! isset( $psc_fs ) ) {
			$psc_fs = false;

			if ( ! psc_fs_sdk_exists() ) {
				return false;
			}

			if ( ! defined( 'WP_FS__PRODUCT_32049_MULTISITE' ) ) {
				define( 'WP_FS__PRODUCT_32049_MULTISITE', true );
			}

			require_once psc_fs_sdk_path();

			$psc_fs = fs_dynamic_init(
				array(
					'id'               => '32049',
					'slug'             => 'payments-square-connect',
					'type'             => 'plugin',
					'public_key'       => 'pk_638a4f065d7930a06f16abd042528',
					'is_premium'       => false,
					'has_addons'       => false,
					'has_paid_plans'   => false,
					'is_org_compliant' => true,
					'menu'             => array(
						'slug'       => 'payments-square-connect',
						'first-path' => 'admin.php?page=payments-square-connect-onboarding',
						'account'    => false,
						'contact'    => false,
						'support'    => false,
						'affiliation'=> false,
						'pricing'    => false,
						'addons'     => false,
					),
				)
			);

			// Custom Support page handles email, WhatsApp, and WP.org forum — hide Freemius duplicate.
			$psc_fs->add_filter(
				'is_submenu_visible',
				static function ( $is_visible, $menu_id ) {
					if ( 'support' === $menu_id ) {
						return false;
					}
					return $is_visible;
				},
				10,
				2
			);
		}

		return $psc_fs;
	}

	if ( psc_fs() ) {
		do_action( 'psc_fs_loaded' );

		add_action(
			'admin_menu',
			static function () {
				remove_submenu_page( 'payments-square-connect', 'payments-square-connect-wp-support-forum' );

				global $submenu;

				$parent = 'payments-square-connect';
				if ( empty( $submenu[ $parent ] ) || ! is_array( $submenu[ $parent ] ) ) {
					return;
				}

				foreach ( $submenu[ $parent ] as $index => $item ) {
					if ( isset( $item[2] ) && false !== strpos( (string) $item[2], 'wp-support-forum' ) ) {
						unset( $submenu[ $parent ][ $index ] );
					}
				}
			},
			PHP_INT_MAX
		);
	} else {
		add_action(
			'admin_notices',
			static function () {
				if ( ! current_user_can( 'manage_options' ) || psc_fs_sdk_exists() ) {
					return;
				}

				echo '<div class="notice notice-warning"><p>';
				echo esc_html__(
					'Payments Square Connect is missing its Freemius SDK files (vendor/freemius). Square payments still work, but you should reinstall the plugin from WordPress.org to restore the complete package.',
					'payments-connect-square'
				);
				echo '</p></div>';
			}
		);
	}
}

define( 'SHC4WC_VERSION', '1.3.33' );
define( 'SHC4WC_PLUGIN_FILE', __FILE__ );
define( 'SHC4WC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'SHC4WC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Default OAuth relay for Payments Square Connect (all installs).
 * Override in wp-config.php: define( 'SHC4WC_OAUTH_WORKER_BASE', 'https://…' );
 */
if ( ! defined( 'SHC4WC_DEFAULT_OAUTH_WORKER_BASE' ) ) {
	define( 'SHC4WC_DEFAULT_OAUTH_WORKER_BASE', 'https://shc4wc-square-oauth.wordpress-ingenious.workers.dev' );
}
if ( ! defined( 'SHC4WC_OAUTH_WORKER_BASE' ) ) {
	define( 'SHC4WC_OAUTH_WORKER_BASE', SHC4WC_DEFAULT_OAUTH_WORKER_BASE );
}
if ( ! defined( 'SHC4WC_OAUTH_WORKER_REG_SECRET' ) ) {
	define( 'SHC4WC_OAUTH_WORKER_REG_SECRET', '' );
}

require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-api.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-refunds.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-oauth-token.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-catalog.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-inventory-sync.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-oauth-return-state.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-webhook.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-plugin.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-admin-menu.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-connection-settings.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-checkout-settings.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-subscriptions.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-subscription-settings.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-support-settings.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-docs.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-docs-settings.php';

require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-onboarding.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-rest-api.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-ai-assistant.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-customer-sync.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-hosted-checkout-flow.php';
require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-deactivation-feedback.php';

/**
 * Load plugin translations (WordPress.org language packs + /languages).
 *
 * @return void
 */
function shc4wc_load_textdomain() {
	load_plugin_textdomain(
		'payments-connect-square',
		false,
		dirname( plugin_basename( SHC4WC_PLUGIN_FILE ) ) . '/languages'
	);
}
add_action( 'init', 'shc4wc_load_textdomain', 0 );

add_action(
	'plugins_loaded',
	static function () {
		SHC4WC_OAuth_Token::register_hooks();
		SHC4WC_OAuth_Token::maybe_schedule_cron();
		SHC4WC_Customer_Sync::instance();
		SHC4WC_Plugin::maybe_migrate_environment_storage();
		SHC4WC_Plugin::instance();
		SHC4WC_Onboarding::instance();
		SHC4WC_REST_API::instance();
		SHC4WC_AI_Assistant::instance();
		SHC4WC_Connection_Settings::instance();
		SHC4WC_Checkout_Settings::instance();
		SHC4WC_Subscription_Settings::instance();
		SHC4WC_Support_Settings::instance();
		SHC4WC_Docs_Settings::instance();
		SHC4WC_Deactivation_Feedback::instance();
		SHC4WC_Subscriptions::register_product_fields();

		require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-payment-link.php';
		SHC4WC_Payment_Link::instance();

		require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-page-builders.php';
		SHC4WC_Page_Builders::instance();

		if ( class_exists( 'WooCommerce' ) && class_exists( 'WC_Payment_Gateway' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-gateway.php';
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-pay-for-order.php';
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-payment-reminders.php';
			SHC4WC_Pay_For_Order::instance();
			SHC4WC_Payment_Reminders::instance();
		}

		if ( defined( 'WPCF7_VERSION' ) || class_exists( 'WPCF7' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-cf7.php';
			SHC4WC_CF7::instance();
		}

		if ( function_exists( 'wpforms' ) || defined( 'WPFORMS_VERSION' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-wpforms.php';
			SHC4WC_WPForms::instance();
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-wpforms-refunds.php';
			SHC4WC_WPForms_Refunds::instance();
		}

		if ( defined( 'FLUENTFORM' ) || defined( 'FLUENTFORM_VERSION' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-fluentforms.php';
			SHC4WC_FluentForms::instance();
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-fluentforms-refunds.php';
			SHC4WC_FluentForms_Refunds::instance();
		}

		if ( defined( 'NINJA_FORMS_VERSION' ) || class_exists( 'Ninja_Forms' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-ninja-forms.php';
			SHC4WC_Ninja_Forms::instance();
		}

		if ( class_exists( 'FrmForm' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-formidable.php';
			SHC4WC_Formidable::instance();
		}

		if ( defined( 'METFORM_VERSION' ) || class_exists( '\MetForm\Plugin' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-metform.php';
			SHC4WC_MetForm::instance();
		}

		if ( defined( 'SRFM_VER' ) || class_exists( '\SRFM\Inc\Payments\Front_End' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-sureforms.php';
			SHC4WC_SureForms::instance();
		}

		if ( class_exists( 'Forminator' ) || class_exists( 'Forminator_Core' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-forminator.php';
			SHC4WC_Forminator::instance();
		}

		if ( defined( 'PMPRO_VERSION' ) || class_exists( 'PMProGateway' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-pmpro-plans.php';
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-pmpro-webhook.php';
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-pmpro.php';
			require_once SHC4WC_PLUGIN_DIR . 'includes/gateways/class.pmprogateway_square_connect.php';
			SHC4WC_PMPro::instance();
			add_action( 'init', array( 'PMProGateway_square_connect', 'init' ) );
		}
	},
	20
);

register_activation_hook(
	SHC4WC_PLUGIN_FILE,
	static function () {
		SHC4WC_OAuth_Token::maybe_schedule_cron();
		require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-payment-reminders.php';
		SHC4WC_Payment_Reminders::maybe_schedule_cron();
		require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-customer-sync.php';
		SHC4WC_Customer_Sync::maybe_schedule_cron();
		if ( class_exists( 'SHC4WC_Onboarding' ) ) {
			SHC4WC_Onboarding::activation_redirect();
		}
	}
);

register_deactivation_hook(
	SHC4WC_PLUGIN_FILE,
	static function () {
		SHC4WC_OAuth_Token::unschedule_cron();
		require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-payment-reminders.php';
		SHC4WC_Payment_Reminders::unschedule_cron();
		require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-customer-sync.php';
		SHC4WC_Customer_Sync::unschedule_cron();
	}
);
