class-install.php 8.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

class WC_QD_INSTALL{

    /**
     * Inits Install Hook
     */
    public static function init(){
        $donation_exist = self::check_donation_exists();
        self::check_db_version();
Varun Sridharan's avatar
Varun Sridharan committed
11
        self::post_register();
12
        self::wc_qd_table_install();
Varun Sridharan's avatar
Varun Sridharan committed
13
        self::check_template_files();
14
		self::add_options();
Varun Sridharan's avatar
Varun Sridharan committed
15
        if(! $donation_exist){
Varun Sridharan's avatar
Varun Sridharan committed
16
            $post_id = self::create_simple_donation(); 
17
			update_option(WC_QD_DB.'product_id',$post_id); 
Varun Sridharan's avatar
Varun Sridharan committed
18
        }
19
		
Varun Sridharan's avatar
Varun Sridharan committed
20 21
    }
    
22 23 24
    /**
     * Registers Custom Post, Custom Taxonomy
     */
Varun Sridharan's avatar
Varun Sridharan committed
25 26 27 28 29
    public static function post_register(){
        WC_QD_Post_Types::register_donation_posttype();
        WC_QD_Post_Types::register_donation_category();
        WC_QD_Post_Types::register_donation_tags();
        flush_rewrite_rules();
30 31
    }

Varun Sridharan's avatar
Varun Sridharan committed
32 33 34
    /**
     * Checks Upgrade Status
     */
35 36 37 38 39 40 41 42 43 44 45 46
    public static function check_db_version(){
        $current_version = get_option(WC_QD_DB.'db_version');
        if(! $current_version){
            add_option(WC_QD_DB.'db_version', WC_QD_DB_V);
        } 
    }
    
    /**
     * Checks Donation Product Exists
     */
    public static function check_donation_exists(){
        $exist = get_option(WC_QD_DB.'product_id');
47
        if($exist && get_post_status ($exist)){ return true; }
48 49 50
        return false;
    }
    
Varun Sridharan's avatar
Varun Sridharan committed
51 52 53
    /**
     * Create Quick Donation Table
     */
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    public static function wc_qd_table_install() {
        global $wpdb;
        global $jal_db_version;

        $table_name = WC_QD_TB;
        $charset_collate = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE $table_name (
            id bigint(9) NOT NULL AUTO_INCREMENT,
            date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
            userid bigint(20) NOT NULL,
            donationid bigint(20) NOT NULL,
            projectid bigint(20) NOT NULL, 
            UNIQUE KEY id (id)
        ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql ); 
    }    
73 74 75 76 77 78 79 80 81 82 83
	

	public static function add_options(){
		$general_settings = self::general_section_settings();
		$message_settings = self::message_section_settings();
		$shortcode_settings = self::shortcode_section_settings();

		add_option( 'wc_qd_general', $general_settings);
		add_option('wc_qd_message', $message_settings);
		add_option('wc_qd_shortcode',$shortcode_settings);
		add_option('wc_qd_anh_notices', '');
84 85 86 87 88 89 90 91 92 93 94
		
		$message = __('Please Configure Any One Payment Gatway To Get Plugin Work :) ', WC_QD_TXT);
		$message = sprintf(
			__( '<p>Please Configure Any One Payment Gatway To Get Plugin Work :) </p> 
			     <p class="submit">%s Config Gateway %s %s</p>',
			  WC_QD_TXT),   
			'<a class="button button-primary" href="' . admin_url( 'edit.php?post_type='.WC_QD_PT.'&page=wc_qd_settings' ) . '">','</a>', 
			wc_qd_remove_link('class="button" ') 

		);
		wc_qd_notice($message,'error',array('times' => 0,'wraper' => false));		 
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
	}
	
	
	private static function general_section_settings(){
		$s = array();
		$s['section_id'] = 'general';
		$s[WC_QD_DB_SETTINGS.'_redirect_user'] = 'checkout';
		$s[WC_QD_DB_SETTINGS.'_already_exist_redirect_user'] = 'cart';
		return $s;
	}
	
	private static function message_section_settings(){
		$s = array();
		$s['section_id'] = 'message';
		$s[WC_QD_DB_SETTINGS.'_donation_with_other_products'] = 'Unable To Process Donation. Kindly Complete The Exist Order ';
		$s[WC_QD_DB_SETTINGS.'_empty_donation_msg'] = 'Please Enter A Donation Amount';
		$s[WC_QD_DB_SETTINGS.'_donation_already_exist'] = 'Donation Already Exist In Cart. Kindly Complete The Existing';
		$s[WC_QD_DB_SETTINGS.'_invalid_donation_msg'] = 'Invalid Donation Amount Entered [{donation_amount}]';
		$s[WC_QD_DB_SETTINGS.'_min_rda_msg'] = 'Please Enter Minimum of {min_amount}.';
		$s[WC_QD_DB_SETTINGS.'_max_rda_msg'] = 'Please Enter Maximum of {max_amount}.';	
		return $s;
	}

	private static function shortcode_section_settings(){
		$s['section_id'] = 'shortcode';
		$s[WC_QD_DB_SETTINGS.'_default_render_type'] = 'select';
		$s[WC_QD_DB_SETTINGS.'_shortcode_show_errors'] = 'true';
		$s[WC_QD_DB_SETTINGS.'_pre_selected_project'] = '';	
		return $s;
	}

	
127
     
Varun Sridharan's avatar
Varun Sridharan committed
128 129 130 131
    /**
     * Create Donation Product In WooCommerce
     * @return int donation Post id
     */
Varun Sridharan's avatar
Varun Sridharan committed
132
    public static function create_simple_donation(){
133
        $userID = 1;
Varun Sridharan's avatar
Varun Sridharan committed
134
        if(get_current_user_id()){ $userID = get_current_user_id(); }
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        
        $post = array(
            'post_author' => $userID,
            'post_content' => 'Used For Donation',
            'post_status' => 'publish',
            'post_title' => 'Donation',
            'post_type' => 'product',
        );
        
        $post_id = wp_insert_post($post);  
        update_post_meta($post_id, '_stock_status', 'instock');
        update_post_meta($post_id, '_tax_status', 'none');
        update_post_meta($post_id, '_tax_class',  'zero-rate');
        update_post_meta($post_id, '_visibility', 'hidden');
        update_post_meta($post_id, '_stock', '');
        update_post_meta($post_id, '_virtual', 'yes');
        update_post_meta($post_id, '_featured', 'no');
        update_post_meta($post_id, '_manage_stock', "no" );
        update_post_meta($post_id, '_sold_individually', "yes" );
        update_post_meta($post_id, '_sku', 'checkout-donation');   			
        return $post_id;
    }
157 158 159
    /**
     * Gets ALl Core Templates
     */
Varun Sridharan's avatar
Varun Sridharan committed
160 161 162 163 164 165
    public static function get_template_list(){
        $core_tempalte_list = WC_Admin_Status::scan_template_files( WC_QD_TEMPLATE );
        return $core_tempalte_list;
    }
    
    
166 167 168
    /**
     * Checks For Template Version 
     */
Varun Sridharan's avatar
Varun Sridharan committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    public static function check_template_files(){
        $template_files = self::get_template_list();
        $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(
                    __( '<p> <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. </p> <p class="submit">%sLear More About Templates%s %s</p>',
202
                      WC_QD_TXT), 
Varun Sridharan's avatar
Varun Sridharan committed
203 204 205 206 207 208 209 210 211 212
                    esc_html( $theme['Name'] ), 
                    '<a href="' . admin_url( 'admin.php?page=wc-status' ) . '">','</a>',
                    '<a  target="_blank" href="" class="button-primary" href="">','</a>', 
                    wc_qd_remove_link('class="button" ') 
                
                );
                wc_qd_notice($message,'error',array('times' => 0,'wraper' => false));
            }        
        }
    }    
213 214
}

Varun Sridharan's avatar
Varun Sridharan committed
215
?>