Passed
Push — master ( eba4be...60f62d )
by Brian
09:36 queued 04:47
created

wpinv_request()   C

Complexity

Conditions 16
Paths 21

Size

Total Lines 68
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 68
rs 5.5666
cc 16
nc 21
nop 1

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
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
add_action( 'manage_wpi_discount_posts_custom_column', 'wpinv_discount_custom_column' );
8
function wpinv_discount_custom_column( $column ) {
9
    global $post;
10
11
    $discount = new WPInv_Discount( $post );
12
13
    switch ( $column ) {
14
        case 'code' :
15
            echo $discount->get_code();
16
        break;
17
        case 'amount' :
18
            echo $discount->get_formatted_amount();
19
        break;
20
        case 'usage' :
21
            echo $discount->get_usage();
22
        break;
23
        case 'start_date' :
24
            if ( $discount->has_start_date() ) {
25
                $value = date_i18n( get_option( 'date_format' ), strtotime( $discount->get_start_date() ) );
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
                $value = date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $discount->get_start_date() ) );
Loading history...
26
            } else {
27
                $value = '&mdash;';
28
            }
29
30
            echo $value;
31
        break;
32
        case 'expiry_date' :
33
            if ( $discount->has_expiration_date() ) {
34
                $value = date_i18n( get_option( 'date_format' ), strtotime( $discount->get_expiration_date() ) );
35
            } else {
36
                $value = __( 'Never', 'invoicing' );
37
            }
38
39
            echo $value;
40
        break;
41
    }
42
}
43
44
add_filter( 'post_row_actions', 'wpinv_post_row_actions', 9999, 2 );
45
function wpinv_post_row_actions( $actions, $post ) {
46
    $post_type = !empty( $post->post_type ) ? $post->post_type : '';
47
48
    if ( $post_type == 'wpi_invoice' ) {
49
        $actions = array();
50
    }
51
52
    if ( $post_type == 'wpi_discount' ) {
53
        $actions = wpinv_discount_row_actions( $post, $actions );
54
    }
55
56
    return $actions;
57
}
58
59
function wpinv_discount_row_actions( $discount, $row_actions ) {
60
    $row_actions  = array();
61
    $edit_link = get_edit_post_link( $discount->ID );
62
    $row_actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'invoicing' ) . '</a>';
63
64
    if( in_array( strtolower( $discount->post_status ),  array(  'publish' ) ) ) {
65
        $row_actions['deactivate'] = '<a href="' . esc_url( wp_nonce_url( add_query_arg( array( 'wpi_action' => 'deactivate_discount', 'discount' => $discount->ID ) ), 'wpinv_discount_nonce' ) ) . '">' . __( 'Deactivate', 'invoicing' ) . '</a>';
66
    } elseif( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
67
        $row_actions['activate'] = '<a href="' . esc_url( wp_nonce_url( add_query_arg( array( 'wpi_action' => 'activate_discount', 'discount' => $discount->ID ) ), 'wpinv_discount_nonce' ) ) . '">' . __( 'Activate', 'invoicing' ) . '</a>';
68
    }
69
70
    $delete_url = esc_url(
71
        wp_nonce_url(
72
            add_query_arg(
73
                array(
74
                    'wpi_action' => 'delete_discount',
75
                    'discount' => $discount->ID
76
                )
77
            ),
78
            'wpinv_discount_nonce'
79
        )
80
    );
81
    $row_actions['delete'] = '<a href="' . $delete_url . '">' . __( 'Delete', 'invoicing' ) . '</a>';
82
83
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
84
85
    return $row_actions;
86
}
87
88
add_filter( 'list_table_primary_column', 'wpinv_table_primary_column', 10, 2 );
89
function wpinv_table_primary_column( $default, $screen_id ) {
90
    if ( 'edit-wpi_invoice' === $screen_id ) {
91
        return 'name';
92
    }
93
94
    return $default;
95
}
96
97
function wpinv_discount_bulk_actions( $actions, $display = false ) {
98
    if ( !$display ) {
99
        return array();
100
    }
101
102
    $actions = array(
103
        'activate'   => __( 'Activate', 'invoicing' ),
104
        'deactivate' => __( 'Deactivate', 'invoicing' ),
105
        'delete'     => __( 'Delete', 'invoicing' ),
106
    );
107
    $two = '';
108
    $which = 'top';
109
    echo '</div><div class="alignleft actions bulkactions">';
110
    echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
111
    echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">";
112
    echo '<option value="-1">' . __( 'Bulk Actions' ) . "</option>";
113
114
    foreach ( $actions as $name => $title ) {
115
        $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
116
117
        echo "" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>";
118
    }
119
    echo "</select>";
120
121
    submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
122
123
    echo '</div><div class="alignleft actions">';
124
}
125
add_filter( 'bulk_actions-edit-wpi_discount', 'wpinv_discount_bulk_actions', 10 );
126
127
function wpinv_disable_months_dropdown( $disable, $post_type ) {
128
    if ( $post_type == 'wpi_discount' ) {
129
        $disable = true;
130
    }
131
132
    return $disable;
133
}
134
add_filter( 'disable_months_dropdown', 'wpinv_disable_months_dropdown', 10, 2 );
135
136
function wpinv_restrict_manage_posts() {
137
    global $typenow;
138
139
    if( 'wpi_discount' == $typenow ) {
140
        wpinv_discount_filters();
141
    }
142
}
143
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
144
145
function wpinv_discount_filters() {
146
    echo wpinv_discount_bulk_actions( array(), true );
0 ignored issues
show
Bug introduced by
Are you sure the usage of wpinv_discount_bulk_actions(array(), true) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
147
148
    ?>
149
    <select name="discount_type" id="dropdown_wpinv_discount_type">
150
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
151
        <?php
152
            $types = wpinv_get_discount_types();
153
154
            foreach ( $types as $name => $type ) {
155
                echo '<option value="' . esc_attr( $name ) . '"';
156
157
                if ( isset( $_GET['discount_type'] ) )
158
                    selected( $name, $_GET['discount_type'] );
159
160
                echo '>' . esc_html__( $type, 'invoicing' ) . '</option>';
161
            }
162
        ?>
163
    </select>
164
    <?php
165
}
166
167
function wpinv_request( $vars ) {
168
    global $typenow, $wp_query, $wp_post_statuses;
169
170
    if ( 'wpi_invoice' === $typenow ) {
171
        if ( !isset( $vars['post_status'] ) ) {
172
            $post_statuses = wpinv_get_invoice_statuses();
173
174
            foreach ( $post_statuses as $status => $value ) {
175
                if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
176
                    unset( $post_statuses[ $status ] );
177
                }
178
            }
179
180
            $vars['post_status'] = array_keys( $post_statuses );
181
        }
182
183
        if ( isset( $vars['orderby'] ) ) {
184
            if ( 'amount' == $vars['orderby'] ) {
185
                $vars = array_merge(
186
                    $vars,
187
                    array(
188
                        'meta_key' => '_wpinv_total',
189
                        'orderby'  => 'meta_value_num'
190
                    )
191
                );
192
            } else if ( 'customer' == $vars['orderby'] ) {
193
                $vars = array_merge(
194
                    $vars,
195
                    array(
196
                        'meta_key' => '_wpinv_first_name',
197
                        'orderby'  => 'meta_value'
198
                    )
199
                );
200
            } else if ( 'number' == $vars['orderby'] ) {
201
                $vars = array_merge(
202
                    $vars,
203
                    array(
204
                        'meta_key' => '_wpinv_number',
205
                        'orderby'  => 'meta_value'
206
                    )
207
                );
208
            } else if ( 'payment_date' == $vars['orderby'] ) {
209
                $vars = array_merge(
210
                    $vars,
211
                    array(
212
                        'meta_key' => '_wpinv_completed_date',
213
                        'orderby'  => 'meta_value'
214
                    )
215
                );
216
            }
217
        }
218
    } else if ( 'wpi_discount' == $typenow ) {
219
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
220
        // Filter vat rule type
221
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
222
            $meta_query[] = array(
223
                    'key'   => '_wpi_discount_type',
224
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
225
                    'compare' => '='
226
                );
227
        }
228
229
        if ( !empty( $meta_query ) ) {
230
            $vars['meta_query'] = $meta_query;
231
        }
232
    }
233
234
    return $vars;
235
}
236
add_filter( 'request', 'wpinv_request' );
237
238
function wpinv_item_type_class( $classes, $class, $post_id ) {
239
    global $pagenow, $typenow;
240
241
    if ( $pagenow == 'edit.php' && $typenow == 'wpi_item' && get_post_type( $post_id ) == $typenow ) {
242
        if ( $type = get_post_meta( $post_id, '_wpinv_type', true ) ) {
243
            $classes[] = 'wpi-type-' . sanitize_html_class( $type );
244
        }
245
246
        if ( !wpinv_item_is_editable( $post_id ) ) {
247
            $classes[] = 'wpi-editable-n';
248
        }
249
    }
250
    return $classes;
251
}
252
add_filter( 'post_class', 'wpinv_item_type_class', 10, 3 );
253
254
function wpinv_check_quick_edit() {
255
    global $pagenow, $current_screen, $wpinv_item_screen;
256
257
    if ( $pagenow == 'edit.php' && !empty( $current_screen->post_type ) ) {
258
        if ( empty( $wpinv_item_screen ) ) {
259
            if ( $current_screen->post_type == 'wpi_item' ) {
260
                $wpinv_item_screen = 'y';
261
            } else {
262
                $wpinv_item_screen = 'n';
263
            }
264
        }
265
266
        if ( $wpinv_item_screen == 'y' && $pagenow == 'edit.php' ) {
267
            add_filter( 'post_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
268
            add_filter( 'page_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
269
        }
270
    }
271
}
272
add_action( 'admin_head', 'wpinv_check_quick_edit', 10 );
273
274
function wpinv_item_disable_quick_edit( $actions = array(), $row = null ) {
275
    if ( isset( $actions['inline hide-if-no-js'] ) ) {
276
        unset( $actions['inline hide-if-no-js'] );
277
    }
278
279
    if ( !empty( $row->post_type ) && $row->post_type == 'wpi_item' && !wpinv_item_is_editable( $row ) ) {
280
        if ( isset( $actions['trash'] ) ) {
281
            unset( $actions['trash'] );
282
        }
283
        if ( isset( $actions['delete'] ) ) {
284
            unset( $actions['delete'] );
285
        }
286
    }
287
288
    return $actions;
289
}
290
291
/**
292
 * Create a page and store the ID in an option.
293
 *
294
 * @param mixed $slug Slug for the new page
295
 * @param string $option Option name to store the page's ID
296
 * @param string $page_title (default: '') Title for the new page
297
 * @param string $page_content (default: '') Content for the new page
298
 * @param int $post_parent (default: 0) Parent for the new page
299
 * @return int page ID
300
 */
301
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
302
    global $wpdb;
303
304
    $option_value = wpinv_get_option( $option );
305
306
    if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) {
0 ignored issues
show
Bug introduced by
It seems like $option_value can also be of type false; however, parameter $post of get_post() does only seem to accept WP_Post|integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

306
    if ( $option_value > 0 && ( $page_object = get_post( /** @scrutinizer ignore-type */ $option_value ) ) ) {
Loading history...
307
        if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
308
            // Valid page is already in place
309
            return $page_object->ID;
310
        }
311
    }
312
313
    if(!empty($post_parent)){
314
        $page = get_page_by_path($post_parent);
315
        if ($page) {
316
            $post_parent = $page->ID;
317
        } else {
318
            $post_parent = '';
319
        }
320
    }
321
322
    if ( strlen( $page_content ) > 0 ) {
323
        // Search for an existing page with the specified page content (typically a shortcode)
324
        $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
325
    } else {
326
        // Search for an existing page with the specified page slug
327
        $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' )  AND post_name = %s LIMIT 1;", $slug ) );
328
    }
329
330
    $valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content );
331
332
    if ( $valid_page_found ) {
333
        if ( $option ) {
334
            wpinv_update_option( $option, $valid_page_found );
335
        }
336
        return $valid_page_found;
337
    }
338
339
    // Search for a matching valid trashed page
340
    if ( strlen( $page_content ) > 0 ) {
341
        // Search for an existing page with the specified page content (typically a shortcode)
342
        $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
343
    } else {
344
        // Search for an existing page with the specified page slug
345
        $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
346
    }
347
348
    if ( $trashed_page_found ) {
349
        $page_id   = $trashed_page_found;
350
        $page_data = array(
351
            'ID'             => $page_id,
352
            'post_status'    => 'publish',
353
            'post_parent'    => $post_parent,
354
        );
355
        wp_update_post( $page_data );
356
    } else {
357
        $page_data = array(
358
            'post_status'    => 'publish',
359
            'post_type'      => 'page',
360
            'post_author'    => 1,
361
            'post_name'      => $slug,
362
            'post_title'     => $page_title,
363
            'post_content'   => $page_content,
364
            'post_parent'    => $post_parent,
365
            'comment_status' => 'closed',
366
        );
367
        $page_id = wp_insert_post( $page_data );
368
    }
369
370
    if ( $option ) {
371
        wpinv_update_option( $option, (int)$page_id );
372
    }
373
374
    return $page_id;
375
}