|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Download Reports Table Class |
|
4
|
|
|
* |
|
5
|
|
|
* @package Give |
|
6
|
|
|
* @subpackage Admin/Reports |
|
7
|
|
|
* @copyright Copyright (c) 2016, WordImpress |
|
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_Form_Reports_Table Class |
|
24
|
|
|
* |
|
25
|
|
|
* Renders the Form Reports table |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
class Give_Form_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
|
|
|
* @var object Query results |
|
39
|
|
|
* @since 1.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
private $products; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get things started |
|
45
|
|
|
* |
|
46
|
|
|
* @since 1.0 |
|
47
|
|
|
* @see WP_List_Table::__construct() |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct() { |
|
50
|
|
|
global $status, $page; |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
// Set parent defaults |
|
53
|
|
|
parent::__construct( array( |
|
54
|
|
|
'singular' => give_get_forms_label_singular(), // Singular name of the listed records |
|
55
|
|
|
'plural' => give_get_forms_label_plural(), // Plural name of the listed records |
|
56
|
|
|
'ajax' => false // Does this table support ajax? |
|
57
|
|
|
) ); |
|
58
|
|
|
|
|
59
|
|
|
add_action( 'give_report_view_actions', array( $this, 'category_filter' ) ); |
|
60
|
|
|
$this->query(); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* This function renders most of the columns in the list table. |
|
66
|
|
|
* |
|
67
|
|
|
* @access public |
|
68
|
|
|
* @since 1.0 |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $item Contains all the data of the donation form |
|
71
|
|
|
* @param string $column_name The name of the column |
|
72
|
|
|
* |
|
73
|
|
|
* @return string Column Name |
|
74
|
|
|
*/ |
|
75
|
|
|
public function column_default( $item, $column_name ) { |
|
76
|
|
|
switch ( $column_name ) { |
|
77
|
|
|
case 'earnings' : |
|
78
|
|
|
return give_currency_filter( give_format_amount( $item[ $column_name ] ) ); |
|
79
|
|
|
case 'average_sales' : |
|
80
|
|
|
return round( $item[ $column_name ] ); |
|
81
|
|
|
case 'average_earnings' : |
|
82
|
|
|
return give_currency_filter( give_format_amount( $item[ $column_name ] ) ); |
|
83
|
|
|
case 'details' : |
|
84
|
|
|
return '<a href="' . admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=forms&form-id=' . $item['ID'] ) . '">' . esc_html__( 'View Detailed Report', 'give' ) . '</a>'; |
|
85
|
|
|
default: |
|
86
|
|
|
return $item[ $column_name ]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Retrieve the table columns |
|
92
|
|
|
* |
|
93
|
|
|
* @access public |
|
94
|
|
|
* @since 1.0 |
|
95
|
|
|
* @return array $columns Array of all the list table columns |
|
96
|
|
|
*/ |
|
97
|
|
|
public function get_columns() { |
|
98
|
|
|
$columns = array( |
|
99
|
|
|
'title' => esc_html__( 'Form', 'give' ), |
|
100
|
|
|
'sales' => esc_html__( 'Donations', 'give' ), |
|
101
|
|
|
'earnings' => esc_html__( 'Income', 'give' ), |
|
102
|
|
|
'average_sales' => esc_html__( 'Monthly Average Donations', 'give' ), |
|
103
|
|
|
'average_earnings' => esc_html__( 'Monthly Average Income', 'give' ), |
|
104
|
|
|
'details' => esc_html__( 'Detailed Report', 'give' ) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
return $columns; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Retrieve the table's sortable columns |
|
112
|
|
|
* |
|
113
|
|
|
* @access public |
|
114
|
|
|
* @since 1.0 |
|
115
|
|
|
* @return array Array of all the sortable columns |
|
116
|
|
|
*/ |
|
117
|
|
|
public function get_sortable_columns() { |
|
118
|
|
|
return array( |
|
119
|
|
|
'title' => array( 'title', true ), |
|
120
|
|
|
'sales' => array( 'sales', false ), |
|
121
|
|
|
'earnings' => array( 'earnings', false ), |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Retrieve the current page number |
|
127
|
|
|
* |
|
128
|
|
|
* @access public |
|
129
|
|
|
* @since 1.0 |
|
130
|
|
|
* @return int Current page number |
|
131
|
|
|
*/ |
|
132
|
|
|
public function get_paged() { |
|
133
|
|
|
return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Retrieve the category being viewed |
|
139
|
|
|
* |
|
140
|
|
|
* @access public |
|
141
|
|
|
* @since 1.0 |
|
142
|
|
|
* @return int Category ID |
|
143
|
|
|
*/ |
|
144
|
|
|
public function get_category() { |
|
145
|
|
|
return isset( $_GET['category'] ) ? absint( $_GET['category'] ) : 0; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Retrieve the total number of forms |
|
151
|
|
|
* |
|
152
|
|
|
* @access public |
|
153
|
|
|
* @since 1.0 |
|
154
|
|
|
* @return int $total Total number of donation forms |
|
155
|
|
|
*/ |
|
156
|
|
|
public function get_total_forms() { |
|
157
|
|
|
$total = 0; |
|
158
|
|
|
$counts = wp_count_posts( 'give_forms', 'readable' ); |
|
159
|
|
|
foreach ( $counts as $status => $count ) { |
|
160
|
|
|
$total += $count; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $total; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Outputs the reporting views |
|
168
|
|
|
* |
|
169
|
|
|
* @access public |
|
170
|
|
|
* @since 1.0 |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
|
public function bulk_actions( $which = '' ) { |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Generate the table navigation above or below the table |
|
179
|
|
|
* |
|
180
|
|
|
* @since 1.0 |
|
181
|
|
|
* @access protected |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $which |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function display_tablenav( $which ) { |
|
186
|
|
|
|
|
187
|
|
|
if ( 'top' === $which ) { |
|
188
|
|
|
wp_nonce_field( 'bulk-' . $this->_args['plural'] ); |
|
189
|
|
|
} |
|
190
|
|
|
?> |
|
191
|
|
|
<div class="tablenav give-clearfix <?php echo esc_attr( $which ); ?>"> |
|
192
|
|
|
|
|
193
|
|
|
<?php if ( 'top' === $which ) { ?> |
|
194
|
|
|
<h3 class="alignleft reports-earnings-title"> |
|
195
|
|
|
<span><?php esc_html_e( 'Donation Forms Report', 'give' ); ?></span> |
|
196
|
|
|
</h3> |
|
197
|
|
|
<?php } ?> |
|
198
|
|
|
|
|
199
|
|
|
<div class="alignright tablenav-right"> |
|
200
|
|
|
<div class="actions bulkactions"> |
|
201
|
|
|
<?php $this->bulk_actions( $which ); ?> |
|
202
|
|
|
</div> |
|
203
|
|
|
<?php |
|
204
|
|
|
$this->extra_tablenav( $which ); |
|
205
|
|
|
$this->pagination( $which ); |
|
206
|
|
|
?> |
|
207
|
|
|
</div> |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
<br class="clear" /> |
|
211
|
|
|
|
|
212
|
|
|
</div> |
|
213
|
|
|
<?php |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Attaches the category filter to the log views |
|
218
|
|
|
* |
|
219
|
|
|
* @access public |
|
220
|
|
|
* @since 1.0 |
|
221
|
|
|
* @return void |
|
222
|
|
|
*/ |
|
223
|
|
|
public function category_filter() { |
|
224
|
|
|
|
|
225
|
|
|
$categories = get_terms( 'form_category' ); |
|
226
|
|
|
if ( $categories && ! is_wp_error( $categories ) ) { |
|
227
|
|
|
echo Give()->html->category_dropdown( 'category', $this->get_category() ); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Performs the products query |
|
234
|
|
|
* |
|
235
|
|
|
* @access public |
|
236
|
|
|
* @since 1.0 |
|
237
|
|
|
* @return void |
|
238
|
|
|
*/ |
|
239
|
|
|
public function query() { |
|
240
|
|
|
|
|
241
|
|
|
$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : 'title'; |
|
242
|
|
|
$order = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC'; |
|
243
|
|
|
$category = $this->get_category(); |
|
244
|
|
|
|
|
245
|
|
|
$args = array( |
|
246
|
|
|
'post_type' => 'give_forms', |
|
247
|
|
|
'post_status' => 'publish', |
|
248
|
|
|
'order' => $order, |
|
249
|
|
|
'fields' => 'ids', |
|
250
|
|
|
'posts_per_page' => $this->per_page, |
|
251
|
|
|
'paged' => $this->get_paged(), |
|
252
|
|
|
'suppress_filters' => true |
|
253
|
|
|
); |
|
254
|
|
|
|
|
255
|
|
|
if ( ! empty( $category ) ) { |
|
256
|
|
|
$args['tax_query'] = array( |
|
257
|
|
|
array( |
|
258
|
|
|
'taxonomy' => 'form_category', |
|
259
|
|
|
'terms' => $category |
|
260
|
|
|
) |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
switch ( $orderby ) : |
|
265
|
|
|
case 'title' : |
|
266
|
|
|
$args['orderby'] = 'title'; |
|
267
|
|
|
break; |
|
268
|
|
|
|
|
269
|
|
|
case 'sales' : |
|
270
|
|
|
$args['orderby'] = 'meta_value_num'; |
|
271
|
|
|
$args['meta_key'] = '_give_form_sales'; |
|
272
|
|
|
break; |
|
273
|
|
|
|
|
274
|
|
|
case 'earnings' : |
|
275
|
|
|
$args['orderby'] = 'meta_value_num'; |
|
276
|
|
|
$args['meta_key'] = '_give_form_earnings'; |
|
277
|
|
|
break; |
|
278
|
|
|
endswitch; |
|
279
|
|
|
|
|
280
|
|
|
$args = apply_filters( 'give_form_reports_prepare_items_args', $args, $this ); |
|
281
|
|
|
|
|
282
|
|
|
$this->products = new WP_Query( $args ); |
|
283
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Build all the reports data |
|
288
|
|
|
* |
|
289
|
|
|
* @access public |
|
290
|
|
|
* @since 1.0 |
|
291
|
|
|
* @return array $reports_data All the data for donor reports |
|
292
|
|
|
*/ |
|
293
|
|
|
public function reports_data() { |
|
294
|
|
|
$reports_data = array(); |
|
295
|
|
|
|
|
296
|
|
|
$give_forms = $this->products->posts; |
|
297
|
|
|
|
|
298
|
|
|
if ( $give_forms ) { |
|
299
|
|
|
foreach ( $give_forms as $form ) { |
|
300
|
|
|
$reports_data[] = array( |
|
301
|
|
|
'ID' => $form, |
|
302
|
|
|
'title' => get_the_title( $form ), |
|
303
|
|
|
'sales' => give_get_form_sales_stats( $form ), |
|
304
|
|
|
'earnings' => give_get_form_earnings_stats( $form ), |
|
305
|
|
|
'average_sales' => give_get_average_monthly_form_sales( $form ), |
|
306
|
|
|
'average_earnings' => give_get_average_monthly_form_earnings( $form ) |
|
307
|
|
|
); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
return $reports_data; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Setup the final data for the table |
|
317
|
|
|
* |
|
318
|
|
|
* @access public |
|
319
|
|
|
* @since 1.5 |
|
320
|
|
|
* @uses Give_Form_Reports_Table::get_columns() |
|
321
|
|
|
* @uses Give_Form_Reports_Table::get_sortable_columns() |
|
322
|
|
|
* @uses Give_Form_Reports_Table::reports_data() |
|
323
|
|
|
* @uses Give_Form_Reports_Table::get_pagenum() |
|
324
|
|
|
* @uses Give_Form_Reports_Table::get_total_forms() |
|
325
|
|
|
* @return void |
|
326
|
|
|
*/ |
|
327
|
|
|
public function prepare_items() { |
|
328
|
|
|
$columns = $this->get_columns(); |
|
329
|
|
|
|
|
330
|
|
|
$hidden = array(); // No hidden columns |
|
331
|
|
|
|
|
332
|
|
|
$sortable = $this->get_sortable_columns(); |
|
333
|
|
|
|
|
334
|
|
|
$this->_column_headers = array( $columns, $hidden, $sortable ); |
|
335
|
|
|
|
|
336
|
|
|
$data = $this->reports_data(); |
|
337
|
|
|
|
|
338
|
|
|
$total_items = $this->get_total_forms(); |
|
339
|
|
|
|
|
340
|
|
|
$this->items = $data; |
|
341
|
|
|
|
|
342
|
|
|
$this->set_pagination_args( array( |
|
343
|
|
|
'total_items' => $total_items, |
|
344
|
|
|
'per_page' => $this->per_page, |
|
345
|
|
|
'total_pages' => ceil( $total_items / $this->per_page ) |
|
346
|
|
|
) |
|
347
|
|
|
); |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.