Passed
Branch master (50908e)
by Stiofan
07:01
created

WPInv_Reports   F

Complexity

Total Complexity 84

Size/Duplication

Total Lines 518
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 42
loc 518
rs 2
c 0
b 0
f 0
wmc 84
lcom 1
cbo 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A attach_export_data() 0 11 3
A set_invoices_export() 0 4 4
A get_invoice_items() 0 13 3
A get_export_status() 0 3 1
A set_export_params() 0 8 3
A actions() 0 13 2
A process_export_step() 0 12 3
A includes() 0 2 1
B ajax_export() 0 56 11
B check_export_location() 0 30 11
A export_location() 0 6 2
B get_invoices_data() 0 70 9
A print_columns() 0 14 3
A get_export_file() 0 10 2
B invoices_export_status() 0 35 7
A get_columns() 0 4 1
A get_invoices_columns() 0 33 1
A print_rows() 0 24 6
A get_export_data() 0 7 1
A export() 0 51 1
A add_submenu() 0 3 1
A init() 0 17 2
A reports_page() 0 24 5
A __construct() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
if ( ! defined( 'ABSPATH' ) ) {
3
    exit; // Exit if accessed directly
4
}
5
6
class WPInv_Reports {
7
    private $section = 'wpinv_reports';
0 ignored issues
show
introduced by
The private property $section is not used, and could be removed.
Loading history...
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>
0 ignored issues
show
Security Cross-Site Scripting introduced by
add_query_arg(array('tab... false), $current_page) can contain request data and is used in html attribute with double-quotes context(s) leading to a potential security vulnerability.

2 paths for user data to reach this point

  1. Path: Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri in wordpress/wp-includes/functions.php on line 1073
  1. Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri
    in wordpress/wp-includes/functions.php on line 1073
  2. $uri . '?' is assigned to $base
    in wordpress/wp-includes/functions.php on line 1100
  3. $protocol . $base . $ret . $frag is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1126
  4. Data is passed through rtrim(), and rtrim($ret, '?') is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1127
  5. $ret is returned
    in wordpress/wp-includes/functions.php on line 1128
  2. Path: Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri in wordpress/wp-includes/functions.php on line 1067
  1. Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri
    in wordpress/wp-includes/functions.php on line 1067
  2. $uri . '?' is assigned to $base
    in wordpress/wp-includes/functions.php on line 1100
  3. $protocol . $base . $ret . $frag is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1126
  4. Data is passed through rtrim(), and rtrim($ret, '?') is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1127
  5. $ret is returned
    in wordpress/wp-includes/functions.php on line 1128

Preventing Cross-Site-Scripting Attacks

Cross-Site-Scripting allows an attacker to inject malicious code into your website - in particular Javascript code, and have that code executed with the privileges of a visiting user. This can be used to obtain data, or perform actions on behalf of that visiting user.

In order to prevent this, make sure to escape all user-provided data:

// for HTML
$sanitized = htmlentities($tainted, ENT_QUOTES);

// for URLs
$sanitized = urlencode($tainted);

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
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 );
0 ignored issues
show
Security Variable Injection introduced by
$_POST['data'] can contain request data and is used in variable name context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_POST
    in includes/class-wpinv-reports.php on line 197

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The call to WPInv_Reports::check_export_location() has too many arguments starting with true. ( Ignorable by Annotation )

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

207
        if ( ( $error = $this->/** @scrutinizer ignore-call */ check_export_location( true ) ) !== true ) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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';
0 ignored issues
show
Bug Best Practice introduced by
The property step does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property empty does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
248
        $this->step     = !empty( $request['step'] ) ? absint( $request['step'] ) : 1;
0 ignored issues
show
Bug Best Practice introduced by
The property step does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
249
        $this->export   = !empty( $request['export'] ) ? $request['export'] : $this->export;
250
        $this->filename = 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype;
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
251
        $this->file     = $this->export_dir . $this->filename;
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 = '' ) {
275
        $filedata   = $this->get_export_file();
276
        $filedata   .= $data;
277
        
278
        $this->wp_filesystem->put_contents( $this->file, $filedata );
279
280
        $rows       = file( $this->file, FILE_SKIP_EMPTY_LINES );
0 ignored issues
show
Security File Exposure introduced by
$this->file can contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and WPInv_Reports::set_export_params() is called
    in includes/class-wpinv-reports.php on line 212
  2. Enters via parameter $request
    in includes/class-wpinv-reports.php on line 246
  3. 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype is assigned to property WPInv_Reports::$filename
    in includes/class-wpinv-reports.php on line 250
  4. Read from property WPInv_Reports::$filename, and $this->export_dir . $this->filename is assigned to property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 251
  5. Read from property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 280

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
281
        $columns    = $this->get_columns();
282
        $columns    = empty( $columns ) ? 0 : 1;
283
284
        $this->empty = count( $rows ) == $columns ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The property empty does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like $rows can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

284
        $this->empty = count( /** @scrutinizer ignore-type */ $rows ) == $columns ? true : false;
Loading history...
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 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

305
            /** @scrutinizer ignore-unhandled */ @unlink( $this->file );

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Security File Manipulation introduced by
$this->file can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and WPInv_Reports::set_export_params() is called
    in includes/class-wpinv-reports.php on line 212
  2. Enters via parameter $request
    in includes/class-wpinv-reports.php on line 246
  3. 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype is assigned to property WPInv_Reports::$filename
    in includes/class-wpinv-reports.php on line 250
  4. Read from property WPInv_Reports::$filename, and $this->export_dir . $this->filename is assigned to property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 251
  5. Read from property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 305

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
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 ) {
360
        $this->from_date    = isset( $request['from_date'] ) ? sanitize_text_field( $request['from_date'] ) : '';
0 ignored issues
show
Bug Best Practice introduced by
The property from_date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
361
        $this->to_date      = isset( $request['to_date'] ) ? sanitize_text_field( $request['to_date'] ) : '';
0 ignored issues
show
Bug Best Practice introduced by
The property to_date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
362
        $this->status       = isset( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'publish';
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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;
0 ignored issues
show
Bug introduced by
It seems like $invoices can also be of type WP_Query; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

495
        $total      = !empty( $invoices ) ? count( /** @scrutinizer ignore-type */ $invoices ) : 0;
Loading history...
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){
510
        if(!$invoice){
511
            return '';
512
        }
513
514
        $cart_details = $invoice->get_cart_details();
515
        if(!empty($cart_details)){
516
            $cart_details = maybe_serialize($cart_details);
517
        } else {
518
            $cart_details = '';
519
        }
520
521
        return $cart_details;
522
    }
523
}
524