Test Failed
Push — issues/1925 ( 817edf )
by Ravinder
04:56
created

Give_Form_Reports_Table::column_default()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 23
nc 8
nop 2
dl 0
loc 28
rs 5.3846
c 0
b 0
f 0
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 of all the donation forms
39
	 * @since 1.0
40
	 */
41
	private $donation_forms;
42
43
	/**
44
	 * @var int Total number of Donation Forms
45
	 * @since 1.8.11
46
	 */
47
	public $count;
48
49
	/**
50
	 * Get things started
51
	 *
52
	 * @since 1.0
53
	 * @see   WP_List_Table::__construct()
54
	 */
55 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
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...
56
		global $status, $page;
57
58
		// Set parent defaults
59
		parent::__construct( array(
60
			'singular' => give_get_forms_label_singular(),    // Singular name of the listed records.
61
			'plural'   => give_get_forms_label_plural(),        // Plural name of the listed records.
62
			'ajax'     => false                        // Does this table support ajax?
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
63
		) );
64
65
		add_action( 'give_report_view_actions', array( $this, 'category_filter' ) );
66
		$this->query();
67
68
	}
69
70
	/**
71
	 * This function renders most of the columns in the list table.
72
	 *
73
	 * @param array  $item        Contains all the data of the donation form
74
	 * @param string $column_name The name of the column
75
	 *
76
	 * @access public
77
	 * @since  1.0
78
	 *
79
	 * @return string Column Name
80
	 */
81
	public function column_default( $item, $column_name ) {
82
		switch ( $column_name ) {
83
			case 'title':
84
				$title = empty( $item['title'] ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $item['ID'] ) : $item['title'];
85
86
				return sprintf(
87
					'<a href="%s">%s</a>',
88
					get_edit_post_link( $item['ID'] ),
89
					$title
90
				);
91
			case 'sales':
92
				return sprintf(
93
					'<a href="%s">%s</a>',
94
					admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&form_id=' . urlencode( $item['ID'] ) ),
95
					$item['sales']
96
				);
97
			case 'earnings' :
98
				return give_currency_filter( give_format_amount( $item[ $column_name ], array( 'sanitize' => false ) ) );
99
			case 'average_sales' :
100
				return round( $item[ $column_name ] );
101
			case 'average_earnings' :
102
				return give_currency_filter( give_format_amount( $item[ $column_name ], array( 'sanitize' => false ) ) );
103
			case 'details' :
104
				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>';
105
			default:
106
				return $item[ $column_name ];
107
		}
108
	}
109
110
	/**
111
	 * Retrieve the table columns
112
	 *
113
	 * @access public
114
	 * @since  1.0
115
	 *
116
	 * @return array $columns Array of all the list table columns
117
	 */
118
	public function get_columns() {
119
		$columns = array(
120
			'title'            => esc_html__( 'Form', 'give' ),
121
			'sales'            => esc_html__( 'Donations', 'give' ),
122
			'earnings'         => esc_html__( 'Income', 'give' ),
123
			'average_sales'    => esc_html__( 'Monthly Average Donations', 'give' ),
124
			'average_earnings' => esc_html__( 'Monthly Average Income', 'give' ),
125
			'details'          => esc_html__( 'Detailed Report', 'give' )
126
		);
127
128
		return $columns;
129
	}
130
131
	/**
132
	 * Retrieve the table's sortable columns
133
	 *
134
	 * @access public
135
	 * @since  1.0
136
	 *
137
	 * @return array Array of all the sortable columns
138
	 */
139
	public function get_sortable_columns() {
140
		return array(
141
			'title'    => array( 'title', true ),
142
			'sales'    => array( 'sales', false ),
143
			'earnings' => array( 'earnings', false ),
144
		);
145
	}
146
147
	/**
148
	 * Retrieve the current page number
149
	 *
150
	 * @access public
151
	 * @since  1.0
152
	 *
153
	 * @return int Current page number
154
	 */
155
	public function get_paged() {
156
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
157
	}
158
159
	/**
160
	 * Retrieve the category being viewed
161
	 *
162
	 * @access public
163
	 * @since  1.0
164
	 *
165
	 * @return int Category ID
166
	 */
167
	public function get_category() {
168
		return isset( $_GET['category'] ) ? absint( $_GET['category'] ) : 0;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
169
	}
170
171
	/**
172
	 * Outputs the reporting views
173
	 *
174
	 * @access public
175
	 * @since  1.0
176
	 *
177
	 * @return void
178
	 */
179
	public function bulk_actions( $which = '' ) {
180
181
	}
182
183
	/**
184
	 * Generate the table navigation above or below the table
185
	 *
186
	 * @since  1.0
187
	 * @access protected
188
	 *
189
	 * @param string $which
190
	 */
191 View Code Duplication
	protected function display_tablenav( $which ) {
0 ignored issues
show
Duplication introduced by
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...
192
193
		if ( 'top' === $which ) {
194
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
195
		}
196
		?>
197
		<div class="tablenav give-clearfix <?php echo esc_attr( $which ); ?>">
198
199
			<?php if ( 'top' === $which ) { ?>
200
				<h3 class="alignleft reports-earnings-title">
201
					<span><?php esc_html_e( 'Donation Forms Report', 'give' ); ?></span>
202
				</h3>
203
			<?php } ?>
204
205
			<div class="alignright tablenav-right">
206
				<div class="actions bulkactions">
207
					<?php $this->bulk_actions( $which ); ?>
208
				</div>
209
				<?php
210
				$this->extra_tablenav( $which );
211
				$this->pagination( $which );
212
				?>
213
			</div>
214
215
			<br class="clear" />
216
217
		</div>
218
		<?php
219
	}
220
221
	/**
222
	 * Attaches the category filter to the log views
223
	 *
224
	 * @access public
225
	 * @since  1.0
226
	 *
227
	 * @return void
228
	 */
229
	public function category_filter() {
230
231
		$categories = get_terms( 'form_category' );
232
		if ( $categories && ! is_wp_error( $categories ) ) {
233
			echo Give()->html->category_dropdown( 'category', $this->get_category() );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'Give'
Loading history...
234
		}
235
	}
236
237
	/**
238
	 * Performs the donation forms query
239
	 *
240
	 * @access public
241
	 * @since  1.0
242
	 *
243
	 * @return void
244
	 */
245
	public function query() {
246
247
		$orderby  = isset( $_GET['orderby'] ) ? $_GET['orderby'] : 'title';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
248
		$order    = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
249
		$category = $this->get_category();
250
251
		$args = array(
252
			'post_type'        => 'give_forms',
253
			'post_status'      => 'publish',
254
			'order'            => $order,
255
			'fields'           => 'ids',
256
			'posts_per_page'   => $this->per_page,
257
			'paged'            => $this->get_paged(),
258
			'suppress_filters' => true
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
259
		);
260
261
		if ( ! empty( $category ) ) {
262
			$args['tax_query'] = array(
0 ignored issues
show
introduced by
Detected usage of tax_query, possible slow query.
Loading history...
263
				array(
264
					'taxonomy' => 'form_category',
265
					'terms'    => $category
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
266
				)
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
267
			);
268
		}
269
270
		switch ( $orderby ) :
271
			case 'title' :
272
				$args['orderby'] = 'title';
273
				break;
274
275
			case 'sales' :
276
				$args['orderby']  = 'meta_value_num';
277
				$args['meta_key'] = '_give_form_sales';
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
278
				break;
279
280
			case 'earnings' :
281
				$args['orderby']  = 'meta_value_num';
282
				$args['meta_key'] = '_give_form_earnings';
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
283
				break;
284
		endswitch;
285
286
		$args = apply_filters( 'give_form_reports_prepare_items_args', $args, $this );
287
288
		$this->donation_forms = new WP_Query( $args );
289
290
		// Store total number of donation forms count.
291
		$this->count = $this->donation_forms->found_posts;
292
293
	}
294
295
	/**
296
	 * Build all the reports data
297
	 *
298
	 * @access public
299
	 * @since  1.0
300
	 *
301
	 * @return array $reports_data All the data for donor reports
302
	 */
303
	public function reports_data() {
304
		$reports_data = array();
305
306
		$give_forms = $this->donation_forms->posts;
307
308
		if ( $give_forms ) {
309
			foreach ( $give_forms as $form ) {
310
				$reports_data[] = array(
311
					'ID'               => $form,
312
					'title'            => get_the_title( $form ),
313
					'sales'            => give_get_form_sales_stats( $form ),
314
					'earnings'         => give_get_form_earnings_stats( $form ),
315
					'average_sales'    => give_get_average_monthly_form_sales( $form ),
316
					'average_earnings' => give_get_average_monthly_form_earnings( $form )
317
				);
318
			}
319
		}
320
321
		return $reports_data;
322
	}
323
324
	/**
325
	 * Setup the final data for the table
326
	 *
327
	 * @access public
328
	 * @since  1.5
329
	 *
330
	 * @uses   Give_Form_Reports_Table::get_columns()
331
	 * @uses   Give_Form_Reports_Table::get_sortable_columns()
332
	 * @uses   Give_Form_Reports_Table::reports_data()
333
	 * @uses   Give_Form_Reports_Table::get_pagenum()
334
	 *
335
	 * @return void
336
	 */
337 View Code Duplication
	public function prepare_items() {
0 ignored issues
show
Duplication introduced by
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...
338
		$columns = $this->get_columns();
339
		$hidden = array(); // No hidden columns
340
		$sortable = $this->get_sortable_columns();
341
		$this->_column_headers = array( $columns, $hidden, $sortable );
342
		$this->items = $this->reports_data();
343
		$total_items = $this->count;
344
345
		$this->set_pagination_args( array(
346
				'total_items' => $total_items,
347
				'per_page'    => $this->per_page,
348
				'total_pages' => ceil( $total_items / $this->per_page )
349
			)
350
		);
351
	}
352
}
353