<?php

include_once ABSPATH . 'wp-admin/includes/plugin.php';
include plugin_dir_path(__FILE__) . 'vendor/autoload.php';



/**
 *
 * @link              Bookingor
 * @since             2.1.2
 * @package           Bookingor
 *
 * @wordpress-plugin
 * Plugin Name:       Bookingor - Booking System for Appointment Booking Calendar, Meeting Scheduling & WooCommerce
 * Description:       Bookingor is an all-in-one appointment and booking management system. Streamline scheduling processes for any business or individuals. Bookingor helps you efficiently handle bookings, save time, and enhance the customer experience.
 * Version:           2.1.2
 * License:           GPL-2.0+
 * Tags:              booking, appointment, booking System, Schedule appointment, calendar, scheduling
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Author:            Bookingor.com
 * Author URI:        https://bookingor.com
 * Text Domain:       bookingor
 * Domain Path:       /languages
 * Requires at least: 3.4 
 * Requires PHP:      7.2 

 * 
 */

// If this file is called directly, abort.
if (! defined('WPINC')) {
    die;
}

/**
 * Currently plugin version.
 */
define('BOOKINGOR_VERSION', '2.1.2');

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-bookingor-activator.php.
 */
function activate_bookingor()
{
    ob_start();
    require_once plugin_dir_path(__FILE__) . 'includes/class-bookingor-activator.php';
    Bookingor_Activator::activate();
    Bookingor_Activator::default_settings_insert();

    if (empty(Bookingor_Activator::email_data_insert())) {
        Bookingor_Activator::email_data_insert();
    }

    set_transient('bookingor_activation_redirect', true, 30);
    ob_get_clean();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-bookingor-deactivator.php.
 */
function disallow_on_bookingor()
{
    require_once plugin_dir_path(__FILE__) . 'includes/class-bookingor-deactivator.php';
    Bookingor_Deactivator::deactivate();
}

register_activation_hook(__FILE__, 'activate_bookingor');
register_deactivation_hook(__FILE__, 'disallow_on_bookingor');

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path(__FILE__) . 'includes/class-bookingor.php';

/**
 * Check for plugin updates and run necessary DB migrations.
 */
add_action('upgrader_process_complete', 'bookingor_update_db_check', 10, 2);
function bookingor_update_db_check($upgrader_object, $options)
{
    if ($options['action'] == 'update' && $options['type'] == 'plugin') {
        if (isset($options['plugins']) && in_array('bookingor/bookingor.php', $options['plugins'])) {
            global $wpdb;
            $prefix = $wpdb->prefix . "bookingor_";
            $table_name = $prefix . 'services';

            $table_exists = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_name));
            if ($table_exists) {
                $column_exists = $wpdb->get_results("SHOW COLUMNS FROM `$table_name` LIKE 'service_duration_count'");
                if (empty($column_exists)) {
                    require_once plugin_dir_path(__FILE__) . 'includes/class-bookingor-activator.php';
                    Bookingor_Activator::activate();
                }
            }
        }
    }
}

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function starting_bookingor()
{
    $plugin = new Bookingor();
    $plugin->run();
}
starting_bookingor();
