class-quick-donation-functions.php 14.9 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',
Varun Sridharan's avatar
Varun Sridharan committed
17
			'cart/mini-cart.php' => 'cart/donation-mini-cart.php',
18 19
        ),

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

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

Varun Sridharan's avatar
Varun Sridharan committed
83 84 85 86 87 88 89 90 91 92
            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';

                    }
93 94
                }

Varun Sridharan's avatar
Varun Sridharan committed
95 96 97 98 99 100 101 102 103
                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' );
                    }
                }
            }
        }
104 105 106
        return $title;    
    }
    
107 108
    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
109 110
        $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');
111 112 113 114 115 116 117 118 119 120 121
        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
122
            'posts_per_page'   => -1,
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
            '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;
    }
    
     
158
    public function generate_donation_selbox($grouped = false,$type = 'select',$selected=''){
159 160
        global $id, $name, $class, $field_output, $is_grouped, $project_list,$attributes;
        $field_output = '';
161
		
162 163 164 165 166 167
        $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);
168
		
169 170 171 172 173
        $class = implode(' ',$class);
        $attributes = '';
        foreach($custom_attributes as $attr_key => $attr_val) {
            $attributes .= $attr_key.'="'.$attr_val.'" ';
        }
Varun Sridharan's avatar
Varun Sridharan committed
174 175 176 177 178 179 180

        $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, 
181
																					 'pre_selected' => $selected,
Varun Sridharan's avatar
Varun Sridharan committed
182
                                                                                     'attributes' => $attributes));
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        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
201 202 203 204 205 206 207 208
        

        $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));        
209 210 211 212
        
        return $field_output;
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
213 214
    public function load_template($file,$path,$args = array()){
        $field_output = '';
215
        $wc_get_template = function_exists('wc_get_template') ? 'wc_get_template' : 'woocommerce_get_template';
Varun Sridharan's avatar
Varun Sridharan committed
216 217 218 219 220
        ob_start();
        $wc_get_template( $file,$args, '', $path); 
        $field_output = ob_get_clean(); 
        ob_flush();
        return $field_output;
221 222 223 224
    }
    
    public function locate_template($template){
        $default_path = WC_QD_TEMPLATE;
Varun Sridharan's avatar
Varun Sridharan committed
225
        $template_path = WC_CORE_TEMPLATE.'donation/';
226
        $template = $template;
227
        $locate = wc_locate_template($template,$template_path, $default_path); 
228 229 230 231
        return $locate;
    }
    
    public function wc_locate_template($template_full_path,$template_name,$template_dir){
Varun Sridharan's avatar
Varun Sridharan committed
232 233
        if(file_exists($template_full_path)){ return $template_full_path; }
        
234 235 236 237 238
        $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
239 240 241 242 243 244 245
        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];
        }
        
246 247 248 249 250 251 252
        return $template_full_path;
    }
    
    
    public function remove_gateway($gateways){
        if(WC_QD()->check_donation_exists_cart()){
           $allowed_gateway = WC_QD()->settings()->get_option(WC_QD_DB.'payment_gateway');
253 254
           if($allowed_gateway === false){return $gateways;}
			foreach($gateways as $gateway){
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 284 285 286 287 288
                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
289
        $file = ''; 
Varun Sridharan's avatar
Varun Sridharan committed
290
        $order_id = 0;
Varun Sridharan's avatar
Varun Sridharan committed
291
        $found = false;
292 293
        if(isset($args['order_id'])){ $order_id = $args['order_id']; }
        if(isset($args['order']->id)){ $order_id = $args['order']->id; }
294

295 296
        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
297
            $found = true;
298 299 300
        }
        
        if(WC_QD()->check_donation_exists_cart()){
301 302
            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
303
                $found = true;
304
            } 
305 306 307 308 309 310
        } 
        
    
        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
311
                $found = true;
312
            } 
313
        }
Varun Sridharan's avatar
Varun Sridharan committed
314
        
Varun Sridharan's avatar
Varun Sridharan committed
315 316 317 318 319 320 321
        if($found){
            return $file;
        } else {
            $file = wc_locate_template($template_name);
            return $file;
        }
        return $located;
322
    }    
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	
	public function modify_wp_count_posts($old_status,$type, $perm = '' ) {
		global $wpdb;
		 
		if ( ! post_type_exists( $type ) ) { return new stdClass;}
		$cache_key = _count_posts_cache_key( $type, $perm );
		$counts = wp_cache_get( $cache_key, 'wc_qd_modified_wp_count_posts' );
				
		if ( false !== $counts ) { return apply_filters( 'wc_qd_modified_wp_count_posts', $counts, $type, $perm ); }
		$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE  ";
		$query .= " ID NOT IN (SELECT donationid FROM `".WC_QD_TB."`) ";
		$query .= " AND post_type = %s ";
		if ( 'readable' == $perm && is_user_logged_in() ) {
			$post_type_object = get_post_type_object($type);
			if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
				$query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
					get_current_user_id()
				);
			}
		}
		$query .= ' GROUP BY post_status';
		$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
		$counts = array_fill_keys( get_post_stati(), 0 );
		foreach ( $results as $row ) { $counts[ $row['post_status'] ] = $row['num_posts']; }
		$counts = (object) $counts;
		wp_cache_set( $cache_key, $counts, 'wc_qd_modified_wp_count_posts' );
		return apply_filters( 'wc_qd_modified_wp_count_posts', $counts, $type, $perm );
	}	
351
}