Passed
Pull Request — master (#57)
by Kiran
04:35
created

admin-pages.php ➔ wpinv_item_disable_quick_edit()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 10
nop 2
dl 0
loc 16
rs 8.2222
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_action( 'admin_menu', 'wpinv_add_options_link', 10 );
8
function wpinv_add_options_link() {
9
    global $menu;
10
11
    if ( current_user_can( 'manage_options' ) ) {
12
        $menu[] = array( '', 'read', 'separator-wpinv', '', 'wp-menu-separator wpinv' );
13
    }
14
    
15
    $wpi_invoice            = get_post_type_object( 'wpi_invoice' );
16
17
    add_menu_page( __( 'Invoicing', 'invoicing' ), __( 'Invoicing', 'invoicing' ), 'manage_options', 'wpinv', null, $wpi_invoice->menu_icon, '54.123460' );
18
    
19
    $wpi_settings_page   = add_submenu_page( 'wpinv', __( 'Invoice Settings', 'invoicing' ), __( 'Settings', 'invoicing' ), 'manage_options', 'wpinv-settings', 'wpinv_options_page' );
0 ignored issues
show
Unused Code introduced by
$wpi_settings_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...
20
}
21
22
add_action( 'admin_menu', 'wpinv_remove_admin_submenus', 999 );
23
function wpinv_remove_admin_submenus() {
24
    remove_submenu_page( 'edit.php?post_type=wpi_invoice', 'post-new.php?post_type=wpi_invoice' );
25
}
26
27
add_filter( 'manage_wpi_discount_posts_columns', 'wpinv_discount_columns' );
28
function wpinv_discount_columns( $existing_columns ) {
29
    $columns                = array();
30
    $columns['cb']          = $existing_columns['cb'];
31
    $columns['name']        = __( 'Name', 'invoicing' );
32
    $columns['code']        = __( 'Code', 'invoicing' );
33
    $columns['amount']      = __( 'Amount', 'invoicing' );
34
    $columns['usage']       = __( 'Usage / Limit', 'invoicing' );
35
    $columns['expiry_date'] = __( 'Expiry Date', 'invoicing' );
36
    $columns['status']      = __( 'Status', 'invoicing' );
37
38
    return $columns;
39
}
40
41
add_action( 'manage_wpi_discount_posts_custom_column', 'wpinv_discount_custom_column' );
42
function wpinv_discount_custom_column( $column ) {
43
    global $post;
44
    
45
    $discount = $post;
46
47
    switch ( $column ) {
48
        case 'name' :
49
            echo get_the_title( $discount->ID );
50
        break;
51
        case 'code' :
52
            echo wpinv_get_discount_code( $discount->ID );
53
        break;
54
        case 'amount' :
55
            echo wpinv_format_discount_rate( wpinv_get_discount_type( $discount->ID ), wpinv_get_discount_amount( $discount->ID ) );
56
        break;
57
        case 'usage_limit' :
58
            echo wpinv_get_discount_uses( $discount->ID );
59
        break;
60
        case 'usage' :
61
            $usage = wpinv_get_discount_uses( $discount->ID ) . ' / ';
62
            if ( wpinv_get_discount_max_uses( $discount->ID ) ) {
63
                $usage .= wpinv_get_discount_max_uses( $discount->ID );
64
            } else {
65
                $usage .= ' &infin;';
66
            }
67
            
68
            echo $usage;
69
        break;
70
        case 'expiry_date' :
71
            if ( wpinv_get_discount_expiration( $discount->ID ) ) {
72
                $expiration = date_i18n( get_option( 'date_format' ), strtotime( wpinv_get_discount_expiration( $discount->ID ) ) );
73
            } else {
74
                $expiration = __( 'Never', 'invoicing' );
75
            }
76
                
77
            echo $expiration;
78
        break;
79
        case 'description' :
80
            echo wp_kses_post( $post->post_excerpt );
81
        break;
82
        case 'status' :
83
            $status = wpinv_is_discount_expired( $discount->ID ) ? 'expired' : $discount->post_status;
84
            
85
            echo wpinv_discount_status( $status );
86
        break;
87
    }
88
}
89
90
add_filter( 'post_row_actions', 'wpinv_post_row_actions', 9999, 2 );
91
function wpinv_post_row_actions( $actions, $post ) {
92
    $post_type = !empty( $post->post_type ) ? $post->post_type : '';
93
    
94
    if ( $post_type == 'wpi_invoice' ) {
95
        $actions = array();
96
    }
97
    
98
    if ( $post_type == 'wpi_discount' ) {
99
        $actions = wpinv_discount_row_actions( $post, $actions );
100
    }
101
    
102
    return $actions;
103
}
104
105
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...
106
    $row_actions  = array();
107
    $edit_link = get_edit_post_link( $discount->ID );
108
    $row_actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'invoicing' ) . '</a>';
109
110
    if( in_array( strtolower( $discount->post_status ),  array(  'publish' ) ) ) {
111
        $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>';
112
    } elseif( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
113
        $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>';
114
    }
115
116
    if ( wpinv_get_discount_uses( $discount->ID ) > 0 ) {
117
        if ( isset( $row_actions['delete'] ) ) {
118
            unset( $row_actions['delete'] ); // Don't delete used discounts.
119
        }
120
    } else {
121
        $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>';
122
    }
123
    
124
125
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
126
127
    return $row_actions;
128
}
129
130
add_filter( 'list_table_primary_column', 'wpinv_table_primary_column', 10, 2 );
131
function wpinv_table_primary_column( $default, $screen_id ) {
132
    if ( 'edit-wpi_invoice' === $screen_id ) {
133
        return 'name';
134
    }
135
    
136
    return $default;
137
}
138
139
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...
140
    if ( !$display ) {
141
        return array();
142
    }
143
    
144
    $actions = array(
145
        'activate'   => __( 'Activate', 'invoicing' ),
146
        'deactivate' => __( 'Deactivate', 'invoicing' ),
147
        'delete'     => __( 'Delete', 'invoicing' ),
148
    );
149
    $two = '';
150
    $which = 'top';
151
    echo '</div><div class="alignleft actions bulkactions">';
152
    echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
153
    echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">";
154
    echo '<option value="-1">' . __( 'Bulk Actions' ) . "</option>";
155
156
    foreach ( $actions as $name => $title ) {
157
        $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
158
159
        echo "" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>";
160
    }
161
    echo "</select>";
162
163
    submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
164
    
165
    echo '</div><div class="alignleft actions">';
166
}
167
add_filter( 'bulk_actions-edit-wpi_discount', 'wpinv_discount_bulk_actions', 10 );
168
169
function wpinv_disable_months_dropdown( $disable, $post_type ) {
170
    if ( $post_type == 'wpi_discount' ) {
171
        $disable = true;
172
    }
173
    
174
    return $disable;
175
}
176
add_filter( 'disable_months_dropdown', 'wpinv_disable_months_dropdown', 10, 2 );
177
178
function wpinv_restrict_manage_posts() {
179
    global $typenow;
180
181
    if( 'wpi_discount' == $typenow ) {
182
        wpinv_discount_filters();
183
    }
184
}
185
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
186
187
function wpinv_discount_filters() {
188
    echo wpinv_discount_bulk_actions( array(), true );
189
    
190
    ?>
191
    <select name="discount_type" id="dropdown_wpinv_discount_type">
192
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
193
        <?php
194
            $types = wpinv_get_discount_types();
195
196
            foreach ( $types as $name => $type ) {
197
                echo '<option value="' . esc_attr( $name ) . '"';
198
199
                if ( isset( $_GET['discount_type'] ) )
200
                    selected( $name, $_GET['discount_type'] );
201
202
                echo '>' . esc_html__( $type, 'invoicing' ) . '</option>';
203
            }
204
        ?>
205
    </select>
206
    <?php
207
}
208
209
function wpinv_request( $vars ) {
210
    global $typenow, $wp_query, $wp_post_statuses;
211
212
    if ( 'wpi_invoice' === $typenow or 'wpi_quote' === $typenow ) {
213
        if ( !isset( $vars['post_status'] ) ) {
214
            $post_statuses = wpinv_get_invoice_statuses();
215
216
            foreach ( $post_statuses as $status => $value ) {
217
                if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
218
                    unset( $post_statuses[ $status ] );
219
                }
220
            }
221
222
            $vars['post_status'] = array_keys( $post_statuses );
223
        }
224
        
225
        if ( isset( $vars['orderby'] ) ) {
226
            if ( 'amount' == $vars['orderby'] ) {
227
                $vars = array_merge(
228
                    $vars,
229
                    array(
230
                        'meta_key' => '_wpinv_total',
231
                        'orderby'  => 'meta_value_num'
232
                    )
233
                );
234
            } else if ( 'customer' == $vars['orderby'] ) {
235
                $vars = array_merge(
236
                    $vars,
237
                    array(
238
                        'meta_key' => '_wpinv_first_name',
239
                        'orderby'  => 'meta_value'
240
                    )
241
                );
242
            } else if ( 'number' == $vars['orderby'] ) {
243
                $vars = array_merge(
244
                    $vars,
245
                    array(
246
                        'meta_key' => '_wpinv_number',
247
                        'orderby'  => 'meta_value'
248
                    )
249
                );
250
            }
251
        }
252
    } else if ( 'wpi_item' == $typenow ) {
253
        // Check if 'orderby' is set to "price"
254
        if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] ) {
255
            $vars = array_merge(
256
                $vars,
257
                array(
258
                    'meta_key' => '_wpinv_price',
259
                    'orderby'  => 'meta_value_num'
260
                )
261
            );
262
        }
263
264
        // Check if "orderby" is set to "vat_rule"
265 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'vat_rule' == $vars['orderby'] ) {
266
            $vars = array_merge(
267
                $vars,
268
                array(
269
                    'meta_key' => '_wpinv_vat_rule',
270
                    'orderby'  => 'meta_value'
271
                )
272
            );
273
        }
274
275
        // Check if "orderby" is set to "vat_class"
276 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'vat_class' == $vars['orderby'] ) {
277
            $vars = array_merge(
278
                $vars,
279
                array(
280
                    'meta_key' => '_wpinv_vat_class',
281
                    'orderby'  => 'meta_value'
282
                )
283
            );
284
        }
285
        
286
        // Check if "orderby" is set to "type"
287 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'type' == $vars['orderby'] ) {
288
            $vars = array_merge(
289
                $vars,
290
                array(
291
                    'meta_key' => '_wpinv_type',
292
                    'orderby'  => 'meta_value'
293
                )
294
            );
295
        }
296
        
297
        // Check if "orderby" is set to "recurring"
298 View Code Duplication
        if ( isset( $vars['orderby'] ) && 'recurring' == $vars['orderby'] ) {
299
            $vars = array_merge(
300
                $vars,
301
                array(
302
                    'meta_key' => '_wpinv_is_recurring',
303
                    'orderby'  => 'meta_value'
304
                )
305
            );
306
        }
307
308
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
309
        // Filter vat rule type
310 View Code Duplication
        if ( isset( $_GET['vat_rule'] ) && $_GET['vat_rule'] !== '' ) {
311
            $meta_query[] = array(
312
                    'key'   => '_wpinv_vat_rule',
313
                    'value' => sanitize_text_field( $_GET['vat_rule'] ),
314
                    'compare' => '='
315
                );
316
        }
317
        
318
        // Filter vat class
319 View Code Duplication
        if ( isset( $_GET['vat_class'] ) && $_GET['vat_class'] !== '' ) {
320
            $meta_query[] = array(
321
                    'key'   => '_wpinv_vat_class',
322
                    'value' => sanitize_text_field( $_GET['vat_class'] ),
323
                    'compare' => '='
324
                );
325
        }
326
        
327
        // Filter item type
328 View Code Duplication
        if ( isset( $_GET['type'] ) && $_GET['type'] !== '' ) {
329
            $meta_query[] = array(
330
                    'key'   => '_wpinv_type',
331
                    'value' => sanitize_text_field( $_GET['type'] ),
332
                    'compare' => '='
333
                );
334
        }
335
        
336
        if ( !empty( $meta_query ) ) {
337
            $vars['meta_query'] = $meta_query;
338
        }
339
    } else if ( 'wpi_discount' == $typenow ) {
340
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
341
        // Filter vat rule type
342 View Code Duplication
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
343
            $meta_query[] = array(
344
                    'key'   => '_wpi_discount_type',
345
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
346
                    'compare' => '='
347
                );
348
        }
349
        
350
        if ( !empty( $meta_query ) ) {
351
            $vars['meta_query'] = $meta_query;
352
        }
353
    }
354
355
    return $vars;
356
}
357
add_filter( 'request', 'wpinv_request' );
358
359
function wpinv_options_page() {
360
    $page       = isset( $_GET['page'] )                ? strtolower( $_GET['page'] )               : false;
361
    
362
    if ( $page !== 'wpinv-settings' ) {
363
        return;
364
    }
365
    
366
    $settings_tabs = wpinv_get_settings_tabs();
367
    $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs;
368
    $active_tab    = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $settings_tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general';
369
    $sections      = wpinv_get_settings_tab_sections( $active_tab );
370
    $key           = 'main';
371
372
    if ( is_array( $sections ) ) {
373
        $key = key( $sections );
374
    }
375
376
    $registered_sections = wpinv_get_settings_tab_sections( $active_tab );
377
    $section             = isset( $_GET['section'] ) && ! empty( $registered_sections ) && array_key_exists( $_GET['section'], $registered_sections ) ? $_GET['section'] : $key;
378
    ob_start();
379
    ?>
380
    <div class="wrap">
381
        <h1 class="nav-tab-wrapper">
382
            <?php
383
            foreach( wpinv_get_settings_tabs() as $tab_id => $tab_name ) {
384
                $tab_url = add_query_arg( array(
385
                    'settings-updated' => false,
386
                    'tab' => $tab_id,
387
                ) );
388
389
                // Remove the section from the tabs so we always end up at the main section
390
                $tab_url = remove_query_arg( 'section', $tab_url );
391
                $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
392
393
                $active = $active_tab == $tab_id ? ' nav-tab-active' : '';
394
395
                echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $tab_name ) . '" class="nav-tab' . $active . '">';
396
                    echo esc_html( $tab_name );
397
                echo '</a>';
398
            }
399
            ?>
400
        </h1>
401
        <?php
402
        $number_of_sections = count( $sections );
403
        $number = 0;
404
        if ( $number_of_sections > 1 ) {
405
            echo '<div><ul class="subsubsub">';
406
            foreach( $sections as $section_id => $section_name ) {
407
                echo '<li>';
408
                $number++;
409
                $tab_url = add_query_arg( array(
410
                    'settings-updated' => false,
411
                    'tab' => $active_tab,
412
                    'section' => $section_id
413
                ) );
414
                $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
415
                $class = '';
416
                if ( $section == $section_id ) {
417
                    $class = 'current';
418
                }
419
                echo '<a class="' . $class . '" href="' . esc_url( $tab_url ) . '">' . $section_name . '</a>';
420
421
                if ( $number != $number_of_sections ) {
422
                    echo ' | ';
423
                }
424
                echo '</li>';
425
            }
426
            echo '</ul></div>';
427
        }
428
        ?>
429
        <div id="tab_container">
430
            <form method="post" action="options.php">
431
                <table class="form-table">
432
                <?php
433
                settings_fields( 'wpinv_settings' );
434
435
                if ( 'main' === $section ) {
436
                    do_action( 'wpinv_settings_tab_top', $active_tab );
437
                }
438
439
                do_action( 'wpinv_settings_tab_top_' . $active_tab . '_' . $section );
440
                do_settings_sections( 'wpinv_settings_' . $active_tab . '_' . $section );
441
                do_action( 'wpinv_settings_tab_bottom_' . $active_tab . '_' . $section  );
442
443
                // For backwards compatibility
444
                if ( 'main' === $section ) {
445
                    do_action( 'wpinv_settings_tab_bottom', $active_tab );
446
                }
447
                ?>
448
                </table>
449
                <?php submit_button(); ?>
450
            </form>
451
        </div><!-- #tab_container-->
452
    </div><!-- .wrap -->
453
    <?php
454
    $content = ob_get_clean(); 
455
    echo $content;
456
}
457
458
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...
459
    global $pagenow, $typenow;
460
461
    if ( $pagenow == 'edit.php' && $typenow == 'wpi_item' && get_post_type( $post_id ) == $typenow ) {
462
        if ( $type = get_post_meta( $post_id, '_wpinv_type', true ) ) {
463
            $classes[] = 'wpi-type-' . sanitize_html_class( $type );
464
        }
465
        
466
        if ( !wpinv_item_is_editable( $post_id ) ) {
467
            $classes[] = 'wpi-editable-n';
468
        }
469
    }
470
    return $classes;
471
}
472
add_filter( 'post_class', 'wpinv_item_type_class', 10, 3 );
473
474
function wpinv_check_quick_edit() {
475
    global $pagenow, $current_screen, $wpinv_item_screen;
476
477
    if ( $pagenow == 'edit.php' && !empty( $current_screen->post_type ) ) {
478
        if ( empty( $wpinv_item_screen ) ) {
479
            if ( $current_screen->post_type == 'wpi_item' ) {
480
                $wpinv_item_screen = 'y';
481
            } else {
482
                $wpinv_item_screen = 'n';
483
            }
484
        }
485
486
        if ( $wpinv_item_screen == 'y' && $pagenow == 'edit.php' ) {
487
            add_filter( 'post_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
488
            add_filter( 'page_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
489
        }
490
    }
491
}
492
add_action( 'admin_head', 'wpinv_check_quick_edit', 10 );
493
494
function wpinv_item_disable_quick_edit( $actions = array(), $row = null ) {
495
    if ( isset( $actions['inline hide-if-no-js'] ) ) {
496
        unset( $actions['inline hide-if-no-js'] );
497
    }
498
    
499
    if ( !empty( $row->post_type ) && $row->post_type == 'wpi_item' && !wpinv_item_is_editable( $row ) ) {
500
        if ( isset( $actions['trash'] ) ) {
501
            unset( $actions['trash'] );
502
        }
503
        if ( isset( $actions['delete'] ) ) {
504
            unset( $actions['delete'] );
505
        }
506
    }
507
508
    return $actions;
509
}