<?php
/**
 * Plugin Name: Sync Master Sheet - Product Sync with Google Sheet for WooCommerce
 * Requires Plugins: woocommerce
 * Plugin URI: https://codeastrology.com/how-to-setup-product-sync-master-plugin/
 * Description: Help you to connect your WooCommerce website with Google Sheet as well as Manage your Stock easy from one menu with Advance Filter
 * Author: CodeAstrology Team
 * Author URI: https://profiles.wordpress.org/codersaiful/#content-plugins
 * Tags: stock sync with google sheet, google sheet sync, bulk edit product
 * 
 * Version: 2.0.4
 * Requires at least:    4.0.0
 * Requires PHP:         7.2
 * Tested up to:         7.0
 * WC requires at least: 5.0.0
 * WC tested up to: 	 10.7.2
 * 
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * 
 * @package PSSG_Sync_Sheet
 * 
 * Text Domain: product-sync-master-sheet
 * Domain Path: /languages/
 * @fs_premium_only /premium/premium-loader.php, /premium/
 * 
 * 
 * Product Stock Sync with Google Sheet for WooCommerce is free software: 
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.
 * 
 * Product Stock Sync with Google Sheet for WooCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 */

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

/**
 * Clear the scheduled sync cron event on plugin deactivation.
 */
function pssg_deactivate_schedule_sync() {
    $timestamp = wp_next_scheduled( 'pssg_scheduled_sync' );
    if ( $timestamp ) {
        wp_unschedule_event( $timestamp, 'pssg_scheduled_sync' );
    }
}
register_deactivation_hook( __FILE__, 'pssg_deactivate_schedule_sync' );

if( ! defined( 'PSSG_DEV_VERSION' ) ){
    define( "PSSG_DEV_VERSION", '2.0.4.0' );
}
if( ! defined( 'PSSG_PLUGIN_NAME' ) ){
    define( "PSSG_PLUGIN_NAME", 'Sync Master Sheet' );
}

if( ! defined( 'PSSG_BASE_URL' ) ){
    define( "PSSG_BASE_URL", plugins_url() . '/'. plugin_basename( dirname( __FILE__ ) ) . '/' );
}

if( ! defined( 'PSSG_BASE_FILE' ) ){
    define( "PSSG_BASE_FILE", plugin_basename( __FILE__ ) );
}
if( ! defined( 'PSSG_FILE' ) ){
    define( "PSSG_FILE", __FILE__ );
}
if( ! defined( 'PSSG_BASE_FOLDER_NAME' ) ){
    define( "PSSG_BASE_FOLDER_NAME", plugin_basename(__DIR__) );
}

if( ! defined( 'PSSG_ASSETS_URL' ) ){
    define( "PSSG_ASSETS_URL", PSSG_BASE_URL . 'assets/' );
}

if( ! defined( 'PSSG_DIR_BASE' ) ){
    define( "PSSG_DIR_BASE", dirname( __FILE__ ) . '/' );
}
if( ! defined( 'PSSG_BASE_DIR' ) ){
    define( "PSSG_BASE_DIR", str_replace( '\\', '/', PSSG_DIR_BASE ) );
}
if( ! defined( 'PSSG_PREFIX' ) ){
    define( "PSSG_PREFIX", 'pssg' );
}
if( ! defined( 'PSSG_APPS_SCRIPT_VERSION' ) ){
    define( "PSSG_APPS_SCRIPT_VERSION", '2.0.1' );
}


if ( ! class_exists( 'PSSG_Init' ) ) :
class PSSG_Init
{
    /**
     * Class Instance
     *
     * @var object
     */
    public static $instance;
    public $textdomain_load = false;
    public static function instance()
    {
        if( is_null( self::$instance ) ){
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function __construct()
    {

        add_action('plugins_loaded', [$this, 'plugins_loaded']);
        add_action('init', [$this, 'init']);

    }
    public function plugins_loaded()
    {

        $is_woocommerce = class_exists( 'WooCommerce' );


        if ( ! $is_woocommerce ) {
            add_action('admin_notices', [$this, 'missing_wc']);
            return;
        }
        

        // Declare compatibility with custom order tables for WooCommerce.
        add_action( 'before_woocommerce_init', function(){
            if ( class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil') ) {
                    \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
                }
            }
        );


        $this->include_dependencies();
        do_action( 'pssg_plugins_loaded' );
    }

    public function init()
    {
        

        

        do_action( 'pssg_init' );

        //Currently we are not using this notice system.
        // if( is_admin() ) PSSG_Sync_Sheet\Framework\Plugin_Required::display_notice();

        //Not only Admin/But also frontend handle also added over there, such: API - it's not for login user.
        $admin = new PSSG_Sync_Sheet\Admin\Admin_Loader();
        $admin->init();
        
        do_action( 'pssg_loaded' );
    }

    protected function include_dependencies()
    {
        include_once PSSG_BASE_DIR . 'autoloader.php';
        include_once PSSG_BASE_DIR . 'app/functions.php';
    }

    /**
     * Displaying Error Notice for 
     * If WooCoommerce is not installed
     *
     * @since 1.0.0.20
     * @author Saiful Islam <codersaiful@gmail.com>
     * 
     * @return void
     */
    public function missing_wc()
    {

        
        $message = sprintf(
                esc_html__('"%1$s" requires "%2$s" to be installed and activated.', 'product-sync-master-sheet'),
                '<strong>' . PSSG_PLUGIN_NAME . '</strong>',
                '<strong><a href="' . esc_url('https://wordpress.org/plugins/woocommerce/') . '" target="_blank">' . esc_html__('WooCommerce', 'product-sync-master-sheet') . '</a></strong>'
        );

        ?>
        <div class="notice notice-error"><p><?php echo wp_kses_post( $message ) ?></p></div>
        <?php
    }
}

PSSG_Init::instance();

endif;