Passed
Pull Request — master (#444)
by Brian
10:32
created

GetPaid_Reports_Report_Gateways::get_labels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * Contains the class that displays the gateways report.
4
 *
5
 *
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * GetPaid_Reports_Report_Items Class.
12
 */
13
class GetPaid_Reports_Report_Gateways extends GetPaid_Reports_Abstract_Report {
14
15
	/**
16
	 * Retrieves the earning sql.
17
	 *
18
	 */
19
	public function get_sql( $range ) {
20
		global $wpdb;
21
22
		$table      = $wpdb->prefix . 'getpaid_invoices';
23
		$clauses    = $this->get_range_sql( $range );
24
25
		$sql        = "SELECT
26
				meta.gateway AS gateway,
27
				SUM(total) as total
28
            FROM $wpdb->posts
29
            LEFT JOIN $table as meta ON meta.post_id = $wpdb->posts.ID
30
            WHERE meta.post_id IS NOT NULL
31
                AND $wpdb->posts.post_type = 'wpi_invoice'
32
                AND ( $wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'wpi-renewal' )
33
                AND {$clauses[1]}
34
            GROUP BY gateway
35
			ORDER BY total DESC
36
        ";
37
38
		return apply_filters( 'getpaid_gateways_graphs_get_sql', $sql, $range );
39
40
	}
41
42
	/**
43
	 * Prepares the report stats.
44
	 *
45
	 */
46
	public function prepare_stats() {
47
		global $wpdb;
48
		$this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) );
49
		$this->stats = $this->normalize_stats( $this->stats );
50
	}
51
52
	/**
53
	 * Normalizes the report stats.
54
	 *
55
	 */
56
	public function normalize_stats( $stats ) {
57
		$normalized = array();
58
		$others     = 0;
59
		$did        = 0;
60
61
		foreach ( $stats as $stat ) {
62
63
			if ( $did > 4 ) {
64
65
				$others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) );
66
67
			} else {
68
69
				$normalized[] = array(
70
					'total'     => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ),
71
					'gateway'   => strip_tags( wpinv_get_gateway_admin_label( $stat->gateway ) ),
72
				);
73
74
			}
75
76
			$did++;
77
		}
78
79
		if ( $others > 0 ) {
80
81
			$normalized[] = array(
82
				'total'     => wpinv_round_amount( wpinv_sanitize_amount( $others ) ),
83
				'gateway'   => esc_html__( 'Others', 'invoicing' ),
84
			);
85
86
		}
87
88
		return $normalized;
89
	}
90
91
	/**
92
	 * Retrieves report data.
93
	 *
94
	 */
95
	public function get_data() {
96
97
		$data     = wp_list_pluck( $this->stats, 'total' );
98
		$colors   = array( '#007bff','#28a745','#333333','#c3e6cb','#dc3545','#6c757d', '#00bcd4', '#00695C', '#03a9f4', '#2196f3', '#3f51b5', '#673ab7', '#9c27b0', '#e91e63', '#f44336', '#8bc34a', '#4caf50' );
99
100
		shuffle( $colors );
101
102
		return array(
103
			'data'            => $data,
104
			'backgroundColor' => $colors,
105
		);
106
107
	}
108
109
	/**
110
	 * Retrieves report labels.
111
	 *
112
	 */
113
	public function get_labels() {
114
		return wp_list_pluck( $this->stats, 'gateway' );
115
	}
116
117
	/**
118
	 * Displays the actual report.
119
	 *
120
	 */
121
	public function display_stats() {
122
		?>
123
124
			<canvas id="getpaid-chartjs-earnings-gateways"></canvas>
125
126
			<script>
127
				window.addEventListener( 'DOMContentLoaded', function() {
128
129
					var ctx = document.getElementById( 'getpaid-chartjs-earnings-gateways' ).getContext('2d');
130
					new Chart(
131
						ctx,
132
						{
133
							type: 'doughnut',
134
							data: {
135
								'labels': <?php echo wp_json_encode( $this->get_labels() ); ?>,
0 ignored issues
show
Bug introduced by
Are you sure wp_json_encode($this->get_labels()) of type false|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
								'labels': <?php echo /** @scrutinizer ignore-type */ wp_json_encode( $this->get_labels() ); ?>,
Loading history...
136
								'datasets': [ <?php echo wp_json_encode( $this->get_data() ); ?> ]
0 ignored issues
show
Bug introduced by
Are you sure wp_json_encode($this->get_data()) of type false|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
								'datasets': [ <?php echo /** @scrutinizer ignore-type */ wp_json_encode( $this->get_data() ); ?> ]
Loading history...
137
							},
138
							options: {
139
								legend: {
140
									display: false
141
								}
142
							}
143
						}
144
					);
145
146
				})
147
148
			</script>
149
150
		<?php
151
	}
152
153
}
154