Passed
Push — master ( c16653...06c25c )
by Brian
04:36
created

wpinv_discount_custom_column()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 21
rs 9.0444
cc 6
nc 6
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_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
            echo getpaid_format_date_value( $discount->get_start_date() );
25
        break;
26
        case 'expiry_date' :
27
            echo getpaid_format_date_value( $discount->get_expiration_date(), __( 'Never', 'invoicing' ) );
28
        break;
29
    }
30
}
31
32
add_filter( 'post_row_actions', 'wpinv_post_row_actions', 9999, 2 );
33
function wpinv_post_row_actions( $actions, $post ) {
34
    $post_type = !empty( $post->post_type ) ? $post->post_type : '';
35
36
    if ( $post_type == 'wpi_invoice' ) {
37
        $actions = array();
38
    }
39
40
    if ( $post_type == 'wpi_discount' ) {
41
        $actions = wpinv_discount_row_actions( $post, $actions );
42
    }
43
44
    return $actions;
45
}
46
47
function wpinv_discount_row_actions( $discount, $row_actions ) {
48
    $row_actions  = array();
49
    $edit_link = get_edit_post_link( $discount->ID );
50
    $row_actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'invoicing' ) . '</a>';
51
52
    if( in_array( strtolower( $discount->post_status ),  array(  'publish' ) ) ) {
53
        $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>';
54
    } elseif( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
55
        $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>';
56
    }
57
58
    $delete_url = esc_url(
59
        wp_nonce_url(
60
            add_query_arg(
61
                array(
62
                    'wpi_action' => 'delete_discount',
63
                    'discount' => $discount->ID
64
                )
65
            ),
66
            'wpinv_discount_nonce'
67
        )
68
    );
69
    $row_actions['delete'] = '<a href="' . $delete_url . '">' . __( 'Delete', 'invoicing' ) . '</a>';
70
71
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
72
73
    return $row_actions;
74
}
75
76
add_filter( 'list_table_primary_column', 'wpinv_table_primary_column', 10, 2 );
77
function wpinv_table_primary_column( $default, $screen_id ) {
78
    if ( 'edit-wpi_invoice' === $screen_id ) {
79
        return 'name';
80
    }
81
82
    return $default;
83
}
84
85
function wpinv_discount_bulk_actions( $actions, $display = false ) {
86
    if ( !$display ) {
87
        return array();
88
    }
89
90
    $actions = array(
91
        'activate'   => __( 'Activate', 'invoicing' ),
92
        'deactivate' => __( 'Deactivate', 'invoicing' ),
93
        'delete'     => __( 'Delete', 'invoicing' ),
94
    );
95
    $two = '';
96
    $which = 'top';
97
    echo '</div><div class="alignleft actions bulkactions">';
98
    echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action' ) . '</label>';
99
    echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">";
100
    echo '<option value="-1">' . __( 'Bulk Actions' ) . "</option>";
101
102
    foreach ( $actions as $name => $title ) {
103
        $class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
104
105
        echo "" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>";
106
    }
107
    echo "</select>";
108
109
    submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction$two" ) );
110
111
    echo '</div><div class="alignleft actions">';
112
}
113
add_filter( 'bulk_actions-edit-wpi_discount', 'wpinv_discount_bulk_actions', 10 );
114
115
function wpinv_disable_months_dropdown( $disable, $post_type ) {
116
    if ( $post_type == 'wpi_discount' ) {
117
        $disable = true;
118
    }
119
120
    return $disable;
121
}
122
add_filter( 'disable_months_dropdown', 'wpinv_disable_months_dropdown', 10, 2 );
123
124
function wpinv_restrict_manage_posts() {
125
    global $typenow;
126
127
    if( 'wpi_discount' == $typenow ) {
128
        wpinv_discount_filters();
129
    }
130
}
131
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
132
133
function wpinv_discount_filters() {
134
    wpinv_discount_bulk_actions( array(), true );
135
136
    ?>
137
    <select name="discount_type" id="dropdown_wpinv_discount_type">
138
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
139
        <?php
140
            $types = wpinv_get_discount_types();
141
142
            foreach ( $types as $name => $type ) {
143
                echo '<option value="' . esc_attr( $name ) . '"';
144
145
                if ( isset( $_GET['discount_type'] ) )
146
                    selected( $name, $_GET['discount_type'] );
147
148
                echo '>' . esc_html__( $type, 'invoicing' ) . '</option>';
149
            }
150
        ?>
151
    </select>
152
    <?php
153
}
154
155
function wpinv_request( $vars ) {
156
    global $typenow, $wp_query, $wp_post_statuses;
157
158
    if ( 'wpi_invoice' === $typenow ) {
159
        if ( !isset( $vars['post_status'] ) ) {
160
            $post_statuses = wpinv_get_invoice_statuses();
161
162
            foreach ( $post_statuses as $status => $value ) {
163
                if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
164
                    unset( $post_statuses[ $status ] );
165
                }
166
            }
167
168
            $vars['post_status'] = array_keys( $post_statuses );
169
        }
170
171
        if ( isset( $vars['orderby'] ) ) {
172
            if ( 'amount' == $vars['orderby'] ) {
173
                $vars = array_merge(
174
                    $vars,
175
                    array(
176
                        'meta_key' => '_wpinv_total',
177
                        'orderby'  => 'meta_value_num'
178
                    )
179
                );
180
            } else if ( 'customer' == $vars['orderby'] ) {
181
                $vars = array_merge(
182
                    $vars,
183
                    array(
184
                        'meta_key' => '_wpinv_first_name',
185
                        'orderby'  => 'meta_value'
186
                    )
187
                );
188
            } else if ( 'number' == $vars['orderby'] ) {
189
                $vars = array_merge(
190
                    $vars,
191
                    array(
192
                        'meta_key' => '_wpinv_number',
193
                        'orderby'  => 'meta_value'
194
                    )
195
                );
196
            } else if ( 'payment_date' == $vars['orderby'] ) {
197
                $vars = array_merge(
198
                    $vars,
199
                    array(
200
                        'meta_key' => '_wpinv_completed_date',
201
                        'orderby'  => 'meta_value'
202
                    )
203
                );
204
            }
205
        }
206
    } else if ( 'wpi_discount' == $typenow ) {
207
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
208
        // Filter vat rule type
209
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
210
            $meta_query[] = array(
211
                    'key'   => '_wpi_discount_type',
212
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
213
                    'compare' => '='
214
                );
215
        }
216
217
        if ( !empty( $meta_query ) ) {
218
            $vars['meta_query'] = $meta_query;
219
        }
220
    }
221
222
    return $vars;
223
}
224
add_filter( 'request', 'wpinv_request' );
225
226
function wpinv_item_type_class( $classes, $class, $post_id ) {
227
    global $pagenow, $typenow;
228
229
    if ( $pagenow == 'edit.php' && $typenow == 'wpi_item' && get_post_type( $post_id ) == $typenow ) {
230
        if ( $type = get_post_meta( $post_id, '_wpinv_type', true ) ) {
231
            $classes[] = 'wpi-type-' . sanitize_html_class( $type );
232
        }
233
234
        if ( !wpinv_item_is_editable( $post_id ) ) {
235
            $classes[] = 'wpi-editable-n';
236
        }
237
    }
238
    return $classes;
239
}
240
add_filter( 'post_class', 'wpinv_item_type_class', 10, 3 );
241
242
function wpinv_check_quick_edit() {
243
    global $pagenow, $current_screen, $wpinv_item_screen;
244
245
    if ( $pagenow == 'edit.php' && !empty( $current_screen->post_type ) ) {
246
        if ( empty( $wpinv_item_screen ) ) {
247
            if ( $current_screen->post_type == 'wpi_item' ) {
248
                $wpinv_item_screen = 'y';
249
            } else {
250
                $wpinv_item_screen = 'n';
251
            }
252
        }
253
254
        if ( $wpinv_item_screen == 'y' && $pagenow == 'edit.php' ) {
255
            add_filter( 'post_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
256
            add_filter( 'page_row_actions', 'wpinv_item_disable_quick_edit', 10, 2 );
257
        }
258
    }
259
}
260
add_action( 'admin_head', 'wpinv_check_quick_edit', 10 );
261
262
function wpinv_item_disable_quick_edit( $actions = array(), $row = null ) {
263
    if ( isset( $actions['inline hide-if-no-js'] ) ) {
264
        unset( $actions['inline hide-if-no-js'] );
265
    }
266
267
    if ( !empty( $row->post_type ) && $row->post_type == 'wpi_item' && !wpinv_item_is_editable( $row ) ) {
268
        if ( isset( $actions['trash'] ) ) {
269
            unset( $actions['trash'] );
270
        }
271
        if ( isset( $actions['delete'] ) ) {
272
            unset( $actions['delete'] );
273
        }
274
    }
275
276
    return $actions;
277
}
278
279
/**
280
 * Create a page and store the ID in an option.
281
 *
282
 * @param mixed $slug Slug for the new page
283
 * @param string $option Option name to store the page's ID
284
 * @param string $page_title (default: '') Title for the new page
285
 * @param string $page_content (default: '') Content for the new page
286
 * @param int $post_parent (default: 0) Parent for the new page
287
 * @return int page ID
288
 */
289
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
290
    global $wpdb;
291
292
    $option_value = wpinv_get_option( $option );
293
294
    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

294
    if ( $option_value > 0 && ( $page_object = get_post( /** @scrutinizer ignore-type */ $option_value ) ) ) {
Loading history...
295
        if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
296
            // Valid page is already in place
297
            return $page_object->ID;
298
        }
299
    }
300
301
    if(!empty($post_parent)){
302
        $page = get_page_by_path($post_parent);
303
        if ($page) {
304
            $post_parent = $page->ID;
305
        } else {
306
            $post_parent = '';
307
        }
308
    }
309
310
    if ( strlen( $page_content ) > 0 ) {
311
        // Search for an existing page with the specified page content (typically a shortcode)
312
        $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}%" ) );
313
    } else {
314
        // Search for an existing page with the specified page slug
315
        $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 ) );
316
    }
317
318
    $valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content );
319
320
    if ( $valid_page_found ) {
321
        if ( $option ) {
322
            wpinv_update_option( $option, $valid_page_found );
323
        }
324
        return $valid_page_found;
325
    }
326
327
    // Search for a matching valid trashed page
328
    if ( strlen( $page_content ) > 0 ) {
329
        // Search for an existing page with the specified page content (typically a shortcode)
330
        $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}%" ) );
331
    } else {
332
        // Search for an existing page with the specified page slug
333
        $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 ) );
334
    }
335
336
    if ( $trashed_page_found ) {
337
        $page_id   = $trashed_page_found;
338
        $page_data = array(
339
            'ID'             => $page_id,
340
            'post_status'    => 'publish',
341
            'post_parent'    => $post_parent,
342
        );
343
        wp_update_post( $page_data );
344
    } else {
345
        $page_data = array(
346
            'post_status'    => 'publish',
347
            'post_type'      => 'page',
348
            'post_author'    => 1,
349
            'post_name'      => $slug,
350
            'post_title'     => $page_title,
351
            'post_content'   => $page_content,
352
            'post_parent'    => $post_parent,
353
            'comment_status' => 'closed',
354
        );
355
        $page_id = wp_insert_post( $page_data );
356
    }
357
358
    if ( $option ) {
359
        wpinv_update_option( $option, (int)$page_id );
360
    }
361
362
    return $page_id;
363
}