Passed
Pull Request — master (#69)
by
unknown
04:21
created

wpinv-user-functions.php ➔ wpinv_user_invoices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
// Exit if accessed directly
3
if ( ! defined( 'ABSPATH' ) ) exit;
4
5
function wpinv_get_users_invoices( $user = 0, $number = 20, $pagination = false, $status = 'publish' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $number is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
6
    if ( empty( $user ) ) {
7
        $user = get_current_user_id();
8
    }
9
10
    if ( 0 === $user ) {
11
        return false;
12
    }
13
14
    if ( $pagination ) {
15
        if ( get_query_var( 'paged' ) )
16
            $paged = get_query_var('paged');
0 ignored issues
show
Unused Code introduced by
$paged is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
17
        else if ( get_query_var( 'page' ) )
18
            $paged = get_query_var( 'page' );
0 ignored issues
show
Unused Code introduced by
$paged is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
        else
20
            $paged = 1;
0 ignored issues
show
Unused Code introduced by
$paged is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
21
    }
22
23
    $args = array(
24
        'post_type'      => 'wpi_invoice',
25
        'posts_per_page' => 20,
26
        'paged'          => null,
27
        'post_status'    => array( 'publish', 'pending' ),
28
        'user'           => $user,
29
        'order'          => 'date',
30
    );
31
32
    $invoices = get_posts( $args );
33
34
    // No invoices
35
    if ( ! $invoices )
36
        return false;
37
38
    return $invoices;
39
}
40
41
function wpinv_dropdown_users( $args = '' ) {
42
    $defaults = array(
43
        'show_option_all' => '', 'show_option_none' => '', 'hide_if_only_one_author' => '',
44
        'orderby' => 'display_name', 'order' => 'ASC',
45
        'include' => '', 'exclude' => '', 'multi' => 0,
46
        'show' => 'display_name', 'echo' => 1,
47
        'selected' => 0, 'name' => 'user', 'class' => '', 'id' => '',
48
        'blog_id' => $GLOBALS['blog_id'], 'who' => '', 'include_selected' => false,
49
        'option_none_value' => -1
50
    );
51
52
    $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0;
53
54
    $r = wp_parse_args( $args, $defaults );
55
56
    $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order', 'who' ) );
57
58
    $fields = array( 'ID', 'user_login', 'user_email' );
59
60
    $show = ! empty( $r['show'] ) ? $r['show'] : 'display_name';
61
    if ( 'display_name_with_login' === $show ) {
62
        $fields[] = 'display_name';
63
    } else if ( 'display_name_with_email' === $show ) {
64
        $fields[] = 'display_name';
65
    } else {
66
        $fields[] = $show;
67
    }
68
69
    $query_args['fields'] = $fields;
70
71
    $show_option_all = $r['show_option_all'];
72
    $show_option_none = $r['show_option_none'];
73
    $option_none_value = $r['option_none_value'];
74
75
    $query_args = apply_filters( 'wpinv_dropdown_users_args', $query_args, $r );
76
77
    $users = get_users( $query_args );
78
79
    $output = '';
80
    if ( ! empty( $users ) && ( empty( $r['hide_if_only_one_author'] ) || count( $users ) > 1 ) ) {
81
        $name = esc_attr( $r['name'] );
82
        if ( $r['multi'] && ! $r['id'] ) {
83
            $id = '';
84
        } else {
85
            $id = $r['id'] ? " id='" . esc_attr( $r['id'] ) . "'" : " id='$name'";
86
        }
87
        $output = "<select name='{$name}'{$id} class='" . $r['class'] . "'>\n";
88
89
        if ( $show_option_all ) {
90
            $output .= "\t<option value='0'>$show_option_all</option>\n";
91
        }
92
93
        if ( $show_option_none ) {
94
            $_selected = selected( $option_none_value, $r['selected'], false );
95
            $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n";
96
        }
97
98
        if ( $r['include_selected'] && ( $r['selected'] > 0 ) ) {
99
            $found_selected = false;
100
            $r['selected'] = (int) $r['selected'];
101
            foreach ( (array) $users as $user ) {
102
                $user->ID = (int) $user->ID;
103
                if ( $user->ID === $r['selected'] ) {
104
                    $found_selected = true;
105
                }
106
            }
107
108
            if ( ! $found_selected ) {
109
                $users[] = get_userdata( $r['selected'] );
110
            }
111
        }
112
113
        foreach ( (array) $users as $user ) {
114
            if ( 'display_name_with_login' === $show ) {
115
                /* translators: 1: display name, 2: user_login */
116
                $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_login );
117
            } elseif ( 'display_name_with_email' === $show ) {
118
                /* translators: 1: display name, 2: user_email */
119
                if ( $user->display_name == $user->user_email ) {
120
                    $display = $user->display_name;
121
                } else {
122
                    $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_email );
123
                }
124
            } elseif ( ! empty( $user->$show ) ) {
125
                $display = $user->$show;
126
            } else {
127
                $display = '(' . $user->user_login . ')';
128
            }
129
130
            $_selected = selected( $user->ID, $r['selected'], false );
131
            $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n";
132
        }
133
134
        $output .= "</select>";
135
    }
136
137
    $html = apply_filters( 'wpinv_dropdown_users', $output );
138
139
    if ( $r['echo'] ) {
140
        echo $html;
141
    }
142
    return $html;
143
}
144
145
function wpinv_guest_redirect( $redirect_to, $user_id = 0 ) {
146
    if ( (int)wpinv_get_option( 'guest_checkout' ) && $user_id > 0 ) {
147
        wpinv_login_user( $user_id );
148
    } else {
149
        $redirect_to = wp_login_url( $redirect_to );
150
    }
151
    
152
    $redirect_to = apply_filters( 'wpinv_invoice_link_guest_redirect', $redirect_to, $user_id );
153
    
154
    wp_redirect( $redirect_to );
155
}
156
157
function wpinv_login_user( $user_id ) {
158
    if ( is_user_logged_in() ) {
159
        return true;
160
    }
161
    
162
    $user = get_user_by( 'id', $user_id );
163
    
164
    if ( !empty( $user ) && !is_wp_error( $user ) && !empty( $user->user_login ) ) {
165
        wp_set_current_user( $user_id, $user->user_login );
166
        wp_set_auth_cookie( $user_id );
167
        do_action( 'wp_login', $user->user_login );
168
        
169
        return true;
170
    }
171
    
172
    return false;
173
}