Passed
Push — master ( eba4be...60f62d )
by Brian
09:36 queued 04:47
created

wpinv_posts_custom_column()   F

Complexity

Conditions 27
Paths 192

Size

Total Lines 82
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 27
eloc 61
nc 192
nop 2
dl 0
loc 82
rs 3.4
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F wpinv_admin_messages() 0 64 45
A wpinv_show_test_payment_gateway_notice() 0 2 1

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
/**
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
	global $wpinv_options, $pagenow, $post;
60
61
	if ( isset( $_GET['wpinv-message'] ) && 'discount_added' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
62
		 add_settings_error( 'wpinv-notices', 'wpinv-discount-added', __( 'Discount code added.', 'invoicing' ), 'updated' );
63
	}
64
65
	if ( isset( $_GET['wpinv-message'] ) && 'discount_add_failed' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
66
		add_settings_error( 'wpinv-notices', 'wpinv-discount-add-fail', __( 'There was a problem adding your discount code, please try again.', 'invoicing' ), 'error' );
67
	}
68
69
	if ( isset( $_GET['wpinv-message'] ) && 'discount_exists' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
70
		add_settings_error( 'wpinv-notices', 'wpinv-discount-exists', __( 'A discount with that code already exists, please use a different code.', 'invoicing' ), 'error' );
71
	}
72
73
	if ( isset( $_GET['wpinv-message'] ) && 'discount_updated' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
74
		 add_settings_error( 'wpinv-notices', 'wpinv-discount-updated', __( 'Discount code updated.', 'invoicing' ), 'updated' );
75
	}
76
77
	if ( isset( $_GET['wpinv-message'] ) && 'discount_update_failed' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
78
		add_settings_error( 'wpinv-notices', 'wpinv-discount-updated-fail', __( 'There was a problem updating your discount code, please try again.', 'invoicing' ), 'error' );
79
	}
80
81
	if ( isset( $_GET['wpinv-message'] ) && 'invoice_deleted' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
82
		add_settings_error( 'wpinv-notices', 'wpinv-deleted', __( 'The invoice has been deleted.', 'invoicing' ), 'updated' );
83
	}
84
85
	if ( isset( $_GET['wpinv-message'] ) && 'email_disabled' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
86
		add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Email notification is disabled. Please check settings.', 'invoicing' ), 'error' );
87
	}
88
89
	if ( isset( $_GET['wpinv-message'] ) && 'email_sent' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
90
		add_settings_error( 'wpinv-notices', 'wpinv-sent', __( 'The email has been sent to customer.', 'invoicing' ), 'updated' );
91
    }
92
    
93
    if ( isset( $_GET['wpinv-message'] ) && 'email_fail' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
94
		add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Fail to send email to the customer.', 'invoicing' ), 'error' );
95
    }
96
97
    if ( isset( $_GET['wpinv-message'] ) && 'invoice-note-deleted' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
98
        add_settings_error( 'wpinv-notices', 'wpinv-note-deleted', __( 'The invoice note has been deleted.', 'invoicing' ), 'updated' );
99
    }
100
101
	if ( isset( $_GET['wpinv-message'] ) && 'settings-imported' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
102
		add_settings_error( 'wpinv-notices', 'wpinv-settings-imported', __( 'The settings have been imported.', 'invoicing' ), 'updated' );
103
	}
104
105
	if ( isset( $_GET['wpinv-message'] ) && 'note-added' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
106
		add_settings_error( 'wpinv-notices', 'wpinv-note-added', __( 'The invoice note has been added successfully.', 'invoicing' ), 'updated' );
107
	}
108
109
	if ( isset( $_GET['wpinv-message'] ) && 'invoice-updated' == $_GET['wpinv-message'] && wpinv_current_user_can_manage_invoicing() ) {
110
		add_settings_error( 'wpinv-notices', 'wpinv-updated', __( 'The invoice has been successfully updated.', 'invoicing' ), 'updated' );
111
	}
112
    
113
	if ( $pagenow == 'post.php' && !empty( $post->post_type ) && $post->post_type == 'wpi_item' && !wpinv_item_is_editable( $post ) ) {
114
		$message = apply_filters( 'wpinv_item_non_editable_message', __( 'This item in not editable.', 'invoicing' ), $post->ID );
115
116
		if ( !empty( $message ) ) {
117
			add_settings_error( 'wpinv-notices', 'wpinv-edit-n', $message, 'updated' );
118
		}
119
	}
120
121
	settings_errors( 'wpinv-notices' );
122
}
123
add_action( 'admin_notices', 'wpinv_admin_messages' );
124
125
add_action( 'admin_init', 'wpinv_show_test_payment_gateway_notice' );
126
function wpinv_show_test_payment_gateway_notice(){
127
    add_action( 'admin_notices', 'wpinv_test_payment_gateway_messages' );
128
}
129
130
function wpinv_test_payment_gateway_messages(){
131
    $gateways = wpinv_get_enabled_payment_gateways();
132
    $name = array(); $test_gateways = '';
133
    if ($gateways) {
134
        foreach ($gateways as $id => $gateway) {
135
            if (wpinv_is_test_mode($id)) {
136
                $name[] = $gateway['checkout_label'];
137
            }
138
        }
139
        $test_gateways = implode(', ', $name);
140
    }
141
    if(isset($test_gateways) && !empty($test_gateways)){
142
        $link = admin_url('admin.php?page=wpinv-settings&tab=gateways');
143
        $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 );
144
        ?>
145
        <div class="notice notice-warning is-dismissible">
146
            <p><?php echo $notice; ?></p>
147
        </div>
148
        <?php
149
    }
150
}
151
152
function wpinv_send_invoice_after_save( $invoice ) {
153
    if ( empty( $_POST['wpi_save_send'] ) ) {
154
        return;
155
    }
156
    
157
    if ( !empty( $invoice->ID ) && !empty( $invoice->post_type ) && 'wpi_invoice' == $invoice->post_type ) {
158
        wpinv_user_invoice_notification( $invoice->ID );
159
    }
160
}
161
add_action( 'wpinv_invoice_metabox_saved', 'wpinv_send_invoice_after_save', 100, 1 );
162
163
164
add_action('admin_init', 'admin_init_example_type');
165
166
/**
167
 * hook the posts search if we're on the admin page for our type
168
 */
169
function admin_init_example_type() {
170
    global $typenow;
171
172
    if ($typenow === 'wpi_invoice' || $typenow === 'wpi_quote' ) {
173
        add_filter('posts_search', 'posts_search_example_type', 10, 2);
174
    }
175
}
176
177
/**
178
 * add query condition for search invoice by email
179
 * @param string $search the search string so far
180
 * @param WP_Query $query
181
 * @return string
182
 */
183
function posts_search_example_type($search, $query) {
184
    global $wpdb;
185
186
    if ($query->is_main_query() && !empty($query->query['s'])) {
187
        $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

187
        $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...
188
        if ( ! empty( $search ) ) {
189
            $search = preg_replace( '/^ AND /', '', $search );
190
            $search = " AND ( {$search} OR ( {$conditions_str} ) )";
191
        } else {
192
            $search = " AND ( {$conditions_str} )";
193
        }
194
    }
195
196
    return $search;
197
}
198
199
add_action( 'admin_init', 'wpinv_reset_invoice_count' );
200
function wpinv_reset_invoice_count(){
201
    if(isset($_GET['reset_invoice_count']) && 1 == $_GET['reset_invoice_count'] && isset($_GET['_nonce']) && wp_verify_nonce($_GET['_nonce'], 'reset_invoice_count')) {
202
        wpinv_update_option('invoice_sequence_start', 1);
203
        delete_option('wpinv_last_invoice_number');
204
        $url = add_query_arg(array('reset_invoice_done' => 1));
205
        $url = remove_query_arg(array('reset_invoice_count', '_nonce'), $url);
206
        wp_redirect($url);
207
        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...
208
    }
209
}
210
211
add_action('admin_notices', 'wpinv_invoice_count_reset_message');
212
function wpinv_invoice_count_reset_message(){
213
    if(isset($_GET['reset_invoice_done']) && 1 == $_GET['reset_invoice_done']) {
214
        $notice = __('Invoice number sequence reset successfully.', 'invoicing');
215
        ?>
216
        <div class="notice notice-success is-dismissible">
217
            <p><?php echo $notice; ?></p>
218
        </div>
219
        <?php
220
    }
221
}
222