<?php
/**
 * Plugin Name: Advanced Order Status For WooCommerce
 * Plugin URI: https://versesofts.com/advanced-order-status-for-woocommerce
 * Description: Create, customize, and manage custom order statuses in WooCommerce with ease. Improve your order management workflow with color-coded labels and Dashicons.
 * Version: 1.2.2
 * Tags: custom order status, woocommerce order status, order management, custom status icons, order workflow
 * Author: VerseSofts
 * Author URI: https://versesofts.com
 * Author Email: contact@versesofts.com
 * Tested up to: 7.0
 * Text Domain: advanced-order-status-for-woocommerce
 * Requires Plugins: woocommerce
 * WC requires at least: 4.0
 * WC tested up to: 9.7
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

defined('ABSPATH') || exit;

define('AOSFW_URL', plugin_dir_url(__FILE__));
define('AOSFW_PATH', plugin_dir_path(__FILE__));
define('AOSFW_FILE', __FILE__);
define('AOSFW_VERSION', '1.2.1');

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

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

            $aosfw_fs = fs_dynamic_init( array(
                'id'                  => '22253',
                'slug'                => 'advanced-order-status-for-woocommerce',
                'type'                => 'plugin',
                'public_key'          => 'pk_05e07733a52f45737164dcb464767',
                'is_premium'          => false,
                'has_addons'          => false,
                'has_paid_plans'      => false,
                'is_org_compliant'    => true,
                'menu'                => array(
                    'first-path'     => 'plugins.php',
                    'account'        => false,
                    'support'        => false,
                ),
            ) );
        }

        return $aosfw_fs;
    }

    // Init Freemius.
    aosfw_fs();
    
    // Signal that SDK was initiated.
    do_action( 'aosfw_fs_loaded' );
}

if ( ! class_exists( 'AOSFW_Main' ) ) {

    class AOSFW_Main {

        public function __construct() {

            if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')), true) ) {

                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, 'aosfw_settings_link' ));
                add_filter( 'plugin_row_meta', array( $this, 'aosfw_plugin_description_links' ), 10, 2 );
            } else {
                add_action('admin_notices', array( $this, 'aosfw_inactive_plugin_notice' ));
            }
        }

        public function includes() {
            include_once AOSFW_PATH . '/includes/class-aosfw-register.php';
            include_once AOSFW_PATH . '/includes/class-aosfw-api.php';
        }

        public function aosfw_settings_link( $links ) {
            $url = esc_url(add_query_arg('page', 'advanced-order-status-for-woocommerce', 'admin.php'));
            $settings_link = sprintf('<a href="%s">%s</a>', $url, __('Settings', 'advanced-order-status-for-woocommerce'));
            array_unshift($links, $settings_link);
            return $links;
        }

        public function aosfw_plugin_description_links( $links, $file ) {

            if ( $file !== plugin_basename( __FILE__ ) ) {
                return $links;
            }

            $links[] = '<a href="https://versesofts.com/docs/advanced-order-status" target="_blank">' . esc_html__('Documentation', 'advanced-order-status-for-woocommerce') . '</a>';
            $links[] = '<a href="https://versesofts.com/feature-request?plugin=advanced-order-status" target="_blank">' . esc_html__('Feature Request', 'advanced-order-status-for-woocommerce') . '</a>';
            $links[] = '<a href="https://versesofts.com/" target="_blank">' . esc_html__('Website', 'advanced-order-status-for-woocommerce') . '</a>';

            return $links;
        }

        public function aosfw_inactive_plugin_notice() {
            ?>
            <div class="notice notice-error">
                <p>
                    <?php
                    printf(
                        /* translators: %s: Order status name */
                        esc_html__('Advanced Order For WooCommerce requires %s to be installed and active.', 'advanced-order-status-for-woocommerce'),
                        '<a href="https://wordpress.org/plugins/woocommerce/" target="_blank">WooCommerce</a>'
                    );
                    ?>
                </p>
            </div>
            <?php
        }
    }

    new AOSFW_Main();

}
