Passed
Push — master ( 919089...f59267 )
by Stiofan
41s queued 11s
created

admin-pages.php ➔ wpinv_add_options_link()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
add_filter( 'manage_wpi_discount_posts_columns', 'wpinv_discount_columns' );
8
function wpinv_discount_columns( $existing_columns ) {
9
    $columns                = array();
10
    $columns['cb']          = $existing_columns['cb'];
11
    $columns['name']        = __( 'Name', 'invoicing' );
12
    $columns['code']        = __( 'Code', 'invoicing' );
13
    $columns['amount']      = __( 'Amount', 'invoicing' );
14
    $columns['usage']       = __( 'Usage / Limit', 'invoicing' );
15
    $columns['start_date']  = __( 'Start Date', 'invoicing' );
16
    $columns['expiry_date'] = __( 'Expiry Date', 'invoicing' );
17
    $columns['status']      = __( 'Status', 'invoicing' );
18
19
    return $columns;
20
}
21
22
add_action( 'manage_wpi_discount_posts_custom_column', 'wpinv_discount_custom_column' );
23
function wpinv_discount_custom_column( $column ) {
24
    global $post;
25
    
26
    $discount = $post;
27
28
    switch ( $column ) {
29
        case 'name' :
30
            echo get_the_title( $discount->ID );
31
        break;
32
        case 'code' :
33
            echo wpinv_get_discount_code( $discount->ID );
34
        break;
35
        case 'amount' :
36
            echo wpinv_format_discount_rate( wpinv_get_discount_type( $discount->ID ), wpinv_get_discount_amount( $discount->ID ) );
37
        break;
38
        case 'usage_limit' :
39
            echo wpinv_get_discount_uses( $discount->ID );
40
        break;
41
        case 'usage' :
42
            $usage = wpinv_get_discount_uses( $discount->ID ) . ' / ';
43
            if ( wpinv_get_discount_max_uses( $discount->ID ) ) {
44
                $usage .= wpinv_get_discount_max_uses( $discount->ID );
45
            } else {
46
                $usage .= ' &infin;';
47
            }
48
            
49
            echo $usage;
50
        break;
51 View Code Duplication
        case 'start_date' :
52
            if ( $start_date = wpinv_get_discount_start_date( $discount->ID ) ) {
53
                $value = date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $start_date ) );
54
            } else {
55
                $value = '-';
56
            }
57
                
58
            echo $value;
59
        break;
60 View Code Duplication
        case 'expiry_date' :
61
            if ( $expiration = wpinv_get_discount_expiration( $discount->ID ) ) {
62
                $value = date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $expiration ) );
63
            } else {
64
                $value = __( 'Never', 'invoicing' );
65
            }
66
                
67
            echo $value;
68
        break;
69
        break;
0 ignored issues
show
Unused Code introduced by
break; 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
        case 'description' :
71
            echo wp_kses_post( $post->post_excerpt );
72
        break;
73
        case 'status' :
74
            $status = wpinv_is_discount_expired( $discount->ID ) ? 'expired' : $discount->post_status;
75
            
76
            echo wpinv_discount_status( $status );
77
        break;
78
    }
79
}
80
81
add_filter( 'post_row_actions', 'wpinv_post_row_actions', 9999, 2 );
82
function wpinv_post_row_actions( $actions, $post ) {
83
    $post_type = !empty( $post->post_type ) ? $post->post_type : '';
84
    
85
    if ( $post_type == 'wpi_invoice' ) {
86
        $actions = array();
87
    }
88
    
89
    if ( $post_type == 'wpi_discount' ) {
90
        $actions = wpinv_discount_row_actions( $post, $actions );
91
    }
92
    
93
    return $actions;
94
}
95
96
function wpinv_discount_row_actions( $discount, $row_actions ) {
0 ignored issues
show
Unused Code introduced by
The parameter $row_actions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97
    $row_actions  = array();
98
    $edit_link = get_edit_post_link( $discount->ID );
99
    $row_actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'invoicing' ) . '</a>';
100
101
    if( in_array( strtolower( $discount->post_status ),  array(  'publish' ) ) ) {
102
        $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>';
103
    } elseif( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
104
        $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>';
105
    }
106
107
    if ( wpinv_get_discount_uses( $discount->ID ) > 0 ) {
108
        if ( isset( $row_actions['delete'] ) ) {
109
            unset( $row_actions['delete'] ); // Don't delete used discounts.
110
        }
111
    } else {
112
        $row_actions['delete'] = '<a href="' . esc_url( wp_nonce_url( add_query_arg( array( 'wpi_action' => 'delete_discount', 'discount' => $discount->ID ) ), 'wpinv_discount_nonce' ) ) . '">' . __( 'Delete', 'invoicing' ) . '</a>';
113
    }
114
    
115
116
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
117
118
    return $row_actions;
119
}
120
121
add_filter( 'list_table_primary_column', 'wpinv_table_primary_column', 10, 2 );
122
function wpinv_table_primary_column( $default, $screen_id ) {
123
    if ( 'edit-wpi_invoice' === $screen_id ) {
124
        return 'name';
125
    }
126
    
127
    return $default;
128
}
129
130
function wpinv_discount_bulk_actions( $actions, $display = false ) {    
0 ignored issues
show
Unused Code introduced by
The parameter $actions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    if ( !$display ) {
132
        return array();
133
    }
134
    
135
    $actions = array(
136
        'activate'   => __( 'Activate', 'invoicing' ),
137
        'deactivate' => __( 'Deactivate', 'invoicing' ),
138
        'delete'     => __( 'Delete', 'invoicing' ),
139
    );
140
    $two = '';
141
    $which = 'top';
142
    echo '</div><div class="alignleft actions bulkactions">';
143
    echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
144
    echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">";
145
    echo '<option value="-1">' . __( 'Bulk Actions' ) . "</option>";
146
147
    foreach ( $actions as $name => $title ) {
148
        $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
149
150
        echo "" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>";
151
    }
152
    echo "</select>";
153
154
    submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
155
    
156
    echo '</div><div class="alignleft actions">';
157
}
158
add_filter( 'bulk_actions-edit-wpi_discount', 'wpinv_discount_bulk_actions', 10 );
159
160
function wpinv_disable_months_dropdown( $disable, $post_type ) {
161
    if ( $post_type == 'wpi_discount' ) {
162
        $disable = true;
163
    }
164
    
165
    return $disable;
166
}
167
add_filter( 'disable_months_dropdown', 'wpinv_disable_months_dropdown', 10, 2 );
168
169
function wpinv_restrict_manage_posts() {
170
    global $typenow;
171
172
    if( 'wpi_discount' == $typenow ) {
173
        wpinv_discount_filters();
174
    }
175
}
176
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
177
178
function wpinv_discount_filters() {
179
    echo wpinv_discount_bulk_actions( array(), true );
180
    
181
    ?>
182
    <select name="discount_type" id="dropdown_wpinv_discount_type">
183
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
184
        <?php
185
            $types = wpinv_get_discount_types();
186
187
            foreach ( $types as $name => $type ) {
188
                echo '<option value="' . esc_attr( $name ) . '"';
189
190
                if ( isset( $_GET['discount_type'] ) )
191
                    selected( $name, $_GET['discount_type'] );
192
193
                echo '>' . esc_html__( $type, 'invoicing' ) . '</option>';
194
            }
195
        ?>
196
    </select>
197
    <?php
198
}
199
200
function wpinv_request( $vars ) {
201
    global $typenow, $wp_query, $wp_post_statuses;
202
203
    if ( 'wpi_invoice' === $typenow ) {
204
        if ( !isset( $vars['post_status'] ) ) {
205
            $post_statuses = wpinv_get_invoice_statuses();
206
207
            foreach ( $post_statuses as $status => $value ) {
208
                if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
209
                    unset( $post_statuses[ $status ] );
210
                }
211
            }
212
213
            $vars['post_status'] = array_keys( $post_statuses );
214
        }
215
        
216
        if ( isset( $vars['orderby'] ) ) {
217
            if ( 'amount' == $vars['orderby'] ) {
218
                $vars = array_merge(
219
                    $vars,
220
                    array(
221
                        'meta_key' => '_wpinv_total',
222
                        'orderby'  => 'meta_value_num'
223
                    )
224
                );
225
            } else if ( 'customer' == $vars['orderby'] ) {
226
                $vars = array_merge(
227
                    $vars,
228
                    array(
229
                        'meta_key' => '_wpinv_first_name',
230
                        'orderby'  => 'meta_value'
231
                    )
232
                );
233
            } else if ( 'number' == $vars['orderby'] ) {
234
                $vars = array_merge(
235
                    $vars,
236
                    array(
237
                        'meta_key' => '_wpinv_number',
238
                        'orderby'  => 'meta_value'
239
                    )
240
                );
241
            } else if ( 'payment_date' == $vars['orderby'] ) {
242
                $vars = array_merge(
243
                    $vars,
244
                    array(
245
                        'meta_key' => '_wpinv_completed_date',
246
                        'orderby'  => 'meta_value'
247
                    )
248
                );
249
            }
250
        }
251
    } else if ( 'wpi_item' == $typenow ) {
252
        // Check if 'orderby' is set to "price"
253
        if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] ) {
254
            $vars = array_merge(
255
                $vars,
256
                array(
257
                    'meta_key' => '_wpinv_price',
258
                    'orderby'  => 'meta_value_num'
259
                )
260
            );
261
        }
262
263
        // Check if "orderby" is set to "vat_rule"
264 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'vat_rule' == $vars['orderby'] ) {
265
            $vars = array_merge(
266
                $vars,
267
                array(
268
                    'meta_key' => '_wpinv_vat_rule',
269
                    'orderby'  => 'meta_value'
270
                )
271
            );
272
        }
273
274
        // Check if "orderby" is set to "vat_class"
275 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'vat_class' == $vars['orderby'] ) {
276
            $vars = array_merge(
277
                $vars,
278
                array(
279
                    'meta_key' => '_wpinv_vat_class',
280
                    'orderby'  => 'meta_value'
281
                )
282
            );
283
        }
284
        
285
        // Check if "orderby" is set to "type"
286 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'type' == $vars['orderby'] ) {
287
            $vars = array_merge(
288
                $vars,
289
                array(
290
                    'meta_key' => '_wpinv_type',
291
                    'orderby'  => 'meta_value'
292
                )
293
            );
294
        }
295
        
296
        // Check if "orderby" is set to "recurring"
297 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'recurring' == $vars['orderby'] ) {
298
            $vars = array_merge(
299
                $vars,
300
                array(
301
                    'meta_key' => '_wpinv_is_recurring',
302
                    'orderby'  => 'meta_value'
303
                )
304
            );
305
        }
306
307
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
308
        // Filter vat rule type
309 View Code Duplication
        if ( isset( $_GET['vat_rule'] ) && $_GET['vat_rule'] !== '' ) {
310
            $meta_query[] = array(
311
                    'key'   => '_wpinv_vat_rule',
312
                    'value' => sanitize_text_field( $_GET['vat_rule'] ),
313
                    'compare' => '='
314
                );
315
        }
316
        
317
        // Filter vat class
318 View Code Duplication
        if ( isset( $_GET['vat_class'] ) && $_GET['vat_class'] !== '' ) {
319
            $meta_query[] = array(
320
                    'key'   => '_wpinv_vat_class',
321
                    'value' => sanitize_text_field( $_GET['vat_class'] ),
322
                    'compare' => '='
323
                );
324
        }
325
        
326
        // Filter item type
327 View Code Duplication
        if ( isset( $_GET['type'] ) && $_GET['type'] !== '' ) {
328
            $meta_query[] = array(
329
                    'key'   => '_wpinv_type',
330
                    'value' => sanitize_text_field( $_GET['type'] ),
331
                    'compare' => '='
332
                );
333
        }
334
        
335
        if ( !empty( $meta_query ) ) {
336
            $vars['meta_query'] = $meta_query;
337
        }
338
    } else if ( 'wpi_discount' == $typenow ) {
339
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
340
        // Filter vat rule type
341 View Code Duplication
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
342
            $meta_query[] = array(
343
                    'key'   => '_wpi_discount_type',
344
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
345
                    'compare' => '='
346
                );
347
        }
348
        
349
        if ( !empty( $meta_query ) ) {
350
            $vars['meta_query'] = $meta_query;
351
        }
352
    }
353
354
    return $vars;
355
}
356
add_filter( 'request', 'wpinv_request' );
357
358
function wpinv_item_type_class( $classes, $class, $post_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
359
    global $pagenow, $typenow;
360
361
    if ( $pagenow == 'edit.php' && $typenow == 'wpi_item' && get_post_type( $post_id ) == $typenow ) {
362
        if ( $type = get_post_meta( $post_id, '_wpinv_type', true ) ) {
363
            $classes[] = 'wpi-type-' . sanitize_html_class( $type );
364
        }
365
        
366
        if ( !wpinv_item_is_editable( $post_id ) ) {
367
            $classes[] = 'wpi-editable-n';
368
        }
369
    }
370
    return $classes;
371
}
372
add_filter( 'post_class', 'wpinv_item_type_class', 10, 3 );
373
374
function wpinv_check_quick_edit() {
375
    global $pagenow, $current_screen, $wpinv_item_screen;
376
377
    if ( $pagenow == 'edit.php' && !empty( $current_screen->post_type ) ) {
378
        if ( empty( $wpinv_item_screen ) ) {
379
            if ( $current_screen->post_type == 'wpi_item' ) {
380
                $wpinv_item_screen = 'y';
381
            } else {
382
                $wpinv_item_screen = 'n';
383
            }
384
        }
385
386
        if ( $wpinv_item_screen == 'y' && $pagenow == 'edit.php' ) {
387
            add_filter( 'post_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
388
            add_filter( 'page_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
389
        }
390
    }
391
}
392
add_action( 'admin_head', 'wpinv_check_quick_edit', 10 );
393
394
function wpinv_item_disable_quick_edit( $actions = array(), $row = null ) {
395
    if ( isset( $actions['inline hide-if-no-js'] ) ) {
396
        unset( $actions['inline hide-if-no-js'] );
397
    }
398
    
399
    if ( !empty( $row->post_type ) && $row->post_type == 'wpi_item' && !wpinv_item_is_editable( $row ) ) {
400
        if ( isset( $actions['trash'] ) ) {
401
            unset( $actions['trash'] );
402
        }
403
        if ( isset( $actions['delete'] ) ) {
404
            unset( $actions['delete'] );
405
        }
406
    }
407
408
    return $actions;
409
}
410
411
/**
412
 * Create a page and store the ID in an option.
413
 *
414
 * @param mixed $slug Slug for the new page
415
 * @param string $option Option name to store the page's ID
416
 * @param string $page_title (default: '') Title for the new page
417
 * @param string $page_content (default: '') Content for the new page
418
 * @param int $post_parent (default: 0) Parent for the new page
419
 * @return int page ID
420
 */
421
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
422
    global $wpdb;
423
424
    $option_value = wpinv_get_option( $option );
425
426
    if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) {
427
        if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
428
            // Valid page is already in place
429
            return $page_object->ID;
430
        }
431
    }
432
433
    if(!empty($post_parent)){
434
        $page = get_page_by_path($post_parent);
435
        if ($page) {
436
            $post_parent = $page->ID;
437
        } else {
438
            $post_parent = '';
439
        }
440
    }
441
442 View Code Duplication
    if ( strlen( $page_content ) > 0 ) {
443
        // Search for an existing page with the specified page content (typically a shortcode)
444
        $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}%" ) );
445
    } else {
446
        // Search for an existing page with the specified page slug
447
        $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 ) );
448
    }
449
450
    $valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content );
451
452
    if ( $valid_page_found ) {
453
        if ( $option ) {
454
            wpinv_update_option( $option, $valid_page_found );
455
        }
456
        return $valid_page_found;
457
    }
458
459
    // Search for a matching valid trashed page
460 View Code Duplication
    if ( strlen( $page_content ) > 0 ) {
461
        // Search for an existing page with the specified page content (typically a shortcode)
462
        $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}%" ) );
463
    } else {
464
        // Search for an existing page with the specified page slug
465
        $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 ) );
466
    }
467
468
    if ( $trashed_page_found ) {
469
        $page_id   = $trashed_page_found;
470
        $page_data = array(
471
            'ID'             => $page_id,
472
            'post_status'    => 'publish',
473
            'post_parent'    => $post_parent,
474
        );
475
        wp_update_post( $page_data );
476
    } else {
477
        $page_data = array(
478
            'post_status'    => 'publish',
479
            'post_type'      => 'page',
480
            'post_author'    => 1,
481
            'post_name'      => $slug,
482
            'post_title'     => $page_title,
483
            'post_content'   => $page_content,
484
            'post_parent'    => $post_parent,
485
            'comment_status' => 'closed',
486
        );
487
        $page_id = wp_insert_post( $page_data );
488
    }
489
490
    if ( $option ) {
491
        wpinv_update_option( $option, (int)$page_id );
0 ignored issues
show
Documentation introduced by
(int) $page_id 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...
492
    }
493
494
    return $page_id;
495
}