Passed
Push — master ( 1a4515...55ed87 )
by Stiofan
02:32 queued 12s
created

WPInv_BP_Component::wp_nav_menu_objects()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 18
rs 9.6666
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 extends BP_Component {
7
    public $position;
8
    public $slug;
9
    public $count;
10
    
11
    public function __construct() {
12
        global $bp;
13
        
14
        if ( !defined( 'WPINV_BP_SLUG' ) ) {
15
            define( 'WPINV_BP_SLUG', 'invoices' );
16
        }
17
        
18
        $position       = wpinv_get_option( 'wpinv_menu_position' );
19
        $position       = $position !== '' && $position !== false ? $position : 91;
20
        $this->position = apply_filters( 'wpinv_bp_nav_position', $position );
21
        $this->slug     = WPINV_BP_SLUG;
22
        
23
        parent::start(
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (start() instead of __construct()). Are you sure this is correct? If so, you might want to change this to $this->start().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

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