Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WPInv_Reports often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPInv_Reports, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class WPInv_Reports { |
||
7 | private $section = 'wpinv_reports'; |
||
|
|||
8 | private $wp_filesystem; |
||
9 | private $export_dir; |
||
10 | private $export_url; |
||
11 | private $export; |
||
12 | public $filetype; |
||
13 | public $per_page; |
||
14 | |||
15 | public function __construct() { |
||
16 | $this->init(); |
||
17 | $this->includes(); |
||
18 | $this->actions(); |
||
19 | } |
||
20 | |||
21 | public function init() { |
||
22 | global $wp_filesystem; |
||
23 | |||
24 | if ( empty( $wp_filesystem ) ) { |
||
25 | require_once( ABSPATH . '/wp-admin/includes/file.php' ); |
||
26 | WP_Filesystem(); |
||
27 | global $wp_filesystem; |
||
28 | } |
||
29 | $this->wp_filesystem = $wp_filesystem; |
||
30 | |||
31 | $this->export_dir = $this->export_location(); |
||
32 | $this->export_url = $this->export_location( true ); |
||
33 | $this->export = 'invoicing'; |
||
34 | $this->filetype = 'csv'; |
||
35 | $this->per_page = 20; |
||
36 | |||
37 | do_action( 'wpinv_class_reports_init', $this ); |
||
38 | } |
||
39 | |||
40 | public function includes() { |
||
41 | do_action( 'wpinv_class_reports_includes', $this ); |
||
42 | } |
||
43 | |||
44 | public function actions() { |
||
45 | if ( is_admin() ) { |
||
46 | add_action( 'admin_menu', array( $this, 'add_submenu' ), 10 ); |
||
47 | add_action( 'wpinv_reports_tab_export', array( $this, 'export' ) ); |
||
48 | add_action( 'wp_ajax_wpinv_ajax_export', array( $this, 'ajax_export' ) ); |
||
49 | |||
50 | // Export Invoices. |
||
51 | add_action( 'wpinv_export_set_params_invoices', array( $this, 'set_invoices_export' ) ); |
||
52 | add_filter( 'wpinv_export_get_columns_invoices', array( $this, 'get_invoices_columns' ) ); |
||
53 | add_filter( 'wpinv_export_get_data_invoices', array( $this, 'get_invoices_data' ) ); |
||
54 | add_filter( 'wpinv_get_export_status_invoices', array( $this, 'invoices_export_status' ) ); |
||
55 | } |
||
56 | do_action( 'wpinv_class_reports_actions', $this ); |
||
57 | } |
||
58 | |||
59 | public function add_submenu() { |
||
60 | global $wpi_reports_page; |
||
61 | $wpi_reports_page = add_submenu_page( 'wpinv', __( 'Reports', 'invoicing' ), __( 'Reports', 'invoicing' ), wpinv_get_capability(), 'wpinv-reports', array( $this, 'reports_page' ) ); |
||
62 | } |
||
63 | |||
64 | public function reports_page() { |
||
65 | if ( !wp_script_is( 'postbox', 'enqueued' ) ) { |
||
66 | wp_enqueue_script( 'postbox' ); |
||
67 | } |
||
68 | if ( !wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) ) { |
||
69 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
||
70 | } |
||
71 | |||
72 | $current_page = admin_url( 'admin.php?page=wpinv-reports' ); |
||
73 | $active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'export'; |
||
74 | ?> |
||
75 | <div class="wrap wpi-reports-wrap"> |
||
76 | <h1><?php echo esc_html( __( 'Reports', 'invoicing' ) ); ?></h1> |
||
77 | <h2 class="nav-tab-wrapper wp-clearfix"> |
||
78 | <a href="<?php echo add_query_arg( array( 'tab' => 'export', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'export' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Export', 'invoicing' ); ?></a> |
||
79 | <?php do_action( 'wpinv_reports_page_tabs' ); ;?> |
||
80 | </h2> |
||
81 | <div class="wpi-reports-content wpi-reports-<?php echo $active_tab; ?>"> |
||
82 | <?php |
||
83 | do_action( 'wpinv_reports_page_top' ); |
||
84 | do_action( 'wpinv_reports_tab_' . $active_tab ); |
||
85 | do_action( 'wpinv_reports_page_bottom' ); |
||
86 | ?> |
||
87 | </div> |
||
88 | <?php |
||
89 | } |
||
90 | |||
91 | public function export() { |
||
92 | $statuses = wpinv_get_invoice_statuses( true ); |
||
93 | $statuses = array_merge( array( 'any' => __( 'All Statuses', 'invoicing' ) ), $statuses ); |
||
94 | ?> |
||
95 | <div class="metabox-holder"> |
||
96 | <div id="post-body"> |
||
97 | <div id="post-body-content"> |
||
98 | <?php do_action( 'wpinv_reports_tab_export_content_top' ); ?> |
||
99 | |||
100 | <div class="postbox wpi-export-invoices"> |
||
101 | <h2 class="hndle ui-sortabled-handle"><span><?php _e( 'Invoices','invoicing' ); ?></span></h2> |
||
102 | <div class="inside"> |
||
103 | <p><?php _e( 'Download a CSV of all payment invoices.', 'invoicing' ); ?></p> |
||
104 | <form id="wpi-export-invoices" class="wpi-export-form" method="post"> |
||
105 | <?php echo wpinv_html_date_field( array( |
||
106 | 'id' => 'wpi_export_from_date', |
||
107 | 'name' => 'from_date', |
||
108 | 'data' => array( |
||
109 | 'dateFormat' => 'yy-mm-dd' |
||
110 | ), |
||
111 | 'placeholder' => __( 'From date', 'invoicing' ) ) |
||
112 | ); ?> |
||
113 | <?php echo wpinv_html_date_field( array( |
||
114 | 'id' => 'wpi_export_to_date', |
||
115 | 'name' => 'to_date', |
||
116 | 'data' => array( |
||
117 | 'dateFormat' => 'yy-mm-dd' |
||
118 | ), |
||
119 | 'placeholder' => __( 'To date', 'invoicing' ) ) |
||
120 | ); ?> |
||
121 | <span id="wpinv-status-wrap"> |
||
122 | <?php echo wpinv_html_select( array( |
||
123 | 'options' => $statuses, |
||
124 | 'name' => 'status', |
||
125 | 'id' => 'wpi_export_status', |
||
126 | 'show_option_all' => false, |
||
127 | 'show_option_none' => false, |
||
128 | 'class' => 'wpi_select2', |
||
129 | ) ); ?> |
||
130 | <?php wp_nonce_field( 'wpi_ajax_export', 'wpi_ajax_export' ); ?> |
||
131 | </span> |
||
132 | <span id="wpinv-submit-wrap"> |
||
133 | <input type="hidden" value="invoices" name="export" /> |
||
134 | <input type="submit" value="<?php _e( 'Generate CSV', 'invoicing' ); ?>" class="button-primary" /> |
||
135 | </span> |
||
136 | </form> |
||
137 | </div> |
||
138 | </div> |
||
139 | |||
140 | <?php do_action( 'wpinv_reports_tab_export_content_bottom' ); ?> |
||
141 | </div> |
||
142 | </div> |
||
143 | </div> |
||
144 | <?php |
||
145 | } |
||
146 | |||
147 | public function export_location( $relative = false ) { |
||
148 | $upload_dir = wp_upload_dir(); |
||
149 | $export_location = $relative ? trailingslashit( $upload_dir['baseurl'] ) . 'cache' : trailingslashit( $upload_dir['basedir'] ) . 'cache'; |
||
150 | $export_location = apply_filters( 'wpinv_export_location', $export_location, $relative ); |
||
151 | |||
152 | return trailingslashit( $export_location ); |
||
153 | } |
||
154 | |||
155 | public function check_export_location() { |
||
156 | try { |
||
157 | if ( empty( $this->wp_filesystem ) ) { |
||
158 | return __( 'Filesystem ERROR: Could not access filesystem.', 'invoicing' ); |
||
159 | } |
||
160 | |||
161 | if ( is_wp_error( $this->wp_filesystem ) ) { |
||
162 | return __( 'Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'invoicing' ); |
||
163 | } |
||
164 | |||
165 | $is_dir = $this->wp_filesystem->is_dir( $this->export_dir ); |
||
166 | $is_writeable = $is_dir && is_writeable( $this->export_dir ); |
||
167 | |||
168 | if ( $is_dir && $is_writeable ) { |
||
169 | return true; |
||
170 | } else if ( $is_dir && !$is_writeable ) { |
||
171 | if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
||
172 | return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'invoicing' ), $this->export_dir ); |
||
173 | } |
||
174 | |||
175 | return true; |
||
176 | } else { |
||
177 | if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) { |
||
178 | return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'invoicing' ), $this->export_dir ); |
||
179 | } |
||
180 | |||
181 | return true; |
||
182 | } |
||
183 | } catch ( Exception $e ) { |
||
184 | return $e->getMessage(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | public function ajax_export() { |
||
189 | $response = array(); |
||
190 | $response['success'] = false; |
||
191 | $response['msg'] = __( 'Invalid export request found.', 'invoicing' ); |
||
192 | |||
193 | if ( empty( $_POST['data'] ) || ! wpinv_current_user_can_manage_invoicing() ) { |
||
194 | wp_send_json( $response ); |
||
195 | } |
||
196 | |||
197 | parse_str( $_POST['data'], $data ); |
||
198 | |||
199 | $data['step'] = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; |
||
200 | |||
201 | $_REQUEST = (array)$data; |
||
202 | if ( !( !empty( $_REQUEST['wpi_ajax_export'] ) && wp_verify_nonce( $_REQUEST['wpi_ajax_export'], 'wpi_ajax_export' ) ) ) { |
||
203 | $response['msg'] = __( 'Security check failed.', 'invoicing' ); |
||
204 | wp_send_json( $response ); |
||
205 | } |
||
206 | |||
207 | if ( ( $error = $this->check_export_location( true ) ) !== true ) { |
||
208 | $response['msg'] = __( 'Filesystem ERROR: ' . $error, 'invoicing' ); |
||
209 | wp_send_json( $response ); |
||
210 | } |
||
211 | |||
212 | $this->set_export_params( $_REQUEST ); |
||
213 | |||
214 | $return = $this->process_export_step(); |
||
215 | $done = $this->get_export_status(); |
||
216 | |||
217 | if ( $return ) { |
||
218 | $this->step += 1; |
||
219 | |||
220 | $response['success'] = true; |
||
221 | $response['msg'] = ''; |
||
222 | |||
223 | if ( $done >= 100 ) { |
||
224 | $this->step = 'done'; |
||
225 | $new_filename = 'wpi-' . $this->export . '-' . date( 'y-m-d-H-i' ) . '.' . $this->filetype; |
||
226 | $new_file = $this->export_dir . $new_filename; |
||
227 | |||
228 | if ( file_exists( $this->file ) ) { |
||
229 | $this->wp_filesystem->move( $this->file, $new_file, true ); |
||
230 | } |
||
231 | |||
232 | if ( file_exists( $new_file ) ) { |
||
233 | $response['data']['file'] = array( 'u' => $this->export_url . $new_filename, 's' => size_format( filesize( $new_file ), 2 ) ); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | $response['data']['step'] = $this->step; |
||
238 | $response['data']['done'] = $done; |
||
239 | } else { |
||
240 | $response['msg'] = __( 'No data found for export.', 'invoicing' ); |
||
241 | } |
||
242 | |||
243 | wp_send_json( $response ); |
||
244 | } |
||
245 | |||
246 | public function set_export_params( $request ) { |
||
247 | $this->empty = false; |
||
248 | $this->step = !empty( $request['step'] ) ? absint( $request['step'] ) : 1; |
||
249 | $this->export = !empty( $request['export'] ) ? $request['export'] : $this->export; |
||
250 | $this->filename = 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype; |
||
251 | $this->file = $this->export_dir . $this->filename; |
||
252 | |||
253 | do_action( 'wpinv_export_set_params_' . $this->export, $request ); |
||
254 | } |
||
255 | |||
256 | public function get_columns() { |
||
257 | $columns = array(); |
||
258 | |||
259 | return apply_filters( 'wpinv_export_get_columns_' . $this->export, $columns ); |
||
260 | } |
||
261 | |||
262 | protected function get_export_file() { |
||
263 | $file = ''; |
||
264 | |||
265 | if ( $this->wp_filesystem->exists( $this->file ) ) { |
||
266 | $file = $this->wp_filesystem->get_contents( $this->file ); |
||
267 | } else { |
||
268 | $this->wp_filesystem->put_contents( $this->file, '' ); |
||
269 | } |
||
270 | |||
271 | return $file; |
||
272 | } |
||
273 | |||
274 | protected function attach_export_data( $data = '' ) { |
||
285 | } |
||
286 | |||
287 | public function print_columns() { |
||
288 | $column_data = ''; |
||
289 | $columns = $this->get_columns(); |
||
290 | $i = 1; |
||
291 | foreach( $columns as $key => $column ) { |
||
292 | $column_data .= '"' . addslashes( $column ) . '"'; |
||
293 | $column_data .= $i == count( $columns ) ? '' : ','; |
||
294 | $i++; |
||
295 | } |
||
296 | $column_data .= "\r\n"; |
||
297 | |||
298 | $this->attach_export_data( $column_data ); |
||
299 | |||
300 | return $column_data; |
||
301 | } |
||
302 | |||
303 | public function process_export_step() { |
||
304 | if ( $this->step < 2 ) { |
||
305 | @unlink( $this->file ); |
||
306 | $this->print_columns(); |
||
307 | } |
||
308 | |||
309 | $return = $this->print_rows(); |
||
310 | |||
311 | if ( $return ) { |
||
312 | return true; |
||
313 | } else { |
||
314 | return false; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | public function get_export_status() { |
||
319 | $status = 100; |
||
320 | return apply_filters( 'wpinv_get_export_status_' . $this->export, $status ); |
||
321 | } |
||
322 | |||
323 | public function get_export_data() { |
||
324 | $data = array(); |
||
325 | |||
326 | $data = apply_filters( 'wpinv_export_get_data', $data ); |
||
327 | $data = apply_filters( 'wpinv_export_get_data_' . $this->export, $data ); |
||
328 | |||
329 | return $data; |
||
330 | } |
||
331 | |||
332 | public function print_rows() { |
||
333 | $row_data = ''; |
||
334 | $data = $this->get_export_data(); |
||
335 | $columns = $this->get_columns(); |
||
336 | |||
337 | if ( $data ) { |
||
338 | foreach ( $data as $row ) { |
||
339 | $i = 1; |
||
340 | foreach ( $row as $key => $column ) { |
||
341 | if ( array_key_exists( $key, $columns ) ) { |
||
342 | $row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"'; |
||
343 | $row_data .= $i == count( $columns ) ? '' : ','; |
||
344 | $i++; |
||
345 | } |
||
346 | } |
||
347 | $row_data .= "\r\n"; |
||
348 | } |
||
349 | |||
350 | $this->attach_export_data( $row_data ); |
||
351 | |||
352 | return $row_data; |
||
353 | } |
||
354 | |||
355 | return false; |
||
356 | } |
||
357 | |||
358 | // Export Invoices. |
||
359 | public function set_invoices_export( $request ) { |
||
363 | } |
||
364 | |||
365 | public function get_invoices_columns( $columns = array() ) { |
||
366 | $columns = array( |
||
367 | 'id' => __( 'ID', 'invoicing' ), |
||
368 | 'number' => __( 'Number', 'invoicing' ), |
||
369 | 'date' => __( 'Date', 'invoicing' ), |
||
370 | 'due_date' => __( 'Due Date', 'invoicing' ), |
||
371 | 'completed_date'=> __( 'Payment Done Date', 'invoicing' ), |
||
372 | 'amount' => __( 'Amount', 'invoicing' ), |
||
373 | 'currency' => __( 'Currency', 'invoicing' ), |
||
374 | 'items' => __( 'Items', 'invoicing' ), |
||
375 | 'status_nicename' => __( 'Status Nicename', 'invoicing' ), |
||
376 | 'status' => __( 'Status', 'invoicing' ), |
||
377 | 'tax' => __( 'Tax', 'invoicing' ), |
||
378 | 'discount' => __( 'Discount', 'invoicing' ), |
||
379 | 'user_id' => __( 'User ID', 'invoicing' ), |
||
380 | 'email' => __( 'Email', 'invoicing' ), |
||
381 | 'first_name' => __( 'First Name', 'invoicing' ), |
||
382 | 'last_name' => __( 'Last Name', 'invoicing' ), |
||
383 | 'address' => __( 'Address', 'invoicing' ), |
||
384 | 'city' => __( 'City', 'invoicing' ), |
||
385 | 'state' => __( 'State', 'invoicing' ), |
||
386 | 'country' => __( 'Country', 'invoicing' ), |
||
387 | 'zip' => __( 'Zipcode', 'invoicing' ), |
||
388 | 'phone' => __( 'Phone', 'invoicing' ), |
||
389 | 'company' => __( 'Company', 'invoicing' ), |
||
390 | 'vat_number' => __( 'Vat Number', 'invoicing' ), |
||
391 | 'ip' => __( 'IP', 'invoicing' ), |
||
392 | 'gateway' => __( 'Gateway', 'invoicing' ), |
||
393 | 'gateway_nicename' => __( 'Gateway Nicename', 'invoicing' ), |
||
394 | 'transaction_id'=> __( 'Transaction ID', 'invoicing' ), |
||
395 | ); |
||
396 | |||
397 | return $columns; |
||
398 | } |
||
399 | |||
400 | public function get_invoices_data( $response = array() ) { |
||
401 | $args = array( |
||
402 | 'limit' => $this->per_page, |
||
403 | 'page' => $this->step, |
||
404 | 'order' => 'DESC', |
||
405 | 'orderby' => 'date', |
||
406 | ); |
||
407 | |||
408 | if ( $this->status != 'any' ) { |
||
409 | $args['status'] = $this->status; |
||
410 | } else { |
||
411 | $args['status'] = array_keys( wpinv_get_invoice_statuses( true ) ); |
||
412 | } |
||
413 | |||
414 | if ( !empty( $this->from_date ) || !empty( $this->to_date ) ) { |
||
415 | $args['date_query'] = array( |
||
416 | array( |
||
417 | 'after' => date( 'Y-n-d 00:00:00', strtotime( $this->from_date ) ), |
||
418 | 'before' => date( 'Y-n-d 23:59:59', strtotime( $this->to_date ) ), |
||
419 | 'inclusive' => true |
||
420 | ) |
||
421 | ); |
||
422 | } |
||
423 | |||
424 | $invoices = wpinv_get_invoices( $args ); |
||
425 | |||
426 | $data = array(); |
||
427 | |||
428 | if ( !empty( $invoices ) ) { |
||
429 | foreach ( $invoices as $invoice ) { |
||
430 | $items = $this->get_invoice_items($invoice); |
||
431 | $row = array( |
||
432 | 'id' => $invoice->ID, |
||
433 | 'number' => $invoice->get_number(), |
||
434 | 'date' => $invoice->get_invoice_date( false ), |
||
435 | 'due_date' => $invoice->get_due_date( false ), |
||
436 | 'completed_date'=> $invoice->get_completed_date(), |
||
437 | 'amount' => wpinv_round_amount( $invoice->get_total() ), |
||
438 | 'currency' => $invoice->get_currency(), |
||
439 | 'items' => $items, |
||
440 | 'status_nicename' => $invoice->get_status( true ), |
||
441 | 'status' => $invoice->get_status(), |
||
442 | 'tax' => $invoice->get_tax() > 0 ? wpinv_round_amount( $invoice->get_tax() ) : '', |
||
443 | 'discount' => $invoice->get_discount() > 0 ? wpinv_round_amount( $invoice->get_discount() ) : '', |
||
444 | 'user_id' => $invoice->get_user_id(), |
||
445 | 'email' => $invoice->get_email(), |
||
446 | 'first_name' => $invoice->get_first_name(), |
||
447 | 'last_name' => $invoice->get_last_name(), |
||
448 | 'address' => $invoice->get_address(), |
||
449 | 'city' => $invoice->city, |
||
450 | 'state' => $invoice->state, |
||
451 | 'country' => $invoice->country, |
||
452 | 'zip' => $invoice->zip, |
||
453 | 'phone' => $invoice->phone, |
||
454 | 'company' => $invoice->company, |
||
455 | 'vat_number' => $invoice->vat_number, |
||
456 | 'ip' => $invoice->get_ip(), |
||
457 | 'gateway' => $invoice->get_gateway(), |
||
458 | 'gateway_nicename' => $invoice->get_gateway_title(), |
||
459 | 'transaction_id'=> $invoice->gateway ? $invoice->get_transaction_id() : '', |
||
460 | ); |
||
461 | |||
462 | $data[] = apply_filters( 'wpinv_export_invoice_row', $row, $invoice ); |
||
463 | } |
||
464 | |||
465 | return $data; |
||
466 | |||
467 | } |
||
468 | |||
469 | return false; |
||
470 | } |
||
471 | |||
472 | public function invoices_export_status() { |
||
473 | $args = array( |
||
474 | 'limit' => -1, |
||
475 | 'return' => 'ids', |
||
476 | ); |
||
477 | |||
478 | if ( $this->status != 'any' ) { |
||
479 | $args['status'] = $this->status; |
||
480 | } else { |
||
481 | $args['status'] = array_keys( wpinv_get_invoice_statuses( true ) ); |
||
482 | } |
||
483 | |||
484 | if ( !empty( $this->from_date ) || !empty( $this->to_date ) ) { |
||
485 | $args['date_query'] = array( |
||
486 | array( |
||
487 | 'after' => date( 'Y-n-d 00:00:00', strtotime( $this->from_date ) ), |
||
488 | 'before' => date( 'Y-n-d 23:59:59', strtotime( $this->to_date ) ), |
||
489 | 'inclusive' => true |
||
490 | ) |
||
491 | ); |
||
492 | } |
||
493 | |||
494 | $invoices = wpinv_get_invoices( $args ); |
||
495 | $total = !empty( $invoices ) ? count( $invoices ) : 0; |
||
496 | $status = 100; |
||
497 | |||
498 | if ( $total > 0 ) { |
||
499 | $status = ( ( $this->per_page * $this->step ) / $total ) * 100; |
||
500 | } |
||
501 | |||
502 | if ( $status > 100 ) { |
||
503 | $status = 100; |
||
504 | } |
||
505 | |||
506 | return $status; |
||
507 | } |
||
508 | |||
509 | public function get_invoice_items($invoice){ |
||
522 | } |
||
523 | } |
||
524 |