<?php
/*
Plugin Name: WP SalesAutomator
Plugin URI: http://wpsalesautomator.com/
Description: Automating Product Sales with Wordpress!
Version: 1.0.5.1
Date: April 10, 2012
Author: wordpressLAB
Author URI: http://www.wordpressLAB.net
*/

/*------------------------------------------------------------------------
		 	CONSTANTS
------------------------------------------------------------------------*/
if ( ! defined( 'WP_PLUGIN_URL' ) ) define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );

define( 'WP_SA_PLUGIN_URL', WP_PLUGIN_URL . '/' . dirname( plugin_basename(__FILE__) ) );
define( 'WP_SA_PLUGIN_DIR', dirname(__FILE__) );
define( 'WP_SA_PLUGIN_ADMIN_DIR', dirname(__FILE__).'/admin' );
define( 'WP_SA_IPN_URL', WP_SA_PLUGIN_URL.'/php/wp-sa-paypal-ipn-handler.php' );

/*------------------------------------------------------------------------
		 	INCLUDES
------------------------------------------------------------------------*/

require_once( WP_SA_PLUGIN_DIR.'/php/wp-sa-core-function.php' );
require_once( WP_SA_PLUGIN_DIR .'/php/wp-sa-products.php' );
require_once( WP_SA_PLUGIN_DIR.'/php/wp-sa-thank-you.php' );

require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-products.php' );
require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-subscription.php' );
require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-products-sales.php' );
require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-subscription-sales.php' );
require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-setting.php' );
require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-rssreader.php' );
require_once( WP_SA_PLUGIN_ADMIN_DIR .'/wp-sa-updates.php' );

require_once( WP_SA_PLUGIN_DIR.'/wp-sa-editorbutton.php' );


/*------------------------------------------------------------------------
		 add js & css into wordpres head
------------------------------------------------------------------------*/
//add CSS
wp_enqueue_style('wp-salesautomator-css',WP_SA_PLUGIN_URL.'/css/wp-sales.css');

add_action('wp_print_scripts', 'wpsa_include_script');
function wpsa_include_script() {
    wp_enqueue_script('wp-sa-js',WP_SA_PLUGIN_URL.'/js/wp-sales.js', array('jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-form'));
    wp_enqueue_script('wp-sa-validate',WP_SA_PLUGIN_URL.'/js/jquery.validate.js','jquery');
 
    //send arguments to our script
    wp_localize_script('wp-sa-js', 'wpsa_var', array(
            'admin_ajax_php' => get_option('siteurl').'/wp-admin/admin-ajax.php',
            'plugindir' => WP_SA_PLUGIN_URL
    ));
}
/*------------------------------------------------------------------------
		 Creat menu box in admin side
------------------------------------------------------------------------*/
add_action('admin_menu', 'sa_rt_admin_menu', 10);
function sa_rt_admin_menu() {    
    add_menu_page('WP SalesAutomator', 'WPSA', 'manage_options', 'wp-sa-salesautomator' , 'rt_products_product_page');
    add_submenu_page('wp-sa-salesautomator', 'WPSA Products', 'Products', 'manage_options', 'wp-sa-salesautomator', 'rt_products_product_page');
    add_submenu_page('wp-sa-salesautomator', 'WPSA Subscriptions', 'Subscriptions', 'manage_options', 'wp-sa-manage-subscription', 'rt_subscription_page');
    add_submenu_page('wp-sa-salesautomator', 'WPSA Product Sales Log', 'Product Sales', 'manage_options', 'wp-sa-transaction-log', array( 'WP_SA_PRODUCT_SAES', 'log' ) );
    add_submenu_page('wp-sa-salesautomator', 'WPSA Subscription Log', 'Subscription Sales', 'manage_options', 'wp-sa-subscription-transaction-log', 'rt_subscription_log_page');
    add_submenu_page('wp-sa-salesautomator', 'WPSA Settings', 'Settings', 'manage_options', 'wp-sa-setting', array('WP_SA_SETTING','setting_page') );
    add_submenu_page('wp-sa-salesautomator', '', 'Updates', 'manage_options', 'wp-sa-updates' , 'wp_sa_updates');
}

add_action("admin_menu", "sa_rt_addom_menu", 11);
function sa_rt_addom_menu() {
    add_menu_page('WP SalesAutomator Addons', 'WPSA Addons', 'manage_options', 'wpsa-addon');
}

//add_action("admin_head", "remove_menu", 999);
//function remove_menu() {
//global $menu, $submenu;
////    if(empty($menu['wpsa-addon']))
////        unset($menu[999]);
//}

function sa_rt_get_addonsubmenu($handler) {
    global $submenu;
    if(!isset($submenu['wpsa-addon']) && count($submenu['wpsa-addon']<=0))  $handler = 'wpsa-addon';
    return $handler;
}

register_activation_hook(__FILE__,'wpsa_install');
function wpsa_install () {
    global $wpdb;   

    $table_name = $wpdb->prefix . "wpsa_subscription_transaction";
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

        $sql =  "CREATE TABLE `$table_name` (
                `id` INT( 5 ) NOT NULL AUTO_INCREMENT,
                `user_ID` bigint(20) NOT NULL,
                `subscriber_id` VARCHAR( 30 ) NOT NULL ,
                `txn_id` VARCHAR( 30 ) NOT NULL ,
                `item_name` VARCHAR( 70 ) NOT NULL ,
                `item_number` INT( 5 ) NOT NULL ,
                `payer_email` VARCHAR( 70 ) NOT NULL ,
                `reciever_email` VARCHAR( 70 ) NOT NULL ,
                `first_name` VARCHAR( 70 ) NOT NULL ,
                `last_name` VARCHAR( 70 ) NOT NULL ,
                `payer_id` VARCHAR( 30 ) NOT NULL ,
                `reciever_id` VARCHAR( 30 ) NOT NULL ,
                `payment_date` VARCHAR( 30 ) NOT NULL ,
                `subscr_date` VARCHAR( 30 ) NOT NULL ,
                `amount` FLOAT( 10 ) NOT NULL ,
                `period` VARCHAR( 10 ) NOT NULL ,
                `recurring` INT( 2 ) NOT NULL ,
                `subscr_status` ENUM( 'active', 'inactive' ) NOT NULL ,
                `check_time` TEXT NOT NULL,
                 `edited` CHAR( 1 ) NOT NULL DEFAULT '0',
                 `time` INT NOT NULL,
                  UNIQUE (`subscriber_id`),
                 PRIMARY KEY  (id)
                )";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
    
    //paypal products table
    //Added product_name in table : Nemi
    //Added customer first and last name: Umesh
    $table_name = $wpdb->prefix . "wpsa_product_transaction";
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
        $sql = "CREATE TABLE " . $table_name . " (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        product_id int(9) NOT NULL,
        product_name varchar(100) NOT NULL,
        customer_fname varchar(67) NOT NULL,
        customer_lname varchar(67) NOT NULL,
        customer_email varchar(200) NOT NULL,
        customer_ip varchar(200) NOT NULL,
        paypal_transaction_id varchar(200) NOT NULL,
        status ENUM( 'active', 'inactive' ) NOT NULL,
        time datetime NOT NULL,
        referal_url varchar(200) NOT NULL,
        check_time TEXT NOT NULL,
        price varchar(200) NOT NULL,
        PRIMARY KEY  (id)
        );";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

    //message table
    
    $table_name = $wpdb->prefix . "wpsa_product_details";
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
        $sql = "CREATE TABLE " . $table_name . " (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name varchar(200) NOT NULL,
        description varchar(200) NOT NULL,
        price varchar(200) NOT NULL,
        file_name TEXT NOT NULL,
        message_template LONGTEXT NOT NULL,
        PRIMARY KEY  (id)
        );";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
    
    // wpsa_download_link
    $table_name = $wpdb->prefix . "wpsa_download_link";
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
        $sql = " CREATE TABLE `{$table_name}` (
			`id` INT NOT NULL AUTO_INCREMENT ,
			`key` VARCHAR( 100 ) NOT NULL ,
			`name` VARCHAR( 100 ) NOT NULL ,
			`count` INT NOT NULL ,
			`expire` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
			`ip_addr` VARCHAR( 15 ) NOT NULL ,
			PRIMARY KEY ( `id` )
			)";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }

    $table_name = $wpdb->prefix . "wpsa_subscriptions";
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
        $sql = "CREATE TABLE `{$table_name}` (
                    `id` INT NOT NULL AUTO_INCREMENT,
                    `name` VARCHAR(100) NOT NULL,
                    `description` TEXT NOT NULL,
                    `amount1` FLOAT(9) NOT NULL,
                    `duration1` INT(9) NOT NULL,
                    `duration_interval1` VARCHAR(9) NOT NULL,
                    `amount2` FLOAT(9) NOT NULL,
                    `duration2` INT(9) NOT NULL,
                    `duration_interval2` VARCHAR(9) NOT NULL,
                    `amount3` FLOAT(9) NOT NULL,
                    `duration3` INT(9) NOT NULL,
                    `duration_interval3` VARCHAR(9) NOT NULL,
                    `categories` TEXT NOT NULL,
                     PRIMARY KEY (`id`)
                    )";
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
    }
    
     if( !get_option('rt_option_products') ){
		wpsa_default_settings();
	}
}


register_uninstall_hook( __FILE__, 'sa_rt_uninstall' );
function sa_rt_uninstall() {
    global $wpdb;
    $wp_sa_setting = get_option('wp_sa_setting');
    if ( $wp_sa_setting['wp_sa_keep_data'] != 1) {
        $wpsa_subscription_transaction = $wpdb->prefix . "wpsa_subscription_transaction";
        $wpsa_product_transaction = $wpdb->prefix . "wpsa_product_transaction";
        $wpsa_product_details = $wpdb->prefix . "wpsa_product_details";
        $wpsa_download_link = $wpdb->prefix . "wpsa_download_link";
        $wpsa_subscriptions = $wpdb->prefix . "wpsa_subscriptions";

        if ($wpdb->get_var("SHOW TABLES LIKE '".$wpsa_product_details."'") == $wpsa_product_details) {
            $wpdb->query( "DROP TABLE $wpsa_subscription_transaction, $wpsa_product_transaction, $wpsa_product_details, $wpsa_download_link, $wpsa_subscriptions" );
        }
    }
}

function wpsa_default_settings() {
	$default = array(
    'from' => get_option('admin_email'),
    'fromname' => get_option('blogname'),
    'subject1' => sprintf(__('[%s] Download Link', 'wp-products'), get_option('blogname')),
    'custom_msg' => '0',
    'user_nl2br' => '0',
    'adminfrom'	=> get_option('admin_email'),
    'adminfromname' => get_option('blogname'),
    'Max_Count_Limit' => 1,

    'msg1' => "Hi %first_name%

Thank you for purchasing %product_name%.

Please find the download link below...

%download_link%

Thanks again
%blogname%"
        );
    
    update_option( 'rt_option_products', $default );

    $wp_sa_setting_default = array(
    'wp_sa_upload_dir_path' => ABSPATH.'wp-content/uploads/salesautomator/uploads/',
    'wp_sa_temp_dir_path' => ABSPATH.'wp-content/uploads/salesautomator/temp/'
    );
    update_option( 'wp_sa_setting', $wp_sa_setting_default );
}
?>