@@ -13,154 +13,154 @@ |
||
13 | 13 | */ |
14 | 14 | class GetPaid_MaxMind_Database_Service { |
15 | 15 | |
16 | - /** |
|
17 | - * The name of the MaxMind database to utilize. |
|
18 | - */ |
|
19 | - const DATABASE = 'GeoLite2-Country'; |
|
20 | - |
|
21 | - /** |
|
22 | - * The extension for the MaxMind database. |
|
23 | - */ |
|
24 | - const DATABASE_EXTENSION = '.mmdb'; |
|
25 | - |
|
26 | - /** |
|
27 | - * A prefix for the MaxMind database filename. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - private $database_prefix; |
|
32 | - |
|
33 | - /** |
|
34 | - * Class constructor. |
|
35 | - * |
|
36 | - * @param string|null $database_prefix A prefix for the MaxMind database filename. |
|
37 | - */ |
|
38 | - public function __construct( $database_prefix ) { |
|
39 | - $this->database_prefix = $database_prefix; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Fetches the path that the database should be stored. |
|
44 | - * |
|
45 | - * @return string The local database path. |
|
46 | - */ |
|
47 | - public function get_database_path() { |
|
48 | - $uploads_dir = wp_upload_dir(); |
|
49 | - |
|
50 | - $database_path = trailingslashit( $uploads_dir['basedir'] ) . 'invoicing/'; |
|
51 | - if ( ! empty( $this->database_prefix ) ) { |
|
52 | - $database_path .= $this->database_prefix . '-'; |
|
53 | - } |
|
54 | - $database_path .= self::DATABASE . self::DATABASE_EXTENSION; |
|
55 | - |
|
56 | - // Filter the geolocation database storage path. |
|
57 | - return apply_filters( 'getpaid_maxmind_geolocation_database_path', $database_path ); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Fetches the database from the MaxMind service. |
|
62 | - * |
|
63 | - * @param string $license_key The license key to be used when downloading the database. |
|
64 | - * @return string|WP_Error The path to the database file or an error if invalid. |
|
65 | - */ |
|
66 | - public function download_database( $license_key ) { |
|
67 | - |
|
68 | - $download_uri = add_query_arg( |
|
69 | - array( |
|
70 | - 'edition_id' => self::DATABASE, |
|
71 | - 'license_key' => urlencode( wpinv_clean( $license_key ) ), |
|
72 | - 'suffix' => 'tar.gz', |
|
73 | - ), |
|
74 | - 'https://download.maxmind.com/app/geoip_download' |
|
75 | - ); |
|
76 | - |
|
77 | - // Needed for the download_url call right below. |
|
78 | - require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
79 | - |
|
80 | - $tmp_archive_path = download_url( esc_url_raw( $download_uri ) ); |
|
81 | - |
|
82 | - if ( is_wp_error( $tmp_archive_path ) ) { |
|
83 | - // Transform the error into something more informative. |
|
84 | - $error_data = $tmp_archive_path->get_error_data(); |
|
85 | - if ( isset( $error_data['code'] ) && $error_data['code'] == 401 ) { |
|
86 | - return new WP_Error( |
|
87 | - 'getpaid_maxmind_geolocation_database_license_key', |
|
88 | - __( 'The MaxMind license key is invalid. If you have recently created this key, you may need to wait for it to become active.', 'invoicing' ) |
|
89 | - ); |
|
90 | - } |
|
91 | - |
|
92 | - return new WP_Error( 'getpaid_maxmind_geolocation_database_download', __( 'Failed to download the MaxMind database.', 'invoicing' ) ); |
|
93 | - } |
|
94 | - |
|
95 | - // Extract the database from the archive. |
|
96 | - return $this->extract_downloaded_database( $tmp_archive_path ); |
|
97 | - |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Extracts the downloaded database. |
|
102 | - * |
|
103 | - * @param string $tmp_archive_path The database archive path. |
|
104 | - * @return string|WP_Error The path to the database file or an error if invalid. |
|
105 | - */ |
|
106 | - protected function extract_downloaded_database( $tmp_archive_path ) { |
|
107 | - |
|
108 | - // Extract the database from the archive. |
|
109 | - $tmp_database_path = ''; |
|
110 | - |
|
111 | - try { |
|
112 | - |
|
113 | - $file = new PharData( $tmp_archive_path ); |
|
114 | - $tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION; |
|
115 | - |
|
116 | - $file->extractTo( |
|
117 | - dirname( $tmp_archive_path ), |
|
118 | - trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION, |
|
119 | - true |
|
120 | - ); |
|
121 | - |
|
122 | - } catch ( Exception $exception ) { |
|
123 | - return new WP_Error( 'invoicing_maxmind_geolocation_database_archive', $exception->getMessage() ); |
|
124 | - } finally { |
|
125 | - // Remove the archive since we only care about a single file in it. |
|
126 | - unlink( $tmp_archive_path ); |
|
127 | - } |
|
128 | - |
|
129 | - return $tmp_database_path; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Fetches the ISO country code associated with an IP address. |
|
134 | - * |
|
135 | - * @param string $ip_address The IP address to find the country code for. |
|
136 | - * @return string The country code for the IP address, or empty if not found. |
|
137 | - */ |
|
138 | - public function get_iso_country_code_for_ip( $ip_address ) { |
|
139 | - $country_code = ''; |
|
140 | - |
|
141 | - if ( ! class_exists( 'MaxMind\Db\Reader' ) ) { |
|
142 | - return $country_code; |
|
143 | - } |
|
144 | - |
|
145 | - $database_path = $this->get_database_path(); |
|
146 | - if ( ! file_exists( $database_path ) ) { |
|
147 | - return $country_code; |
|
148 | - } |
|
149 | - |
|
150 | - try { |
|
151 | - $reader = new MaxMind\Db\Reader( $database_path ); |
|
152 | - $data = $reader->get( $ip_address ); |
|
153 | - |
|
154 | - if ( isset( $data['country']['iso_code'] ) ) { |
|
155 | - $country_code = $data['country']['iso_code']; |
|
156 | - } |
|
157 | - |
|
158 | - $reader->close(); |
|
159 | - } catch ( Exception $e ) { |
|
160 | - wpinv_error_log( $e->getMessage(), 'SOURCE: MaxMind GeoLocation' ); |
|
161 | - } |
|
162 | - |
|
163 | - return $country_code; |
|
164 | - } |
|
16 | + /** |
|
17 | + * The name of the MaxMind database to utilize. |
|
18 | + */ |
|
19 | + const DATABASE = 'GeoLite2-Country'; |
|
20 | + |
|
21 | + /** |
|
22 | + * The extension for the MaxMind database. |
|
23 | + */ |
|
24 | + const DATABASE_EXTENSION = '.mmdb'; |
|
25 | + |
|
26 | + /** |
|
27 | + * A prefix for the MaxMind database filename. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + private $database_prefix; |
|
32 | + |
|
33 | + /** |
|
34 | + * Class constructor. |
|
35 | + * |
|
36 | + * @param string|null $database_prefix A prefix for the MaxMind database filename. |
|
37 | + */ |
|
38 | + public function __construct( $database_prefix ) { |
|
39 | + $this->database_prefix = $database_prefix; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Fetches the path that the database should be stored. |
|
44 | + * |
|
45 | + * @return string The local database path. |
|
46 | + */ |
|
47 | + public function get_database_path() { |
|
48 | + $uploads_dir = wp_upload_dir(); |
|
49 | + |
|
50 | + $database_path = trailingslashit( $uploads_dir['basedir'] ) . 'invoicing/'; |
|
51 | + if ( ! empty( $this->database_prefix ) ) { |
|
52 | + $database_path .= $this->database_prefix . '-'; |
|
53 | + } |
|
54 | + $database_path .= self::DATABASE . self::DATABASE_EXTENSION; |
|
55 | + |
|
56 | + // Filter the geolocation database storage path. |
|
57 | + return apply_filters( 'getpaid_maxmind_geolocation_database_path', $database_path ); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Fetches the database from the MaxMind service. |
|
62 | + * |
|
63 | + * @param string $license_key The license key to be used when downloading the database. |
|
64 | + * @return string|WP_Error The path to the database file or an error if invalid. |
|
65 | + */ |
|
66 | + public function download_database( $license_key ) { |
|
67 | + |
|
68 | + $download_uri = add_query_arg( |
|
69 | + array( |
|
70 | + 'edition_id' => self::DATABASE, |
|
71 | + 'license_key' => urlencode( wpinv_clean( $license_key ) ), |
|
72 | + 'suffix' => 'tar.gz', |
|
73 | + ), |
|
74 | + 'https://download.maxmind.com/app/geoip_download' |
|
75 | + ); |
|
76 | + |
|
77 | + // Needed for the download_url call right below. |
|
78 | + require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
79 | + |
|
80 | + $tmp_archive_path = download_url( esc_url_raw( $download_uri ) ); |
|
81 | + |
|
82 | + if ( is_wp_error( $tmp_archive_path ) ) { |
|
83 | + // Transform the error into something more informative. |
|
84 | + $error_data = $tmp_archive_path->get_error_data(); |
|
85 | + if ( isset( $error_data['code'] ) && $error_data['code'] == 401 ) { |
|
86 | + return new WP_Error( |
|
87 | + 'getpaid_maxmind_geolocation_database_license_key', |
|
88 | + __( 'The MaxMind license key is invalid. If you have recently created this key, you may need to wait for it to become active.', 'invoicing' ) |
|
89 | + ); |
|
90 | + } |
|
91 | + |
|
92 | + return new WP_Error( 'getpaid_maxmind_geolocation_database_download', __( 'Failed to download the MaxMind database.', 'invoicing' ) ); |
|
93 | + } |
|
94 | + |
|
95 | + // Extract the database from the archive. |
|
96 | + return $this->extract_downloaded_database( $tmp_archive_path ); |
|
97 | + |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Extracts the downloaded database. |
|
102 | + * |
|
103 | + * @param string $tmp_archive_path The database archive path. |
|
104 | + * @return string|WP_Error The path to the database file or an error if invalid. |
|
105 | + */ |
|
106 | + protected function extract_downloaded_database( $tmp_archive_path ) { |
|
107 | + |
|
108 | + // Extract the database from the archive. |
|
109 | + $tmp_database_path = ''; |
|
110 | + |
|
111 | + try { |
|
112 | + |
|
113 | + $file = new PharData( $tmp_archive_path ); |
|
114 | + $tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION; |
|
115 | + |
|
116 | + $file->extractTo( |
|
117 | + dirname( $tmp_archive_path ), |
|
118 | + trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION, |
|
119 | + true |
|
120 | + ); |
|
121 | + |
|
122 | + } catch ( Exception $exception ) { |
|
123 | + return new WP_Error( 'invoicing_maxmind_geolocation_database_archive', $exception->getMessage() ); |
|
124 | + } finally { |
|
125 | + // Remove the archive since we only care about a single file in it. |
|
126 | + unlink( $tmp_archive_path ); |
|
127 | + } |
|
128 | + |
|
129 | + return $tmp_database_path; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Fetches the ISO country code associated with an IP address. |
|
134 | + * |
|
135 | + * @param string $ip_address The IP address to find the country code for. |
|
136 | + * @return string The country code for the IP address, or empty if not found. |
|
137 | + */ |
|
138 | + public function get_iso_country_code_for_ip( $ip_address ) { |
|
139 | + $country_code = ''; |
|
140 | + |
|
141 | + if ( ! class_exists( 'MaxMind\Db\Reader' ) ) { |
|
142 | + return $country_code; |
|
143 | + } |
|
144 | + |
|
145 | + $database_path = $this->get_database_path(); |
|
146 | + if ( ! file_exists( $database_path ) ) { |
|
147 | + return $country_code; |
|
148 | + } |
|
149 | + |
|
150 | + try { |
|
151 | + $reader = new MaxMind\Db\Reader( $database_path ); |
|
152 | + $data = $reader->get( $ip_address ); |
|
153 | + |
|
154 | + if ( isset( $data['country']['iso_code'] ) ) { |
|
155 | + $country_code = $data['country']['iso_code']; |
|
156 | + } |
|
157 | + |
|
158 | + $reader->close(); |
|
159 | + } catch ( Exception $e ) { |
|
160 | + wpinv_error_log( $e->getMessage(), 'SOURCE: MaxMind GeoLocation' ); |
|
161 | + } |
|
162 | + |
|
163 | + return $country_code; |
|
164 | + } |
|
165 | 165 | |
166 | 166 | } |
@@ -12,22 +12,22 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Reports_Report_Discounts extends GetPaid_Reports_Abstract_Report { |
14 | 14 | |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - public $field = 'discount_code'; |
|
19 | - |
|
20 | - /** |
|
21 | - * Retrieves the discounts sql. |
|
22 | - * |
|
23 | - */ |
|
24 | - public function get_sql( $range ) { |
|
25 | - global $wpdb; |
|
26 | - |
|
27 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
28 | - $clauses = $this->get_range_sql( $range ); |
|
29 | - |
|
30 | - $sql = "SELECT |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + public $field = 'discount_code'; |
|
19 | + |
|
20 | + /** |
|
21 | + * Retrieves the discounts sql. |
|
22 | + * |
|
23 | + */ |
|
24 | + public function get_sql( $range ) { |
|
25 | + global $wpdb; |
|
26 | + |
|
27 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
28 | + $clauses = $this->get_range_sql( $range ); |
|
29 | + |
|
30 | + $sql = "SELECT |
|
31 | 31 | meta.discount_code AS discount_code, |
32 | 32 | SUM(total) as total |
33 | 33 | FROM $wpdb->posts |
@@ -41,91 +41,91 @@ discard block |
||
41 | 41 | ORDER BY total DESC |
42 | 42 | "; |
43 | 43 | |
44 | - return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range ); |
|
44 | + return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range ); |
|
45 | 45 | |
46 | - } |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Prepares the report stats. |
|
50 | - * |
|
51 | - */ |
|
52 | - public function prepare_stats() { |
|
53 | - global $wpdb; |
|
54 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
55 | - $this->stats = $this->normalize_stats( $this->stats ); |
|
56 | - } |
|
48 | + /** |
|
49 | + * Prepares the report stats. |
|
50 | + * |
|
51 | + */ |
|
52 | + public function prepare_stats() { |
|
53 | + global $wpdb; |
|
54 | + $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
55 | + $this->stats = $this->normalize_stats( $this->stats ); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Normalizes the report stats. |
|
60 | - * |
|
61 | - */ |
|
62 | - public function normalize_stats( $stats ) { |
|
63 | - $normalized = array(); |
|
64 | - $others = 0; |
|
65 | - $did = 0; |
|
58 | + /** |
|
59 | + * Normalizes the report stats. |
|
60 | + * |
|
61 | + */ |
|
62 | + public function normalize_stats( $stats ) { |
|
63 | + $normalized = array(); |
|
64 | + $others = 0; |
|
65 | + $did = 0; |
|
66 | 66 | |
67 | - foreach ( $stats as $stat ) { |
|
67 | + foreach ( $stats as $stat ) { |
|
68 | 68 | |
69 | - if ( $did > 4 ) { |
|
69 | + if ( $did > 4 ) { |
|
70 | 70 | |
71 | - $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
71 | + $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
72 | 72 | |
73 | - } else { |
|
73 | + } else { |
|
74 | 74 | |
75 | - $normalized[] = array( |
|
76 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
77 | - 'discount_code' => strip_tags( $stat->discount_code ), |
|
78 | - ); |
|
75 | + $normalized[] = array( |
|
76 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
77 | + 'discount_code' => strip_tags( $stat->discount_code ), |
|
78 | + ); |
|
79 | 79 | |
80 | - } |
|
80 | + } |
|
81 | 81 | |
82 | - $did++; |
|
83 | - } |
|
82 | + $did++; |
|
83 | + } |
|
84 | 84 | |
85 | - if ( $others > 0 ) { |
|
85 | + if ( $others > 0 ) { |
|
86 | 86 | |
87 | - $normalized[] = array( |
|
88 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
89 | - 'discount_code' => esc_html__( 'Others', 'invoicing' ), |
|
90 | - ); |
|
87 | + $normalized[] = array( |
|
88 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
89 | + 'discount_code' => esc_html__( 'Others', 'invoicing' ), |
|
90 | + ); |
|
91 | 91 | |
92 | - } |
|
92 | + } |
|
93 | 93 | |
94 | - return $normalized; |
|
95 | - } |
|
94 | + return $normalized; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Retrieves report data. |
|
99 | - * |
|
100 | - */ |
|
101 | - public function get_data() { |
|
97 | + /** |
|
98 | + * Retrieves report data. |
|
99 | + * |
|
100 | + */ |
|
101 | + public function get_data() { |
|
102 | 102 | |
103 | - $data = wp_list_pluck( $this->stats, 'total' ); |
|
104 | - $colors = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' ); |
|
103 | + $data = wp_list_pluck( $this->stats, 'total' ); |
|
104 | + $colors = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' ); |
|
105 | 105 | |
106 | - shuffle( $colors ); |
|
106 | + shuffle( $colors ); |
|
107 | 107 | |
108 | - return array( |
|
109 | - 'data' => $data, |
|
110 | - 'backgroundColor' => $colors, |
|
111 | - ); |
|
108 | + return array( |
|
109 | + 'data' => $data, |
|
110 | + 'backgroundColor' => $colors, |
|
111 | + ); |
|
112 | 112 | |
113 | - } |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Retrieves report labels. |
|
117 | - * |
|
118 | - */ |
|
119 | - public function get_labels() { |
|
120 | - return wp_list_pluck( $this->stats, 'discount_code' ); |
|
121 | - } |
|
115 | + /** |
|
116 | + * Retrieves report labels. |
|
117 | + * |
|
118 | + */ |
|
119 | + public function get_labels() { |
|
120 | + return wp_list_pluck( $this->stats, 'discount_code' ); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Displays the actual report. |
|
125 | - * |
|
126 | - */ |
|
127 | - public function display_stats() { |
|
128 | - ?> |
|
123 | + /** |
|
124 | + * Displays the actual report. |
|
125 | + * |
|
126 | + */ |
|
127 | + public function display_stats() { |
|
128 | + ?> |
|
129 | 129 | |
130 | 130 | <canvas id="getpaid-chartjs-earnings-discount_code"></canvas> |
131 | 131 | |
@@ -154,6 +154,6 @@ discard block |
||
154 | 154 | </script> |
155 | 155 | |
156 | 156 | <?php |
157 | - } |
|
157 | + } |
|
158 | 158 | |
159 | 159 | } |
@@ -12,23 +12,23 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Reports_Report_Items extends GetPaid_Reports_Abstract_Report { |
14 | 14 | |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - public $field = 'item_name'; |
|
19 | - |
|
20 | - /** |
|
21 | - * Retrieves the earning sql. |
|
22 | - * |
|
23 | - */ |
|
24 | - public function get_sql( $range ) { |
|
25 | - global $wpdb; |
|
26 | - |
|
27 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
28 | - $table2 = $wpdb->prefix . 'getpaid_invoice_items'; |
|
29 | - $clauses = $this->get_range_sql( $range ); |
|
30 | - |
|
31 | - $sql = "SELECT |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + public $field = 'item_name'; |
|
19 | + |
|
20 | + /** |
|
21 | + * Retrieves the earning sql. |
|
22 | + * |
|
23 | + */ |
|
24 | + public function get_sql( $range ) { |
|
25 | + global $wpdb; |
|
26 | + |
|
27 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
28 | + $table2 = $wpdb->prefix . 'getpaid_invoice_items'; |
|
29 | + $clauses = $this->get_range_sql( $range ); |
|
30 | + |
|
31 | + $sql = "SELECT |
|
32 | 32 | item.item_name AS item_name, |
33 | 33 | item.item_id AS item_id, |
34 | 34 | SUM(price) as total |
@@ -43,91 +43,91 @@ discard block |
||
43 | 43 | ORDER BY total DESC |
44 | 44 | "; |
45 | 45 | |
46 | - return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range ); |
|
46 | + return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range ); |
|
47 | 47 | |
48 | - } |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Prepares the report stats. |
|
52 | - * |
|
53 | - */ |
|
54 | - public function prepare_stats() { |
|
55 | - global $wpdb; |
|
56 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
57 | - $this->stats = $this->normalize_stats( $this->stats ); |
|
58 | - } |
|
50 | + /** |
|
51 | + * Prepares the report stats. |
|
52 | + * |
|
53 | + */ |
|
54 | + public function prepare_stats() { |
|
55 | + global $wpdb; |
|
56 | + $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
57 | + $this->stats = $this->normalize_stats( $this->stats ); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Normalizes the report stats. |
|
62 | - * |
|
63 | - */ |
|
64 | - public function normalize_stats( $stats ) { |
|
65 | - $normalized = array(); |
|
66 | - $others = 0; |
|
67 | - $did = 0; |
|
60 | + /** |
|
61 | + * Normalizes the report stats. |
|
62 | + * |
|
63 | + */ |
|
64 | + public function normalize_stats( $stats ) { |
|
65 | + $normalized = array(); |
|
66 | + $others = 0; |
|
67 | + $did = 0; |
|
68 | 68 | |
69 | - foreach ( $stats as $stat ) { |
|
69 | + foreach ( $stats as $stat ) { |
|
70 | 70 | |
71 | - if ( $did > 4 ) { |
|
71 | + if ( $did > 4 ) { |
|
72 | 72 | |
73 | - $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
73 | + $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
74 | 74 | |
75 | - } else { |
|
75 | + } else { |
|
76 | 76 | |
77 | - $normalized[] = array( |
|
78 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
79 | - 'item_name' => strip_tags( $stat->item_name ), |
|
80 | - ); |
|
77 | + $normalized[] = array( |
|
78 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
79 | + 'item_name' => strip_tags( $stat->item_name ), |
|
80 | + ); |
|
81 | 81 | |
82 | - } |
|
82 | + } |
|
83 | 83 | |
84 | - $did++; |
|
85 | - } |
|
84 | + $did++; |
|
85 | + } |
|
86 | 86 | |
87 | - if ( $others > 0 ) { |
|
87 | + if ( $others > 0 ) { |
|
88 | 88 | |
89 | - $normalized[] = array( |
|
90 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
91 | - 'item_name' => esc_html__( 'Others', 'invoicing' ), |
|
92 | - ); |
|
89 | + $normalized[] = array( |
|
90 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
91 | + 'item_name' => esc_html__( 'Others', 'invoicing' ), |
|
92 | + ); |
|
93 | 93 | |
94 | - } |
|
94 | + } |
|
95 | 95 | |
96 | - return $normalized; |
|
97 | - } |
|
96 | + return $normalized; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Retrieves report data. |
|
101 | - * |
|
102 | - */ |
|
103 | - public function get_data() { |
|
99 | + /** |
|
100 | + * Retrieves report data. |
|
101 | + * |
|
102 | + */ |
|
103 | + public function get_data() { |
|
104 | 104 | |
105 | - $data = wp_list_pluck( $this->stats, 'total' ); |
|
106 | - $colors = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' ); |
|
105 | + $data = wp_list_pluck( $this->stats, 'total' ); |
|
106 | + $colors = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' ); |
|
107 | 107 | |
108 | - shuffle( $colors ); |
|
108 | + shuffle( $colors ); |
|
109 | 109 | |
110 | - return array( |
|
111 | - 'data' => $data, |
|
112 | - 'backgroundColor' => $colors, |
|
113 | - ); |
|
110 | + return array( |
|
111 | + 'data' => $data, |
|
112 | + 'backgroundColor' => $colors, |
|
113 | + ); |
|
114 | 114 | |
115 | - } |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Retrieves report labels. |
|
119 | - * |
|
120 | - */ |
|
121 | - public function get_labels() { |
|
122 | - return wp_list_pluck( $this->stats, 'item_name' ); |
|
123 | - } |
|
117 | + /** |
|
118 | + * Retrieves report labels. |
|
119 | + * |
|
120 | + */ |
|
121 | + public function get_labels() { |
|
122 | + return wp_list_pluck( $this->stats, 'item_name' ); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Displays the actual report. |
|
127 | - * |
|
128 | - */ |
|
129 | - public function display_stats() { |
|
130 | - ?> |
|
125 | + /** |
|
126 | + * Displays the actual report. |
|
127 | + * |
|
128 | + */ |
|
129 | + public function display_stats() { |
|
130 | + ?> |
|
131 | 131 | |
132 | 132 | <canvas id="getpaid-chartjs-earnings-items"></canvas> |
133 | 133 | |
@@ -156,6 +156,6 @@ discard block |
||
156 | 156 | </script> |
157 | 157 | |
158 | 158 | <?php |
159 | - } |
|
159 | + } |
|
160 | 160 | |
161 | 161 | } |
@@ -12,22 +12,22 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Reports_Report_Gateways extends GetPaid_Reports_Abstract_Report { |
14 | 14 | |
15 | - /** |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - public $field = 'gateway'; |
|
19 | - |
|
20 | - /** |
|
21 | - * Retrieves the earning sql. |
|
22 | - * |
|
23 | - */ |
|
24 | - public function get_sql( $range ) { |
|
25 | - global $wpdb; |
|
26 | - |
|
27 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
28 | - $clauses = $this->get_range_sql( $range ); |
|
29 | - |
|
30 | - $sql = "SELECT |
|
15 | + /** |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + public $field = 'gateway'; |
|
19 | + |
|
20 | + /** |
|
21 | + * Retrieves the earning sql. |
|
22 | + * |
|
23 | + */ |
|
24 | + public function get_sql( $range ) { |
|
25 | + global $wpdb; |
|
26 | + |
|
27 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
28 | + $clauses = $this->get_range_sql( $range ); |
|
29 | + |
|
30 | + $sql = "SELECT |
|
31 | 31 | meta.gateway AS gateway, |
32 | 32 | SUM(total) as total |
33 | 33 | FROM $wpdb->posts |
@@ -40,91 +40,91 @@ discard block |
||
40 | 40 | ORDER BY total DESC |
41 | 41 | "; |
42 | 42 | |
43 | - return apply_filters( 'getpaid_gateways_graphs_get_sql', $sql, $range ); |
|
43 | + return apply_filters( 'getpaid_gateways_graphs_get_sql', $sql, $range ); |
|
44 | 44 | |
45 | - } |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Prepares the report stats. |
|
49 | - * |
|
50 | - */ |
|
51 | - public function prepare_stats() { |
|
52 | - global $wpdb; |
|
53 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
54 | - $this->stats = $this->normalize_stats( $this->stats ); |
|
55 | - } |
|
47 | + /** |
|
48 | + * Prepares the report stats. |
|
49 | + * |
|
50 | + */ |
|
51 | + public function prepare_stats() { |
|
52 | + global $wpdb; |
|
53 | + $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
54 | + $this->stats = $this->normalize_stats( $this->stats ); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Normalizes the report stats. |
|
59 | - * |
|
60 | - */ |
|
61 | - public function normalize_stats( $stats ) { |
|
62 | - $normalized = array(); |
|
63 | - $others = 0; |
|
64 | - $did = 0; |
|
57 | + /** |
|
58 | + * Normalizes the report stats. |
|
59 | + * |
|
60 | + */ |
|
61 | + public function normalize_stats( $stats ) { |
|
62 | + $normalized = array(); |
|
63 | + $others = 0; |
|
64 | + $did = 0; |
|
65 | 65 | |
66 | - foreach ( $stats as $stat ) { |
|
66 | + foreach ( $stats as $stat ) { |
|
67 | 67 | |
68 | - if ( $did > 4 ) { |
|
68 | + if ( $did > 4 ) { |
|
69 | 69 | |
70 | - $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
70 | + $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
71 | 71 | |
72 | - } else { |
|
72 | + } else { |
|
73 | 73 | |
74 | - $normalized[] = array( |
|
75 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
76 | - 'gateway' => strip_tags( wpinv_get_gateway_admin_label( $stat->gateway ) ), |
|
77 | - ); |
|
74 | + $normalized[] = array( |
|
75 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
76 | + 'gateway' => strip_tags( wpinv_get_gateway_admin_label( $stat->gateway ) ), |
|
77 | + ); |
|
78 | 78 | |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - $did++; |
|
82 | - } |
|
81 | + $did++; |
|
82 | + } |
|
83 | 83 | |
84 | - if ( $others > 0 ) { |
|
84 | + if ( $others > 0 ) { |
|
85 | 85 | |
86 | - $normalized[] = array( |
|
87 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
88 | - 'gateway' => esc_html__( 'Others', 'invoicing' ), |
|
89 | - ); |
|
86 | + $normalized[] = array( |
|
87 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
88 | + 'gateway' => esc_html__( 'Others', 'invoicing' ), |
|
89 | + ); |
|
90 | 90 | |
91 | - } |
|
91 | + } |
|
92 | 92 | |
93 | - return $normalized; |
|
94 | - } |
|
93 | + return $normalized; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Retrieves report data. |
|
98 | - * |
|
99 | - */ |
|
100 | - public function get_data() { |
|
96 | + /** |
|
97 | + * Retrieves report data. |
|
98 | + * |
|
99 | + */ |
|
100 | + public function get_data() { |
|
101 | 101 | |
102 | - $data = wp_list_pluck( $this->stats, 'total' ); |
|
103 | - $colors = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' ); |
|
102 | + $data = wp_list_pluck( $this->stats, 'total' ); |
|
103 | + $colors = array( '#009688','#4caf50','#8bc34a','#00bcd4','#03a9f4','#2196f3' ); |
|
104 | 104 | |
105 | - shuffle( $colors ); |
|
105 | + shuffle( $colors ); |
|
106 | 106 | |
107 | - return array( |
|
108 | - 'data' => $data, |
|
109 | - 'backgroundColor' => $colors, |
|
110 | - ); |
|
107 | + return array( |
|
108 | + 'data' => $data, |
|
109 | + 'backgroundColor' => $colors, |
|
110 | + ); |
|
111 | 111 | |
112 | - } |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Retrieves report labels. |
|
116 | - * |
|
117 | - */ |
|
118 | - public function get_labels() { |
|
119 | - return wp_list_pluck( $this->stats, 'gateway' ); |
|
120 | - } |
|
114 | + /** |
|
115 | + * Retrieves report labels. |
|
116 | + * |
|
117 | + */ |
|
118 | + public function get_labels() { |
|
119 | + return wp_list_pluck( $this->stats, 'gateway' ); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * Displays the actual report. |
|
124 | - * |
|
125 | - */ |
|
126 | - public function display_stats() { |
|
127 | - ?> |
|
122 | + /** |
|
123 | + * Displays the actual report. |
|
124 | + * |
|
125 | + */ |
|
126 | + public function display_stats() { |
|
127 | + ?> |
|
128 | 128 | |
129 | 129 | <canvas id="getpaid-chartjs-earnings-gateways"></canvas> |
130 | 130 | |
@@ -153,6 +153,6 @@ discard block |
||
153 | 153 | </script> |
154 | 154 | |
155 | 155 | <?php |
156 | - } |
|
156 | + } |
|
157 | 157 | |
158 | 158 | } |
@@ -12,219 +12,219 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Graph_Downloader { |
14 | 14 | |
15 | - /** |
|
16 | - * @var GetPaid_Reports_Report |
|
17 | - */ |
|
18 | - public $handler; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - */ |
|
24 | - public function __construct() { |
|
25 | - $this->handler = new GetPaid_Reports_Report(); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Prepares the datastore handler. |
|
30 | - * |
|
31 | - * @return GetPaid_Reports_Report_Items|GetPaid_Reports_Report_Gateways|GetPaid_Reports_Report_Discounts |
|
32 | - */ |
|
33 | - public function prepare_handler( $graph ) { |
|
34 | - |
|
35 | - if ( empty( $this->handler->views[ $graph ] ) ) { |
|
36 | - wp_die( __( 'Invalid Graph', 'invoicing' ), 400 ); |
|
37 | - } |
|
38 | - |
|
39 | - return new $this->handler->views[ $graph ]['class'](); |
|
40 | - |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Prepares the output stream. |
|
45 | - * |
|
46 | - * @return resource |
|
47 | - */ |
|
48 | - public function prepare_output() { |
|
49 | - |
|
50 | - $output = fopen( 'php://output', 'w' ); |
|
51 | - |
|
52 | - if ( false === $output ) { |
|
53 | - wp_die( __( 'Unsupported server', 'invoicing' ), 500 ); |
|
54 | - } |
|
55 | - |
|
56 | - return $output; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Prepares the file type. |
|
61 | - * |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public function prepare_file_type( $graph ) { |
|
65 | - |
|
66 | - $file_type = empty( $_REQUEST['file_type'] ) ? 'csv' : sanitize_text_field( $_REQUEST['file_type'] ); |
|
67 | - $file_name = wpinv_sanitize_key( "getpaid-$graph-" . current_time( 'Y-m-d' ) ); |
|
68 | - |
|
69 | - header( "Content-Type:application/$file_type" ); |
|
70 | - header( "Content-Disposition:attachment;filename=$file_name.$file_type" ); |
|
71 | - |
|
72 | - return $file_type; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Handles the actual download. |
|
77 | - * |
|
78 | - */ |
|
79 | - public function download( $graph ) { |
|
80 | - global $wpdb; |
|
81 | - |
|
82 | - $handler = $this->prepare_handler( $graph ); |
|
83 | - $stream = $this->prepare_output(); |
|
84 | - $stats = $wpdb->get_results( $handler->get_sql( $handler->get_range() ) ); |
|
85 | - $headers = array( $handler->field, 'total', 'total_raw' ); |
|
86 | - $file_type = $this->prepare_file_type( $graph ); |
|
87 | - |
|
88 | - if ( 'csv' == $file_type ) { |
|
89 | - $this->download_csv( $stats, $stream, $headers ); |
|
90 | - } else if( 'xml' == $file_type ) { |
|
91 | - $this->download_xml( $stats, $stream, $headers ); |
|
92 | - } else { |
|
93 | - $this->download_json( $stats, $stream, $headers ); |
|
94 | - } |
|
95 | - |
|
96 | - fclose( $stream ); |
|
97 | - exit; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Downloads graph as csv |
|
102 | - * |
|
103 | - * @param array $stats The stats being downloaded. |
|
104 | - * @param resource $stream The stream to output to. |
|
105 | - * @param array $headers The fields to stream. |
|
106 | - * @since 1.0.19 |
|
107 | - */ |
|
108 | - public function download_csv( $stats, $stream, $headers ) { |
|
109 | - |
|
110 | - // Output the csv column headers. |
|
111 | - fputcsv( $stream, $headers ); |
|
112 | - |
|
113 | - // Loop through |
|
114 | - foreach ( $stats as $stat ) { |
|
115 | - $row = array_values( $this->prepare_row( $stat, $headers ) ); |
|
116 | - $row = array_map( 'maybe_serialize', $row ); |
|
117 | - fputcsv( $stream, $row ); |
|
118 | - } |
|
119 | - |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Downloads graph as json |
|
124 | - * |
|
125 | - * @param array $stats The stats being downloaded. |
|
126 | - * @param resource $stream The stream to output to. |
|
127 | - * @param array $headers The fields to stream. |
|
128 | - * @since 1.0.19 |
|
129 | - */ |
|
130 | - public function download_json( $stats, $stream, $headers ) { |
|
131 | - |
|
132 | - $prepared = array(); |
|
133 | - |
|
134 | - // Loop through |
|
135 | - foreach ( $stats as $stat ) { |
|
136 | - $prepared[] = $this->prepare_row( $stat, $headers ); |
|
137 | - } |
|
138 | - |
|
139 | - fwrite( $stream, wp_json_encode( $prepared ) ); |
|
140 | - |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Downloads graph as xml |
|
145 | - * |
|
146 | - * @param array $stats The stats being downloaded. |
|
147 | - * @param resource $stream The stream to output to. |
|
148 | - * @param array $headers The fields to stream. |
|
149 | - * @since 1.0.19 |
|
150 | - */ |
|
151 | - public function download_xml( $stats, $stream, $headers ) { |
|
152 | - |
|
153 | - $prepared = array(); |
|
154 | - |
|
155 | - // Loop through |
|
156 | - foreach ( $stats as $stat ) { |
|
157 | - $prepared[] = $this->prepare_row( $stat, $headers ); |
|
158 | - } |
|
159 | - |
|
160 | - $xml = new SimpleXMLElement('<?xml version="1.0"?><data></data>'); |
|
161 | - $this->convert_array_xml( $prepared, $xml ); |
|
162 | - |
|
163 | - fwrite( $stream, $xml->asXML() ); |
|
164 | - |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Converts stats array to xml |
|
169 | - * |
|
170 | - * @access public |
|
171 | - * @since 1.0.19 |
|
172 | - */ |
|
173 | - public function convert_array_xml( $data, $xml ) { |
|
174 | - |
|
175 | - // Loop through |
|
176 | - foreach ( $data as $key => $value ) { |
|
177 | - |
|
178 | - $key = preg_replace( "/[^A-Za-z0-9_\-]/", '', $key ); |
|
179 | - |
|
180 | - if ( is_array( $value ) ) { |
|
181 | - |
|
182 | - if ( is_numeric( $key ) ){ |
|
183 | - $key = 'item'.$key; //dealing with <0/>..<n/> issues |
|
184 | - } |
|
185 | - |
|
186 | - $subnode = $xml->addChild( $key ); |
|
187 | - $this->convert_array_xml( $value, $subnode ); |
|
188 | - |
|
189 | - } else { |
|
190 | - $xml->addChild( $key, htmlspecialchars( $value ) ); |
|
191 | - } |
|
192 | - |
|
193 | - } |
|
15 | + /** |
|
16 | + * @var GetPaid_Reports_Report |
|
17 | + */ |
|
18 | + public $handler; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + */ |
|
24 | + public function __construct() { |
|
25 | + $this->handler = new GetPaid_Reports_Report(); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Prepares the datastore handler. |
|
30 | + * |
|
31 | + * @return GetPaid_Reports_Report_Items|GetPaid_Reports_Report_Gateways|GetPaid_Reports_Report_Discounts |
|
32 | + */ |
|
33 | + public function prepare_handler( $graph ) { |
|
34 | + |
|
35 | + if ( empty( $this->handler->views[ $graph ] ) ) { |
|
36 | + wp_die( __( 'Invalid Graph', 'invoicing' ), 400 ); |
|
37 | + } |
|
38 | + |
|
39 | + return new $this->handler->views[ $graph ]['class'](); |
|
40 | + |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Prepares the output stream. |
|
45 | + * |
|
46 | + * @return resource |
|
47 | + */ |
|
48 | + public function prepare_output() { |
|
49 | + |
|
50 | + $output = fopen( 'php://output', 'w' ); |
|
51 | + |
|
52 | + if ( false === $output ) { |
|
53 | + wp_die( __( 'Unsupported server', 'invoicing' ), 500 ); |
|
54 | + } |
|
55 | + |
|
56 | + return $output; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Prepares the file type. |
|
61 | + * |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public function prepare_file_type( $graph ) { |
|
65 | + |
|
66 | + $file_type = empty( $_REQUEST['file_type'] ) ? 'csv' : sanitize_text_field( $_REQUEST['file_type'] ); |
|
67 | + $file_name = wpinv_sanitize_key( "getpaid-$graph-" . current_time( 'Y-m-d' ) ); |
|
68 | + |
|
69 | + header( "Content-Type:application/$file_type" ); |
|
70 | + header( "Content-Disposition:attachment;filename=$file_name.$file_type" ); |
|
71 | + |
|
72 | + return $file_type; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Handles the actual download. |
|
77 | + * |
|
78 | + */ |
|
79 | + public function download( $graph ) { |
|
80 | + global $wpdb; |
|
81 | + |
|
82 | + $handler = $this->prepare_handler( $graph ); |
|
83 | + $stream = $this->prepare_output(); |
|
84 | + $stats = $wpdb->get_results( $handler->get_sql( $handler->get_range() ) ); |
|
85 | + $headers = array( $handler->field, 'total', 'total_raw' ); |
|
86 | + $file_type = $this->prepare_file_type( $graph ); |
|
87 | + |
|
88 | + if ( 'csv' == $file_type ) { |
|
89 | + $this->download_csv( $stats, $stream, $headers ); |
|
90 | + } else if( 'xml' == $file_type ) { |
|
91 | + $this->download_xml( $stats, $stream, $headers ); |
|
92 | + } else { |
|
93 | + $this->download_json( $stats, $stream, $headers ); |
|
94 | + } |
|
95 | + |
|
96 | + fclose( $stream ); |
|
97 | + exit; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Downloads graph as csv |
|
102 | + * |
|
103 | + * @param array $stats The stats being downloaded. |
|
104 | + * @param resource $stream The stream to output to. |
|
105 | + * @param array $headers The fields to stream. |
|
106 | + * @since 1.0.19 |
|
107 | + */ |
|
108 | + public function download_csv( $stats, $stream, $headers ) { |
|
109 | + |
|
110 | + // Output the csv column headers. |
|
111 | + fputcsv( $stream, $headers ); |
|
112 | + |
|
113 | + // Loop through |
|
114 | + foreach ( $stats as $stat ) { |
|
115 | + $row = array_values( $this->prepare_row( $stat, $headers ) ); |
|
116 | + $row = array_map( 'maybe_serialize', $row ); |
|
117 | + fputcsv( $stream, $row ); |
|
118 | + } |
|
119 | + |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Downloads graph as json |
|
124 | + * |
|
125 | + * @param array $stats The stats being downloaded. |
|
126 | + * @param resource $stream The stream to output to. |
|
127 | + * @param array $headers The fields to stream. |
|
128 | + * @since 1.0.19 |
|
129 | + */ |
|
130 | + public function download_json( $stats, $stream, $headers ) { |
|
131 | + |
|
132 | + $prepared = array(); |
|
133 | + |
|
134 | + // Loop through |
|
135 | + foreach ( $stats as $stat ) { |
|
136 | + $prepared[] = $this->prepare_row( $stat, $headers ); |
|
137 | + } |
|
138 | + |
|
139 | + fwrite( $stream, wp_json_encode( $prepared ) ); |
|
140 | + |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Downloads graph as xml |
|
145 | + * |
|
146 | + * @param array $stats The stats being downloaded. |
|
147 | + * @param resource $stream The stream to output to. |
|
148 | + * @param array $headers The fields to stream. |
|
149 | + * @since 1.0.19 |
|
150 | + */ |
|
151 | + public function download_xml( $stats, $stream, $headers ) { |
|
152 | + |
|
153 | + $prepared = array(); |
|
154 | + |
|
155 | + // Loop through |
|
156 | + foreach ( $stats as $stat ) { |
|
157 | + $prepared[] = $this->prepare_row( $stat, $headers ); |
|
158 | + } |
|
159 | + |
|
160 | + $xml = new SimpleXMLElement('<?xml version="1.0"?><data></data>'); |
|
161 | + $this->convert_array_xml( $prepared, $xml ); |
|
162 | + |
|
163 | + fwrite( $stream, $xml->asXML() ); |
|
164 | + |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Converts stats array to xml |
|
169 | + * |
|
170 | + * @access public |
|
171 | + * @since 1.0.19 |
|
172 | + */ |
|
173 | + public function convert_array_xml( $data, $xml ) { |
|
174 | + |
|
175 | + // Loop through |
|
176 | + foreach ( $data as $key => $value ) { |
|
177 | + |
|
178 | + $key = preg_replace( "/[^A-Za-z0-9_\-]/", '', $key ); |
|
179 | + |
|
180 | + if ( is_array( $value ) ) { |
|
181 | + |
|
182 | + if ( is_numeric( $key ) ){ |
|
183 | + $key = 'item'.$key; //dealing with <0/>..<n/> issues |
|
184 | + } |
|
185 | + |
|
186 | + $subnode = $xml->addChild( $key ); |
|
187 | + $this->convert_array_xml( $value, $subnode ); |
|
188 | + |
|
189 | + } else { |
|
190 | + $xml->addChild( $key, htmlspecialchars( $value ) ); |
|
191 | + } |
|
192 | + |
|
193 | + } |
|
194 | 194 | |
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Prepares a single row for download. |
|
199 | - * |
|
200 | - * @param stdClass|array $row The row to prepare.. |
|
201 | - * @param array $fields The fields to stream. |
|
202 | - * @since 1.0.19 |
|
203 | - * @return array |
|
204 | - */ |
|
205 | - public function prepare_row( $row, $fields ) { |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Prepares a single row for download. |
|
199 | + * |
|
200 | + * @param stdClass|array $row The row to prepare.. |
|
201 | + * @param array $fields The fields to stream. |
|
202 | + * @since 1.0.19 |
|
203 | + * @return array |
|
204 | + */ |
|
205 | + public function prepare_row( $row, $fields ) { |
|
206 | 206 | |
207 | - $prepared = array(); |
|
208 | - $row = (array) $row; |
|
207 | + $prepared = array(); |
|
208 | + $row = (array) $row; |
|
209 | 209 | |
210 | - foreach ( $fields as $field ) { |
|
210 | + foreach ( $fields as $field ) { |
|
211 | 211 | |
212 | - if ( $field === 'total' ) { |
|
213 | - $prepared[ $field ] = html_entity_decode( strip_tags( wpinv_price( $row['total'] ) ), ENT_QUOTES ); |
|
214 | - continue; |
|
215 | - } |
|
212 | + if ( $field === 'total' ) { |
|
213 | + $prepared[ $field ] = html_entity_decode( strip_tags( wpinv_price( $row['total'] ) ), ENT_QUOTES ); |
|
214 | + continue; |
|
215 | + } |
|
216 | 216 | |
217 | - if ( $field === 'total_raw' ) { |
|
218 | - $prepared[ $field ] = wpinv_round_amount( wpinv_sanitize_amount( $row['total'] ) ); |
|
219 | - continue; |
|
220 | - } |
|
217 | + if ( $field === 'total_raw' ) { |
|
218 | + $prepared[ $field ] = wpinv_round_amount( wpinv_sanitize_amount( $row['total'] ) ); |
|
219 | + continue; |
|
220 | + } |
|
221 | 221 | |
222 | - $prepared[ $field ] = strip_tags( $row[ $field ] ); |
|
222 | + $prepared[ $field ] = strip_tags( $row[ $field ] ); |
|
223 | 223 | |
224 | - } |
|
224 | + } |
|
225 | 225 | |
226 | - return $prepared; |
|
227 | - } |
|
226 | + return $prepared; |
|
227 | + } |
|
228 | 228 | |
229 | 229 | |
230 | 230 | } |
@@ -12,46 +12,46 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Reports_Export { |
14 | 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 | - ?> |
|
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 | 55 | |
56 | 56 | <div class="col-12 col-md-6"> |
57 | 57 | <div class="card m-0 p-0" style="max-width:100%"> |
@@ -59,11 +59,11 @@ discard block |
||
59 | 59 | <div class="card-header"> |
60 | 60 | <strong> |
61 | 61 | <?php |
62 | - printf( |
|
63 | - __( 'Export %s', 'invoicing' ), |
|
64 | - sanitize_text_field( getpaid_get_post_type_label( $post_type ) ) |
|
65 | - ); |
|
66 | - ?> |
|
62 | + printf( |
|
63 | + __( 'Export %s', 'invoicing' ), |
|
64 | + sanitize_text_field( getpaid_get_post_type_label( $post_type ) ) |
|
65 | + ); |
|
66 | + ?> |
|
67 | 67 | </strong> |
68 | 68 | </div> |
69 | 69 | |
@@ -72,12 +72,12 @@ discard block |
||
72 | 72 | <form method="post" action="<?php echo esc_url( $this->get_download_url( $post_type ) ); ?>"> |
73 | 73 | |
74 | 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 | - ?> |
|
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 | 81 | |
82 | 82 | </form> |
83 | 83 | |
@@ -88,107 +88,107 @@ discard block |
||
88 | 88 | |
89 | 89 | <?php |
90 | 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 | - } |
|
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 | 193 | |
194 | 194 | } |
@@ -12,194 +12,194 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Invoice_Exporter extends GetPaid_Graph_Downloader { |
14 | 14 | |
15 | - /** |
|
16 | - * Retrieves invoices query args. |
|
17 | - * |
|
18 | - * @param string $post_type post type to retrieve. |
|
19 | - * @param array $args Args to search for. |
|
20 | - * @return array |
|
21 | - */ |
|
22 | - public function get_invoice_query_args( $post_type, $args ) { |
|
23 | - |
|
24 | - $query_args = array( |
|
25 | - 'post_type' => $post_type, |
|
26 | - 'post_status' => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ), |
|
27 | - 'posts_per_page' => -1, |
|
28 | - 'no_found_rows' => true, |
|
29 | - 'update_post_term_cache' => false, |
|
30 | - 'fields' => 'ids', |
|
31 | - ); |
|
32 | - |
|
33 | - if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) { |
|
34 | - $query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) ); |
|
35 | - } |
|
36 | - |
|
37 | - $date_query = array(); |
|
38 | - if ( ! empty( $args['to_date'] ) ) { |
|
39 | - $date_query['before'] = wpinv_clean( $args['to_date'] ); |
|
40 | - } |
|
41 | - |
|
42 | - if ( ! empty( $args['from_date'] ) ) { |
|
43 | - $date_query['after'] = wpinv_clean( $args['from_date'] ); |
|
44 | - } |
|
45 | - |
|
46 | - if ( ! empty( $date_query ) ) { |
|
47 | - $date_query['inclusive'] = true; |
|
48 | - $query_args['date_query'] = array( $date_query ); |
|
49 | - } |
|
50 | - |
|
51 | - return $query_args; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Retrieves invoices. |
|
56 | - * |
|
57 | - * @param array $query_args WP_Query args. |
|
58 | - * @return WPInv_Invoice[] |
|
59 | - */ |
|
60 | - public function get_invoices( $query_args ) { |
|
61 | - |
|
62 | - // Get invoices. |
|
63 | - $invoices = new WP_Query( $query_args ); |
|
64 | - |
|
65 | - // Prepare the results. |
|
66 | - return array_map( 'wpinv_get_invoice', $invoices->posts ); |
|
67 | - |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Handles the actual download. |
|
72 | - * |
|
73 | - */ |
|
74 | - public function export( $post_type, $args ) { |
|
75 | - |
|
76 | - $invoices = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) ); |
|
77 | - $stream = $this->prepare_output(); |
|
78 | - $headers = $this->get_export_fields( $post_type ); |
|
79 | - $file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) ); |
|
80 | - |
|
81 | - if ( 'csv' == $file_type ) { |
|
82 | - $this->download_csv( $invoices, $stream, $headers ); |
|
83 | - } else if( 'xml' == $file_type ) { |
|
84 | - $this->download_xml( $invoices, $stream, $headers ); |
|
85 | - } else { |
|
86 | - $this->download_json( $invoices, $stream, $headers ); |
|
87 | - } |
|
88 | - |
|
89 | - fclose( $stream ); |
|
90 | - exit; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Prepares a single invoice for download. |
|
95 | - * |
|
96 | - * @param WPInv_Invoice $invoice The invoice to prepare.. |
|
97 | - * @param array $fields The fields to stream. |
|
98 | - * @since 1.0.19 |
|
99 | - * @return array |
|
100 | - */ |
|
101 | - public function prepare_row( $invoice, $fields ) { |
|
102 | - |
|
103 | - $prepared = array(); |
|
104 | - $amount_fields = $this->get_amount_fields( $invoice->get_post_type() ); |
|
105 | - |
|
106 | - foreach ( $fields as $field ) { |
|
107 | - |
|
108 | - $value = ''; |
|
109 | - $method = "get_$field"; |
|
110 | - |
|
111 | - if ( method_exists( $invoice, $method ) ) { |
|
112 | - $value = $invoice->$method(); |
|
113 | - } |
|
114 | - |
|
115 | - if ( in_array( $field, $amount_fields ) ) { |
|
116 | - $value = wpinv_round_amount( wpinv_sanitize_amount( $value ) ); |
|
117 | - } |
|
118 | - |
|
119 | - $prepared[ $field ] = wpinv_clean( $value ); |
|
120 | - |
|
121 | - } |
|
122 | - |
|
123 | - return $prepared; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Retrieves export fields. |
|
128 | - * |
|
129 | - * @param string $post_type |
|
130 | - * @since 1.0.19 |
|
131 | - * @return array |
|
132 | - */ |
|
133 | - public function get_export_fields( $post_type ) { |
|
134 | - |
|
135 | - $fields = array( |
|
136 | - 'id', |
|
137 | - 'parent_id', |
|
138 | - 'status', |
|
139 | - 'date_created', |
|
140 | - 'date_modified', |
|
141 | - 'date_due', |
|
142 | - 'date_completed', |
|
143 | - 'number', |
|
144 | - 'key', |
|
145 | - 'description', |
|
146 | - 'post_type', |
|
147 | - 'mode', |
|
148 | - 'customer_id', |
|
149 | - 'customer_first_name', |
|
150 | - 'customer_last_name', |
|
151 | - 'customer_phone', |
|
152 | - 'customer_email', |
|
153 | - 'customer_country', |
|
154 | - 'customer_city', |
|
155 | - 'customer_state', |
|
156 | - 'customer_zip', |
|
157 | - 'customer_company', |
|
158 | - 'customer_vat_number', |
|
159 | - 'customer_address', |
|
160 | - 'subtotal', |
|
161 | - 'total_discount', |
|
162 | - 'total_tax', |
|
163 | - 'total_fees', |
|
164 | - 'fees', |
|
165 | - 'discounts', |
|
166 | - 'taxes', |
|
167 | - 'cart_details', |
|
168 | - 'item_ids', |
|
169 | - 'payment_form', |
|
170 | - 'discount_code', |
|
171 | - 'gateway', |
|
172 | - 'transaction_id', |
|
173 | - 'currency', |
|
174 | - 'disable_taxes', |
|
175 | - 'subscription_id', |
|
176 | - 'remote_subscription_id', |
|
177 | - 'is_viewed', |
|
178 | - 'email_cc', |
|
179 | - 'template', |
|
180 | - 'created_via' |
|
181 | - ); |
|
182 | - |
|
183 | - return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type ); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Retrieves amount fields. |
|
188 | - * |
|
189 | - * @param string $post_type |
|
190 | - * @since 1.0.19 |
|
191 | - * @return array |
|
192 | - */ |
|
193 | - public function get_amount_fields( $post_type ) { |
|
194 | - |
|
195 | - $fields = array( |
|
196 | - 'subtotal', |
|
197 | - 'total_discount', |
|
198 | - 'total_tax', |
|
199 | - 'total_fees' |
|
200 | - ); |
|
201 | - |
|
202 | - return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type ); |
|
203 | - } |
|
15 | + /** |
|
16 | + * Retrieves invoices query args. |
|
17 | + * |
|
18 | + * @param string $post_type post type to retrieve. |
|
19 | + * @param array $args Args to search for. |
|
20 | + * @return array |
|
21 | + */ |
|
22 | + public function get_invoice_query_args( $post_type, $args ) { |
|
23 | + |
|
24 | + $query_args = array( |
|
25 | + 'post_type' => $post_type, |
|
26 | + 'post_status' => array_keys( wpinv_get_invoice_statuses( true, false, $post_type ) ), |
|
27 | + 'posts_per_page' => -1, |
|
28 | + 'no_found_rows' => true, |
|
29 | + 'update_post_term_cache' => false, |
|
30 | + 'fields' => 'ids', |
|
31 | + ); |
|
32 | + |
|
33 | + if ( ! empty( $args['status'] ) && in_array( $args['status'], $query_args['post_status'], true ) ) { |
|
34 | + $query_args['post_status'] = wpinv_clean( wpinv_parse_list( $args['status'] ) ); |
|
35 | + } |
|
36 | + |
|
37 | + $date_query = array(); |
|
38 | + if ( ! empty( $args['to_date'] ) ) { |
|
39 | + $date_query['before'] = wpinv_clean( $args['to_date'] ); |
|
40 | + } |
|
41 | + |
|
42 | + if ( ! empty( $args['from_date'] ) ) { |
|
43 | + $date_query['after'] = wpinv_clean( $args['from_date'] ); |
|
44 | + } |
|
45 | + |
|
46 | + if ( ! empty( $date_query ) ) { |
|
47 | + $date_query['inclusive'] = true; |
|
48 | + $query_args['date_query'] = array( $date_query ); |
|
49 | + } |
|
50 | + |
|
51 | + return $query_args; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Retrieves invoices. |
|
56 | + * |
|
57 | + * @param array $query_args WP_Query args. |
|
58 | + * @return WPInv_Invoice[] |
|
59 | + */ |
|
60 | + public function get_invoices( $query_args ) { |
|
61 | + |
|
62 | + // Get invoices. |
|
63 | + $invoices = new WP_Query( $query_args ); |
|
64 | + |
|
65 | + // Prepare the results. |
|
66 | + return array_map( 'wpinv_get_invoice', $invoices->posts ); |
|
67 | + |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Handles the actual download. |
|
72 | + * |
|
73 | + */ |
|
74 | + public function export( $post_type, $args ) { |
|
75 | + |
|
76 | + $invoices = $this->get_invoices( $this->get_invoice_query_args( $post_type, $args ) ); |
|
77 | + $stream = $this->prepare_output(); |
|
78 | + $headers = $this->get_export_fields( $post_type ); |
|
79 | + $file_type = $this->prepare_file_type( strtolower( getpaid_get_post_type_label( $post_type ) ) ); |
|
80 | + |
|
81 | + if ( 'csv' == $file_type ) { |
|
82 | + $this->download_csv( $invoices, $stream, $headers ); |
|
83 | + } else if( 'xml' == $file_type ) { |
|
84 | + $this->download_xml( $invoices, $stream, $headers ); |
|
85 | + } else { |
|
86 | + $this->download_json( $invoices, $stream, $headers ); |
|
87 | + } |
|
88 | + |
|
89 | + fclose( $stream ); |
|
90 | + exit; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Prepares a single invoice for download. |
|
95 | + * |
|
96 | + * @param WPInv_Invoice $invoice The invoice to prepare.. |
|
97 | + * @param array $fields The fields to stream. |
|
98 | + * @since 1.0.19 |
|
99 | + * @return array |
|
100 | + */ |
|
101 | + public function prepare_row( $invoice, $fields ) { |
|
102 | + |
|
103 | + $prepared = array(); |
|
104 | + $amount_fields = $this->get_amount_fields( $invoice->get_post_type() ); |
|
105 | + |
|
106 | + foreach ( $fields as $field ) { |
|
107 | + |
|
108 | + $value = ''; |
|
109 | + $method = "get_$field"; |
|
110 | + |
|
111 | + if ( method_exists( $invoice, $method ) ) { |
|
112 | + $value = $invoice->$method(); |
|
113 | + } |
|
114 | + |
|
115 | + if ( in_array( $field, $amount_fields ) ) { |
|
116 | + $value = wpinv_round_amount( wpinv_sanitize_amount( $value ) ); |
|
117 | + } |
|
118 | + |
|
119 | + $prepared[ $field ] = wpinv_clean( $value ); |
|
120 | + |
|
121 | + } |
|
122 | + |
|
123 | + return $prepared; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Retrieves export fields. |
|
128 | + * |
|
129 | + * @param string $post_type |
|
130 | + * @since 1.0.19 |
|
131 | + * @return array |
|
132 | + */ |
|
133 | + public function get_export_fields( $post_type ) { |
|
134 | + |
|
135 | + $fields = array( |
|
136 | + 'id', |
|
137 | + 'parent_id', |
|
138 | + 'status', |
|
139 | + 'date_created', |
|
140 | + 'date_modified', |
|
141 | + 'date_due', |
|
142 | + 'date_completed', |
|
143 | + 'number', |
|
144 | + 'key', |
|
145 | + 'description', |
|
146 | + 'post_type', |
|
147 | + 'mode', |
|
148 | + 'customer_id', |
|
149 | + 'customer_first_name', |
|
150 | + 'customer_last_name', |
|
151 | + 'customer_phone', |
|
152 | + 'customer_email', |
|
153 | + 'customer_country', |
|
154 | + 'customer_city', |
|
155 | + 'customer_state', |
|
156 | + 'customer_zip', |
|
157 | + 'customer_company', |
|
158 | + 'customer_vat_number', |
|
159 | + 'customer_address', |
|
160 | + 'subtotal', |
|
161 | + 'total_discount', |
|
162 | + 'total_tax', |
|
163 | + 'total_fees', |
|
164 | + 'fees', |
|
165 | + 'discounts', |
|
166 | + 'taxes', |
|
167 | + 'cart_details', |
|
168 | + 'item_ids', |
|
169 | + 'payment_form', |
|
170 | + 'discount_code', |
|
171 | + 'gateway', |
|
172 | + 'transaction_id', |
|
173 | + 'currency', |
|
174 | + 'disable_taxes', |
|
175 | + 'subscription_id', |
|
176 | + 'remote_subscription_id', |
|
177 | + 'is_viewed', |
|
178 | + 'email_cc', |
|
179 | + 'template', |
|
180 | + 'created_via' |
|
181 | + ); |
|
182 | + |
|
183 | + return apply_filters( 'getpaid_invoice_exporter_get_fields', $fields, $post_type ); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Retrieves amount fields. |
|
188 | + * |
|
189 | + * @param string $post_type |
|
190 | + * @since 1.0.19 |
|
191 | + * @return array |
|
192 | + */ |
|
193 | + public function get_amount_fields( $post_type ) { |
|
194 | + |
|
195 | + $fields = array( |
|
196 | + 'subtotal', |
|
197 | + 'total_discount', |
|
198 | + 'total_tax', |
|
199 | + 'total_fees' |
|
200 | + ); |
|
201 | + |
|
202 | + return apply_filters( 'getpaid_invoice_exporter_get_amount_fields', $fields, $post_type ); |
|
203 | + } |
|
204 | 204 | |
205 | 205 | } |
@@ -215,16 +215,16 @@ discard block |
||
215 | 215 | function getpaid_prepare_item_tax( $item, $tax_name, $tax_amount, $recurring_tax_amount ) { |
216 | 216 | |
217 | 217 | $initial_tax = $tax_amount; |
218 | - $recurring_tax = 0; |
|
218 | + $recurring_tax = 0; |
|
219 | 219 | |
220 | 220 | if ( $item->is_recurring() ) { |
221 | - $recurring_tax = $recurring_tax_amount; |
|
222 | - } |
|
221 | + $recurring_tax = $recurring_tax_amount; |
|
222 | + } |
|
223 | 223 | |
224 | - return array( |
|
225 | - 'name' => sanitize_text_field( $tax_name ), |
|
226 | - 'initial_tax' => $initial_tax, |
|
227 | - 'recurring_tax' => $recurring_tax, |
|
224 | + return array( |
|
225 | + 'name' => sanitize_text_field( $tax_name ), |
|
226 | + 'initial_tax' => $initial_tax, |
|
227 | + 'recurring_tax' => $recurring_tax, |
|
228 | 228 | ); |
229 | 229 | |
230 | 230 | } |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | */ |
329 | 329 | function wpinv_should_validate_vat_number() { |
330 | 330 | $validate = wpinv_get_option( 'validate_vat_number' ); |
331 | - return ! empty( $validate ); |
|
331 | + return ! empty( $validate ); |
|
332 | 332 | } |
333 | 333 | |
334 | 334 | function wpinv_sales_tax_for_year( $year = null ) { |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
11 | - exit; // Exit if accessed directly |
|
11 | + exit; // Exit if accessed directly |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | /** |
@@ -17,10 +17,10 @@ discard block |
||
17 | 17 | class GetPaid_Meta_Box_Item_VAT { |
18 | 18 | |
19 | 19 | /** |
20 | - * Output the metabox. |
|
21 | - * |
|
22 | - * @param WP_Post $post |
|
23 | - */ |
|
20 | + * Output the metabox. |
|
21 | + * |
|
22 | + * @param WP_Post $post |
|
23 | + */ |
|
24 | 24 | public static function output( $post ) { |
25 | 25 | |
26 | 26 | // Prepare the item. |
@@ -46,10 +46,10 @@ discard block |
||
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
49 | - * Output the VAT rules settings. |
|
50 | - * |
|
51 | - * @param WPInv_Item $item |
|
52 | - */ |
|
49 | + * Output the VAT rules settings. |
|
50 | + * |
|
51 | + * @param WPInv_Item $item |
|
52 | + */ |
|
53 | 53 | public static function output_vat_rules( $item ) { |
54 | 54 | ?> |
55 | 55 | |
@@ -87,10 +87,10 @@ discard block |
||
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
90 | - * Output the VAT class settings. |
|
91 | - * |
|
92 | - * @param WPInv_Item $item |
|
93 | - */ |
|
90 | + * Output the VAT class settings. |
|
91 | + * |
|
92 | + * @param WPInv_Item $item |
|
93 | + */ |
|
94 | 94 | public static function output_vat_classes( $item ) { |
95 | 95 | ?> |
96 | 96 |