class-quick-donation-functions.php 13.4 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
            'myaccount/my-donations.php' => 'myaccount/my-donations.php',
17 18
        ),

19 20 21 22 23 24
        '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',
Varun Sridharan's avatar
Varun Sridharan committed
25
            
26 27 28 29 30 31 32 33 34 35 36 37
            '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',
        ),
        
38 39
        'after_order' => array(
            'checkout/thankyou.php' => 'checkout/donation-thankyou.php',
Varun Sridharan's avatar
Varun Sridharan committed
40
            'myaccount/view-order.php' => 'myaccount/view-donation.php',
41

Varun Sridharan's avatar
Varun Sridharan committed
42 43 44 45 46
            'order/order-details.php' => 'order/donation-order-details.php',
            'order/order-details-item.php' => 'order/donation-order-details-item.php',
            'order/order-details-customer.php' => 'order/donation-order-details-customer.php',

            'emails/email-styles.php' => 'emails/donation-email-styles.php',
47
            
Varun Sridharan's avatar
Varun Sridharan committed
48
            'emails/donation-admin-new.php' => 'emails/donation-admin-new.php',
49
            'emails/email-addresses.php' => 'emails/donation-email-addresses.php',
Varun Sridharan's avatar
Varun Sridharan committed
50 51
            'emails/donation-email-footer.php' => 'emails/donation-email-footer.php',
            'emails/donation-email-header.php' => 'emails/donation-email-header.php',
52 53 54
            'emails/email-order-items.php' => 'emails/donation-email-order-items.php',
            'emails/plain/email-addresses.php' => 'emails/plain/donation-email-addresses.php',
            'emails/plain/email-order-items.php' => 'emails/plain/donation-email-order-items.php',
Varun Sridharan's avatar
Varun Sridharan committed
55
            'emails/plain/donation-customer-invoice.php' => 'emails/plain/donation-customer-invoice.php',
Varun Sridharan's avatar
Varun Sridharan committed
56 57 58
            
            'emails/donation-processing.php' => 'emails/donation-processing.php',
            'emails/plain/donation-processing.php' => 'emails/plain/donation-processing.php',
59 60
        )
        
61 62 63 64 65 66 67
        );    
    
    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);
Varun Sridharan's avatar
Varun Sridharan committed
68
        add_filter( 'the_title', array($this,'wc_page_endpoint_title' ),10,2);
69 70
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
71 72 73 74
    public function get_template_list(){
        return self::$search_template;
    }
    
75
    
Varun Sridharan's avatar
Varun Sridharan committed
76
    public function wc_page_endpoint_title($title = '', $id = ''){
Varun Sridharan's avatar
Varun Sridharan committed
77 78
        if(is_page($id)){
            global $wp_query;
79

Varun Sridharan's avatar
Varun Sridharan committed
80 81 82 83 84 85 86 87 88 89
            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';

                    }
90 91
                }

Varun Sridharan's avatar
Varun Sridharan committed
92 93 94 95 96 97 98 99 100
                if('view-order' == $endpoint){
                    $order_id = $wp_query->query['view-order'];
                    if(WC_QD()->db()->_is_donation($order_id)){
                        $title = 'Donation #'.$order_id;
                        remove_filter( 'the_title', 'wc_page_endpoint_title' );
                    }
                }
            }
        }
101 102 103
        return $title;    
    }
    
104 105
    public function add_email_classes($email_classes){
        $email_classes[WC_QD_DB.'new_donation_email'] = require(WC_QD_INC.'emails/class-new-email.php');
Varun Sridharan's avatar
Varun Sridharan committed
106 107
        $email_classes[WC_QD_DB.'donation_processing_email'] = require(WC_QD_INC.'emails/class-processing-email.php');
        $email_classes[WC_QD_DB.'donation_completed_email'] = require(WC_QD_INC.'emails/class-completed-email.php');
108 109 110 111 112 113 114 115 116 117 118
        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(
Varun Sridharan's avatar
Varun Sridharan committed
119
            'posts_per_page'   => -1,
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
            '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.'" ';
        }
Varun Sridharan's avatar
Varun Sridharan committed
170 171 172 173 174 175 176 177

        $field_output = $this->load_template('field-'.$type.'.php', WC_QD_TEMPLATE.'fields/' , array('id' => $id, 
                                                                                     'name' => $name, 
                                                                                     'class' => $class, 
                                                                                     'field_output' => $field_output, 
                                                                                     'is_grouped' => $is_grouped, 
                                                                                     'project_list' => $project_list, 
                                                                                     'attributes' => $attributes));
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        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.'" ';
        }
        
Varun Sridharan's avatar
Varun Sridharan committed
196 197 198 199 200 201 202 203
        

        $field_output = $this->load_template('field-text.php',WC_QD_TEMPLATE . 'fields/' ,array('id' => $id,
                                                                                'name' => $name,
                                                                                'class' => $class,
                                                                                'field_output' => $field_output,
                                                                                'attributes' => $attributes,
                                                                                'value' => $value));        
204 205 206 207
        
        return $field_output;
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
208 209
    public function load_template($file,$path,$args = array()){
        $field_output = '';
210
        $wc_get_template = function_exists('wc_get_template') ? 'wc_get_template' : 'woocommerce_get_template';
Varun Sridharan's avatar
Varun Sridharan committed
211 212 213 214 215
        ob_start();
        $wc_get_template( $file,$args, '', $path); 
        $field_output = ob_get_clean(); 
        ob_flush();
        return $field_output;
216 217 218 219
    }
    
    public function locate_template($template){
        $default_path = WC_QD_TEMPLATE;
Varun Sridharan's avatar
Varun Sridharan committed
220
        $template_path = WC_CORE_TEMPLATE.'donation/';
221
        $template = $template;
222
        $locate = wc_locate_template($template,$template_path, $default_path); 
223 224 225 226
        return $locate;
    }
    
    public function wc_locate_template($template_full_path,$template_name,$template_dir){
Varun Sridharan's avatar
Varun Sridharan committed
227 228
        if(file_exists($template_full_path)){ return $template_full_path; }
        
229 230 231 232 233
        $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];
        } 
Varun Sridharan's avatar
Varun Sridharan committed
234 235 236 237 238 239 240
        if(isset(self::$search_template['is_donation'][$template_name])){
            $template_full_path = WC_QD_TEMPLATE.self::$search_template['is_donation'][$template_name];
        }
        if(isset(self::$search_template['after_order'][$template_name])){
            $template_full_path = WC_QD_TEMPLATE.self::$search_template['after_order'][$template_name];
        }
        
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 277 278 279 280 281 282 283
        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 ){
Varun Sridharan's avatar
Varun Sridharan committed
284
        $file = ''; 
Varun Sridharan's avatar
Varun Sridharan committed
285
        $order_id = 0;
Varun Sridharan's avatar
Varun Sridharan committed
286
        $found = false;
287 288
        if(isset($args['order_id'])){ $order_id = $args['order_id']; }
        if(isset($args['order']->id)){ $order_id = $args['order']->id; }
289

290 291
        if(isset(self::$search_template['general'][$template_name])){
            $file = WC_QD()->f()->locate_template(self::$search_template['general'][$template_name]);
Varun Sridharan's avatar
Varun Sridharan committed
292
            $found = true;
293 294 295
        }
        
        if(WC_QD()->check_donation_exists_cart()){
296 297
            if(isset(self::$search_template['is_donation'][$template_name])){
                $file = WC_QD()->f()->locate_template(self::$search_template['is_donation'][$template_name]);
Varun Sridharan's avatar
Varun Sridharan committed
298
                $found = true;
299
            } 
300 301 302 303 304 305
        } 
        
    
        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]);
Varun Sridharan's avatar
Varun Sridharan committed
306
                $found = true;
307
            } 
308
        }
Varun Sridharan's avatar
Varun Sridharan committed
309
        
Varun Sridharan's avatar
Varun Sridharan committed
310 311 312 313 314 315 316
        if($found){
            return $file;
        } else {
            $file = wc_locate_template($template_name);
            return $file;
        }
        return $located;
317 318
    }    
}