<?php
/**
 * Plugin Name:       Foodbook Lite - Online Food Ordering System
 * Plugin URI:        https://www.themelooks.com/blog/
 * Description:       FoodBookLite is a Online Food Ordering system for WordPress. It is developed based on woocommerce. It is a standalone WordPress plugin which allows you to easily add Food Ordering System to your WordPress Website. Using FoodBookLite you can easily receive both PickUp and Delivery orders.
 * Version:           1.5.5
 * Author:            ThemeLooks
 * Author URI:        https://themelooks.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       foodbooklite
 * Domain Path:       /languages
 */

/**
 * Define all constant
 *
 */

// Version constant
if (!defined('FOODBOOKLITE_VERSION')) {
    define('FOODBOOKLITE_VERSION', '1.5.5');
}

// Plugin dir path constant
if (!defined('FOODBOOKLITE_DIR_PATH')) {
    define('FOODBOOKLITE_DIR_PATH', trailingslashit(plugin_dir_path(__FILE__)));
}
// Plugin dir url constant
if (!defined('FOODBOOKLITE_DIR_URL')) {
    define('FOODBOOKLITE_DIR_URL', trailingslashit(plugin_dir_url(__FILE__)));
}
// Plugin dir admin assets url constant
if (!defined('FOODBOOKLITE_DIR_ADMIN_ASSETS_URL')) {
    define('FOODBOOKLITE_DIR_ADMIN_ASSETS_URL', trailingslashit(FOODBOOKLITE_DIR_URL . 'admin/assets'));
}
// Admin dir path
if (!defined('FOODBOOKLITE_DIR_ADMIN')) {
    define('FOODBOOKLITE_DIR_ADMIN', trailingslashit(FOODBOOKLITE_DIR_PATH . 'admin'));
}
// Inc dir path
if (!defined('FOODBOOKLITE_DIR_INC')) {
    define('FOODBOOKLITE_DIR_INC', trailingslashit(FOODBOOKLITE_DIR_PATH . 'inc'));
}
// Inc dir path
if (!defined('FOODBOOK_PRO_URL')) {
    define('FOODBOOK_PRO_URL', 'https://codecanyon.net/item/foodbook-online-food-ordering-system-for-wordpress/27669182');
}


final class FoodBookLite
{

    private static $instance = null;

    function __construct()
    {
        if (class_exists('FoodBook'))
            return;
        //
        add_action('init', [$this, 'client_insights']);
        add_action('init', [$this, 'foodbooklite_load_textdomain']);
        register_deactivation_hook(__FILE__, [$this, 'foodbooklite_plugin_deactivate']);
        register_activation_hook(__FILE__, [$this, 'foodbooklite_plugin_activate']);
        add_action('plugins_loaded', [$this, 'foodbooklite_is_woocommerce_activated']);
    }

    public static function getInstance()
    {

        if (is_null(self::$instance)) {
            self::$instance = new self;
        }

        return self::$instance;
    }

    /**
     * Load plugin textdomain.
     */
    public function foodbooklite_load_textdomain()
    {
        if (class_exists('FoodBook'))
            return;
        load_plugin_textdomain('foodbooklite', false, FOODBOOKLITE_DIR_PATH . 'languages');
    }

    /**
     * Check WooCommerce is activated or not
     * 
     */
    public function foodbooklite_is_woocommerce_activated()
    {
        if (class_exists('FoodBook'))
            return;

        if (class_exists('woocommerce')) {
            require_once(FOODBOOKLITE_DIR_PATH . 'foodbooklite-init.php');
        } else {
            add_action('admin_notices', [$this, 'foodbooklite_activation_admin_notice']);
        }

    }

    /**
     * foodbooklite_activation_admin_notice description
     * 
     * If wooocommerce plugin not active 
     * show the admin notification to active woocommerce plugin 
     * 
     * @return 
     */
    public function foodbooklite_activation_admin_notice()
    {

        $btn = [

            'install' => [
                'url' => wp_nonce_url('update.php?action=install-plugin&plugin=woocommerce', 'install-plugin_woocommerce'),
                'label' => esc_html__('Install WooCommerce', 'foodbooklite')
            ],
            'active' => [
                'url' => wp_nonce_url('plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=all&paged=1', 'activate-plugin_woocommerce/woocommerce.php'),
                'label' => esc_html__('Activate WooCommerce', 'foodbooklite')
            ]

        ];

        $is_active = $this->foodbooklite_check_wc();

        if ($is_active == 'notinstalled') {
            $getBtn = $btn['install'];
        } else {
            $getBtn = $btn['active'];
        }

        ?>
        <div class="notice notice-error is-dismissible">
            <h4><?php echo sprintf(esc_html__('FoodBookLite requires the WooCommerce plugin to be installed and active. %s %s %s', 'foodbooklite'), '<a href="' . esc_url($getBtn['url']) . '" target="_blank">', esc_html($getBtn['label']), '</a>', ); ?>
            </h4>
        </div>
        <?php
    }

    public function foodbooklite_check_wc()
    {

        $woocommerceFile = WP_PLUGIN_DIR . '/woocommerce/woocommerce.php';
        $activatedPlugin = get_option('active_plugins');

        if (!file_exists($woocommerceFile)) {
            $has_woocommerce = 'notinstalled';
        } else if (file_exists($woocommerceFile) && !in_array('woocommerce/woocommerce.php', $activatedPlugin)) {
            $has_woocommerce = 'deactive';
        } else {
            $has_woocommerce = 'active';
        }
        return $has_woocommerce;
    }

    /**
     * foodbooklite_default_pages_list 
     * @return array
     */
    public function foodbooklite_default_pages_list()
    {

        return [
            "foodbooklite" => "FoodBookLite"
        ];

    }

    /**
     * foodbooklite_insert_page 
     * Add plugin default page
     * @return 
     * 
     */
    public function foodbooklite_insert_page()
    {

        $getPages = $this->foodbooklite_default_pages_list();

        foreach ($getPages as $page_title) {

            if (!get_page_by_path(sanitize_title($page_title))) {

                // Create page object
                $page = array(
                    'post_type' => 'page',
                    'post_title' => wp_strip_all_tags($page_title),
                    'post_status' => 'publish'
                );

                // Insert the post into the database
                wp_insert_post($page);

            }

        }

    }

    /**
     * foodbooklite_delete_page description
     * @return 
     */
    public function foodbooklite_delete_page()
    {
        // Pages
        $getPages = $this->foodbooklite_default_pages_list();
        //
        foreach ($getPages as $key => $page) {
            $page_data = get_page_by_path($key);

            wp_delete_post($page_data->ID);
        }

    }

    /**
     * foodbooklite_plugin_activate
     * @return 
     */
    public function foodbooklite_plugin_activate()
    {
        if (class_exists('FoodBook'))
            return;

        // Insert default pages
        $this->foodbooklite_insert_page();

        // Default options set
        $defaultOption = array(

            "product-limit" => 6,
            "search-section" => 'yes',
            "show-cart-button" => 'yes',
            "checkout-delivery-option" => 'yes',
            "pickup-time-switch" => 'yes',
            "delivery-options" => 'all',
            "shop-page" => 'foodbooklite'
        );

        update_option('foodbooklite_options', $defaultOption);

        // Create classic Checkout page for foodbook

        $page_slug = 'order-checkout';

        // Check if page exists
        if (!get_page_by_path($page_slug)) {
            $page_data = array(
                'post_title' => 'Order Checkout',
                'post_name' => $page_slug,
                'post_content' => '[woocommerce_checkout]',
                'post_status' => 'publish',
                'post_type' => 'page',
            );
            $pageId = wp_insert_post($page_data);

            if (!is_wp_error($pageId)) {
                update_option('woocommerce_checkout_page_id', absint($pageId));
            }

        }

    }

    /**
     * foodbooklite_plugin_deactivate 
     * @return 
     */
    public function foodbooklite_plugin_deactivate()
    {

        // Delete default pages
        $this->foodbooklite_delete_page();

        //
        delete_option('foodbooklite_options');

    }

    public function client_insights()
    {

        if (!class_exists('Appsero\Client')) {
            require_once __DIR__ . '/appsero/src/Client.php';
        }

        $client = new Appsero\Client('f63dad29-c41f-47ba-bca8-8a863156ec23', 'FoodBook Lite &#8211; Online Food Ordering System', __FILE__);

        // Active insights
        $client->insights()->init();

    }

}

// Init FoodBookLite class
FoodBookLite::getInstance();
