Passed
Push — master ( 98ef57...3b6696 )
by Brian
05:27
created

getpaid_setup_buddypress_integration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
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
    ob_start();
204
    ?>
205
        <div class="bsui">
206
            <?php wpinv_print_errors(); ?>
207
            <form method="post" class="getpaid-address-edit-form">
208
209
                <?php
210
211
                    foreach ( getpaid_user_address_fields() as $key => $label ) {
212
213
                        // Display the country.
214
                        if ( 'country' == $key ) {
215
216
                            echo aui()->select(
217
                                array(
218
                                    'options'     => wpinv_get_country_list(),
219
                                    'name'        => esc_attr( $key ),
220
                                    'id'          => 'wpinv-' . sanitize_html_class( $key ),
221
                                    'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
222
                                    'placeholder' => $label,
223
                                    'label'       => wp_kses_post( $label ),
224
                                    'label_type'  => 'vertical',
225
                                    'class'       => 'getpaid-address-field',
226
                                )
227
                            );
228
229
                        }
230
231
                        // Display the state.
232
                        else if ( 'state' == $key ) {
233
234
                            echo getpaid_get_states_select_markup (
235
                                getpaid_get_user_address_field( get_current_user_id(), 'country' ),
236
                                getpaid_get_user_address_field( get_current_user_id(), 'state' ),
237
                                $label,
238
                                $label,
239
                                '',
240
                                false,
241
                                '',
242
                                $key
243
                            );
244
245
                        } else {
246
247
                            echo aui()->input(
248
                                array(
249
                                    'name'        => esc_attr( $key ),
250
                                    'id'          => 'wpinv-' . sanitize_html_class( $key ),
251
                                    'placeholder' => $label,
252
                                    'label'       => wp_kses_post( $label ),
253
                                    'label_type'  => 'vertical',
254
                                    'type'        => 'text',
255
                                    'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
256
                                    'class'       => 'getpaid-address-field',
257
                                )
258
                            );
259
260
                        }
261
262
                    }
263
264
                    do_action( 'getpaid_display_address_edit_tab' );
265
266
                    echo aui()->input(
267
                        array(
268
                            'name'             => 'getpaid_profile_edit_submit_button',
269
                            'id'               => 'getpaid_profile_edit_submit_button',
270
                            'value'            => __( 'Save Address', 'invoicing' ),
271
                            'help_text'        => __( 'New invoices will use this address as the billing address.', 'invoicing' ),
272
                            'type'             => 'submit',
273
                            'class'            => 'btn btn-primary btn-block submit-button',
274
                        )
275
                    );
276
277
                    wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' );
278
                    getpaid_hidden_field( 'getpaid-action', 'edit_billing_details' );
279
                ?>
280
281
            </form>
282
283
        </div>
284
    <?php
285
286
    return ob_get_clean();
287
}
288
289
/**
290
 * Saves the billing address edit tab.
291
 *
292
 * @since 2.1.4
293
 * @param array $data
294
 */
295
function getpaid_save_address_edit_tab( $data ) {
296
297
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
298
299
        if ( isset( $data[ $field ] ) ) {
300
            $value = sanitize_text_field( $data[ $field ] );
301
            update_user_meta( get_current_user_id(), '_wpinv_' . $field, $value );
302
        }
303
304
        wpinv_set_error( 'address_updated', __( 'You billing address has been updated', 'invoicing' ), 'success');
305
    }
306
307
}
308
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' );
309
310
311
/*
312
 |--------------------------------------------------------------------------
313
 | UsersWP
314
 |--------------------------------------------------------------------------
315
 |
316
 | Functions that integrate GetPaid and UsersWP.
317
*/
318
319
/**
320
 * Add our tabs to UsersWP account tabs.
321
 *
322
 * @since 1.0.19
323
 * @param  array $tabs
324
 * @return array
325
 */
326
function getpaid_filter_userswp_account_tabs( $tabs ) {
327
328
    // Abort if the integration is inactive.
329
    if ( ! getpaid_is_userswp_integration_active() ) {
330
        return $tabs;
331
    }
332
333
    $new_tabs   = array();
334
335
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
336
337
        $new_tabs[ $slug ] = array(
338
            'title' => $tab[ 'label'],
339
            'icon'  =>  $tab[ 'icon'],
340
        );
341
342
    }
343
344
    return array_merge( $tabs, $new_tabs );
345
}
346
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
347
348
/**
349
 * Display our UsersWP account tabs.
350
 *
351
 * @since 1.0.19
352
 * @param  array $tabs
353
 * @return array
354
 */
355
function getpaid_display_userswp_account_tabs( $tab ) {
356
    global $getpaid_tab_url;
357
358
    $our_tabs = getpaid_get_user_content_tabs();
359
360
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
361
        $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

361
        $getpaid_tab_url = add_query_arg( 'type', '%s', /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
362
        echo getpaid_prepare_user_content_tab( $our_tabs[ $tab ] );
0 ignored issues
show
Bug introduced by
Are you sure getpaid_prepare_user_content_tab($our_tabs[$tab]) of type array can be used in echo? ( Ignorable by Annotation )

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

362
        echo /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] );
Loading history...
363
    }
364
365
}
366
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
367
368
369
/**
370
 * Filters the account page title.
371
 *
372
 * @since  1.0.19
373
 * @param  string $title Current title.
374
 * @param  string $tab   Current tab.
375
 * @return string Title.
376
 */
377
function getpaid_filter_userswp_account_title( $title, $tab ) {
378
379
    $our_tabs   = getpaid_get_user_content_tabs();
380
381
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
382
        return $our_tabs[ $tab ]['label'];
383
    }
384
385
    return $title;
386
}
387
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
388
389
/**
390
 * Registers the UsersWP integration settings.
391
 *
392
 * @since  1.0.19
393
 * @param  array $settings An array of integration settings.
394
 * @return array
395
 */
396
function getpaid_register_userswp_settings( $settings ) {
397
398
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
399
400
        $settings[] = array(
401
402
            'id'       => 'userswp',
403
            'label'    => __( 'UsersWP', 'invoicing' ),
404
            'settings' => array(
405
406
                'userswp_settings' => array(
407
                    'id'   => 'userswp_settings',
408
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
409
                    'type' => 'header',
410
                ),
411
412
                'enable_userswp' => array(
413
                    'id'         => 'enable_userswp',
414
                    'name'       => __( 'Enable Integration', 'invoicing' ),
415
                    'desc'       => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
416
                    'type'       => 'checkbox',
417
                    'std'        => 1,
418
                )
419
420
            )
421
422
        );
423
424
    }
425
426
    return $settings;
427
}
428
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
429
430
/**
431
 * Checks if the integration is enabled.
432
 *
433
 * @since  1.0.19
434
 * @return bool
435
 */
436
function getpaid_is_userswp_integration_active() {
437
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
438
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
439
}
440
441
/*
442
 |--------------------------------------------------------------------------
443
 | BuddyPress
444
 |--------------------------------------------------------------------------
445
 |
446
 | Functions that integrate GetPaid and BuddyPress.
447
*/
448
449
/**
450
 * Registers the BuddyPress integration settings.
451
 *
452
 * @since  2.1.5
453
 * @param  array $settings An array of integration settings.
454
 * @return array
455
 */
456
function getpaid_register_buddypress_settings( $settings ) {
457
458
    if ( class_exists( 'BuddyPress' ) ) {
459
460
        $settings[] = array(
461
462
            'id'       => 'buddypress',
463
            'label'    => __( 'BuddyPress', 'invoicing' ),
464
            'settings' => array(
465
466
                'buddypress_settings' => array(
467
                    'id'   => 'buddypress_settings',
468
                    'name' => '<h3>' . __( 'BuddyPress', 'invoicing' ) . '</h3>',
469
                    'type' => 'header',
470
                ),
471
472
                'enable_buddypress' => array(
473
                    'id'         => 'enable_buddypress',
474
                    'name'       => __( 'Enable Integration', 'invoicing' ),
475
                    'desc'       => __( 'Display GetPaid items on BuddyPress account pages.', 'invoicing' ),
476
                    'type'       => 'checkbox',
477
                    'std'        => 1,
478
                )
479
480
            )
481
482
        );
483
484
    }
485
486
    return $settings;
487
}
488
add_filter( 'getpaid_integration_settings', 'getpaid_register_buddypress_settings' );
489
490
/**
491
 * Checks if the integration is enabled.
492
 *
493
 * @since  2.1.5
494
 * @return bool
495
 */
496
function getpaid_is_buddypress_integration_active() {
497
    $enabled = wpinv_get_option( 'enable_buddypress', 1 );
498
    return class_exists( 'BuddyPress' ) && ! empty( $enabled );
499
}
500
501
/**
502
 * Loads the BuddyPress component.
503
 *
504
 * @since  2.1.5
505
 * @return bool
506
 */
507
function getpaid_setup_buddypress_integration() {
508
509
    if ( getpaid_is_buddypress_integration_active() ) {
510
        require_once( WPINV_PLUGIN_DIR . 'includes/class-bp-getpaid-component.php' );
511
        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

511
        /** @scrutinizer ignore-call */ 
512
        buddypress()->getpaid = new BP_GetPaid_Component();
Loading history...
512
    }
513
514
}
515
add_action( 'bp_setup_components', 'getpaid_setup_buddypress_integration' );
516