1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; // Exit if accessed directly |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
class WPInv_BP_Component { |
7
|
|
|
public $position; |
8
|
|
|
public $count; |
9
|
|
|
|
10
|
|
|
public function __construct() { |
11
|
|
|
|
12
|
|
|
if ( !defined( 'WPINV_BP_SLUG' ) ) { |
13
|
|
|
define( 'WPINV_BP_SLUG', 'invoices' ); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
add_action( 'wp_ajax_invoicing_filter', array( $this, 'invoices_content' ) ); |
17
|
|
|
add_action( 'wp_ajax_nopriv_invoicing_filter', array( $this, 'invoices_content' ) ); |
18
|
|
|
add_filter( 'wpinv_settings_sections_general', array( $this, 'bp_section' ), 10, 1 ); |
19
|
|
|
add_filter( 'wpinv_settings_general', array( $this, 'bp_settings' ), 10, 1 ); |
20
|
|
|
add_filter( 'wp_nav_menu_objects', array( $this, 'wp_nav_menu_objects' ), 10, 2 ); |
21
|
|
|
add_action('bp_setup_nav', array($this, 'setup_nav'), 15); |
22
|
|
|
|
23
|
|
|
$position = wpinv_get_option( 'wpinv_menu_position' ); |
24
|
|
|
$position = $position !== '' && $position !== false ? $position : 91; |
25
|
|
|
$this->position = apply_filters( 'wpinv_bp_nav_position', $position ); |
26
|
|
|
$this->id = WPINV_BP_SLUG; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setup_nav() { |
30
|
|
|
|
31
|
|
|
if ( wpinv_get_option( 'wpinv_bp_hide_menu' ) ) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->setup_invoice_count(); |
36
|
|
|
$class = ( 0 === $this->count ) ? 'no-count' : 'count'; |
37
|
|
|
|
38
|
|
|
$main_nav_name = sprintf( |
39
|
|
|
__( 'My Invoices %s', 'invoicing' ), |
40
|
|
|
sprintf( |
41
|
|
|
'<span class="%s">%s</span>', |
42
|
|
|
esc_attr( $class ), |
43
|
|
|
bp_core_number_format( $this->count ) |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$main_nav = array( |
48
|
|
|
'name' => $main_nav_name, |
49
|
|
|
'slug' => WPINV_BP_SLUG, |
50
|
|
|
'position' => $this->position, |
51
|
|
|
'screen_function' => array( $this, 'invoices_screen' ), |
52
|
|
|
'default_subnav_slug' => 'invoices', |
53
|
|
|
'item_css_id' => $this->id |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
bp_core_new_nav_item( $main_nav ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function invoices_screen() { |
60
|
|
|
if ( wpinv_get_option( 'wpinv_bp_hide_menu' ) ) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
add_action( 'bp_template_content', array( $this, 'invoices_content' ) ); |
65
|
|
|
|
66
|
|
|
$template = apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ); |
67
|
|
|
|
68
|
|
|
bp_core_load_template( apply_filters( 'wpinv_bp_core_template_plugin', $template ) ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function invoices_content() { |
72
|
|
|
if ( $this->has_invoices( bp_ajax_querystring( 'invoices' ) ) ) { |
73
|
|
|
global $invoices_template; |
74
|
|
|
|
75
|
|
|
do_action( 'wpinv_bp_invoices_before_content' ); |
76
|
|
|
?> |
77
|
|
|
<div class="wpi-g wpi-bp-invoices invoices invoicing" style="position:relative"> |
78
|
|
|
<div id="pag-top" class="pagination"> |
79
|
|
|
<div class="pag-count" id="invoice-dir-count-top"> |
80
|
|
|
<?php echo $this->pagination_count(); ?> |
81
|
|
|
</div> |
82
|
|
|
<div class="pagination-links" id="invoice-dir-pag-top"> |
83
|
|
|
<?php echo $this->pagination_links(); ?> |
84
|
|
|
</div> |
85
|
|
|
</div> |
86
|
|
|
<table class="table table-bordered table-hover table-responsive wpi-user-invoices" style="margin:0"> |
87
|
|
|
<thead> |
88
|
|
|
<tr> |
89
|
|
View Code Duplication |
<?php foreach ( wpinv_get_user_invoices_columns() as $column_id => $column_name ) : ?> |
90
|
|
|
<th class="<?php echo esc_attr( $column_id ); ?> <?php echo (!empty($column_name['class']) ? $column_name['class'] : '');?>"><span class="nobr"><?php echo esc_html( $column_name['title'] ); ?></span></th> |
91
|
|
|
<?php endforeach; ?> |
92
|
|
|
</tr> |
93
|
|
|
</thead> |
94
|
|
|
<tbody> |
95
|
|
View Code Duplication |
<?php foreach ( $invoices_template->invoices as $invoice ) { |
96
|
|
|
?> |
97
|
|
|
<tr class="wpinv-item wpinv-item-<?php echo $invoice_status = $invoice->get_status(); ?>"> |
98
|
|
|
<?php foreach ( wpinv_get_user_invoices_columns() as $column_id => $column_name ) : ?> |
99
|
|
|
<td class="<?php echo esc_attr( $column_id ); ?> <?php echo (!empty($column_name['class']) ? $column_name['class'] : '');?>" data-title="<?php echo esc_attr( $column_name['title'] ); ?>"> |
100
|
|
|
<?php if ( has_action( 'wpinv_user_invoices_column_' . $column_id ) ) : ?> |
101
|
|
|
<?php do_action( 'wpinv_user_invoices_column_' . $column_id, $invoice ); ?> |
102
|
|
|
|
103
|
|
|
<?php elseif ( 'invoice-number' === $column_id ) : ?> |
104
|
|
|
<a href="<?php echo esc_url( $invoice->get_view_url() ); ?>"> |
105
|
|
|
<?php echo _x( '#', 'hash before invoice number', 'invoicing' ) . $invoice->get_number(); ?> |
106
|
|
|
</a> |
107
|
|
|
|
108
|
|
|
<?php elseif ( 'created-date' === $column_id ) : $date = wpinv_get_date_created( $invoice->ID ); $dateYMD = wpinv_get_date_created( $invoice->ID, 'Y-m-d H:i:s' ); ?> |
109
|
|
|
<time datetime="<?php echo strtotime( $dateYMD ); ?>" title="<?php echo $dateYMD; ?>"><?php echo $date; ?></time> |
110
|
|
|
|
111
|
|
|
<?php elseif ( 'payment-date' === $column_id ) : $date = wpinv_get_invoice_date( $invoice->ID, '', false ); $dateYMD = wpinv_get_invoice_date( $invoice->ID, 'Y-m-d H:i:s', false ); ?> |
112
|
|
|
<time datetime="<?php echo strtotime( $dateYMD ); ?>" title="<?php echo $dateYMD; ?>"><?php echo $date; ?></time> |
113
|
|
|
|
114
|
|
|
<?php elseif ( 'invoice-status' === $column_id ) : ?> |
115
|
|
|
<?php echo wpinv_invoice_status_label( $invoice_status, $invoice->get_status( true ) ) ; ?> |
116
|
|
|
|
117
|
|
|
<?php elseif ( 'invoice-total' === $column_id ) : ?> |
118
|
|
|
<?php echo $invoice->get_total( true ); ?> |
119
|
|
|
|
120
|
|
|
<?php elseif ( 'invoice-actions' === $column_id ) : ?> |
121
|
|
|
<?php |
122
|
|
|
$actions = array( |
123
|
|
|
'pay' => array( |
124
|
|
|
'url' => $invoice->get_checkout_payment_url(), |
125
|
|
|
'name' => __( 'Pay Now', 'invoicing' ), |
126
|
|
|
'class' => 'btn-success' |
127
|
|
|
), |
128
|
|
|
'print' => array( |
129
|
|
|
'url' => $invoice->get_view_url(), |
130
|
|
|
'name' => __( 'Print', 'invoicing' ), |
131
|
|
|
'class' => 'btn-primary', |
132
|
|
|
'attrs' => 'target="_blank"' |
133
|
|
|
) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
if ( ! $invoice->needs_payment() ) { |
137
|
|
|
unset( $actions['pay'] ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ( $actions = apply_filters( 'wpinv_user_invoices_actions', $actions, $invoice ) ) { |
141
|
|
|
foreach ( $actions as $key => $action ) { |
142
|
|
|
$class = !empty($action['class']) ? sanitize_html_class($action['class']) : ''; |
143
|
|
|
echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-sm ' . $class . ' ' . sanitize_html_class( $key ) . '" ' . ( !empty($action['attrs']) ? $action['attrs'] : '' ) . '>' . $action['name'] . '</a>'; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
?> |
147
|
|
|
<?php endif; ?> |
148
|
|
|
</td> |
149
|
|
|
<?php endforeach; ?> |
150
|
|
|
</tr> |
151
|
|
|
<?php } ?> |
152
|
|
|
</tbody> |
153
|
|
|
</table> |
154
|
|
|
<div id="pag-bottom" class="pagination"> |
155
|
|
|
<div class="pag-count" id="invoice-dir-count-bottom"> |
156
|
|
|
<?php echo $this->pagination_count(); ?> |
157
|
|
|
</div> |
158
|
|
|
<div class="pagination-links" id="invoice-dir-pag-bottom"> |
159
|
|
|
<?php echo $this->pagination_links(); ?> |
160
|
|
|
</div> |
161
|
|
|
</div> |
162
|
|
|
<script type="text/javascript"> |
163
|
|
|
jQuery('.wpi-bp-invoices .pagination-links').click(function(e) { |
164
|
|
|
jQuery('table.wpi-user-invoices').css({'opacity': '0.67'}); |
165
|
|
|
jQuery('.wpi-bp-invoices').append('<span style="position:absolute;top:49.5%;left:49.5%;"><i class="fa fa-spin fa-refresh"></i></span>'); |
166
|
|
|
}); |
167
|
|
|
</script> |
168
|
|
|
</div> |
169
|
|
|
<?php |
170
|
|
|
|
171
|
|
|
do_action( 'wpinv_bp_invoices_after_content' ); |
172
|
|
|
} else { |
173
|
|
|
?> |
174
|
|
|
<div id="message" class="info"> |
175
|
|
|
<p><?php _e( 'No invoice has been made yet.', 'invoicing' ); ?></p> |
176
|
|
|
</div> |
177
|
|
|
<?php |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ( defined( 'DOING_AJAX' ) ) { |
181
|
|
|
exit; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function has_invoices( $args = '' ) { |
186
|
|
|
global $invoices_template; |
187
|
|
|
|
188
|
|
|
$per_page = absint( wpinv_get_option( 'wpinv_bp_per_page' ) ); |
189
|
|
|
// Parse arguments. |
190
|
|
|
$r = bp_parse_args( $args, array( |
191
|
|
|
'status' => 'all', |
192
|
|
|
'page_arg' => 'bpage', |
193
|
|
|
'page' => 1, |
194
|
|
|
'per_page' => $per_page > 0 ? $per_page : 20, |
195
|
|
|
'max' => false, |
196
|
|
|
'user_id' => bp_loggedin_user_id(), |
197
|
|
|
), 'has_invoices' ); |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
if ( ! empty( $r['max'] ) && ( (int)$r['per_page'] > (int)$r['max'] ) ) { |
201
|
|
|
$r['per_page'] = (int)$r['max']; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Get the invoices. |
205
|
|
|
$invoices_template = new WPInv_BP_Invoices_Template( $r['status'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['page_arg'] ); |
206
|
|
|
|
207
|
|
|
return apply_filters( 'wpinv_bp_has_invoices', $invoices_template->has_invoices(), $invoices_template, $r ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function setup_invoice_count() { |
211
|
|
|
$query = apply_filters( 'wpinv_user_invoices_count_query', array( 'user' => bp_loggedin_user_id(), 'limit' => '-1', 'return' => 'ids', 'paginate' => false ) ); |
212
|
|
|
$invoices = wpinv_get_invoices( $query ); |
213
|
|
|
|
214
|
|
|
$this->count = !empty( $invoices ) ? count( $invoices ) : 0; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function pagination_count() { |
218
|
|
|
global $invoices_template; |
219
|
|
|
|
220
|
|
|
$start_num = intval( ( $invoices_template->pag_page - 1 ) * $invoices_template->pag_num ) + 1; |
221
|
|
|
$from_num = bp_core_number_format( $start_num ); |
222
|
|
|
$to_num = bp_core_number_format( ( $start_num + ( $invoices_template->pag_num - 1 ) > $invoices_template->total_invoice_count ) ? $invoices_template->total_invoice_count : $start_num + ( $invoices_template->pag_num - 1 ) ); |
223
|
|
|
$total = bp_core_number_format( $invoices_template->total_invoice_count ); |
224
|
|
|
|
225
|
|
|
if ( 1 == $invoices_template->total_invoice_count ) { |
226
|
|
|
$message = __( 'Viewing 1 invoice', 'invoicing' ); |
227
|
|
|
} else { |
228
|
|
|
$message = sprintf( _n( 'Viewing %1$s - %2$s of %3$s invoice', 'Viewing %1$s - %2$s of %3$s invoices', $invoices_template->total_invoice_count, 'invoicing' ), $from_num, $to_num, $total ); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $message; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
function pagination_links() { |
|
|
|
|
235
|
|
|
global $invoices_template; |
236
|
|
|
|
237
|
|
|
return apply_filters( 'wpinv_bp_get_pagination_links', $invoices_template->pag_links ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function bp_section( $settings = array() ) { |
241
|
|
|
$settings['wpinv_bp'] = __( 'BuddyPress Integration', 'invoicing' ); |
242
|
|
|
return $settings; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function bp_settings( $settings = array() ) { |
246
|
|
|
$settings['wpinv_bp'] = array( |
247
|
|
|
'wpinv_bp_labels' => array( |
248
|
|
|
'id' => 'wpinv_bp_settings', |
249
|
|
|
'name' => '<h3>' . __( 'BuddyPress Integration', 'invoicing' ) . '</h3>', |
250
|
|
|
'desc' => '', |
251
|
|
|
'type' => 'header', |
252
|
|
|
), |
253
|
|
|
'wpinv_bp_hide_menu' => array( |
254
|
|
|
'id' => 'wpinv_bp_hide_menu', |
255
|
|
|
'name' => __( 'Hide Invoices link', 'invoicing' ), |
256
|
|
|
'desc' => __( 'Hide Invoices link from BP Profile menu.', 'invoicing' ), |
257
|
|
|
'type' => 'checkbox', |
258
|
|
|
), |
259
|
|
|
'wpinv_menu_position' => array( |
260
|
|
|
'id' => 'wpinv_menu_position', |
261
|
|
|
'name' => __( 'Menu position', 'invoicing' ), |
262
|
|
|
'desc' => __( 'Menu position for the Invoices link in BP Profile menu.', 'invoicing' ), |
263
|
|
|
'type' => 'number', |
264
|
|
|
'size' => 'small', |
265
|
|
|
'min' => '1', |
266
|
|
|
'max' => '100000', |
267
|
|
|
'step' => '1', |
268
|
|
|
'std' => '91' |
269
|
|
|
), |
270
|
|
|
'wpinv_bp_per_page' => array( |
271
|
|
|
'id' => 'wpinv_bp_per_page', |
272
|
|
|
'name' => __( 'Max invoices per page', 'invoicing' ), |
273
|
|
|
'desc' => __( 'Enter a number to lists the invoices for each page.', 'invoicing' ), |
274
|
|
|
'type' => 'number', |
275
|
|
|
'size' => 'small', |
276
|
|
|
'min' => '1', |
277
|
|
|
'max' => '1000', |
278
|
|
|
'step' => '1', |
279
|
|
|
'std' => '20' |
280
|
|
|
), |
281
|
|
|
); |
282
|
|
|
|
283
|
|
|
return $settings; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function wp_nav_menu_objects($items, $args){ |
287
|
|
|
if(!is_user_logged_in()){ |
288
|
|
|
return $items; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
if(!apply_filters('wpinv_bp_invoice_history_redirect', true, $items, $args)){ |
292
|
|
|
return $items; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$user_id = get_current_user_id(); |
296
|
|
|
$link = bp_core_get_user_domain( $user_id ).WPINV_BP_SLUG; |
297
|
|
|
$history_link = wpinv_get_history_page_uri(); |
298
|
|
|
foreach ( $items as $item ) { |
299
|
|
|
$item->url = str_replace( $history_link, $link, $item->url ); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $items; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
class WPInv_BP_Invoices_Template { |
307
|
|
|
public $current_invoice = -1; |
308
|
|
|
public $invoice_count = 0; |
309
|
|
|
public $invoices = array(); |
310
|
|
|
public $invoice; |
311
|
|
|
public $in_the_loop = false; |
312
|
|
|
public $pag_page = 1; |
313
|
|
|
public $pag_num = 20; |
314
|
|
|
public $pag_links = ''; |
315
|
|
|
public $total_invoice_count = 0; |
316
|
|
|
|
317
|
|
|
public function __construct( $status, $page, $per_page, $max, $user_id, $page_arg = 'bpage' ) { |
318
|
|
|
$this->invoices = array( 'invoices' => array(), 'total' => 0 ); |
319
|
|
|
|
320
|
|
|
$this->pag_arg = sanitize_key( $page_arg ); |
|
|
|
|
321
|
|
|
$this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page ); |
322
|
|
|
$this->pag_num = bp_sanitize_pagination_arg( 'num', $per_page ); |
323
|
|
|
|
324
|
|
|
$query_args = array( 'user' => $user_id, 'page' => $this->pag_page, 'limit' => $this->pag_num, 'return' => 'self', 'paginate' => true ); |
325
|
|
|
if ( !empty( $status ) && $status != 'all' ) { |
326
|
|
|
$query_args['status'] = $status; |
327
|
|
|
} |
328
|
|
|
$invoices = wpinv_get_invoices( apply_filters( 'wpinv_bp_user_invoices_query', $query_args ) ); |
329
|
|
|
|
330
|
|
|
if ( !empty( $invoices ) && !empty( $invoices->found_posts ) ) { |
331
|
|
|
$this->invoices['invoices'] = array_map( 'wpinv_get_invoice', $invoices->posts ); |
332
|
|
|
$this->invoices['total'] = $invoices->found_posts; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
if ( empty( $max ) || ( $max >= (int)$this->invoices['total'] ) ) { |
336
|
|
|
$this->total_invoice_count = (int)$this->invoices['total']; |
337
|
|
|
} else { |
338
|
|
|
$this->total_invoice_count = (int)$max; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
$this->invoices = $this->invoices['invoices']; |
342
|
|
|
|
343
|
|
|
$invoice_count = count( $this->invoices ); |
344
|
|
|
|
345
|
|
|
if ( empty( $max ) || ( $max >= (int)$invoice_count ) ) { |
346
|
|
|
$this->invoice_count = (int)$invoice_count; |
347
|
|
|
} else { |
348
|
|
|
$this->invoice_count = (int)$max; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
if ( ! empty( $this->total_invoice_count ) && ! empty( $this->pag_num ) ) { |
352
|
|
|
$this->pag_links = paginate_links( array( |
353
|
|
|
'base' => add_query_arg( $this->pag_arg, '%#%' ), |
354
|
|
|
'format' => '', |
355
|
|
|
'total' => ceil( (int)$this->total_invoice_count / (int)$this->pag_num ), |
356
|
|
|
'current' => (int)$this->pag_page, |
357
|
|
|
'prev_text' => _x( '←', 'Invoice pagination previous text', 'invoicing' ), |
358
|
|
|
'next_text' => _x( '→', 'Invoice pagination next text', 'invoicing' ), |
359
|
|
|
'mid_size' => 1, |
360
|
|
|
'add_args' => array(), |
361
|
|
|
) ); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function has_invoices() { |
366
|
|
|
return (bool) ! empty( $this->invoice_count ); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function next_invoice() { |
370
|
|
|
$this->current_invoice++; |
371
|
|
|
$this->invoice = $this->invoices[ $this->current_invoice ]; |
372
|
|
|
|
373
|
|
|
return $this->invoice; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function rewind_invoices() { |
377
|
|
|
$this->current_invoice = -1; |
378
|
|
|
if ( $this->invoice_count > 0 ) { |
379
|
|
|
$this->invoice = $this->invoices[0]; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function invoices() { |
384
|
|
|
if ( ( $this->current_invoice + 1 ) < $this->invoice_count ) { |
385
|
|
|
return true; |
386
|
|
|
} elseif ( ( $this->current_invoice + 1 ) === $this->invoice_count ) { |
387
|
|
|
|
388
|
|
|
do_action( 'wpinv_bp_invoice_loop_end' ); |
389
|
|
|
|
390
|
|
|
$this->rewind_invoices(); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$this->in_the_loop = false; |
394
|
|
|
return false; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function the_invoice() { |
398
|
|
|
|
399
|
|
|
$this->in_the_loop = true; |
400
|
|
|
$this->invoice = $this->next_invoice(); |
401
|
|
|
|
402
|
|
|
if ( 0 === $this->current_invoice ) { |
403
|
|
|
do_action( 'wpinv_bp_invoice_loop_start' ); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
function wpinv_bp_setup_component() { |
409
|
|
|
|
410
|
|
|
new WPInv_BP_Component(); |
411
|
|
|
|
412
|
|
|
} |
413
|
|
|
add_action( 'bp_loaded', 'wpinv_bp_setup_component' ); |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: