class-quick-donation-functions.php 9.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/**
 * functionality of the plugin.
 * @author  Varun Sridharan <varunsridharan23@gmail.com>
 */
if ( ! defined( 'WPINC' ) ) { die; }

class WooCommerce_Quick_Donation_Functions  {
    protected static $project_db_list = null;
    public static $search_template =   array(
        'general' => array(
            'donation-form.php' => 'donation-form.php',
            'field-radio.php' => 'fields/field-radio.php',
            'field-select.php' => 'fields/field-select.php',
            'field-text.php' => 'fields/field-text.php',
Varun Sridharan's avatar
Varun Sridharan committed
16
            
17 18
            'emails/donation-customer-invoice.php' => 'emails/donation-customer-invoice.php',
            'emails/plaint/donation-customer-invoice.php' => 'emails/plain/donation-customer-invoice.php',
Varun Sridharan's avatar
Varun Sridharan committed
19 20
            
            'myaccount/my-donations.php' => 'myaccount/my-donations.php',
21 22
        ),

23 24 25 26 27 28
        'is_donation' => array( 
            'cart/cart-item-data.php' => 'cart/donation-cart-item-data',
            'cart/cart-shipping.php' => 'cart/donation-cart-shipping.php',
            'cart/cart-totals.php' => 'cart/donation-cart-totals.php',
            'cart/cart.php' => 'cart/donation-cart.php',
            'cart/proceed-to-checkout-button.php' => 'cart/donation-proceed-to-checkout-button.php',
29 30 31 32 33 34 35 36 37 38 39 40
            'checkout/cart-errors.php' => 'checkout/donation-cart-errors.php',
            'checkout/form-billing.php' => 'checkout/donation-form-billing.php',
            'checkout/form-checkout.php' => 'checkout/donation-form-checkout.php',
            'checkout/form-coupon.php' => 'checkout/donation-form-coupon.php',
            'checkout/form-login.php' => 'checkout/donation-form-login.php',
            'checkout/form-pay.php' => 'checkout/donation-form-pay.php',
            'checkout/form-shipping.php' => 'checkout/donation-form-shipping.php',
            'checkout/payment-method.php' => 'checkout/donation-payment-method.php',
            'checkout/payment.php' => 'checkout/donation-payment.php',
            'checkout/review-order.php' => 'checkout/donation-review-order.php',
        ),
        
41 42 43 44 45
        'after_order' => array(
            'order/order-details.php' => 'order/donation-order-details.php',
            'checkout/thankyou.php' => 'checkout/donation-thankyou.php',
        )
        
46 47 48 49 50 51 52 53
        
        );    
    
    function __construct(){
        add_filter( 'wc_get_template',array($this,'get_template'),10,5);
        add_filter( 'woocommerce_email_classes',  array($this,'add_email_classes'));
        add_action( 'woocommerce_available_payment_gateways',array($this,'remove_gateway'));
        add_filter( 'woocommerce_locate_template' , array($this,'wc_locate_template'),10,3);
54
        add_filter( 'the_title', array($this,'wc_page_endpoint_title' ),1);
55 56
    }
    
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    public function wc_page_endpoint_title($title){
        global $wp_query;

        if ( ! is_null( $wp_query ) && ! is_admin() && is_main_query() && in_the_loop() && is_page() && is_wc_endpoint_url() ) {
            
            $endpoint = WC()->query->get_current_endpoint();
            if('order-received' == $endpoint){
                $order_id = $wp_query->query['order-received'];
                if(WC_QD()->db()->_is_donation($order_id)){
                    $title = 'Donation Received';
                    remove_filter( 'the_title', 'wc_page_endpoint_title' );
                }
            } 
        }

        return $title;    
    }
    
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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
    public function add_email_classes($email_classes){
        $email_classes[WC_QD_DB.'new_donation_email'] = require(WC_QD_INC.'emails/class-new-email.php');
        //$email_classes[WC_QD_DB.'processing_donation_email'] = require(WC_QD_INC.'emails/class-processing-email.php');
        //$email_classes[WC_QD_DB.'completed_donation_email'] = require(WC_QD_INC.'emails/class-completed-email.php');
        return $email_classes;
    }
    
    /**
     * Get Donation Project List From DB
     */
    public function get_donation_project_list(){
        if(self::$project_db_list != null || self::$project_db_list != ''){
            return self::$project_db_list;
        }
        $args = array(
            'posts_per_page'   => 0,
            'offset'           => 0,
            'category'         => '',
            'category_name'    => '',
            'orderby'          => 'date',
            'order'            => 'DESC',
            'include'          => '',
            'exclude'          => '',
            'meta_key'         => '',
            'meta_value'       => '',
            'post_type'        => WC_QD_PT,
            'post_mime_type'   => '',
            'post_parent'      => '',
            'author'	   => '',
            'post_status'      => 'publish',
            'suppress_filters' => true 
        );
        self::$project_db_list = get_posts($args);
        return self::$project_db_list;
    }
    
    public function get_porject_list($grouped = false){
        $list = $this->get_donation_project_list();
        $projects = array();
        foreach($list as $project){
            if($grouped){
                $term = get_the_terms( $project->ID, WC_QD_CAT );
                $projects[$term[0]->name][$project->ID] = $project->post_title;
            } else {
                $projects[$project->ID] = $project->post_title;
            } 
        }
        return $projects;
    }
    
     
    public function generate_donation_selbox($grouped = false,$type = 'select'){
        global $id, $name, $class, $field_output, $is_grouped, $project_list,$attributes;
        $field_output = '';
        $id = 'donation_project';
        $name = 'wc_qd_donate_project_name';
        $class = apply_filters('wcqd_project_name_'.$type.'_class',array(),$type);
        $custom_attributes = apply_filters('wcqd_project_name_'.$type.'_attribute',array(),$type);
        $is_grouped = $grouped;
        $project_list = $this->get_porject_list($grouped);
        
        $class = implode(' ',$class);
        $attributes = '';
        foreach($custom_attributes as $attr_key => $attr_val) {
            $attributes .= $attr_key.'="'.$attr_val.'" ';
        }
        
        $this->load_template('field-'.$type.'.php', WC_QD_TEMPLATE.'fields/' );
        
        return $field_output;
    }

    
    public function generate_price_box(){
        global $id, $name, $class, $field_output,$attributes,$value;
        $field_output = '';
        $id = 'donation_price';
        $name = 'wc_qd_donate_project_price';
        $class = apply_filters('wcqd_project_price_text_class',array(),'text');
        $custom_attributes = apply_filters('wcqd_project_price_text_attribute',array(),'text');
        $value = '';
        
        
        $class = implode(' ',$class);
        $attributes = '';
        foreach($custom_attributes as $attr_key => $attr_val) {
            $attributes .= $attr_key.'="'.$attr_val.'" ';
        }
        
        $this->load_template('field-text.php',WC_QD_TEMPLATE . 'fields/' );
        
        return $field_output;
    }
    
    public function load_template($file,$path){
        $wc_get_template = function_exists('wc_get_template') ? 'wc_get_template' : 'woocommerce_get_template';
        $wc_get_template( $file,array(), '', $path); 
    }
    
    public function locate_template($template){
        $default_path = WC_QD_TEMPLATE;
        $template_path = WC_TEMPLATE.'donation/';
        $template = $template;
178
        $locate = wc_locate_template($template,$template_path, $default_path); 
179 180 181 182
        return $locate;
    }
    
    public function wc_locate_template($template_full_path,$template_name,$template_dir){
Varun Sridharan's avatar
Varun Sridharan committed
183 184
        if(file_exists($template_full_path)){ return $template_full_path; }
        
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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 230 231 232 233 234
        $template_full_path = $template_full_path;

        if(isset(self::$search_template['general'][$template_name])){
            $template_full_path = WC_QD_TEMPLATE.self::$search_template['general'][$template_name];
        } 

        return $template_full_path;
    }
    
    
    public function remove_gateway($gateways){
        if(WC_QD()->check_donation_exists_cart()){
           // var_dump($gateway);
           $allowed_gateway = WC_QD()->settings()->get_option(WC_QD_DB.'payment_gateway');
           foreach($gateways as $gateway){
                if(! in_array($gateway->id,$allowed_gateway)){
                    unset($gateways[$gateway->id]);
                }
            }
        }
        
        return $gateways;
    }
 
    public function get_admin_pay_gate(){
        $gateway = $this->get_payment_gateways();
        if(! empty($gateway)){
            return $gateway;
        } else {
            wc_qd_notice(__('No Payment Gateway Configured In WooCommerce. Kindly Configure One',WC_QD_TXT),'error');
            
        }
        return array();
    }
    
    public function get_payment_gateways(){
        $payment = WC()->payment_gateways->payment_gateways();
		$gateways = array();

		foreach($payment as $gateway){
			if ( $gateway->enabled == 'yes' ){
				$gateways[$gateway->id] = $gateway->title;
			}
		}
        
		return $gateways;
    }
    
    public function get_template($located, $template_name, $args, $template_path, $default_path ){
        $file = $located; 
Varun Sridharan's avatar
Varun Sridharan committed
235
        $order_id = 0;
236 237 238
        if(isset($args['order_id'])){ $order_id = $args['order_id']; }
        if(isset($args['order']->id)){ $order_id = $args['order']->id; }
         
239 240
        if(isset(self::$search_template['general'][$template_name])){
            $file = WC_QD()->f()->locate_template(self::$search_template['general'][$template_name]);
241
        } else  if(WC_QD()->check_donation_exists_cart()){
242 243 244
            if(isset(self::$search_template['is_donation'][$template_name])){
                $file = WC_QD()->f()->locate_template(self::$search_template['is_donation'][$template_name]);
            } 
245 246 247 248 249 250
        } else {
            if(WC_QD()->db()->_is_donation($order_id)){
                if(isset(self::$search_template['after_order'][$template_name])){
                    $file = WC_QD()->f()->locate_template(self::$search_template['after_order'][$template_name]);
                }             
            }
251
        }
Varun Sridharan's avatar
Varun Sridharan committed
252
        
253 254 255
        return $file;
    }    
}