|
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; |
|
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
|
|
|
} elseif ( get_the_ID() ) { |
|
30
|
|
|
return (int) get_the_ID(); |
|
31
|
|
|
} elseif ( isset( $post->ID ) && ! empty( $post->ID ) ) { |
|
32
|
|
|
return (int) $post->ID; |
|
33
|
|
|
} elseif ( isset( $_GET['post'] ) && ! empty( $_GET['post'] ) ) { |
|
34
|
|
|
return (int) $_GET['post']; |
|
35
|
|
|
} elseif ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) { |
|
36
|
|
|
return (int) $_GET['id']; |
|
37
|
|
|
} elseif ( 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'] ) ? sanitize_text_field( $_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 ) && wpinv_current_user_can_manage_invoicing() ) { |
|
80
|
|
|
$link = admin_url( 'admin.php?page=wpinv-settings&tab=gateways' ); |
|
81
|
|
|
$notice = wp_sprintf( __( '<strong>Important:</strong> Payment Gateway(s) %1$s are in testing mode and will not receive real payments. Go to <a href="%2$s"> Gateway Settings</a>.', 'invoicing' ), $test_gateways, $link ); |
|
82
|
|
|
?> |
|
83
|
|
|
<div class="notice notice-warning is-dismissible"> |
|
84
|
|
|
<p><?php echo wp_kses_post( $notice ); ?></p> |
|
85
|
|
|
</div> |
|
86
|
|
|
<?php |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks if all tables are available, |
|
92
|
|
|
* and alerts the user for any missing tables. |
|
93
|
|
|
*/ |
|
94
|
|
|
function wpinv_check_for_missing_tables() { |
|
95
|
|
|
global $wpdb; |
|
96
|
|
|
|
|
97
|
|
|
// Only do this on our settings page. |
|
98
|
|
|
if ( empty( $_GET['page'] ) || 'wpinv-settings' !== $_GET['page'] ) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Check tables. |
|
103
|
|
|
$tables = array( |
|
104
|
|
|
"{$wpdb->prefix}wpinv_subscriptions", |
|
105
|
|
|
"{$wpdb->prefix}getpaid_invoices", |
|
106
|
|
|
"{$wpdb->prefix}getpaid_invoice_items", |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
foreach ( $tables as $table ) { |
|
110
|
|
|
if ( $table != $wpdb->get_var( "SHOW TABLES LIKE '$table'" ) ) { |
|
111
|
|
|
|
|
112
|
|
|
$url = wp_nonce_url( |
|
113
|
|
|
add_query_arg( 'getpaid-admin-action', 'create_missing_tables' ), |
|
114
|
|
|
'getpaid-nonce', |
|
115
|
|
|
'getpaid-nonce' |
|
116
|
|
|
); |
|
117
|
|
|
$message = __( 'Some GetPaid database tables are missing. To use GetPaid without any issues, click on the button below to create the missing tables.', 'invoicing' ); |
|
118
|
|
|
$message2 = __( 'Create Tables', 'invoicing' ); |
|
119
|
|
|
echo wp_kses_post( "<div class='notice notice-warning is-dismissible'><p>$message<br><br><a href='$url' class='button button-primary'>$message2</a></p></div>" ); |
|
120
|
|
|
break; |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
add_action( 'admin_notices', 'wpinv_check_for_missing_tables' ); |
|
127
|
|
|
|
|
128
|
|
|
add_action( 'admin_init', 'wpinv_admin_search_by_invoice' ); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* hook the posts search if we're on the admin page for our type |
|
132
|
|
|
*/ |
|
133
|
|
|
function wpinv_admin_search_by_invoice() { |
|
134
|
|
|
global $typenow; |
|
135
|
|
|
|
|
136
|
|
|
if ( $typenow === 'wpi_invoice' || $typenow === 'wpi_quote' ) { |
|
137
|
|
|
add_filter( 'posts_search', 'wpinv_posts_search_example_type', 10, 2 ); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* add query condition for search invoice by email |
|
143
|
|
|
* @param string $search the search string so far |
|
144
|
|
|
* @param WP_Query $query |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
function wpinv_posts_search_example_type( $search, $query ) { |
|
148
|
|
|
global $wpdb; |
|
149
|
|
|
|
|
150
|
|
|
if ( $query->is_main_query() && ! empty( $query->query['s'] ) ) { |
|
151
|
|
|
$conditions_str = "{$wpdb->posts}.post_author IN ( SELECT ID FROM {$wpdb->users} WHERE user_email LIKE '%" . esc_sql( $query->query['s'] ) . "%' )"; |
|
|
|
|
|
|
152
|
|
|
if ( ! empty( $search ) ) { |
|
153
|
|
|
$search = preg_replace( '/^ AND /', '', $search ); |
|
154
|
|
|
$search = " AND ( {$search} OR ( {$conditions_str} ) )"; |
|
155
|
|
|
} else { |
|
156
|
|
|
$search = " AND ( {$conditions_str} )"; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $search; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Resets invoice counts. |
|
165
|
|
|
*/ |
|
166
|
|
|
function wpinv_reset_invoice_count() { |
|
167
|
|
|
if ( ! empty( $_GET['reset_invoice_count'] ) && isset( $_GET['_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_GET['_nonce'] ), 'reset_invoice_count' ) ) { |
|
168
|
|
|
wpinv_update_option( 'invoice_sequence_start', 1 ); |
|
169
|
|
|
delete_option( 'wpinv_last_invoice_number' ); |
|
170
|
|
|
getpaid_admin()->show_success( __( 'Invoice number sequence reset successfully.', 'invoicing' ) ); |
|
171
|
|
|
$url = remove_query_arg( array( 'reset_invoice_count', '_nonce' ) ); |
|
172
|
|
|
wp_redirect( $url ); |
|
173
|
|
|
exit(); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
add_action( 'admin_init', 'wpinv_reset_invoice_count' ); |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Displays line items on the invoice edit page. |
|
180
|
|
|
* |
|
181
|
|
|
* @param WPInv_Invoice $invoice |
|
182
|
|
|
* @param array $columns |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
|
|
function wpinv_admin_get_line_items( $invoice, $columns ) { |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
ob_start(); |
|
188
|
|
|
|
|
189
|
|
|
do_action( 'getpaid_admin_before_line_items', $invoice ); |
|
190
|
|
|
|
|
191
|
|
|
$count = 0; |
|
192
|
|
|
foreach ( $invoice->get_items() as $item ) { |
|
193
|
|
|
|
|
194
|
|
|
$item_price = wpinv_price( $item->get_price(), $invoice->get_currency() ); |
|
195
|
|
|
$quantity = (int) $item->get_quantity(); |
|
196
|
|
|
$item_subtotal = wpinv_price( $item->get_sub_total(), $invoice->get_currency() ); |
|
197
|
|
|
$summary = apply_filters( 'getpaid_admin_invoice_line_item_summary', $item->get_description(), $item, $invoice ); |
|
198
|
|
|
$item_tax = $item->item_tax; |
|
199
|
|
|
$tax_rate = wpinv_round_amount( getpaid_get_invoice_tax_rate( $invoice, $item ), 2, true ) . '%'; |
|
200
|
|
|
$tax_rate = empty( $tax_rate ) ? ' <span class="tax-rate">(' . $tax_rate . '%)</span>' : ''; |
|
201
|
|
|
$line_item_tax = $item_tax . $tax_rate; |
|
202
|
|
|
$line_item = '<tr class="item item-' . ( ($count % 2 == 0) ? 'even' : 'odd' ) . '" data-item-id="' . esc_attr( $item->get_id() ) . '">'; |
|
203
|
|
|
$line_item .= '<td class="id">' . (int) $item->get_id() . '</td>'; |
|
204
|
|
|
$line_item .= '<td class="title"><a href="' . get_edit_post_link( $item->get_id() ) . '" target="_blank">' . $item->get_name() . '</a>'; |
|
205
|
|
|
|
|
206
|
|
|
if ( $summary !== '' ) { |
|
207
|
|
|
$line_item .= '<span class="meta">' . wp_kses_post( wpautop( $summary ) ) . '</span>'; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$line_item .= '</td>'; |
|
211
|
|
|
$line_item .= '<td class="price">' . $item_price . '</td>'; |
|
212
|
|
|
$line_item .= '<td class="qty" data-quantity="' . $quantity . '"> × ' . $quantity . '</td>'; |
|
213
|
|
|
$line_item .= '<td class="total">' . $item_subtotal . '</td>'; |
|
214
|
|
|
|
|
215
|
|
|
if ( wpinv_use_taxes() && $invoice->is_taxable() ) { |
|
216
|
|
|
$line_item .= '<td class="tax">' . $line_item_tax . '</td>'; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$line_item .= '<td class="action">'; |
|
220
|
|
|
if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
221
|
|
|
$line_item .= '<i class="fa fa-remove wpinv-item-remove"></i>'; |
|
222
|
|
|
} |
|
223
|
|
|
$line_item .= '</td>'; |
|
224
|
|
|
$line_item .= '</tr>'; |
|
225
|
|
|
|
|
226
|
|
|
echo wp_kses_post( apply_filters( 'getpaid_admin_line_item', $line_item, $item, $invoice ) ); |
|
227
|
|
|
|
|
228
|
|
|
$count++; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
do_action( 'getpaid_admin_after_line_items', $invoice ); |
|
232
|
|
|
|
|
233
|
|
|
return ob_get_clean(); |
|
234
|
|
|
} |
|
235
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.