<?php
/**
 * Plugin Name: Subaccounts for WooCommerce
 * Plugin URI: https://subaccounts.pro/
 * Description: Subaccounts for WooCommerce allows the creation of subaccounts for your WooCommerce customers and subscribers.
 * Version: 2.0.0
 * Author: Mediaticus
 * Author URI: https://subaccounts.pro/
 * Update URI: https://wordpress.org/plugins/subaccounts-for-woocommerce/
 *
 * Text Domain: subaccounts-for-woocommerce
 * Domain Path: /languages/
 *
 * Requires at least: 5.7
 * Tested up to: 7.0
 *
 * WC tested up to: 10.7.0
 * Requires PHP: 5.7
 *
 * Copyright 2022 Mediaticus
 *
 * License: GPL3
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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




if ( ! function_exists( 'sfwc_free' ) ) {
    // Create a helper function for easy SDK access.
    function sfwc_free() {
        global $sfwc_free;

        if ( ! isset( $sfwc_free ) ) {
            // Include Freemius SDK.
            require_once dirname(__FILE__) . '/freemius/start.php';

            $sfwc_free = fs_dynamic_init( array(
                'id'				=> '10450',
                'slug'				=> 'subaccounts-for-woocommerce',
                'type'				=> 'plugin',
                'public_key'		=> 'pk_5e73c22e9eb9062ca988afae26a46',
                'is_premium'		=> false,
                'has_addons'		=> true,
                'has_paid_plans'	=> false,
				'navigation'		=> 'tabs',
                'menu'				=> array(
                    'slug'			=> 'subaccounts',
                    'account'		=> true,
                    'support'		=> false,
                    'parent'		=> array(
                        'slug'		=> 'woocommerce',
                    ),
                ),
            ) );
        }

        return $sfwc_free;
    }

    // Init Freemius.
    sfwc_free();
	
    // Signal that SDK was initiated.
    do_action( 'sfwc_free_loaded' );
	
	/**
	 * Enable cpt advanced menu logic (for Freemius tabbed nav).
	 *
	 * Since Freemius SDK v2.11.0.
	 * 
	 * It will try to detect the parent slug smartly, instead of assuming it to be admin.php.
	 *
	 * Right now we consider this feature beta, hence it is put behind the custom filter.
	 * Once we have tested it across many use cases, we will consider removing the filter.
	 */
	sfwc_free()->add_filter( 'enable_cpt_advanced_menu_logic', '__return_true' );
}




if ( ! defined( 'SFWC_CURRENT_VERSION' ) ) {
	define( 'SFWC_CURRENT_VERSION', '2.0.0' ); // MAJOR.MINOR.PATCH
}




/**
 * Declare plugin compatible with High-Performance Order Storage (HPOS) feature.
 *
 * @since 1.2.0
 */
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 );
	}
} );




/**
 * Declare plugin compatible with WooCommerce Cart and Checkout Blocks feature.
 *
 * @since 1.9.0
 */
add_action( 'before_woocommerce_init', function() {
    if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
    }
} );




/**
 * Check Plugin Requirements.
 *
 * Custom function to get list of active plugins. Unlike is_plugin_active WordPress function,
 * this one is available also on frontend.
 */
if ( ! function_exists( 'sfwc_is_plugin_active' ) ) {  // Check if function already exists from Pro plugin to avoid issues in case the Pro is activated first and then Free plugin.
	
	function sfwc_is_plugin_active( $plugin_name ) {
		
		// Get list of active plugins. In case of multisite, this will return a list of active plugins for the current sub-site.
		$active_plugins = (array) get_option( 'active_plugins', array() );
		
		// In case of multisite, get also network active plugins.
		$active_plugins_network = get_site_option('active_sitewide_plugins');

		$plugin_filenames = array();
		
		// Loop through site active plugins.
		foreach ( $active_plugins as $plugin ) {

			if ( false !== strpos( $plugin, '/' ) ) {

				// Normal plugin name (plugin-dir/plugin-filename.php).
				list( , $filename ) = explode( '/', $plugin );

			} else {

				// No directory, just plugin file.
				$filename = $plugin;
			}

			$plugin_filenames[] = $filename;
		}
		
		// Loop through network active plugins.
		if ( ! empty( $active_plugins_network ) ) {
			
			foreach( $active_plugins_network as $key => $value ) {
				
				if ( false !== strpos( $key, '/' ) ) {

					// Normal plugin name (plugin-dir/plugin-filename.php).
					list( , $filename ) = explode( '/', $key );

				} else {

					// No directory, just plugin file.
					$filename = $key;
				}

				$plugin_filenames[] = $filename;
			}
		}
		
		return in_array( $plugin_name, $plugin_filenames );
	}
}




/**
 * Check Plugin Requirements.
 *
 * Add admin notice in case WooCommerce is not active.
 */
if ( ! sfwc_is_plugin_active( 'woocommerce.php' ) ) {

	add_action( 'admin_notices', 'sfwc_child_plugin_notice_woocommerce_not_active', -50 );
	return;
}




/**
 * Check Plugin Requirements.
 *
 * Echo admin notice in case WooCommerce is not active.
 */
function sfwc_child_plugin_notice_woocommerce_not_active() {
	
	$action = 'install-plugin';
	$slug = 'woocommerce';

	$wc_download_url = wp_nonce_url(
		add_query_arg(
			array(
				'action' => $action,
				'plugin' => $slug
			),
			admin_url( 'update.php' )
		),
		$action.'_'.$slug
	);

    echo '<div class="notice notice-info" style="display:flex; flex-direction:row; align-items:center; padding:20px;">';
	echo '<img id="sfwc-symbol" src="' . esc_url( WP_PLUGIN_URL ) . '/subaccounts-for-woocommerce/assets/images/sfwc_symbol.svg" width="80" height="80" style="margin-right:15px;">';
	echo '<div><h2 style="margin: 0 0 10px 0;">' . esc_html__( 'Thanks for installing Subaccounts for WooCommerce!', 'subaccounts-for-woocommerce' ) . '</h2>';
	printf( esc_html__( '%1$s Subaccounts for WooCommerce needs WooCommerce to be installed and activated before you can use it.', 'subaccounts-for-woocommerce' ), '<strong>Almost ready to launch! 🚀</strong>' );
	echo '<p><a href="' . esc_url( $wc_download_url ) . '">' . esc_html__( 'Install WooCommerce', 'subaccounts-for-woocommerce' ) . '</a></p>';
    echo '</div></div>';
}




/**
 * Load plugin textdomain for translations.
 */
function sfwc_load_textdomain() {
    load_plugin_textdomain( 'subaccounts-for-woocommerce', false, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action('init', 'sfwc_load_textdomain');




/**
 * Load files.
 */
if ( is_admin() ) {
	
	if ( ! wp_doing_ajax() ) {
		
		// Admin area (non-ajax functions).
		require_once( plugin_dir_path( __FILE__ ) . 'admin/admin.php' );
		
	} else {
		
		// Ajax functions, both admin and public area.
		require_once( plugin_dir_path( __FILE__ ) . 'admin/ajax.php' );
	}
	
} else {
	
	// Public area.
	require_once( plugin_dir_path( __FILE__ ) . 'public/my-account.php' );

}

require_once( plugin_dir_path( __FILE__ ) . 'includes/functions.php' );

// Third party integration stuff.
require_once( plugin_dir_path( __FILE__ ) . 'includes/third-party/hosting/pantheon.php' );
require_once( plugin_dir_path( __FILE__ ) . 'includes/third-party/plugins/user-switching-by-john-blackbourn.php' );
require_once( plugin_dir_path( __FILE__ ) . 'includes/third-party/plugins/wc4bp-premium-by-themekraft.php' );