woocommerce-quick-donation.php 17.6 KB
Newer Older
Tech No Freaky's avatar
Tech No Freaky committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/*  Copyright 2014  Varun Sridharan  (email : varunsridharan23@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
Tech No Freaky's avatar
Tech No Freaky committed
15
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
Tech No Freaky's avatar
Tech No Freaky committed
16 17 18 19
 
    Plugin Name: Woocommerce Quick Donation
    Plugin URI: http://varunsridharan.in/
    Description: Woocommerce Quick Donation
Tech No Freaky's avatar
Tech No Freaky committed
20
    Version: 1.2
Tech No Freaky's avatar
Tech No Freaky committed
21 22 23 24 25 26 27 28 29 30
    Author: Varun Sridharan
    Author URI: http://varunsridharan.in/
    License: GPL2
*/
defined('ABSPATH') or die("No script kiddies please!"); 
define( 'wc_qd_u', plugin_dir_url( __FILE__ ) );
define( 'wc_qd_p', plugin_dir_path( __FILE__ ) );

class wc_quick_donation{
	
Tech No Freaky's avatar
Tech No Freaky committed
31 32
	public $donation_id;
    private $plugin_v;
Tech No Freaky's avatar
Tech No Freaky committed
33 34 35 36 37 38
	
	/**
	 * Setup The Plugin Class
	 */
	function __construct() {
		$this->donation_id = get_option('wc_quick_donation_product_id');
Tech No Freaky's avatar
Tech No Freaky committed
39
        $this->plugin_v = '1.2';
Tech No Freaky's avatar
Tech No Freaky committed
40
		add_shortcode( 'wc_quick_donation', array($this,'shortcode_handler' ));
Tech No Freaky's avatar
Tech No Freaky committed
41 42
        
        add_action( 'post_row_actions', array($this,'protect_donation_product'),99,2);
Tech No Freaky's avatar
Tech No Freaky committed
43 44
        add_action( 'get_the_generator_html',  array($this,'generate_meta_tags'), 15, 2 );
        add_action( 'get_the_generator_xhtml', array($this,'generate_meta_tags'), 15, 2 );        
Tech No Freaky's avatar
Tech No Freaky committed
45
		add_action( 'wp_loaded',array($this,'process_donation'),20);  
Tech No Freaky's avatar
Tech No Freaky committed
46 47 48 49 50 51
		add_action( 'wc_qd_show_projects_list',array($this,'get_projects_list'));	
    
		add_action( 'woocommerce_checkout_update_order_meta',  array($this,'save_order_id_db'));
        add_action( 'woocommerce_add_order_item_meta',  array($this,'add_order_meta'),99,3);
        
		
Tech No Freaky's avatar
Tech No Freaky committed
52
		add_action( 'woocommerce_admin_order_data_after_billing_address', array($this,'custom_order_details_page_info'), 10, 1 ); 
Tech No Freaky's avatar
Tech No Freaky committed
53
        add_action( 'woocommerce_available_payment_gateways',array($this,'remove_gateway'));
Tech No Freaky's avatar
Tech No Freaky committed
54
        
Tech No Freaky's avatar
Tech No Freaky committed
55
        add_filter('woocommerce_hidden_order_itemmeta',array($this,'hide_core_fields'));
Tech No Freaky's avatar
Tech No Freaky committed
56 57
        add_filter( 'woocommerce_get_price', array($this,'get_price'),10,2);
		add_filter( 'woocommerce_get_settings_pages',  array($this,'settings_page') );
Tech No Freaky's avatar
Tech No Freaky committed
58 59
		add_filter( 'woocommerce_email_classes',  array($this,'email_classes'));
		add_filter('woocommerce_enable_order_notes_field',array($this,'order_notes'));
Tech No Freaky's avatar
Tech No Freaky committed
60 61
	}
 
Tech No Freaky's avatar
Tech No Freaky committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
	/**
	 * Hides Order Notes
	 * @return boolean [[Description]]
	 * @since 1.2
	 */
	public function order_notes(){ 
		if($this->donation_exsits() &&  $this->only_donation_exsits()){ 
			if(get_option('wc_quick_donation_hide_order_notes') === true){
				return false;
			}
		}
		return true;
	}
	
Tech No Freaky's avatar
Tech No Freaky committed
76 77 78 79 80 81 82 83 84 85
    /**
     * Adds Donation Meta Tag
     * @param   String $gen  Refer WP.ORG
     * @param   String $type Refer WP.ORG 
     * @returns String
     * @since 0.4
     */
    public function generate_meta_tags( $gen, $type ) {
        switch ( $type ) {
            case 'html':
Tech No Freaky's avatar
Tech No Freaky committed
86
                $gen .= "\n" . '<meta name="generator" content="WooCommerce Quick Donation '.$this->plugin_v.'">';
Tech No Freaky's avatar
Tech No Freaky committed
87 88
                break;
            case 'xhtml':
Tech No Freaky's avatar
Tech No Freaky committed
89
                $gen .= "\n" . '<meta name="generator" content="WooCommerce Quick Donation '.$this->plugin_v.'" />';
Tech No Freaky's avatar
Tech No Freaky committed
90 91 92 93 94
                break;
        }
        return $gen;
    }    
    
Tech No Freaky's avatar
Tech No Freaky committed
95 96 97 98 99 100 101 102 103 104 105 106
    /**
     * Hides Some Important Fields 
     * @since 1.0
     * @param   [[Type]] $fields [[Description]]
     * @returns [[Type]] [[Description]]
     */
    public function hide_core_fields($fields){
        $fields[] = '_is_donation';
        $fields[] = '_project_details';
        return $fields;
    }
                   
Tech No Freaky's avatar
Tech No Freaky committed
107
	/**
Tech No Freaky's avatar
Tech No Freaky committed
108
	 * Adds Settings Page
Tech No Freaky's avatar
Tech No Freaky committed
109
	 */
Tech No Freaky's avatar
Tech No Freaky committed
110
 	public function settings_page( $settings ) {
Tech No Freaky's avatar
Tech No Freaky committed
111
		$settings[] = include( wc_qd_p.'wc_qd_settings.php' );  
Tech No Freaky's avatar
Tech No Freaky committed
112 113 114 115 116 117 118
		return $settings;
	}
	 
	/**
	 * Adds Email Classes
	 */
	public function email_classes($email_classes){
Tech No Freaky's avatar
Tech No Freaky committed
119 120
		require_once( wc_qd_p.'wc_qd_email_processing.php' );
		require_once( wc_qd_p.'wc_qd_email_completed.php' );
Tech No Freaky's avatar
Tech No Freaky committed
121 122 123 124
		$email_classes['wc_quick_donation_processing_donation_email'] = new wc_quick_donation_processing_donation_email();
		$email_classes['wc_quick_donation_completed_donation_email'] = new wc_quick_donation_completed_donation_email();
		return $email_classes;		
	}	
Tech No Freaky's avatar
Tech No Freaky committed
125
	
Tech No Freaky's avatar
Tech No Freaky committed
126 127 128
	/**
	 * Adds Donation Order Meta. [Project Name]
	 * @param [[Type]] $order_id [[Description]]
Tech No Freaky's avatar
Tech No Freaky committed
129 130
	 * @since 0.2
     * @updated 1.0
Tech No Freaky's avatar
Tech No Freaky committed
131
	 */
Tech No Freaky's avatar
Tech No Freaky committed
132 133 134 135 136 137
	public function add_order_meta( $item_id, $values, $cart_item_key) { 
        if($this->donation_id == $values['product_id']){
            global $woocommerce;
            wc_add_order_item_meta( $item_id, "_project_details",$woocommerce->session->projects);	
            wc_add_order_item_meta( $item_id, "_is_donation",'yes');
        }
Tech No Freaky's avatar
Tech No Freaky committed
138
	} 
Tech No Freaky's avatar
Tech No Freaky committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    
    /**
     * Saves Donation Order ID and adds order notes...
     * @param [[Type]] $order_id [[Description]]
     * @since 1.0
     * @adopedFrom add_order_meta
     */
    public function save_order_id_db($order_id){
        $order = new WC_Order($order_id);
        $items = $order->get_items();
        foreach($items as $item){
            $order_product_id = floatval($item['product_id']);
            if($this->donation_id == $order_product_id){
                $this->update_order_id($order_id);
                $format = sprintf(get_option('wc_quick_donation_order_notes_title'), $woocommerce->session->projects);
                $order->add_order_note($format);
                update_post_meta($order_id,'_is_donation',true);
            }
        }
        unset($order);
        return $order_id;
    }
Tech No Freaky's avatar
Tech No Freaky committed
161
 
Tech No Freaky's avatar
Tech No Freaky committed
162

Tech No Freaky's avatar
Tech No Freaky committed
163
    /**
Tech No Freaky's avatar
Tech No Freaky committed
164
     * Updates Order ID to [wc_quick_donation_ids] when donation is ordered
Tech No Freaky's avatar
Tech No Freaky committed
165 166 167 168
     * @param [int] $order_id [Donation Order ID]
     * @since 1.0
     */
    private function update_order_id($order_id){
Tech No Freaky's avatar
Tech No Freaky committed
169
        $ordersID = get_option('wc_quick_donation_ids');
Tech No Freaky's avatar
Tech No Freaky committed
170 171 172 173 174 175 176
        $save_order_id = array();
        if(empty($ordersID)){
            $save_order_id[] = $order_id;
        } else {
            $save_order_id = json_decode($ordersID,true);
            $save_order_id[] = $order_id;
        }
Tech No Freaky's avatar
Tech No Freaky committed
177
        update_option('wc_quick_donation_ids',json_encode($save_order_id));
Tech No Freaky's avatar
Tech No Freaky committed
178 179
        
    }
Tech No Freaky's avatar
Tech No Freaky committed
180
	
Tech No Freaky's avatar
Tech No Freaky committed
181 182 183 184 185
	/**
	 * Custom Title In Order View Page
	 * @since 1.0
	 */
	public function custom_order_details_page_info($order){
Tech No Freaky's avatar
Tech No Freaky committed
186 187 188 189 190 191
        $is_donation = wc_get_order_item_meta($order->id, '_is_donation');
        if($is_donation === true){
            echo '<p><strong>'.get_option('wc_quick_donation_project_section_title').' :</strong>'.wc_get_order_item_meta($order->id, '_project_details') . '</p>';
        }
        
		
Tech No Freaky's avatar
Tech No Freaky committed
192 193 194
	} 	
	
	
Tech No Freaky's avatar
Tech No Freaky committed
195 196
	 
    
Tech No Freaky's avatar
Tech No Freaky committed
197
	/**
Tech No Freaky's avatar
Tech No Freaky committed
198 199
	 * Get All Enabled And Avaiable Payment Gateway To List In Settings Page
	 * @returns array [Aviable Gateways]
Tech No Freaky's avatar
Tech No Freaky committed
200
	 */
Tech No Freaky's avatar
Tech No Freaky committed
201 202 203 204 205 206 207 208 209
	public function get_payments_gateway(){
		$payment = WC()->payment_gateways->payment_gateways();
		$gateways = array();
		foreach($payment as $gateway){
			if ( $gateway->enabled == 'yes' ){
				$gateways[$gateway->id] = $gateway->title;
			}
		}
		return $gateways;
Tech No Freaky's avatar
Tech No Freaky committed
210 211 212 213 214 215 216 217
	}
	
	
	/**
	 * Check's For Donation Product Exist In Cart
	 * @returns Boolean True|False
	 */
	public function donation_exsits(){
Tech No Freaky's avatar
Tech No Freaky committed
218
		global $woocommerce; 
219
		if(is_object($woocommerce->cart) && sizeof($woocommerce->cart->get_cart()) > 0){
Tech No Freaky's avatar
Tech No Freaky committed
220 221 222 223 224 225 226 227 228 229 230
			foreach($woocommerce->cart->get_cart() as $cart_item_key => $values){
				$_product = $values['data'];
				if($_product->id == $this->donation_id){
					return true;	
				}				
			}
		}
		return false;
	}	
	
	
Tech No Freaky's avatar
Tech No Freaky committed
231 232 233 234 235 236 237 238 239 240 241 242
	
	/**
	 * Check's For Donation Product Exist In Cart
	 * @returns Boolean True|False
	 */
	public function only_donation_exsits(){
		global $woocommerce;
        if( sizeof($woocommerce->cart->get_cart()) == 1 && $this->donation_exsits()){
			return true;	
		}
		return false;
	}		
Tech No Freaky's avatar
Tech No Freaky committed
243 244 245 246 247 248 249 250 251 252 253 254 255
	/**
	 * Gets Donation Current Price
	 * @param  $price 
	 * @param  $product 
	 * @returns 0 | price
	 */
	public function get_price($price, $product){
		global $woocommerce;
		if($product->id == $this->donation_id){ 
			return isset($woocommerce->session->jc_donation) ? floatval($woocommerce->session->jc_donation) : 0; 
		}
		return $price;
	}	
Tech No Freaky's avatar
Tech No Freaky committed
256
 
Tech No Freaky's avatar
Tech No Freaky committed
257 258 259 260 261 262 263
	
	/**
	 * Process The Given Donation
	 */
	public function process_donation(){
		global $woocommerce;
		
Tech No Freaky's avatar
Tech No Freaky committed
264 265 266
		
		if(isset($_POST['donation_add'])){
			$error = 0;
Tech No Freaky's avatar
Tech No Freaky committed
267
			$found = false;
Tech No Freaky's avatar
Tech No Freaky committed
268
			$donation = isset($_POST['donation_ammount']) ? $_POST['donation_ammount'] : false;
Tech No Freaky's avatar
Tech No Freaky committed
269 270
			$projects = isset($_POST['projects']) && !empty($_POST['projects']) ? $_POST['projects'] : false;
			$_SESSION['wc_qd_projects'] = $projects;  
Tech No Freaky's avatar
Tech No Freaky committed
271 272 273
            $min = get_option('wc_quick_donation_min_required_donation');
            $max = get_option('wc_quick_donation_max_required_donation');

Tech No Freaky's avatar
Tech No Freaky committed
274 275

			if(isset($_POST['projects']) && $_POST['projects'] == '' ){
Tech No Freaky's avatar
Tech No Freaky committed
276
				wc_add_notice(get_option('wc_quick_donation_msg_project_invalid'), 'error' );
Tech No Freaky's avatar
Tech No Freaky committed
277 278 279
				$error += 1;
			}	
			
Tech No Freaky's avatar
Tech No Freaky committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            if(! empty($donation)){
                $donate_price = floatval($donation); 
                if($donate_price != 0 || $donate_price != null){
                    if($donate_price < $min){
                        $message = str_replace(array('{donation_amount}','{min_amount}'),array($donation,$min),get_option('wc_quick_donation_msg_amount_min_required'));
                        wc_add_notice($message, 'error' );
                        $error += 1;
                    }
                    
                    if($donate_price > $max){
                         $message = str_replace(array('{donation_amount}','{max_amount}'),array($donation,$max),get_option('wc_quick_donation_msg_amount_max_allowed'));
                        wc_add_notice($message, 'error' );
                        $error += 1;
                    }
                    
                } else {
                    $message = str_replace('{donation_amount}',$donation,get_option('wc_quick_donation_msg_amount_invalid'));
                    wc_add_notice($message, 'error' );
                    $error += 1;
                }
                
            } else {
                wc_add_notice(get_option('wc_quick_donation_msg_amount_empty'), 'error' );
                $error += 1;
            } 
Tech No Freaky's avatar
Tech No Freaky committed
305 306 307 308 309 310 311 312 313 314 315
			if($error ==0 ) {
                if($donation >= 0){
                    $woocommerce->session->jc_donation = $donation;
                    $woocommerce->session->projects = $projects; 
                    if( sizeof($woocommerce->cart->get_cart()) > 0){
                        foreach($woocommerce->cart->get_cart() as $cart_item_key=>$values){
                            $_product = $values['data'];
                            if($_product->id == $this->donation_id)
                                $found = true;
                        }

Tech No Freaky's avatar
Tech No Freaky committed
316 317 318 319 320 321 322 323
                        if(! $found){ 
							$this->add_donation_cart();
						 	
						} else {
							wc_add_notice(get_option('wc_quick_donation_msg_donation_exist'), 'error' );
						}
							
                            
Tech No Freaky's avatar
Tech No Freaky committed
324 325 326 327 328
                    }else{
                        $this->add_donation_cart();
                    }
                }			
            }
Tech No Freaky's avatar
Tech No Freaky committed
329
		}
Tech No Freaky's avatar
Tech No Freaky committed
330 331
		
 
Tech No Freaky's avatar
Tech No Freaky committed
332 333
	}	
	
Tech No Freaky's avatar
Tech No Freaky committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	/**
	 * Adds Donation Product To Cart
	 */
	private function add_donation_cart(){
		global $woocommerce; 
		$this->remove_cart_items();
		$woocommerce->cart->add_to_cart($this->donation_id);
        $this->redirectCART();
		
	}

	/**
	 * Redirect To Checkout Page After Donation is Added
	 */
	public function redirectCART(){
		global $woocommerce; 
        $redirect_op = get_option('wc_quick_donation_redirect');
        if($redirect_op == 'cart'){
            wp_safe_redirect(WC()->cart->get_cart_url() );exit; 
        } else if($redirect_op == 'checkout'){
            wp_safe_redirect(WC()->cart->get_checkout_url() );exit; 
        }
		 
	}
	
	
	/**
	 * Allowes only selected payment gateway for donation product.
	 * @since 0.2
	 * @access public
	 */
	public function remove_gateway($gateway){ 
		if($this->donation_exsits() &&  $this->only_donation_exsits()){
			$payments = get_option('wc_quick_donation_payment_gateway'); 
			
			if(!empty($payments)){
				foreach($gateway as $val){
					if(! in_array($val->id,$payments)){
						unset($gateway[$val->id]);
					}  
				}
			}
			return $gateway;	
		} 
		return $gateway;	
	}
	
	/**
	 * Removes Cart ITEM if donation is added
	 * @returns Boolean [[Description]]
	 */
Tech No Freaky's avatar
Tech No Freaky committed
385 386 387 388 389 390 391 392 393 394 395
	private function remove_cart_items(){
		$cart_remove = get_option('wc_quick_donation_cart_remove');
		if(isset($cart_remove) && $cart_remove == 'true'){
			global $woocommerce;
			$woocommerce->cart->empty_cart();
			return true;
		}
		return true;
	}


Tech No Freaky's avatar
Tech No Freaky committed
396 397
	/**
	 * Gets Donation Form.
Tech No Freaky's avatar
Tech No Freaky committed
398
	 * @updated 1.2
Tech No Freaky's avatar
Tech No Freaky committed
399 400
	 */
	public function wc_qd_form(){
Tech No Freaky's avatar
Tech No Freaky committed
401
		global $woocommerce; 
Tech No Freaky's avatar
Tech No Freaky committed
402
        
Tech No Freaky's avatar
Tech No Freaky committed
403 404 405 406 407
		$donate = isset($woocommerce->session->jc_donation) ? floatval($woocommerce->session->jc_donation) : 0;
		if(!$this->donation_exsits()){  
			unset($woocommerce->session->jc_donation); 
			unset($woocommerce->session->projects); 
		}
Tech No Freaky's avatar
Tech No Freaky committed
408
		
Tech No Freaky's avatar
Tech No Freaky committed
409
		// $donate = jc_round_donation($woocommerce->cart->total ); 
Tech No Freaky's avatar
Tech No Freaky committed
410 411 412 413 414 415
		$show_form_donatio_exist = get_option('wc_quick_donation_hide_form');
		if($this->donation_exsits() && $show_form_donatio_exist){
			$this->_load_donation_form();
		}else if(! $this->donation_exsits()){
			$this->_load_donation_form();
			
Tech No Freaky's avatar
Tech No Freaky committed
416
		}
Tech No Freaky's avatar
Tech No Freaky committed
417 418
		
		 
Tech No Freaky's avatar
Tech No Freaky committed
419 420
	}	
	 
Tech No Freaky's avatar
Tech No Freaky committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
 	/**
 	 * Requires Donation Form
 	 * Actions 1 wc_quick_donation_before_form
 	 * Actions 2 wc_quick_donation_after_form
 	 * @since 1.2
 	 */
 	private function _load_donation_form(){
		do_action('wc_quick_donation_before_form');
		$wc_get_template = function_exists('wc_get_template') ? 'wc_get_template' : 'woocommerce_get_template';
		$wc_get_template( 'donation_form.php', array(), '', wc_qd_p . 'template/' ); 
		do_action('wc_quick_donation_after_form');
	}
	
	public function donation_projects(){
		$projects_db = get_option('wc_quick_donation_projects');
		if(!empty($projects_db) && $projects_db != null){
			$project = explode(',',$projects_db);
			return $project;
		}
		return false;
	}
	
Tech No Freaky's avatar
Tech No Freaky committed
443
	
Tech No Freaky's avatar
Tech No Freaky committed
444 445
	/**
	 * Generates Select Box For Projects List
Tech No Freaky's avatar
Tech No Freaky committed
446
	 * @updated 1.2
Tech No Freaky's avatar
Tech No Freaky committed
447
	 */
Tech No Freaky's avatar
Tech No Freaky committed
448
	public function get_projects_list(){
Tech No Freaky's avatar
Tech No Freaky committed
449
			$projects_db = $this->donation_projects();
Tech No Freaky's avatar
Tech No Freaky committed
450 451 452
			if($projects_db){
				$project_list = '';
				$project_list .= '<option value="" > Select A Project </option>';
Tech No Freaky's avatar
Tech No Freaky committed
453
				foreach($projects_db as $proj){
Tech No Freaky's avatar
Tech No Freaky committed
454 455 456 457 458 459 460 461 462 463 464
					$project_list .= '<option value="'.$proj.'" > '.$proj.'</option>';
				}
				echo '<select name="projects">'.$project_list.'</select>';
			}
	}
	
	
	/**
	 * wc_quick_donation shortcode Handler
	 */
	public function shortcode_handler(){
Tech No Freaky's avatar
Tech No Freaky committed
465
		return $this->wc_qd_form();
Tech No Freaky's avatar
Tech No Freaky committed
466 467 468 469 470 471
	}
	
	
	/**
	 * Install The Plugin
	 */
Tech No Freaky's avatar
Tech No Freaky committed
472
	public static function install() {
Tech No Freaky's avatar
Tech No Freaky committed
473
		$exist = get_option('wc_quick_donation_product_id');
Tech No Freaky's avatar
Tech No Freaky committed
474

Tech No Freaky's avatar
Tech No Freaky committed
475
		if($exist){
Tech No Freaky's avatar
Tech No Freaky committed
476 477 478 479 480 481 482 483
            if ( get_post_status ( $exist ) ) {
                return true;
            } else {
                $post_id = create_donation();
                add_option('wc_quick_donation_product_id',$post_id); 
                add_site_option( 'wc_quick_donation_product_id', $post_id) ;
            }
			
Tech No Freaky's avatar
Tech No Freaky committed
484 485
		} else { 
            $post_id = create_donation();
Tech No Freaky's avatar
Tech No Freaky committed
486
			add_option('wc_quick_donation_product_id',$post_id); 
Tech No Freaky's avatar
Tech No Freaky committed
487
            add_option('wc_quick_donation_ids','');
Tech No Freaky's avatar
Tech No Freaky committed
488 489 490
		    add_site_option( 'wc_quick_donation_product_id', $post_id) ;
		}
	}
Tech No Freaky's avatar
Tech No Freaky committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	
	/**
	* Protects Donation Product By 
	* @filter_user post_row_actions
	* @param  Array $actions Refer WP.org
	* @param  Array $post    Refer WP.org
	* @return Array Refer WP.org
	* @since 1.0
	*/
	public function protect_donation_product($actions,$post) {
		if('product' == $post->post_type) {  
			if($post->ID == $this->donation_id){
				unset($actions['inline hide-if-no-js']);
				unset($actions['trash']);
				unset($actions['duplicate']);
				$actions['trash'] = '<a href="javascript:alert(\'Remove Woocommerce Quick Donation Plugin To Remove This Product \');"> Trash </a>';
				
			}
		}
        return $actions;
	}
Tech No Freaky's avatar
Tech No Freaky committed
512 513
     
}
Tech No Freaky's avatar
Tech No Freaky committed
514

Tech No Freaky's avatar
Tech No Freaky committed
515 516 517 518 519

function create_donation(){
    $userID = 1;
    if(get_current_user_id()){
        $userID = get_current_user_id();
Tech No Freaky's avatar
Tech No Freaky committed
520
    }
Tech No Freaky's avatar
Tech No Freaky committed
521 522 523 524 525 526 527
    $post = array(
        'post_author' => $userID,
        'post_content' => 'Used For Donation',
        'post_status' => 'publish',
        'post_title' => 'Donation',
        'post_type' => 'product',
    );
Tech No Freaky's avatar
Tech No Freaky committed
528

Tech No Freaky's avatar
Tech No Freaky committed
529
    $post_id = wp_insert_post($post);  
Tech No Freaky's avatar
Tech No Freaky committed
530 531 532 533 534 535 536 537 538 539 540 541
    update_post_meta($post_id, '_stock_status', 'instock');
    update_post_meta($post_id, '_tax_status', 'none');
    update_post_meta($post_id, '_tax_class',  'zero-rate');
    update_post_meta($post_id, '_visibility', 'hidden');
    update_post_meta($post_id, '_stock', '');
    update_post_meta($post_id, '_virtual', 'yes');
    update_post_meta($post_id, '_featured', 'no');
    update_post_meta($post_id, '_manage_stock', "no" );
    update_post_meta($post_id, '_sold_individually', "yes" );
    update_post_meta($post_id, '_sku', 'checkout-donation');   			
    return $post_id;
}
Tech No Freaky's avatar
Tech No Freaky committed
542 543 544 545 546 547 548

/**
 * Check if WooCommerce is active 
 * if yes then call the class
 */
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
	register_activation_hook( __FILE__, array( 'wc_quick_donation', 'install' ) );
Tech No Freaky's avatar
Tech No Freaky committed
549
	$wc_quick_donation = new wc_quick_donation;
Tech No Freaky's avatar
Tech No Freaky committed
550 551 552 553 554 555 556 557
    require(wc_qd_p.'wc_qd_donation_orders.php');
    require(wc_qd_p.'wc_qd_widget.php');
    
    function register_foo_widget() {
        register_widget( 'woocommerce_quick_donation_widget' );
    }
    add_action( 'widgets_init', 'register_foo_widget' );
    
Tech No Freaky's avatar
Tech No Freaky committed
558 559
} else {
	add_action( 'admin_notices', 'wc_quick_donation_notice' );
Tech No Freaky's avatar
Tech No Freaky committed
560 561
}

Tech No Freaky's avatar
Tech No Freaky committed
562 563 564
function wc_quick_donation_notice() {
	echo '<div class="error"><p><strong> <i> Woocommerce Quick Donation </i> </strong> Requires <a href="'.admin_url( 'plugin-install.php?tab=plugin-information&plugin=woocommerce').'"> <strong> <u>Woocommerce</u></strong>  </a> To Be Installed And Activated </p></div>';
}
565
?>