Passed
Push — master ( 32435e...84aa6b )
by Brian
04:07
created

getpaid_get_tab_url()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
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 ) {
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
        'gp-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
        'gp-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
 * Generates the current integrations tab URL.
157
 *
158
 * @since 1.0.19
159
 * @param string $tab
160
 * @param string $default
161
 * @return array
162
 */
163
function getpaid_get_tab_url( $tab, $default ) {
164
    global $getpaid_tab_url;
165
166
    if ( empty( $getpaid_tab_url ) ) {
167
        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...
168
    }
169
170
    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...
171
172
}
173
174
/*
175
 |--------------------------------------------------------------------------
176
 | UsersWP
177
 |--------------------------------------------------------------------------
178
 |
179
 | Functions that integrate GetPaid and UsersWP.
180
*/
181
182
/**
183
 * Add our tabs to UsersWP account tabs.
184
 *
185
 * @since 1.0.19
186
 * @param  array $tabs
187
 * @return array
188
 */
189
function getpaid_filter_userswp_account_tabs( $tabs ) {
190
191
    // Abort if the integration is inactive.
192
    if ( ! getpaid_is_userswp_integration_active() ) {
193
        return $tabs;
194
    }
195
196
    $new_tabs   = array();
197
198
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
199
200
        $new_tabs[ $slug ] = array(
201
            'title' => $tab[ 'label'],
202
            'icon'  =>  $tab[ 'icon'],
203
        );
204
205
    }
206
207
    return array_merge( $new_tabs, $tabs );
208
}
209
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
210
211
/**
212
 * Display our UsersWP account tabs.
213
 *
214
 * @since 1.0.19
215
 * @param  array $tabs
216
 * @return array
217
 */
218
function getpaid_display_userswp_account_tabs( $tab ) {
219
    global $getpaid_tab_url;
220
221
    $our_tabs = getpaid_get_user_content_tabs();
222
223
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
224
        $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

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

225
        echo /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] );
Loading history...
226
    }
227
228
}
229
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
230
231
232
/**
233
 * Filters the account page title.
234
 *
235
 * @since  1.0.19
236
 * @param  string $title Current title.
237
 * @param  string $tab   Current tab.
238
 * @return string Title.
239
 */
240
function getpaid_filter_userswp_account_title( $title, $tab ) {
241
242
    $our_tabs   = getpaid_get_user_content_tabs();
243
244
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
245
        return $our_tabs[ $tab ]['label'];
246
    }
247
248
    return $title;
249
}
250
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
251
252
/**
253
 * Registers the UsersWP integration settings.
254
 *
255
 * @since  1.0.19
256
 * @param  array $settings An array of integration settings.
257
 * @return array
258
 */
259
function getpaid_register_userswp_settings( $settings ) {
260
261
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
262
263
        $settings[] = array(
264
265
            'id'       => 'userswp',
266
            'label'    => __( 'UsersWP', 'invoicing' ),
267
            'settings' => array(
268
269
                'userswp_settings' => array(
270
                    'id'   => 'userswp_settings',
271
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
272
                    'type' => 'header',
273
                ),
274
275
                'enable_userswp' => array(
276
                    'id'         => 'enable_userswp',
277
                    'name'       => __( 'Enable Integration', 'invoicing' ),
278
                    'desc'       => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
279
                    'type'       => 'checkbox',
280
                    'std'        => 1,
281
                )
282
283
            )
284
285
        );
286
287
    }
288
289
    return $settings;
290
}
291
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
292
293
/**
294
 * Checks if the integration is enabled.
295
 *
296
 * @since  1.0.19
297
 * @return bool
298
 */
299
function getpaid_is_userswp_integration_active() {
300
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
301
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
302
}
303