<?php

/*
 * Plugin Name: Pricely - Dynamic Pricing & Cart Discounts for WooCommerce
 * Description: <code><strong>Pricely - Dynamic Pricing & Cart Discounts</strong></code> is an all-in-one WooCommerce plugin for dynamic pricing, advanced discounts, bulk pricing, and sales-boosting promotions.
 * Author: sundayfanz
 * Tags: bulk discount, category discount, coupon, discount, woocommerce
 * 
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * 
 * Requires PHP: 7.0
 * Requires at least: 5.9
 * WC requires at least: 5.6
 * 
 * Text Domain: pricely-dynamic-pricing-cart-discounts
 * Domain Path: /languages/
 *
 * Version: 1.0.2
 *
 * 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( 'PRICELY_OPTION_NAME' ) ) {

    define( 'PRICELY_OPTION_NAME', 'pricely_settings' );
}

if ( !defined( 'PRICELY_MENU_SLUG' ) ) {

    define( 'PRICELY_MENU_SLUG', 'pricely-settings' );
}

if ( !defined( 'PRICELY_VERSION' ) ) {

    define( 'PRICELY_VERSION', '1.0' );
}

if ( !defined( 'PRICELY_MAIN_FILE' ) ) {

    define( 'PRICELY_MAIN_FILE', __FILE__ );
}

if ( !class_exists( 'Pricely_Init' ) ) {

    class Pricely_Init {

        public function __construct() {

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

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

        public function plugin_loaded() {

            if ( function_exists( 'WC' ) ) {

                // Check whether WooCommerce is active                
                $this->main();
            } 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', PRICELY_MAIN_FILE, true );


            //Check if HPOS tables are used
            if ( !(\Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled()) ) {

                return;
            }

            //Adds support for HPOS Tables
            if ( !defined( 'PRICELY_USING_HPOS' ) ) {

                define( 'PRICELY_USING_HPOS', true );
            }
        }

        public function missing_notice() {

            $allowed_html = array(
                'div' => array(
                    'class' => true,
                ),
                'p' => array(),
                'strong' => array(),
            );

            $pl_name = esc_html__( 'Pricely - Dynamic Pricing & Cart Discounts for WooCommerce', 'pricely-dynamic-pricing-cart-discounts' );
            $pl_name_wc = esc_html__( 'WooCommerce', 'pricely-dynamic-pricing-cart-discounts' );

            /* translators: 1: plugin name 2: woocomerce name */
            $pl_name_msg = wp_kses( sprintf( __( '<strong>%1$s</strong> requires <strong>%2$s</strong> to be installed and activated.', 'pricely-dynamic-pricing-cart-discounts' ), $pl_name, $pl_name_wc ), $allowed_html );

            echo wp_kses( '<div class="error"><p>' . $pl_name_msg . '</p></div>', $allowed_html );
        }
        
        private function main() {

            //Pricely Main
            if ( !class_exists( 'Pricely_Main' ) ) {

                include_once ('main/main.php');

                Pricely_Main::get_instance();
            }
        }

    }

    new Pricely_Init();
}
