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

GetPaid_Reports_Export::display_markup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * Contains the class that displays the reports export page.
4
 *
5
 *
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 * GetPaid_Reports_Export Class.
12
 */
13
class GetPaid_Reports_Export {
14
15
	/**
16
	 * Displays the reports tab.
17
	 *
18
	 */
19
	public function display() {
20
21
		echo "<div class='row mt-4' style='max-width: 920px;' >";
22
		foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) {
23
			$this->display_post_type_export( $post_type );
24
		}
25
		echo "</div>";
26
27
	}
28
29
	/**
30
	 * Retrieves the download url.
31
	 *
32
	 */
33
	public function get_download_url( $post_type ) {
34
35
		return wp_nonce_url(
36
			add_query_arg(
37
				array(
38
					'getpaid-admin-action' => 'export_invoices',
39
					'post_type'            => urlencode( $post_type ),
40
				)
41
			),
42
			'getpaid-nonce',
43
			'getpaid-nonce'
44
		);
45
46
	}
47
48
	/**
49
	 * Displays a single post type export card.
50
	 *
51
	 */
52
	public function display_post_type_export( $post_type ) {
53
54
		?>
55
56
		<div class="col-12 col-md-6">
57
			<div class="card m-0 p-0" style="max-width:100%">
58
59
				<div class="card-header">
60
					<strong>
61
						<?php
62
							printf(
63
								__( 'Export %s', 'invoicing' ),
64
								sanitize_text_field( getpaid_get_post_type_label( $post_type ) )
65
							);
66
						?>
67
					</strong>
68
				</div>
69
70
				<div class="card-body">
71
72
					<form method="post" action="<?php echo esc_url( $this->get_download_url( $post_type ) ); ?>">
73
74
						<?php
75
							$this->display_markup( $this->generate_from_date( $post_type ) );
76
							$this->display_markup( $this->generate_to_date( $post_type ) );
77
							$this->display_markup( $this->generate_post_status_select( $post_type ) );
78
							$this->display_markup( $this->generate_file_type_select( $post_type ) );
79
							submit_button( __( 'Download', 'invoicing' ) );
80
						?>
81
82
					</form>
83
84
				</div>
85
86
			</div>
87
		</div>
88
89
		<?php
90
91
	}
92
93
	/**
94
	 * Generates the from date input field.
95
	 *
96
	 */
97
	public function generate_from_date( $post_type ) {
98
99
		return aui()->input(
100
			array(
101
				'name'       => 'from_date',
102
				'id'         => esc_attr( "$post_type-from_date" ),
103
				'placeholder'=> 'yy-mm-dd',
104
				'label'      => __( 'From Date', 'invoicing' ),
105
				'label_type' => 'vertical',
106
				'label_class' => 'd-block',
107
				'type'       => 'datepicker',
108
			)
109
		);
110
111
	}
112
113
	/**
114
	 * Generates the to date input field.
115
	 *
116
	 */
117
	public function generate_to_date( $post_type ) {
118
119
		return aui()->input(
120
			array(
121
				'name'       => 'to_date',
122
				'id'         => esc_attr( "$post_type-to_date" ),
123
				'placeholder'=> 'yy-mm-dd',
124
				'label'      => __( 'To Date', 'invoicing' ),
125
				'label_type' => 'vertical',
126
				'label_class' => 'd-block',
127
				'type'       => 'datepicker',
128
			)
129
		);
130
131
	}
132
133
	/**
134
	 * Generates the to post status select field.
135
	 *
136
	 */
137
	public function generate_post_status_select( $post_type ) {
138
139
		return aui()->select(
140
			array(
141
				'name'        => 'status',
142
				'id'          => esc_attr( "$post_type-status" ),
143
				'placeholder' => __( 'All Statuses', 'invoicing' ),
144
				'label'       => __( 'Status', 'invoicing' ),
145
				'label_type'  => 'vertical',
146
				'label_class' => 'd-block',
147
				'options'     => wpinv_get_invoice_statuses( true, false, $post_type ),
148
			)
149
		);
150
151
	}
152
153
	/**
154
	 * Generates the to file type select field.
155
	 *
156
	 */
157
	public function generate_file_type_select( $post_type ) {
158
159
		return aui()->select(
160
			array(
161
				'name'        => 'file_type',
162
				'id'          => esc_attr( "$post_type-file_type" ),
163
				'placeholder' => __( 'Select File Type', 'invoicing' ),
164
				'label'       => __( 'Export File', 'invoicing' ),
165
				'label_type'  => 'vertical',
166
				'label_class' => 'd-block',
167
				'options'     => array(
168
					'csv'  => __( 'CSV', 'invoicing' ),
169
					'xml'  => __( 'XML', 'invoicing' ),
170
					'json' => __( 'JSON', 'invoicing' ),
171
				),
172
			)
173
		);
174
175
	}
176
177
	/**
178
	 * Displays a field's markup.
179
	 *
180
	 */
181
	public function display_markup( $markup ) {
182
183
		echo str_replace(
184
			array(
185
				'form-control',
186
				'custom-select'
187
			),
188
			'regular-text',
189
			$markup
190
		);
191
192
	}
193
194
}
195