<?php
/**
 * Plugin Name: Payments Square Connect
 * Description: Square Hosted Checkout for WooCommerce and Contact Form 7 — one-time payments, subscriptions, checkout settings, OAuth, and webhooks.
 * Version: 1.1.5
 * Author: WPPayments
 * Text Domain: payments-connect-square
 * 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' ) ) {
	/**
	 * Create a helper function for easy SDK access.
	 */
	function psc_fs() {
		global $psc_fs;

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

			require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

			$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',
						'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;
	}

	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
	);
}

define( 'SHC4WC_VERSION', '1.1.5' );
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-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';

add_action(
	'plugins_loaded',
	static function () {
		SHC4WC_Plugin::instance();
		SHC4WC_Connection_Settings::instance();
		SHC4WC_Checkout_Settings::instance();
		SHC4WC_Subscription_Settings::instance();
		SHC4WC_Support_Settings::instance();
		SHC4WC_Subscriptions::register_product_fields();

		if ( class_exists( 'WooCommerce' ) && class_exists( 'WC_Payment_Gateway' ) ) {
			require_once SHC4WC_PLUGIN_DIR . 'includes/class-shc4wc-gateway.php';
		}

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