Passed
Push — master ( 0b939f...8c5c54 )
by Brian
24:09
created

getpaid_userswp_overwrite_invoice_history_page()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
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
    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'        => 'getpaid_address[' . 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
                                'getpaid_address[' . esc_attr( $key ) . ']'
243
                            );
244
245
                        } else {
246
247
                            echo aui()->input(
248
                                array(
249
                                    'name'        => 'getpaid_address[' . 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
    if ( empty( $data['getpaid_address'] ) || ! is_array( $data['getpaid_address'] ) ) {
298
        return;
299
    }
300
301
    $data    = $data['getpaid_address'];
302
    $user_id = get_current_user_id();
303
304
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
305
306
        if ( isset( $data[ $field ] ) ) {
307
            $value = sanitize_text_field( $data[ $field ] );
308
            update_user_meta( $user_id, '_wpinv_' . $field, $value );
309
        }
310
311
        wpinv_set_error( 'address_updated', __( 'Your billing address has been updated', 'invoicing' ), 'success');
312
    }
313
314
}
315
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' );
316
317
318
/*
319
 |--------------------------------------------------------------------------
320
 | UsersWP
321
 |--------------------------------------------------------------------------
322
 |
323
 | Functions that integrate GetPaid and UsersWP.
324
*/
325
326
/**
327
 * Add our tabs to UsersWP account tabs.
328
 *
329
 * @since 1.0.19
330
 * @param  array $tabs
331
 * @return array
332
 */
333
function getpaid_filter_userswp_account_tabs( $tabs ) {
334
335
    // Abort if the integration is inactive.
336
    if ( ! getpaid_is_userswp_integration_active() ) {
337
        return $tabs;
338
    }
339
340
    $new_tabs   = array();
341
342
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
343
344
        $new_tabs[ $slug ] = array(
345
            'title' => $tab[ 'label'],
346
            'icon'  =>  $tab[ 'icon'],
347
        );
348
349
    }
350
351
    return array_merge( $tabs, $new_tabs );
352
}
353
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
354
355
/**
356
 * Display our UsersWP account tabs.
357
 *
358
 * @since 1.0.19
359
 * @param  array $tabs
360
 * @return array
361
 */
362
function getpaid_display_userswp_account_tabs( $tab ) {
363
    global $getpaid_tab_url;
364
365
    $our_tabs = getpaid_get_user_content_tabs();
366
367
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
368
        $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

368
        $getpaid_tab_url = add_query_arg( 'type', '%s', /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
369
        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

369
        echo /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] );
Loading history...
370
    }
371
372
}
373
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
374
375
376
/**
377
 * Filters the account page title.
378
 *
379
 * @since  1.0.19
380
 * @param  string $title Current title.
381
 * @param  string $tab   Current tab.
382
 * @return string Title.
383
 */
384
function getpaid_filter_userswp_account_title( $title, $tab ) {
385
386
    $our_tabs   = getpaid_get_user_content_tabs();
387
388
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
389
        return $our_tabs[ $tab ]['label'];
390
    }
391
392
    return $title;
393
}
394
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
395
396
/**
397
 * Registers the UsersWP integration settings.
398
 *
399
 * @since  1.0.19
400
 * @param  array $settings An array of integration settings.
401
 * @return array
402
 */
403
function getpaid_register_userswp_settings( $settings ) {
404
405
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
406
407
        $settings[] = array(
408
409
            'id'       => 'userswp',
410
            'label'    => __( 'UsersWP', 'invoicing' ),
411
            'settings' => array(
412
413
                'userswp_settings' => array(
414
                    'id'   => 'userswp_settings',
415
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
416
                    'type' => 'header',
417
                ),
418
419
                'enable_userswp' => array(
420
                    'id'         => 'enable_userswp',
421
                    'name'       => __( 'Enable Integration', 'invoicing' ),
422
                    'desc'       => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
423
                    'type'       => 'checkbox',
424
                    'std'        => 1,
425
                )
426
427
            )
428
429
        );
430
431
    }
432
433
    return $settings;
434
}
435
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
436
437
/**
438
 * Ovewrites the invoices history page to UsersWP.
439
 *
440
 * @since  2.3.1
441
 * @return bool
442
 */
443
function getpaid_userswp_overwrite_invoice_history_page( $url, $post_type ) {
444
445
    $our_tabs = getpaid_get_user_content_tabs();
446
    $tab      = "gp-{$post_type}s";
447
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
448
        return add_query_arg( 'type', $tab, uwp_get_account_page_url() );
0 ignored issues
show
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...
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

448
        return add_query_arg( 'type', $tab, /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
449
    }
450
451
    return $url;
452
453
}
454
add_filter( 'wpinv_get_history_page_uri', 'getpaid_userswp_overwrite_invoice_history_page', 10, 2 );
455
456
/**
457
 * Checks if the integration is enabled.
458
 *
459
 * @since  1.0.19
460
 * @return bool
461
 */
462
function getpaid_is_userswp_integration_active() {
463
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
464
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
465
}
466
467
/*
468
 |--------------------------------------------------------------------------
469
 | BuddyPress
470
 |--------------------------------------------------------------------------
471
 |
472
 | Functions that integrate GetPaid and BuddyPress.
473
*/
474
475
/**
476
 * Registers the BuddyPress integration settings.
477
 *
478
 * @since  2.1.5
479
 * @param  array $settings An array of integration settings.
480
 * @return array
481
 */
482
function getpaid_register_buddypress_settings( $settings ) {
483
484
    if ( class_exists( 'BuddyPress' ) ) {
485
486
        $settings[] = array(
487
488
            'id'       => 'buddypress',
489
            'label'    => __( 'BuddyPress', 'invoicing' ),
490
            'settings' => array(
491
492
                'buddypress_settings' => array(
493
                    'id'   => 'buddypress_settings',
494
                    'name' => '<h3>' . __( 'BuddyPress', 'invoicing' ) . '</h3>',
495
                    'type' => 'header',
496
                ),
497
498
                'enable_buddypress' => array(
499
                    'id'         => 'enable_buddypress',
500
                    'name'       => __( 'Enable Integration', 'invoicing' ),
501
                    'desc'       => __( 'Display GetPaid items on BuddyPress account pages.', 'invoicing' ),
502
                    'type'       => 'checkbox',
503
                    'std'        => 1,
504
                )
505
506
            )
507
508
        );
509
510
    }
511
512
    return $settings;
513
}
514
add_filter( 'getpaid_integration_settings', 'getpaid_register_buddypress_settings' );
515
516
/**
517
 * Checks if the integration is enabled.
518
 *
519
 * @since  2.1.5
520
 * @return bool
521
 */
522
function getpaid_is_buddypress_integration_active() {
523
    $enabled = wpinv_get_option( 'enable_buddypress', 1 );
524
    return class_exists( 'BuddyPress' ) && ! empty( $enabled );
525
}
526
527
/**
528
 * Loads the BuddyPress component.
529
 *
530
 * @since  2.1.5
531
 * @return bool
532
 */
533
function getpaid_setup_buddypress_integration() {
534
535
    if ( getpaid_is_buddypress_integration_active() ) {
536
        require_once( WPINV_PLUGIN_DIR . 'includes/class-bp-getpaid-component.php' );
537
        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

537
        /** @scrutinizer ignore-call */ 
538
        buddypress()->getpaid = new BP_GetPaid_Component();
Loading history...
538
    }
539
540
}
541
add_action( 'bp_setup_components', 'getpaid_setup_buddypress_integration' );
542