Passed
Push — master ( 345b42...86cae5 )
by Brian
05:49
created

getpaid_save_address_edit_tab()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
c 1
b 0
f 0
nc 13
nop 1
dl 0
loc 27
rs 8.8333
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
 * Retrieves a customer.
13
 *
14
 * @param int|string|object|GetPaid_Customer $customer customer id, email or object.
15
 * @return GetPaid_Customer|null
16
 */
17
function getpaid_get_customer( $customer ) {
18
19
    if ( empty( $customer ) ) {
20
        return null;
21
    }
22
23
    // Retrieve the customer.
24
    if ( ! is_a( $customer, 'GetPaid_Customer' ) ) {
25
        $customer = new GetPaid_Customer( $customer );
26
    }
27
28
    // Check if it exists.
29
    if ( $customer->exists() ) {
30
        return $customer;
31
    }
32
33
    return null;
34
}
35
36
/**
37
 * Fetch customer by user ID.
38
 *
39
 * @return GetPaid_Customer|null
40
 * @since 1.0.0
41
 */
42
function getpaid_get_customer_by_user_id( $user_id ) {
43
    return getpaid_get_customer(
44
        GetPaid_Customer::get_customer_id_by( $user_id, 'user_id' )
45
    );
46
}
47
48
/**
49
 *  Generates a users select dropdown.
50
 *
51
 * @since 1.0.0
52
 * @return string|void Users dropdown markup.
53
 * @param array $args
54
 * @see wp_dropdown_users
55
 */
56
function wpinv_dropdown_users( $args = '' ) {
57
58
    if ( is_array( $args ) && ! empty( $args['show'] ) && 'display_name_with_email' == $args['show'] ) {
59
        $args['show'] = 'display_name_with_login';
60
    }
61
62
    return wp_dropdown_users( $args );
63
}
64
65
/**
66
 *  Returns the appropriate capability to check against
67
 *
68
 * @since 1.0.13
69
 * @return string capability to check against
70
 * @param string $capalibilty Optional. The alternative capability to check against.
71
 */
72
function wpinv_get_capability( $capalibilty = 'manage_invoicing' ) {
73
74
	if ( current_user_can( 'manage_options' ) ) {
75
		return 'manage_options';
76
	};
77
78
	return $capalibilty;
79
}
80
81
/**
82
 *  Checks if the current user can manager invoicing
83
 *
84
 * @since 1.0.13
85
 * @return bool
86
 */
87
function wpinv_current_user_can_manage_invoicing() {
88
    return current_user_can( wpinv_get_capability() );
89
}
90
91
/**
92
 * Returns whether the current user has the specified getpaid capability.
93
 *
94
 * @since 2.7.8
95
 *
96
 * @param string $capability Capability name.
97
 * @param mixed  $args    Optional further parameters, typically starting with an object.
98
 * @return bool Whether the current user has the given capability.
99
 */
100
function wpinv_current_user_can( $capability, $args = array() ) {
101
	$can = wpinv_current_user_can_manage_invoicing();
102
103
	return apply_filters( 'getpaid_current_user_can', $can, $capability, $args );
104
}
105
106
/**
107
 *  Given an email address, it creates a new user.
108
 *
109
 * @since 1.0.19
110
 * @return int|WP_Error
111
 */
112
function wpinv_create_user( $email, $prefix = '' ) {
113
114
    // Prepare user values.
115
    $prefix = preg_replace( '/\s+/', '', $prefix );
116
    $prefix = empty( $prefix ) ? $email : $prefix;
117
	$args   = array(
118
		'user_login' => wpinv_generate_user_name( $prefix ),
119
		'user_pass'  => wp_generate_password(),
120
		'user_email' => $email,
121
        'role'       => 'subscriber',
122
    );
123
124
    return wp_insert_user( $args );
125
126
}
127
128
/**
129
 *  Generates a unique user name from an email.
130
 *
131
 * @since 1.0.19
132
 * @return bool|WP_User
133
 */
134
function wpinv_generate_user_name( $prefix = '' ) {
135
136
    // If prefix is an email, retrieve the part before the email.
137
	$prefix = strtok( $prefix, '@' );
138
    $prefix = trim( $prefix, '.' );
139
140
	// Sanitize the username.
141
	$prefix = sanitize_user( $prefix, true );
142
143
	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
144
	if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) {
145
		$prefix = 'gtp_' . zeroise( wp_rand( 0, 9999 ), 4 );
146
	}
147
148
    $username = $prefix;
149
    $postfix  = 2;
150
151
    while ( username_exists( $username ) ) {
152
        $username = "{$prefix}{$postfix}";
153
        $postfix ++;
154
    }
155
156
    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...
157
}
158
159
/**
160
 * Returns an array of user content tabs.
161
 *
162
 * @since 1.0.19
163
 * @return array
164
 */
165
function getpaid_get_user_content_tabs() {
166
167
    $tabs = array(
168
169
        'gp-invoices'      => array(
170
            'label'   => __( 'Invoices', 'invoicing' ), // Name of the tab.
171
            'content' => '[wpinv_history]', // Content of the tab. Or specify "callback" to provide a callback instead.
172
            'icon'    => 'fas fa-file-invoice', // Shown on some profile plugins.
173
        ),
174
175
        'gp-subscriptions' => array(
176
            'label'   => __( 'Subscriptions', 'invoicing' ),
177
            'content' => '[wpinv_subscriptions]',
178
            'icon'    => 'fas fa-redo',
179
        ),
180
181
        'gp-edit-address'  => array(
182
            'label'    => __( 'Billing Address', 'invoicing' ),
183
            'callback' => 'getpaid_display_address_edit_tab',
184
            'icon'     => 'fas fa-credit-card',
185
        ),
186
187
    );
188
189
    $tabs = apply_filters( 'getpaid_user_content_tabs', $tabs );
190
191
    // Make sure address editing is last on the list.
192
    if ( isset( $tabs['gp-edit-address'] ) ) {
193
        $address = $tabs['gp-edit-address'];
194
        unset( $tabs['gp-edit-address'] );
195
        $tabs['gp-edit-address'] = $address;
196
    }
197
198
    return $tabs;
199
}
200
201
/**
202
 * Prepares the contents of a tab.
203
 *
204
 * @since 1.0.19
205
 * @param array $tab
206
 * @return array
207
 */
208
function getpaid_prepare_user_content_tab( $tab ) {
209
210
    if ( ! empty( $tab['callback'] ) ) {
211
        return call_user_func( $tab['callback'] );
212
    }
213
214
    if ( ! empty( $tab['content'] ) ) {
215
        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...
216
    }
217
218
    $notice = aui()->alert(
219
        array(
220
            'content' => __( 'This tab has no content or content callback.', 'invoicing' ),
221
            'type'    => 'error',
222
        )
223
    );
224
225
    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...
226
}
227
228
/**
229
 * Generates the current integrations tab URL.
230
 *
231
 * @since 1.0.19
232
 * @param string $tab
233
 * @param string $default
234
 * @return array
235
 */
236
function getpaid_get_tab_url( $tab, $default ) {
237
    global $getpaid_tab_url;
238
239
    if ( empty( $getpaid_tab_url ) ) {
240
        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...
241
    }
242
243
    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...
244
245
}
246
247
/**
248
 * Generates the address edit tab.
249
 *
250
 * @since 2.1.4
251
 * @return string
252
 */
253
function getpaid_display_address_edit_tab() {
254
255
    if ( 0 === get_current_user_id() ) {
256
        return '<div class="bsui">' . aui()->alert(
257
            array(
258
                'type'        => 'error',
259
                'content'     => __( 'Your must be logged in to view this section', 'invoicing' ),
260
                'dismissible' => false,
261
            )
262
        ) . '</div>';
263
    }
264
265
    $customer = getpaid_get_customer_by_user_id( get_current_user_id() );
266
267
    if ( empty( $customer ) ) {
268
        $customer = new GetPaid_Customer( 0 );
269
        $customer->clone_user( get_current_user_id() );
270
        $customer->save();
271
    }
272
273
    ob_start();
274
    ?>
275
        <div class="bsui">
276
            <?php wpinv_print_errors(); ?>
277
            <form method="post" class="getpaid-address-edit-form">
278
279
                <?php
280
281
                    foreach ( getpaid_user_address_fields() as $key => $label ) {
282
283
                        $value = $customer->get( $key );
284
285
					// Display the country.
286
					if ( 'country' == $key ) {
287
288
						aui()->select(
289
							array(
290
								'options'     => wpinv_get_country_list(),
291
								'name'        => 'getpaid_address[' . esc_attr( $key ) . ']',
292
								'id'          => 'wpinv-' . sanitize_html_class( $key ),
293
								'value'       => sanitize_text_field( $value ),
294
								'placeholder' => $label,
295
								'label'       => wp_kses_post( $label ),
296
								'label_type'  => 'vertical',
297
								'class'       => 'getpaid-address-field',
298
                            ),
299
                            true
300
						);
301
302
					}
303
304
					// Display the state.
305
					elseif ( 'state' == $key ) {
306
307
						getpaid_get_states_select_markup(
308
                            $customer->get( 'country' ),
309
							$value,
310
							$label,
311
							$label,
312
							'',
313
							false,
314
							'',
315
							'getpaid_address[' . esc_attr( $key ) . ']',
316
                            true
317
						);
318
319
                        } else {
320
321
						aui()->input(
322
                            array(
323
                                'name'        => 'getpaid_address[' . esc_attr( $key ) . ']',
324
                                'id'          => 'wpinv-' . sanitize_html_class( $key ),
325
                                'placeholder' => $label,
326
                                'label'       => wp_kses_post( $label ),
327
                                'label_type'  => 'vertical',
328
                                'type'        => 'text',
329
                                'value'       => sanitize_text_field( $value ),
330
                                'class'       => 'getpaid-address-field',
331
                            ),
332
                            true
333
						);
334
335
                        }
336
                    }
337
338
                    aui()->input(
339
                        array(
340
                            'name'        => 'getpaid_address[email_cc]',
341
                            'id'          => 'wpinv-email_cc',
342
                            'placeholder' => '[email protected], [email protected]',
343
                            'label'       => __( 'Other email addresses', 'invoicing' ),
344
                            'label_type'  => 'vertical',
345
                            'type'        => 'text',
346
                            'value'       => sanitize_text_field( $customer->get( 'email_cc' ) ),
347
                            'class'       => 'getpaid-address-field',
348
                            'help_text'   => __( 'Optionally provide other email addresses where we should send payment notifications', 'invoicing' ),
349
                        ),
350
                        true
351
                    );
352
353
                    do_action( 'getpaid_display_address_edit_tab' );
354
355
                    aui()->input(
356
                        array(
357
                            'name'      => 'getpaid_profile_edit_submit_button',
358
                            'id'        => 'getpaid_profile_edit_submit_button',
359
                            'value'     => __( 'Save Address', 'invoicing' ),
360
                            'help_text' => __( 'New invoices will use this address as the billing address.', 'invoicing' ),
361
                            'type'      => 'submit',
362
                            'class'     => 'btn btn-primary btn-block submit-button',
363
                        ),
364
                        true
365
                    );
366
367
                    wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' );
368
                    getpaid_hidden_field( 'getpaid-action', 'edit_billing_details' );
369
                ?>
370
371
            </form>
372
373
        </div>
374
    <?php
375
376
    return ob_get_clean();
377
}
378
add_shortcode( 'getpaid_edit_address', 'getpaid_display_address_edit_tab' );
379
380
/**
381
 * Saves the billing address edit tab.
382
 *
383
 * @since 2.1.4
384
 * @param array $data
385
 */
386
function getpaid_save_address_edit_tab( $data ) {
387
388
    if ( empty( $data['getpaid_address'] ) || ! is_array( $data['getpaid_address'] ) ) {
389
        return;
390
    }
391
392
    $data     = $data['getpaid_address'];
393
    $customer = getpaid_get_customer_by_user_id( get_current_user_id() );
394
395
    if ( empty( $customer ) ) {
396
        $customer = new GetPaid_Customer( 0 );
397
        $customer->clone_user( get_current_user_id() );
398
    }
399
400
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
401
402
        if ( isset( $data[ $field ] ) ) {
403
            $customer->set( $field, sanitize_text_field( $data[ $field ] ) );
404
        }
405
    }
406
407
    if ( isset( $data['email_cc'] ) ) {
408
        $customer->set( 'email_cc', sanitize_text_field( $data['email_cc'] ) );
409
    }
410
411
    $customer->save();
412
    wpinv_set_error( 'address_updated' );
413
}
414
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' );
415
416
417
/*
418
 |--------------------------------------------------------------------------
419
 | UsersWP
420
 |--------------------------------------------------------------------------
421
 |
422
 | Functions that integrate GetPaid and UsersWP.
423
*/
424
425
/**
426
 * Add our tabs to UsersWP account tabs.
427
 *
428
 * @since 1.0.19
429
 * @param  array $tabs
430
 * @return array
431
 */
432
function getpaid_filter_userswp_account_tabs( $tabs ) {
433
434
    // Abort if the integration is inactive.
435
    if ( ! getpaid_is_userswp_integration_active() ) {
436
        return $tabs;
437
    }
438
439
    $new_tabs   = array();
440
441
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
442
443
        $new_tabs[ $slug ] = array(
444
            'title' => $tab['label'],
445
            'icon'  => $tab['icon'],
446
        );
447
448
    }
449
450
    return array_merge( $tabs, $new_tabs );
451
}
452
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
453
454
/**
455
 * Display our UsersWP account tabs.
456
 *
457
 * @since 1.0.19
458
 * @param  array $tabs
459
 * @return array
460
 */
461
function getpaid_display_userswp_account_tabs( $tab ) {
462
    global $getpaid_tab_url;
463
464
    $our_tabs = getpaid_get_user_content_tabs();
465
466
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
467
        $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

467
        $getpaid_tab_url = add_query_arg( 'type', '%s', /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
468
        echo wp_kses( getpaid_prepare_user_content_tab( $our_tabs[ $tab ] ), getpaid_allowed_html() );
0 ignored issues
show
Bug introduced by
getpaid_prepare_user_content_tab($our_tabs[$tab]) of type array is incompatible with the type string expected by parameter $content of wp_kses(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

468
        echo wp_kses( /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] ), getpaid_allowed_html() );
Loading history...
469
    }
470
471
}
472
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
473
474
function getpaid_allowed_html() {
475
    $allowed_html = wp_kses_allowed_html( 'post' );
476
477
	// form fields
478
    $allowed_html['form'] = array(
479
        'action'         => true,
480
        'accept'         => true,
481
        'accept-charset' => true,
482
        'enctype'        => true,
483
        'method'         => true,
484
        'name'           => true,
485
        'target'         => true,
486
    );
487
488
    // - input
489
	$allowed_html['input'] = array(
490
		'class'        => array(),
491
		'id'           => array(),
492
		'name'         => array(),
493
		'value'        => array(),
494
		'type'         => array(),
495
        'placeholder'  => array(),
496
        'autocomplete' => array(),
497
        'autofocus'    => array(),
498
        'required'     => array(),
499
        'disabled'     => array(),
500
        'readonly'     => array(),
501
        'checked'      => array(),
502
        'maxlength'    => array(),
503
        'pattern'      => array(),
504
        'min'          => array(),
505
        'max'          => array(),
506
        'step'         => array(),
507
        'size'         => array(),
508
	);
509
510
    // - input
511
	$allowed_html['textarea'] = array(
512
		'class' => array(),
513
		'id'    => array(),
514
		'name'  => array(),
515
		'value' => array(),
516
	);
517
518
	// select
519
	$allowed_html['select'] = array(
520
		'class'        => array(),
521
		'id'           => array(),
522
		'name'         => array(),
523
        'autocomplete' => array(),
524
        'multiple'     => array(),
525
	);
526
527
	// select options
528
	$allowed_html['option'] = array(
529
		'selected' => array(),
530
        'disabled' => array(),
531
        'value'    => array(),
532
	);
533
534
	return $allowed_html;
535
536
}
537
538
/**
539
 * Filters the account page title.
540
 *
541
 * @since  1.0.19
542
 * @param  string $title Current title.
543
 * @param  string $tab   Current tab.
544
 * @return string Title.
545
 */
546
function getpaid_filter_userswp_account_title( $title, $tab ) {
547
548
    $our_tabs   = getpaid_get_user_content_tabs();
549
550
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
551
        return $our_tabs[ $tab ]['label'];
552
    }
553
554
    return $title;
555
}
556
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
557
558
/**
559
 * Registers the UsersWP integration settings.
560
 *
561
 * @since  1.0.19
562
 * @param  array $settings An array of integration settings.
563
 * @return array
564
 */
565
function getpaid_register_userswp_settings( $settings ) {
566
567
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
568
569
        $settings[] = array(
570
571
            'id'       => 'userswp',
572
            'label'    => __( 'UsersWP', 'invoicing' ),
573
            'settings' => array(
574
575
                'userswp_settings' => array(
576
                    'id'   => 'userswp_settings',
577
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
578
                    'type' => 'header',
579
                ),
580
581
                'enable_userswp'   => array(
582
                    'id'   => 'enable_userswp',
583
                    'name' => __( 'Enable Integration', 'invoicing' ),
584
                    'desc' => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
585
                    'type' => 'checkbox',
586
                    'std'  => 1,
587
                ),
588
589
            ),
590
591
        );
592
593
    }
594
595
    return $settings;
596
}
597
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
598
599
/**
600
 * Ovewrites the invoices history page to UsersWP.
601
 *
602
 * @since  2.3.1
603
 * @return bool
604
 */
605
function getpaid_userswp_overwrite_invoice_history_page( $url, $post_type ) {
606
607
    $our_tabs = getpaid_get_user_content_tabs();
608
    $tab      = "gp-{$post_type}s";
609
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
610
        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

610
        return add_query_arg( 'type', $tab, /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
611
    }
612
613
    return $url;
614
615
}
616
add_filter( 'wpinv_get_history_page_uri', 'getpaid_userswp_overwrite_invoice_history_page', 10, 2 );
617
618
/**
619
 * Checks if the integration is enabled.
620
 *
621
 * @since  1.0.19
622
 * @return bool
623
 */
624
function getpaid_is_userswp_integration_active() {
625
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
626
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
627
}
628
629
/*
630
 |--------------------------------------------------------------------------
631
 | BuddyPress
632
 |--------------------------------------------------------------------------
633
 |
634
 | Functions that integrate GetPaid and BuddyPress.
635
*/
636
637
/**
638
 * Registers the BuddyPress integration settings.
639
 *
640
 * @since  2.1.5
641
 * @param  array $settings An array of integration settings.
642
 * @return array
643
 */
644
function getpaid_register_buddypress_settings( $settings ) {
645
646
    if ( class_exists( 'BuddyPress' ) ) {
647
648
        $settings[] = array(
649
650
            'id'       => 'buddypress',
651
            'label'    => __( 'BuddyPress', 'invoicing' ),
652
            'settings' => array(
653
654
                'buddypress_settings' => array(
655
                    'id'   => 'buddypress_settings',
656
                    'name' => '<h3>' . __( 'BuddyPress', 'invoicing' ) . '</h3>',
657
                    'type' => 'header',
658
                ),
659
660
                'enable_buddypress'   => array(
661
                    'id'   => 'enable_buddypress',
662
                    'name' => __( 'Enable Integration', 'invoicing' ),
663
                    'desc' => __( 'Display GetPaid items on BuddyPress account pages.', 'invoicing' ),
664
                    'type' => 'checkbox',
665
                    'std'  => 1,
666
                ),
667
668
            ),
669
670
        );
671
672
    }
673
674
    return $settings;
675
}
676
add_filter( 'getpaid_integration_settings', 'getpaid_register_buddypress_settings' );
677
678
/**
679
 * Checks if the integration is enabled.
680
 *
681
 * @since  2.1.5
682
 * @return bool
683
 */
684
function getpaid_is_buddypress_integration_active() {
685
    $enabled = wpinv_get_option( 'enable_buddypress', 1 );
686
    return class_exists( 'BuddyPress' ) && ! empty( $enabled );
687
}
688
689
/**
690
 * Loads the BuddyPress component.
691
 *
692
 * @since  2.1.5
693
 * @return bool
694
 */
695
function getpaid_setup_buddypress_integration() {
696
697
    if ( getpaid_is_buddypress_integration_active() ) {
698
        require_once WPINV_PLUGIN_DIR . 'includes/class-bp-getpaid-component.php';
699
        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

699
        /** @scrutinizer ignore-call */ 
700
        buddypress()->getpaid = new BP_GetPaid_Component();
Loading history...
700
    }
701
702
}
703
add_action( 'bp_setup_components', 'getpaid_setup_buddypress_integration' );
704
705
/**
706
 * Checks if a given user has purchased a given item.
707
 *
708
 * @since 2.7.3
709
 * @param int $item_id The item id.
710
 * @return int The IDs of users who purchased the item.
711
 */
712
function getpaid_user_ids_who_purchased_item( $item_id ) {
713
    global $wpdb;
714
715
    if ( empty( $item_id ) ) {
716
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
717
    }
718
719
    $ids = $wpdb->get_col(
720
        $wpdb->prepare(
721
            "SELECT DISTINCT invoices.post_author FROM {$wpdb->prefix}getpaid_invoice_items AS items
722
            INNER JOIN {$wpdb->posts} AS invoices ON invoices.ID = items.post_id
723
            WHERE items.item_id = %d AND invoices.post_status = 'publish'",
724
            $item_id
725
        )
726
    );
727
728
    return wp_parse_id_list( $ids );
0 ignored issues
show
Bug Best Practice introduced by
The expression return wp_parse_id_list($ids) returns the type integer[] which is incompatible with the documented return type integer.
Loading history...
729
}
730
731
/**
732
 * Returns an array of user IDs who purchased a given item.
733
 *
734
 * @since 2.6.17
735
 * @param int $user_id The user id.
736
 */
737
function getpaid_has_user_purchased_item( $user_id, $item_id ) {
738
    global $wpdb;
739
740
    if ( empty( $user_id ) ) {
741
        return false;
742
    }
743
744
    $count = $wpdb->get_var(
745
        $wpdb->prepare(
746
            "SELECT COUNT(*) FROM {$wpdb->prefix}getpaid_invoice_items AS items
747
            INNER JOIN {$wpdb->posts} AS invoices ON invoices.ID = items.post_id
748
            WHERE items.item_id = %d AND invoices.post_author = %d AND invoices.post_status = 'publish'
749
            LIMIT 1",
750
            $item_id,
751
            $user_id
752
        )
753
    );
754
755
    return ! empty( $count );
756
}
757
758
/**
759
 * Queries the db for the total spend by a given user ID.
760
 *
761
 * @since 2.6.17
762
 * @param int $user_id The user id.
763
 */
764
function getpaid_get_user_total_spend( $user_id ) {
765
    $args = array(
766
        'data'           => array(
767
768
            'total' => array(
769
                'type'     => 'invoice_data',
770
                'function' => 'SUM',
771
                'name'     => 'total_sales',
772
            ),
773
774
        ),
775
        'where'          => array(
776
777
            'author' => array(
778
                'type'     => 'post_data',
779
                'value'    => absint( $user_id ),
780
                'key'      => 'posts.post_author',
781
                'operator' => '=',
782
            ),
783
784
        ),
785
        'query_type'     => 'get_var',
786
        'invoice_status' => array( 'wpi-renewal', 'wpi-processing', 'publish' ),
787
    );
788
789
    return wpinv_round_amount( GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
0 ignored issues
show
Bug introduced by
It seems like GetPaid_Reports_Helper::...oice_report_data($args) can also be of type string; however, parameter $amount of wpinv_round_amount() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

789
    return wpinv_round_amount( /** @scrutinizer ignore-type */ GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
Loading history...
790
}
791
792
/**
793
 * Queries the db for the total invoices by a given user ID.
794
 *
795
 * @since 2.6.17
796
 * @param int $user_id The user id.
797
 */
798
function getpaid_count_user_invoices( $user_id ) {
799
    $args = array(
800
        'data'           => array(
801
802
            'ID' => array(
803
                'type'     => 'post_data',
804
                'function' => 'COUNT',
805
                'name'     => 'count',
806
                'distinct' => true,
807
            ),
808
809
        ),
810
        'where'          => array(
811
812
            'author' => array(
813
                'type'     => 'post_data',
814
                'value'    => absint( $user_id ),
815
                'key'      => 'posts.post_author',
816
                'operator' => '=',
817
            ),
818
819
        ),
820
        'query_type'     => 'get_var',
821
        'invoice_status' => array_keys( wpinv_get_invoice_statuses() ),
822
    );
823
824
    return absint( GetPaid_Reports_Helper::get_invoice_report_data( $args ) );
825
}
826