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