woocommerce-quick-donation.php 8.51 KB
Newer Older
1 2 3
<?php
/**
 * Plugin Name:       WooCommerce Quick Donation
Varun Sridharan's avatar
Varun Sridharan committed
4
 * Plugin URI:        http://wordpress.org/plugins/woocommerce-quick-donation/
5
 * Description:       Turns WooCommerce Into Online Donation
Varun Sridharan's avatar
Varun Sridharan committed
6
 * Version:           1.3.5 BETA
7 8 9
 * Author:            Varun Sridharan
 * Author URI:        http://varunsridharan.in
 * Text Domain:       woocommerce-quick-donation
Varun Sridharan's avatar
Varun Sridharan committed
10
 * Domain Path:       /languages/
11 12 13 14 15 16 17 18 19 20 21
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt 
 * GitHub Plugin URI: https://github.com/technofreaky/woocomerce-quick-donation
 */

if ( ! defined( 'WPINC' ) ) { die; }
 
class WooCommerce_Quick_Donation {
	/**
	 * @var string
	 */
Varun Sridharan's avatar
Varun Sridharan committed
22
	public $version = '1.3.5';
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

	/**
	 * @var WooCommerce The single instance of the class
	 * @since 2.1
	 */
	protected static $_instance = null;
    public static $is_donation_product_exist = true;
    protected static $f = null;
    public static $shortcode = null;
    public static $donation_id = null;
    public static $settings = null;
    public static $settings_values = null;
    private static $db = null;
    /**
     * Creates or returns an instance of this class.
     */
    public static function get_instance() {
        if ( null == self::$_instance ) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }
    
    /**
     * Class Constructor
     */
    public function __construct() {
        $this->define_constant();
        self::$donation_id = get_option(WC_QD_DB.'product_id');
        $this->define('WC_QD_ID',intval(get_option(WC_QD_DB.'product_id')));
        $this->load_required_files();
        register_activation_hook( __FILE__,array('WC_QD_INSTALL','INIT') );
        add_action( 'init', array( $this, 'init' ));
    }
    
    /**
     * Triggers When INIT Action Called
     */
    public function init(){
        $this->init_class();
        $this->check_donation_product_exist();
        add_action('plugins_loaded', array( $this, 'after_plugins_loaded' ));
        add_filter('load_textdomain_mofile',  array( $this, 'load_plugin_mo_files' ), 10, 2);
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
68 69 70
    /**
     * Checks If Donation Product Exist In WooCommerce Products
     */
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    private function check_donation_product_exist(){
        $install = new WC_QD_INSTALL;
        if(! $install->check_donation_exists()){
            self::$is_donation_product_exist = false;
            wc_qd_notice('WooCommerce Donation Product Not Exist','error');
        }
    }
    
    /**
     * Checks If Donation Product Exists In Cart
     */
    public function check_donation_exists_cart(){
        global $woocommerce;
        $found = false;
        if( sizeof($woocommerce->cart->get_cart()) > 0){
            foreach($woocommerce->cart->get_cart() as $cart_item_key=>$values){
                $_product = $values['data'];
                if($_product->id == self::$donation_id)
                    $found = true;
            }

        }
        return $found;
    }
    /**
     * Loads Required Plugins For Plugin
     */
    private function load_required_files(){
Varun Sridharan's avatar
Varun Sridharan committed
99
        $this->load_files(WC_QD_INC.'wc-quick-donation-*.php'); 
100 101 102 103 104 105 106 107 108
        $this->load_files(WC_QD_ADMIN.'wps/*.php'); 
        $this->load_files(WC_QD_INC.'class-admin-notice.php');
        $this->load_files(WC_QD_INC.'class-post-*.php');
        $this->load_files(WC_QD_INC.'class-quick-donation-db.php');
        $this->load_files(WC_QD_INC.'class-install.php');
        $this->load_files(WC_QD_INC.'class-quick-donation-functions.php');
        
        if($this->is_request('frontend')){
            $this->load_files(WC_QD_INC.'class-quick-donation-process.php');
Varun Sridharan's avatar
Varun Sridharan committed
109
            $this->load_files(WC_QD_INC.'class-wc-myaccount-functions.php');
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            $this->load_files(WC_QD_INC.'class-shortcode-handler.php');
        }
        
        if($this->is_request('admin')){
           $this->load_files(WC_QD_ADMIN.'class-*.php');
        } 

    }
    
    /**
     * Inits loaded Class
     */
    private function init_class(){
        
        self::$f = new WooCommerce_Quick_Donation_Functions;
        self::$settings = new WooCommerce_Quick_Donation_Settings;
Varun Sridharan's avatar
Varun Sridharan committed
126 127
        self::$db = new WooCommerce_Quick_Donation_DB; 
        
128 129
        if($this->is_request('frontend')){
            self::$shortcode = new WooCommerce_Quick_Donation_Shortcode;
Varun Sridharan's avatar
Varun Sridharan committed
130 131
            $this->donation  =  new WooCommerce_Quick_Donation_Process;
            $this->my_account = new WooCommerce_Quick_Donation_MyAccount_Fuctions;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        }
        
        if($this->is_request('admin')){
            $this->admin = new WooCommerce_Quick_Donation_Admin;
        }
 
    }
    
    /**
     * Function Get Call Admin
     */
    public function admin(){
        return $this->admin;
    }
    
    /**
     * Returns Function Class 
     */
    public function f(){
        return self::$f;
    }
    
    /**
     * Retruns DB Class 
     */
    public function db(){
        return self::$db;
    }
    
    /**
     * Returns Settings Class 
     */
    public function settings(){
        return self::$settings;
    }
    
    /**
     * Gets Settings From DB 
     */
     public function get_option($key = ''){
        var_dump(self::$settings_values);
     }
    
    /**
     * Loads The Files From Given Path
     */
    public function load_files($path,$type = 'require'){
        foreach( glob( $path ) as $files ){

            if($type == 'require'){
                require_once( $files );
            } else if($type == 'include'){
                include_once( $files );
            }
            
        } 
    }
    
    /**
     * Set Plugin Text Domain
     */
    public function after_plugins_loaded(){
        load_plugin_textdomain(WC_QD_TXT, false, WC_QD_LANG );
    }
    
    /**
     * load translated mo file based on wp settings
     */
    public function load_plugin_mo_files($mofile, $domain) {
Varun Sridharan's avatar
Varun Sridharan committed
201 202 203
        if (WC_QD_TXT === $domain){ 
            return WC_QD_LANG.'/'.get_locale().'/'.get_locale().'.mo';
        }
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        return $mofile;
    }
    
    /**
     * Define Required Constant
     */
    private function define_constant(){
        global $wpdb;
        $this->define('WC_QD','WooCommerce Quick Donation'); # Plugin Name
        $this->define('WC_QD_SLUG','wc-qd'); # Plugin Slug
        
        $this->define('WC_QD_DB_V','1.0');
        $this->define('WC_QD_V','1.0');
        
        $this->define('WC_QD_FILE',plugin_basename( __FILE__ ));
        
        $this->define('WC_QD_PATH',plugin_dir_path( __FILE__ )); # Plugin DIR
        $this->define('WC_QD_INC',WC_QD_PATH.'includes/');
        $this->define('WC_QD_ADMIN',WC_QD_INC.'admin/');
        
        $this->define('WC_QD_URL',plugins_url('', __FILE__ )); 
        $this->define('WC_QD_ADMIN_URL',WC_QD_URL.'/includes/admin/');
        $this->define('WC_QD_JS',WC_QD_URL.'/includes/js/');
        $this->define('WC_QD_CSS',WC_QD_URL.'/includes/css/');
        
        $this->define('WC_QD_TEMPLATE',WC_QD_PATH.'template/'); # Plugin Template DIR
Varun Sridharan's avatar
Varun Sridharan committed
230 231
        $this->define('WC_CORE_TEMPLATE','woocommerce/');
        $this->define('WC_QD_THEME_TEMPLATE','/'.WC_CORE_TEMPLATE.'donation/');
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
        $this->define('WC_QD_LANG',WC_QD_PATH.'languages');
        $this->define('WC_QD_TXT','woocommerce-quick-donation'); #plugin lang Domain

        $this->define('WC_QD_TB',$wpdb->prefix . 'wc_quick_donation');
        $this->define('WC_QD_DB','wc_qd_');
        $this->define('WC_QD_PT','wcqd_project');
        $this->define('WC_QD_CAT','wcqd_category');
        $this->define('WC_QD_TAG','wcqd_tags'); 
    }
    
    /**
	 * Define constant if not already set
	 * @param  string $name
	 * @param  string|bool $value
	 */
    protected function define($key,$value){
        if(!defined($key)){
            define($key,$value);
        }
    }
    
    
    
	/**
	 * What type of request is this?
	 * string $type ajax, frontend or admin
	 * @return bool
	 */
	private function is_request( $type ) {
		switch ( $type ) {
			case 'admin' :
				return is_admin();
			case 'ajax' :
				return defined( 'DOING_AJAX' );
			case 'cron' :
				return defined( 'DOING_CRON' );
			case 'frontend' :
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
		}
	}
    
    
    
}

Varun Sridharan's avatar
Varun Sridharan committed
277 278 279 280 281 282 283
require_once(plugin_dir_path(__FILE__).'includes/class-wc-dependencies.php');
require_once(plugin_dir_path(__FILE__).'includes/class-admin-notice.php');

if(WC_QD_Dependencies()){
    function WC_QD(){
        return WooCommerce_Quick_Donation::get_instance();
    }
284

Varun Sridharan's avatar
Varun Sridharan committed
285 286
    $GLOBALS['woocommerce_quick_donation'] =  WC_QD();
} else {
Varun Sridharan's avatar
Varun Sridharan committed
287
    wc_qd_notice(__('WooCommerce Is Required. To Use This Plugin :)','woocommerce-quick-donation'),'error');
Varun Sridharan's avatar
Varun Sridharan committed
288
}
289 290

?>