<?php

/**
 * @package cwpt
 * @author WPSAAD
 * @link https://wpsaad.com
 * @since 1.0.0
 */
/**
 * Plugin Name: Custom Product Type for WooCommerce (Add-Ons, Data, Options, Booking and Appointments)
 * plugin URI: https://wpsaad.com/product/custom-product-type-for-woocommerce/
 * Description: This Plugin Add WooCommerce Custom Product types with awesome features.
 * Version: 1.4.0
 * Author: WPSAAD
 * Author URI: https://wpsaad.com
 * License: GPLv2 or later
 * Text Domain: custom-product-type-for-woocommerce
 * Domain Path: /languages
 * Requires Plugins: woocommerce
 *  *
 */
defined( 'ABSPATH' ) or die;
if ( function_exists( 'cwpt_fs' ) ) {
    cwpt_fs()->set_basename( false, __FILE__ );
} else {
    // DO NOT REMOVE THIS IF, IT IS ESSENTIAL FOR THE `function_exists` CALL ABOVE TO PROPERLY WORK.
    if ( !function_exists( 'cwpt_fs' ) ) {
        // Create a helper function for easy SDK access.
        function cwpt_fs() {
            global $cwpt_fs;
            if ( !isset( $cwpt_fs ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/freemius/start.php';
                $cwpt_fs = fs_dynamic_init( array(
                    'id'               => '7849',
                    'slug'             => 'custom-product-type-for-wooCommerce',
                    'type'             => 'plugin',
                    'navigation'       => 'tabs',
                    'public_key'       => 'pk_ccdd126fcd113a8aa9aca59e4e684',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Premium',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'menu'             => array(
                        'slug'   => 'cwpt-settings',
                        'parent' => array(
                            'slug' => 'edit.php?post_type=custom-product-type',
                        ),
                    ),
                    'is_live'          => true,
                    'is_org_compliant' => true,
                ) );
            }
            return $cwpt_fs;
        }

        // Init Freemius.
        cwpt_fs();
        // Signal that SDK was initiated.
        do_action( 'cwpt_fs_loaded' );
    }
    // Load modular class files
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-settings.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-admin-scripts.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-front-scripts.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-admin-tabs.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-front-fields.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-templates.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-cart-order.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-product-types.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-polylang.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-core.php';
    require_once plugin_dir_path( __FILE__ ) . 'includes/cwpt-theme.php';
    /**
     * Main CWPT_Plugin Class
     * 
     * Delegates all functionality to modular handler classes.
     * No business logic is contained here - only hooks are registered.
     */
    class CWPT_Plugin {
        /**
         * Initialize plugin
         */
        public function __construct() {
            // Core hooks
            register_activation_hook( __FILE__, ['CWPT_Core', 'install'] );
            add_action( 'init', [$this, 'init_plugin'] );
            add_action( 'admin_init', ['CWPT_Core', 'init_icon_select'] );
            add_action( 'woocommerce_loaded', ['CWPT_Core', 'load'], 99 );
            // Scripts hooks
            add_action( 'admin_enqueue_scripts', ['CWPT_Admin_Scripts', 'enqueue'] );
            add_action( 'wp_enqueue_scripts', ['CWPT_Front_Scripts', 'enqueue'] );
            // Product type hooks
            add_filter( 'product_type_selector', ['CWPT_Product_Types', 'add_type'] );
            add_filter(
                'woocommerce_product_class',
                ['CWPT_Product_Types', 'set_product_class'],
                10,
                2
            );
            add_action( 'woocommerce_product_query', ['CWPT_Product_Types', 'hide_in_shop'] );
            // Template hooks
            add_filter( 'body_class', ['CWPT_Templates', 'add_wc_class'] );
            add_shortcode( 'cwpt_products', ['CWPT_Templates', 'display_products'] );
            add_filter( 'template_include', ['CWPT_Templates', 'override_template'], 50 );
            // Admin product tab hooks
            add_filter( 'woocommerce_product_data_tabs', ['CWPT_Admin_Tabs', 'add_tab'], 50 );
            add_action( 'woocommerce_product_data_panels', ['CWPT_Admin_Tabs', 'add_tab_content'] );
            add_action( 'woocommerce_process_product_meta', ['CWPT_Admin_Tabs', 'save_settings'] );
            add_action( 'cmb2_admin_init', ['CWPT_Admin_Tabs', 'register_meta_boxes'] );
            // Front-end product hooks
            add_action( 'woocommerce_before_shop_loop_item', ['CWPT_Front_Fields', 'display_badge'], 100 );
            add_action( 'woocommerce_before_add_to_cart_button', ['CWPT_Front_Fields', 'render'] );
            add_action( 'woocommerce_after_add_to_cart_button', ['CWPT_Front_Fields', 'render'] );
            add_action( 'woocommerce_product_meta_start', ['CWPT_Front_Fields', 'render'] );
            add_action( 'woocommerce_product_meta_end', ['CWPT_Front_Fields', 'render'] );
            add_action( 'woocommerce_after_add_to_cart_form', ['CWPT_Front_Fields', 'render'] );
            add_action( 'woocommerce_single_product_summary', ['CWPT_Front_Fields', 'render'], 25 );
            // Admin footer hooks
            add_action( 'admin_footer', ['CWPT_Theme', 'enable_js_on_footer'] );
            // Polylang hooks
            add_filter(
                'pll_copy_post_metas',
                ['CWPT_Polylang', 'disable_meta_sync'],
                10,
                3
            );
            add_filter(
                'pll_get_post_types',
                ['CWPT_Polylang', 'add_to_post_types'],
                10,
                2
            );
        }

        /**
         * Initialize plugin functions
         * 
         * @return void
         */
        public function init_plugin() {
            CWPT_Product_Types::register_types();
            CWPT_Product_Types::register_taxonomies();
            CWPT_Product_Types::generate_classes();
            $current_user = wp_get_current_user();
            if ( user_can( $current_user, 'manage_options' ) ) {
                include_once plugin_dir_path( __FILE__ ) . 'includes/cwpt_admin.php';
            }
        }

    }

    new CWPT_Plugin();
}