class-admin-functions.php 6.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://wordpress.org/plugins/woocommerce-role-based-price/
 *
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    @TODO
 * @subpackage @TODO
 * @author     Varun Sridharan <varunsridharan23@gmail.com>
 */
if ( ! defined( 'WPINC' ) ) { die; }

class WooCommerce_Quick_Donation_Admin_Function {
    
Varun Sridharan's avatar
Varun Sridharan committed
20
    public function __construct(){ 
21
        add_action( 'post_row_actions', array($this,'protect_donation_product'),99,2);
Varun Sridharan's avatar
Varun Sridharan committed
22
        add_action( 'parse_query', array( $this, 'hide_donation_order_woocommerce_order' ) );
Varun Sridharan's avatar
Varun Sridharan committed
23
        add_filter( 'wc_order_types',array($this,'add_wc_order_types'),1,2);
24
		add_action( 'delete_post', array($this,'delete_donation'));
25
		
Varun Sridharan's avatar
Varun Sridharan committed
26
		add_action( 'wp_ajax_CreateDonationProduct', array($this,'create_donation_product') );
27 28
		add_action( 'wp_ajax_ClearDonationLog', array($this,'clear_wc_qd_logs') );
		
29
    }   
30
    
Varun Sridharan's avatar
Varun Sridharan committed
31
    
32 33 34 35 36 37 38
	public function delete_donation($id){
		if('shop_order' == get_post_type($id)){
			if(wcqd_is_donation($id)){
				WC_QD()->db()->delete_donation_entry($id);
			}
		}
	}
Varun Sridharan's avatar
Varun Sridharan committed
39
    
40 41 42
    public function hide_donation_order_woocommerce_order($query) {
        global $pagenow,$post_type;  
        $query = $query;
Varun Sridharan's avatar
Varun Sridharan committed
43
        
44 45 46 47 48 49 50 51 52
        if(!defined('WC_QD_QRY_OVERRIDE')){
            if( 'edit.php' == $pagenow || $query->is_admin && 'shop_order' == $post_type){ 
                $query->set('meta_query',array('relation' => 'AND', array('key' => '_is_donation','compare' => 'NOT EXISTS')));
            }
        }
        return $query;
    }    
    
     
53
    public function add_wc_order_types($order_types,$type){ 
Varun Sridharan's avatar
Varun Sridharan committed
54 55
        $order_type = $order_types;  
        
56
        if('' == $type){
Varun Sridharan's avatar
Varun Sridharan committed
57 58
            $order_type[] = WC_QD_PT;  
            $order_type[] = 'wc_qd_orders';
59
        } 
Varun Sridharan's avatar
Varun Sridharan committed
60 61 62 63 64 65
        
        if('shop_order' == $type){
            $order_type[] = WC_QD_PT;  
            $order_type[] = 'wc_qd_orders';
        }
        
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        return $order_type;
    }
    
	/**
	* 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 == WC_QD_ID){
				unset($actions['inline hide-if-no-js']);
				unset($actions['trash']);
				unset($actions['duplicate']);
                $text = __('Remove '.WC_QD.' Plugin To Remove This Product',WC_QD_TXT);
				$actions['trash'] = '<a href="javascript:alert(\' '.$text.' \');"> Trash </a>';
				
			}
		}
        return $actions;
Varun Sridharan's avatar
Varun Sridharan committed
90 91
	}  
    
Varun Sridharan's avatar
Varun Sridharan committed
92
   
Varun Sridharan's avatar
Varun Sridharan committed
93
    
Varun Sridharan's avatar
Varun Sridharan committed
94
	
Varun Sridharan's avatar
Varun Sridharan committed
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
    public function get_OverRided(){
        $template_files = WC_QD_INSTALL::get_template_list();
        $overrided = array();
        if(is_array($template_files)){
            foreach($template_files as $file){
                $theme_file = false;
                if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
                    $theme_file = get_stylesheet_directory() . '/' . $file;
                } elseif ( file_exists( get_stylesheet_directory() . WC_QD_THEME_TEMPLATE . $file ) ) {
                    $theme_file = get_stylesheet_directory() . WC_QD_THEME_TEMPLATE . $file;
                } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
                    $theme_file = get_template_directory() . '/' . $file;
                } elseif( file_exists( get_template_directory() . WC_QD_THEME_TEMPLATE . $file ) ) {
                    $theme_file = get_template_directory() . WC_QD_THEME_TEMPLATE . $file;
                }   
                
                if ( $theme_file !== false ) {
                    $core_version  = WC_Admin_Status::get_file_version(WC_QD_TEMPLATE.$file);
                    $theme_version = WC_Admin_Status::get_file_version( $theme_file );
                    if ( $core_version && $theme_version && version_compare( $theme_version, $core_version, '<' ) ) {
                        $overrided[] = array('file' => $file,'corev' => $core_version , 'themev' => $theme_version, 'is_old' => true);
                    } else {
                        $overrided[] = array('file' => $file,'corev' => $core_version , 'themev' => $theme_version, 'is_old' => false);
                    }
                    
                }
            }
        }
        return $overrided;
Varun Sridharan's avatar
Varun Sridharan committed
124 125 126 127 128
    }
	
	
	public function create_donation_product(){
		if(! isset($_REQUEST['_wpnonce'])) {
129
			echo '<span class="wc_qd_error">'.__('Invalid Nonce. kindly try again',WC_QD_TXT).'</span>';
Varun Sridharan's avatar
Varun Sridharan committed
130 131 132 133 134 135 136 137 138 139
			exit;
		}
		
		if(wp_verify_nonce($_REQUEST['_wpnonce'], 'CreateDonationProduct')){
			$type = 'simple';
			$install = new WC_QD_INSTALL;
			$callBack_function = 'create_'.$type.'_donation';
			$donation_exist = $install::check_donation_exists();
			
			if(isset($_REQUEST['force'])){
140
				$post_id = $install->$callBack_function(); 
Varun Sridharan's avatar
Varun Sridharan committed
141 142 143 144 145 146
				update_option(WC_QD_DB.'product_id',$post_id); 
				echo '<span class="wcqdsuccess">'.__('Donation Product Created',WC_QD_TXT).'</span>';
				exit;
			}
			
			if(! $donation_exist){
147
				$post_id = $install->$callBack_function(); 
Varun Sridharan's avatar
Varun Sridharan committed
148 149 150 151 152 153 154 155 156 157 158 159
				update_option(WC_QD_DB.'product_id',$post_id); 
				echo '<span class="wcqdsuccess">'.__('Donation Product Created',WC_QD_TXT).'</span>';
			} else {
				$url = wp_nonce_url(admin_url('admin-ajax.php?action=CreateDonationProduct&force=true'),'CreateDonationProduct'); 
				echo '<button href="'.$url.'" 
				       class="wcqdAjaxCall button wcqdAutoRemove" type="button">
					   '.__('Force Create Donation Product',WC_QD_TXT).'</button>';
			}
		}  
		
		exit;
	}
160 161 162 163 164 165 166 167 168 169 170 171 172 173
	
	public function clear_wc_qd_logs(){ 
		if(! isset($_REQUEST['_wpnonce'])) {
			echo '<span class="wc_qd_error">'.__('Invalid Nonce. kindly try again',WC_QD_TXT).'</span>';
			exit;
		}

		if(wp_verify_nonce($_REQUEST['_wpnonce'], 'ClearDonationLog')){	
			$delete = WC_QD()->db()->delete_donation_logs();
			echo '<span class="wcqdsuccess">'.sprintf(__('%s Records Deleted',WC_QD_TXT),$delete).'</span>';
		}
		
		exit;
	}
174
}