Passed
Push — master ( 75dadc...1ce22f )
by Brian
12:27 queued 06:18
created

wpinv_request()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 33
rs 7.3166
cc 11
nc 11
nop 1

How to fix   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
            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
54
        $url    = esc_url(
55
                    wp_nonce_url(
56
                        add_query_arg(
57
                            array(
58
                                'getpaid-admin-action' => 'deactivate_discount',
59
                                'discount'             => $discount->ID,
60
                            )
61
                        ),
62
                        'getpaid-nonce',
63
                        'getpaid-nonce'
64
                    )
65
                );
66
		$anchor = __( 'Deactivate', 'invoicing' );
67
		$title  = esc_attr__( 'Are you sure you want to deactivate this discount?', 'invoicing' );
68
        $row_actions['deactivate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>";
69
70
    } else if( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
71
72
        $url    = esc_url(
73
            wp_nonce_url(
74
                add_query_arg(
75
                    array(
76
                        'getpaid-admin-action' => 'activate_discount',
77
                        'discount'             => $discount->ID,
78
                    )
79
                ),
80
                'getpaid-nonce',
81
                'getpaid-nonce'
82
            )
83
        );
84
		$anchor = __( 'Activate', 'invoicing' );
85
		$title  = esc_attr__( 'Are you sure you want to activate this discount?', 'invoicing' );
86
        $row_actions['activate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>";
87
88
    }
89
90
    $url    = esc_url(
91
        wp_nonce_url(
92
            add_query_arg(
93
                array(
94
                    'getpaid-admin-action' => 'delete_discount',
95
                    'discount'             => $discount->ID,
96
                )
97
            ),
98
            'getpaid-nonce',
99
            'getpaid-nonce'
100
        )
101
    );
102
	$anchor = __( 'Delete', 'invoicing' );
103
	$title  = esc_attr__( 'Are you sure you want to delete this discount?', 'invoicing' );
104
    $row_actions['delete'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>";
105
106
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
107
108
    return $row_actions;
109
}
110
111
function wpinv_restrict_manage_posts() {
112
    global $typenow;
113
114
    if( 'wpi_discount' == $typenow ) {
115
        wpinv_discount_filters();
116
    }
117
}
118
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
119
120
function wpinv_discount_filters() {
121
122
    ?>
123
    <select name="discount_type" id="dropdown_wpinv_discount_type">
124
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
125
        <?php
126
            $types = wpinv_get_discount_types();
127
128
            foreach ( $types as $name => $type ) {
129
                echo '<option value="' . esc_attr( $name ) . '"';
130
131
                if ( isset( $_GET['discount_type'] ) )
132
                    selected( $name, $_GET['discount_type'] );
133
134
                echo '>' . esc_html__( $type, 'invoicing' ) . '</option>';
135
            }
136
        ?>
137
    </select>
138
    <?php
139
}
140
141
function wpinv_request( $vars ) {
142
    global $typenow, $wp_post_statuses;
143
144
    if ( getpaid_is_invoice_post_type( $typenow ) ) {
145
        if ( ! isset( $vars['post_status'] ) ) {
146
            $post_statuses = wpinv_get_invoice_statuses( false, false, $typenow );
147
148
            foreach ( $post_statuses as $status => $value ) {
149
                if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) {
150
                    unset( $post_statuses[ $status ] );
151
                }
152
            }
153
154
            $vars['post_status'] = array_keys( $post_statuses );
155
        }
156
157
    } else if ( 'wpi_discount' == $typenow ) {
158
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
159
        // Filter vat rule type
160
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
161
            $meta_query[] = array(
162
                    'key'   => '_wpi_discount_type',
163
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
164
                    'compare' => '='
165
                );
166
        }
167
168
        if ( !empty( $meta_query ) ) {
169
            $vars['meta_query'] = $meta_query;
170
        }
171
    }
172
173
    return $vars;
174
}
175
add_filter( 'request', 'wpinv_request' );
176
177
/**
178
 * Create a page and store the ID in an option.
179
 *
180
 * @param mixed $slug Slug for the new page
181
 * @param string $option Option name to store the page's ID
182
 * @param string $page_title (default: '') Title for the new page
183
 * @param string $page_content (default: '') Content for the new page
184
 * @param int $post_parent (default: 0) Parent for the new page
185
 * @return int page ID
186
 */
187
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
188
    global $wpdb;
189
190
    $option_value = wpinv_get_option( $option );
191
192
    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

192
    if ( $option_value > 0 && ( $page_object = get_post( /** @scrutinizer ignore-type */ $option_value ) ) ) {
Loading history...
193
        if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
194
            // Valid page is already in place
195
            return $page_object->ID;
196
        }
197
    }
198
199
    if(!empty($post_parent)){
200
        $page = get_page_by_path($post_parent);
201
        if ($page) {
202
            $post_parent = $page->ID;
203
        } else {
204
            $post_parent = '';
205
        }
206
    }
207
208
    if ( strlen( $page_content ) > 0 ) {
209
        // Search for an existing page with the specified page content (typically a shortcode)
210
        $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}%" ) );
211
    } else {
212
        // Search for an existing page with the specified page slug
213
        $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 ) );
214
    }
215
216
    $valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content );
217
218
    if ( $valid_page_found ) {
219
        if ( $option ) {
220
            wpinv_update_option( $option, $valid_page_found );
221
        }
222
        return $valid_page_found;
223
    }
224
225
    // Search for a matching valid trashed page
226
    if ( strlen( $page_content ) > 0 ) {
227
        // Search for an existing page with the specified page content (typically a shortcode)
228
        $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}%" ) );
229
    } else {
230
        // Search for an existing page with the specified page slug
231
        $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 ) );
232
    }
233
234
    if ( $trashed_page_found ) {
235
        $page_id   = $trashed_page_found;
236
        $page_data = array(
237
            'ID'             => $page_id,
238
            'post_status'    => 'publish',
239
            'post_parent'    => $post_parent,
240
        );
241
        wp_update_post( $page_data );
242
    } else {
243
        $page_data = array(
244
            'post_status'    => 'publish',
245
            'post_type'      => 'page',
246
            'post_author'    => 1,
247
            'post_name'      => $slug,
248
            'post_title'     => $page_title,
249
            'post_content'   => $page_content,
250
            'post_parent'    => $post_parent,
251
            'comment_status' => 'closed',
252
        );
253
        $page_id = wp_insert_post( $page_data );
254
    }
255
256
    if ( $option ) {
257
        wpinv_update_option( $option, (int)$page_id );
258
    }
259
260
    return $page_id;
261
}