Passed
Push — master ( d100e4...32435e )
by Brian
11:14
created

getpaid_display_userswp_account_tabs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
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
            'icon'      => 'fas fa-file-invoice', // Shown on some profile plugins.
116
        ),
117
118
        'subscriptions' => array(
119
            'label'     => __( 'Subscriptions', 'invoicing' ),
120
            'content'   => '[wpinv_subscriptions]',
121
            'icon'      => 'fas fa-redo',
122
        )
123
    );
124
125
    return apply_filters( 'getpaid_user_content_tabs', $tabs );
126
}
127
128
/**
129
 * Prepares the contents of a tab.
130
 *
131
 * @since 1.0.19
132
 * @param array $tab
133
 * @return array
134
 */
135
function getpaid_prepare_user_content_tab( $tab ) {
136
137
    if ( ! empty( $tab['callback'] ) ) {
138
        return call_user_func( $tab['callback'] );
139
    }
140
141
    if ( ! empty( $tab['content'] ) ) {
142
        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...
143
    }
144
145
    $notice = aui()->alert(
146
        array(
147
            'content'     => __( 'This tab has no content or content callback.', 'invoicing' ),
148
            'type'        => 'error',
149
        )
150
    );
151
152
    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...
153
}
154
155
/*
156
 |--------------------------------------------------------------------------
157
 | UsersWP
158
 |--------------------------------------------------------------------------
159
 |
160
 | Functions that integrate GetPaid and UsersWP.
161
*/
162
163
/**
164
 * Add our tabs to UsersWP account tabs.
165
 *
166
 * @since 1.0.19
167
 * @param  array $tabs
168
 * @return array
169
 */
170
function getpaid_filter_userswp_account_tabs( $tabs ) {
171
172
    // Abort if the integration is inactive.
173
    if ( ! getpaid_is_userswp_integration_active() ) {
174
        return $tabs;
175
    }
176
177
    $new_tabs   = array();
178
179
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
180
181
        $new_tabs[ $slug ] = array(
182
            'title' => $tab[ 'label'],
183
            'icon'  =>  $tab[ 'icon'],
184
        );
185
186
    }
187
188
    return array_merge( $new_tabs, $tabs );
189
}
190
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
191
192
/**
193
 * Display our UsersWP account tabs.
194
 *
195
 * @since 1.0.19
196
 * @param  array $tabs
197
 * @return array
198
 */
199
function getpaid_display_userswp_account_tabs( $tab ) {
200
201
    $our_tabs = getpaid_get_user_content_tabs();
202
203
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
204
        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

204
        echo /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] );
Loading history...
205
    }
206
207
}
208
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
209
210
211
/**
212
 * Filters the account page title.
213
 *
214
 * @since  1.0.19
215
 * @param  string $title Current title.
216
 * @param  string $tab   Current tab.
217
 * @return string Title.
218
 */
219
function getpaid_filter_userswp_account_title( $title, $tab ) {
220
221
    $our_tabs   = getpaid_get_user_content_tabs();
222
223
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
224
        return $our_tabs[ $tab ]['label'];
225
    }
226
227
    return $title;
228
}
229
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
230
231
/**
232
 * Registers the UsersWP integration settings.
233
 *
234
 * @since  1.0.19
235
 * @param  array $settings An array of integration settings.
236
 * @return array
237
 */
238
function getpaid_register_userswp_settings( $settings ) {
239
240
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
241
242
        $settings[] = array(
243
244
            'id'       => 'userswp',
245
            'label'    => __( 'UsersWP', 'invoicing' ),
246
            'settings' => array(
247
248
                'userswp_settings' => array(
249
                    'id'   => 'userswp_settings',
250
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
251
                    'type' => 'header',
252
                ),
253
254
                'enable_userswp' => array(
255
                    'id'         => 'enable_userswp',
256
                    'name'       => __( 'Enable Integration', 'invoicing' ),
257
                    'desc'       => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
258
                    'type'       => 'checkbox',
259
                    'std'        => 1,
260
                )
261
262
            )
263
264
        );
265
266
    }
267
268
    return $settings;
269
}
270
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
271
272
/**
273
 * Checks if the integration is enabled.
274
 *
275
 * @since  1.0.19
276
 * @return bool
277
 */
278
function getpaid_is_userswp_integration_active() {
279
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
280
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
281
}
282