Passed
Push — master ( ecf53c...107ebc )
by Brian
05:44
created

getpaid_allowed_html()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 0
dl 0
loc 38
rs 9.536
c 0
b 0
f 0
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
 *  Given an email address, it creates a new user.
56
 *
57
 * @since 1.0.19
58
 * @return int|WP_Error
59
 */
60
function wpinv_create_user( $email, $prefix = '' ) {
61
62
    // Prepare user values.
63
    $prefix = preg_replace( '/\s+/', '', $prefix );
64
    $prefix = empty( $prefix ) ? $email : $prefix;
65
	$args   = array(
66
		'user_login' => wpinv_generate_user_name( $prefix ),
67
		'user_pass'  => wp_generate_password(),
68
		'user_email' => $email,
69
        'role'       => 'subscriber',
70
    );
71
72
    return wp_insert_user( $args );
73
74
}
75
76
/**
77
 *  Generates a unique user name from an email.
78
 *
79
 * @since 1.0.19
80
 * @return bool|WP_User
81
 */
82
function wpinv_generate_user_name( $prefix = '' ) {
83
84
    // If prefix is an email, retrieve the part before the email.
85
	$prefix = strtok( $prefix, '@' );
86
    $prefix = trim( $prefix, '.' );
87
88
	// Sanitize the username.
89
	$prefix = sanitize_user( $prefix, true );
90
91
	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
92
	if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) {
93
		$prefix = 'gtp_' . zeroise( wp_rand( 0, 9999 ), 4 );
94
	}
95
96
    $username = $prefix;
97
    $postfix  = 2;
98
99
    while ( username_exists( $username ) ) {
100
        $username = $prefix + $postfix;
101
        $postfix ++;
102
    }
103
104
    return $username;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $username returns the type integer|string which is incompatible with the documented return type WP_User|boolean.
Loading history...
105
}
106
107
/**
108
 * Returns an array of user content tabs.
109
 *
110
 * @since 1.0.19
111
 * @return array
112
 */
113
function getpaid_get_user_content_tabs() {
114
115
    $tabs = array(
116
117
        'gp-invoices'      => array(
118
            'label'   => __( 'Invoices', 'invoicing' ), // Name of the tab.
119
            'content' => '[wpinv_history]', // Content of the tab. Or specify "callback" to provide a callback instead.
120
            'icon'    => 'fas fa-file-invoice', // Shown on some profile plugins.
121
        ),
122
123
        'gp-subscriptions' => array(
124
            'label'   => __( 'Subscriptions', 'invoicing' ),
125
            'content' => '[wpinv_subscriptions]',
126
            'icon'    => 'fas fa-redo',
127
        ),
128
129
        'gp-edit-address'  => array(
130
            'label'    => __( 'Billing Address', 'invoicing' ),
131
            'callback' => 'getpaid_display_address_edit_tab',
132
            'icon'     => 'fas fa-credit-card',
133
        ),
134
135
    );
136
137
    $tabs = apply_filters( 'getpaid_user_content_tabs', $tabs );
138
139
    // Make sure address editing is last on the list.
140
    if ( isset( $tabs['gp-edit-address'] ) ) {
141
        $address = $tabs['gp-edit-address'];
142
        unset( $tabs['gp-edit-address'] );
143
        $tabs['gp-edit-address'] = $address;
144
    }
145
146
    return $tabs;
147
}
148
149
/**
150
 * Prepares the contents of a tab.
151
 *
152
 * @since 1.0.19
153
 * @param array $tab
154
 * @return array
155
 */
156
function getpaid_prepare_user_content_tab( $tab ) {
157
158
    if ( ! empty( $tab['callback'] ) ) {
159
        return call_user_func( $tab['callback'] );
160
    }
161
162
    if ( ! empty( $tab['content'] ) ) {
163
        return convert_smilies( capital_P_dangit( wp_filter_content_tags( do_shortcode( shortcode_unautop( wpautop( wptexturize( do_blocks( $tab['content'] ) ) ) ) ) ) ) );
0 ignored issues
show
Bug Best Practice introduced by
The expression return convert_smilies(c...$tab['content'])))))))) returns the type string which is incompatible with the documented return type array.
Loading history...
164
    }
165
166
    $notice = aui()->alert(
167
        array(
168
            'content' => __( 'This tab has no content or content callback.', 'invoicing' ),
169
            'type'    => 'error',
170
        )
171
    );
172
173
    return "<div class='bsui'>$notice</div>";
0 ignored issues
show
Bug Best Practice introduced by
The expression return '<div class='bsui'>'.$notice.'</div>' returns the type string which is incompatible with the documented return type array.
Loading history...
174
}
175
176
/**
177
 * Generates the current integrations tab URL.
178
 *
179
 * @since 1.0.19
180
 * @param string $tab
181
 * @param string $default
182
 * @return array
183
 */
184
function getpaid_get_tab_url( $tab, $default ) {
185
    global $getpaid_tab_url;
186
187
    if ( empty( $getpaid_tab_url ) ) {
188
        return $default;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $default returns the type string which is incompatible with the documented return type array.
Loading history...
189
    }
190
191
    return sprintf( $getpaid_tab_url, $tab );
0 ignored issues
show
Bug Best Practice introduced by
The expression return sprintf($getpaid_tab_url, $tab) returns the type string which is incompatible with the documented return type array.
Loading history...
192
193
}
194
195
/**
196
 * Generates the address edit tab.
197
 *
198
 * @since 2.1.4
199
 * @return string
200
 */
201
function getpaid_display_address_edit_tab() {
202
203
    if ( 0 === get_current_user_id() ) {
204
        return '<div class="bsui">' . aui()->alert(
205
            array(
206
                'type'        => 'error',
207
                'content'     => __( 'Your must be logged in to view this section', 'invoicing' ),
208
                'dismissible' => false,
209
            )
210
        ) . '</div>';
211
    }
212
213
    ob_start();
214
    ?>
215
        <div class="bsui">
216
            <?php wpinv_print_errors(); ?>
217
            <form method="post" class="getpaid-address-edit-form">
218
219
                <?php
220
221
                    foreach ( getpaid_user_address_fields() as $key => $label ) {
222
223
					// Display the country.
224
					if ( 'country' == $key ) {
225
226
						aui()->select(
227
							array(
228
								'options'     => wpinv_get_country_list(),
229
								'name'        => 'getpaid_address[' . esc_attr( $key ) . ']',
230
								'id'          => 'wpinv-' . sanitize_html_class( $key ),
231
								'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
232
								'placeholder' => $label,
233
								'label'       => wp_kses_post( $label ),
234
								'label_type'  => 'vertical',
235
								'class'       => 'getpaid-address-field',
236
                            ),
237
                            true
238
						);
239
240
					}
241
242
					// Display the state.
243
					elseif ( 'state' == $key ) {
244
245
						getpaid_get_states_select_markup(
246
							getpaid_get_user_address_field( get_current_user_id(), 'country' ),
247
							getpaid_get_user_address_field( get_current_user_id(), 'state' ),
248
							$label,
249
							$label,
250
							'',
251
							false,
252
							'',
253
							'getpaid_address[' . esc_attr( $key ) . ']',
254
                            true
255
						);
256
257
                        } else {
258
259
						aui()->input(
260
                            array(
261
                                'name'        => 'getpaid_address[' . esc_attr( $key ) . ']',
262
                                'id'          => 'wpinv-' . sanitize_html_class( $key ),
263
                                'placeholder' => $label,
264
                                'label'       => wp_kses_post( $label ),
265
                                'label_type'  => 'vertical',
266
                                'type'        => 'text',
267
                                'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
268
                                'class'       => 'getpaid-address-field',
269
                            ),
270
                            true
271
						);
272
273
                        }
274
                    }
275
276
                    aui()->input(
277
                        array(
278
                            'name'        => 'getpaid_address[email_cc]',
279
                            'id'          => 'wpinv-email_cc',
280
                            'placeholder' => '[email protected], [email protected]',
281
                            'label'       => __( 'Other email addresses', 'invoicing' ),
282
                            'label_type'  => 'vertical',
283
                            'type'        => 'text',
284
                            'value'       => sanitize_text_field( get_user_meta( get_current_user_id(), '_wpinv_email_cc', true ) ),
0 ignored issues
show
Bug introduced by
It seems like get_user_meta(get_curren..._wpinv_email_cc', true) can also be of type false; however, parameter $str of sanitize_text_field() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

284
                            'value'       => sanitize_text_field( /** @scrutinizer ignore-type */ get_user_meta( get_current_user_id(), '_wpinv_email_cc', true ) ),
Loading history...
285
                            'class'       => 'getpaid-address-field',
286
                            'help_text'   => __( 'Optionally provide other email addresses where we should send payment notifications', 'invoicing' ),
287
                        ),
288
                        true
289
                    );
290
291
                    do_action( 'getpaid_display_address_edit_tab' );
292
293
                    aui()->input(
294
                        array(
295
                            'name'      => 'getpaid_profile_edit_submit_button',
296
                            'id'        => 'getpaid_profile_edit_submit_button',
297
                            'value'     => __( 'Save Address', 'invoicing' ),
298
                            'help_text' => __( 'New invoices will use this address as the billing address.', 'invoicing' ),
299
                            'type'      => 'submit',
300
                            'class'     => 'btn btn-primary btn-block submit-button',
301
                        ),
302
                        true
303
                    );
304
305
                    wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' );
306
                    getpaid_hidden_field( 'getpaid-action', 'edit_billing_details' );
307
                ?>
308
309
            </form>
310
311
        </div>
312
    <?php
313
314
    return ob_get_clean();
315
}
316
add_shortcode( 'getpaid_edit_address', 'getpaid_display_address_edit_tab' );
317
318
/**
319
 * Saves the billing address edit tab.
320
 *
321
 * @since 2.1.4
322
 * @param array $data
323
 */
324
function getpaid_save_address_edit_tab( $data ) {
325
326
    if ( empty( $data['getpaid_address'] ) || ! is_array( $data['getpaid_address'] ) ) {
327
        return;
328
    }
329
330
    $data    = $data['getpaid_address'];
331
    $user_id = get_current_user_id();
332
333
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
334
335
        if ( isset( $data[ $field ] ) ) {
336
            $value = sanitize_text_field( $data[ $field ] );
337
            update_user_meta( $user_id, '_wpinv_' . $field, $value );
338
        }
339
}
340
341
    if ( isset( $data['email_cc'] ) ) {
342
        update_user_meta( $user_id, '_wpinv_email_cc', sanitize_text_field( $data['email_cc'] ) );
343
    }
344
345
    wpinv_set_error( 'address_updated', __( 'Your billing address has been updated', 'invoicing' ), 'success' );
346
}
347
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' );
348
349
350
/*
351
 |--------------------------------------------------------------------------
352
 | UsersWP
353
 |--------------------------------------------------------------------------
354
 |
355
 | Functions that integrate GetPaid and UsersWP.
356
*/
357
358
/**
359
 * Add our tabs to UsersWP account tabs.
360
 *
361
 * @since 1.0.19
362
 * @param  array $tabs
363
 * @return array
364
 */
365
function getpaid_filter_userswp_account_tabs( $tabs ) {
366
367
    // Abort if the integration is inactive.
368
    if ( ! getpaid_is_userswp_integration_active() ) {
369
        return $tabs;
370
    }
371
372
    $new_tabs   = array();
373
374
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
375
376
        $new_tabs[ $slug ] = array(
377
            'title' => $tab['label'],
378
            'icon'  => $tab['icon'],
379
        );
380
381
    }
382
383
    return array_merge( $tabs, $new_tabs );
384
}
385
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
386
387
/**
388
 * Display our UsersWP account tabs.
389
 *
390
 * @since 1.0.19
391
 * @param  array $tabs
392
 * @return array
393
 */
394
function getpaid_display_userswp_account_tabs( $tab ) {
395
    global $getpaid_tab_url;
396
397
    $our_tabs = getpaid_get_user_content_tabs();
398
399
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
400
        $getpaid_tab_url = add_query_arg( 'type', '%s', uwp_get_account_page_url() );
0 ignored issues
show
Bug introduced by
The function uwp_get_account_page_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

400
        $getpaid_tab_url = add_query_arg( 'type', '%s', /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
401
        echo wp_kses( getpaid_prepare_user_content_tab( $our_tabs[ $tab ] ), getpaid_allowed_html() );
0 ignored issues
show
Bug introduced by
getpaid_prepare_user_content_tab($our_tabs[$tab]) of type array is incompatible with the type string expected by parameter $string of wp_kses(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

401
        echo wp_kses( /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] ), getpaid_allowed_html() );
Loading history...
402
    }
403
404
}
405
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
406
407
function getpaid_allowed_html() {
408
    $allowed_html = wp_kses_allowed_html( 'post' );
409
410
	// form fields
411
    $allowed_html['form'] = array(
412
        'action'         => true,
413
        'accept'         => true,
414
        'accept-charset' => true,
415
        'enctype'        => true,
416
        'method'         => true,
417
        'name'           => true,
418
        'target'         => true,
419
    );
420
    
421
    // - input
422
	$allowed_html['input'] = array(
423
		'class' => array(),
424
		'id'    => array(),
425
		'name'  => array(),
426
		'value' => array(),
427
		'type'  => array(),
428
	);
429
430
	// select
431
	$allowed_html['select'] = array(
432
		'class'  => array(),
433
		'id'     => array(),
434
		'name'   => array(),
435
		'value'  => array(),
436
		'type'   => array(),
437
	);
438
439
	// select options
440
	$allowed_html['option'] = array(
441
		'selected' => array(),
442
	);
443
444
	return $allowed_html;
445
446
}
447
448
/**
449
 * Filters the account page title.
450
 *
451
 * @since  1.0.19
452
 * @param  string $title Current title.
453
 * @param  string $tab   Current tab.
454
 * @return string Title.
455
 */
456
function getpaid_filter_userswp_account_title( $title, $tab ) {
457
458
    $our_tabs   = getpaid_get_user_content_tabs();
459
460
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
461
        return $our_tabs[ $tab ]['label'];
462
    }
463
464
    return $title;
465
}
466
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
467
468
/**
469
 * Registers the UsersWP integration settings.
470
 *
471
 * @since  1.0.19
472
 * @param  array $settings An array of integration settings.
473
 * @return array
474
 */
475
function getpaid_register_userswp_settings( $settings ) {
476
477
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
478
479
        $settings[] = array(
480
481
            'id'       => 'userswp',
482
            'label'    => __( 'UsersWP', 'invoicing' ),
483
            'settings' => array(
484
485
                'userswp_settings' => array(
486
                    'id'   => 'userswp_settings',
487
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
488
                    'type' => 'header',
489
                ),
490
491
                'enable_userswp'   => array(
492
                    'id'   => 'enable_userswp',
493
                    'name' => __( 'Enable Integration', 'invoicing' ),
494
                    'desc' => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
495
                    'type' => 'checkbox',
496
                    'std'  => 1,
497
                ),
498
499
            ),
500
501
        );
502
503
    }
504
505
    return $settings;
506
}
507
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
508
509
/**
510
 * Ovewrites the invoices history page to UsersWP.
511
 *
512
 * @since  2.3.1
513
 * @return bool
514
 */
515
function getpaid_userswp_overwrite_invoice_history_page( $url, $post_type ) {
516
517
    $our_tabs = getpaid_get_user_content_tabs();
518
    $tab      = "gp-{$post_type}s";
519
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
520
        return add_query_arg( 'type', $tab, uwp_get_account_page_url() );
0 ignored issues
show
Bug introduced by
The function uwp_get_account_page_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

520
        return add_query_arg( 'type', $tab, /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
Bug Best Practice introduced by
The expression return add_query_arg('ty...get_account_page_url()) returns the type string which is incompatible with the documented return type boolean.
Loading history...
521
    }
522
523
    return $url;
524
525
}
526
add_filter( 'wpinv_get_history_page_uri', 'getpaid_userswp_overwrite_invoice_history_page', 10, 2 );
527
528
/**
529
 * Checks if the integration is enabled.
530
 *
531
 * @since  1.0.19
532
 * @return bool
533
 */
534
function getpaid_is_userswp_integration_active() {
535
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
536
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
537
}
538
539
/*
540
 |--------------------------------------------------------------------------
541
 | BuddyPress
542
 |--------------------------------------------------------------------------
543
 |
544
 | Functions that integrate GetPaid and BuddyPress.
545
*/
546
547
/**
548
 * Registers the BuddyPress integration settings.
549
 *
550
 * @since  2.1.5
551
 * @param  array $settings An array of integration settings.
552
 * @return array
553
 */
554
function getpaid_register_buddypress_settings( $settings ) {
555
556
    if ( class_exists( 'BuddyPress' ) ) {
557
558
        $settings[] = array(
559
560
            'id'       => 'buddypress',
561
            'label'    => __( 'BuddyPress', 'invoicing' ),
562
            'settings' => array(
563
564
                'buddypress_settings' => array(
565
                    'id'   => 'buddypress_settings',
566
                    'name' => '<h3>' . __( 'BuddyPress', 'invoicing' ) . '</h3>',
567
                    'type' => 'header',
568
                ),
569
570
                'enable_buddypress'   => array(
571
                    'id'   => 'enable_buddypress',
572
                    'name' => __( 'Enable Integration', 'invoicing' ),
573
                    'desc' => __( 'Display GetPaid items on BuddyPress account pages.', 'invoicing' ),
574
                    'type' => 'checkbox',
575
                    'std'  => 1,
576
                ),
577
578
            ),
579
580
        );
581
582
    }
583
584
    return $settings;
585
}
586
add_filter( 'getpaid_integration_settings', 'getpaid_register_buddypress_settings' );
587
588
/**
589
 * Checks if the integration is enabled.
590
 *
591
 * @since  2.1.5
592
 * @return bool
593
 */
594
function getpaid_is_buddypress_integration_active() {
595
    $enabled = wpinv_get_option( 'enable_buddypress', 1 );
596
    return class_exists( 'BuddyPress' ) && ! empty( $enabled );
597
}
598
599
/**
600
 * Loads the BuddyPress component.
601
 *
602
 * @since  2.1.5
603
 * @return bool
604
 */
605
function getpaid_setup_buddypress_integration() {
606
607
    if ( getpaid_is_buddypress_integration_active() ) {
608
        require_once WPINV_PLUGIN_DIR . 'includes/class-bp-getpaid-component.php';
609
        buddypress()->getpaid = new BP_GetPaid_Component();
0 ignored issues
show
Bug introduced by
The function buddypress was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

609
        /** @scrutinizer ignore-call */ 
610
        buddypress()->getpaid = new BP_GetPaid_Component();
Loading history...
610
    }
611
612
}
613
add_action( 'bp_setup_components', 'getpaid_setup_buddypress_integration' );
614