<?php

	/*
	Plugin Name: wpShopGermany
	Text Domain: wpsg
	Domain Path: /lang
	Plugin URI: http://wpshopgermany.maennchen1.de/
	Description: Das deutsche WordPress Shop-Plugin!
	Author: maennchen1.de
	Version: 9.9.9
	Author URI: http://maennchen1.de/
	*/
 
	define('WPSG_VERSION', '9.9.9');

	global $wpdb;
	
	if (defined('MULTISITE') && MULTISITE === true && get_site_option('wpsg_multiblog_standalone', true) != '1') 
	{
		
		$prefix = $wpdb->base_prefix;
		
	}
	else 
	{
		
		$prefix = $wpdb->prefix;
		
	}
	
	// Tabellen
	define('WPSG_TBL_META', $prefix.'wpsg_meta');
	define('WPSG_TBL_PRODUCTS', $prefix.'wpsg_products');
	define('WPSG_TBL_PRODUCTS_GROUP', $prefix.'wpsg_productgroups'); 
	define('WPSG_TBL_MWST', $prefix.'wpsg_mwst'); // Deprecated ab 3.5 nur noch für Migrationszwecke! 
	define('WPSG_TBL_ORDER', $prefix.'wpsg_order');
	define('WPSG_TBL_ORDERPRODUCT', $prefix.'wpsg_order_products');
	define('WPSG_TBL_KU', $prefix.'wpsg_kunden');
	define('WPSG_TBL_KG', $prefix.'wpsg_customergroup');
	define('WPSG_TBL_VZ', $prefix.'wpsg_versandzonen');
	define('WPSG_TBL_LAND', $prefix.'wpsg_laender');
	define('WPSG_TBL_AT', $prefix.'wpsg_produktattribute');
	define('WPSG_TBL_PRODUCTS_AT', $prefix.'wpsg_products_attribute');
	define('WPSG_TBL_OL', $prefix.'wpsg_orderlog');
	define('WPSG_TBL_VA', $prefix.'wpsg_versandarten');
	define('WPSG_TBL_RECHNUNGEN', $prefix.'wpsg_rechnungen');
	define('WPSG_TBL_GUTSCHEIN', $prefix.'wpsg_gutscheine');
 	define('WPSG_TBL_PRODUCTS_REL', $prefix.'wpsg_products_related');
 	define('WPSG_TBL_EXPORTPROFILE', $prefix.'wpsg_exportprofile');
 	define('WPSG_TBL_PRODUCTS_VARS', $prefix.'wpsg_productvars');
 	define('WPSG_TBL_PRODUCTS_STICKY', $prefix.'wpsg_products_sticky');
 	define('WPSG_TBL_ZV', $prefix.'wpsg_zahlvarianten');
 	define('WPSG_TBL_VIDEOINDIV', $prefix.'wpsg_videoindividualization');
	define('WPSG_TBL_PDFINDIV', $prefix.'wpsg_pdfindividualization');
	define('WPSG_TBL_ORDERVARS', $prefix.'wpsg_ordervars');
 	define('WPSG_TBL_ORDERCOND', $prefix.'wpsg_orderconditions');
 	define('WPSG_TBL_CABLOG', $prefix.'wpsg_cab_log');
 	define('WPSG_TBL_PAP', $prefix.'wpsg_pap_referenz');
 	define('WPSG_TBL_ABO', $prefix.'wpsg_abo');
 	define('WPSG_TBL_SCALEPRICE', $prefix.'wpsg_scaleprice');
 	define('WPSG_TBL_DELIVERYNOTE', $prefix.'wpsg_deliverynote');
		
	// Slug
	define('WPSG_SLUG', 'wpshopgermany/wpshopgermany.php');
	
	// Ist in Multiblog manchma nicht definiert :? Sonst ist hier das Verzeichnis drin
	if (!defined('SITECOOKIEPATH')) define('SITECOOKIEPATH', '/');
	
	$wp_upload_dir = wp_upload_dir();
	
	// Pfade
	define('WPSG_PATH', dirname(__FILE__).'/');
	define('WPSG_PATH_LIB', WPSG_PATH.'/lib/');
	define('WPSG_PATH_WP', ABSPATH);
	define('WPSG_PATH_CONTENT', WP_CONTENT_DIR.'/'); /* wp-content/ */
	define('WPSG_PATH_UPLOADS', $wp_upload_dir['basedir'].'/wpsg/');
	define('WPSG_PATH_VIEW', dirname(__FILE__).'/views/');
	define('WPSG_PATH_TEMPLATEVIEW', get_template_directory().'/wpsg_views/');
	define('WPSG_PATH_USERVIEW', WPSG_PATH_UPLOADS.'user_views/');
	define('WPSG_PATH_USERVIEW_OLD', WPSG_PATH.'user_views/');
	define('WPSG_PATH_USERVIEW_UPLOADS', WPSG_PATH_UPLOADS.'user_views/');
	define('WPSG_PATH_MOD', dirname(__FILE__).'/mods/');	
	define('WPSG_PATH_USERMOD', WPSG_PATH_UPLOADS.'user_mods/');
	define('WPSG_PATH_PRODUKTTEMPLATES', WPSG_PATH_VIEW.'produkttemplates/');
	define('WPSG_PATH_PRODUKTTEMPLATES_UV', WPSG_PATH_USERVIEW.'produkttemplates/');
	define('WPSG_PATH_PRODUKTTEMPLATES_UV_OLD', WPSG_PATH_USERVIEW_OLD.'produkttemplates/');
	define('WPSG_PATH_PRODUKTTEMPLATES_TV', WPSG_PATH_TEMPLATEVIEW.'produkttemplates/');
	define('WPSG_URL', plugins_url().'/wpshopgermany/');	
	define('WPSG_URL_WP', site_url().'/');
	define('WPSG_URL_CONTENT', content_url().'/');
	define('WPSG_URL_UPLOADS',  $wp_upload_dir['baseurl'].'/wpsg/');
	define('WPSG_URL_USERVIEW', $wp_upload_dir['baseurl'].'/wpsg/user_views/');
	define('WPSG_PLUGIN_URL', plugins_url().'/');
	define('WPSG_PATH_TRANSLATION', dirname(__FILE__).'/lib/translation.phtml');	
	          
	// Konstanten
	define('WPSG_BRUTTO', '0');
	define('WPSG_NETTO', '1');
	
	require_once(dirname(__FILE__).'/lib/functions.inc.php');
	require_once(dirname(__FILE__).'/lib/filter_functions.inc.php');
	require_once(dirname(__FILE__).'/lib/helper_functions.inc.php');
	require_once(dirname(__FILE__).'/lib/wpsg_db.class.php');
	require_once(dirname(__FILE__).'/lib/wpsg_cache.class.php');
	require_once(dirname(__FILE__).'/lib/wpsg_basket.class.php');
	require_once(dirname(__FILE__).'/lib/wpsg_header.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_model.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_product.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_order.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_country.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_order_product.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_customer.class.php');
	require_once(dirname(__FILE__).'/model/wpsg_news.class.php');
	require_once(dirname(__FILE__).'/mods/wpsg_mod_basic.class.php');
	require_once(dirname(__FILE__).'/controller/wpsg_SystemController.class.php');
	require_once(dirname(__FILE__).'/controller/wpsg_ShopController.class.php');
	require_once(dirname(__FILE__).'/controller/wpsg_AdminController.class.php');
	require_once(dirname(__FILE__).'/controller/wpsg_ProduktController.class.php');	
	require_once(dirname(__FILE__).'/controller/wpsg_BasketController.class.php');
	require_once(dirname(__FILE__).'/controller/wpsg_OrderController.class.php');
		
	$_GET['wpsg_quotecheck'] = '\"CHECK';
	
	/** @var wpsg_db */
	$GLOBALS['wpsg_db'] = new wpsg_db();

	/** @var wpsg_ShopController */
	$GLOBALS['wpsg_sc'] = new wpsg_ShopController();
			
	if ($GLOBALS['wpsg_sc']->isMultiBlog() && !is_admin() && $GLOBALS['wpsg_sc']->get_option('wpsg_multiblog_sessionPath', true) == '1')
	{
		
		global $current_blog; 
		 
		session_set_cookie_params(0, $current_blog->path);
				
	}
	
	if (!session_id()) { session_start(); }

	// UPLOADS Dir für Multiblogunterstützung
	global $current_blog;
	if ($GLOBALS['wpsg_sc']->isMultiBlog())
	{
	
		$path = $GLOBALS['wpsg_sc']->get_option('wpsg_path_upload_multiblog', true);
		$path = preg_replace('/%blog_id%/', $current_blog->blog_id, $path);
	
		define('WPSG_MB_UPLOADS', $path);
	
	}
	
	$GLOBALS['wpsg_sc']->initShop($prefix);
	 
	// Standardrollen für die Rechteverteilung 
	$role_object = get_role('administrator');
	$role_object->add_cap('wpsg_menu');
	$role_object->add_cap('wpsg_conf');
	$role_object->add_cap('wpsg_order');
	$role_object->add_cap('wpsg_produkt');
	$role_object->add_cap('wpsg_lizence');
		  
	register_activation_hook(__FILE__, 'wpsg_install');
	register_deactivation_hook(__FILE__, 'wpsg_uninstall');
	
	add_action('admin_menu', 'wpsg_add_pages');	
	add_action('phpmailer_init', 'wpsg_phpmailer_init');
	add_action('shutdown', 'wpsg_shutdown');
	add_action('wp_loaded', array($GLOBALS['wpsg_sc'], 'wp_load'));
	
	add_filter('upgrader_pre_install', 'wpsg_pre_install', 10, 2);
	add_filter('upgrader_post_install', 'wpsg_post_install', 10, 2);
	
	if (is_admin())
	{
				
		if ($GLOBALS['wpsg_sc']->hasMod('wpsg_mod_core')) 
		{
		
			add_filter('plugins_api', 'wpsg_info', 10, 3);
			add_filter('site_transient_update_plugins', 'wpsg_update');
			
		}
		
		add_action('admin_enqueue_scripts', array($GLOBALS['wpsg_sc'], 'wp_enqueue'));
		add_action('admin_init', 'wpsg_admin_init');
		add_action('admin_footer', 'wpsg_admin_footer');
				
	} 
	else
	{
		
		add_shortcode('wpshopgermany', array($GLOBALS['wpsg_sc'], 'shortcode'));
		
		add_filter('the_title', array($GLOBALS['wpsg_sc'], 'the_filter'), 10, 2);
		add_filter('the_content', array($GLOBALS['wpsg_sc'], 'content_filter'));	
		add_filter('the_excerpt', array($GLOBALS['wpsg_sc'], 'the_excerpt'));	 
		add_filter('pre_get_posts', array($GLOBALS['wpsg_sc'], 'pre_get_posts'));
				
		add_action('wp_enqueue_scripts', array($GLOBALS['wpsg_sc'], 'wp_enqueue'));
		add_action('wp_head', array($GLOBALS['wpsg_sc'], 'wp_head'));
		add_action('wp_footer', array($GLOBALS['wpsg_sc'], 'wp_foot'));
		add_action('template_redirect', array($GLOBALS['wpsg_sc'], 'template_redirect'));
		
		if ($GLOBALS['wpsg_sc']->get_option('wpsg_debugModus') == '1') add_action('admin_bar_menu', 'wpsg_admin_bar_menu', 2000);
								
		if ($GLOBALS['wpsg_sc']->get_option('wpsg_removeWpTrimExcerpt') == '1') remove_filter('get_the_excerpt', 'wp_trim_excerpt');
		if ($GLOBALS['wpsg_sc']->get_option('wpsg_removeWpAutoOp') == '1') remove_filter('the_content', 'wpautop');
		
	}

	add_action('widgets_init', array($GLOBALS['wpsg_sc'], 'widget_init'));
	//add_action('widgets_init', create_function('', 'return register_widget("wpsg_basket_widget");'));
	
	$GLOBALS['wpsg_sc']->callMods('load', array());
	
?>