Passed
Pull Request — master (#69)
by
unknown
04:21
created

admin-pages.php ➔ wpinv_create_page()   D

Complexity

Conditions 13
Paths 121

Size

Total Lines 75
Code Lines 46

Duplication

Lines 14
Ratio 18.67 %

Importance

Changes 0
Metric Value
cc 13
eloc 46
nc 121
nop 5
dl 14
loc 75
rs 4.94
c 0
b 0
f 0

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