<?php

/**
 * Plugin Name: Cybersoldier
 * Plugin URI: https://cybersoldier.com/plugin/
 * Description: A rap battle plugin for Wordpress.
 * Version: 1.8.2
 * Stable tag: 1.8.2
 * Author: Mattias Kallio
 * Author URI: http://webbigt.se
 * Text Domain: cybersoldier
 * Domain Path: /languages
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Tested up to: 6.8.1
 */

define("CYBERSOLDIER_PLUGIN_PATH", plugin_dir_path(__FILE__));
define("CYBERSOLDIER_PLUGIN_URL", plugin_dir_url(__FILE__));

require_once(CYBERSOLDIER_PLUGIN_PATH . 'classes/battle_posttype.php');
require_once(CYBERSOLDIER_PLUGIN_PATH . 'classes/user.php');
require_once(CYBERSOLDIER_PLUGIN_PATH . 'classes/settings.php');
//require_once(CYBERSOLDIER_PLUGIN_PATH . 'classes/functions.php');
require_once(CYBERSOLDIER_PLUGIN_PATH . 'classes/adminajaxcalls.php');
require_once(CYBERSOLDIER_PLUGIN_PATH . 'classes/ajaxcalls.php');
require_once(CYBERSOLDIER_PLUGIN_PATH . 'views/shortcodes.php');

add_action('plugins_loaded', 'cybersoldier_textdomain');
function cybersoldier_textdomain()
{
	load_plugin_textdomain('cybersoldier', FALSE, basename(dirname(__FILE__)) . '/languages/');
}

add_action('admin_enqueue_scripts', 'cybersoldier_admin_scripts');
function cybersoldier_admin_scripts()
{
	wp_enqueue_script('cs_admin_scripts', CYBERSOLDIER_PLUGIN_URL . 'js/cybersoldier-admin.js',["jquery"]);
}

add_action('wp_enqueue_scripts', 'cybersoldier_addCSScripts');
add_action('admin_enqueue_scripts', 'cybersoldier_addCSScripts');
function cybersoldier_addCSScripts()
{
	wp_enqueue_style('googleFonts_cybersoldier', 'https://fonts.googleapis.com/css?family=Bad+Script|Nothing+You+Could+Do|Rancho|Shadows+Into+Light|Shadows+Into+Light+Two|Waiting+for+the+Sunrise');
	wp_enqueue_style('cybersoldier-main', CYBERSOLDIER_PLUGIN_URL . 'css/main.css');
	wp_enqueue_script('svg-script', CYBERSOLDIER_PLUGIN_URL . 'js/jquery.svg.js', array('jquery'));
	wp_enqueue_script('canvgrgb-script', CYBERSOLDIER_PLUGIN_URL . 'js/rgbcolor.js', array('jquery'));
	wp_enqueue_script('canvg-script', CYBERSOLDIER_PLUGIN_URL . 'js/canvg_cybersoldiermod.js', array('jquery'));
	wp_enqueue_script('cybersoldier-script', CYBERSOLDIER_PLUGIN_URL . 'js/cybersoldier-main.js', array('jquery'));
	wp_enqueue_script('jscolor', CYBERSOLDIER_PLUGIN_URL . 'js/jscolor.min.js', array('jquery'));

	wp_localize_script('cybersoldier-script', 'ajax_object', array(
		'ajax_url' => admin_url('admin-ajax.php'),
		'we_value' => 1337
	));
}


add_filter('query_vars', 'cybersoldier_add_cs_vars', 10, 1);
function cybersoldier_add_cs_vars($vars)
{
	$vars[] = 'csid'; // csid is the name of variable you want to add
	return $vars;
}

/**
 * Test to radmomize look of users
 */
/* add_filter('init', function(){
	CyberSoldier_User::cybersoldier_give_character_random_items(6);
}); */

/**
 * install stuff
 */
function cybersoldier_install()
{
	global $wpdb;
	/*
	 * We'll set the default character set and collation for this table.
	 * If we don't do this, some characters could end up being converted
	 * to just ?'s when saved in our table.
	 */
	$charset_collate = '';

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

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

	$table_name = $wpdb->prefix . "cybersoldier_lines";
	$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
		id INT(11) NOT NULL AUTO_INCREMENT,
		post_id INT(11) NOT NULL,
		user_id INT(11) NOT NULL,
		line VARCHAR(255) NOT NULL,
		date_added DATETIME,
		date_used DATETIME,
		PRIMARY KEY (id)
	);";

	$wpdb->query($sql);

	$table_name = $wpdb->prefix . "cybersoldier_scores";
	$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
		id INT(11) NOT NULL AUTO_INCREMENT,
		line_id INT(11) NOT NULL,
		value INT(11) NOT NULL,
		post_user INT(11) NOT NULL,
		PRIMARY KEY (id)
	);";

	$wpdb->query($sql);


	$table_name = $wpdb->prefix . "cybersoldier_character_items";
	$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
		id INT(11) NOT NULL AUTO_INCREMENT,
		type VARCHAR(255) NOT NULL,
		name VARCHAR(255) NOT NULL,
		icon VARCHAR(255) NOT NULL,
		svg_info TEXT NOT NULL,
		PRIMARY KEY (id)
	);";

	$wpdb->query($sql);

	/*
* Setting up the folders in upload
*/

	$upload = wp_upload_dir();
	$upload_dir = $upload['basedir'];
	$upload_dir = $upload_dir . '/cybersoldier';
	if (!is_dir($upload_dir)) {
		mkdir($upload_dir, 0755);
	}
	$upload_dir = $upload_dir . '/users';
	if (!is_dir($upload_dir)) {
		mkdir($upload_dir, 0755);
	}
}


/**
 * The uninstall function.
 */
function cybersoldier_uninstall()
{
	global $wpdb;
	$cs_tables = array(
		"cybersoldier_lines",
		"cybersoldier_scores",
		"cybersoldier_character_items"
	);
	// Delete any options thats stored also?
	// delete_option('wp_yourplugin_version');
	foreach ($cs_tables as $tabbe) {
		$table_name = $wpdb->prefix . $tabbe;
		//$wpdb->query ( "DROP TABLE IF EXISTS $table_name" );
	}
}

register_activation_hook(__FILE__, 'cybersoldier_install');
register_deactivation_hook(__FILE__, 'cybersoldier_uninstall');
