1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains all user related functions. |
4
|
|
|
* |
5
|
|
|
* @since 1.0.0 |
6
|
|
|
* @package GetPaid |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
defined( 'ABSPATH' ) || exit; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generates a users select dropdown. |
13
|
|
|
* |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
* @return string|void Users dropdown markup. |
16
|
|
|
* @param array $args |
17
|
|
|
* @see wp_dropdown_users |
18
|
|
|
*/ |
19
|
|
|
function wpinv_dropdown_users( $args = '' ) { |
20
|
|
|
|
21
|
|
|
if ( is_array( $args ) && ! empty( $args['show'] ) && 'display_name_with_email' == $args['show'] ) { |
22
|
|
|
$args['show'] = 'display_name_with_login'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
return wp_dropdown_users( $args ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the appropriate capability to check against |
30
|
|
|
* |
31
|
|
|
* @since 1.0.13 |
32
|
|
|
* @return string capability to check against |
33
|
|
|
* @param string $capalibilty Optional. The alternative capability to check against. |
34
|
|
|
*/ |
35
|
|
|
function wpinv_get_capability( $capalibilty = 'manage_invoicing' ) { |
36
|
|
|
|
37
|
|
|
if ( current_user_can( 'manage_options' ) ) { |
38
|
|
|
return 'manage_options'; |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
return $capalibilty; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Checks if the current user can manager invoicing |
46
|
|
|
* |
47
|
|
|
* @since 1.0.13 |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
function wpinv_current_user_can_manage_invoicing() { |
51
|
|
|
return current_user_can( wpinv_get_capability() ); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns whether the current user has the specified getpaid capability. |
56
|
|
|
* |
57
|
|
|
* @since 2.7.8 |
58
|
|
|
* |
59
|
|
|
* @param string $capability Capability name. |
60
|
|
|
* @param mixed $args Optional further parameters, typically starting with an object. |
61
|
|
|
* @return bool Whether the current user has the given capability. |
62
|
|
|
*/ |
63
|
|
|
function wpinv_current_user_can( $capability, $args = array() ) { |
64
|
|
|
$can = wpinv_current_user_can_manage_invoicing(); |
65
|
|
|
|
66
|
|
|
return apply_filters( 'getpaid_current_user_can', $can, $capability, $args ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Given an email address, it creates a new user. |
71
|
|
|
* |
72
|
|
|
* @since 1.0.19 |
73
|
|
|
* @return int|WP_Error |
74
|
|
|
*/ |
75
|
|
|
function wpinv_create_user( $email, $prefix = '' ) { |
76
|
|
|
|
77
|
|
|
// Prepare user values. |
78
|
|
|
$prefix = preg_replace( '/\s+/', '', $prefix ); |
79
|
|
|
$prefix = empty( $prefix ) ? $email : $prefix; |
80
|
|
|
$args = array( |
81
|
|
|
'user_login' => wpinv_generate_user_name( $prefix ), |
82
|
|
|
'user_pass' => wp_generate_password(), |
83
|
|
|
'user_email' => $email, |
84
|
|
|
'role' => 'subscriber', |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return wp_insert_user( $args ); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Generates a unique user name from an email. |
93
|
|
|
* |
94
|
|
|
* @since 1.0.19 |
95
|
|
|
* @return bool|WP_User |
96
|
|
|
*/ |
97
|
|
|
function wpinv_generate_user_name( $prefix = '' ) { |
98
|
|
|
|
99
|
|
|
// If prefix is an email, retrieve the part before the email. |
100
|
|
|
$prefix = strtok( $prefix, '@' ); |
101
|
|
|
$prefix = trim( $prefix, '.' ); |
102
|
|
|
|
103
|
|
|
// Sanitize the username. |
104
|
|
|
$prefix = sanitize_user( $prefix, true ); |
105
|
|
|
|
106
|
|
|
$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); |
107
|
|
|
if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) { |
108
|
|
|
$prefix = 'gtp_' . zeroise( wp_rand( 0, 9999 ), 4 ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$username = $prefix; |
112
|
|
|
$postfix = 2; |
113
|
|
|
|
114
|
|
|
while ( username_exists( $username ) ) { |
115
|
|
|
$username = "{$prefix}{$postfix}"; |
116
|
|
|
$postfix ++; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $username; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns an array of user content tabs. |
124
|
|
|
* |
125
|
|
|
* @since 1.0.19 |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
function getpaid_get_user_content_tabs() { |
129
|
|
|
|
130
|
|
|
$tabs = array( |
131
|
|
|
|
132
|
|
|
'gp-invoices' => array( |
133
|
|
|
'label' => __( 'Invoices', 'invoicing' ), // Name of the tab. |
134
|
|
|
'content' => '[wpinv_history]', // Content of the tab. Or specify "callback" to provide a callback instead. |
135
|
|
|
'icon' => 'fas fa-file-invoice', // Shown on some profile plugins. |
136
|
|
|
), |
137
|
|
|
|
138
|
|
|
'gp-subscriptions' => array( |
139
|
|
|
'label' => __( 'Subscriptions', 'invoicing' ), |
140
|
|
|
'content' => '[wpinv_subscriptions]', |
141
|
|
|
'icon' => 'fas fa-redo', |
142
|
|
|
), |
143
|
|
|
|
144
|
|
|
'gp-edit-address' => array( |
145
|
|
|
'label' => __( 'Billing Address', 'invoicing' ), |
146
|
|
|
'callback' => 'getpaid_display_address_edit_tab', |
147
|
|
|
'icon' => 'fas fa-credit-card', |
148
|
|
|
), |
149
|
|
|
|
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$tabs = apply_filters( 'getpaid_user_content_tabs', $tabs ); |
153
|
|
|
|
154
|
|
|
// Make sure address editing is last on the list. |
155
|
|
|
if ( isset( $tabs['gp-edit-address'] ) ) { |
156
|
|
|
$address = $tabs['gp-edit-address']; |
157
|
|
|
unset( $tabs['gp-edit-address'] ); |
158
|
|
|
$tabs['gp-edit-address'] = $address; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $tabs; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Prepares the contents of a tab. |
166
|
|
|
* |
167
|
|
|
* @since 1.0.19 |
168
|
|
|
* @param array $tab |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
function getpaid_prepare_user_content_tab( $tab ) { |
172
|
|
|
|
173
|
|
|
if ( ! empty( $tab['callback'] ) ) { |
174
|
|
|
return call_user_func( $tab['callback'] ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ( ! empty( $tab['content'] ) ) { |
178
|
|
|
return convert_smilies( capital_P_dangit( wp_filter_content_tags( do_shortcode( shortcode_unautop( wpautop( wptexturize( do_blocks( $tab['content'] ) ) ) ) ) ) ) ); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$notice = aui()->alert( |
182
|
|
|
array( |
183
|
|
|
'content' => __( 'This tab has no content or content callback.', 'invoicing' ), |
184
|
|
|
'type' => 'error', |
185
|
|
|
) |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
return "<div class='bsui'>$notice</div>"; |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Generates the current integrations tab URL. |
193
|
|
|
* |
194
|
|
|
* @since 1.0.19 |
195
|
|
|
* @param string $tab |
196
|
|
|
* @param string $default |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
function getpaid_get_tab_url( $tab, $default ) { |
200
|
|
|
global $getpaid_tab_url; |
201
|
|
|
|
202
|
|
|
if ( empty( $getpaid_tab_url ) ) { |
203
|
|
|
return $default; |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return sprintf( $getpaid_tab_url, $tab ); |
|
|
|
|
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Generates the address edit tab. |
212
|
|
|
* |
213
|
|
|
* @since 2.1.4 |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
|
|
function getpaid_display_address_edit_tab() { |
217
|
|
|
|
218
|
|
|
if ( 0 === get_current_user_id() ) { |
219
|
|
|
return '<div class="bsui">' . aui()->alert( |
220
|
|
|
array( |
221
|
|
|
'type' => 'error', |
222
|
|
|
'content' => __( 'Your must be logged in to view this section', 'invoicing' ), |
223
|
|
|
'dismissible' => false, |
224
|
|
|
) |
225
|
|
|
) . '</div>'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
ob_start(); |
229
|
|
|
?> |
230
|
|
|
<div class="bsui"> |
231
|
|
|
<?php wpinv_print_errors(); ?> |
232
|
|
|
<form method="post" class="getpaid-address-edit-form"> |
233
|
|
|
|
234
|
|
|
<?php |
235
|
|
|
|
236
|
|
|
foreach ( getpaid_user_address_fields() as $key => $label ) { |
237
|
|
|
|
238
|
|
|
// Display the country. |
239
|
|
|
if ( 'country' == $key ) { |
240
|
|
|
|
241
|
|
|
aui()->select( |
242
|
|
|
array( |
243
|
|
|
'options' => wpinv_get_country_list(), |
244
|
|
|
'name' => 'getpaid_address[' . esc_attr( $key ) . ']', |
245
|
|
|
'id' => 'wpinv-' . sanitize_html_class( $key ), |
246
|
|
|
'value' => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ), |
247
|
|
|
'placeholder' => $label, |
248
|
|
|
'label' => wp_kses_post( $label ), |
249
|
|
|
'label_type' => 'vertical', |
250
|
|
|
'class' => 'getpaid-address-field', |
251
|
|
|
), |
252
|
|
|
true |
253
|
|
|
); |
254
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// Display the state. |
258
|
|
|
elseif ( 'state' == $key ) { |
259
|
|
|
|
260
|
|
|
getpaid_get_states_select_markup( |
261
|
|
|
getpaid_get_user_address_field( get_current_user_id(), 'country' ), |
262
|
|
|
getpaid_get_user_address_field( get_current_user_id(), 'state' ), |
263
|
|
|
$label, |
264
|
|
|
$label, |
265
|
|
|
'', |
266
|
|
|
false, |
267
|
|
|
'', |
268
|
|
|
'getpaid_address[' . esc_attr( $key ) . ']', |
269
|
|
|
true |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
} else { |
273
|
|
|
|
274
|
|
|
aui()->input( |
275
|
|
|
array( |
276
|
|
|
'name' => 'getpaid_address[' . esc_attr( $key ) . ']', |
277
|
|
|
'id' => 'wpinv-' . sanitize_html_class( $key ), |
278
|
|
|
'placeholder' => $label, |
279
|
|
|
'label' => wp_kses_post( $label ), |
280
|
|
|
'label_type' => 'vertical', |
281
|
|
|
'type' => 'text', |
282
|
|
|
'value' => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ), |
283
|
|
|
'class' => 'getpaid-address-field', |
284
|
|
|
), |
285
|
|
|
true |
286
|
|
|
); |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
aui()->input( |
292
|
|
|
array( |
293
|
|
|
'name' => 'getpaid_address[email_cc]', |
294
|
|
|
'id' => 'wpinv-email_cc', |
295
|
|
|
'placeholder' => '[email protected], [email protected]', |
296
|
|
|
'label' => __( 'Other email addresses', 'invoicing' ), |
297
|
|
|
'label_type' => 'vertical', |
298
|
|
|
'type' => 'text', |
299
|
|
|
'value' => sanitize_text_field( get_user_meta( get_current_user_id(), '_wpinv_email_cc', true ) ), |
|
|
|
|
300
|
|
|
'class' => 'getpaid-address-field', |
301
|
|
|
'help_text' => __( 'Optionally provide other email addresses where we should send payment notifications', 'invoicing' ), |
302
|
|
|
), |
303
|
|
|
true |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
do_action( 'getpaid_display_address_edit_tab' ); |
307
|
|
|
|
308
|
|
|
aui()->input( |
309
|
|
|
array( |
310
|
|
|
'name' => 'getpaid_profile_edit_submit_button', |
311
|
|
|
'id' => 'getpaid_profile_edit_submit_button', |
312
|
|
|
'value' => __( 'Save Address', 'invoicing' ), |
313
|
|
|
'help_text' => __( 'New invoices will use this address as the billing address.', 'invoicing' ), |
314
|
|
|
'type' => 'submit', |
315
|
|
|
'class' => 'btn btn-primary btn-block submit-button', |
316
|
|
|
), |
317
|
|
|
true |
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' ); |
321
|
|
|
getpaid_hidden_field( 'getpaid-action', 'edit_billing_details' ); |
322
|
|
|
?> |
323
|
|
|
|
324
|
|
|
</form> |
325
|
|
|
|
326
|
|
|
</div> |
327
|
|
|
<?php |
328
|
|
|
|
329
|
|
|
return ob_get_clean(); |
330
|
|
|
} |
331
|
|
|
add_shortcode( 'getpaid_edit_address', 'getpaid_display_address_edit_tab' ); |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Saves the billing address edit tab. |
335
|
|
|
* |
336
|
|
|
* @since 2.1.4 |
337
|
|
|
* @param array $data |
338
|
|
|
*/ |
339
|
|
|
function getpaid_save_address_edit_tab( $data ) { |
340
|
|
|
|
341
|
|
|
if ( empty( $data['getpaid_address'] ) || ! is_array( $data['getpaid_address'] ) ) { |
342
|
|
|
return; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$data = $data['getpaid_address']; |
346
|
|
|
$user_id = get_current_user_id(); |
347
|
|
|
|
348
|
|
|
foreach ( array_keys( getpaid_user_address_fields() ) as $field ) { |
349
|
|
|
|
350
|
|
|
if ( isset( $data[ $field ] ) ) { |
351
|
|
|
$value = sanitize_text_field( $data[ $field ] ); |
352
|
|
|
update_user_meta( $user_id, '_wpinv_' . $field, $value ); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if ( isset( $data['email_cc'] ) ) { |
357
|
|
|
update_user_meta( $user_id, '_wpinv_email_cc', sanitize_text_field( $data['email_cc'] ) ); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
wpinv_set_error( 'address_updated' ); |
361
|
|
|
} |
362
|
|
|
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' ); |
363
|
|
|
|
364
|
|
|
|
365
|
|
|
/* |
366
|
|
|
|-------------------------------------------------------------------------- |
367
|
|
|
| UsersWP |
368
|
|
|
|-------------------------------------------------------------------------- |
369
|
|
|
| |
370
|
|
|
| Functions that integrate GetPaid and UsersWP. |
371
|
|
|
*/ |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Add our tabs to UsersWP account tabs. |
375
|
|
|
* |
376
|
|
|
* @since 1.0.19 |
377
|
|
|
* @param array $tabs |
378
|
|
|
* @return array |
379
|
|
|
*/ |
380
|
|
|
function getpaid_filter_userswp_account_tabs( $tabs ) { |
381
|
|
|
|
382
|
|
|
// Abort if the integration is inactive. |
383
|
|
|
if ( ! getpaid_is_userswp_integration_active() ) { |
384
|
|
|
return $tabs; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$new_tabs = array(); |
388
|
|
|
|
389
|
|
|
foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) { |
390
|
|
|
|
391
|
|
|
$new_tabs[ $slug ] = array( |
392
|
|
|
'title' => $tab['label'], |
393
|
|
|
'icon' => $tab['icon'], |
394
|
|
|
); |
395
|
|
|
|
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return array_merge( $tabs, $new_tabs ); |
399
|
|
|
} |
400
|
|
|
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' ); |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Display our UsersWP account tabs. |
404
|
|
|
* |
405
|
|
|
* @since 1.0.19 |
406
|
|
|
* @param array $tabs |
407
|
|
|
* @return array |
408
|
|
|
*/ |
409
|
|
|
function getpaid_display_userswp_account_tabs( $tab ) { |
410
|
|
|
global $getpaid_tab_url; |
411
|
|
|
|
412
|
|
|
$our_tabs = getpaid_get_user_content_tabs(); |
413
|
|
|
|
414
|
|
|
if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) { |
415
|
|
|
$getpaid_tab_url = add_query_arg( 'type', '%s', uwp_get_account_page_url() ); |
|
|
|
|
416
|
|
|
echo wp_kses( getpaid_prepare_user_content_tab( $our_tabs[ $tab ] ), getpaid_allowed_html() ); |
|
|
|
|
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
} |
420
|
|
|
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' ); |
421
|
|
|
|
422
|
|
|
function getpaid_allowed_html() { |
423
|
|
|
$allowed_html = wp_kses_allowed_html( 'post' ); |
424
|
|
|
|
425
|
|
|
// form fields |
426
|
|
|
$allowed_html['form'] = array( |
427
|
|
|
'action' => true, |
428
|
|
|
'accept' => true, |
429
|
|
|
'accept-charset' => true, |
430
|
|
|
'enctype' => true, |
431
|
|
|
'method' => true, |
432
|
|
|
'name' => true, |
433
|
|
|
'target' => true, |
434
|
|
|
); |
435
|
|
|
|
436
|
|
|
// - input |
437
|
|
|
$allowed_html['input'] = array( |
438
|
|
|
'class' => array(), |
439
|
|
|
'id' => array(), |
440
|
|
|
'name' => array(), |
441
|
|
|
'value' => array(), |
442
|
|
|
'type' => array(), |
443
|
|
|
'placeholder' => array(), |
444
|
|
|
'autocomplete' => array(), |
445
|
|
|
'autofocus' => array(), |
446
|
|
|
'required' => array(), |
447
|
|
|
'disabled' => array(), |
448
|
|
|
'readonly' => array(), |
449
|
|
|
'checked' => array(), |
450
|
|
|
'maxlength' => array(), |
451
|
|
|
'pattern' => array(), |
452
|
|
|
'min' => array(), |
453
|
|
|
'max' => array(), |
454
|
|
|
'step' => array(), |
455
|
|
|
'size' => array(), |
456
|
|
|
); |
457
|
|
|
|
458
|
|
|
// - input |
459
|
|
|
$allowed_html['textarea'] = array( |
460
|
|
|
'class' => array(), |
461
|
|
|
'id' => array(), |
462
|
|
|
'name' => array(), |
463
|
|
|
'value' => array(), |
464
|
|
|
); |
465
|
|
|
|
466
|
|
|
// select |
467
|
|
|
$allowed_html['select'] = array( |
468
|
|
|
'class' => array(), |
469
|
|
|
'id' => array(), |
470
|
|
|
'name' => array(), |
471
|
|
|
'autocomplete' => array(), |
472
|
|
|
'multiple' => array(), |
473
|
|
|
); |
474
|
|
|
|
475
|
|
|
// select options |
476
|
|
|
$allowed_html['option'] = array( |
477
|
|
|
'selected' => array(), |
478
|
|
|
'disabled' => array(), |
479
|
|
|
'value' => array(), |
480
|
|
|
); |
481
|
|
|
|
482
|
|
|
return $allowed_html; |
483
|
|
|
|
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Filters the account page title. |
488
|
|
|
* |
489
|
|
|
* @since 1.0.19 |
490
|
|
|
* @param string $title Current title. |
491
|
|
|
* @param string $tab Current tab. |
492
|
|
|
* @return string Title. |
493
|
|
|
*/ |
494
|
|
|
function getpaid_filter_userswp_account_title( $title, $tab ) { |
495
|
|
|
|
496
|
|
|
$our_tabs = getpaid_get_user_content_tabs(); |
497
|
|
|
|
498
|
|
|
if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) { |
499
|
|
|
return $our_tabs[ $tab ]['label']; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
return $title; |
503
|
|
|
} |
504
|
|
|
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 ); |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Registers the UsersWP integration settings. |
508
|
|
|
* |
509
|
|
|
* @since 1.0.19 |
510
|
|
|
* @param array $settings An array of integration settings. |
511
|
|
|
* @return array |
512
|
|
|
*/ |
513
|
|
|
function getpaid_register_userswp_settings( $settings ) { |
514
|
|
|
|
515
|
|
|
if ( defined( 'USERSWP_PLUGIN_FILE' ) ) { |
516
|
|
|
|
517
|
|
|
$settings[] = array( |
518
|
|
|
|
519
|
|
|
'id' => 'userswp', |
520
|
|
|
'label' => __( 'UsersWP', 'invoicing' ), |
521
|
|
|
'settings' => array( |
522
|
|
|
|
523
|
|
|
'userswp_settings' => array( |
524
|
|
|
'id' => 'userswp_settings', |
525
|
|
|
'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>', |
526
|
|
|
'type' => 'header', |
527
|
|
|
), |
528
|
|
|
|
529
|
|
|
'enable_userswp' => array( |
530
|
|
|
'id' => 'enable_userswp', |
531
|
|
|
'name' => __( 'Enable Integration', 'invoicing' ), |
532
|
|
|
'desc' => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ), |
533
|
|
|
'type' => 'checkbox', |
534
|
|
|
'std' => 1, |
535
|
|
|
), |
536
|
|
|
|
537
|
|
|
), |
538
|
|
|
|
539
|
|
|
); |
540
|
|
|
|
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return $settings; |
544
|
|
|
} |
545
|
|
|
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' ); |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Ovewrites the invoices history page to UsersWP. |
549
|
|
|
* |
550
|
|
|
* @since 2.3.1 |
551
|
|
|
* @return bool |
552
|
|
|
*/ |
553
|
|
|
function getpaid_userswp_overwrite_invoice_history_page( $url, $post_type ) { |
554
|
|
|
|
555
|
|
|
$our_tabs = getpaid_get_user_content_tabs(); |
556
|
|
|
$tab = "gp-{$post_type}s"; |
557
|
|
|
if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) { |
558
|
|
|
return add_query_arg( 'type', $tab, uwp_get_account_page_url() ); |
|
|
|
|
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
return $url; |
562
|
|
|
|
563
|
|
|
} |
564
|
|
|
add_filter( 'wpinv_get_history_page_uri', 'getpaid_userswp_overwrite_invoice_history_page', 10, 2 ); |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Checks if the integration is enabled. |
568
|
|
|
* |
569
|
|
|
* @since 1.0.19 |
570
|
|
|
* @return bool |
571
|
|
|
*/ |
572
|
|
|
function getpaid_is_userswp_integration_active() { |
573
|
|
|
$enabled = wpinv_get_option( 'enable_userswp', 1 ); |
574
|
|
|
return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled ); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/* |
578
|
|
|
|-------------------------------------------------------------------------- |
579
|
|
|
| BuddyPress |
580
|
|
|
|-------------------------------------------------------------------------- |
581
|
|
|
| |
582
|
|
|
| Functions that integrate GetPaid and BuddyPress. |
583
|
|
|
*/ |
584
|
|
|
|
585
|
|
|
/** |
586
|
|
|
* Registers the BuddyPress integration settings. |
587
|
|
|
* |
588
|
|
|
* @since 2.1.5 |
589
|
|
|
* @param array $settings An array of integration settings. |
590
|
|
|
* @return array |
591
|
|
|
*/ |
592
|
|
|
function getpaid_register_buddypress_settings( $settings ) { |
593
|
|
|
|
594
|
|
|
if ( class_exists( 'BuddyPress' ) ) { |
595
|
|
|
|
596
|
|
|
$settings[] = array( |
597
|
|
|
|
598
|
|
|
'id' => 'buddypress', |
599
|
|
|
'label' => __( 'BuddyPress', 'invoicing' ), |
600
|
|
|
'settings' => array( |
601
|
|
|
|
602
|
|
|
'buddypress_settings' => array( |
603
|
|
|
'id' => 'buddypress_settings', |
604
|
|
|
'name' => '<h3>' . __( 'BuddyPress', 'invoicing' ) . '</h3>', |
605
|
|
|
'type' => 'header', |
606
|
|
|
), |
607
|
|
|
|
608
|
|
|
'enable_buddypress' => array( |
609
|
|
|
'id' => 'enable_buddypress', |
610
|
|
|
'name' => __( 'Enable Integration', 'invoicing' ), |
611
|
|
|
'desc' => __( 'Display GetPaid items on BuddyPress account pages.', 'invoicing' ), |
612
|
|
|
'type' => 'checkbox', |
613
|
|
|
'std' => 1, |
614
|
|
|
), |
615
|
|
|
|
616
|
|
|
), |
617
|
|
|
|
618
|
|
|
); |
619
|
|
|
|
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
return $settings; |
623
|
|
|
} |
624
|
|
|
add_filter( 'getpaid_integration_settings', 'getpaid_register_buddypress_settings' ); |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Checks if the integration is enabled. |
628
|
|
|
* |
629
|
|
|
* @since 2.1.5 |
630
|
|
|
* @return bool |
631
|
|
|
*/ |
632
|
|
|
function getpaid_is_buddypress_integration_active() { |
633
|
|
|
$enabled = wpinv_get_option( 'enable_buddypress', 1 ); |
634
|
|
|
return class_exists( 'BuddyPress' ) && ! empty( $enabled ); |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* Loads the BuddyPress component. |
639
|
|
|
* |
640
|
|
|
* @since 2.1.5 |
641
|
|
|
* @return bool |
642
|
|
|
*/ |
643
|
|
|
function getpaid_setup_buddypress_integration() { |
644
|
|
|
|
645
|
|
|
if ( getpaid_is_buddypress_integration_active() ) { |
646
|
|
|
require_once WPINV_PLUGIN_DIR . 'includes/class-bp-getpaid-component.php'; |
647
|
|
|
buddypress()->getpaid = new BP_GetPaid_Component(); |
|
|
|
|
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
} |
651
|
|
|
add_action( 'bp_setup_components', 'getpaid_setup_buddypress_integration' ); |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Checks if a given user has purchased a given item. |
655
|
|
|
* |
656
|
|
|
* @since 2.7.3 |
657
|
|
|
* @param int $item_id The item id. |
658
|
|
|
* @return int The IDs of users who purchased the item. |
659
|
|
|
*/ |
660
|
|
|
function getpaid_user_ids_who_purchased_item( $item_id ) { |
661
|
|
|
global $wpdb; |
662
|
|
|
|
663
|
|
|
if ( empty( $item_id ) ) { |
664
|
|
|
return false; |
|
|
|
|
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
$ids = $wpdb->get_col( |
668
|
|
|
$wpdb->prepare( |
669
|
|
|
"SELECT DISTINCT invoices.post_author FROM {$wpdb->prefix}getpaid_invoice_items AS items |
670
|
|
|
INNER JOIN {$wpdb->posts} AS invoices ON invoices.ID = items.post_id |
671
|
|
|
WHERE items.item_id = %d AND invoices.post_status = 'publish'", |
672
|
|
|
$item_id |
673
|
|
|
) |
674
|
|
|
); |
675
|
|
|
|
676
|
|
|
return wp_parse_id_list( $ids ); |
|
|
|
|
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
/** |
680
|
|
|
* Returns an array of user IDs who purchased a given item. |
681
|
|
|
* |
682
|
|
|
* @since 2.6.17 |
683
|
|
|
* @param int $user_id The user id. |
684
|
|
|
*/ |
685
|
|
|
function getpaid_has_user_purchased_item( $user_id, $item_id ) { |
686
|
|
|
global $wpdb; |
687
|
|
|
|
688
|
|
|
if ( empty( $user_id ) ) { |
689
|
|
|
return false; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
$count = $wpdb->get_var( |
693
|
|
|
$wpdb->prepare( |
694
|
|
|
"SELECT COUNT(*) FROM {$wpdb->prefix}getpaid_invoice_items AS items |
695
|
|
|
INNER JOIN {$wpdb->posts} AS invoices ON invoices.ID = items.post_id |
696
|
|
|
WHERE items.item_id = %d AND invoices.post_author = %d AND invoices.post_status = 'publish' |
697
|
|
|
LIMIT 1", |
698
|
|
|
$item_id, |
699
|
|
|
$user_id |
700
|
|
|
) |
701
|
|
|
); |
702
|
|
|
|
703
|
|
|
return ! empty( $count ); |
704
|
|
|
} |
705
|
|
|
|