Passed
Push — master ( 3100fb...fd541e )
by Brian
03:57
created

wpinv_dropdown_users()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
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 ) {
61
62
    // Prepare user values.
63
	$args = array(
64
		'user_login' => wpinv_generate_user_name( $email ),
65
		'user_pass'  => wp_generate_password(),
66
		'user_email' => $email,
67
        'role'       => 'subscriber',
68
    );
69
70
    return wp_insert_user( $args );
71
72
}
73
74
/**
75
 *  Generates a unique user name from an email.
76
 *
77
 * @since 1.0.19
78
 * @return bool|WP_User
79
 */
80
function wpinv_generate_user_name( $prefix = '' ) {
81
82
    // If prefix is an email, retrieve the part before the email.
83
	$prefix = strtok( $prefix, '@' );
84
85
	// Trim to 4 characters max.
86
	$prefix = sanitize_user( $prefix );
87
88
	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
89
	if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) {
90
		$prefix = 'gtp';
91
	}
92
93
	$username = $prefix . '_' . zeroise( wp_rand( 0, 9999 ), 4 );
94
	if ( username_exists( $username ) ) {
95
		return wpinv_generate_user_name( $username );
96
	}
97
98
    return $username;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $username returns the type string which is incompatible with the documented return type WP_User|boolean.
Loading history...
99
}
100
101
/**
102
 * Returns an array of user content tabs.
103
 *
104
 * @since 1.0.19
105
 * @return array
106
 */
107
function getpaid_get_user_content_tabs() {
108
109
    $tabs = array(
110
111
        // Slug - invoices.
112
        'invoices'      => array(
113
            'label'     => __( 'Invoices', 'invoicing' ), // Name of the tab.
114
            'content'   => '[wpinv_history]', // Content of the tab. Or specify "callback" to provide a callback instead.
115
        ),
116
117
        'subscriptions' => array(
118
            'label'     => __( 'Subscriptions', 'invoicing' ),
119
            'content'   => '[wpinv_subscriptions]',
120
        )
121
    );
122
123
    return apply_filters( 'getpaid_user_content_tabs', $tabs );
124
}
125
126
/**
127
 * Prepares the contents of a tab.
128
 *
129
 * @since 1.0.19
130
 * @param array $tab
131
 * @return array
132
 */
133
function getpaid_prepare_user_content_tab( $tab ) {
134
135
    if ( ! empty( $tab['callback'] ) ) {
136
        return call_user_func( $tab['callback'] );
137
    }
138
139
    if ( ! empty( $tab['content'] ) ) {
140
        return convert_smilies( capital_P_dangit( wp_filter_content_tags( the_content( shortcode_unautop( wpautop( wptexturize( do_blocks( $tab['content'] ) ) ) ) ) ) ) );
0 ignored issues
show
Bug introduced by
Are you sure the usage of the_content(shortcode_un...ks($tab['content']))))) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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...
141
    }
142
143
    $notice = aui()->alert(
144
        array(
145
            'content'     => __( 'This tab has no content or content callback.', 'invoicing' ),
146
            'type'        => 'error',
147
        )
148
    );
149
150
    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...
151
}
152