Completed
Pull Request — master (#1749)
by Devin
06:21
created

Give_Gateway_Reports_Table   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 179
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A column_default() 0 6 1
A get_columns() 0 11 1
A get_paged() 0 3 2
A bulk_actions() 0 3 1
B display_tablenav() 0 30 3
A reports_data() 0 23 2
A prepare_items() 0 8 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 14.

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.

Loading history...
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
	public function __construct() {
45
		global $status, $page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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?
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
			default:
0 ignored issues
show
Unused Code introduced by
default: return $item[$column_name]; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
				return $item[ $column_name ];
71
		}
72
	}
73
74
	/**
75
	 * Retrieve the table columns
76
	 *
77
	 * @access public
78
	 * @since  1.0
79
	 * @return array $columns Array of all the list table columns
80
	 */
81
	public function get_columns() {
82
		$columns = array(
83
			'label'           => esc_attr__( 'Gateway', 'give' ),
84
			'complete_sales'  => esc_attr__( 'Complete Transactions', 'give' ),
85
			'pending_sales'   => esc_attr__( 'Pending / Failed Transactions', 'give' ),
86
			'total_sales'     => esc_attr__( 'Total Transactions', 'give' ),
87
			'total_donations' => esc_attr__( 'Total Donated', 'give' )
88
		);
89
90
		return $columns;
91
	}
92
93
94
	/**
95
	 * Retrieve the current page number
96
	 *
97
	 * @access public
98
	 * @since  1.0
99
	 * @return int Current page number
100
	 */
101
	public function get_paged() {
102
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
103
	}
104
105
106
	/**
107
	 * Outputs the reporting views
108
	 *
109
	 * @access public
110
	 * @since  1.0
111
	 * @return void
112
	 */
113
	public function bulk_actions( $which = '' ) {
114
115
	}
116
117
	/**
118
	 * Generate the table navigation above or below the table
119
	 *
120
	 * @since  1.0
121
	 * @access protected
122
	 *
123
	 * @param string $which
124
	 */
125
	protected function display_tablenav( $which ) {
126
127
		if ( 'top' === $which ) {
128
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
129
		}
130
		?>
131
		<div class="tablenav gateways-report-tablenav give-clearfix <?php echo esc_attr( $which ); ?>">
132
133
			<?php if ( 'top' === $which ) { ?>
134
				<h3 class="alignleft reports-earnings-title">
135
					<span><?php esc_html_e( 'Donation Methods Report', 'give' ); ?></span>
136
				</h3>
137
			<?php } ?>
138
139
			<div class="alignright tablenav-right">
140
				<div class="actions bulkactions">
141
					<?php $this->bulk_actions( $which ); ?>
142
				</div>
143
				<?php
144
				$this->extra_tablenav( $which );
145
				$this->pagination( $which );
146
				?>
147
			</div>
148
149
150
			<br class="clear" />
151
152
		</div>
153
		<?php
154
	}
155
156
157
	/**
158
	 * Build all the reports data
159
	 *
160
	 * @access public
161
	 * @since  1.0
162
	 * @return array $reports_data All the data for donor reports
163
	 */
164
	public function reports_data() {
165
166
		$reports_data = array();
167
		$gateways     = give_get_payment_gateways();
168
		$stats        = new Give_Payment_Stats();
169
170
		foreach ( $gateways as $gateway_id => $gateway ) {
171
172
			$complete_count = give_count_sales_by_gateway( $gateway_id, 'publish' );
173
			$pending_count  = give_count_sales_by_gateway( $gateway_id, array( 'pending', 'failed' ) );
174
175
			$reports_data[] = array(
176
				'ID'              => $gateway_id,
177
				'label'           => $gateway['admin_label'],
178
				'complete_sales'  => give_format_amount( $complete_count, false ),
179
				'pending_sales'   => give_format_amount( $pending_count, false ),
180
				'total_sales'     => give_format_amount( $complete_count + $pending_count, false ),
181
				'total_donations' => give_currency_filter( give_format_amount( $stats->get_earnings( 0, 0, 0, $gateway_id ) ) )
182
			);
183
		}
184
185
		return $reports_data;
186
	}
187
188
189
	/**
190
	 * Setup the final data for the table
191
	 *
192
	 * @access public
193
	 * @since  1.0
194
	 * @uses   Give_Gateway_Reports_Table::get_columns()
195
	 * @uses   Give_Gateway_Reports_Table::get_sortable_columns()
196
	 * @uses   Give_Gateway_Reports_Table::reports_data()
197
	 * @return void
198
	 */
199
	public function prepare_items() {
200
		$columns               = $this->get_columns();
201
		$hidden                = array(); // No hidden columns
202
		$sortable              = $this->get_sortable_columns();
203
		$this->_column_headers = array( $columns, $hidden, $sortable );
204
		$this->items           = $this->reports_data();
205
206
	}
207
}
208