Test Failed
Push — issues/2397 ( 4736c2...c66f81 )
by Ravinder
04:53
created

Give_Gateway_Reports_Table::column_default()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 9
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Gateways 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_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 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...
45
		global $status, $page;
46
47
		// Set parent defaults
48
		parent::__construct( array(
49
			'singular' => give_get_forms_label_singular(),    // Singular name of the listed records
50
			'plural'   => give_get_forms_label_plural(),        // Plural name of the listed records
51
			'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...
52
		) );
53
54
	}
55
56
	/**
57
	 * This function renders most of the columns in the list table.
58
	 *
59
	 * @access public
60
	 * @since  1.0
61
	 *
62
	 * @param array  $item        Contains all the data of the form
63
	 * @param string $column_name The name of the column
64
	 *
65
	 * @return string Column Name
66
	 */
67
	public function column_default( $item, $column_name ) {
68
		switch ( $column_name ) {
69
			case 'complete_sales':
70
				$status = 'publish';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
71
				
72
			case 'pending_sales':
73
				$value = $item[ $column_name ];
74
				$status = ! empty( $status ) ? $status : 'pending';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
75
76
				if ( $item[ $column_name ] ) {
77
					$value = sprintf(
78
						'<a href="%1$s" target="_blank">%2$s</a>',
79
						admin_url( "edit.php?post_type=give_forms&page=give-payment-history&status={$status}&gateway={$item['ID']}" ),
80
						$item[ $column_name ]
81
					);
82
				}
83
84
				break;
85
86
			default:
87
				$value = $item[ $column_name ];
88
		}
89
90
		return $value;
91
	}
92
93
	/**
94
	 * Retrieve the table columns
95
	 *
96
	 * @access public
97
	 * @since  1.0
98
	 * @return array $columns Array of all the list table columns
99
	 */
100
	public function get_columns() {
101
		$columns = array(
102
			'label'           => esc_attr__( 'Gateway', 'give' ),
103
			'complete_sales'  => esc_attr__( 'Complete Payments', 'give' ),
104
			'pending_sales'   => esc_attr__( 'Pending / Failed Payments', 'give' ),
105
			'total_sales'     => esc_attr__( 'Total Payments', 'give' ),
106
			'total_donations' => esc_attr__( 'Total Donated', 'give' )
107
		);
108
109
		return $columns;
110
	}
111
112
	/**
113
	 * Get the sortable columns
114
	 *
115
	 * @access public
116
	 * @since  1.8.12
117
	 * @return array Array of all the sortable columns
118
	 */
119
	public function get_sortable_columns() {
120
		return array(
121
			'total_donations' => array( 'total_donations', false )
122
		);
123
	}
124
125
126
	/**
127
	 * Retrieve the current page number
128
	 *
129
	 * @access public
130
	 * @since  1.0
131
	 * @return int Current page number
132
	 */
133
	public function get_paged() {
134
		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...
135
	}
136
137
138
	/**
139
	 * Outputs the reporting views
140
	 *
141
	 * @access public
142
	 * @since  1.0
143
	 * @return void
144
	 */
145
	public function bulk_actions( $which = '' ) {
146
147
	}
148
149
	/**
150
	 * Generate the table navigation above or below the table
151
	 *
152
	 * @since  1.0
153
	 * @access protected
154
	 *
155
	 * @param string $which
156
	 */
157 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...
158
159
		if ( 'top' === $which ) {
160
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
161
		}
162
		?>
163
		<div class="tablenav gateways-report-tablenav give-clearfix <?php echo esc_attr( $which ); ?>">
164
165
			<?php if ( 'top' === $which ) { ?>
166
				<h2 class="alignleft reports-earnings-title screen-reader-text">
167
					<?php _e( 'Donation Methods Report', 'give' ); ?>
168
				</h2>
169
			<?php } ?>
170
171
			<div class="alignright tablenav-right">
172
				<div class="actions bulkactions">
173
					<?php $this->bulk_actions( $which ); ?>
174
				</div>
175
				<?php
176
				$this->extra_tablenav( $which );
177
				$this->pagination( $which );
178
				?>
179
			</div>
180
181
182
			<br class="clear" />
183
184
		</div>
185
		<?php
186
	}
187
188
	/**
189
	 * Reorder User Defined Array
190
	 *
191
	 * @param $old_value
192
	 * @param $new_value
193
	 *
194
	 * @access public
195
	 * @since  1.8.12
196
	 *
197
	 * @return int
198
	 */
199
	public function give_sort_total_donations( $old_value, $new_value ) {
200
		// If no sort, default to label.
201
		$orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'label';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
202
203
		//If no order, default to asc.
204
		$order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
205
206
		//Determine sort order.
207
		$result = strcmp( $old_value[ $orderby ], $new_value[ $orderby ] );
208
209
		return ( $order === 'asc' ) ? $result : -$result;
210
	}
211
212
213
	/**
214
	 * Build all the reports data
215
	 *
216
	 * @access public
217
	 * @since  1.0
218
	 * @return array $reports_data All the data for donor reports
219
	 */
220
	public function reports_data() {
221
222
		$reports_data = array();
223
		$gateways     = give_get_payment_gateways();
224
		$stats        = new Give_Payment_Stats();
225
226
		foreach ( $gateways as $gateway_id => $gateway ) {
227
228
			$complete_count = give_count_sales_by_gateway( $gateway_id, 'publish' );
229
			$pending_count  = give_count_sales_by_gateway( $gateway_id, array( 'pending', 'failed' ) );
0 ignored issues
show
Documentation introduced by
array('pending', 'failed') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
230
231
			$reports_data[] = array(
232
				'ID'              => $gateway_id,
233
				'label'           => $gateway['admin_label'],
234
				'complete_sales'  => $complete_count,
235
				'pending_sales'   => $pending_count,
236
				'total_sales'     => $complete_count + $pending_count,
237
				'total_donations' => give_currency_filter( give_format_amount( $stats->get_earnings( 0, strtotime('04/13/2015' ), current_time('timestamp' ), $gateway_id ), array( 'sanitize' => false ) ) ),
0 ignored issues
show
Documentation introduced by
strtotime('04/13/2015') is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$gateway_id is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
238
			);
239
		}
240
241
		return $reports_data;
242
	}
243
244
	/**
245
	 * Setup the final data for the table
246
	 *
247
	 * @access public
248
	 * @since  1.0
249
	 * @uses   Give_Gateway_Reports_Table::get_columns()
250
	 * @uses   Give_Gateway_Reports_Table::get_sortable_columns()
251
	 * @uses   Give_Gateway_Reports_Table::reports_data()
252
	 * @return void
253
	 */
254
	public function prepare_items() {
255
		$columns               = $this->get_columns();
256
		$hidden                = array(); // No hidden columns
257
		$sortable              = $this->get_sortable_columns();
258
		$this->_column_headers = array( $columns, $hidden, $sortable );
259
		$this->items           = $this->reports_data();
260
261
		// Sort Array when we are sorting data in array.
262
		usort( $this->items, array( $this, 'give_sort_total_donations' ) );
263
264
	}
265
}
266