| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gateways Reports Table Class |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Admin/Reports |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
// Load WP_List_Table if not loaded |
| 18 |
if ( ! class_exists( 'WP_List_Table' ) ) { |
| 19 |
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Give_Gateway_Reports_Table Class |
| 24 |
* |
| 25 |
* Renders the Download Reports table |
| 26 |
* |
| 27 |
* @since 1.0 |
| 28 |
*/ |
| 29 |
class Give_Gateway_Reports_Table extends WP_List_Table { |
| 30 |
|
| 31 |
/** |
| 32 |
* @var int Number of items per page |
| 33 |
* @since 1.0 |
| 34 |
*/ |
| 35 |
public $per_page = 30; |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Get things started |
| 40 |
* |
| 41 |
* @since 1.0 |
| 42 |
* @see WP_List_Table::__construct() |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
global $status, $page; |
| 46 |
|
| 47 |
// Set parent defaults |
| 48 |
parent::__construct( |
| 49 |
array( |
| 50 |
'singular' => give_get_forms_label_singular(), // Singular name of the listed records |
| 51 |
'plural' => give_get_forms_label_plural(), // Plural name of the listed records |
| 52 |
'ajax' => false, // Does this table support ajax? |
| 53 |
) |
| 54 |
); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* This function renders most of the columns in the list table. |
| 60 |
* |
| 61 |
* @access public |
| 62 |
* @since 1.0 |
| 63 |
* |
| 64 |
* @param array $item Contains all the data of the form |
| 65 |
* @param string $column_name The name of the column |
| 66 |
* |
| 67 |
* @return string Column Name |
| 68 |
*/ |
| 69 |
public function column_default( $item, $column_name ) { |
| 70 |
$donation_list_page_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); |
| 71 |
|
| 72 |
switch ( $column_name ) { |
| 73 |
case 'complete_sales': |
| 74 |
$value = $item[ $column_name ] ? |
| 75 |
sprintf( |
| 76 |
'<a href="%s">%s</a>', |
| 77 |
esc_url( |
| 78 |
add_query_arg( |
| 79 |
array( |
| 80 |
'status' => 'publish', |
| 81 |
'gateway' => $item['ID'], |
| 82 |
), |
| 83 |
$donation_list_page_url |
| 84 |
) |
| 85 |
), |
| 86 |
$item[ $column_name ] |
| 87 |
) : |
| 88 |
$item[ $column_name ]; |
| 89 |
break; |
| 90 |
|
| 91 |
case 'pending_sales': |
| 92 |
$value = $item[ $column_name ] ? |
| 93 |
sprintf( |
| 94 |
'<a href="%s">%s</a>', |
| 95 |
esc_url( |
| 96 |
add_query_arg( |
| 97 |
array( |
| 98 |
'status' => 'pending', |
| 99 |
'gateway' => $item['ID'], |
| 100 |
), |
| 101 |
$donation_list_page_url |
| 102 |
) |
| 103 |
), |
| 104 |
$item[ $column_name ] |
| 105 |
) : |
| 106 |
$item[ $column_name ]; |
| 107 |
break; |
| 108 |
|
| 109 |
case 'total_sales': |
| 110 |
$value = $item[ $column_name ] ? |
| 111 |
sprintf( |
| 112 |
'<a href="%s">%s</a>', |
| 113 |
esc_url( |
| 114 |
add_query_arg( |
| 115 |
array( |
| 116 |
'gateway' => $item['ID'], |
| 117 |
), |
| 118 |
$donation_list_page_url |
| 119 |
) |
| 120 |
), |
| 121 |
$item[ $column_name ] |
| 122 |
) : |
| 123 |
$item[ $column_name ]; |
| 124 |
|
| 125 |
break; |
| 126 |
|
| 127 |
default: |
| 128 |
$value = $item[ $column_name ]; |
| 129 |
} |
| 130 |
|
| 131 |
return $value; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Retrieve the table columns |
| 136 |
* |
| 137 |
* @access public |
| 138 |
* @since 1.0 |
| 139 |
* @return array $columns Array of all the list table columns |
| 140 |
*/ |
| 141 |
public function get_columns() { |
| 142 |
$columns = array( |
| 143 |
'label' => esc_attr__( 'Gateway', 'give' ), |
| 144 |
'complete_sales' => esc_attr__( 'Complete Payments', 'give' ), |
| 145 |
'pending_sales' => esc_attr__( 'Pending / Failed Payments', 'give' ), |
| 146 |
'total_sales' => esc_attr__( 'Total Payments', 'give' ), |
| 147 |
'total_donations' => esc_attr__( 'Total Donated', 'give' ), |
| 148 |
); |
| 149 |
|
| 150 |
return $columns; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get the sortable columns |
| 155 |
* |
| 156 |
* @access public |
| 157 |
* @since 1.8.12 |
| 158 |
* @return array Array of all the sortable columns |
| 159 |
*/ |
| 160 |
public function get_sortable_columns() { |
| 161 |
return array( |
| 162 |
'total_donations' => array( 'total_donations', false ), |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Retrieve the current page number |
| 169 |
* |
| 170 |
* @access public |
| 171 |
* @since 1.0 |
| 172 |
* @return int Current page number |
| 173 |
*/ |
| 174 |
public function get_paged() { |
| 175 |
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Outputs the reporting views |
| 181 |
* |
| 182 |
* @access public |
| 183 |
* @since 1.0 |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function bulk_actions( $which = '' ) { |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Generate the table navigation above or below the table |
| 192 |
* |
| 193 |
* @since 1.0 |
| 194 |
* @access protected |
| 195 |
* |
| 196 |
* @param string $which |
| 197 |
*/ |
| 198 |
protected function display_tablenav( $which ) { |
| 199 |
|
| 200 |
if ( 'top' === $which ) { |
| 201 |
wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
| 202 |
} |
| 203 |
?> |
| 204 |
<div class="tablenav gateways-report-tablenav give-clearfix <?php echo esc_attr( $which ); ?>"> |
| 205 |
|
| 206 |
<?php if ( 'top' === $which ) { ?> |
| 207 |
<h2 class="alignleft reports-earnings-title screen-reader-text"> |
| 208 |
<?php _e( 'Donation Methods Report', 'give' ); ?> |
| 209 |
</h2> |
| 210 |
<?php } ?> |
| 211 |
|
| 212 |
<div class="alignright tablenav-right"> |
| 213 |
<div class="actions bulkactions"> |
| 214 |
<?php $this->bulk_actions( $which ); ?> |
| 215 |
</div> |
| 216 |
<?php |
| 217 |
$this->extra_tablenav( $which ); |
| 218 |
$this->pagination( $which ); |
| 219 |
?> |
| 220 |
</div> |
| 221 |
|
| 222 |
|
| 223 |
<br class="clear" /> |
| 224 |
|
| 225 |
</div> |
| 226 |
<?php |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Reorder User Defined Array |
| 231 |
* |
| 232 |
* @param $old_value |
| 233 |
* @param $new_value |
| 234 |
* |
| 235 |
* @access public |
| 236 |
* @since 1.8.12 |
| 237 |
* |
| 238 |
* @return int |
| 239 |
*/ |
| 240 |
public function give_sort_total_donations( $old_value, $new_value ) { |
| 241 |
// If no sort, default to label. |
| 242 |
$orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'label'; |
| 243 |
|
| 244 |
// If no order, default to asc. |
| 245 |
$order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc'; |
| 246 |
|
| 247 |
// Determine sort order. |
| 248 |
$result = strcmp( $old_value[ $orderby ], $new_value[ $orderby ] ); |
| 249 |
|
| 250 |
return ( $order === 'asc' ) ? $result : -$result; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Build all the reports data |
| 256 |
* |
| 257 |
* @access public |
| 258 |
* @since 1.0 |
| 259 |
* @return array $reports_data All the data for donor reports |
| 260 |
*/ |
| 261 |
public function reports_data() { |
| 262 |
|
| 263 |
$reports_data = array(); |
| 264 |
$gateways = give_get_payment_gateways(); |
| 265 |
$stats = new Give_Payment_Stats(); |
| 266 |
|
| 267 |
foreach ( $gateways as $gateway_id => $gateway ) { |
| 268 |
|
| 269 |
$complete_count = give_count_sales_by_gateway( $gateway_id, 'publish' ); |
| 270 |
$pending_count = give_count_sales_by_gateway( $gateway_id, array( 'pending', 'failed' ) ); |
| 271 |
|
| 272 |
$reports_data[] = array( |
| 273 |
'ID' => $gateway_id, |
| 274 |
'label' => $gateway['admin_label'], |
| 275 |
'complete_sales' => $complete_count, |
| 276 |
'pending_sales' => $pending_count, |
| 277 |
'total_sales' => $complete_count + $pending_count, |
| 278 |
'total_donations' => give_currency_filter( give_format_amount( $stats->get_earnings( 0, strtotime( '04/13/2015' ), current_time( 'timestamp' ), $gateway_id ), array( 'sanitize' => false ) ) ), |
| 279 |
); |
| 280 |
} |
| 281 |
|
| 282 |
return $reports_data; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Setup the final data for the table |
| 287 |
* |
| 288 |
* @access public |
| 289 |
* @since 1.0 |
| 290 |
* @uses Give_Gateway_Reports_Table::get_columns() |
| 291 |
* @uses Give_Gateway_Reports_Table::get_sortable_columns() |
| 292 |
* @uses Give_Gateway_Reports_Table::reports_data() |
| 293 |
* @return void |
| 294 |
*/ |
| 295 |
public function prepare_items() { |
| 296 |
$columns = $this->get_columns(); |
| 297 |
$hidden = array(); // No hidden columns |
| 298 |
$sortable = $this->get_sortable_columns(); |
| 299 |
$this->_column_headers = array( $columns, $hidden, $sortable ); |
| 300 |
$this->items = $this->reports_data(); |
| 301 |
|
| 302 |
// Sort Array when we are sorting data in array. |
| 303 |
usort( $this->items, array( $this, 'give_sort_total_donations' ) ); |
| 304 |
|
| 305 |
} |
| 306 |
} |
| 307 |
|