Test Failed
Push — release/1.8.12 ( b58a2f...d255b1 )
by Ravinder
375:09 queued 372:17
created

includes/admin/donors/class-donor-table.php (23 issues)

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
 * Donor List Table Class.
4
 *
5
 * The list view under WP-Admin > Donations > Donors.
6
 *
7
 * @package     Give
8
 * @subpackage  Admin/Reports
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       1.0
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
// Load WP_List_Table if not loaded
20
if ( ! class_exists( 'WP_List_Table' ) ) {
21
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
22
}
23
24
/**
25
 * Give_Donor_List_Table Class.
26
 *
27
 * @since 1.0
28
 */
29
class Give_Donor_List_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 View Code Duplication
	public function __construct() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
63
		// Set parent defaults
64
		parent::__construct( array(
65
			'singular' => __( 'Donor', 'give' ),     // Singular name of the listed records.
66
			'plural'   => __( '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 View Code Duplication
	public function search_box( $text, $input_id ) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
		$input_id = $input_id . '-search-input';
85
86
		if ( ! empty( $_REQUEST['orderby'] ) ) {
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
87
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
88
		}
89
		if ( ! empty( $_REQUEST['order'] ) ) {
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
90
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
0 ignored issues
show
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
91
		}
92
		?>
93
		<p class="search-box" role="search">
94
			<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
0 ignored issues
show
Expected next thing to be a escaping function, not '$input_id'
Loading history...
Expected next thing to be a escaping function, not '$text'
Loading history...
95
			<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/>
0 ignored issues
show
Expected next thing to be a escaping function, not '$input_id'
Loading history...
96
			<?php submit_button( $text, 'button', false, false, array(
97
				'ID' => 'search-submit',
98
			) ); ?>
99
		</p>
100
		<?php
101
	}
102
103
	/**
104
	 * This function renders most of the columns in the list table.
105
	 *
106
	 * @access public
107
	 * @since  1.0
108
	 *
109
	 * @param array  $donor        Contains all the data of the donors.
110
	 * @param string $column_name The name of the column.
111
	 *
112
	 * @return string Column Name.
113
	 */
114
	public function column_default( $donor, $column_name ) {
115
		switch ( $column_name ) {
116
117 View Code Duplication
			case 'num_donations' :
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
				$value = '<a href="' . admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . urlencode( $donor['id'] ) ) . '&status=publish' . ' ">' . esc_html( $donor['num_donations'] ) . '</a>';
119
				break;
120
121 View Code Duplication
			case 'amount_spent' :
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
				$value = give_currency_filter( give_format_amount( $donor[ $column_name ], array( 'sanitize' => false ) ) );
123
				break;
124
125
			case 'date_created' :
126
				$value = date_i18n( give_date_format(), strtotime( $donor['date_created'] ) );
127
				break;
128
129
			default:
130
				$value = isset( $donor[ $column_name ] ) ? $donor[ $column_name ] : null;
131
				break;
132
		}
133
134
		return apply_filters( "give_report_column_{$column_name}", $value, $donor['id'] );
135
136
	}
137
138
	/**
139
	 * Column name.
140
	 *
141
	 * @param $donor
142
	 *
143
	 * @return string
144
	 */
145
	public function column_name( $donor ) {
146
		$name     = '#' . $donor['id'] . ' ';
147
		$name     .= ! empty( $donor['name'] ) ? $donor['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=' . $donor['id'] );
149
		$actions  = $this->get_row_actions( $donor );
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'          => __( 'Name', 'give' ),
164
			'email'         => __( 'Email', 'give' ),
165
			'num_donations' => __( 'Donations', 'give' ),
166
			'amount_spent'  => __( 'Total Donated', 'give' ),
167
			'date_created'  => __( 'Date Created', 'give' ),
168
		);
169
170
		return apply_filters( 'give_list_donors_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 View Code Duplication
	public function get_sortable_columns() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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_list_donors_sortable_columns', $columns );
191
	}
192
193
	/**
194
	 * Retrieve row actions.
195
	 *
196
	 * @since  1.7
197
	 * @access public
198
	 *
199
	 * @param $donor
200
	 *
201
	 * @return array An array of action links.
202
	 */
203
	public function get_row_actions( $donor ) {
204
205
		$actions = array(
206
207
			'view' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] ), sprintf( esc_attr__( 'View "%s"', 'give' ), $donor['name'] ), __( 'View Donor', 'give' ) ),
208
209
			'notes' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=notes&id=' . $donor['id'] ), sprintf( esc_attr__( 'Notes for "%s"', 'give' ), $donor['name'] ), __( 'Notes', 'give' ) ),
0 ignored issues
show
Expected next thing to be a escaping function, not '$donor'
Loading history...
210
211
			'delete' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=delete&id=' . $donor['id'] ), sprintf( esc_attr__( 'Delete "%s"', 'give' ), $donor['name'] ), __( 'Delete', 'give' ) ),
0 ignored issues
show
Expected next thing to be a escaping function, not '$donor'
Loading history...
212
213
		);
214
215
		return apply_filters( 'give_donor_row_actions', $actions, $donor );
216
217
	}
218
219
	/**
220
	 * Outputs bulk reviews
221
	 *
222
	 * @access public
223
	 *
224
	 * @param $which
225
	 *
226
	 * @since  1.0
227
	 * @return void
228
	 */
229
	public function bulk_actions( $which = '' ) {
230
		// These aren't really bulk actions but this outputs the markup in the right place.
231
	}
232
233
	/**
234
	 * Retrieve the current page number.
235
	 *
236
	 * @access public
237
	 * @since  1.0
238
	 * @return int Current page number.
239
	 */
240
	public function get_paged() {
241
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
242
	}
243
244
	/**
245
	 * Retrieves the search query string.
246
	 *
247
	 * @access public
248
	 * @since  1.0
249
	 * @return mixed string If search is present, false otherwise.
250
	 */
251
	public function get_search() {
252
		return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false;
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
Detected usage of a non-sanitized input variable: $_GET
Loading history...
253
	}
254
255
	/**
256
	 * Retrieves the donor data from db.
257
	 *
258
	 * @access public
259
	 * @since  1.0
260
	 *
261
	 * @return array $data The Donor data.
262
	 */
263 View Code Duplication
	public function donor_data() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
264
265
		$data = array();
266
267
		// Get donor query.
268
		$args   = $this->get_donor_query();
269
		$donors = Give()->donors->get_donors( $args );
270
271
		if ( $donors ) {
272
273
			foreach ( $donors as $donor ) {
274
275
				$user_id = ! empty( $donor->user_id ) ? intval( $donor->user_id ) : 0;
276
277
				$data[] = array(
278
					'id'            => $donor->id,
279
					'user_id'       => $user_id,
280
					'name'          => $donor->name,
281
					'email'         => $donor->email,
282
					'num_donations' => $donor->purchase_count,
283
					'amount_spent'  => $donor->purchase_value,
284
					'date_created'  => $donor->date_created,
285
				);
286
			}
287
		}
288
289
		return apply_filters( 'give_donors_column_query_data', $data );
290
	}
291
292
	/**
293
	 * Get donor count.
294
	 *
295
	 * @since  1.8.1
296
	 * @access private
297
	 */
298 View Code Duplication
	private function get_donor_count() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
		// Get donor query.
300
		$_donor_query = $this->get_donor_query();
301
302
		$_donor_query['number'] = - 1;
303
		$_donor_query['offset'] = 0;
304
		$donors                 = Give()->donors->get_donors( $_donor_query );
305
306
		return count( $donors );
307
	}
308
309
	/**
310
	 * Get donor query.
311
	 *
312
	 * @since  1.8.1
313
	 * @access public
314
	 * @return array
315
	 */
316 View Code Duplication
	public function get_donor_query() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
		$paged   = $this->get_paged();
318
		$offset  = $this->per_page * ( $paged - 1 );
319
		$search  = $this->get_search();
320
		$order   = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC';
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
321
		$orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id';
0 ignored issues
show
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
322
323
		$args = array(
324
			'number'  => $this->per_page,
325
			'offset'  => $offset,
326
			'order'   => $order,
327
			'orderby' => $orderby,
328
		);
329
330
		if ( $search ) {
331
			if ( is_email( $search ) ) {
332
				$args['email'] = $search;
333
			} elseif ( is_numeric( $search ) ) {
334
				$args['id'] = $search;
335
			} else {
336
				$args['name'] = $search;
337
			}
338
		}
339
340
		return $args;
341
	}
342
343
	/**
344
	 * Setup the final data for the table.
345
	 *
346
	 * @access public
347
	 * @since  1.0
348
	 * @return void
349
	 */
350 View Code Duplication
	public function prepare_items() {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
351
352
		$columns  = $this->get_columns();
353
		$hidden   = array(); // No hidden columns.
354
		$sortable = $this->get_sortable_columns();
355
356
		$this->_column_headers = array( $columns, $hidden, $sortable );
357
358
		$this->items = $this->donor_data();
359
360
		$this->total = $this->get_donor_count();
361
362
		$this->set_pagination_args( array(
363
			'total_items' => $this->total,
364
			'per_page'    => $this->per_page,
365
			'total_pages' => ceil( $this->total / $this->per_page ),
366
		) );
367
	}
368
}
369