Passed
Pull Request — master (#376)
by Brian
106:18
created

wpinv_discount_custom_column()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 33
rs 8.4444
cc 8
nc 8
nop 1
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_payment_form_posts_columns', 'wpinv_payment_form_columns' );
8
function wpinv_payment_form_columns( $existing_columns ) {
9
    $date = $existing_columns['date'];
10
    unset( $existing_columns['date'] );
11
    $existing_columns['shortcode'] = __( 'Shortcode', 'invoicing' );
12
    $existing_columns['date'] = $date;
13
    return $existing_columns;
14
}
15
16
add_action( 'manage_wpi_payment_form_posts_custom_column', 'wpinv_payment_form_custom_column' );
17
function wpinv_payment_form_custom_column( $column ) {
18
    global $post;
19
20
    if( 'shortcode' == $column ) {
21
        WPInv_Meta_Box_Payment_Form::output_shortcode( $post );
22
    }
23
24
}
25
26
function wpinv_filter_discount_post_state( $post_states, $post ) {
27
28
    if ( 'wpi_discount' == $post->post_type ) {
29
        $discount = new WPInv_Discount( $post );
30
31
        $status = $discount->is_expired() ? 'expired' : $discount->get_status();
32
33
        if ( $status != 'publish' ) {
34
            return array(
35
                'discount_status' => wpinv_discount_status( $status ),
36
            );
37
        }
38
39
        return array();
40
        
41
    }
42
43
    return $post_states;
44
45
}
46
add_filter( 'display_post_states', 'wpinv_filter_discount_post_state', 10, 2 );
47
48
add_filter( 'manage_wpi_discount_posts_columns', 'wpinv_discount_columns' );
49
function wpinv_discount_columns( $columns ) {
50
    
51
    if ( isset( $columns['date'] ) ) {
52
        unset( $columns['date'] );
53
    }
54
55
    if ( isset( $columns['title'] ) ) {
56
        $columns['title'] = __( 'Name', 'invoicing' );
57
    }
58
59
    $columns['code']        = __( 'Code', 'invoicing' );
60
    $columns['amount']      = __( 'Amount', 'invoicing' );
61
    $columns['usage']       = __( 'Usage / Limit', 'invoicing' );
62
    $columns['start_date']  = __( 'Start Date', 'invoicing' );
63
    $columns['expiry_date'] = __( 'Expiry Date', 'invoicing' );
64
65
    return $columns;
66
}
67
68
add_action( 'manage_wpi_discount_posts_custom_column', 'wpinv_discount_custom_column' );
69
function wpinv_discount_custom_column( $column ) {
70
    global $post;
71
72
    $discount = new WPInv_Discount( $post );
73
74
    switch ( $column ) {
75
        case 'code' :
76
            echo $discount->get_code();
77
        break;
78
        case 'amount' :
79
            echo $discount->get_formatted_amount();
80
        break;
81
        case 'usage' :
82
            echo $discount->get_usage();
83
        break;
84
        case 'start_date' :
85
            if ( $discount->has_start_date() ) {
86
                $value = date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $discount->get_start_date() ) );
0 ignored issues
show
Bug introduced by
Are you sure get_option('date_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

86
                $value = date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $discount->get_start_date() ) );
Loading history...
Bug introduced by
Are you sure get_option('time_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

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

86
                $value = date_i18n( get_option( 'date_format' ) . ' @ ' . /** @scrutinizer ignore-type */ get_option( 'time_format' ), strtotime( $discount->get_start_date() ) );
Loading history...
87
            } else {
88
                $value = '&mdash;';
89
            }
90
91
            echo $value;
92
        break;
93
        case 'expiry_date' :
94
            if ( $discount->has_expiration_date() ) {
95
                $value = date_i18n( get_option( 'date_format' ) . ' @ ' . get_option( 'time_format' ), strtotime( $discount->get_expiration_date() ) );
96
            } else {
97
                $value = __( 'Never', 'invoicing' );
98
            }
99
100
            echo $value;
101
        break;
102
    }
103
}
104
105
add_filter( 'post_row_actions', 'wpinv_post_row_actions', 9999, 2 );
106
function wpinv_post_row_actions( $actions, $post ) {
107
    $post_type = !empty( $post->post_type ) ? $post->post_type : '';
108
109
    if ( $post_type == 'wpi_invoice' ) {
110
        $actions = array();
111
    }
112
113
    if ( $post_type == 'wpi_discount' ) {
114
        $actions = wpinv_discount_row_actions( $post, $actions );
115
    }
116
117
    return $actions;
118
}
119
120
function wpinv_discount_row_actions( $discount, $row_actions ) {
121
    $row_actions  = array();
122
    $edit_link = get_edit_post_link( $discount->ID );
123
    $row_actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'invoicing' ) . '</a>';
124
125
    if( in_array( strtolower( $discount->post_status ),  array(  'publish' ) ) ) {
126
        $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>';
127
    } elseif( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
128
        $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>';
129
    }
130
131
    $delete_url = esc_url(
132
        wp_nonce_url(
133
            add_query_arg(
134
                array(
135
                    'wpi_action' => 'delete_discount',
136
                    'discount' => $discount->ID
137
                )
138
            ),
139
            'wpinv_discount_nonce'
140
        )
141
    );
142
    $row_actions['delete'] = '<a href="' . $delete_url . '">' . __( 'Delete', 'invoicing' ) . '</a>';
143
144
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
145
146
    return $row_actions;
147
}
148
149
add_filter( 'list_table_primary_column', 'wpinv_table_primary_column', 10, 2 );
150
function wpinv_table_primary_column( $default, $screen_id ) {
151
    if ( 'edit-wpi_invoice' === $screen_id ) {
152
        return 'name';
153
    }
154
155
    return $default;
156
}
157
158
function wpinv_discount_bulk_actions( $actions, $display = false ) {
159
    if ( !$display ) {
160
        return array();
161
    }
162
163
    $actions = array(
164
        'activate'   => __( 'Activate', 'invoicing' ),
165
        'deactivate' => __( 'Deactivate', 'invoicing' ),
166
        'delete'     => __( 'Delete', 'invoicing' ),
167
    );
168
    $two = '';
169
    $which = 'top';
170
    echo '</div><div class="alignleft actions bulkactions">';
171
    echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
172
    echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">";
173
    echo '<option value="-1">' . __( 'Bulk Actions' ) . "</option>";
174
175
    foreach ( $actions as $name => $title ) {
176
        $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
177
178
        echo "" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>";
179
    }
180
    echo "</select>";
181
182
    submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
183
184
    echo '</div><div class="alignleft actions">';
185
}
186
add_filter( 'bulk_actions-edit-wpi_discount', 'wpinv_discount_bulk_actions', 10 );
187
188
function wpinv_disable_months_dropdown( $disable, $post_type ) {
189
    if ( $post_type == 'wpi_discount' ) {
190
        $disable = true;
191
    }
192
193
    return $disable;
194
}
195
add_filter( 'disable_months_dropdown', 'wpinv_disable_months_dropdown', 10, 2 );
196
197
function wpinv_restrict_manage_posts() {
198
    global $typenow;
199
200
    if( 'wpi_discount' == $typenow ) {
201
        wpinv_discount_filters();
202
    }
203
}
204
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
205
206
function wpinv_discount_filters() {
207
    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...
208
209
    ?>
210
    <select name="discount_type" id="dropdown_wpinv_discount_type">
211
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
212
        <?php
213
            $types = wpinv_get_discount_types();
214
215
            foreach ( $types as $name => $type ) {
216
                echo '<option value="' . esc_attr( $name ) . '"';
217
218
                if ( isset( $_GET['discount_type'] ) )
219
                    selected( $name, $_GET['discount_type'] );
220
221
                echo '>' . esc_html__( $type, 'invoicing' ) . '</option>';
222
            }
223
        ?>
224
    </select>
225
    <?php
226
}
227
228
function wpinv_request( $vars ) {
229
    global $typenow, $wp_query, $wp_post_statuses;
230
231
    if ( 'wpi_invoice' === $typenow ) {
232
        if ( !isset( $vars['post_status'] ) ) {
233
            $post_statuses = wpinv_get_invoice_statuses();
234
235
            foreach ( $post_statuses as $status => $value ) {
236
                if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
237
                    unset( $post_statuses[ $status ] );
238
                }
239
            }
240
241
            $vars['post_status'] = array_keys( $post_statuses );
242
        }
243
244
        if ( isset( $vars['orderby'] ) ) {
245
            if ( 'amount' == $vars['orderby'] ) {
246
                $vars = array_merge(
247
                    $vars,
248
                    array(
249
                        'meta_key' => '_wpinv_total',
250
                        'orderby'  => 'meta_value_num'
251
                    )
252
                );
253
            } else if ( 'customer' == $vars['orderby'] ) {
254
                $vars = array_merge(
255
                    $vars,
256
                    array(
257
                        'meta_key' => '_wpinv_first_name',
258
                        'orderby'  => 'meta_value'
259
                    )
260
                );
261
            } else if ( 'number' == $vars['orderby'] ) {
262
                $vars = array_merge(
263
                    $vars,
264
                    array(
265
                        'meta_key' => '_wpinv_number',
266
                        'orderby'  => 'meta_value'
267
                    )
268
                );
269
            } else if ( 'payment_date' == $vars['orderby'] ) {
270
                $vars = array_merge(
271
                    $vars,
272
                    array(
273
                        'meta_key' => '_wpinv_completed_date',
274
                        'orderby'  => 'meta_value'
275
                    )
276
                );
277
            }
278
        }
279
    } else if ( 'wpi_item' == $typenow ) {
280
        // Check if 'orderby' is set to "price"
281
        if ( isset( $vars['orderby'] ) && 'price' == $vars['orderby'] ) {
282
            $vars = array_merge(
283
                $vars,
284
                array(
285
                    'meta_key' => '_wpinv_price',
286
                    'orderby'  => 'meta_value_num'
287
                )
288
            );
289
        }
290
291
        // Check if "orderby" is set to "vat_rule"
292
        if ( isset( $vars['orderby'] ) && 'vat_rule' == $vars['orderby'] ) {
293
            $vars = array_merge(
294
                $vars,
295
                array(
296
                    'meta_key' => '_wpinv_vat_rule',
297
                    'orderby'  => 'meta_value'
298
                )
299
            );
300
        }
301
302
        // Check if "orderby" is set to "vat_class"
303
        if ( isset( $vars['orderby'] ) && 'vat_class' == $vars['orderby'] ) {
304
            $vars = array_merge(
305
                $vars,
306
                array(
307
                    'meta_key' => '_wpinv_vat_class',
308
                    'orderby'  => 'meta_value'
309
                )
310
            );
311
        }
312
313
        // Check if "orderby" is set to "type"
314
        if ( isset( $vars['orderby'] ) && 'type' == $vars['orderby'] ) {
315
            $vars = array_merge(
316
                $vars,
317
                array(
318
                    'meta_key' => '_wpinv_type',
319
                    'orderby'  => 'meta_value'
320
                )
321
            );
322
        }
323
324
        // Check if "orderby" is set to "recurring"
325
        if ( isset( $vars['orderby'] ) && 'recurring' == $vars['orderby'] ) {
326
            $vars = array_merge(
327
                $vars,
328
                array(
329
                    'meta_key' => '_wpinv_is_recurring',
330
                    'orderby'  => 'meta_value'
331
                )
332
            );
333
        }
334
335
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
336
        // Filter vat rule type
337
        if ( isset( $_GET['vat_rule'] ) && $_GET['vat_rule'] !== '' ) {
338
            $meta_query[] = array(
339
                    'key'   => '_wpinv_vat_rule',
340
                    'value' => sanitize_text_field( $_GET['vat_rule'] ),
341
                    'compare' => '='
342
                );
343
        }
344
345
        // Filter vat class
346
        if ( isset( $_GET['vat_class'] ) && $_GET['vat_class'] !== '' ) {
347
            $meta_query[] = array(
348
                    'key'   => '_wpinv_vat_class',
349
                    'value' => sanitize_text_field( $_GET['vat_class'] ),
350
                    'compare' => '='
351
                );
352
        }
353
354
        // Filter item type
355
        if ( isset( $_GET['type'] ) && $_GET['type'] !== '' ) {
356
            $meta_query[] = array(
357
                    'key'   => '_wpinv_type',
358
                    'value' => sanitize_text_field( $_GET['type'] ),
359
                    'compare' => '='
360
                );
361
        }
362
363
        if ( !empty( $meta_query ) ) {
364
            $vars['meta_query'] = $meta_query;
365
        }
366
    } else if ( 'wpi_discount' == $typenow ) {
367
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
368
        // Filter vat rule type
369
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
370
            $meta_query[] = array(
371
                    'key'   => '_wpi_discount_type',
372
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
373
                    'compare' => '='
374
                );
375
        }
376
377
        if ( !empty( $meta_query ) ) {
378
            $vars['meta_query'] = $meta_query;
379
        }
380
    }
381
382
    return $vars;
383
}
384
add_filter( 'request', 'wpinv_request' );
385
386
function wpinv_item_type_class( $classes, $class, $post_id ) {
387
    global $pagenow, $typenow;
388
389
    if ( $pagenow == 'edit.php' && $typenow == 'wpi_item' && get_post_type( $post_id ) == $typenow ) {
390
        if ( $type = get_post_meta( $post_id, '_wpinv_type', true ) ) {
391
            $classes[] = 'wpi-type-' . sanitize_html_class( $type );
392
        }
393
394
        if ( !wpinv_item_is_editable( $post_id ) ) {
395
            $classes[] = 'wpi-editable-n';
396
        }
397
    }
398
    return $classes;
399
}
400
add_filter( 'post_class', 'wpinv_item_type_class', 10, 3 );
401
402
function wpinv_check_quick_edit() {
403
    global $pagenow, $current_screen, $wpinv_item_screen;
404
405
    if ( $pagenow == 'edit.php' && !empty( $current_screen->post_type ) ) {
406
        if ( empty( $wpinv_item_screen ) ) {
407
            if ( $current_screen->post_type == 'wpi_item' ) {
408
                $wpinv_item_screen = 'y';
409
            } else {
410
                $wpinv_item_screen = 'n';
411
            }
412
        }
413
414
        if ( $wpinv_item_screen == 'y' && $pagenow == 'edit.php' ) {
415
            add_filter( 'post_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
416
            add_filter( 'page_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
417
        }
418
    }
419
}
420
add_action( 'admin_head', 'wpinv_check_quick_edit', 10 );
421
422
function wpinv_item_disable_quick_edit( $actions = array(), $row = null ) {
423
    if ( isset( $actions['inline hide-if-no-js'] ) ) {
424
        unset( $actions['inline hide-if-no-js'] );
425
    }
426
427
    if ( !empty( $row->post_type ) && $row->post_type == 'wpi_item' && !wpinv_item_is_editable( $row ) ) {
428
        if ( isset( $actions['trash'] ) ) {
429
            unset( $actions['trash'] );
430
        }
431
        if ( isset( $actions['delete'] ) ) {
432
            unset( $actions['delete'] );
433
        }
434
    }
435
436
    return $actions;
437
}
438
439
/**
440
 * Create a page and store the ID in an option.
441
 *
442
 * @param mixed $slug Slug for the new page
443
 * @param string $option Option name to store the page's ID
444
 * @param string $page_title (default: '') Title for the new page
445
 * @param string $page_content (default: '') Content for the new page
446
 * @param int $post_parent (default: 0) Parent for the new page
447
 * @return int page ID
448
 */
449
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
450
    global $wpdb;
451
452
    $option_value = wpinv_get_option( $option );
453
454
    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

454
    if ( $option_value > 0 && ( $page_object = get_post( /** @scrutinizer ignore-type */ $option_value ) ) ) {
Loading history...
455
        if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
456
            // Valid page is already in place
457
            return $page_object->ID;
458
        }
459
    }
460
461
    if(!empty($post_parent)){
462
        $page = get_page_by_path($post_parent);
463
        if ($page) {
464
            $post_parent = $page->ID;
465
        } else {
466
            $post_parent = '';
467
        }
468
    }
469
470
    if ( strlen( $page_content ) > 0 ) {
471
        // Search for an existing page with the specified page content (typically a shortcode)
472
        $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}%" ) );
473
    } else {
474
        // Search for an existing page with the specified page slug
475
        $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 ) );
476
    }
477
478
    $valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content );
479
480
    if ( $valid_page_found ) {
481
        if ( $option ) {
482
            wpinv_update_option( $option, $valid_page_found );
483
        }
484
        return $valid_page_found;
485
    }
486
487
    // Search for a matching valid trashed page
488
    if ( strlen( $page_content ) > 0 ) {
489
        // Search for an existing page with the specified page content (typically a shortcode)
490
        $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}%" ) );
491
    } else {
492
        // Search for an existing page with the specified page slug
493
        $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 ) );
494
    }
495
496
    if ( $trashed_page_found ) {
497
        $page_id   = $trashed_page_found;
498
        $page_data = array(
499
            'ID'             => $page_id,
500
            'post_status'    => 'publish',
501
            'post_parent'    => $post_parent,
502
        );
503
        wp_update_post( $page_data );
504
    } else {
505
        $page_data = array(
506
            'post_status'    => 'publish',
507
            'post_type'      => 'page',
508
            'post_author'    => 1,
509
            'post_name'      => $slug,
510
            'post_title'     => $page_title,
511
            'post_content'   => $page_content,
512
            'post_parent'    => $post_parent,
513
            'comment_status' => 'closed',
514
        );
515
        $page_id = wp_insert_post( $page_data );
516
    }
517
518
    if ( $option ) {
519
        wpinv_update_option( $option, (int)$page_id );
520
    }
521
522
    return $page_id;
523
}