Completed
Push — issues/611 ( 661115...758b1c )
by Ravinder
21:11
created

includes/admin/customers/class-customer-table.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Customer (Donor) 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_Customer_Reports_Table Class.
24
 *
25
 * Renders the Customer Reports table.
26
 *
27
 * @since 1.0
28
 */
29
class Give_Customer_Reports_Table extends WP_List_Table {
30
31
	/**
32
	 * Number of items per page.
33
	 *
34
	 * @var int
35
	 * @since 1.0
36
	 */
37
	public $per_page = 30;
38
39
	/**
40
	 * Number of donors found.
41
	 *
42
	 * @var int
43
	 * @since 1.0
44
	 */
45
	public $count = 0;
46
47
	/**
48
	 * Total donors.
49
	 *
50
	 * @var int
51
	 * @since 1.0
52
	 */
53
	public $total = 0;
54
55
	/**
56
	 * Get things started.
57
	 *
58
	 * @since 1.0
59
	 * @see   WP_List_Table::__construct()
60
	 */
61
	public function __construct() {
62
63
		// Set parent defaults
64
		parent::__construct( array(
65
			'singular' => esc_html__( 'Donor', 'give' ),     // Singular name of the listed records
66
			'plural'   => esc_html__( 'Donors', 'give' ),    // Plural name of the listed records
67
			'ajax'     => false                       // Does this table support ajax?
68
		) );
69
70
	}
71
72
	/**
73
	 * Show the search field.
74
	 *
75
	 * @since  1.0
76
	 * @access public
77
	 *
78
	 * @param string $text     Label for the search box.
79
	 * @param string $input_id ID of the search box.
80
	 *
81
	 * @return void
82
	 */
83
	public function search_box( $text, $input_id ) {
84
		$input_id = $input_id . '-search-input';
85
86
		if ( ! empty( $_REQUEST['orderby'] ) ) {
87
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
88
		}
89
		if ( ! empty( $_REQUEST['order'] ) ) {
90
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
91
		}
92
		?>
93
        <p class="search-box" role="search">
94
            <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
95
            <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/>
96
			<?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?>
97
        </p>
98
		<?php
99
	}
100
101
	/**
102
	 * This function renders most of the columns in the list table.
103
	 *
104
	 * @access public
105
	 * @since  1.0
106
	 *
107
	 * @param array  $item        Contains all the data of the customers.
108
	 * @param string $column_name The name of the column.
109
	 *
110
	 * @return string Column Name.
111
	 */
112
	public function column_default( $item, $column_name ) {
113
		switch ( $column_name ) {
114
115
			case 'num_donations' :
116
				$value = '<a href="' .
117
				         admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&user=' . urlencode( $item['email'] )
118
				         ) . '">' . esc_html( $item['num_donations'] ) . '</a>';
119
				break;
120
121
			case 'amount_spent' :
122
				$value = give_currency_filter( give_format_amount( $item[ $column_name ] ) );
123
				break;
124
125
			case 'date_created' :
126
				$value = date_i18n( give_date_format(), strtotime( $item['date_created'] ) );
127
				break;
128
129
			default:
130
				$value = isset( $item[ $column_name ] ) ? $item[ $column_name ] : null;
131
				break;
132
		}
133
134
		return apply_filters( "give_report_column_{$column_name}", $value, $item['id'] );
135
136
	}
137
138
	/**
139
	 * Column name.
140
	 *
141
	 * @param $item
142
	 *
143
	 * @return string
144
	 */
145
	public function column_name( $item ) {
146
		$name = '#' . $item['id'] . ' ';
147
		$name .= ! empty( $item['name'] ) ? $item['name'] : '<em>' . esc_html__( 'Unnamed Donor', 'give' ) . '</em>';
148
		$view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $item['id'] );
149
		$actions  = $this->get_row_actions( $item );
150
151
		return '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>' . $this->row_actions( $actions );
152
	}
153
154
	/**
155
	 * Retrieve the table columns.
156
	 *
157
	 * @access public
158
	 * @since  1.0
159
	 * @return array $columns Array of all the list table columns.
160
	 */
161
	public function get_columns() {
162
		$columns = array(
163
			'name'          => esc_html__( 'Name', 'give' ),
164
			'email'         => esc_html__( 'Email', 'give' ),
165
			'num_donations' => esc_html__( 'Donations', 'give' ),
166
			'amount_spent'  => esc_html__( 'Total Donated', 'give' ),
167
			'date_created'  => esc_html__( 'Date Created', 'give' )
168
		);
169
170
		return apply_filters( 'give_report_customer_columns', $columns );
171
172
	}
173
174
	/**
175
	 * Get the sortable columns.
176
	 *
177
	 * @access public
178
	 * @since  2.1
179
	 * @return array Array of all the sortable columns.
180
	 */
181
	public function get_sortable_columns() {
182
183
		$columns = array(
184
			'date_created'  => array( 'date_created', true ),
185
			'name'          => array( 'name', true ),
186
			'num_donations' => array( 'purchase_count', false ),
187
			'amount_spent'  => array( 'purchase_value', false ),
188
		);
189
190
		return apply_filters( 'give_report_sortable_customer_columns', $columns );
191
	}
192
193
	/**
194
	 * Retrieve row actions.
195
	 *
196
	 * @since  1.7
197
	 * @access public
198
	 *
199
	 * @param $item
200
	 *
201
	 * @return array An array of action links.
202
	 */
203
	public function get_row_actions( $item ) {
204
205
		$actions = array(
206
207
			'view' => sprintf(
208
				'<a href="%1$s" aria-label="%2$s">%3$s</a>',
209
				admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $item['id'] ),
210
				sprintf( esc_attr__( 'View "%s"', 'give' ), $item['name'] ),
211
				esc_html__( 'View Donor', 'give' )
212
			),
213
214
			'notes' => sprintf(
215
				'<a href="%1$s" aria-label="%2$s">%3$s</a>',
216
				admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=notes&id=' . $item['id'] ),
217
				sprintf( esc_attr__( 'Notes for "%s"', 'give' ), $item['name'] ),
218
				esc_html__( 'Notes', 'give' )
219
			),
220
221
			'delete' => sprintf(
222
				'<a href="%1$s" aria-label="%2$s">%3$s</a>',
223
				admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $item['id'] ),
224
				sprintf( esc_attr__( 'Delete "%s"', 'give' ), $item['name'] ),
225
				esc_html__( 'Delete', 'give' )
226
			)
227
228
		);
229
230
		return apply_filters( 'give_donor_row_actions', $actions, $item );
231
232
	}
233
234
	/**
235
	 * Outputs bulk reviews
236
	 *
237
	 * @access public
238
	 *
239
	 * @param $which
240
	 *
241
	 * @since  1.0
242
	 * @return void
243
	 */
244
	public function bulk_actions( $which = '' ) {
245
		// These aren't really bulk actions but this outputs the markup in the right place.
246
	}
247
248
	/**
249
	 * Retrieve the current page number.
250
	 *
251
	 * @access public
252
	 * @since  1.0
253
	 * @return int Current page number.
254
	 */
255
	public function get_paged() {
256
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
257
	}
258
259
	/**
260
	 * Retrieves the search query string.
261
	 *
262
	 * @access public
263
	 * @since  1.0
264
	 * @return mixed string If search is present, false otherwise.
265
	 */
266
	public function get_search() {
267
		return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
268
	}
269
270
	/**
271
	 * Retrieves the donor data from db.
272
	 *
273
	 * @access public
274
	 * @since  1.0
275
	 *
276
	 * @return array $data The Donor data.
277
	 */
278
	public function donor_data() {
279
280
		$data = array();
281
282
		// Get donor query.
283
		$args      = $this->get_donor_query();
284
		$customers = Give()->customers->get_customers( $args );
285
286
		if ( $customers ) {
287
288
			foreach ( $customers as $customer ) {
289
290
				$user_id = ! empty( $customer->user_id ) ? intval( $customer->user_id ) : 0;
291
292
				$data[] = array(
293
					'id'            => $customer->id,
294
					'user_id'       => $user_id,
295
					'name'          => $customer->name,
296
					'email'         => $customer->email,
297
					'num_donations' => $customer->purchase_count,
298
					'amount_spent'  => $customer->purchase_value,
299
					'date_created'  => $customer->date_created,
300
				);
301
			}
302
		}
303
304
		return apply_filters( 'give_donors_column_query_data', $data );
305
	}
306
307
	/**
308
	 * Get donor count.
309
	 *
310
	 * @since  1.8.1
311
	 * @access private
312
	 */
313
	private function get_donor_count() {
314
		// Get donor query.
315
		$_donor_query = $this->get_donor_query();
316
317
		$_donor_query['number'] = - 1;
318
		$donors                 = Give()->customers->get_customers( $_donor_query );
319
320
		return count( $donors );
321
	}
322
323
	/**
324
	 * Get donor query.
325
	 *
326
	 * @since  1.8.1
327
	 * @access public
328
	 * @return array
329
	 */
330
	public function get_donor_query() {
331
		$paged   = $this->get_paged();
332
		$offset  = $this->per_page * ( $paged - 1 );
333
		$search  = $this->get_search();
334
		$order   = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC';
335
		$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id';
336
337
		$args = array(
338
			'number'  => $this->per_page,
339
			'offset'  => $offset,
340
			'order'   => $order,
341
			'orderby' => $orderby,
342
		);
343
344
		if ( $search ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $search of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
345
			if ( is_email( $search ) ) {
346
				$args['email'] = $search;
347
			} elseif ( is_numeric( $search ) ) {
348
				$args['id'] = $search;
349
			} else {
350
				$args['name'] = $search;
351
			}
352
		}
353
354
		return $args;
355
	}
356
357
	/**
358
	 * Setup the final data for the table.
359
	 *
360
	 * @access public
361
	 * @since  1.0
362
	 * @uses   Give_Customer_Reports_Table::get_columns()
363
	 * @uses   WP_List_Table::get_sortable_columns()
364
	 * @uses   Give_Customer_Reports_Table::get_pagenum()
365
	 * @uses   Give_Customer_Reports_Table::get_total_customers()
366
	 * @return void
367
	 */
368
	public function prepare_items() {
369
370
		$columns  = $this->get_columns();
371
		$hidden   = array(); // No hidden columns
372
		$sortable = $this->get_sortable_columns();
373
374
		$this->_column_headers = array( $columns, $hidden, $sortable );
375
376
		$this->items = $this->donor_data();
377
378
		$this->total = $this->get_donor_count();
379
380
		$this->set_pagination_args( array(
381
			'total_items' => $this->total,
382
			'per_page'    => $this->per_page,
383
			'total_pages' => ceil( $this->total / $this->per_page )
384
		) );
385
	}
386
}