Passed
Pull Request — master (#47)
by Kiran
04:02
created

WPInv_Subscriptions_List_Table::prepare_items()   C

Complexity

Conditions 12
Paths 72

Size

Total Lines 59
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 46
nc 72
nop 0
dl 0
loc 59
rs 6.4485
c 0
b 0
f 0

How to fix   Long Method    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
if ( ! defined( 'ABSPATH' ) ) {
3
    exit; // Exit if accessed directly
4
}
5
6
// Load WP_List_Table if not loaded
7
if ( ! class_exists( 'WP_List_Table' ) ) {
8
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
9
}
10
11
/**
12
 * Invoicing Subscriptions List Table Class
13
 *
14
 * @access      private
15
 */
16
class WPInv_Subscriptions_List_Table extends WP_List_Table {
17
18
    public $per_page        = 30;
19
    public $total_count     = 0;
20
    public $active_count    = 0;
21
    public $cancelled_count = 0;
22
    public $completed_count = 0;
23
    public $expired_count   = 0;
24
    public $failing_count   = 0;
25
    public $pending_count   = 0;
26
    public $stopped_count   = 0;
27
    public $trialling_count = 0;
28
29
    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...
30
        global $status, $page;
31
32
        parent::__construct( array(
33
            'singular'  => 'subscription',
34
            'plural'    => 'subscriptions',
35
            'ajax'      => false
36
        ) );
37
38
        $this->get_subscription_counts();
39
40
    }
41
42
    /**
43
     * Retrieve the view types
44
     *
45
     * @access public
46
     * @since 2.4
47
     * @return array $views All the views available
48
     */
49
    public function get_views() {
50
51
        $current         = isset( $_GET['status'] ) ? $_GET['status'] : '';
52
        $total_count     = '&nbsp;<span class="count">(' . $this->total_count    . ')</span>';
53
        $active_count    = '&nbsp;<span class="count">(' . $this->active_count . ')</span>';
54
        $pending_count   = '&nbsp;<span class="count">(' . $this->pending_count . ')</span>';
55
        $expired_count   = '&nbsp;<span class="count">(' . $this->expired_count  . ')</span>';
56
        $completed_count = '&nbsp;<span class="count">(' . $this->completed_count . ')</span>';
57
        $trialling_count  = '&nbsp;<span class="count">(' . $this->trialling_count   . ')</span>';
58
        $cancelled_count = '&nbsp;<span class="count">(' . $this->cancelled_count   . ')</span>';
59
        $failing_count   = '&nbsp;<span class="count">(' . $this->failing_count   . ')</span>';
60
61
        $views = array(
62
            'all'       => sprintf( '<a href="%s"%s>%s</a>', remove_query_arg( array( 'status', 'paged' ) ), $current === 'all' || $current == '' ? ' class="current"' : '', __('All','easy-digital-downloads' ) . $total_count ),
63
            'active'    => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'active', 'paged' => FALSE ) ), $current === 'active' ? ' class="current"' : '', __('Active','easy-digital-downloads' ) . $active_count ),
64
            'pending'   => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'pending', 'paged' => FALSE ) ), $current === 'pending' ? ' class="current"' : '', __('Pending','easy-digital-downloads' ) . $pending_count ),
65
            'expired'   => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'expired', 'paged' => FALSE ) ), $current === 'expired' ? ' class="current"' : '', __('Expired','easy-digital-downloads' ) . $expired_count ),
66
            'completed' => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'completed', 'paged' => FALSE ) ), $current === 'completed' ? ' class="current"' : '', __('Completed','easy-digital-downloads' ) . $completed_count ),
67
            'trialling'  => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'trialling', 'paged' => FALSE ) ), $current === 'trialling' ? ' class="current"' : '', __('Trialling','easy-digital-downloads' ) . $trialling_count ),
68
            'cancelled' => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'cancelled', 'paged' => FALSE ) ), $current === 'cancelled' ? ' class="current"' : '', __('Cancelled','easy-digital-downloads' ) . $cancelled_count ),
69
            'failing'   => sprintf( '<a href="%s"%s>%s</a>', add_query_arg( array( 'status' => 'failing', 'paged' => FALSE ) ), $current === 'failing' ? ' class="current"' : '', __('Failing','easy-digital-downloads' ) . $failing_count ),
70
        );
71
72
        return apply_filters( 'edd_recurring_subscriptions_table_views', $views );
73
    }
74
75
    /**
76
     * Show the search field
77
     *
78
     * @since 2.5
79
     * @access public
80
     *
81
     * @param string $text Label for the search box
82
     * @param string $input_id ID of the search box
83
     *
84
     * @return void
85
     */
86
    public function search_box( $text, $input_id ) {
87
88
        if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) {
89
            return;
90
        }
91
92
        $input_id = $input_id . '-search-input';
93
94 View Code Duplication
        if ( ! empty( $_REQUEST['orderby'] ) ) {
95
            echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
96
        }
97
98 View Code Duplication
        if ( ! empty( $_REQUEST['order'] ) ) {
99
            echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
100
        }
101
    ?>
102
        <p class="search-box">
103
            <?php do_action( 'edd_recurring_subscription_search_box' ); ?>
104
            <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
105
            <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
106
            <?php submit_button( $text, 'button', false, false, array('ID' => 'search-submit') ); ?><br/>
107
        </p>
108
    <?php
109
    }
110
111
    /**
112
     * Render most columns
113
     *
114
     * @access      private
115
     * @since       2.4
116
     * @return      string
117
     */
118
    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...
119
        return $item->$column_name;
120
    }
121
122
    /**
123
     * Customer column
124
     *
125
     * @access      private
126
     * @since       2.4
127
     * @return      string
128
     */
129
    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...
130
        $subscriber = new EDD_Recurring_Subscriber( $item->customer_id );
131
        $customer   = ! empty( $subscriber->name ) ? $subscriber->name : $subscriber->email;
132
133
        return '<a href="' . esc_url( admin_url( 'edit.php?post_type=download&page=edd-customers&view=overview&id=' . $subscriber->id ) ) . '">' . $customer . '</a>';
134
    }
135
136
137
    /**
138
     * Status column
139
     *
140
     * @access      private
141
     * @since       2.4
142
     * @return      string
143
     */
144
    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...
145
        return $item->get_status_label();
146
    }
147
148
    /**
149
     * Period column
150
     *
151
     * @access      private
152
     * @since       2.4
153
     * @return      string
154
     */
155
    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...
156
157
        $period = EDD_Recurring()->get_pretty_subscription_frequency( $item->period,$item->frequency );
158
159
        return edd_currency_filter( edd_format_amount( $item->recurring_amount ), edd_get_payment_currency_code( $item->parent_payment_id ) ) . ' / ' . $period;
160
    }
161
162
    /**
163
     * Billing Times column
164
     *
165
     * @access      private
166
     * @since       2.4
167
     * @return      string
168
     */
169
    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...
170
        return $item->get_times_billed() . ' / ' . ( ( $item->bill_times == 0 ) ? 'Until Cancelled' : $item->bill_times );
171
    }
172
173
    /**
174
     * Initial Amount column
175
     *
176
     * @access      private
177
     * @since       2.4
178
     * @return      string
179
     */
180
    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...
181
        return edd_currency_filter( edd_format_amount( $item->initial_amount ), edd_get_payment_currency_code( $item->parent_payment_id ) );
182
    }
183
184
    /**
185
     * Renewal date column
186
     *
187
     * @access      private
188
     * @since       2.4
189
     * @return      string
190
     */
191
    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...
192
        return $renewal_date = ! empty( $item->expiration ) ? date_i18n( get_option( 'date_format' ), strtotime( $item->expiration ) ) : __( 'N/A', 'edd-recurring' );
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...
193
    }
194
195
    /**
196
     * Payment column
197
     *
198
     * @access      private
199
     * @since       2.4
200
     * @return      string
201
     */
202
    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...
203
        return '<a href="' . esc_url( admin_url( 'edit.php?post_type=download&page=edd-payment-history&view=view-order-details&id=' . $item->parent_payment_id ) ) . '">' . edd_get_payment_number( $item->parent_payment_id ) . '</a>';
204
    }
205
206
    /**
207
     * Product ID column
208
     *
209
     * @access      private
210
     * @since       2.4
211
     * @return      string
212
     */
213
    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...
214
        return '<a href="' . esc_url( admin_url( 'post.php?action=edit&post=' . $item->product_id ) ) . '">' . get_the_title( $item->product_id ) . '</a>';
215
    }
216
217
    /**
218
     * Render the edit column
219
     *
220
     * @access      private
221
     * @since       2.0
222
     * @return      string
223
     */
224
    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...
225
        return '<a href="' . esc_url( admin_url( 'edit.php?post_type=download&page=edd-subscriptions&id=' . $item->id ) ) . '" title="' . esc_attr( __( 'View or edit subscription', 'edd-recurring' ) ) . '">' . __( 'View', 'edd-recurring' ) . '</a>';
226
    }
227
228
229
    /**
230
     * Retrieve the table columns
231
     *
232
     * @access      private
233
     * @since       2.4
234
     * @return      array
235
     */
236
237
    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...
238
        $columns = array(
239
            'customer_id'       => __( 'Customer', 'edd-recurring' ),
240
            'status'            => __( 'Status', 'edd-recurring' ),
241
            'period'            => __( 'Billing Cycle', 'edd-recurring' ),
242
            'initial_amount'    => __( 'Initial Amount', 'edd-recurring' ),
243
            'bill_times'        => __( 'Times Billed', 'edd-recurring' ),
244
            'renewal_date'      => __( 'Renewal Date', 'edd-recurring' ),
245
            'parent_payment_id' => __( 'Payment', 'edd-recurring' ),
246
            'product_id'        => edd_get_label_singular(),
247
            'actions'           => __( 'Actions', 'edd-recurring' ),
248
        );
249
250
        return apply_filters( 'edd_report_subscription_columns', $columns );
251
    }
252
253
    /**
254
     * Retrieve the current page number
255
     *
256
     * @access      private
257
     * @since       2.4
258
     * @return      int
259
     */
260
    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...
261
        return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
262
    }
263
264
    public function get_subscription_counts() {
265
266
        global $wp_query;
267
268
        $db = new EDD_Subscriptions_DB;
269
270
        $search = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
271
272
        $this->total_count     = $db->count();
273
        $this->active_count    = $db->count( array( 'status' => 'active', 'search' => $search ) );
274
        $this->pending_count   = $db->count( array( 'status' => 'pending', 'search' => $search ) );
275
        $this->expired_count   = $db->count( array( 'status' => 'expired', 'search' => $search ) );
276
        $this->trialling_count  = $db->count( array( 'status' => 'trialling', 'search' => $search ) );
277
        $this->cancelled_count = $db->count( array( 'status' => 'cancelled', 'search' => $search ) );
278
        $this->completed_count = $db->count( array( 'status' => 'completed', 'search' => $search ) );
279
        $this->failing_count   = $db->count( array( 'status' => 'failing', 'search' => $search ) );
280
    }
281
282
    /**
283
     * Setup the final data for the table
284
     *
285
     * @access      private
286
     * @since       2.4
287
     * @uses        $this->_column_headers
288
     * @uses        $this->items
289
     * @uses        $this->get_columns()
290
     * @uses        $this->get_sortable_columns()
291
     * @uses        $this->get_pagenum()
292
     * @uses        $this->set_pagination_args()
293
     * @return      array
294
     */
295
    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...
296
297
        $columns  = $this->get_columns();
298
        $hidden   = array(); // No hidden columns
299
        $status   = isset( $_GET['status'] ) ? $_GET['status'] : 'any';
300
        $sortable = $this->get_sortable_columns();
301
302
        $this->_column_headers = array( $columns, $hidden, $sortable );
303
304
        $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...
305
306
        $db     = new EDD_Subscriptions_DB;
307
        $search = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : '';
308
        $args   = array(
309
            'number' => $this->per_page,
310
            'offset' => $this->per_page * ( $this->get_paged() - 1 ),
311
            'search' => $search
312
        );
313
314
        if ( 'any' !== $status ) {
315
            $args['status'] = $status;
316
        }
317
318
        $this->items = $db->get_subscriptions( $args );
319
320
        switch ( $status ) {
321
            case 'active':
322
                $total_items = $this->active_count;
323
                break;
324
            case 'pending':
325
                $total_items = $this->pending_count;
326
                break;
327
            case 'expired':
328
                $total_items = $this->expired_count;
329
                break;
330
            case 'cancelled':
331
                $total_items = $this->cancelled_count;
332
                break;
333
            case 'failing':
334
                $total_items = $this->failing_count;
335
                break;
336
            case 'trialling':
337
                $total_items = $this->trialling_count;
338
                break;
339
            case 'completed':
340
                $total_items = $this->completed_count;
341
                break;
342
            case 'any':
343
            default:
344
                $total_items = $this->total_count;
345
                break;
346
        }
347
348
        $this->set_pagination_args( array(
349
            'total_items' => $total_items,
350
            'per_page'    => $this->per_page,
351
            'total_pages' => ceil( $total_items / $this->per_page )
352
        ) );
353
    }
354
}
355