<?php
/**
 * Plugin Name:       Order Role Manager for WooCommerce
 * Description:       Restrict specific user roles from updating order statuses, moving orders to trash, editing notes, billing/shipping data, performing bulk actions, and more!
 * Version:           8.2.0
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            JAD Plugins
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       order-role-manager-for-woocommerce
 */

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

/**
 * Freemius SDK Loader
 */
if ( ! function_exists( 'ormfw_wosr_fs' ) ) {
    function ormfw_wosr_fs() {
        global $ormfw_wosr_fs;

        if ( ! isset( $ormfw_wosr_fs ) ) {
            $freemius_path = __DIR__ . '/vendor/freemius/start.php';

            if ( file_exists( $freemius_path ) ) {
                require_once $freemius_path;

                $ormfw_wosr_fs = fs_dynamic_init( array(
                    'id'                  => '18160',
                    'slug'                => 'order-role-manager-for-woocommerce',
                    'premium_slug'        => 'order-role-manager-for-woocommerce-pro',
                    'type'                => 'plugin',
                    'public_key'          => 'pk_1b57d46d109240ec967efd53bd6fa',
                    'is_premium'          => false,
                    'premium_suffix'      => 'Pro',
                    'has_premium_version' => true,
                    'has_addons'          => false,
                    'has_paid_plans'      => true,
					'is_org_compliant' => true,
                    'menu'                => array(
                        'slug'    => 'wosr-settings',
                        'contact' => false,
                        'support' => false,
                        'parent'  => array(
                            'slug' => 'woocommerce',
                        ),
                    ),
                ) );
            }
        }

        return $ormfw_wosr_fs;
    }

    // Init Freemius.
    ormfw_wosr_fs();
    // Signal that SDK was initiated.
    do_action( 'ormfw_wosr_fs_loaded' );
}

/**
 * Plugin Activation Checks
 */
register_activation_hook( __FILE__, 'wosr_free_on_activate' );

function wosr_free_on_activate() {
    // Load plugin functions if not already loaded.
    if ( ! function_exists( 'is_plugin_active' ) ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

    // Check if Pro version is active.
    $pro_plugin = 'order-role-manager-for-woocommerce-pro/order-role-manager-for-woocommerce-pro.php';

    if ( is_plugin_active( $pro_plugin ) ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die(
            '<p><strong>Order Role Manager for WooCommerce (Free)</strong> cannot be activated while the <strong>Pro version</strong> is active.</p><p><a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">Return to Plugins</a></p>',
            'Plugin Activation Conflict',
            array( 'back_link' => false )
        );
    }

    // Check for WooCommerce dependency.
    if ( ! class_exists( 'WooCommerce' ) ) {
        deactivate_plugins( plugin_basename( __FILE__ ) );
        wp_die(
            '<p><strong>Order Role Manager for WooCommerce</strong> requires WooCommerce to be installed and active.</p><p><a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">Return to Plugins</a></p>',
            'Missing Dependency',
            array( 'back_link' => false )
        );
    }
}

/**
 * Self-deactivate if WooCommerce is deactivated
 */
add_action( 'deactivated_plugin', 'wosr_schedule_self_deactivation', 10, 2 );

function wosr_schedule_self_deactivation( $plugin, $network_deactivating ) {
    if ( $plugin === 'woocommerce/woocommerce.php' ) {
        add_action( 'shutdown', 'wosr_self_deactivate_after_wc' );
    }
}

function wosr_self_deactivate_after_wc() {
    deactivate_plugins( plugin_basename( __FILE__ ) );
}

/**
 * Plugin Constants
 */
define( 'WOSR_PLUGIN_VERSION', '8.2.0' );
define( 'WOSR_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'WOSR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Includes
 */
require_once WOSR_PLUGIN_PATH . 'includes/class-wosr-activator.php';
require_once WOSR_PLUGIN_PATH . 'includes/class-wosr-admin.php';
require_once WOSR_PLUGIN_PATH . 'includes/cache.php';

// Load restriction modules.
foreach ( glob( WOSR_PLUGIN_PATH . 'includes/restrictions/class-wosr-*.php' ) as $file ) {
    require_once $file;
}

// Load functional modules.
foreach ( glob( WOSR_PLUGIN_PATH . 'includes/functions/class-wosr-*.php' ) as $file ) {
    require_once $file;
}

/**
 * Add "Settings" link to Plugins list
 */
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wosr_plugin_action_links' );
function wosr_plugin_action_links( $links ) {
    $settings_link = '<a href="admin.php?page=wosr-settings">' . esc_html__( 'Settings', 'order-role-manager-for-woocommerce' ) . '</a>';
    array_unshift( $links, $settings_link );
    return $links;
}

/**
 * Activation Hook for initial setup
 */
register_activation_hook( __FILE__, array( 'WOSR_Activator', 'install' ) );
