Passed
Push — master ( bdf164...c6d931 )
by Brian
05:05
created

GetPaid_Reports   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 55
c 4
b 0
f 0
dl 0
loc 142
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A export_invoices() 0 5 2
A display_exports_tab() 0 4 1
A __construct() 0 6 1
A display_reports_page() 0 42 5
A get_tabs() 0 8 1
A register_reports_page() 0 9 1
A display_reports_tab() 0 4 1
A download_graph() 0 5 2
1
<?php
2
/**
3
 * Contains the reports class
4
 *
5
 *
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * GetPaid_Reports Class.
12
 */
13
class GetPaid_Reports {
14
15
	/**
16
	 * Class constructor.
17
	 *
18
	 */
19
	public function __construct() {
20
		add_action( 'admin_menu', array( $this, 'register_reports_page' ), 20 );
21
		add_action( 'wpinv_reports_tab_reports', array( $this, 'display_reports_tab' ) );
22
		add_action( 'wpinv_reports_tab_export', array( $this, 'display_exports_tab' ) );
23
		add_action( 'getpaid_authenticated_admin_action_download_graph', array( $this, 'download_graph' ) );
24
		add_action( 'getpaid_authenticated_admin_action_export_invoices', array( $this, 'export_invoices' ) );
25
26
	}
27
28
	/**
29
	 * Registers the reports page.
30
	 *
31
	 */
32
	public function register_reports_page() {
33
34
		add_submenu_page(
35
            'wpinv',
36
            __( 'Reports', 'invoicing' ),
37
            __( 'Reports', 'invoicing' ),
38
            wpinv_get_capability(),
39
            'wpinv-reports',
40
            array( $this, 'display_reports_page' )
41
		);
42
43
	}
44
45
	/**
46
	 * Displays the reports page.
47
	 *
48
	 */
49
	public function display_reports_page() {
50
51
		// Prepare variables.
52
		$tabs        = $this->get_tabs();
53
		$current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'reports';
54
		$current_tab = array_key_exists( $current_tab, $tabs ) ? $current_tab : 'reports';
55
56
		// Display the current tab.
57
		?>
58
59
        <div class="wrap">
60
61
			<h1><?php echo sanitize_text_field( $tabs[ $current_tab ] ); ?></h1>
62
63
			<nav class="nav-tab-wrapper">
64
65
				<?php
66
					foreach( $tabs as $key => $label ) {
67
68
						$key   = sanitize_text_field( $key );
69
						$label = sanitize_text_field( $label );
70
						$class = $key == $current_tab ? 'nav-tab nav-tab-active' : 'nav-tab';
71
						$url   = esc_url(
72
							add_query_arg( 'tab', $key, admin_url( 'admin.php?page=wpinv-reports' ) )
73
						);
74
75
						echo "\n\t\t\t<a href='$url' class='$class'>$label</a>";
76
77
					}
78
				?>
79
80
			</nav>
81
82
			<div class="bsui <?php echo esc_attr( $current_tab ); ?>">
83
				<?php do_action( "wpinv_reports_tab_{$current_tab}" ); ?>
84
			</div>
85
86
        </div>
87
		<?php
88
89
			wp_enqueue_script( 'chart-js', WPINV_PLUGIN_URL . 'assets/js/chart.bundle.min.js', array( 'jquery' ), '2.9.4', true );
90
			wp_enqueue_style( 'chart-js', WPINV_PLUGIN_URL . 'assets/css/chart.min.css', array(), '2.9.4' );
91
92
	}
93
94
	/**
95
	 * Retrieves reports page tabs.
96
	 *
97
	 * @return array
98
	 */
99
	public function get_tabs() {
100
101
		$tabs = array(
102
			'reports' => __( 'Reports', 'invoicing' ),
103
			'export'  => __( 'Export', 'invoicing' ),
104
		);
105
106
		return apply_filters( 'getpaid_report_tabs', $tabs );
107
	}
108
109
	/**
110
	 * Displays the reports tab.
111
	 *
112
	 */
113
	public function display_reports_tab() {
114
115
		$reports = new GetPaid_Reports_Report();
116
		$reports->display();
117
118
	}
119
120
	/**
121
	 * Displays the exports tab.
122
	 *
123
	 */
124
	public function display_exports_tab() {
125
126
		$exports = new GetPaid_Reports_Export();
127
		$exports->display();
128
129
	}
130
131
	/**
132
	 * Donwnloads a graph.
133
	 *
134
	 * @param array $args
135
	 */
136
	public function download_graph( $args ) {
137
138
		if ( ! empty( $args['graph'] ) ) {
139
			$downloader = new GetPaid_Graph_Downloader();
140
			$downloader->download( $args['graph'] );
141
		}
142
143
	}
144
145
	/**
146
	 * Exports invoices.
147
	 *
148
	 * @param array $args
149
	 */
150
	public function export_invoices( $args ) {
151
152
		if ( ! empty( $args['post_type'] ) ) {
153
			$downloader = new GetPaid_Invoice_Exporter();
154
			$downloader->export( $args['post_type'], $args );
155
		}
156
157
	}
158
159
}
160