class-admin-init.php 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?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  {
    private $settings_page_hook;
    /**
	 * Initialize the class and set its properties.
	 * @since      0.1
	 */
	public function __construct() {
        $this->load_required_files(); 
        $this->init_hooks();
	}
    
    public function load_required_files(){
        WC_QD()->load_files(WC_QD_ADMIN.'metabox_framework/meta-box.php'); 
    } 
    
    public function init_hooks(){
        add_action( 'admin_menu', array( $this, 'sub_donation_order_menu' ) );
        add_action( 'admin_menu',  array($this,'add_donation_notification_bubble'),99);
        
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ),99);
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
        add_action( 'admin_init', array( $this, 'init_admin_class' ));
        add_filter( 'plugin_row_meta', array($this, 'plugin_row_links' ), 10, 2 );
        add_filter( 'woocommerce_screen_ids',array($this,'set_wc_screen_ids'),99);
        add_filter( 'custom_menu_order', array($this,'reorder_donation_menu' ));
        
    }
    
    public function sub_donation_order_menu(){
        
Varun Sridharan's avatar
Varun Sridharan committed
48 49 50 51 52 53 54 55 56 57 58 59 60
        $this->order_menu_slug = add_submenu_page('edit.php?post_type=wcqd_project',
                                                  __('Donation Orders',WC_QD_TXT),
                                                  __('Donation\'s',WC_QD_TXT),
                                                  'administrator',
                                                  'wc_qd_orders',
                                                  array($this,'donation_orders_page'));
        
        $this->donors_list = add_submenu_page('edit.php?post_type=wcqd_project',
                                                  __('Donors List',WC_QD_TXT),
                                                  __('Donors List',WC_QD_TXT),
                                                  'administrator',
                                                  'wc_qd_donors',
                                                  array($this,'donors_listing_page'));
Varun Sridharan's avatar
Varun Sridharan committed
61
        
Varun Sridharan's avatar
Varun Sridharan committed
62 63 64
        $this->tools = add_submenu_page('edit.php?post_type=wcqd_project',
                                                  __('System Tools',WC_QD_TXT),
                                                  __('System Tools',WC_QD_TXT),
Varun Sridharan's avatar
Varun Sridharan committed
65
                                                  'administrator',
Varun Sridharan's avatar
Varun Sridharan committed
66 67
                                                  'wc_qd_tools',
                                                  array($this,'system_tools'));
68 69 70 71 72 73 74
    }
    
    
    public function reorder_donation_menu ($menu_ord ) {
        global $submenu;
        //echo '<pre>'.print_r($submenu,true).'</pre>'; exit;
        $name = 'edit.php?post_type='.WC_QD_PT;
75
        if(empty($submenu)){return $submenu;}
76
        $arr = array();
Varun Sridharan's avatar
Varun Sridharan committed
77

78
        $arr[] = $submenu[$name][18];
Varun Sridharan's avatar
Varun Sridharan committed
79
        $arr[] = $submenu[$name][19];
80 81 82 83 84
        $arr[] = $submenu[$name][5];
        $arr[] = $submenu[$name][10];
        $arr[] = $submenu[$name][15];
        $arr[] = $submenu[$name][16];
        $arr[] = $submenu[$name][17];
Varun Sridharan's avatar
Varun Sridharan committed
85
        $arr[] = $submenu[$name][20];
86 87 88 89 90 91 92
        $submenu[$name] = $arr;
        return $menu_ord;
    }  
    
    
    public function add_donation_notification_bubble()  {
        global $submenu; 
Varun Sridharan's avatar
Varun Sridharan committed
93
        $c = $this->get_status_count();
94 95
        if(isset($submenu['edit.php?post_type='.WC_QD_PT])){
            foreach($submenu['edit.php?post_type='.WC_QD_PT] as $menuK => $menu){
Varun Sridharan's avatar
Varun Sridharan committed
96 97 98
                
                if($menu[2] == 'wc_qd_orders' ){
                    
99
                    $submenu['edit.php?post_type='.WC_QD_PT][$menuK][0] .=  "<span class='update-plugins count-1'>
Varun Sridharan's avatar
Varun Sridharan committed
100
                                                                             <span class='update-count'>$c</span></span>"; 
101 102
                }
            }
Varun Sridharan's avatar
Varun Sridharan committed
103
        } 
104 105
    }    
    
Varun Sridharan's avatar
Varun Sridharan committed
106 107 108 109 110 111 112 113 114 115 116 117
    private function get_status_count(){
        $order_ids = WC_QD()->db()->get_donation_order_ids(); 
        $count = 0;
        foreach($order_ids as $id){
            $order_status = get_post_status($id['donationid']); 
            if($order_status == 'wc-on-hold' || $order_status == 'wc-processing'){
                $count++; 
            }
        }
        
        return $count;
    }
118 119 120 121 122
    /**
     * Inits Admin Sttings
     */
    public function init_admin_class(){
        $this->functions =  new WooCommerce_Quick_Donation_Admin_Function;
Varun Sridharan's avatar
Varun Sridharan committed
123
        $this->admin_order_page = new WooCommerce_Quick_Donation_Admin_Order_Page_Functions;
124 125 126
    }
 
    
Varun Sridharan's avatar
Varun Sridharan committed
127 128 129 130 131 132 133 134
    public function donors_listing_page(){
        $ids = WC_QD()->db()->get_doners_ids();
        $ids = WC_QD()->db()->extract_donation_id($ids);
        $args = array('include'  => $ids); 
        require('wp-donors-listing-table.php');
        donor_render_list_page($args); 
    }
        
Varun Sridharan's avatar
Varun Sridharan committed
135 136 137 138 139 140
    public function system_tools(){
        require(WC_QD_ADMIN.'/views/tools.php');
        //require(WC_QD_ADMIN.'/sysinfo/sysinfo.php');
        //$sysinfo = new WooCommerce_Quick_Donation_SysInfo;
        //$sysinfo->setup();
        //$sysinfo->render_info();
Varun Sridharan's avatar
Varun Sridharan committed
141
    }
Varun Sridharan's avatar
Varun Sridharan committed
142
    
143 144 145 146 147 148 149
    public function donation_orders_page(){
        global $wpdb;

        define('WC_QD_QRY_OVERRIDE',true);
        $order_ids = WC_QD()->db()->get_donation_order_ids();
        $order_ids = WC_QD()->db()->extract_donation_id($order_ids);

Varun Sridharan's avatar
Varun Sridharan committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        $args = array('posts_per_page' => '0',
                      'post_type' => 'shop_order', 
                      'post_status' =>  array_keys(wc_get_order_statuses()),
                      'post__in' => $order_ids 
                     );
        
        if(isset($_GET['paged'])){$args['paged'] = $_GET['paged']; }
        if(isset($_GET['m'])){ $args['m'] = $_GET['m'];}        
        if(isset($_GET['_customer_user'])) { 
            $args['meta_query'][]['key'] = '_customer_user';
            $args['meta_query'][]['value'] = $_GET['_customer_user'];
            $args['meta_query'][]['compare'] = '=';
        }
        
        if(isset($_GET['dproj'])) { 
            $args['meta_query'][]['key'] = '_project_details';
            $args['meta_query'][]['value'] = $_GET['dproj'];
            $args['meta_query'][]['compare'] = '=';
        } 
Varun Sridharan's avatar
Varun Sridharan committed
169
        if(isset($_GET['order_status'])){ $args['post_status'] = $_GET['order_status'];}    
170 171 172 173 174 175 176 177 178
        $wp_query = new WP_Query($args);
        require('wp-donation-listing-table.php');
        tt_render_list_page($wp_query);
    }
    
    /**
	 * Register the stylesheets for the admin area.
	 */
	public function enqueue_styles() { 
179 180
        wp_enqueue_style(WC_QD_SLUG.'_quick_hacks',WC_QD_CSS.'admin-hack-style.css' , array(), WC_QD()->version, 'all' );  
        
181
        if('wcqd_project_page_WC_QD_settings' == $this->current_screen()){
Varun Sridharan's avatar
Varun Sridharan committed
182
            wp_enqueue_style(WC_QD_SLUG.'_settings_style',WC_QD_CSS.'admin-settings-style.css' , array(), WC_QD()->version, 'all' );  
183
        }
184
        
Varun Sridharan's avatar
Varun Sridharan committed
185
        if('wcqd_project_page_wc_qd_tools' == $this->current_screen()){
Varun Sridharan's avatar
Varun Sridharan committed
186 187
            wp_enqueue_style(WC_QD_SLUG.'_sysinfo_style',WC_QD_CSS.'sysinfo.css' , array(), WC_QD()->version, 'all' );  
        }
188
        
189 190 191 192 193 194 195 196 197 198 199 200 201
        if(in_array($this->current_screen() , $this->get_screen_ids())) {
            wp_enqueue_style(WC_QD_SLUG.'_core_style',WC_QD_CSS.'admin-style.css' , array(), WC_QD()->version, 'all' );  
        }
	}
	
    
    /**
	 * Register the JavaScript for the admin area.
	 */
	public function enqueue_scripts() {
        if(in_array($this->current_screen() , $this->get_screen_ids())) {
            wp_enqueue_script(WC_QD_SLUG.'_core_script', WC_QD_JS.'admin-script.js', array('jquery'), WC_QD()->version, false ); 
        }
Varun Sridharan's avatar
Varun Sridharan committed
202
        if('wcqd_project_page_wc_qd_tools' == $this->current_screen()){
Varun Sridharan's avatar
Varun Sridharan committed
203 204 205 206 207 208
            wp_register_script(WC_QD_SLUG.'_sysinfo_script', WC_QD_JS.'sysinfo.js', array( 'jquery' ), WC_QD()->version,false );
            wp_localize_script(WC_QD_SLUG.'_sysinfo_script', 'systemInfoAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
            wp_enqueue_script(WC_QD_SLUG.'_sysinfo_script');
            
        }
            
209 210 211 212 213
	}
    
    public function set_wc_screen_ids($screens){
        $screen = $screens; 
        $screen[] = 'wcqd_project_page_WC_QD_settings';
Varun Sridharan's avatar
Varun Sridharan committed
214 215 216
        $screen[] = $this->order_menu_slug;
        $screen[] = $this->donors_list;
        $screen[] = $this->tools; 
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        return $screen;
    }    
    
    /**
     * Gets Current Screen ID from wordpress
     * @return string [Current Screen ID]
     */
    public function current_screen(){
       $screen =  get_current_screen();
       return $screen->id;
    }
    
    /**
     * Returns Predefined Screen IDS
     * @return [Array] 
     */
    public function get_screen_ids(){
        $screen_ids = array();
        $screen_ids[] = 'edit-product';
        $screen_ids[] = 'product';
        $screen_ids[] = WC_QD_PT.'_page_wc_qd_settings';
        $screen_ids[] = 'wcqd_project_page_WC_QD_settings';
        $screen_ids[] = $this->order_menu_slug;
Varun Sridharan's avatar
Varun Sridharan committed
240 241 242
        $screen_ids[] = $this->order_menu_slug;
        $screen_ids[] = $this->donors_list;
        $screen_ids[] = $this->tools;        
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
        return $screen_ids;
    }
    
    
    /**
	 * Adds Some Plugin Options
	 * @param  array  $plugin_meta
	 * @param  string $plugin_file
	 * @since 0.11
	 * @return array
	 */
	public function plugin_row_links( $plugin_meta, $plugin_file ) {
		if ( WC_QD_FILE == $plugin_file ) {
            $plugin_meta[] = sprintf('<a href="%s">%s</a>', admin_url('edit.php?post_type=wcqd_project&page=WC_QD_settings'), __('Settings',WC_QD_TXT) );
            $plugin_meta[] = sprintf('<a href="%s">%s</a>', 'https://wordpress.org/plugins/woocommerce-quick-donation/faq/', __('F.A.Q',WC_QD_TXT) );
            $plugin_meta[] = sprintf('<a href="%s">%s</a>', 'https://github.com/technofreaky/woocomerce-quick-donation/', __('View On Github',WC_QD_TXT) );
            $plugin_meta[] = sprintf('<a href="%s">%s</a>', 'https://github.com/technofreaky/woocomerce-quick-donation/issues/', __('Report Issue',WC_QD_TXT) );
            $plugin_meta[] = sprintf('&hearts; <a href="%s">%s</a>', 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9MLKDVUVB7WBJ', __('Donate',WC_QD_TXT) );
            $plugin_meta[] = sprintf('<a href="%s">%s</a>', 'http://varunsridharan.in/plugin-support/', __('Contact Author',WC_QD_TXT) );
		}
		return $plugin_meta;
	}	    
}

?>