Passed
Pull Request — master (#434)
by Brian
06:25
created

wpinv_admin_messages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
function wpinv_bulk_actions( $actions ) {
15
    if ( isset( $actions['edit'] ) ) {
16
        unset( $actions['edit'] );
17
    }
18
19
    return $actions;
20
}
21
add_filter( 'bulk_actions-edit-wpi_invoice', 'wpinv_bulk_actions' );
22
add_filter( 'bulk_actions-edit-wpi_item', 'wpinv_bulk_actions' );
23
24
function wpinv_admin_post_id( $id = 0 ) {
25
    global $post;
26
27
    if ( isset( $id ) && ! empty( $id ) ) {
28
        return (int)$id;
29
    } else if ( get_the_ID() ) {
30
        return (int) get_the_ID();
31
    } else if ( isset( $post->ID ) && !empty( $post->ID ) ) {
32
        return (int) $post->ID;
33
    } else if ( isset( $_GET['post'] ) && !empty( $_GET['post'] ) ) {
34
        return (int) $_GET['post'];
35
    } else if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) ) {
36
        return (int) $_GET['id'];
37
    } else if ( isset( $_POST['id'] ) && !empty( $_POST['id'] ) ) {
38
        return (int) $_POST['id'];
39
    } 
40
41
    return null;
42
}
43
    
44
function wpinv_admin_post_type( $id = 0 ) {
45
    if ( !$id ) {
46
        $id = wpinv_admin_post_id();
47
    }
48
    
49
    $type = get_post_type( $id );
50
    
51
    if ( !$type ) {
52
        $type = isset( $_GET['post_type'] ) && !empty( $_GET['post_type'] ) ? $_GET['post_type'] : null;
53
    }
54
    
55
    return apply_filters( 'wpinv_admin_post_type', $type, $id );
56
}
57
58
function wpinv_admin_messages() {
59
	settings_errors( 'wpinv-notices' );
60
}
61
add_action( 'admin_notices', 'wpinv_admin_messages' );
62
63
add_action( 'admin_init', 'wpinv_show_test_payment_gateway_notice' );
64
function wpinv_show_test_payment_gateway_notice(){
65
    add_action( 'admin_notices', 'wpinv_test_payment_gateway_messages' );
66
}
67
68
function wpinv_test_payment_gateway_messages(){
69
    $gateways = wpinv_get_enabled_payment_gateways();
70
    $name = array(); $test_gateways = '';
71
    if ($gateways) {
72
        foreach ($gateways as $id => $gateway) {
73
            if (wpinv_is_test_mode($id)) {
74
                $name[] = $gateway['checkout_label'];
75
            }
76
        }
77
        $test_gateways = implode(', ', $name);
78
    }
79
    if(isset($test_gateways) && !empty($test_gateways)){
80
        $link = admin_url('admin.php?page=wpinv-settings&tab=gateways');
81
        $notice = wp_sprintf( __('<strong>Important:</strong> Payment Gateway(s) %s are in testing mode and will not receive real payments. Go to <a href="%s"> Gateway Settings</a>.', 'invoicing'), $test_gateways, $link );
82
        ?>
83
        <div class="notice notice-warning is-dismissible">
84
            <p><?php echo $notice; ?></p>
85
        </div>
86
        <?php
87
    }
88
}
89
90
function wpinv_send_invoice_after_save( $invoice ) {
91
    if ( empty( $_POST['wpi_save_send'] ) ) {
92
        return;
93
    }
94
    
95
    if ( !empty( $invoice->ID ) && !empty( $invoice->post_type ) && 'wpi_invoice' == $invoice->post_type ) {
96
        wpinv_user_invoice_notification( $invoice->ID );
0 ignored issues
show
Deprecated Code introduced by
The function wpinv_user_invoice_notification() has been deprecated. ( Ignorable by Annotation )

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

96
        /** @scrutinizer ignore-deprecated */ wpinv_user_invoice_notification( $invoice->ID );
Loading history...
Unused Code introduced by
The call to wpinv_user_invoice_notification() has too many arguments starting with $invoice->ID. ( Ignorable by Annotation )

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

96
        /** @scrutinizer ignore-call */ 
97
        wpinv_user_invoice_notification( $invoice->ID );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97
    }
98
}
99
add_action( 'wpinv_invoice_metabox_saved', 'wpinv_send_invoice_after_save', 100, 1 );
100
101
102
add_action('admin_init', 'admin_init_example_type');
103
104
/**
105
 * hook the posts search if we're on the admin page for our type
106
 */
107
function admin_init_example_type() {
108
    global $typenow;
109
110
    if ($typenow === 'wpi_invoice' || $typenow === 'wpi_quote' ) {
111
        add_filter('posts_search', 'posts_search_example_type', 10, 2);
112
    }
113
}
114
115
/**
116
 * add query condition for search invoice by email
117
 * @param string $search the search string so far
118
 * @param WP_Query $query
119
 * @return string
120
 */
121
function posts_search_example_type($search, $query) {
122
    global $wpdb;
123
124
    if ($query->is_main_query() && !empty($query->query['s'])) {
125
        $conditions_str = "{$wpdb->posts}.post_author IN ( SELECT ID FROM {$wpdb->users} WHERE user_email LIKE '%" . esc_sql( $query->query['s'] ) . "%' )";
0 ignored issues
show
Bug introduced by
Are you sure esc_sql($query->query['s']) of type array|string 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

125
        $conditions_str = "{$wpdb->posts}.post_author IN ( SELECT ID FROM {$wpdb->users} WHERE user_email LIKE '%" . /** @scrutinizer ignore-type */ esc_sql( $query->query['s'] ) . "%' )";
Loading history...
126
        if ( ! empty( $search ) ) {
127
            $search = preg_replace( '/^ AND /', '', $search );
128
            $search = " AND ( {$search} OR ( {$conditions_str} ) )";
129
        } else {
130
            $search = " AND ( {$conditions_str} )";
131
        }
132
    }
133
134
    return $search;
135
}
136
137
add_action( 'admin_init', 'wpinv_reset_invoice_count' );
138
function wpinv_reset_invoice_count(){
139
    if(isset($_GET['reset_invoice_count']) && 1 == $_GET['reset_invoice_count'] && isset($_GET['_nonce']) && wp_verify_nonce($_GET['_nonce'], 'reset_invoice_count')) {
140
        wpinv_update_option('invoice_sequence_start', 1);
141
        delete_option('wpinv_last_invoice_number');
142
        $url = add_query_arg(array('reset_invoice_done' => 1));
143
        $url = remove_query_arg(array('reset_invoice_count', '_nonce'), $url);
144
        wp_redirect($url);
145
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
146
    }
147
}
148
149
add_action('admin_notices', 'wpinv_invoice_count_reset_message');
150
function wpinv_invoice_count_reset_message(){
151
    if(isset($_GET['reset_invoice_done']) && 1 == $_GET['reset_invoice_done']) {
152
        $notice = __('Invoice number sequence reset successfully.', 'invoicing');
153
        ?>
154
        <div class="notice notice-success is-dismissible">
155
            <p><?php echo $notice; ?></p>
156
        </div>
157
        <?php
158
    }
159
}
160