Passed
Push — master ( 09085e...d2d5ef )
by Brian
13:25
created

getpaid_user_ids_who_purchased_item()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 17
rs 9.9666
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, $prefix = '' ) {
61
62
    // Prepare user values.
63
    $prefix = preg_replace( '/\s+/', '', $prefix );
64
    $prefix = empty( $prefix ) ? $email : $prefix;
65
	$args   = array(
66
		'user_login' => wpinv_generate_user_name( $prefix ),
67
		'user_pass'  => wp_generate_password(),
68
		'user_email' => $email,
69
        'role'       => 'subscriber',
70
    );
71
72
    return wp_insert_user( $args );
73
74
}
75
76
/**
77
 *  Generates a unique user name from an email.
78
 *
79
 * @since 1.0.19
80
 * @return bool|WP_User
81
 */
82
function wpinv_generate_user_name( $prefix = '' ) {
83
84
    // If prefix is an email, retrieve the part before the email.
85
	$prefix = strtok( $prefix, '@' );
86
    $prefix = trim( $prefix, '.' );
87
88
	// Sanitize the username.
89
	$prefix = sanitize_user( $prefix, true );
90
91
	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
92
	if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) {
93
		$prefix = 'gtp_' . zeroise( wp_rand( 0, 9999 ), 4 );
94
	}
95
96
    $username = $prefix;
97
    $postfix  = 2;
98
99
    while ( username_exists( $username ) ) {
100
        $username = "{$prefix}{$postfix}";
101
        $postfix ++;
102
    }
103
104
    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...
105
}
106
107
/**
108
 * Returns an array of user content tabs.
109
 *
110
 * @since 1.0.19
111
 * @return array
112
 */
113
function getpaid_get_user_content_tabs() {
114
115
    $tabs = array(
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
        'gp-edit-address'  => array(
130
            'label'    => __( 'Billing Address', 'invoicing' ),
131
            'callback' => 'getpaid_display_address_edit_tab',
132
            'icon'     => 'fas fa-credit-card',
133
        ),
134
135
    );
136
137
    $tabs = apply_filters( 'getpaid_user_content_tabs', $tabs );
138
139
    // Make sure address editing is last on the list.
140
    if ( isset( $tabs['gp-edit-address'] ) ) {
141
        $address = $tabs['gp-edit-address'];
142
        unset( $tabs['gp-edit-address'] );
143
        $tabs['gp-edit-address'] = $address;
144
    }
145
146
    return $tabs;
147
}
148
149
/**
150
 * Prepares the contents of a tab.
151
 *
152
 * @since 1.0.19
153
 * @param array $tab
154
 * @return array
155
 */
156
function getpaid_prepare_user_content_tab( $tab ) {
157
158
    if ( ! empty( $tab['callback'] ) ) {
159
        return call_user_func( $tab['callback'] );
160
    }
161
162
    if ( ! empty( $tab['content'] ) ) {
163
        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...
164
    }
165
166
    $notice = aui()->alert(
167
        array(
168
            'content' => __( 'This tab has no content or content callback.', 'invoicing' ),
169
            'type'    => 'error',
170
        )
171
    );
172
173
    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...
174
}
175
176
/**
177
 * Generates the current integrations tab URL.
178
 *
179
 * @since 1.0.19
180
 * @param string $tab
181
 * @param string $default
182
 * @return array
183
 */
184
function getpaid_get_tab_url( $tab, $default ) {
185
    global $getpaid_tab_url;
186
187
    if ( empty( $getpaid_tab_url ) ) {
188
        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...
189
    }
190
191
    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...
192
193
}
194
195
/**
196
 * Generates the address edit tab.
197
 *
198
 * @since 2.1.4
199
 * @return string
200
 */
201
function getpaid_display_address_edit_tab() {
202
203
    if ( 0 === get_current_user_id() ) {
204
        return '<div class="bsui">' . aui()->alert(
205
            array(
206
                'type'        => 'error',
207
                'content'     => __( 'Your must be logged in to view this section', 'invoicing' ),
208
                'dismissible' => false,
209
            )
210
        ) . '</div>';
211
    }
212
213
    ob_start();
214
    ?>
215
        <div class="bsui">
216
            <?php wpinv_print_errors(); ?>
217
            <form method="post" class="getpaid-address-edit-form">
218
219
                <?php
220
221
                    foreach ( getpaid_user_address_fields() as $key => $label ) {
222
223
					// Display the country.
224
					if ( 'country' == $key ) {
225
226
						aui()->select(
227
							array(
228
								'options'     => wpinv_get_country_list(),
229
								'name'        => 'getpaid_address[' . esc_attr( $key ) . ']',
230
								'id'          => 'wpinv-' . sanitize_html_class( $key ),
231
								'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
232
								'placeholder' => $label,
233
								'label'       => wp_kses_post( $label ),
234
								'label_type'  => 'vertical',
235
								'class'       => 'getpaid-address-field',
236
                            ),
237
                            true
238
						);
239
240
					}
241
242
					// Display the state.
243
					elseif ( 'state' == $key ) {
244
245
						getpaid_get_states_select_markup(
246
							getpaid_get_user_address_field( get_current_user_id(), 'country' ),
247
							getpaid_get_user_address_field( get_current_user_id(), 'state' ),
248
							$label,
249
							$label,
250
							'',
251
							false,
252
							'',
253
							'getpaid_address[' . esc_attr( $key ) . ']',
254
                            true
255
						);
256
257
                        } else {
258
259
						aui()->input(
260
                            array(
261
                                'name'        => 'getpaid_address[' . esc_attr( $key ) . ']',
262
                                'id'          => 'wpinv-' . sanitize_html_class( $key ),
263
                                'placeholder' => $label,
264
                                'label'       => wp_kses_post( $label ),
265
                                'label_type'  => 'vertical',
266
                                'type'        => 'text',
267
                                'value'       => sanitize_text_field( getpaid_get_user_address_field( get_current_user_id(), $key ) ),
268
                                'class'       => 'getpaid-address-field',
269
                            ),
270
                            true
271
						);
272
273
                        }
274
                    }
275
276
                    aui()->input(
277
                        array(
278
                            'name'        => 'getpaid_address[email_cc]',
279
                            'id'          => 'wpinv-email_cc',
280
                            'placeholder' => '[email protected], [email protected]',
281
                            'label'       => __( 'Other email addresses', 'invoicing' ),
282
                            'label_type'  => 'vertical',
283
                            'type'        => 'text',
284
                            'value'       => sanitize_text_field( get_user_meta( get_current_user_id(), '_wpinv_email_cc', true ) ),
0 ignored issues
show
Bug introduced by
It seems like get_user_meta(get_curren..._wpinv_email_cc', true) can also be of type false; however, parameter $str of sanitize_text_field() does only seem to accept string, 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

284
                            'value'       => sanitize_text_field( /** @scrutinizer ignore-type */ get_user_meta( get_current_user_id(), '_wpinv_email_cc', true ) ),
Loading history...
285
                            'class'       => 'getpaid-address-field',
286
                            'help_text'   => __( 'Optionally provide other email addresses where we should send payment notifications', 'invoicing' ),
287
                        ),
288
                        true
289
                    );
290
291
                    do_action( 'getpaid_display_address_edit_tab' );
292
293
                    aui()->input(
294
                        array(
295
                            'name'      => 'getpaid_profile_edit_submit_button',
296
                            'id'        => 'getpaid_profile_edit_submit_button',
297
                            'value'     => __( 'Save Address', 'invoicing' ),
298
                            'help_text' => __( 'New invoices will use this address as the billing address.', 'invoicing' ),
299
                            'type'      => 'submit',
300
                            'class'     => 'btn btn-primary btn-block submit-button',
301
                        ),
302
                        true
303
                    );
304
305
                    wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' );
306
                    getpaid_hidden_field( 'getpaid-action', 'edit_billing_details' );
307
                ?>
308
309
            </form>
310
311
        </div>
312
    <?php
313
314
    return ob_get_clean();
315
}
316
add_shortcode( 'getpaid_edit_address', 'getpaid_display_address_edit_tab' );
317
318
/**
319
 * Saves the billing address edit tab.
320
 *
321
 * @since 2.1.4
322
 * @param array $data
323
 */
324
function getpaid_save_address_edit_tab( $data ) {
325
326
    if ( empty( $data['getpaid_address'] ) || ! is_array( $data['getpaid_address'] ) ) {
327
        return;
328
    }
329
330
    $data    = $data['getpaid_address'];
331
    $user_id = get_current_user_id();
332
333
    foreach ( array_keys( getpaid_user_address_fields() ) as $field ) {
334
335
        if ( isset( $data[ $field ] ) ) {
336
            $value = sanitize_text_field( $data[ $field ] );
337
            update_user_meta( $user_id, '_wpinv_' . $field, $value );
338
        }
339
    }
340
341
    if ( isset( $data['email_cc'] ) ) {
342
        update_user_meta( $user_id, '_wpinv_email_cc', sanitize_text_field( $data['email_cc'] ) );
343
    }
344
345
    wpinv_set_error( 'address_updated' );
346
}
347
add_action( 'getpaid_authenticated_action_edit_billing_details', 'getpaid_save_address_edit_tab' );
348
349
350
/*
351
 |--------------------------------------------------------------------------
352
 | UsersWP
353
 |--------------------------------------------------------------------------
354
 |
355
 | Functions that integrate GetPaid and UsersWP.
356
*/
357
358
/**
359
 * Add our tabs to UsersWP account tabs.
360
 *
361
 * @since 1.0.19
362
 * @param  array $tabs
363
 * @return array
364
 */
365
function getpaid_filter_userswp_account_tabs( $tabs ) {
366
367
    // Abort if the integration is inactive.
368
    if ( ! getpaid_is_userswp_integration_active() ) {
369
        return $tabs;
370
    }
371
372
    $new_tabs   = array();
373
374
    foreach ( getpaid_get_user_content_tabs() as $slug => $tab ) {
375
376
        $new_tabs[ $slug ] = array(
377
            'title' => $tab['label'],
378
            'icon'  => $tab['icon'],
379
        );
380
381
    }
382
383
    return array_merge( $tabs, $new_tabs );
384
}
385
add_filter( 'uwp_account_available_tabs', 'getpaid_filter_userswp_account_tabs' );
386
387
/**
388
 * Display our UsersWP account tabs.
389
 *
390
 * @since 1.0.19
391
 * @param  array $tabs
392
 * @return array
393
 */
394
function getpaid_display_userswp_account_tabs( $tab ) {
395
    global $getpaid_tab_url;
396
397
    $our_tabs = getpaid_get_user_content_tabs();
398
399
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
400
        $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

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

401
        echo wp_kses( /** @scrutinizer ignore-type */ getpaid_prepare_user_content_tab( $our_tabs[ $tab ] ), getpaid_allowed_html() );
Loading history...
402
    }
403
404
}
405
add_action( 'uwp_account_form_display', 'getpaid_display_userswp_account_tabs' );
406
407
function getpaid_allowed_html() {
408
    $allowed_html = wp_kses_allowed_html( 'post' );
409
410
	// form fields
411
    $allowed_html['form'] = array(
412
        'action'         => true,
413
        'accept'         => true,
414
        'accept-charset' => true,
415
        'enctype'        => true,
416
        'method'         => true,
417
        'name'           => true,
418
        'target'         => true,
419
    );
420
421
    // - input
422
	$allowed_html['input'] = array(
423
		'class'        => array(),
424
		'id'           => array(),
425
		'name'         => array(),
426
		'value'        => array(),
427
		'type'         => array(),
428
        'placeholder'  => array(),
429
        'autocomplete' => array(),
430
        'autofocus'    => array(),
431
        'required'     => array(),
432
        'disabled'     => array(),
433
        'readonly'     => array(),
434
        'checked'      => array(),
435
        'maxlength'    => array(),
436
        'pattern'      => array(),
437
        'min'          => array(),
438
        'max'          => array(),
439
        'step'         => array(),
440
        'size'         => array(),
441
	);
442
443
    // - input
444
	$allowed_html['textarea'] = array(
445
		'class' => array(),
446
		'id'    => array(),
447
		'name'  => array(),
448
		'value' => array(),
449
	);
450
451
	// select
452
	$allowed_html['select'] = array(
453
		'class'        => array(),
454
		'id'           => array(),
455
		'name'         => array(),
456
        'autocomplete' => array(),
457
        'multiple'     => array(),
458
	);
459
460
	// select options
461
	$allowed_html['option'] = array(
462
		'selected' => array(),
463
        'disabled' => array(),
464
        'value'    => array(),
465
	);
466
467
	return $allowed_html;
468
469
}
470
471
/**
472
 * Filters the account page title.
473
 *
474
 * @since  1.0.19
475
 * @param  string $title Current title.
476
 * @param  string $tab   Current tab.
477
 * @return string Title.
478
 */
479
function getpaid_filter_userswp_account_title( $title, $tab ) {
480
481
    $our_tabs   = getpaid_get_user_content_tabs();
482
483
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
484
        return $our_tabs[ $tab ]['label'];
485
    }
486
487
    return $title;
488
}
489
add_filter( 'uwp_account_page_title', 'getpaid_filter_userswp_account_title', 10, 2 );
490
491
/**
492
 * Registers the UsersWP integration settings.
493
 *
494
 * @since  1.0.19
495
 * @param  array $settings An array of integration settings.
496
 * @return array
497
 */
498
function getpaid_register_userswp_settings( $settings ) {
499
500
    if ( defined( 'USERSWP_PLUGIN_FILE' ) ) {
501
502
        $settings[] = array(
503
504
            'id'       => 'userswp',
505
            'label'    => __( 'UsersWP', 'invoicing' ),
506
            'settings' => array(
507
508
                'userswp_settings' => array(
509
                    'id'   => 'userswp_settings',
510
                    'name' => '<h3>' . __( 'UsersWP', 'invoicing' ) . '</h3>',
511
                    'type' => 'header',
512
                ),
513
514
                'enable_userswp'   => array(
515
                    'id'   => 'enable_userswp',
516
                    'name' => __( 'Enable Integration', 'invoicing' ),
517
                    'desc' => __( 'Display GetPaid items on UsersWP account page.', 'invoicing' ),
518
                    'type' => 'checkbox',
519
                    'std'  => 1,
520
                ),
521
522
            ),
523
524
        );
525
526
    }
527
528
    return $settings;
529
}
530
add_filter( 'getpaid_integration_settings', 'getpaid_register_userswp_settings' );
531
532
/**
533
 * Ovewrites the invoices history page to UsersWP.
534
 *
535
 * @since  2.3.1
536
 * @return bool
537
 */
538
function getpaid_userswp_overwrite_invoice_history_page( $url, $post_type ) {
539
540
    $our_tabs = getpaid_get_user_content_tabs();
541
    $tab      = "gp-{$post_type}s";
542
    if ( getpaid_is_userswp_integration_active() && isset( $our_tabs[ $tab ] ) ) {
543
        return add_query_arg( 'type', $tab, 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

543
        return add_query_arg( 'type', $tab, /** @scrutinizer ignore-call */ uwp_get_account_page_url() );
Loading history...
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...
544
    }
545
546
    return $url;
547
548
}
549
add_filter( 'wpinv_get_history_page_uri', 'getpaid_userswp_overwrite_invoice_history_page', 10, 2 );
550
551
/**
552
 * Checks if the integration is enabled.
553
 *
554
 * @since  1.0.19
555
 * @return bool
556
 */
557
function getpaid_is_userswp_integration_active() {
558
    $enabled = wpinv_get_option( 'enable_userswp', 1 );
559
    return defined( 'USERSWP_PLUGIN_FILE' ) && ! empty( $enabled );
560
}
561
562
/*
563
 |--------------------------------------------------------------------------
564
 | BuddyPress
565
 |--------------------------------------------------------------------------
566
 |
567
 | Functions that integrate GetPaid and BuddyPress.
568
*/
569
570
/**
571
 * Registers the BuddyPress integration settings.
572
 *
573
 * @since  2.1.5
574
 * @param  array $settings An array of integration settings.
575
 * @return array
576
 */
577
function getpaid_register_buddypress_settings( $settings ) {
578
579
    if ( class_exists( 'BuddyPress' ) ) {
580
581
        $settings[] = array(
582
583
            'id'       => 'buddypress',
584
            'label'    => __( 'BuddyPress', 'invoicing' ),
585
            'settings' => array(
586
587
                'buddypress_settings' => array(
588
                    'id'   => 'buddypress_settings',
589
                    'name' => '<h3>' . __( 'BuddyPress', 'invoicing' ) . '</h3>',
590
                    'type' => 'header',
591
                ),
592
593
                'enable_buddypress'   => array(
594
                    'id'   => 'enable_buddypress',
595
                    'name' => __( 'Enable Integration', 'invoicing' ),
596
                    'desc' => __( 'Display GetPaid items on BuddyPress account pages.', 'invoicing' ),
597
                    'type' => 'checkbox',
598
                    'std'  => 1,
599
                ),
600
601
            ),
602
603
        );
604
605
    }
606
607
    return $settings;
608
}
609
add_filter( 'getpaid_integration_settings', 'getpaid_register_buddypress_settings' );
610
611
/**
612
 * Checks if the integration is enabled.
613
 *
614
 * @since  2.1.5
615
 * @return bool
616
 */
617
function getpaid_is_buddypress_integration_active() {
618
    $enabled = wpinv_get_option( 'enable_buddypress', 1 );
619
    return class_exists( 'BuddyPress' ) && ! empty( $enabled );
620
}
621
622
/**
623
 * Loads the BuddyPress component.
624
 *
625
 * @since  2.1.5
626
 * @return bool
627
 */
628
function getpaid_setup_buddypress_integration() {
629
630
    if ( getpaid_is_buddypress_integration_active() ) {
631
        require_once WPINV_PLUGIN_DIR . 'includes/class-bp-getpaid-component.php';
632
        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

632
        /** @scrutinizer ignore-call */ 
633
        buddypress()->getpaid = new BP_GetPaid_Component();
Loading history...
633
    }
634
635
}
636
add_action( 'bp_setup_components', 'getpaid_setup_buddypress_integration' );
637
638
/**
639
 * Checks if a given user has purchased a given item.
640
 *
641
 * @since 2.7.3
642
 * @param int $item_id The item id.
643
 * @return int The IDs of users who purchased the item.
644
 */
645
function getpaid_user_ids_who_purchased_item( $item_id ) {
646
    global $wpdb;
647
648
    if ( empty( $item_id ) ) {
649
        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...
650
    }
651
652
    $ids = $wpdb->get_col(
653
        $wpdb->prepare(
654
            "SELECT DISTINCT invoices.post_author FROM {$wpdb->prefix}getpaid_invoice_items AS items
655
            INNER JOIN {$wpdb->posts} AS invoices ON invoices.ID = items.post_id
656
            WHERE items.item_id = %d AND invoices.post_status = 'publish'",
657
            $item_id
658
        )
659
    );
660
661
    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...
662
}
663
664
/**
665
 * Returns an array of user IDs who purchased a given item.
666
 *
667
 * @since 2.6.17
668
 * @param int $user_id The user id.
669
 */
670
function getpaid_has_user_purchased_item( $user_id, $item_id ) {
671
    global $wpdb;
672
673
    if ( empty( $user_id ) ) {
674
        return false;
675
    }
676
677
    $count = $wpdb->get_var(
678
        $wpdb->prepare(
679
            "SELECT COUNT(*) FROM {$wpdb->prefix}getpaid_invoice_items AS items
680
            INNER JOIN {$wpdb->posts} AS invoices ON invoices.ID = items.post_id
681
            WHERE items.item_id = %d AND invoices.post_author = %d AND invoices.post_status = 'publish'
682
            LIMIT 1",
683
            $item_id,
684
            $user_id
685
        )
686
    );
687
688
    return ! empty( $count );
689
}
690