| 1 |
<?php |
| 2 |
namespace Wdk\DashWidgets\Widgets; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class WDK_Dashboard_Widget_Stats_Earning extends WDK_DashWidgets { |
| 9 |
|
| 10 |
/** |
| 11 |
* The id of this widget. |
| 12 |
*/ |
| 13 |
static $widget_id = 'wdk_dashboard_widget_stats_earning'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Hook to wp_dashboard_setup to add the widget. |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
|
| 20 |
if(!wmvc_user_in_role('administrator') && !is_super_admin()) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
global $Winter_MVC_wdk_membership, $Winter_MVC_wdk_payments; |
| 25 |
/* if missing addon membership and payments, hide widget */ |
| 26 |
if(!isset($Winter_MVC_wdk_membership) && !isset($Winter_MVC_wdk_payments)) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
//Register widget settings... |
| 31 |
self::update_dashboard_widget_options( |
| 32 |
self::$widget_id, //The widget id |
| 33 |
array( //Associative array of options & default values |
| 34 |
'example_number' => 42, |
| 35 |
), |
| 36 |
true //Add only (will not update existing options) |
| 37 |
); |
| 38 |
|
| 39 |
//Register the widget... |
| 40 |
wp_add_dashboard_widget( |
| 41 |
self::$widget_id, //A unique slug/ID |
| 42 |
__( 'Wdk Earning Stats', 'wpdirectorykit' ),//Visible name for the widget |
| 43 |
array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Stats_Earning','widget'), //Callback for the main widget content |
| 44 |
array('Wdk\DashWidgets\Widgets\WDK_Dashboard_Widget_Stats_Earning','config') //Optional callback for widget configuration content |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Load the widget code |
| 50 |
*/ |
| 51 |
public static function widget() { |
| 52 |
global $Winter_MVC_wdk_membership, $Winter_MVC_wdk_payments; |
| 53 |
|
| 54 |
$WMVC = &wdk_get_instance(); |
| 55 |
$WMVC->model('listing_m'); |
| 56 |
|
| 57 |
$data = array(); |
| 58 |
|
| 59 |
$data['settings'] = array( |
| 60 |
'conf_limit' => 50, |
| 61 |
); |
| 62 |
|
| 63 |
wp_enqueue_style('dash-widgets-statistics-earning'); |
| 64 |
|
| 65 |
/* default from settings */ |
| 66 |
$data['id_element'] = self::$widget_id; |
| 67 |
|
| 68 |
/* widgets data */ |
| 69 |
$stat_default = array('color' => '', 'class' => '', 'title' => '', 'value' => '', 'icon' => '', 'link' =>''); |
| 70 |
|
| 71 |
/* membership */ |
| 72 |
$count_membership = null; |
| 73 |
|
| 74 |
/* if missing addon membership and payments, hide widget */ |
| 75 |
if(!isset($Winter_MVC_wdk_membership) && !isset($Winter_MVC_wdk_payments)) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
if(isset($Winter_MVC_wdk_membership)) { |
| 80 |
$Winter_MVC_wdk_membership->model('subscription_m'); |
| 81 |
$Winter_MVC_wdk_membership->model('subscription_user_m'); |
| 82 |
|
| 83 |
$WMVC->db->select('COUNT(*) AS total, |
| 84 |
sum(days_limit) AS total_days, |
| 85 |
sum(price) AS total_cost'); |
| 86 |
$WMVC->db->from($Winter_MVC_wdk_membership->subscription_user_m->_table_name); |
| 87 |
$WMVC->db->join($Winter_MVC_wdk_membership->subscription_m->_table_name.' ON '.$Winter_MVC_wdk_membership->subscription_m->_table_name.'.idsubscription = '.$Winter_MVC_wdk_membership->subscription_user_m->_table_name.'.subscription_id', TRUE, 'left'); |
| 88 |
|
| 89 |
$WMVC->db->where(array('(date_expire > "'.current_time('mysql').'")'=>NULL)); |
| 90 |
|
| 91 |
$query = $WMVC->db->get(); |
| 92 |
$count_membership = $WMVC->db->row(); |
| 93 |
} |
| 94 |
/* packages */ |
| 95 |
$count_packages = null; |
| 96 |
|
| 97 |
if(isset($Winter_MVC_wdk_payments)) { |
| 98 |
$Winter_MVC_wdk_payments->model('package_m'); |
| 99 |
|
| 100 |
$WMVC->db->select('COUNT(*) AS total, |
| 101 |
sum(days_limit) AS total_days, |
| 102 |
sum(price) AS total_cost'); |
| 103 |
$WMVC->db->from($WMVC->listing_m->_table_name); |
| 104 |
$WMVC->db->join($Winter_MVC_wdk_payments->package_m->_table_name.' ON '.$WMVC->listing_m->_table_name.'.package_id = '.$Winter_MVC_wdk_payments->package_m->_table_name.'.idpackage', TRUE, 'left'); |
| 105 |
|
| 106 |
$WMVC->db->where(array('(date_package_expire > "'.current_time('mysql').'")'=>NULL, '(package_id IS NOT NULL)'=>NULL)); |
| 107 |
|
| 108 |
$query = $WMVC->db->get(); |
| 109 |
$count_packages = $WMVC->db->row(); |
| 110 |
} |
| 111 |
|
| 112 |
/* Total number of active Membership subscriptions */ |
| 113 |
if($count_membership){ |
| 114 |
$data['stats']['total_membership_subscriptions'] = array('title' => __( 'Total Membership Subscriptions', 'wpdirectorykit' ), 'class'=>'nohover'); |
| 115 |
$data['stats']['total_membership_subscriptions']['value'] = $count_membership->total; |
| 116 |
$data['stats']['total_membership_subscriptions'] = array_merge($stat_default, $data['stats']['total_membership_subscriptions']); |
| 117 |
} |
| 118 |
|
| 119 |
/* Total number of active Listings subscriptions */ |
| 120 |
if($count_packages){ |
| 121 |
$data['stats']['total_listings_packages'] = array('title' => __( 'Total Listings Subscriptions', 'wpdirectorykit'), 'class'=>'blue nohover'); |
| 122 |
$data['stats']['total_listings_packages']['value'] = $count_packages->total; |
| 123 |
$data['stats']['total_listings_packages'] = array_merge($stat_default, $data['stats']['total_listings_packages']); |
| 124 |
} |
| 125 |
|
| 126 |
/* Approx Earnings per month from Membership subscriptions */ |
| 127 |
if($count_membership){ |
| 128 |
$data['stats']['total_membership_subscriptions_earns'] = array('title' => __( 'Membership Subscriptions', 'wpdirectorykit' ), 'class'=>'red nohover'); |
| 129 |
|
| 130 |
$data['stats']['total_membership_subscriptions_earns']['value'] = '0'.wdk_currency_symbol().'/'.__( 'm', 'wpdirectorykit'); |
| 131 |
if(!empty($count_membership->total_cost) && !empty($count_membership->total_days)) |
| 132 |
$data['stats']['total_membership_subscriptions_earns']['value'] = round(intval($count_membership->total_cost)/intval($count_membership->total_days)*30).wdk_currency_symbol().'/'.__( 'm', 'wpdirectorykit'); |
| 133 |
|
| 134 |
$data['stats']['total_membership_subscriptions_earns'] = array_merge($stat_default, $data['stats']['total_membership_subscriptions_earns']); |
| 135 |
} |
| 136 |
|
| 137 |
/* Approx Earnings per month from Listings subscriptions */ |
| 138 |
if($count_packages){ |
| 139 |
$data['stats']['total_listings_packages_earns'] = array('title' => __( 'Listings Subscriptions', 'wpdirectorykit' ), 'class'=>'orange nohover'); |
| 140 |
|
| 141 |
$data['stats']['total_listings_packages_earns']['value'] = '0'.wdk_currency_symbol().'/'.__( 'm', 'wpdirectorykit'); |
| 142 |
if(!empty($count_packages->total_cost) && !empty($count_packages->total_days)) |
| 143 |
$data['stats']['total_listings_packages_earns']['value'] = round(intval($count_packages->total_cost)/intval($count_packages->total_days)*30).wdk_currency_symbol().'/'.__( 'm', 'wpdirectorykit'); |
| 144 |
$data['stats']['total_listings_packages_earns'] = array_merge($stat_default, $data['stats']['total_listings_packages_earns']); |
| 145 |
} |
| 146 |
|
| 147 |
self::view('dash-widgets-statistics-earning', $data, true); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Load widget config code. |
| 152 |
* |
| 153 |
* This is what will display when an admin clicks |
| 154 |
*/ |
| 155 |
public static function config() { |
| 156 |
wp_redirect(admin_url("admin.php?page=listing_settings")); exit; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
} |