<?php
/**
 * Plugin Name: Store Restriction for WooCommerce
 * Plugin URI: https://wordpress.org/plugins/store-restriction-for-woocommerce
 * Description: Store Restriction for WooCommerce transforms your WooCommerce store into a powerful restricted access platform. Whether you're running a wholesale business, B2B marketplace, or members-only shop, our plugin gives you complete control over product visibility and access.
 * Version: 1.4.1
 * Tags: woocommerce, product visibility, restrict products, hide prices, user roles, wholesale, b2b, members only, country restriction, category restriction, private store, catalog visibility
 * Author: Versesofts
 * Author URI: https://versesofts.com
 * Requires at least: 5.0
 * Tested up to: 7.0
 * Requires PHP: 7.0
 * Text Domain: store-restriction-for-woocommerce
 * Requires Plugins: woocommerce
 * WC requires at least: 4.0
 * WC tested up to: 10.3
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

defined('ABSPATH') || exit;

define('SRFW_URL', plugin_dir_url(__FILE__));
define('SRFW_PATH', plugin_dir_path(__FILE__));
define('SRFW_FILE', __FILE__);
define('SRFW_VERSION', '1.4.1');

// Freemius Integration Start (restored)
if ( ! function_exists( 'srfw_fs' ) ) {
	function srfw_fs() {
		global $srfw_fs;

		if ( ! isset( $srfw_fs ) ) {
			require_once dirname( __FILE__ ) . '/freemius/start.php';
			$srfw_fs = fs_dynamic_init( array(
				'id'                  => '18037',
				'slug'                => 'store-restriction-for-woocommerce',
				'type'                => 'plugin',
				'public_key'          => 'pk_a2333623b9fdf718edb46d8ec7dc6',
				'is_premium'          => false,
				'has_addons'          => false,
				'has_paid_plans'      => false,
				'menu'                => array(
                    'first-path'     => 'admin.php?page=wc-settings&tab=srfw',
                    'account'        => false,
                    'contact'        => false,
                    'support'        => false,
                ),
			) );
		}

		return $srfw_fs;
	}

	// Init Freemius.
	srfw_fs();
	// Signal that SDK was initiated.
	do_action( 'srfw_fs_loaded' );
}
// Freemius Integration End



if ( ! class_exists( 'Store_Restriction' ) ) {
	
	class Store_Restriction {

		public function __construct() {
			
			/**
			* Filter active_plugins
			* 
			* @since 1.0
			**/
			// Load only if WooCommerce is available
			if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) || ( is_multisite() && array_key_exists( 'woocommerce/woocommerce.php', get_site_option( 'active_sitewide_plugins', array() ) ) ) ) {

				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);
						}
					} 
				);
				$this->includes();

				add_filter('plugin_action_links_' . plugin_basename(__FILE__), array( $this, 'srfw_settings_link' ));
				add_filter('plugin_row_meta', array( $this, 'srfw_plugin_row_meta' ), 10, 2 );
			} else {
				add_action('admin_notices', array( $this, 'srfw_inactive_plugin_notice' ));
			}
		}
		
		public function srfw_settings_link( $links ) {

			$url = esc_url( add_query_arg( array( 'page' => 'wc-settings', 'tab' => 'srfw' ), 'admin.php' ) );
			$settings_link = sprintf('<a href="%s">%s</a>', $url, __('Settings', 'store-restriction-for-woocommerce'));
			$docs_link = '<a href="http://versesofts.com/docs/store-restriction" target="_blank">' . esc_html__('🚀 Go Pro', 'store-restriction-for-woocommerce') . '</a>';
			array_unshift($links, $settings_link, $docs_link);
			return $links;
		}

		public function srfw_plugin_row_meta( $links, $file ) {
			if ( $file !== plugin_basename( __FILE__ ) ) {
				return $links;
			}
			$links[] = '<a href="https://woocommerce.com/document/conditional-store-restrictions/" target="_blank">' . esc_html__('Documentation', 'store-restriction-for-woocommerce') . '</a>';
			$links[] = '<a href="https://woocommerce.com/feature-requests/conditional-store-restrictions/" target="_blank">' . esc_html__('Feature Request', 'store-restriction-for-woocommerce') . '</a>';
			return $links;
		}
		
		public function includes() {

			include_once SRFW_PATH . '/includes/admin/class-srfw-register.php';
			include_once SRFW_PATH . '/includes/front/class-srfw-front.php';
		}

		public function srfw_inactive_plugin_notice() {
			?>
			<div class="notice notice-error">
				<p>
					<?php
					printf(
						/* translators: %s: WooCommerce */
						esc_html__('Store Restriction requires %s to be installed and active.', 'store-restriction-for-woocommerce'),
						'<a href="https://wordpress.org/plugins/woocommerce/" target="_blank">WooCommerce</a>'
					);
					?>
				</p>
			</div>
			<?php
		}
	}

	new Store_Restriction();
}