class-admin-functions.php 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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 {
    
    public function __construct(){
Varun Sridharan's avatar
Varun Sridharan committed
21
        $this->check_template_files();
22
        add_action( 'post_row_actions', array($this,'protect_donation_product'),99,2);
Varun Sridharan's avatar
Varun Sridharan committed
23
        add_action( 'parse_query', array( $this, 'hide_donation_order_woocommerce_order' ) );
24
        add_filter( 'wc_order_types',array($this,'add_wc_order_types'),99,2);
25
    }   
26
    
Varun Sridharan's avatar
Varun Sridharan committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    
    public function check_template_files(){
        $template_files = WC_Admin_Status::scan_template_files( WC_QD_TEMPLATE );
        $outdated = false;
        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, '<' ) ) {
                        $outdated = true;
                        break;
                    }
                }
            }
            
                
            if ( $outdated ) {
                $theme = wp_get_theme();
                $message = sprintf( __( '<strong>Your theme (%s) contains outdated copies of some WooCommerce Quick Donation template files.</strong> These files may need updating to ensure they are compatible with the current version of WooCommerce Quick Donation. You can see which files are affected from the %ssystem status page%s. If in doubt, check with the author of the theme.', 'woocommerce' ), esc_html( $theme['Name'] ), '<a href="' . esc_url( admin_url( 'admin.php?page=wc-status' ) ) . '">', '</a>' );
                wc_qd_notice($message,'error');
            }        
        }
        
    

    }
    
68 69 70
    public function hide_donation_order_woocommerce_order($query) {
        global $pagenow,$post_type;  
        $query = $query;
Varun Sridharan's avatar
Varun Sridharan committed
71
        
72 73 74 75 76 77 78 79 80
        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;
    }    
    
     
81
    public function add_wc_order_types($order_types,$type){ 
82
        $order_type = $order_types;
Varun Sridharan's avatar
Varun Sridharan committed
83
        global $post_type; 
84 85
        if('' == $type){
            $order_type[] = WC_QD_PT; 
Varun Sridharan's avatar
Varun Sridharan committed
86
            $order_type[] = 'wcqd_project'; 
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
        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;
	}    
}