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_columns( $columns ) { |
15
|
|
|
$columns = array( |
16
|
|
|
'cb' => $columns['cb'], |
17
|
|
|
'number' => __( 'Number', 'invoicing' ), |
18
|
|
|
'customer' => __( 'Customer', 'invoicing' ), |
19
|
|
|
'amount' => __( 'Amount', 'invoicing' ), |
20
|
|
|
'invoice_date' => __( 'Created Date', 'invoicing' ), |
21
|
|
|
'payment_date' => __( 'Payment Date', 'invoicing' ), |
22
|
|
|
'status' => __( 'Status', 'invoicing' ), |
23
|
|
|
'ID' => __( 'ID', 'invoicing' ), |
24
|
|
|
'wpi_actions' => __( 'Actions', 'invoicing' ), |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
return apply_filters( 'wpi_invoice_table_columns', $columns ); |
28
|
|
|
} |
29
|
|
|
add_filter( 'manage_wpi_invoice_posts_columns', 'wpinv_columns' ); |
30
|
|
|
|
31
|
|
|
function wpinv_bulk_actions( $actions ) { |
32
|
|
|
if ( isset( $actions['edit'] ) ) { |
33
|
|
|
unset( $actions['edit'] ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $actions; |
37
|
|
|
} |
38
|
|
|
add_filter( 'bulk_actions-edit-wpi_invoice', 'wpinv_bulk_actions' ); |
39
|
|
|
add_filter( 'bulk_actions-edit-wpi_item', 'wpinv_bulk_actions' ); |
40
|
|
|
|
41
|
|
|
function wpinv_sortable_columns( $columns ) { |
|
|
|
|
42
|
|
|
$columns = array( |
43
|
|
|
'ID' => array( 'ID', true ), |
44
|
|
|
'number' => array( 'number', false ), |
45
|
|
|
'amount' => array( 'amount', false ), |
46
|
|
|
'invoice_date' => array( 'date', false ), |
47
|
|
|
'payment_date' => array( 'payment_date', true ), |
48
|
|
|
'customer' => array( 'customer', false ), |
49
|
|
|
'status' => array( 'status', false ), |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
return apply_filters( 'wpi_invoice_table_sortable_columns', $columns ); |
53
|
|
|
} |
54
|
|
|
add_filter( 'manage_edit-wpi_invoice_sortable_columns', 'wpinv_sortable_columns' ); |
55
|
|
|
|
56
|
|
|
add_action( 'manage_wpi_invoice_posts_custom_column', 'wpinv_posts_custom_column'); |
57
|
|
|
function wpinv_posts_custom_column( $column_name, $post_id = 0 ) { |
|
|
|
|
58
|
|
|
global $post, $wpi_invoice; |
59
|
|
|
|
60
|
|
|
if ( empty( $wpi_invoice ) || ( !empty( $wpi_invoice ) && $post->ID != $wpi_invoice->ID ) ) { |
61
|
|
|
$wpi_invoice = new WPInv_Invoice( $post->ID ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$value = NULL; |
65
|
|
|
|
66
|
|
|
switch ( $column_name ) { |
67
|
|
|
case 'email' : |
68
|
|
|
$value = $wpi_invoice->get_email(); |
69
|
|
|
break; |
70
|
|
|
case 'customer' : |
71
|
|
|
$customer_name = $wpi_invoice->get_user_full_name(); |
72
|
|
|
$customer_name = $customer_name != '' ? $customer_name : __( 'Customer', 'invoicing' ); |
73
|
|
|
$value = '<a href="' . esc_url( get_edit_user_link( $wpi_invoice->get_user_id() ) ) . '">' . $customer_name . '</a>'; |
74
|
|
|
if ( $email = $wpi_invoice->get_email() ) { |
75
|
|
|
$value .= '<br><a class="email" href="mailto:' . $email . '">' . $email . '</a>'; |
76
|
|
|
} |
77
|
|
|
break; |
78
|
|
|
case 'amount' : |
79
|
|
|
echo $wpi_invoice->get_total( true ); |
80
|
|
|
break; |
81
|
|
|
case 'invoice_date' : |
82
|
|
|
$date_format = get_option( 'date_format' ); |
83
|
|
|
$time_format = get_option( 'time_format' ); |
84
|
|
|
$date_time_format = $date_format . ' '. $time_format; |
85
|
|
|
|
86
|
|
|
$t_time = get_the_time( $date_time_format ); |
87
|
|
|
$m_time = $post->post_date; |
88
|
|
|
$h_time = mysql2date( $date_format, $m_time ); |
89
|
|
|
|
90
|
|
|
$value = '<abbr title="' . $t_time . '">' . $h_time . '</abbr>'; |
91
|
|
|
break; |
92
|
|
|
case 'payment_date' : |
93
|
|
|
if ( $date_completed = $wpi_invoice->get_meta( '_wpinv_completed_date', true ) ) { |
94
|
|
|
$date_format = get_option( 'date_format' ); |
95
|
|
|
$time_format = get_option( 'time_format' ); |
96
|
|
|
$date_time_format = $date_format . ' '. $time_format; |
97
|
|
|
|
98
|
|
|
$t_time = get_the_time( $date_time_format ); |
99
|
|
|
$m_time = $date_completed; |
100
|
|
|
$h_time = mysql2date( $date_format, $m_time ); |
101
|
|
|
|
102
|
|
|
$value = '<abbr title="' . $t_time . '">' . $h_time . '</abbr>'; |
103
|
|
|
} else { |
104
|
|
|
$value = '-'; |
105
|
|
|
} |
106
|
|
|
break; |
107
|
|
|
case 'status' : |
108
|
|
|
$value = $wpi_invoice->get_status( true ) . ( $wpi_invoice->is_recurring() && $wpi_invoice->is_parent() ? ' <span class="wpi-suffix">' . __( '(r)', 'invoicing' ) . '</span>' : '' ); |
109
|
|
|
if ( ( $wpi_invoice->is_paid() || $wpi_invoice->is_refunded() ) && $gateway_title = $wpi_invoice->get_gateway_title() ) { |
110
|
|
|
$value .= '<br><small class="meta gateway">' . wp_sprintf( __( 'Via %s', 'invoicing' ), $gateway_title ) . '</small>'; |
111
|
|
|
} |
112
|
|
|
break; |
113
|
|
|
case 'number' : |
114
|
|
|
$edit_link = get_edit_post_link( $post->ID ); |
115
|
|
|
$value = '<a title="' . esc_attr__( 'View Invoice Details', 'invoicing' ) . '" href="' . esc_url( $edit_link ) . '">' . $wpi_invoice->get_number() . '</a>'; |
116
|
|
|
break; |
117
|
|
|
case 'wpi_actions' : |
118
|
|
|
$value = ''; |
119
|
|
|
if ( !empty( $post->post_name ) ) { |
120
|
|
|
$value .= '<a title="' . esc_attr__( 'Print invoice', 'invoicing' ) . '" href="' . esc_url( get_permalink( $post->ID ) ) . '" class="button ui-tip column-act-btn" title="" target="_blank"><span class="dashicons dashicons-print"><i style="" class="fa fa-print"></i></span></a>'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ( $email = $wpi_invoice->get_email() ) { |
|
|
|
|
124
|
|
|
$value .= '<a title="' . esc_attr__( 'Send invoice to customer', 'invoicing' ) . '" href="' . esc_url( add_query_arg( array( 'wpi_action' => 'send_invoice', 'invoice_id' => $post->ID ) ) ) . '" class="button ui-tip column-act-btn"><span class="dashicons dashicons-email-alt"></span></a>'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
break; |
128
|
|
|
default: |
129
|
|
|
$value = isset( $post->$column_name ) ? $post->$column_name : ''; |
130
|
|
|
break; |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
$value = apply_filters( 'wpinv_payments_table_column', $value, $post->ID, $column_name ); |
134
|
|
|
|
135
|
|
|
if ( $value !== NULL ) { |
136
|
|
|
echo $value; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function wpinv_admin_post_id( $id = 0 ) { |
141
|
|
|
global $post; |
142
|
|
|
|
143
|
|
|
if ( isset( $id ) && ! empty( $id ) ) { |
144
|
|
|
return (int)$id; |
145
|
|
|
} else if ( get_the_ID() ) { |
146
|
|
|
return (int) get_the_ID(); |
147
|
|
|
} else if ( isset( $post->ID ) && !empty( $post->ID ) ) { |
148
|
|
|
return (int) $post->ID; |
149
|
|
|
} else if ( isset( $_GET['post'] ) && !empty( $_GET['post'] ) ) { |
150
|
|
|
return (int) $_GET['post']; |
151
|
|
|
} else if ( isset( $_GET['id'] ) && !empty( $_GET['id'] ) ) { |
152
|
|
|
return (int) $_GET['id']; |
153
|
|
|
} else if ( isset( $_POST['id'] ) && !empty( $_POST['id'] ) ) { |
154
|
|
|
return (int) $_POST['id']; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
function wpinv_admin_post_type( $id = 0 ) { |
161
|
|
|
if ( !$id ) { |
162
|
|
|
$id = wpinv_admin_post_id(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$type = get_post_type( $id ); |
166
|
|
|
|
167
|
|
|
if ( !$type ) { |
168
|
|
|
$type = isset( $_GET['post_type'] ) && !empty( $_GET['post_type'] ) ? $_GET['post_type'] : null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return apply_filters( 'wpinv_admin_post_type', $type, $id ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
function wpinv_admin_messages() { |
175
|
|
|
global $wpinv_options, $pagenow, $post; |
176
|
|
|
|
177
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'discount_added' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
178
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-discount-added', __( 'Discount code added.', 'invoicing' ), 'updated' ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'discount_add_failed' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
182
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-discount-add-fail', __( 'There was a problem adding your discount code, please try again.', 'invoicing' ), 'error' ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'discount_exists' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
186
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-discount-exists', __( 'A discount with that code already exists, please use a different code.', 'invoicing' ), 'error' ); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'discount_updated' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
190
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-discount-updated', __( 'Discount code updated.', 'invoicing' ), 'updated' ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'discount_update_failed' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
194
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-discount-updated-fail', __( 'There was a problem updating your discount code, please try again.', 'invoicing' ), 'error' ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'invoice_deleted' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
198
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-deleted', __( 'The invoice has been deleted.', 'invoicing' ), 'updated' ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'email_sent' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
202
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-sent', __( 'The email has been sent to customer.', 'invoicing' ), 'updated' ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'email_fail' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
206
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-sent-fail', __( 'Fail to send email to the customer.', 'invoicing' ), 'error' ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'invoice-note-deleted' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
210
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-note-deleted', __( 'The invoice note has been deleted.', 'invoicing' ), 'updated' ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'settings-imported' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
214
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-settings-imported', __( 'The settings have been imported.', 'invoicing' ), 'updated' ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'note-added' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
218
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-note-added', __( 'The invoice note has been added successfully.', 'invoicing' ), 'updated' ); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
View Code Duplication |
if ( isset( $_GET['wpinv-message'] ) && 'invoice-updated' == $_GET['wpinv-message'] && current_user_can( 'manage_options' ) ) { |
222
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-updated', __( 'The invoice has been successfully updated.', 'invoicing' ), 'updated' ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ( $pagenow == 'post.php' && !empty( $post->post_type ) && $post->post_type == 'wpi_item' && !wpinv_item_is_editable( $post ) ) { |
226
|
|
|
$message = apply_filters( 'wpinv_item_non_editable_message', __( 'This item in not editable.', 'invoicing' ), $post->ID ); |
227
|
|
|
|
228
|
|
|
if ( !empty( $message ) ) { |
229
|
|
|
add_settings_error( 'wpinv-notices', 'wpinv-edit-n', $message, 'updated' ); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
settings_errors( 'wpinv-notices' ); |
234
|
|
|
} |
235
|
|
|
add_action( 'admin_notices', 'wpinv_admin_messages' ); |
236
|
|
|
|
237
|
|
|
function wpinv_items_columns( $existing_columns ) { |
238
|
|
|
global $wpinv_euvat; |
239
|
|
|
|
240
|
|
|
$columns = array(); |
241
|
|
|
$columns['cb'] = $existing_columns['cb']; |
242
|
|
|
$columns['title'] = __( 'Title', 'invoicing' ); |
243
|
|
|
$columns['price'] = __( 'Price', 'invoicing' ); |
244
|
|
|
if ( $wpinv_euvat->allow_vat_rules() ) { |
245
|
|
|
$columns['vat_rule'] = __( 'VAT rule type', 'invoicing' ); |
246
|
|
|
} |
247
|
|
|
if ( $wpinv_euvat->allow_vat_classes() ) { |
248
|
|
|
$columns['vat_class'] = __( 'VAT class', 'invoicing' ); |
249
|
|
|
} |
250
|
|
|
$columns['type'] = __( 'Type', 'invoicing' ); |
251
|
|
|
$columns['recurring'] = __( 'Recurring', 'invoicing' ); |
252
|
|
|
$columns['date'] = __( 'Date', 'invoicing' ); |
253
|
|
|
$columns['id'] = __( 'ID', 'invoicing' ); |
254
|
|
|
|
255
|
|
|
return apply_filters( 'wpinv_items_columns', $columns ); |
256
|
|
|
} |
257
|
|
|
add_filter( 'manage_wpi_item_posts_columns', 'wpinv_items_columns' ); |
258
|
|
|
|
259
|
|
|
function wpinv_items_sortable_columns( $columns ) { |
260
|
|
|
$columns['price'] = 'price'; |
261
|
|
|
$columns['vat_rule'] = 'vat_rule'; |
262
|
|
|
$columns['vat_class'] = 'vat_class'; |
263
|
|
|
$columns['type'] = 'type'; |
264
|
|
|
//$columns['recurring'] = 'recurring'; |
|
|
|
|
265
|
|
|
$columns['id'] = 'ID'; |
266
|
|
|
|
267
|
|
|
return $columns; |
268
|
|
|
} |
269
|
|
|
add_filter( 'manage_edit-wpi_item_sortable_columns', 'wpinv_items_sortable_columns' ); |
270
|
|
|
|
271
|
|
|
function wpinv_items_table_custom_column( $column ) { |
272
|
|
|
global $wpinv_euvat, $post, $wpi_item; |
273
|
|
|
|
274
|
|
|
if ( empty( $wpi_item ) || ( !empty( $wpi_item ) && $post->ID != $wpi_item->ID ) ) { |
275
|
|
|
$wpi_item = new WPInv_Item( $post->ID ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
switch ( $column ) { |
279
|
|
|
case 'price' : |
280
|
|
|
echo wpinv_item_price( $post->ID ); |
281
|
|
|
break; |
282
|
|
|
case 'vat_rule' : |
283
|
|
|
echo $wpinv_euvat->item_rule_label( $post->ID ); |
284
|
|
|
break; |
285
|
|
|
case 'vat_class' : |
286
|
|
|
echo $wpinv_euvat->item_class_label( $post->ID ); |
287
|
|
|
break; |
288
|
|
|
case 'type' : |
289
|
|
|
echo wpinv_item_type( $post->ID ) . '<span class="meta">' . $wpi_item->get_custom_singular_name() . '</span>'; |
290
|
|
|
break; |
291
|
|
|
case 'recurring' : |
292
|
|
|
echo ( wpinv_is_recurring_item( $post->ID ) ? '<i class="fa fa-check fa-recurring-y"></i>' : '<i class="fa fa-close fa-recurring-n"></i>' ); |
293
|
|
|
break; |
294
|
|
|
case 'id' : |
295
|
|
|
echo $post->ID; |
296
|
|
|
echo '<div class="hidden" id="wpinv_inline-' . $post->ID . '"> |
297
|
|
|
<div class="price">' . wpinv_get_item_price( $post->ID ) . '</div>'; |
298
|
|
|
if ( $wpinv_euvat->allow_vat_rules() ) { |
299
|
|
|
echo '<div class="vat_rule">' . $wpinv_euvat->get_item_rule( $post->ID ) . '</div>'; |
300
|
|
|
} |
301
|
|
|
if ( $wpinv_euvat->allow_vat_classes() ) { |
302
|
|
|
echo '<div class="vat_class">' . $wpinv_euvat->get_item_class( $post->ID ) . '</div>'; |
303
|
|
|
} |
304
|
|
|
echo '<div class="type">' . wpinv_get_item_type( $post->ID ) . '</div> |
305
|
|
|
</div>'; |
306
|
|
|
break; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
do_action( 'wpinv_items_table_column_item_' . $column, $wpi_item, $post ); |
310
|
|
|
} |
311
|
|
|
add_action( 'manage_wpi_item_posts_custom_column', 'wpinv_items_table_custom_column' ); |
312
|
|
|
|
313
|
|
|
function wpinv_add_items_filters() { |
314
|
|
|
global $wpinv_euvat, $typenow; |
315
|
|
|
|
316
|
|
|
// Checks if the current post type is 'item' |
317
|
|
|
if ( $typenow == 'wpi_item') { |
318
|
|
View Code Duplication |
if ( $wpinv_euvat->allow_vat_rules() ) { |
319
|
|
|
echo wpinv_html_select( array( |
320
|
|
|
'options' => array_merge( array( '' => __( 'All VAT rules', 'invoicing' ) ), $wpinv_euvat->get_rules() ), |
321
|
|
|
'name' => 'vat_rule', |
322
|
|
|
'id' => 'vat_rule', |
323
|
|
|
'selected' => ( isset( $_GET['vat_rule'] ) ? $_GET['vat_rule'] : '' ), |
324
|
|
|
'show_option_all' => false, |
325
|
|
|
'show_option_none' => false, |
326
|
|
|
'class' => 'gdmbx2-text-medium', |
327
|
|
|
) ); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
View Code Duplication |
if ( $wpinv_euvat->allow_vat_classes() ) { |
331
|
|
|
echo wpinv_html_select( array( |
332
|
|
|
'options' => array_merge( array( '' => __( 'All VAT classes', 'invoicing' ) ), $wpinv_euvat->get_all_classes() ), |
333
|
|
|
'name' => 'vat_class', |
334
|
|
|
'id' => 'vat_class', |
335
|
|
|
'selected' => ( isset( $_GET['vat_class'] ) ? $_GET['vat_class'] : '' ), |
336
|
|
|
'show_option_all' => false, |
337
|
|
|
'show_option_none' => false, |
338
|
|
|
'class' => 'gdmbx2-text-medium', |
339
|
|
|
) ); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
echo wpinv_html_select( array( |
343
|
|
|
'options' => array_merge( array( '' => __( 'All item types', 'invoicing' ) ), wpinv_get_item_types() ), |
344
|
|
|
'name' => 'type', |
345
|
|
|
'id' => 'type', |
346
|
|
|
'selected' => ( isset( $_GET['type'] ) ? $_GET['type'] : '' ), |
347
|
|
|
'show_option_all' => false, |
348
|
|
|
'show_option_none' => false, |
349
|
|
|
'class' => 'gdmbx2-text-medium', |
350
|
|
|
) ); |
351
|
|
|
|
352
|
|
|
if ( isset( $_REQUEST['all_posts'] ) && '1' === $_REQUEST['all_posts'] ) { |
353
|
|
|
echo '<input type="hidden" name="all_posts" value="1" />'; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
add_action( 'restrict_manage_posts', 'wpinv_add_items_filters', 100 ); |
358
|
|
|
|
359
|
|
|
function wpinv_send_invoice_after_save( $invoice ) { |
360
|
|
|
if ( empty( $_POST['wpi_save_send'] ) ) { |
361
|
|
|
return; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
if ( !empty( $invoice->ID ) && !empty( $invoice->post_type ) && 'wpi_invoice' == $invoice->post_type ) { |
365
|
|
|
wpinv_user_invoice_notification( $invoice->ID ); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
add_action( 'wpinv_invoice_metabox_saved', 'wpinv_send_invoice_after_save', 100, 1 ); |
369
|
|
|
|
370
|
|
|
function wpinv_send_register_new_user( $data, $postarr ) { |
371
|
|
|
if ( current_user_can( 'manage_options' ) && !empty( $data['post_type'] ) && ( 'wpi_invoice' == $data['post_type'] || 'wpi_quote' == $data['post_type'] ) ) { |
372
|
|
|
$is_new_user = !empty( $postarr['wpinv_new_user'] ) ? true : false; |
373
|
|
|
$email = !empty( $postarr['wpinv_email'] ) && $postarr['wpinv_email'] && is_email( $postarr['wpinv_email'] ) ? $postarr['wpinv_email'] : NULL; |
374
|
|
|
|
375
|
|
|
if ( $is_new_user && $email && !email_exists( $email ) ) { |
376
|
|
|
$first_name = !empty( $postarr['wpinv_first_name'] ) ? sanitize_text_field( $postarr['wpinv_first_name'] ) : ''; |
377
|
|
|
$last_name = !empty( $postarr['wpinv_last_name'] ) ? sanitize_text_field( $postarr['wpinv_last_name'] ) : ''; |
378
|
|
|
$display_name = $first_name || $last_name ? trim( $first_name . ' ' . $last_name ) : ''; |
379
|
|
|
$user_nicename = $display_name ? trim( $display_name ) : $email; |
380
|
|
|
$user_company = !empty( $postarr['wpinv_company'] ) ? sanitize_text_field( $postarr['wpinv_company'] ) : ''; |
|
|
|
|
381
|
|
|
|
382
|
|
|
$user_login = sanitize_user( str_replace( ' ', '', $display_name ), true ); |
383
|
|
|
if ( !( validate_username( $user_login ) && !username_exists( $user_login ) ) ) { |
384
|
|
|
$new_user_login = strstr($email, '@', true); |
385
|
|
|
if ( validate_username( $user_login ) && username_exists( $user_login ) ) { |
386
|
|
|
$user_login = sanitize_user($new_user_login, true ); |
387
|
|
|
} |
388
|
|
|
if ( validate_username( $user_login ) && username_exists( $user_login ) ) { |
389
|
|
|
$user_append_text = rand(10,1000); |
390
|
|
|
$user_login = sanitize_user($new_user_login.$user_append_text, true ); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
if ( !( validate_username( $user_login ) && !username_exists( $user_login ) ) ) { |
394
|
|
|
$user_login = $email; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$userdata = array( |
399
|
|
|
'user_login' => $user_login, |
400
|
|
|
'user_pass' => wp_generate_password( 12, false ), |
401
|
|
|
'user_email' => sanitize_text_field( $email ), |
402
|
|
|
'first_name' => $first_name, |
403
|
|
|
'last_name' => $last_name, |
404
|
|
|
'user_nicename' => wpinv_utf8_substr( $user_nicename, 0, 50 ), |
405
|
|
|
'nickname' => $display_name, |
406
|
|
|
'display_name' => $display_name, |
407
|
|
|
); |
408
|
|
|
|
409
|
|
|
$userdata = apply_filters( 'wpinv_register_new_user_data', $userdata ); |
410
|
|
|
|
411
|
|
|
$new_user_id = wp_insert_user( $userdata ); |
412
|
|
|
|
413
|
|
|
if ( !is_wp_error( $new_user_id ) ) { |
414
|
|
|
$data['post_author'] = $new_user_id; |
415
|
|
|
$_POST['post_author'] = $new_user_id; |
416
|
|
|
$_POST['post_author_override'] = $new_user_id; |
417
|
|
|
|
418
|
|
|
$meta_fields = array( |
419
|
|
|
'first_name', |
420
|
|
|
'last_name', |
421
|
|
|
'company', |
422
|
|
|
'vat_number', |
423
|
|
|
///'email', |
424
|
|
|
'address', |
425
|
|
|
'city', |
426
|
|
|
'state', |
427
|
|
|
'country', |
428
|
|
|
'zip', |
429
|
|
|
'phone', |
430
|
|
|
); |
431
|
|
|
|
432
|
|
|
$meta = array(); |
433
|
|
|
///$meta['_wpinv_user_id'] = $new_user_id; |
|
|
|
|
434
|
|
|
foreach ( $meta_fields as $field ) { |
435
|
|
|
$meta['_wpinv_' . $field] = isset( $postarr['wpinv_' . $field] ) ? sanitize_text_field( $postarr['wpinv_' . $field] ) : ''; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$meta = apply_filters( 'wpinv_register_new_user_meta', $meta, $new_user_id ); |
439
|
|
|
|
440
|
|
|
// Update user meta. |
441
|
|
|
foreach ( $meta as $key => $value ) { |
442
|
|
|
update_user_meta( $new_user_id, $key, $value ); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
if ( function_exists( 'wp_send_new_user_notifications' ) ) { |
446
|
|
|
// Send email notifications related to the creation of new user. |
447
|
|
|
wp_send_new_user_notifications( $new_user_id, 'user' ); |
448
|
|
|
} |
449
|
|
|
} else { |
450
|
|
|
wpinv_error_log( $new_user_id->get_error_message(), 'Invoice add new user', __FILE__, __LINE__ ); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return $data; |
456
|
|
|
} |
457
|
|
|
add_filter( 'wp_insert_post_data', 'wpinv_send_register_new_user', 10, 2 ); |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.