<?php

/*
 * Plugin Name: Shipped - WooCommerce Table Rate Shipping
 * Plugin URI: https://wordpress.org/plugins/table-rate-shipping-rates
 * Description: Create multiple shipping rates & handling fees based on product rules and cart conditions
 * Version: 1.5.4
 * Author: zendcrew
 * Author URI: https://codecanyon.net/user/zendcrew/portfolio?ref=zendcrew
 * Text Domain: table-rate-shipping-rates
 * Domain Path: /languages/
 * Requires at least: 5.8
 * Requires PHP: 5.6
 * WC requires at least: 5.6
 * License: GPL v2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * 
 * Tested up to: 7.0
 * WC tested up to: 10.8
 * 
 * Requires Plugins: woocommerce
 */

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

if ( is_admin() ) {

    require_once (dirname( __FILE__ ) . '/framework/reon_loader.php');
}

if ( !defined( 'WTARS_SHIPPED_FILE' ) ) {

    define( 'WTARS_SHIPPED_FILE', __FILE__ );
}

if ( !defined( 'WTARS_SHIPPED_METHOD_ID' ) ) {

    define( 'WTARS_SHIPPED_METHOD_ID', 'wtars_shipped' );
}

if ( !defined( 'WTARS_SHIPPED_OPTION_NAME' ) ) {

    define( 'WTARS_SHIPPED_OPTION_NAME', 'wtars_shipped' );
}

if ( !defined( 'WTARS_SHIPPED_VERSION' ) ) {

    define( 'WTARS_SHIPPED_VERSION', '1.5' );
}

if ( !class_exists( 'WTARS_Shipped_Main' ) ) {

    class WTARS_Shipped_Main {

        public function __construct() {

            add_action( 'plugins_loaded', array( $this, 'init' ), 1 );

            add_action( 'before_woocommerce_init', array( $this, 'before_woocommerce_init' ) );

            add_action( 'init', array( $this, 'load_textdomain' ) );
            
            add_filter( 'wp_plugin_dependencies_slug', array( $this, 'get_dependency_slug' ) );
        }
        
        public function load_textdomain() {

            load_plugin_textdomain( 'table-rate-shipping-rates', false, dirname( plugin_basename( WTARS_SHIPPED_FILE ) ) . '/languages/' );
        }

        public function init() {

            if ( function_exists( 'WC' ) ) { // Check if WooCommerce is active
                
                require_once 'extensions/extensions.php';
            } else {

                add_action( 'admin_notices', array( $this, 'missing_notice' ) );
            }
        }

        public function before_woocommerce_init() {

            // Check for HPOS
            if ( !class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {

                return;
            }

            // Adds support for HPOS
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', WTARS_SHIPPED_FILE, true );
        }

        public function missing_notice() {

            echo '<div class="error"><p><strong>' . esc_html__( 'WooCommerce Table Rate Shipping requires WooCommerce to be installed and activated.', 'table-rate-shipping-rates' ) . '</strong></p></div>';
        }
        
        public function get_dependency_slug( $slug ) {

            $dep_slug = '{{shipped}}';

            if ( $dep_slug != $slug ) {

                return $slug;
            }

          return str_replace( $dep_slug, 'table-rate-shipping-rates', $slug );
        }

    }

    new WTARS_Shipped_Main();
}

