Passed
Push — master ( 77758e...0d18b8 )
by Brian
06:17
created

getpaid_display_address_edit_tab()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 86
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 58
nc 4
nop 0
dl 0
loc 86
rs 8.9163
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        'gp-edit-address'  => array(
112
            'label'        => __( 'Billing Address', 'invoicing' ),
113
            'callback'     => 'getpaid_display_address_edit_tab',
114
            'icon'         => 'fas fa-credit-card',
115
        ),
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
    );
130
131
    return apply_filters( 'getpaid_user_content_tabs', $tabs );
132
}
133
134
/**
135
 * Prepares the contents of a tab.
136
 *
137
 * @since 1.0.19
138
 * @param array $tab
139
 * @return array
140
 */
141
function getpaid_prepare_user_content_tab( $tab ) {
142
143
    if ( ! empty( $tab['callback'] ) ) {
144
        return call_user_func( $tab['callback'] );
145
    }
146
147
    if ( ! empty( $tab['content'] ) ) {
148
        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...
149
    }
150
151
    $notice = aui()->alert(
152
        array(
153
            'content'     => __( 'This tab has no content or content callback.', 'invoicing' ),
154
            'type'        => 'error',
155
        )
156
    );
157
158
    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...
159
}
160
161
/**
162
 * Generates the current integrations tab URL.
163
 *
164
 * @since 1.0.19
165
 * @param string $tab
166
 * @param string $default
167
 * @return array
168
 */
169
function getpaid_get_tab_url( $tab, $default ) {
170
    global $getpaid_tab_url;
171
172
    if ( empty( $getpaid_tab_url ) ) {
173
        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...
174
    }
175
176
    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...
177
178
}
179
180
/**
181
 * Generates the address edit tab.
182
 *
183
 * @since 2.1.4
184
 * @return string
185
 */
186
function getpaid_display_address_edit_tab() {
187
188
    ob_start();
189
    ?>
190
        <div class="bsui">
191
            <?php wpinv_print_errors(); ?>
192
            <form method="post" class="getpaid-address-edit-form">
193
194
                <?php
195
196
                    foreach ( getpaid_user_address_fields() as $key => $label ) {
197
198
                        // Display the country.
199
                        if ( 'country' == $key ) {
200
201
                            echo aui()->select(
202
                                array(
203
                                    'options'     => wpinv_get_country_list(),
204
                                    'name'        => esc_attr( $key ),
205
                                    'id'          => 'wpinv-' . sanitize_html_class( $key ),
206
                                    'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
207
                                    'placeholder' => $label,
208
                                    'label'       => wp_kses_post( $label ),
209
                                    'label_type'  => 'vertical',
210
                                    'class'       => 'getpaid-address-field',
211
                                )
212
                            );
213
214
                        }
215
216
                        // Display the state.
217
                        else if ( 'state' == $key ) {
218
219
                            echo getpaid_get_states_select_markup (
220
                                getpaid_get_user_address_field( get_current_user_id(), 'country' ),
221
                                getpaid_get_user_address_field( get_current_user_id(), 'state' ),
222
                                $label,
223
                                $label,
224
                                '',
225
                                false,
226
                                '',
227
                                $key
228
                            );
229
230
                        } else {
231
232
                            echo aui()->input(
233
                                array(
234
                                    'name'        => esc_attr( $key ),
235
                                    'id'          => 'wpinv-' . sanitize_html_class( $key ),
236
                                    'placeholder' => $label,
237
                                    'label'       => wp_kses_post( $label ),
238
                                    'label_type'  => 'vertical',
239
                                    'type'        => 'text',
240
                                    'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
241
                                    'class'       => 'getpaid-address-field',
242
                                )
243
                            );
244
245
                        }
246
247
                    }
248
249
                    do_action( 'getpaid_display_address_edit_tab' );
250
251
                    echo aui()->input(
252
                        array(
253
                            'name'             => 'getpaid_profile_edit_submit_button',
254
                            'id'               => 'getpaid_profile_edit_submit_button',
255
                            'value'            => __( 'Save Address', 'invoicing' ),
256
                            'help_text'        => __( 'New invoices will use this address as the billing address.', 'invoicing' ),
257
                            'type'             => 'submit',
258
                            'class'            => 'btn btn-primary btn-block submit-button',
259
                        )
260
                    );
261
262
                    wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' );
263
                    getpaid_hidden_field( 'getpaid-action', 'edit_billing_details' );
264
                ?>
265
266
            </form>
267
268
        </div>
269
    <?php
270
271
    return ob_get_clean();
272
}
273
274
/**
275
 * Saves the billing address edit tab.
276
 *
277
 * @since 2.1.4
278
 * @param array $data
279
 */
280
function getpaid_save_address_edit_tab( $data ) {
281
282
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
283
284
        if ( isset( $data[ $field ] ) ) {
285
            $value = sanitize_text_field( $data[ $field ] );
286
            update_user_meta( get_current_user_id(), '_wpinv_' . $field, $value );
287
        }
288
289
        wpinv_set_error( 'address_updated', __( 'You billing address has been updated', 'invoicing' ), 'success');
290
    }
291
292
}
293
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' );
294
295
296
/*
297
 |--------------------------------------------------------------------------
298
 | UsersWP
299
 |--------------------------------------------------------------------------
300
 |
301
 | Functions that integrate GetPaid and UsersWP.
302
*/
303
304
/**
305
 * Add our tabs to UsersWP account tabs.
306
 *
307
 * @since 1.0.19
308
 * @param  array $tabs
309
 * @return array
310
 */
311
function getpaid_filter_userswp_account_tabs( $tabs ) {
312
313
    // Abort if the integration is inactive.
314
    if ( ! getpaid_is_userswp_integration_active() ) {
315
        return $tabs;
316
    }
317
318
    $new_tabs   = array();
319
320
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
321
322
        $new_tabs[ $slug ] = array(
323
            'title' => $tab[ 'label'],
324
            'icon'  =>  $tab[ 'icon'],
325
        );
326
327
    }
328
329
    return array_merge( $tabs, $new_tabs );
330
}
331
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
332
333
/**
334
 * Display our UsersWP account tabs.
335
 *
336
 * @since 1.0.19
337
 * @param  array $tabs
338
 * @return array
339
 */
340
function getpaid_display_userswp_account_tabs( $tab ) {
341
    global $getpaid_tab_url;
342
343
    $our_tabs = getpaid_get_user_content_tabs();
344
345
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
346
        $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

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

347
        echo /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] );
Loading history...
348
    }
349
350
}
351
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
352
353
354
/**
355
 * Filters the account page title.
356
 *
357
 * @since  1.0.19
358
 * @param  string $title Current title.
359
 * @param  string $tab   Current tab.
360
 * @return string Title.
361
 */
362
function getpaid_filter_userswp_account_title( $title, $tab ) {
363
364
    $our_tabs   = getpaid_get_user_content_tabs();
365
366
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
367
        return $our_tabs[ $tab ]['label'];
368
    }
369
370
    return $title;
371
}
372
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
373
374
/**
375
 * Registers the UsersWP integration settings.
376
 *
377
 * @since  1.0.19
378
 * @param  array $settings An array of integration settings.
379
 * @return array
380
 */
381
function getpaid_register_userswp_settings( $settings ) {
382
383
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
384
385
        $settings[] = array(
386
387
            'id'       => 'userswp',
388
            'label'    => __( 'UsersWP', 'invoicing' ),
389
            'settings' => array(
390
391
                'userswp_settings' => array(
392
                    'id'   => 'userswp_settings',
393
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
394
                    'type' => 'header',
395
                ),
396
397
                'enable_userswp' => array(
398
                    'id'         => 'enable_userswp',
399
                    'name'       => __( 'Enable Integration', 'invoicing' ),
400
                    'desc'       => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
401
                    'type'       => 'checkbox',
402
                    'std'        => 1,
403
                )
404
405
            )
406
407
        );
408
409
    }
410
411
    return $settings;
412
}
413
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
414
415
/**
416
 * Checks if the integration is enabled.
417
 *
418
 * @since  1.0.19
419
 * @return bool
420
 */
421
function getpaid_is_userswp_integration_active() {
422
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
423
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
424
}
425