<?php
/**
 * Plugin Name: Customer Journey
 * Description: Displays every path of the customer until the order of the product right on the WooCommerce orders page
 * Requires Plugins: woocommerce
 * Version: 2.0.0
 * Requires PHP: 7.0
 * Author: Mini Plugins
 * Author URI: https://miniplugins.com
 * Text Domain: customer-journey
 * Domain Path: /languages/
 * License: GPLv3
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * @package    Customer Journey
 * @since      2.0.0
 */

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

if ( ! function_exists( 'cjfw_fs' ) ) {
    // Create a helper function for easy SDK access.
    function cjfw_fs() {
        global $cjfw_fs;

        if ( ! isset( $cjfw_fs ) ) {
            // Include Freemius SDK.
            require_once dirname(__FILE__) . '/vendor/freemius/wordpress-sdk/start.php';

            $cjfw_fs = fs_dynamic_init( array(
                'id'                  => '14627',
                'slug'                => 'customer-journey',
                'premium_slug'        => 'customer-journey-premium',
                'type'                => 'plugin',
                'public_key'          => 'pk_e98a127c69b2213d305ddd8fa7445',
                'is_premium'          => false,
                'has_addons'          => false,
                'has_paid_plans'      => false,
                'menu'                => array(
                    'first-path'     => 'plugins.php',
                    'contact'        => false,
                    'support'        => false,
                ),
            ) );
        }

        return $cjfw_fs;
    }

    // Init Freemius.
    cjfw_fs();
    // Signal that SDK was initiated.
    do_action( 'cjfw_fs_loaded' );
}

// Plugin Root File.
const CUSTOMER_JOURNEY_FOR_WOOCOMMERCE_PLUGIN_FILE = __FILE__;

// Plugin Folder Path.
const CUSTOMER_JOURNEY_FOR_WOOCOMMERCE_PLUGIN_PATH = __DIR__;

/**
 * Plugin version.
 *
 * @var string
 */
const CUSTOMER_JOURNEY_FOR_WOOCOMMERCE_VERSION = '2.0.0';

/**
 * Require files.
 */
require_once __DIR__ . '/vendor/autoload.php';

/**
 * Return the main instance of Plugin Class.
 *
 * @since  1.0.0
 *
 * @return Plugin.
 */
function customer_journey_for_woocommerce() {

	$instance = \CustomerJourneyForWooCommerce\Plugin::get_instance();
	$instance->init();

	return $instance;
}
customer_journey_for_woocommerce();

register_activation_hook( CUSTOMER_JOURNEY_FOR_WOOCOMMERCE_PLUGIN_FILE, 'cjforwoo_create_tables' );

/**
 * Create tables on plugin installation.
 *
 * @return void.
 */
function cjforwoo_create_tables() {
	global $wpdb;

	require_once ABSPATH . 'wp-admin/includes/upgrade.php';

	$charset_collate = '';

	if ( ! empty( $wpdb->charset ) ) {
		$charset_collate .= "DEFAULT CHARACTER SET {$wpdb->charset}";
	}
	if ( ! empty( $wpdb->collate ) ) {
		$charset_collate .= " COLLATE {$wpdb->collate}";
	}

	$sql = "CREATE TABLE `{$wpdb->base_prefix}cjforwoo` (
        id bigint(20) NOT NULL AUTO_INCREMENT,
        order_id bigint(20) NOT NULL,
        post_id bigint(20) NOT NULL,
        url varchar(2083) NOT NULL,
        parameters varchar(2000) NOT NULL,
        external tinyint(1) DEFAULT 0,
        title varchar(256) NOT NULL,
        duration int NOT NULL,
        step tinyint NOT NULL,
        date datetime NOT NULL,
        PRIMARY KEY  (id),
        KEY order_id (order_id)
    ) {$charset_collate};";

	dbDelta( $sql );
}
