Passed
Push — master ( 5bc2fd...5f8ee6 )
by Stiofan
27s
created

WPInv_Admin_Users   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 3
A __construct() 0 4 1
A wpinv_add_user_column() 0 4 1
A wpinv_user_column_content() 0 9 2
A get_user_invoices() 0 22 2
1
<?php
2
/**
3
 * Contains functions related to Invoicing users.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
 
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
class WPInv_Admin_Users {
15
    private static $instance;
16
    
17
    public static function run() {
18
        if ( !isset( self::$instance ) && !( self::$instance instanceof WPInv_Admin_Users ) ) {
19
            self::$instance = new WPInv_Admin_Users;
20
        }
21
22
        return self::$instance;
23
    }
24
    
25
    public function __construct() {
26
        add_filter( 'manage_users_columns', array( $this, 'wpinv_add_user_column') );
27
        add_filter( 'manage_users_custom_column', array( $this, 'wpinv_user_column_content') , 10, 3 );
28
    }
29
30
    /**
31
     * Adds a new backend user column.
32
     *
33
     * @param $column
34
     *
35
     * @return mixed
36
     */
37
    public function wpinv_add_user_column( $column ) {
38
        $column['wpinvoicing'] = __('Invoicing','invoicing');
39
        return $column;
40
    }
41
42
    /**
43
     * Add the backend user column content.
44
     *
45
     * @param $val
46
     * @param $column_name
47
     * @param $user_id
48
     *
49
     * @return string
50
     */
51
    function wpinv_user_column_content( $val, $column_name, $user_id ) {
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...
52
        switch ($column_name) {
53
            case 'wpinvoicing' :
54
                return $this->get_user_invoices( $user_id );
55
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
56
            default:
57
        }
58
        return $val;
59
    }
60
61
    /**
62
     * Get the backend user invoices.
63
     *
64
     * @param $user_id
65
     *
66
     * @return string
67
     */
68
    public function get_user_invoices($user_id){
69
        $output = '';
70
        $wp_query_args = array(
71
            'post_type'      => 'wpi_invoice',
72
            'post_status'    => array('wpi-pending', 'publish', 'wpi-processing', 'wpi-onhold', 'wpi-refunded', 'wpi-cancelled', 'wpi-failed', 'wpi-renewal'),
73
            'posts_per_page' => -1,
74
            'fields'         => 'ids',
75
            'author'         => $user_id,
76
        );
77
        $invoices = new WP_Query( $wp_query_args );
78
        $count = absint( $invoices->found_posts );
79
80
        if(empty($count)){
81
            $output .= __('No Invoice(s)','invoicing');
82
        }else{
83
            $link_url = admin_url( "edit.php?post_type=wpi_invoice&author=".absint($user_id) );
84
            $link_text = sprintf( __('Invoices ( %d )', 'invoicing'), $count );
85
            $output .= "<a href='$link_url' >$link_text</a>";
86
        }
87
88
        return apply_filters('wpinv_user_invoice_content', $output, $user_id);
89
    }
90
91
}