Passed
Push — master ( 6a5c27...530be4 )
by Stiofan
41s queued 14s
created

WPInv_BP_Component::setup_nav()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 0
dl 0
loc 33
rs 8.7697
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    public function setup_nav() {
30
31
        if ( wpinv_get_option( 'wpinv_bp_hide_menu' ) || !is_user_logged_in()) {
32
            return;
33
        }
34
35
        if(bp_displayed_user_id() != bp_loggedin_user_id() && !current_user_can('administrator')){
36
            return;
37
        }
38
39
        $count = $this->get_invoice_count();
40
        $class = ( 0 === $count ) ? 'no-count' : 'count';
41
42
        $main_nav_name = sprintf(
43
            __( 'My Invoices %s', 'invoicing' ),
44
            sprintf(
45
                '<span class="%s">%s</span>',
46
                esc_attr( $class ),
47
                bp_core_number_format( $count )
48
            )
49
        );
50
51
        $main_nav = array(
52
            'name'                => $main_nav_name,
53
            'slug'                => WPINV_BP_SLUG,
54
            'position'            => $this->position,
55
            'screen_function'     => array( $this, 'invoices_screen' ),
56
            'default_subnav_slug' => 'invoices',
57
            'item_css_id'         => $this->id
58
        );
59
60
        bp_core_new_nav_item( $main_nav );
61
    }
62
    
63
    public function invoices_screen() {
64
        if ( wpinv_get_option( 'wpinv_bp_hide_menu' ) ) {
65
            return;
66
        }
67
        
68
        add_action( 'bp_template_content', array( $this, 'invoices_content' ) );
69
70
        $template = apply_filters( 'bp_core_template_plugin', 'members/single/plugins' );
71
        
72
        bp_core_load_template( apply_filters( 'wpinv_bp_core_template_plugin', $template ) );
73
    }
74
    
75
    public function invoices_content() {
76
        if ( $this->has_invoices( bp_ajax_querystring( 'invoices' ) ) ) {
77
            global $invoices_template;
78
            
79
            do_action( 'wpinv_bp_invoices_before_content' );
80
            ?>
81
            <div class="wpi-g wpi-bp-invoices invoices invoicing" style="position:relative">
82
                <div id="pag-top" class="pagination">
83
                    <div class="pag-count" id="invoice-dir-count-top">
84
                        <?php echo $this->pagination_count(); ?>
85
                    </div>
86
                    <div class="pagination-links" id="invoice-dir-pag-top">
87
                        <?php echo $this->pagination_links(); ?>
88
                    </div>
89
                </div>
90
                <table class="table table-bordered table-hover table-responsive wpi-user-invoices" style="margin:0">
91
                    <thead>
92
                        <tr>
93 View Code Duplication
                            <?php foreach ( wpinv_get_user_invoices_columns() as $column_id => $column_name ) : ?>
94
                                <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>
95
                            <?php endforeach; ?>
96
                        </tr>
97
                    </thead>
98
                    <tbody>
99 View Code Duplication
                        <?php foreach ( $invoices_template->invoices as $invoice ) {
100
                            ?>
101
                            <tr class="wpinv-item wpinv-item-<?php echo $invoice_status = $invoice->get_status(); ?>">
102
                                <?php foreach ( wpinv_get_user_invoices_columns() as $column_id => $column_name ) : ?>
103
                                    <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'] ); ?>">
104
                                        <?php if ( has_action( 'wpinv_user_invoices_column_' . $column_id ) ) : ?>
105
                                            <?php do_action( 'wpinv_user_invoices_column_' . $column_id, $invoice ); ?>
106
107
                                        <?php elseif ( 'invoice-number' === $column_id ) : ?>
108
                                            <a href="<?php echo esc_url( $invoice->get_view_url() ); ?>">
109
                                                <?php echo _x( '#', 'hash before invoice number', 'invoicing' ) . $invoice->get_number(); ?>
110
                                            </a>
111
112
                                        <?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' ); ?>
113
                                            <time datetime="<?php echo strtotime( $dateYMD ); ?>" title="<?php echo $dateYMD; ?>"><?php echo $date; ?></time>
114
115
                                        <?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 ); ?>
116
                                            <time datetime="<?php echo strtotime( $dateYMD ); ?>" title="<?php echo $dateYMD; ?>"><?php echo $date; ?></time>
117
118
                                        <?php elseif ( 'invoice-status' === $column_id ) : ?>
119
                                            <?php echo wpinv_invoice_status_label( $invoice_status, $invoice->get_status( true ) ) ; ?>
120
121
                                        <?php elseif ( 'invoice-total' === $column_id ) : ?>
122
                                            <?php echo $invoice->get_total( true ); ?>
123
124
                                        <?php elseif ( 'invoice-actions' === $column_id ) : ?>
125
                                            <?php
126
                                                $actions = array(
127
                                                    'pay'    => array(
128
                                                        'url'  => $invoice->get_checkout_payment_url(),
129
                                                        'name' => __( 'Pay Now', 'invoicing' ),
130
                                                        'class' => 'btn-success'
131
                                                    ),
132
                                                    'print'   => array(
133
                                                        'url'  => $invoice->get_view_url(),
134
                                                        'name' => __( 'Print', 'invoicing' ),
135
                                                        'class' => 'btn-primary',
136
                                                        'attrs' => 'target="_blank"'
137
                                                    )
138
                                                );
139
140
                                                if ( ! $invoice->needs_payment() ) {
141
                                                    unset( $actions['pay'] );
142
                                                }
143
144
                                                if ( $actions = apply_filters( 'wpinv_user_invoices_actions', $actions, $invoice ) ) {
145
                                                    foreach ( $actions as $key => $action ) {
146
                                                        $class = !empty($action['class']) ? sanitize_html_class($action['class']) : '';
147
                                                        echo '<a href="' . esc_url( $action['url'] ) . '" class="btn btn-sm ' . $class . ' ' . sanitize_html_class( $key ) . '" ' . ( !empty($action['attrs']) ? $action['attrs'] : '' ) . '>' . $action['name'] . '</a>';
148
                                                    }
149
                                                }
150
                                            ?>
151
                                        <?php endif; ?>
152
                                    </td>
153
                                <?php endforeach; ?>
154
                            </tr>
155
                        <?php } ?>
156
                    </tbody>
157
                </table>
158
                <div id="pag-bottom" class="pagination">
159
                    <div class="pag-count" id="invoice-dir-count-bottom">
160
                        <?php echo $this->pagination_count(); ?>
161
                    </div>
162
                    <div class="pagination-links" id="invoice-dir-pag-bottom">
163
                        <?php echo $this->pagination_links(); ?>
164
                    </div>
165
                </div>
166
                <script type="text/javascript">
167
                jQuery('.wpi-bp-invoices .pagination-links').click(function(e) {
168
                    jQuery('table.wpi-user-invoices').css({'opacity': '0.67'});
169
                    jQuery('.wpi-bp-invoices').append('<span style="position:absolute;top:49.5%;left:49.5%;"><i class="fa fa-spin fa-refresh"></i></span>');
170
                });
171
                </script>
172
            </div>
173
            <?php
174
        
175
            do_action( 'wpinv_bp_invoices_after_content' );
176
        } else {
177
            ?>
178
            <div id="message" class="info">
179
                <p><?php _e( 'No invoice has been made yet.', 'invoicing' ); ?></p>
180
            </div>
181
            <?php
182
        }
183
        
184
        if ( defined( 'DOING_AJAX' ) ) {
185
            exit;
186
        }
187
    }
188
    
189
    public function has_invoices( $args = '' ) {
190
        global $invoices_template;
191
192
        $per_page = absint( wpinv_get_option( 'wpinv_bp_per_page' ) );
193
        // Parse arguments.
194
        $r = bp_parse_args( $args, array(
195
            'status'            => 'all',
196
            'page_arg'          => 'bpage',
197
            'page'              => 1,
198
            'per_page'          => $per_page > 0 ? $per_page : 20,
199
            'max'               => false,
200
            'user_id'           => bp_displayed_user_id(),
201
        ), 'has_invoices' );
202
203
204
        if ( ! empty( $r['max'] ) && ( (int)$r['per_page'] > (int)$r['max'] ) ) {
205
            $r['per_page'] = (int)$r['max'];
206
        }
207
208
        // Get the invoices.
209
        $invoices_template = new WPInv_BP_Invoices_Template( $r['status'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['page_arg'] );
210
211
        return apply_filters( 'wpinv_bp_has_invoices', $invoices_template->has_invoices(), $invoices_template, $r );
212
    }
213
    
214
    public function get_invoice_count() {
215
        $query      = apply_filters( 'wpinv_user_invoices_count_query', array( 'status' => 'all','user' => bp_displayed_user_id(), 'limit' => '-1', 'return' => 'ids', 'paginate' => false ) );
216
        $invoices   = wpinv_get_invoices( $query );
217
        
218
        return !empty( $invoices ) ? count( $invoices ) : 0;
219
    }
220
    
221
    public function pagination_count() {
222
        global $invoices_template;
223
224
        $start_num = intval( ( $invoices_template->pag_page - 1 ) * $invoices_template->pag_num ) + 1;
225
        $from_num  = bp_core_number_format( $start_num );
226
        $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 ) );
227
        $total     = bp_core_number_format( $invoices_template->total_invoice_count );
228
229
        if ( 1 == $invoices_template->total_invoice_count ) {
230
            $message = __( 'Viewing 1 invoice', 'invoicing' );
231
        } else {
232
            $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 );
233
        }
234
235
        return $message;
236
    }
237
    
238
    function pagination_links() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
239
        global $invoices_template;
240
241
        return apply_filters( 'wpinv_bp_get_pagination_links', $invoices_template->pag_links );
242
    }
243
    
244
    public function bp_section( $settings = array() ) {
245
        $settings['wpinv_bp'] = __( 'BuddyPress Integration', 'invoicing' );
246
        return $settings;
247
    }
248
    
249
    public function bp_settings( $settings = array() ) {
250
        $settings['wpinv_bp'] = array(
251
            'wpinv_bp_labels' => array(
252
                'id'   => 'wpinv_bp_settings',
253
                'name' => '<h3>' . __( 'BuddyPress Integration', 'invoicing' ) . '</h3>',
254
                'desc' => '',
255
                'type' => 'header',
256
            ),
257
            'wpinv_bp_hide_menu' => array(
258
                'id'   => 'wpinv_bp_hide_menu',
259
                'name' => __( 'Hide Invoices link', 'invoicing' ),
260
                'desc' => __( 'Hide Invoices link from BP Profile menu.', 'invoicing' ),
261
                'type' => 'checkbox',
262
            ),
263
            'wpinv_menu_position' => array(
264
                'id'   => 'wpinv_menu_position',
265
                'name' => __( 'Menu position', 'invoicing' ),
266
                'desc' => __( 'Menu position for the Invoices link in BP Profile menu.', 'invoicing' ),
267
                'type' => 'number',
268
                'size' => 'small',
269
                'min'  => '1',
270
                'max'  => '100000',
271
                'step' => '1',
272
                'std'  => '91'
273
            ),
274
            'wpinv_bp_per_page' => array(
275
                'id'   => 'wpinv_bp_per_page',
276
                'name' => __( 'Max invoices per page', 'invoicing' ),
277
                'desc' => __( 'Enter a number to lists the invoices for each page.', 'invoicing' ),
278
                'type' => 'number',
279
                'size' => 'small',
280
                'min'  => '1',
281
                'max'  => '1000',
282
                'step' => '1',
283
                'std'  => '20'
284
            ),
285
        );
286
        
287
        return $settings;
288
    }
289
290
    public function wp_nav_menu_objects($items, $args){
291
        if(!is_user_logged_in()){
292
            return $items;
293
        }
294
295
        if(!apply_filters('wpinv_bp_invoice_history_redirect', true, $items, $args)){
296
            return $items;
297
        }
298
299
        $user_id = get_current_user_id();
300
        $link = bp_core_get_user_domain( $user_id ).WPINV_BP_SLUG;
301
        $history_link = wpinv_get_history_page_uri();
302
        foreach ( $items as $item ) {
303
            $item->url = str_replace( $history_link, $link, $item->url );
304
        }
305
306
        return $items;
307
    }
308
}
309
310
class WPInv_BP_Invoices_Template {
311
    public $current_invoice = -1;
312
    public $invoice_count = 0;
313
    public $invoices = array();
314
    public $invoice;
315
    public $in_the_loop = false;
316
    public $pag_page = 1;
317
    public $pag_num = 20;
318
    public $pag_links = '';
319
    public $total_invoice_count = 0;
320
    
321
    public function __construct( $status, $page, $per_page, $max, $user_id, $page_arg = 'bpage' ) {
322
        $this->invoices = array( 'invoices' => array(), 'total' => 0 );
323
        
324
        $this->pag_arg  = sanitize_key( $page_arg );
0 ignored issues
show
Bug introduced by
The property pag_arg does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
325
        $this->pag_page = bp_sanitize_pagination_arg( $this->pag_arg, $page );
326
        $this->pag_num  = bp_sanitize_pagination_arg( 'num', $per_page );
327
328
        $query_args     = array( 'user' => $user_id, 'page' => $this->pag_page, 'limit' => $this->pag_num, 'return' => 'self', 'paginate' => true );
329
        if ( !empty( $status ) && $status != 'all' ) {
330
           $query_args['status'] = $status;
331
        }
332
        $invoices  = wpinv_get_invoices( apply_filters( 'wpinv_bp_user_invoices_query', $query_args ) );
333
        
334
        if ( !empty( $invoices ) && !empty( $invoices->found_posts ) ) {
335
            $this->invoices['invoices'] = array_map( 'wpinv_get_invoice', $invoices->posts );
336
            $this->invoices['total']    = $invoices->found_posts;
337
        }
338
339
        if ( empty( $max ) || ( $max >= (int)$this->invoices['total'] ) ) {
340
            $this->total_invoice_count = (int)$this->invoices['total'];
341
        } else {
342
            $this->total_invoice_count = (int)$max;
343
        }
344
345
        $this->invoices = $this->invoices['invoices'];
346
347
        $invoice_count = count( $this->invoices );
348
349
        if ( empty( $max ) || ( $max >= (int)$invoice_count ) ) {
350
            $this->invoice_count = (int)$invoice_count;
351
        } else {
352
            $this->invoice_count = (int)$max;
353
        }
354
        
355
        if ( ! empty( $this->total_invoice_count ) && ! empty( $this->pag_num ) ) {
356
            $this->pag_links = paginate_links( array(
357
                'base'      => add_query_arg( $this->pag_arg, '%#%' ),
358
                'format'    => '',
359
                'total'     => ceil( (int)$this->total_invoice_count / (int)$this->pag_num ),
360
                'current'   => (int)$this->pag_page,
361
                'prev_text' => _x( '&larr;', 'Invoice pagination previous text', 'invoicing' ),
362
                'next_text' => _x( '&rarr;', 'Invoice pagination next text',     'invoicing' ),
363
                'mid_size'  => 1,
364
                'add_args'  => array(),
365
            ) );
366
        }
367
    }
368
369
    public function has_invoices() {
370
        return (bool) ! empty( $this->invoice_count );
371
    }
372
373
    public function next_invoice() {
374
        $this->current_invoice++;
375
        $this->invoice = $this->invoices[ $this->current_invoice ];
376
377
        return $this->invoice;
378
    }
379
380
    public function rewind_invoices() {
381
        $this->current_invoice = -1;
382
        if ( $this->invoice_count > 0 ) {
383
            $this->invoice = $this->invoices[0];
384
        }
385
    }
386
387
    public function invoices() {
388
        if ( ( $this->current_invoice + 1 ) < $this->invoice_count ) {
389
            return true;
390
        } elseif ( ( $this->current_invoice + 1 ) === $this->invoice_count ) {
391
392
            do_action( 'wpinv_bp_invoice_loop_end' );
393
            
394
            $this->rewind_invoices();
395
        }
396
397
        $this->in_the_loop = false;
398
        return false;
399
    }
400
401
    public function the_invoice() {
402
403
        $this->in_the_loop = true;
404
        $this->invoice     = $this->next_invoice();
405
406
        if ( 0 === $this->current_invoice ) {
407
            do_action( 'wpinv_bp_invoice_loop_start' );
408
        }
409
    }
410
}
411
412
function wpinv_bp_setup_component() {
413
414
    if(!class_exists( 'BuddyPress' )){
415
        return;
416
    }
417
418
    new WPInv_BP_Component();
419
420
}
421
add_action( 'bp_loaded', 'wpinv_bp_setup_component' );