Passed
Pull Request — master (#126)
by Kiran
03:46
created

WPInv_Subscription_Reports_Table::get_views()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 2
nop 0
dl 0
loc 25
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Subscription List Table Class
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
9
10
// Exit if accessed directly
11
if ( ! defined( 'ABSPATH' ) ) exit;
12
13
14
// Load WP_List_Table if not loaded
15
if( ! class_exists( 'WP_List_Table' ) ) {
16
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
17
}
18
19
/**
20
 * Subscriptions List Table Class
21
 *
22
 * @access      private
23
 */
24
class WPInv_Subscription_Reports_Table extends WP_List_Table {
25
26
	/**
27
	 * Number of results to show per page
28
	 *
29
	 * @since       1.0.0
30
	 */
31
32
	public $per_page        = 20;
33
	public $total_count     = 0;
34
	public $active_count    = 0;
35
	public $pending_count   = 0;
36
	public $expired_count   = 0;
37
	public $completed_count = 0;
38
	public $trialling_count  = 0;
39
	public $cancelled_count = 0;
40
	public $failing_count   = 0;
41
42
	/**
43
	 * Get things started
44
	 *
45
	 * @access      private
46
	 * @since       1.0.0
47
	 * @return      void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
	 */
49
	function __construct(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
		global $status, $page;
51
52
		// Set parent defaults
53
		parent::__construct( array(
54
			'singular'  => 'subscription',
55
			'plural'    => 'subscriptions',
56
			'ajax'      => false
57
		) );
58
59
		$this->get_subscription_counts();
60
61
	}
62
63
	/**
64
	 * Retrieve the view types
65
	 *
66
	 * @access public
67
	 * @since 1.0.0
68
	 * @return array $views All the views available
69
	 */
70
	public function get_views() {
71
72
		$current         = isset( $_GET['status'] ) ? $_GET['status'] : '';
73
		$total_count     = '&nbsp;<span class="count">(' . $this->total_count    . ')</span>';
74
		$active_count    = '&nbsp;<span class="count">(' . $this->active_count . ')</span>';
75
		$pending_count   = '&nbsp;<span class="count">(' . $this->pending_count . ')</span>';
76
		$expired_count   = '&nbsp;<span class="count">(' . $this->expired_count  . ')</span>';
77
		$completed_count = '&nbsp;<span class="count">(' . $this->completed_count . ')</span>';
78
		$trialling_count  = '&nbsp;<span class="count">(' . $this->trialling_count   . ')</span>';
79
		$cancelled_count = '&nbsp;<span class="count">(' . $this->cancelled_count   . ')</span>';
80
		$failing_count   = '&nbsp;<span class="count">(' . $this->failing_count   . ')</span>';
81
82
		$views = array(
83
			'all'       => sprintf( '<a href="%s"%s>%s</a>', remove_query_arg( array( 'status', 'paged' ) ), $current === 'all' || $current == '' ? ' class="current"' : '', __('All','invoicing' ) . $total_count ),
84
			'active'    => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'active', 'paged' => FALSE ) ), $current === 'active' ? ' class="current"' : '', __('Active','invoicing' ) . $active_count ),
85
			'pending'   => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'pending', 'paged' => FALSE ) ), $current === 'pending' ? ' class="current"' : '', __('Pending','invoicing' ) . $pending_count ),
86
			'expired'   => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'expired', 'paged' => FALSE ) ), $current === 'expired' ? ' class="current"' : '', __('Expired','invoicing' ) . $expired_count ),
87
			'completed' => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'completed', 'paged' => FALSE ) ), $current === 'completed' ? ' class="current"' : '', __('Completed','invoicing' ) . $completed_count ),
88
			'trialling'  => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'trialling', 'paged' => FALSE ) ), $current === 'trialling' ? ' class="current"' : '', __('Trialling','invoicing' ) . $trialling_count ),
89
			'cancelled' => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'cancelled', 'paged' => FALSE ) ), $current === 'cancelled' ? ' class="current"' : '', __('Cancelled','invoicing' ) . $cancelled_count ),
90
			'failing'   => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'failing', 'paged' => FALSE ) ), $current === 'failing' ? ' class="current"' : '', __('Failing','invoicing' ) . $failing_count ),
91
		);
92
93
		return apply_filters( 'wpinv_recurring_subscriptions_table_views', $views );
94
	}
95
96
	/**
97
	 * Show the search field
98
	 *
99
	 * @since 2.5
100
	 * @access public
101
	 *
102
	 * @param string $text Label for the search box
103
	 * @param string $input_id ID of the search box
104
	 *
105
	 * @return void
106
	 */
107
	public function search_box( $text, $input_id ) {
108
109
		if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
110
			return;
111
		}
112
113
		$input_id = $input_id . '-search-input';
114
115 View Code Duplication
		if ( ! empty( $_REQUEST['orderby'] ) ) {
116
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
117
		}
118
119 View Code Duplication
		if ( ! empty( $_REQUEST['order'] ) ) {
120
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
121
		}
122
?>
123
		<p class="search-box">
124
			<?php do_action( 'wpinv_recurring_subscription_search_box' ); ?>
125
			<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
126
			<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
127
			<?php submit_button( $text, 'button', false, false, array('ID' => 'search-submit') ); ?><br/>
128
		</p>
129
<?php
130
	}
131
132
	/**
133
	 * Render most columns
134
	 *
135
	 * @access      private
136
	 * @since       1.0.0
137
	 * @return      string
138
	 */
139
	function column_default( $item, $column_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
140
		return $item->$column_name;
141
	}
142
143
    /**
144
     * Subscription id column
145
     *
146
     * @access      private
147
     * @since       1.0.0
148
     * @return      string
149
     */
150
    function column_sub_id( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
151
        return '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $item->id ) ) . '" target="_blank">' . $item->id . '</a>';
152
    }
153
154
	/**
155
	 * Customer column
156
	 *
157
	 * @access      private
158
	 * @since       1.0.0
159
	 * @return      string
160
	 */
161
	function column_customer_id( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
162
		$subscriber = get_userdata( $item->customer_id );
163
		$customer   = ! empty( $subscriber->display_name ) ? $subscriber->display_name : $subscriber->user_email;
164
165
		return '<a href="' . esc_url( get_edit_user_link( $item->customer_id ) ) . '" target="_blank">' . $customer . '</a>';
166
	}
167
168
	/**
169
	 * Status column
170
	 *
171
	 * @access      private
172
	 * @since       1.0.0
173
	 * @return      string
174
	 */
175
	function column_status( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
176
		return $item->get_status_label();
177
	}
178
179
	/**
180
	 * Period column
181
	 *
182
	 * @access      private
183
	 * @since       1.0.0
184
	 * @return      string
185
	 */
186
	function column_period( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
187
188
		$period = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $item->period,$item->frequency );
189
190
		return wpinv_price( wpinv_format_amount( $item->recurring_amount ), wpinv_get_invoice_currency_code( $item->parent_payment_id ) ) . ' / ' . $period;
191
	}
192
193
	/**
194
	 * Billing Times column
195
	 *
196
	 * @access      private
197
	 * @since       1.0.0
198
	 * @return      string
199
	 */
200
	function column_bill_times( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
201
		return $item->get_times_billed() . ' / ' . ( ( $item->bill_times == 0 ) ? 'Until Cancelled' : $item->bill_times );
202
	}
203
204
	/**
205
	 * Initial Amount column
206
	 *
207
	 * @access      private
208
	 * @since       1.0.0
209
	 * @return      string
210
	 */
211
	function column_initial_amount( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
212
		return wpinv_price( wpinv_format_amount( $item->initial_amount ), wpinv_get_invoice_currency_code( $item->parent_payment_id ) );
213
	}
214
215
	/**
216
	 * Renewal date column
217
	 *
218
	 * @access      private
219
	 * @since       1.0.0
220
	 * @return      string
221
	 */
222
	function column_renewal_date( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
223
		return $renewal_date = ! empty( $item->expiration ) ? date_i18n( get_option( 'date_format' ), strtotime( $item->expiration ) ) : __( 'N/A', 'invoicing' );
0 ignored issues
show
Unused Code introduced by
$renewal_date is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
	}
225
226
	/**
227
	 * Payment column
228
	 *
229
	 * @access      private
230
	 * @since       1.0.0
231
	 * @return      string
232
	 */
233
	function column_parent_payment_id( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
234
		return '<a href="' . get_edit_post_link( $item->parent_payment_id ) . '" target="_blank">' . wpinv_get_invoice_number( $item->parent_payment_id ) . '</a>';
235
	}
236
237
	/**
238
	 * Product ID column
239
	 *
240
	 * @access      private
241
	 * @since       1.0.0
242
	 * @return      string
243
	 */
244
	function column_product_id( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
245
		return '<a href="' . esc_url( admin_url( 'post.php?action=edit&post=' . $item->product_id ) ) . '" target="_blank">' . get_the_title( $item->product_id ) . '</a>';
246
	}
247
248
	/**
249
	 * Render the edit column
250
	 *
251
	 * @access      private
252
	 * @since       2.0
253
	 * @return      string
254
	 */
255
	function column_actions( $item ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
256
		return '<a href="' . esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $item->id ) ) . '" title="' . esc_attr( __( 'View or edit subscription', 'invoicing' ) ) . '" target="_blank">' . __( 'View', 'invoicing' ) . '</a>';
257
	}
258
259
260
	/**
261
	 * Retrieve the table columns
262
	 *
263
	 * @access      private
264
	 * @since       1.0.0
265
	 * @return      array
266
	 */
267
268
	function get_columns(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
269
		$columns = array(
270
			'sub_id'            => __( 'ID', 'invoicing' ),
271
			'customer_id'       => __( 'Customer', 'invoicing' ),
272
			'status'            => __( 'Status', 'invoicing' ),
273
			'period'            => __( 'Billing Cycle', 'invoicing' ),
274
			'initial_amount'    => __( 'Initial Amount', 'invoicing' ),
275
			'bill_times'        => __( 'Times Billed', 'invoicing' ),
276
			'renewal_date'      => __( 'Renewal Date', 'invoicing' ),
277
			'parent_payment_id' => __( 'Invoice', 'invoicing' ),
278
			'product_id'        => __( 'Item', 'invoicing' ),
279
			'actions'           => __( 'Actions', 'invoicing' ),
280
		);
281
282
		return apply_filters( 'wpinv_report_subscription_columns', $columns );
283
	}
284
285
	/**
286
	 * Retrieve the current page number
287
	 *
288
	 * @access      private
289
	 * @since       1.0.0
290
	 * @return      int
291
	 */
292
	function get_paged() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
293
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
294
	}
295
296
	/**
297
	 * Retrieve the subscription counts
298
	 *
299
	 * @access public
300
	 * @since 1.4
301
	 * @return void
302
	 */
303
	public function get_subscription_counts() {
304
305
		global $wp_query;
306
307
		$db = new WPInv_Subscriptions_DB;
308
309
		$search = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
310
311
		$this->total_count     = $db->count();
312
		$this->active_count    = $db->count( array( 'status' => 'active', 'search' => $search ) );
313
		$this->pending_count   = $db->count( array( 'status' => 'pending', 'search' => $search ) );
314
		$this->expired_count   = $db->count( array( 'status' => 'expired', 'search' => $search ) );
315
		$this->trialling_count  = $db->count( array( 'status' => 'trialling', 'search' => $search ) );
316
		$this->cancelled_count = $db->count( array( 'status' => 'cancelled', 'search' => $search ) );
317
		$this->completed_count = $db->count( array( 'status' => 'completed', 'search' => $search ) );
318
		$this->failing_count   = $db->count( array( 'status' => 'failing', 'search' => $search ) );
319
320
	}
321
322
	/**
323
	 * Setup the final data for the table
324
	 *
325
	 * @access      private
326
	 * @since       1.0.0
327
	 * @uses        $this->_column_headers
328
	 * @uses        $this->items
329
	 * @uses        $this->get_columns()
330
	 * @uses        $this->get_sortable_columns()
331
	 * @uses        $this->get_pagenum()
332
	 * @uses        $this->set_pagination_args()
333
	 * @return      array
334
	 */
335
	function prepare_items() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
336
337
		$columns  = $this->get_columns();
338
		$hidden   = array(); // No hidden columns
339
		$status   = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
340
		$sortable = $this->get_sortable_columns();
341
342
		$this->_column_headers = array( $columns, $hidden, $sortable );
343
344
		$current_page = $this->get_pagenum();
0 ignored issues
show
Unused Code introduced by
$current_page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
345
346
		$db     = new WPInv_Subscriptions_DB;
347
		$search = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
348
		$args   = array(
349
			'number' => $this->per_page,
350
			'offset' => $this->per_page * ( $this->get_paged() - 1 ),
351
			'search' => $search
352
		);
353
354
		if ( 'any' !== $status ) {
355
			$args['status'] = $status;
356
		}
357
358
		$this->items = $db->get_subscriptions( $args );
359
360
		switch ( $status ) {
361
			case 'active':
362
				$total_items = $this->active_count;
363
				break;
364
			case 'pending':
365
				$total_items = $this->pending_count;
366
				break;
367
			case 'expired':
368
				$total_items = $this->expired_count;
369
				break;
370
			case 'cancelled':
371
				$total_items = $this->cancelled_count;
372
				break;
373
			case 'failing':
374
				$total_items = $this->failing_count;
375
				break;
376
			case 'trialling':
377
				$total_items = $this->trialling_count;
378
				break;
379
			case 'completed':
380
				$total_items = $this->completed_count;
381
				break;
382
			case 'any':
383
			default:
384
				$total_items = $this->total_count;
385
				break;
386
		}
387
388
		$this->set_pagination_args( array(
389
			'total_items' => $total_items,
390
			'per_page'    => $this->per_page,
391
			'total_pages' => ceil( $total_items / $this->per_page )
392
		) );
393
	}
394
}
395