<?php

/**
 * Plugin Name:       Softminal B2B Suite for WooCommerce – Wholesale Prices, B2B Store & Bulk Orders
 * Plugin URI:        https://github.com/softminal/softminal-b2b-wholesale-for-woocommerce
 * Description:       Real B2B & wholesale for WooCommerce: customer-group pricing, hidden prices, trade registration and approval, quantity rules, and a bulk order form.
 * Version:           1.3.2
 * Author:            Softminal
 * Author URI:        https://softminal.com
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       softminal-b2b-wholesale-for-woocommerce
 * Domain Path:       /languages
 * Requires at least: 6.5
 * Requires PHP:      8.1
 * Requires Plugins:  woocommerce
 * WC requires at least: 8.0
 * WC tested up to:   10.9
 */

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

require_once plugin_dir_path(__FILE__) . 'config/constants.php';

/*
 * PHP-version gate (FEATURES H7). This runs BEFORE the autoloader, using only
 * syntax that parses on PHP 5.6+, because every class this plugin loads uses
 * PHP 8.1 features that would cause a parse fatal on an older runtime. Showing
 * a notice and returning keeps the site up instead.
 */
if (version_compare(PHP_VERSION, SMB2B_MIN_PHP, '<')) {
    add_action('admin_notices', 'smb2b_php_version_notice');

    /**
     * @return void
     */
    function smb2b_php_version_notice() {
        if (!current_user_can('activate_plugins')) {
            return;
        }
        echo '<div class="notice notice-error"><p>';
        echo esc_html(sprintf(
            /* translators: 1: required PHP version, 2: current PHP version */
            __('Softminal B2B & Wholesale for WooCommerce needs PHP %1$s or newer. Your site is running PHP %2$s, so the plugin is paused.', 'softminal-b2b-wholesale-for-woocommerce'),
            SMB2B_MIN_PHP,
            PHP_VERSION
        ));
        echo '</p></div>';
    }

    return;
}

require_once SMB2B_DIR . 'vendor/autoload.php';

use SoftminalB2B\App;
use SoftminalB2B\Cli\BenchmarkCommand;
use SoftminalB2B\Cli\FixtureCommand;
use SoftminalB2B\Cli\SeedCommand;
use SoftminalB2B\Cli\VerifyCommand;
use SoftminalB2B\Install;
use SoftminalB2B\Services\Compat;
use SoftminalB2B\Services\Database;
use SoftminalB2B\Services\Requirements;

Database::init(__FILE__);

Compat::register(__FILE__);

function smb2b_init() {
    if (!Requirements::met()) {
        return;
    }

    // Catches plugins updated by file copy, which never fire the activation hook.
    Install::maybeUpgrade();

    $app = App::getInstance();
    $app->run();
}
add_action('plugins_loaded', 'smb2b_init');

if (defined('WP_CLI') && WP_CLI) {
    foreach ([FixtureCommand::class, SeedCommand::class, VerifyCommand::class, BenchmarkCommand::class] as $smb2b_cli_command) {
        if (class_exists($smb2b_cli_command)) {
            $smb2b_cli_command::register();
        }
    }
}
