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