@@ -12,207 +12,207 @@ |
||
12 | 12 | */ |
13 | 13 | class WPInv_Notes { |
14 | 14 | |
15 | - /** |
|
16 | - * Class constructor. |
|
17 | - */ |
|
18 | - public function __construct() { |
|
19 | - |
|
20 | - // Filter inovice notes. |
|
21 | - add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
22 | - add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
23 | - |
|
24 | - // Delete comments count cache whenever there is a new comment or a comment status changes. |
|
25 | - add_action( 'wp_insert_comment', array( $this, 'delete_comments_count_cache' ) ); |
|
26 | - add_action( 'wp_set_comment_status', array( $this, 'delete_comments_count_cache' ) ); |
|
27 | - |
|
28 | - // Count comments. |
|
29 | - add_filter( 'wp_count_comments', array( $this, 'wp_count_comments' ), 100, 2 ); |
|
30 | - |
|
31 | - // Fires after notes are loaded. |
|
32 | - do_action( 'wpinv_notes_init', $this ); |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Filters invoice notes query to only include our notes. |
|
37 | - * |
|
38 | - * @param WP_Comment_Query $query |
|
39 | - */ |
|
40 | - public function set_invoice_note_type( $query ) { |
|
41 | - $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
42 | - |
|
43 | - if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
44 | - $query->query_vars['type'] = 'wpinv_note'; |
|
45 | - } else { |
|
46 | - |
|
47 | - if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
48 | - $query->query_vars['type__not_in'] = array(); |
|
49 | - } |
|
50 | - |
|
51 | - $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
52 | - $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
53 | - } |
|
54 | - |
|
55 | - return $query; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Exclude notes from the comments feed. |
|
60 | - */ |
|
61 | - function wpinv_comment_feed_where( $where ) { |
|
62 | - return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Delete comments count cache whenever there is |
|
67 | - * new comment or the status of a comment changes. Cache |
|
68 | - * will be regenerated next time WPInv_Notes::wp_count_comments() |
|
69 | - * is called. |
|
70 | - */ |
|
71 | - public function delete_comments_count_cache() { |
|
72 | - delete_transient( 'getpaid_count_comments' ); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Remove invoice notes from wp_count_comments(). |
|
77 | - * |
|
78 | - * @since 2.2 |
|
79 | - * @param object $stats Comment stats. |
|
80 | - * @param int $post_id Post ID. |
|
81 | - * @return object |
|
82 | - */ |
|
83 | - public function wp_count_comments( $stats, $post_id ) { |
|
84 | - global $wpdb; |
|
85 | - |
|
86 | - if ( empty( $post_id ) ) { |
|
87 | - $stats = get_transient( 'getpaid_count_comments' ); |
|
88 | - |
|
89 | - if ( ! $stats ) { |
|
90 | - $stats = array( |
|
91 | - 'total_comments' => 0, |
|
92 | - 'all' => 0, |
|
93 | - ); |
|
94 | - |
|
95 | - $count = $wpdb->get_results( |
|
96 | - " |
|
15 | + /** |
|
16 | + * Class constructor. |
|
17 | + */ |
|
18 | + public function __construct() { |
|
19 | + |
|
20 | + // Filter inovice notes. |
|
21 | + add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
22 | + add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
23 | + |
|
24 | + // Delete comments count cache whenever there is a new comment or a comment status changes. |
|
25 | + add_action( 'wp_insert_comment', array( $this, 'delete_comments_count_cache' ) ); |
|
26 | + add_action( 'wp_set_comment_status', array( $this, 'delete_comments_count_cache' ) ); |
|
27 | + |
|
28 | + // Count comments. |
|
29 | + add_filter( 'wp_count_comments', array( $this, 'wp_count_comments' ), 100, 2 ); |
|
30 | + |
|
31 | + // Fires after notes are loaded. |
|
32 | + do_action( 'wpinv_notes_init', $this ); |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Filters invoice notes query to only include our notes. |
|
37 | + * |
|
38 | + * @param WP_Comment_Query $query |
|
39 | + */ |
|
40 | + public function set_invoice_note_type( $query ) { |
|
41 | + $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
42 | + |
|
43 | + if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
44 | + $query->query_vars['type'] = 'wpinv_note'; |
|
45 | + } else { |
|
46 | + |
|
47 | + if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
48 | + $query->query_vars['type__not_in'] = array(); |
|
49 | + } |
|
50 | + |
|
51 | + $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
52 | + $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
53 | + } |
|
54 | + |
|
55 | + return $query; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Exclude notes from the comments feed. |
|
60 | + */ |
|
61 | + function wpinv_comment_feed_where( $where ) { |
|
62 | + return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Delete comments count cache whenever there is |
|
67 | + * new comment or the status of a comment changes. Cache |
|
68 | + * will be regenerated next time WPInv_Notes::wp_count_comments() |
|
69 | + * is called. |
|
70 | + */ |
|
71 | + public function delete_comments_count_cache() { |
|
72 | + delete_transient( 'getpaid_count_comments' ); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Remove invoice notes from wp_count_comments(). |
|
77 | + * |
|
78 | + * @since 2.2 |
|
79 | + * @param object $stats Comment stats. |
|
80 | + * @param int $post_id Post ID. |
|
81 | + * @return object |
|
82 | + */ |
|
83 | + public function wp_count_comments( $stats, $post_id ) { |
|
84 | + global $wpdb; |
|
85 | + |
|
86 | + if ( empty( $post_id ) ) { |
|
87 | + $stats = get_transient( 'getpaid_count_comments' ); |
|
88 | + |
|
89 | + if ( ! $stats ) { |
|
90 | + $stats = array( |
|
91 | + 'total_comments' => 0, |
|
92 | + 'all' => 0, |
|
93 | + ); |
|
94 | + |
|
95 | + $count = $wpdb->get_results( |
|
96 | + " |
|
97 | 97 | SELECT comment_approved, COUNT(*) AS num_comments |
98 | 98 | FROM {$wpdb->comments} |
99 | 99 | WHERE comment_type NOT IN ('action_log', 'order_note', 'webhook_delivery', 'wpinv_note') |
100 | 100 | GROUP BY comment_approved |
101 | 101 | ", |
102 | - ARRAY_A |
|
103 | - ); |
|
104 | - |
|
105 | - $approved = array( |
|
106 | - '0' => 'moderated', |
|
107 | - '1' => 'approved', |
|
108 | - 'spam' => 'spam', |
|
109 | - 'trash' => 'trash', |
|
110 | - 'post-trashed' => 'post-trashed', |
|
111 | - ); |
|
112 | - |
|
113 | - foreach ( (array) $count as $row ) { |
|
114 | - // Don't count post-trashed toward totals. |
|
115 | - if ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash', 'spam' ), true ) ) { |
|
116 | - $stats['all'] += $row['num_comments']; |
|
117 | - $stats['total_comments'] += $row['num_comments']; |
|
118 | - } elseif ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash' ), true ) ) { |
|
119 | - $stats['total_comments'] += $row['num_comments']; |
|
120 | - } |
|
121 | - if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
122 | - $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - foreach ( $approved as $key ) { |
|
127 | - if ( empty( $stats[ $key ] ) ) { |
|
128 | - $stats[ $key ] = 0; |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - $stats = (object) $stats; |
|
133 | - set_transient( 'getpaid_count_comments', $stats ); |
|
134 | - } |
|
102 | + ARRAY_A |
|
103 | + ); |
|
104 | + |
|
105 | + $approved = array( |
|
106 | + '0' => 'moderated', |
|
107 | + '1' => 'approved', |
|
108 | + 'spam' => 'spam', |
|
109 | + 'trash' => 'trash', |
|
110 | + 'post-trashed' => 'post-trashed', |
|
111 | + ); |
|
112 | + |
|
113 | + foreach ( (array) $count as $row ) { |
|
114 | + // Don't count post-trashed toward totals. |
|
115 | + if ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash', 'spam' ), true ) ) { |
|
116 | + $stats['all'] += $row['num_comments']; |
|
117 | + $stats['total_comments'] += $row['num_comments']; |
|
118 | + } elseif ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash' ), true ) ) { |
|
119 | + $stats['total_comments'] += $row['num_comments']; |
|
120 | + } |
|
121 | + if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
122 | + $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + foreach ( $approved as $key ) { |
|
127 | + if ( empty( $stats[ $key ] ) ) { |
|
128 | + $stats[ $key ] = 0; |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + $stats = (object) $stats; |
|
133 | + set_transient( 'getpaid_count_comments', $stats ); |
|
134 | + } |
|
135 | 135 | } |
136 | 136 | |
137 | - return $stats; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Returns an array of invoice notes. |
|
142 | - * |
|
143 | - * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
144 | - * @param string $type Optional. Pass in customer to only return customer notes. |
|
145 | - * @return WP_Comment[] |
|
146 | - */ |
|
147 | - public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
148 | - |
|
149 | - // Default comment args. |
|
150 | - $args = array( |
|
151 | - 'post_id' => $invoice_id, |
|
152 | - 'orderby' => 'comment_ID', |
|
153 | - 'order' => 'ASC', |
|
154 | - ); |
|
155 | - |
|
156 | - // Maybe only show customer comments. |
|
157 | - if ( $type == 'customer' ) { |
|
158 | - $args['meta_key'] = '_wpi_customer_note'; |
|
159 | - $args['meta_value'] = 1; |
|
160 | - } |
|
161 | - |
|
162 | - $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
163 | - |
|
164 | - return get_comments( $args ); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Saves an invoice comment. |
|
169 | - * |
|
170 | - * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
171 | - * @param string $note The note content. |
|
172 | - * @param string $note_author The name of the author of the note. |
|
173 | - * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
174 | - * @return int|false The new note's ID on success, false on failure. |
|
175 | - */ |
|
176 | - function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ) { |
|
177 | - |
|
178 | - do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
179 | - |
|
180 | - /** |
|
181 | - * Insert the comment. |
|
182 | - */ |
|
183 | - $note_id = wp_insert_comment( |
|
184 | - wp_filter_comment( |
|
185 | - array( |
|
186 | - 'comment_post_ID' => $invoice->get_id(), |
|
187 | - 'comment_content' => $note, |
|
188 | - 'comment_agent' => 'Invoicing', |
|
189 | - 'user_id' => get_current_user_id(), |
|
190 | - 'comment_author' => $note_author, |
|
191 | - 'comment_author_IP' => wpinv_get_ip(), |
|
192 | - 'comment_author_email' => $author_email, |
|
193 | - 'comment_author_url' => $invoice->get_view_url(), |
|
194 | - 'comment_type' => 'wpinv_note', |
|
195 | - ) |
|
196 | - ) |
|
197 | - ); |
|
198 | - |
|
199 | - do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
200 | - |
|
201 | - // Are we notifying the customer? |
|
202 | - if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
203 | - return $note_id; |
|
204 | - } |
|
205 | - |
|
206 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
207 | - do_action( |
|
137 | + return $stats; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Returns an array of invoice notes. |
|
142 | + * |
|
143 | + * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
144 | + * @param string $type Optional. Pass in customer to only return customer notes. |
|
145 | + * @return WP_Comment[] |
|
146 | + */ |
|
147 | + public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
148 | + |
|
149 | + // Default comment args. |
|
150 | + $args = array( |
|
151 | + 'post_id' => $invoice_id, |
|
152 | + 'orderby' => 'comment_ID', |
|
153 | + 'order' => 'ASC', |
|
154 | + ); |
|
155 | + |
|
156 | + // Maybe only show customer comments. |
|
157 | + if ( $type == 'customer' ) { |
|
158 | + $args['meta_key'] = '_wpi_customer_note'; |
|
159 | + $args['meta_value'] = 1; |
|
160 | + } |
|
161 | + |
|
162 | + $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
163 | + |
|
164 | + return get_comments( $args ); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Saves an invoice comment. |
|
169 | + * |
|
170 | + * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
171 | + * @param string $note The note content. |
|
172 | + * @param string $note_author The name of the author of the note. |
|
173 | + * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
174 | + * @return int|false The new note's ID on success, false on failure. |
|
175 | + */ |
|
176 | + function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ) { |
|
177 | + |
|
178 | + do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
179 | + |
|
180 | + /** |
|
181 | + * Insert the comment. |
|
182 | + */ |
|
183 | + $note_id = wp_insert_comment( |
|
184 | + wp_filter_comment( |
|
185 | + array( |
|
186 | + 'comment_post_ID' => $invoice->get_id(), |
|
187 | + 'comment_content' => $note, |
|
188 | + 'comment_agent' => 'Invoicing', |
|
189 | + 'user_id' => get_current_user_id(), |
|
190 | + 'comment_author' => $note_author, |
|
191 | + 'comment_author_IP' => wpinv_get_ip(), |
|
192 | + 'comment_author_email' => $author_email, |
|
193 | + 'comment_author_url' => $invoice->get_view_url(), |
|
194 | + 'comment_type' => 'wpinv_note', |
|
195 | + ) |
|
196 | + ) |
|
197 | + ); |
|
198 | + |
|
199 | + do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
200 | + |
|
201 | + // Are we notifying the customer? |
|
202 | + if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
203 | + return $note_id; |
|
204 | + } |
|
205 | + |
|
206 | + add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
207 | + do_action( |
|
208 | 208 | 'wpinv_new_customer_note', |
209 | 209 | array( |
210 | - 'invoice_id' => $invoice->get_id(), |
|
211 | - 'user_note' => $note, |
|
210 | + 'invoice_id' => $invoice->get_id(), |
|
211 | + 'user_note' => $note, |
|
212 | 212 | ) |
213 | 213 | ); |
214 | - do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
215 | - return $note_id; |
|
216 | - } |
|
214 | + do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
215 | + return $note_id; |
|
216 | + } |
|
217 | 217 | |
218 | 218 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Handles invoice notes. |
@@ -18,18 +18,18 @@ discard block |
||
18 | 18 | public function __construct() { |
19 | 19 | |
20 | 20 | // Filter inovice notes. |
21 | - add_action( 'pre_get_comments', array( $this, 'set_invoice_note_type' ), 11, 1 ); |
|
22 | - add_action( 'comment_feed_where', array( $this, 'wpinv_comment_feed_where' ), 10, 1 ); |
|
21 | + add_action('pre_get_comments', array($this, 'set_invoice_note_type'), 11, 1); |
|
22 | + add_action('comment_feed_where', array($this, 'wpinv_comment_feed_where'), 10, 1); |
|
23 | 23 | |
24 | 24 | // Delete comments count cache whenever there is a new comment or a comment status changes. |
25 | - add_action( 'wp_insert_comment', array( $this, 'delete_comments_count_cache' ) ); |
|
26 | - add_action( 'wp_set_comment_status', array( $this, 'delete_comments_count_cache' ) ); |
|
25 | + add_action('wp_insert_comment', array($this, 'delete_comments_count_cache')); |
|
26 | + add_action('wp_set_comment_status', array($this, 'delete_comments_count_cache')); |
|
27 | 27 | |
28 | 28 | // Count comments. |
29 | - add_filter( 'wp_count_comments', array( $this, 'wp_count_comments' ), 100, 2 ); |
|
29 | + add_filter('wp_count_comments', array($this, 'wp_count_comments'), 100, 2); |
|
30 | 30 | |
31 | 31 | // Fires after notes are loaded. |
32 | - do_action( 'wpinv_notes_init', $this ); |
|
32 | + do_action('wpinv_notes_init', $this); |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
@@ -37,19 +37,19 @@ discard block |
||
37 | 37 | * |
38 | 38 | * @param WP_Comment_Query $query |
39 | 39 | */ |
40 | - public function set_invoice_note_type( $query ) { |
|
41 | - $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
40 | + public function set_invoice_note_type($query) { |
|
41 | + $post_id = !empty($query->query_vars['post_ID']) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
42 | 42 | |
43 | - if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
43 | + if ($post_id && getpaid_is_invoice_post_type(get_post_type($post_id))) { |
|
44 | 44 | $query->query_vars['type'] = 'wpinv_note'; |
45 | 45 | } else { |
46 | 46 | |
47 | - if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
47 | + if (empty($query->query_vars['type__not_in'])) { |
|
48 | 48 | $query->query_vars['type__not_in'] = array(); |
49 | 49 | } |
50 | 50 | |
51 | - $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
52 | - $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
51 | + $query->query_vars['type__not_in'] = wpinv_parse_list($query->query_vars['type__not_in']); |
|
52 | + $query->query_vars['type__not_in'] = array_merge(array('wpinv_note'), $query->query_vars['type__not_in']); |
|
53 | 53 | } |
54 | 54 | |
55 | 55 | return $query; |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | /** |
59 | 59 | * Exclude notes from the comments feed. |
60 | 60 | */ |
61 | - function wpinv_comment_feed_where( $where ) { |
|
62 | - return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
61 | + function wpinv_comment_feed_where($where) { |
|
62 | + return $where . ($where ? ' AND ' : '') . " comment_type != 'wpinv_note' "; |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * is called. |
70 | 70 | */ |
71 | 71 | public function delete_comments_count_cache() { |
72 | - delete_transient( 'getpaid_count_comments' ); |
|
72 | + delete_transient('getpaid_count_comments'); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |
@@ -80,13 +80,13 @@ discard block |
||
80 | 80 | * @param int $post_id Post ID. |
81 | 81 | * @return object |
82 | 82 | */ |
83 | - public function wp_count_comments( $stats, $post_id ) { |
|
83 | + public function wp_count_comments($stats, $post_id) { |
|
84 | 84 | global $wpdb; |
85 | 85 | |
86 | - if ( empty( $post_id ) ) { |
|
87 | - $stats = get_transient( 'getpaid_count_comments' ); |
|
86 | + if (empty($post_id)) { |
|
87 | + $stats = get_transient('getpaid_count_comments'); |
|
88 | 88 | |
89 | - if ( ! $stats ) { |
|
89 | + if (!$stats) { |
|
90 | 90 | $stats = array( |
91 | 91 | 'total_comments' => 0, |
92 | 92 | 'all' => 0, |
@@ -110,27 +110,27 @@ discard block |
||
110 | 110 | 'post-trashed' => 'post-trashed', |
111 | 111 | ); |
112 | 112 | |
113 | - foreach ( (array) $count as $row ) { |
|
113 | + foreach ((array) $count as $row) { |
|
114 | 114 | // Don't count post-trashed toward totals. |
115 | - if ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash', 'spam' ), true ) ) { |
|
115 | + if (!in_array($row['comment_approved'], array('post-trashed', 'trash', 'spam'), true)) { |
|
116 | 116 | $stats['all'] += $row['num_comments']; |
117 | 117 | $stats['total_comments'] += $row['num_comments']; |
118 | - } elseif ( ! in_array( $row['comment_approved'], array( 'post-trashed', 'trash' ), true ) ) { |
|
118 | + } elseif (!in_array($row['comment_approved'], array('post-trashed', 'trash'), true)) { |
|
119 | 119 | $stats['total_comments'] += $row['num_comments']; |
120 | 120 | } |
121 | - if ( isset( $approved[ $row['comment_approved'] ] ) ) { |
|
122 | - $stats[ $approved[ $row['comment_approved'] ] ] = $row['num_comments']; |
|
121 | + if (isset($approved[$row['comment_approved']])) { |
|
122 | + $stats[$approved[$row['comment_approved']]] = $row['num_comments']; |
|
123 | 123 | } |
124 | 124 | } |
125 | 125 | |
126 | - foreach ( $approved as $key ) { |
|
127 | - if ( empty( $stats[ $key ] ) ) { |
|
128 | - $stats[ $key ] = 0; |
|
126 | + foreach ($approved as $key) { |
|
127 | + if (empty($stats[$key])) { |
|
128 | + $stats[$key] = 0; |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | 132 | $stats = (object) $stats; |
133 | - set_transient( 'getpaid_count_comments', $stats ); |
|
133 | + set_transient('getpaid_count_comments', $stats); |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * @param string $type Optional. Pass in customer to only return customer notes. |
145 | 145 | * @return WP_Comment[] |
146 | 146 | */ |
147 | - public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
147 | + public function get_invoice_notes($invoice_id = 0, $type = 'all') { |
|
148 | 148 | |
149 | 149 | // Default comment args. |
150 | 150 | $args = array( |
@@ -154,14 +154,14 @@ discard block |
||
154 | 154 | ); |
155 | 155 | |
156 | 156 | // Maybe only show customer comments. |
157 | - if ( $type == 'customer' ) { |
|
157 | + if ($type == 'customer') { |
|
158 | 158 | $args['meta_key'] = '_wpi_customer_note'; |
159 | 159 | $args['meta_value'] = 1; |
160 | 160 | } |
161 | 161 | |
162 | - $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
162 | + $args = apply_filters('wpinv_invoice_notes_args', $args, $this, $invoice_id, $type); |
|
163 | 163 | |
164 | - return get_comments( $args ); |
|
164 | + return get_comments($args); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
@@ -173,9 +173,9 @@ discard block |
||
173 | 173 | * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
174 | 174 | * @return int|false The new note's ID on success, false on failure. |
175 | 175 | */ |
176 | - function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ) { |
|
176 | + function add_invoice_note($invoice, $note, $note_author, $author_email, $for_customer = false) { |
|
177 | 177 | |
178 | - do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
178 | + do_action('wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer); |
|
179 | 179 | |
180 | 180 | /** |
181 | 181 | * Insert the comment. |
@@ -196,14 +196,14 @@ discard block |
||
196 | 196 | ) |
197 | 197 | ); |
198 | 198 | |
199 | - do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
199 | + do_action('wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer); |
|
200 | 200 | |
201 | 201 | // Are we notifying the customer? |
202 | - if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
202 | + if (empty($note_id) || empty($for_customer)) { |
|
203 | 203 | return $note_id; |
204 | 204 | } |
205 | 205 | |
206 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
206 | + add_comment_meta($note_id, '_wpi_customer_note', 1); |
|
207 | 207 | do_action( |
208 | 208 | 'wpinv_new_customer_note', |
209 | 209 | array( |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | 'user_note' => $note, |
212 | 212 | ) |
213 | 213 | ); |
214 | - do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
214 | + do_action('getpaid_new_customer_note', $invoice, $note); |
|
215 | 215 | return $note_id; |
216 | 216 | } |
217 | 217 |
@@ -15,31 +15,31 @@ discard block |
||
15 | 15 | abstract class GetPaid_Authorize_Net_Legacy_Gateway extends GetPaid_Payment_Gateway { |
16 | 16 | |
17 | 17 | /** |
18 | - * Class constructor. |
|
19 | - */ |
|
20 | - public function __construct() { |
|
18 | + * Class constructor. |
|
19 | + */ |
|
20 | + public function __construct() { |
|
21 | 21 | parent::__construct(); |
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | - * Returns the API URL. |
|
26 | - * |
|
27 | - * |
|
28 | - * @param WPInv_Invoice $invoice Invoice. |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function get_api_url( $invoice ) { |
|
25 | + * Returns the API URL. |
|
26 | + * |
|
27 | + * |
|
28 | + * @param WPInv_Invoice $invoice Invoice. |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function get_api_url( $invoice ) { |
|
32 | 32 | return $this->is_sandbox( $invoice ) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
36 | - * Communicates with authorize.net |
|
37 | - * |
|
38 | - * |
|
39 | - * @param array $post Data to post. |
|
36 | + * Communicates with authorize.net |
|
37 | + * |
|
38 | + * |
|
39 | + * @param array $post Data to post. |
|
40 | 40 | * @param WPInv_Invoice $invoice Invoice. |
41 | - * @return stdClass|WP_Error |
|
42 | - */ |
|
41 | + * @return stdClass|WP_Error |
|
42 | + */ |
|
43 | 43 | public function post( $post, $invoice ) { |
44 | 44 | |
45 | 45 | $url = $this->get_api_url( $invoice ); |
@@ -89,12 +89,12 @@ discard block |
||
89 | 89 | } |
90 | 90 | |
91 | 91 | /** |
92 | - * Returns the API authentication params. |
|
93 | - * |
|
94 | - * |
|
95 | - * @return array |
|
96 | - */ |
|
97 | - public function get_auth_params() { |
|
92 | + * Returns the API authentication params. |
|
93 | + * |
|
94 | + * |
|
95 | + * @return array |
|
96 | + */ |
|
97 | + public function get_auth_params() { |
|
98 | 98 | |
99 | 99 | return array( |
100 | 100 | 'name' => $this->get_option( 'login_id' ), |
@@ -104,13 +104,13 @@ discard block |
||
104 | 104 | } |
105 | 105 | |
106 | 106 | /** |
107 | - * Cancels a subscription remotely |
|
108 | - * |
|
109 | - * |
|
110 | - * @param WPInv_Subscription $subscription Subscription. |
|
107 | + * Cancels a subscription remotely |
|
108 | + * |
|
109 | + * |
|
110 | + * @param WPInv_Subscription $subscription Subscription. |
|
111 | 111 | * @param WPInv_Invoice $invoice Invoice. |
112 | - */ |
|
113 | - public function cancel_subscription( $subscription, $invoice ) { |
|
112 | + */ |
|
113 | + public function cancel_subscription( $subscription, $invoice ) { |
|
114 | 114 | |
115 | 115 | // Backwards compatibility. New version do not use authorize.net subscriptions. |
116 | 116 | $this->post( |
@@ -126,17 +126,17 @@ discard block |
||
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
129 | - * Processes ipns. |
|
130 | - * |
|
131 | - * @return void |
|
132 | - */ |
|
133 | - public function verify_ipn() { |
|
129 | + * Processes ipns. |
|
130 | + * |
|
131 | + * @return void |
|
132 | + */ |
|
133 | + public function verify_ipn() { |
|
134 | 134 | |
135 | 135 | $this->maybe_process_old_ipn(); |
136 | 136 | |
137 | 137 | // Validate the IPN. |
138 | 138 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
139 | - wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 200 ) ); |
|
139 | + wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 200 ) ); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | // Event type. |
@@ -175,24 +175,24 @@ discard block |
||
175 | 175 | } |
176 | 176 | |
177 | 177 | /** |
178 | - * Validates IPN invoices. |
|
179 | - * |
|
178 | + * Validates IPN invoices. |
|
179 | + * |
|
180 | 180 | * @param WPInv_Invoice $invoice |
181 | 181 | * @param object $payload |
182 | - * @return void |
|
183 | - */ |
|
184 | - public function validate_ipn_invoice( $invoice, $payload ) { |
|
182 | + * @return void |
|
183 | + */ |
|
184 | + public function validate_ipn_invoice( $invoice, $payload ) { |
|
185 | 185 | if ( ! $invoice->exists() || $payload->id != $invoice->get_transaction_id() ) { |
186 | 186 | exit; |
187 | 187 | } |
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
191 | - * Process subscriptio IPNS. |
|
192 | - * |
|
193 | - * @return void |
|
194 | - */ |
|
195 | - public function maybe_process_old_ipn() { |
|
191 | + * Process subscriptio IPNS. |
|
192 | + * |
|
193 | + * @return void |
|
194 | + */ |
|
195 | + public function maybe_process_old_ipn() { |
|
196 | 196 | |
197 | 197 | $data = wp_kses_post_deep( wp_unslash( $_POST ) ); |
198 | 198 | |
@@ -234,11 +234,11 @@ discard block |
||
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
237 | - * Validates the old IPN signature. |
|
237 | + * Validates the old IPN signature. |
|
238 | 238 | * |
239 | 239 | * @param array $posted |
240 | - */ |
|
241 | - public function validate_old_ipn_signature( $posted ) { |
|
240 | + */ |
|
241 | + public function validate_old_ipn_signature( $posted ) { |
|
242 | 242 | |
243 | 243 | $signature = $this->get_option( 'signature_key' ); |
244 | 244 | if ( ! empty( $signature ) ) { |
@@ -256,9 +256,9 @@ discard block |
||
256 | 256 | } |
257 | 257 | |
258 | 258 | /** |
259 | - * Check Authorize.NET IPN validity. |
|
260 | - */ |
|
261 | - public function validate_ipn() { |
|
259 | + * Check Authorize.NET IPN validity. |
|
260 | + */ |
|
261 | + public function validate_ipn() { |
|
262 | 262 | |
263 | 263 | wpinv_error_log( 'Validating Authorize.NET IPN response' ); |
264 | 264 |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Authorize.net Legacy Payment Gateway class. |
@@ -28,8 +28,8 @@ discard block |
||
28 | 28 | * @param WPInv_Invoice $invoice Invoice. |
29 | 29 | * @return string |
30 | 30 | */ |
31 | - public function get_api_url( $invoice ) { |
|
32 | - return $this->is_sandbox( $invoice ) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
|
31 | + public function get_api_url($invoice) { |
|
32 | + return $this->is_sandbox($invoice) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
@@ -40,48 +40,48 @@ discard block |
||
40 | 40 | * @param WPInv_Invoice $invoice Invoice. |
41 | 41 | * @return stdClass|WP_Error |
42 | 42 | */ |
43 | - public function post( $post, $invoice ) { |
|
43 | + public function post($post, $invoice) { |
|
44 | 44 | |
45 | - $url = $this->get_api_url( $invoice ); |
|
45 | + $url = $this->get_api_url($invoice); |
|
46 | 46 | $response = wp_remote_post( |
47 | 47 | $url, |
48 | 48 | array( |
49 | 49 | 'headers' => array( |
50 | 50 | 'Content-Type' => 'application/json; charset=utf-8', |
51 | 51 | ), |
52 | - 'body' => json_encode( $post ), |
|
52 | + 'body' => json_encode($post), |
|
53 | 53 | 'method' => 'POST', |
54 | 54 | ) |
55 | 55 | ); |
56 | 56 | |
57 | - if ( is_wp_error( $response ) ) { |
|
57 | + if (is_wp_error($response)) { |
|
58 | 58 | return $response; |
59 | 59 | } |
60 | 60 | |
61 | - $response = wp_unslash( wp_remote_retrieve_body( $response ) ); |
|
62 | - $response = preg_replace( '/\xEF\xBB\xBF/', '', $response ); // https://community.developer.authorize.net/t5/Integration-and-Testing/JSON-issues/td-p/48851 |
|
63 | - $response = json_decode( $response ); |
|
61 | + $response = wp_unslash(wp_remote_retrieve_body($response)); |
|
62 | + $response = preg_replace('/\xEF\xBB\xBF/', '', $response); // https://community.developer.authorize.net/t5/Integration-and-Testing/JSON-issues/td-p/48851 |
|
63 | + $response = json_decode($response); |
|
64 | 64 | |
65 | - if ( empty( $response ) ) { |
|
66 | - return new WP_Error( 'invalid_reponse', __( 'Invalid gateway response', 'invoicing' ) ); |
|
65 | + if (empty($response)) { |
|
66 | + return new WP_Error('invalid_reponse', __('Invalid gateway response', 'invoicing')); |
|
67 | 67 | } |
68 | 68 | |
69 | - if ( $response->messages->resultCode == 'Error' ) { |
|
69 | + if ($response->messages->resultCode == 'Error') { |
|
70 | 70 | |
71 | - if ( $this->is_sandbox( $invoice ) ) { |
|
72 | - wpinv_error_log( $response ); |
|
71 | + if ($this->is_sandbox($invoice)) { |
|
72 | + wpinv_error_log($response); |
|
73 | 73 | } |
74 | 74 | |
75 | - if ( $response->messages->message[0]->code == 'E00039' && ! empty( $response->customerProfileId ) && ! empty( $response->customerPaymentProfileId ) ) { |
|
76 | - return new WP_Error( 'dup_payment_profile', $response->customerProfileId . '.' . $response->customerPaymentProfileId ); |
|
75 | + if ($response->messages->message[0]->code == 'E00039' && !empty($response->customerProfileId) && !empty($response->customerPaymentProfileId)) { |
|
76 | + return new WP_Error('dup_payment_profile', $response->customerProfileId . '.' . $response->customerPaymentProfileId); |
|
77 | 77 | } |
78 | 78 | |
79 | - if ( ! empty( $response->transactionResponse ) && ! empty( $response->transactionResponse->errors ) ) { |
|
79 | + if (!empty($response->transactionResponse) && !empty($response->transactionResponse->errors)) { |
|
80 | 80 | $error = $response->transactionResponse->errors[0]; |
81 | - return new WP_Error( $error->errorCode, $error->errorText ); |
|
81 | + return new WP_Error($error->errorCode, $error->errorText); |
|
82 | 82 | } |
83 | 83 | |
84 | - return new WP_Error( $response->messages->message[0]->code, $response->messages->message[0]->text ); |
|
84 | + return new WP_Error($response->messages->message[0]->code, $response->messages->message[0]->text); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | return $response; |
@@ -97,8 +97,8 @@ discard block |
||
97 | 97 | public function get_auth_params() { |
98 | 98 | |
99 | 99 | return array( |
100 | - 'name' => $this->get_option( 'login_id' ), |
|
101 | - 'transactionKey' => $this->get_option( 'transaction_key' ), |
|
100 | + 'name' => $this->get_option('login_id'), |
|
101 | + 'transactionKey' => $this->get_option('transaction_key'), |
|
102 | 102 | ); |
103 | 103 | |
104 | 104 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param WPInv_Subscription $subscription Subscription. |
111 | 111 | * @param WPInv_Invoice $invoice Invoice. |
112 | 112 | */ |
113 | - public function cancel_subscription( $subscription, $invoice ) { |
|
113 | + public function cancel_subscription($subscription, $invoice) { |
|
114 | 114 | |
115 | 115 | // Backwards compatibility. New version do not use authorize.net subscriptions. |
116 | 116 | $this->post( |
@@ -135,38 +135,38 @@ discard block |
||
135 | 135 | $this->maybe_process_old_ipn(); |
136 | 136 | |
137 | 137 | // Validate the IPN. |
138 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
139 | - wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 200 ) ); |
|
138 | + if (empty($_POST) || !$this->validate_ipn()) { |
|
139 | + wp_die('Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array('response' => 200)); |
|
140 | 140 | } |
141 | 141 | |
142 | 142 | // Event type. |
143 | - $posted = json_decode( file_get_contents( 'php://input' ) ); |
|
144 | - if ( empty( $posted ) ) { |
|
145 | - wp_die( 'Invalid JSON', 'Authorize.NET IPN', array( 'response' => 200 ) ); |
|
143 | + $posted = json_decode(file_get_contents('php://input')); |
|
144 | + if (empty($posted)) { |
|
145 | + wp_die('Invalid JSON', 'Authorize.NET IPN', array('response' => 200)); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | // Process the IPN. |
149 | - $posted = (object) wp_unslash( $posted ); |
|
149 | + $posted = (object) wp_unslash($posted); |
|
150 | 150 | |
151 | 151 | // Process refunds. |
152 | - if ( 'net.authorize.payment.refund.created' == $posted->eventType ) { |
|
153 | - $invoice = new WPInv_Invoice( $posted->payload->merchantReferenceId ); |
|
154 | - $this->validate_ipn_invoice( $invoice, $posted->payload ); |
|
152 | + if ('net.authorize.payment.refund.created' == $posted->eventType) { |
|
153 | + $invoice = new WPInv_Invoice($posted->payload->merchantReferenceId); |
|
154 | + $this->validate_ipn_invoice($invoice, $posted->payload); |
|
155 | 155 | $invoice->refund(); |
156 | 156 | } |
157 | 157 | |
158 | 158 | // Held funds approved. |
159 | - if ( 'net.authorize.payment.fraud.approved' == $posted->eventType ) { |
|
160 | - $invoice = new WPInv_Invoice( $posted->payload->id ); |
|
161 | - $this->validate_ipn_invoice( $invoice, $posted->payload ); |
|
162 | - $invoice->mark_paid( false, __( 'Payment released', 'invoicing' ) ); |
|
159 | + if ('net.authorize.payment.fraud.approved' == $posted->eventType) { |
|
160 | + $invoice = new WPInv_Invoice($posted->payload->id); |
|
161 | + $this->validate_ipn_invoice($invoice, $posted->payload); |
|
162 | + $invoice->mark_paid(false, __('Payment released', 'invoicing')); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | // Held funds declined. |
166 | - if ( 'net.authorize.payment.fraud.declined' == $posted->eventType ) { |
|
167 | - $invoice = new WPInv_Invoice( $posted->payload->id ); |
|
168 | - $this->validate_ipn_invoice( $invoice, $posted->payload ); |
|
169 | - $invoice->set_status( 'wpi-failed', __( 'Payment declined', 'invoicing' ) ); |
|
166 | + if ('net.authorize.payment.fraud.declined' == $posted->eventType) { |
|
167 | + $invoice = new WPInv_Invoice($posted->payload->id); |
|
168 | + $this->validate_ipn_invoice($invoice, $posted->payload); |
|
169 | + $invoice->set_status('wpi-failed', __('Payment declined', 'invoicing')); |
|
170 | 170 | $invoice->save(); |
171 | 171 | } |
172 | 172 | |
@@ -181,8 +181,8 @@ discard block |
||
181 | 181 | * @param object $payload |
182 | 182 | * @return void |
183 | 183 | */ |
184 | - public function validate_ipn_invoice( $invoice, $payload ) { |
|
185 | - if ( ! $invoice->exists() || $payload->id != $invoice->get_transaction_id() ) { |
|
184 | + public function validate_ipn_invoice($invoice, $payload) { |
|
185 | + if (!$invoice->exists() || $payload->id != $invoice->get_transaction_id()) { |
|
186 | 186 | exit; |
187 | 187 | } |
188 | 188 | } |
@@ -194,32 +194,32 @@ discard block |
||
194 | 194 | */ |
195 | 195 | public function maybe_process_old_ipn() { |
196 | 196 | |
197 | - $data = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
197 | + $data = wp_kses_post_deep(wp_unslash($_POST)); |
|
198 | 198 | |
199 | 199 | // Only process subscriptions subscriptions. |
200 | - if ( empty( $data['x_subscription_id'] ) ) { |
|
200 | + if (empty($data['x_subscription_id'])) { |
|
201 | 201 | return; |
202 | 202 | } |
203 | 203 | |
204 | 204 | // Check validity. |
205 | - $this->validate_old_ipn_signature( $data ); |
|
205 | + $this->validate_old_ipn_signature($data); |
|
206 | 206 | |
207 | 207 | // Fetch the associated subscription. |
208 | - $subscription_id = WPInv_Subscription::get_subscription_id_by_field( $data['x_subscription_id'] ); |
|
209 | - $subscription = new WPInv_Subscription( $subscription_id ); |
|
208 | + $subscription_id = WPInv_Subscription::get_subscription_id_by_field($data['x_subscription_id']); |
|
209 | + $subscription = new WPInv_Subscription($subscription_id); |
|
210 | 210 | |
211 | 211 | // Abort if it is missing or completed. |
212 | - if ( ! $subscription->get_id() || $subscription->has_status( 'completed' ) ) { |
|
212 | + if (!$subscription->get_id() || $subscription->has_status('completed')) { |
|
213 | 213 | return; |
214 | 214 | } |
215 | 215 | |
216 | 216 | // Payment status. |
217 | - if ( 1 == $data['x_response_code'] ) { |
|
217 | + if (1 == $data['x_response_code']) { |
|
218 | 218 | |
219 | 219 | // Renew the subscription. |
220 | 220 | $subscription->add_payment( |
221 | 221 | array( |
222 | - 'transaction_id' => sanitize_text_field( $data['x_trans_id'] ), |
|
222 | + 'transaction_id' => sanitize_text_field($data['x_trans_id']), |
|
223 | 223 | 'gateway' => $this->id, |
224 | 224 | ) |
225 | 225 | ); |
@@ -238,17 +238,17 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @param array $posted |
240 | 240 | */ |
241 | - public function validate_old_ipn_signature( $posted ) { |
|
241 | + public function validate_old_ipn_signature($posted) { |
|
242 | 242 | |
243 | - $signature = $this->get_option( 'signature_key' ); |
|
244 | - if ( ! empty( $signature ) ) { |
|
245 | - $login_id = $this->get_option( 'login_id' ); |
|
246 | - $trans_id = wpinv_clean( $_POST['x_trans_id'] ); |
|
247 | - $amount = wpinv_clean( $_POST['x_amount'] ); |
|
248 | - $hash = hash_hmac( 'sha512', "^$login_id^$trans_id^$amount^", hex2bin( $signature ) ); |
|
243 | + $signature = $this->get_option('signature_key'); |
|
244 | + if (!empty($signature)) { |
|
245 | + $login_id = $this->get_option('login_id'); |
|
246 | + $trans_id = wpinv_clean($_POST['x_trans_id']); |
|
247 | + $amount = wpinv_clean($_POST['x_amount']); |
|
248 | + $hash = hash_hmac('sha512', "^$login_id^$trans_id^$amount^", hex2bin($signature)); |
|
249 | 249 | |
250 | - if ( ! hash_equals( $hash, $posted['x_SHA2_Hash'] ) ) { |
|
251 | - wpinv_error_log( $posted['x_SHA2_Hash'], "Invalid signature. Expected $hash" ); |
|
250 | + if (!hash_equals($hash, $posted['x_SHA2_Hash'])) { |
|
251 | + wpinv_error_log($posted['x_SHA2_Hash'], "Invalid signature. Expected $hash"); |
|
252 | 252 | exit; |
253 | 253 | } |
254 | 254 | } |
@@ -260,28 +260,28 @@ discard block |
||
260 | 260 | */ |
261 | 261 | public function validate_ipn() { |
262 | 262 | |
263 | - wpinv_error_log( 'Validating Authorize.NET IPN response' ); |
|
263 | + wpinv_error_log('Validating Authorize.NET IPN response'); |
|
264 | 264 | |
265 | - if ( empty( $_SERVER['HTTP_X_ANET_SIGNATURE'] ) ) { |
|
265 | + if (empty($_SERVER['HTTP_X_ANET_SIGNATURE'])) { |
|
266 | 266 | return false; |
267 | 267 | } |
268 | 268 | |
269 | - $signature = $this->get_option( 'signature_key' ); |
|
269 | + $signature = $this->get_option('signature_key'); |
|
270 | 270 | |
271 | - if ( empty( $signature ) ) { |
|
272 | - wpinv_error_log( 'Error: You have not set a signature key' ); |
|
271 | + if (empty($signature)) { |
|
272 | + wpinv_error_log('Error: You have not set a signature key'); |
|
273 | 273 | return false; |
274 | 274 | } |
275 | 275 | |
276 | - $hash = hash_hmac( 'sha512', file_get_contents( 'php://input' ), hex2bin( $signature ) ); |
|
276 | + $hash = hash_hmac('sha512', file_get_contents('php://input'), hex2bin($signature)); |
|
277 | 277 | |
278 | - if ( hash_equals( $hash, $_SERVER['HTTP_X_ANET_SIGNATURE'] ) ) { |
|
279 | - wpinv_error_log( 'Successfully validated the IPN' ); |
|
278 | + if (hash_equals($hash, $_SERVER['HTTP_X_ANET_SIGNATURE'])) { |
|
279 | + wpinv_error_log('Successfully validated the IPN'); |
|
280 | 280 | return true; |
281 | 281 | } |
282 | 282 | |
283 | - wpinv_error_log( 'IPN hash is not valid' ); |
|
284 | - wpinv_error_log( $_SERVER['HTTP_X_ANET_SIGNATURE'] ); |
|
283 | + wpinv_error_log('IPN hash is not valid'); |
|
284 | + wpinv_error_log($_SERVER['HTTP_X_ANET_SIGNATURE']); |
|
285 | 285 | return false; |
286 | 286 | |
287 | 287 | } |
@@ -13,65 +13,65 @@ discard block |
||
13 | 13 | class GetPaid_Worldpay_Gateway extends GetPaid_Payment_Gateway { |
14 | 14 | |
15 | 15 | /** |
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | 20 | public $id = 'worldpay'; |
21 | 21 | |
22 | 22 | /** |
23 | - * Payment method order. |
|
24 | - * |
|
25 | - * @var int |
|
26 | - */ |
|
23 | + * Payment method order. |
|
24 | + * |
|
25 | + * @var int |
|
26 | + */ |
|
27 | 27 | public $order = 5; |
28 | 28 | |
29 | 29 | /** |
30 | - * Endpoint for requests from Worldpay. |
|
31 | - * |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $notify_url; |
|
35 | - |
|
36 | - /** |
|
37 | - * Endpoint for requests to Worldpay. |
|
38 | - * |
|
39 | - * @var string |
|
40 | - */ |
|
30 | + * Endpoint for requests from Worldpay. |
|
31 | + * |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $notify_url; |
|
35 | + |
|
36 | + /** |
|
37 | + * Endpoint for requests to Worldpay. |
|
38 | + * |
|
39 | + * @var string |
|
40 | + */ |
|
41 | 41 | protected $endpoint; |
42 | 42 | |
43 | 43 | /** |
44 | - * An array of features that this gateway supports. |
|
45 | - * |
|
46 | - * @var array |
|
47 | - */ |
|
44 | + * An array of features that this gateway supports. |
|
45 | + * |
|
46 | + * @var array |
|
47 | + */ |
|
48 | 48 | protected $supports = array( 'sandbox' ); |
49 | 49 | |
50 | 50 | /** |
51 | - * Currencies this gateway is allowed for. |
|
52 | - * |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
51 | + * Currencies this gateway is allowed for. |
|
52 | + * |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
56 | 56 | |
57 | 57 | /** |
58 | - * URL to view a transaction. |
|
59 | - * |
|
60 | - * @var string |
|
61 | - */ |
|
58 | + * URL to view a transaction. |
|
59 | + * |
|
60 | + * @var string |
|
61 | + */ |
|
62 | 62 | public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s'; |
63 | 63 | |
64 | 64 | /** |
65 | - * URL to view a subscription. |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
65 | + * URL to view a subscription. |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
70 | 70 | |
71 | 71 | /** |
72 | - * Class constructor. |
|
73 | - */ |
|
74 | - public function __construct() { |
|
72 | + * Class constructor. |
|
73 | + */ |
|
74 | + public function __construct() { |
|
75 | 75 | |
76 | 76 | $this->method_title = __( 'Worldpay', 'invoicing' ); |
77 | 77 | $this->title = __( 'Worldpay - Credit Card / Debit Card', 'invoicing' ); |
@@ -85,15 +85,15 @@ discard block |
||
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | - * Process Payment. |
|
89 | - * |
|
90 | - * |
|
91 | - * @param WPInv_Invoice $invoice Invoice. |
|
92 | - * @param array $submission_data Posted checkout fields. |
|
93 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
88 | + * Process Payment. |
|
89 | + * |
|
90 | + * |
|
91 | + * @param WPInv_Invoice $invoice Invoice. |
|
92 | + * @param array $submission_data Posted checkout fields. |
|
93 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
97 | 97 | |
98 | 98 | // Get redirect url. |
99 | 99 | $worldpay_redirect = esc_url( $this->get_request_url( $invoice ) ); |
@@ -128,31 +128,31 @@ discard block |
||
128 | 128 | } |
129 | 129 | |
130 | 130 | /** |
131 | - * Get the Worldpay request URL for an invoice. |
|
132 | - * |
|
133 | - * @param WPInv_Invoice $invoice Invoice object. |
|
134 | - * @return string |
|
135 | - */ |
|
136 | - public function get_request_url( $invoice ) { |
|
131 | + * Get the Worldpay request URL for an invoice. |
|
132 | + * |
|
133 | + * @param WPInv_Invoice $invoice Invoice object. |
|
134 | + * @return string |
|
135 | + */ |
|
136 | + public function get_request_url( $invoice ) { |
|
137 | 137 | |
138 | 138 | // Endpoint for this request |
139 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
139 | + $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
140 | 140 | |
141 | 141 | return $this->endpoint; |
142 | 142 | |
143 | - } |
|
143 | + } |
|
144 | 144 | |
145 | 145 | /** |
146 | - * Get Worldpay Args for passing to Worldpay. |
|
147 | - * |
|
148 | - * @param WPInv_Invoice $invoice Invoice object. |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - protected function get_worldpay_args( $invoice ) { |
|
152 | - |
|
153 | - return apply_filters( |
|
154 | - 'getpaid_worldpay_args', |
|
155 | - array( |
|
146 | + * Get Worldpay Args for passing to Worldpay. |
|
147 | + * |
|
148 | + * @param WPInv_Invoice $invoice Invoice object. |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + protected function get_worldpay_args( $invoice ) { |
|
152 | + |
|
153 | + return apply_filters( |
|
154 | + 'getpaid_worldpay_args', |
|
155 | + array( |
|
156 | 156 | 'amount' => wpinv_sanitize_amount( $invoice->get_total() ), // mandatory |
157 | 157 | 'cartId' => wpinv_clean( $invoice->get_number() ), // mandatory reference for the item purchased |
158 | 158 | 'currency' => wpinv_clean( $invoice->get_currency() ), // mandatory |
@@ -177,18 +177,18 @@ discard block |
||
177 | 177 | 'countryString' => wpinv_clean( wpinv_country_name( $invoice->get_country() ) ), |
178 | 178 | 'compName' => wpinv_clean( $invoice->get_company() ), |
179 | 179 | ), |
180 | - $invoice |
|
181 | - ); |
|
180 | + $invoice |
|
181 | + ); |
|
182 | 182 | |
183 | 183 | } |
184 | 184 | |
185 | 185 | /** |
186 | - * Secures worldpay args with an md5 hash. |
|
187 | - * |
|
188 | - * @param array $args Gateway args. |
|
189 | - * @return array |
|
190 | - */ |
|
191 | - public function hash_args( $args ) { |
|
186 | + * Secures worldpay args with an md5 hash. |
|
187 | + * |
|
188 | + * @param array $args Gateway args. |
|
189 | + * @return array |
|
190 | + */ |
|
191 | + public function hash_args( $args ) { |
|
192 | 192 | |
193 | 193 | $md5_secret = $this->get_option( 'md5_secret' ); |
194 | 194 | |
@@ -204,16 +204,16 @@ discard block |
||
204 | 204 | } |
205 | 205 | |
206 | 206 | /** |
207 | - * Processes ipns and marks payments as complete. |
|
208 | - * |
|
209 | - * @return void |
|
210 | - */ |
|
211 | - public function verify_ipn() { |
|
207 | + * Processes ipns and marks payments as complete. |
|
208 | + * |
|
209 | + * @return void |
|
210 | + */ |
|
211 | + public function verify_ipn() { |
|
212 | 212 | |
213 | 213 | // Validate the IPN. |
214 | 214 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
215 | - wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
216 | - } |
|
215 | + wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
216 | + } |
|
217 | 217 | |
218 | 218 | // Process the IPN. |
219 | 219 | $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
@@ -229,8 +229,8 @@ discard block |
||
229 | 229 | $invoice->set_transaction_id( wpinv_clean( $posted['transId'] ) ); |
230 | 230 | } |
231 | 231 | |
232 | - // Update the ip address. |
|
233 | - if ( ! empty( $posted['ipAddress'] ) ) { |
|
232 | + // Update the ip address. |
|
233 | + if ( ! empty( $posted['ipAddress'] ) ) { |
|
234 | 234 | $invoice->set_ip( wpinv_clean( $posted['ipAddress'] ) ); |
235 | 235 | } |
236 | 236 | |
@@ -257,9 +257,9 @@ discard block |
||
257 | 257 | } |
258 | 258 | |
259 | 259 | /** |
260 | - * Check Worldpay IPN validity. |
|
261 | - */ |
|
262 | - public function validate_ipn() { |
|
260 | + * Check Worldpay IPN validity. |
|
261 | + */ |
|
262 | + public function validate_ipn() { |
|
263 | 263 | |
264 | 264 | wpinv_error_log( 'Validating Worldpay IPN response' ); |
265 | 265 | |
@@ -305,11 +305,11 @@ discard block |
||
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
308 | - * Filters the gateway settings. |
|
309 | - * |
|
310 | - * @param array $admin_settings |
|
311 | - */ |
|
312 | - public function admin_settings( $admin_settings ) { |
|
308 | + * Filters the gateway settings. |
|
309 | + * |
|
310 | + * @param array $admin_settings |
|
311 | + */ |
|
312 | + public function admin_settings( $admin_settings ) { |
|
313 | 313 | |
314 | 314 | $currencies = sprintf( |
315 | 315 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | 'readonly' => true, |
351 | 351 | ); |
352 | 352 | |
353 | - return $admin_settings; |
|
354 | - } |
|
353 | + return $admin_settings; |
|
354 | + } |
|
355 | 355 | |
356 | 356 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Worldpay Payment Gateway class. |
@@ -45,14 +45,14 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @var array |
47 | 47 | */ |
48 | - protected $supports = array( 'sandbox' ); |
|
48 | + protected $supports = array('sandbox'); |
|
49 | 49 | |
50 | 50 | /** |
51 | 51 | * Currencies this gateway is allowed for. |
52 | 52 | * |
53 | 53 | * @var array |
54 | 54 | */ |
55 | - public $currencies = array( 'AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR' ); |
|
55 | + public $currencies = array('AUD', 'ARS', 'CAD', 'CHF', 'DKK', 'EUR', 'HKD', 'MYR', 'GBP', 'NZD', 'NOK', 'SGD', 'LKR', 'SEK', 'TRY', 'USD', 'ZAR'); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * URL to view a transaction. |
@@ -73,13 +73,13 @@ discard block |
||
73 | 73 | */ |
74 | 74 | public function __construct() { |
75 | 75 | |
76 | - $this->method_title = __( 'Worldpay', 'invoicing' ); |
|
77 | - $this->title = __( 'Worldpay - Credit Card / Debit Card', 'invoicing' ); |
|
78 | - $this->checkout_button_text = __( 'Proceed to Worldpay', 'invoicing' ); |
|
79 | - $this->notify_url = wpinv_get_ipn_url( $this->id ); |
|
76 | + $this->method_title = __('Worldpay', 'invoicing'); |
|
77 | + $this->title = __('Worldpay - Credit Card / Debit Card', 'invoicing'); |
|
78 | + $this->checkout_button_text = __('Proceed to Worldpay', 'invoicing'); |
|
79 | + $this->notify_url = wpinv_get_ipn_url($this->id); |
|
80 | 80 | |
81 | - add_filter( 'wpinv_gateway_description', array( $this, 'sandbox_notice' ), 10, 2 ); |
|
82 | - add_filter( 'getpaid_worldpay_args', array( $this, 'hash_args' ) ); |
|
81 | + add_filter('wpinv_gateway_description', array($this, 'sandbox_notice'), 10, 2); |
|
82 | + add_filter('getpaid_worldpay_args', array($this, 'hash_args')); |
|
83 | 83 | |
84 | 84 | parent::__construct(); |
85 | 85 | } |
@@ -93,24 +93,24 @@ discard block |
||
93 | 93 | * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
94 | 94 | * @return array |
95 | 95 | */ |
96 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
96 | + public function process_payment($invoice, $submission_data, $submission) { |
|
97 | 97 | |
98 | 98 | // Get redirect url. |
99 | - $worldpay_redirect = esc_url( $this->get_request_url( $invoice ) ); |
|
99 | + $worldpay_redirect = esc_url($this->get_request_url($invoice)); |
|
100 | 100 | |
101 | 101 | // Get submission args. |
102 | - $worldpay_args = $this->get_worldpay_args( $invoice ); |
|
102 | + $worldpay_args = $this->get_worldpay_args($invoice); |
|
103 | 103 | |
104 | 104 | $form = "<form action='$worldpay_redirect' name='wpi_worldpay_form' method='POST'>"; |
105 | 105 | |
106 | - foreach ( $worldpay_args as $key => $value ) { |
|
106 | + foreach ($worldpay_args as $key => $value) { |
|
107 | 107 | |
108 | - if ( false === $value || '' === trim( $value ) ) { |
|
108 | + if (false === $value || '' === trim($value)) { |
|
109 | 109 | continue; |
110 | 110 | } |
111 | 111 | |
112 | - $value = esc_attr( $value ); |
|
113 | - $key = wpinv_clean( $key ); |
|
112 | + $value = esc_attr($value); |
|
113 | + $key = wpinv_clean($key); |
|
114 | 114 | $form .= "<input type='hidden' name='$key' value='$value'>"; |
115 | 115 | } |
116 | 116 | |
@@ -133,10 +133,10 @@ discard block |
||
133 | 133 | * @param WPInv_Invoice $invoice Invoice object. |
134 | 134 | * @return string |
135 | 135 | */ |
136 | - public function get_request_url( $invoice ) { |
|
136 | + public function get_request_url($invoice) { |
|
137 | 137 | |
138 | 138 | // Endpoint for this request |
139 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
139 | + $this->endpoint = $this->is_sandbox($invoice) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
140 | 140 | |
141 | 141 | return $this->endpoint; |
142 | 142 | |
@@ -148,34 +148,34 @@ discard block |
||
148 | 148 | * @param WPInv_Invoice $invoice Invoice object. |
149 | 149 | * @return array |
150 | 150 | */ |
151 | - protected function get_worldpay_args( $invoice ) { |
|
151 | + protected function get_worldpay_args($invoice) { |
|
152 | 152 | |
153 | 153 | return apply_filters( |
154 | 154 | 'getpaid_worldpay_args', |
155 | 155 | array( |
156 | - 'amount' => wpinv_sanitize_amount( $invoice->get_total() ), // mandatory |
|
157 | - 'cartId' => wpinv_clean( $invoice->get_number() ), // mandatory reference for the item purchased |
|
158 | - 'currency' => wpinv_clean( $invoice->get_currency() ), // mandatory |
|
159 | - 'instId' => wpinv_clean( $this->get_option( 'instId', '' ) ), // mandatory |
|
160 | - 'testMode' => $this->is_sandbox( $invoice ) ? 100 : 0, // mandatory |
|
161 | - 'name' => wpinv_clean( $invoice->get_full_name() ), |
|
162 | - 'address' => wpinv_clean( $invoice->get_address() ), |
|
163 | - 'postcode' => wpinv_clean( $invoice->get_zip() ), |
|
164 | - 'tel' => wpinv_clean( $invoice->get_phone() ), |
|
165 | - 'email' => sanitize_email( $invoice->get_email() ), |
|
166 | - 'country' => wpinv_clean( $invoice->get_country() ), |
|
167 | - 'desc' => sprintf( __( 'Payment for invoice %s.', 'invoicing' ), wpinv_clean( $invoice->get_number() ) ), |
|
168 | - 'MC_description' => sprintf( __( 'Payment for invoice %s.', 'invoicing' ), wpinv_clean( $invoice->get_number() ) ), |
|
169 | - 'MC_callback' => esc_url_raw( $this->notify_url ), |
|
170 | - 'resultfile' => esc_url_raw( $this->get_return_url( $invoice ) ), |
|
171 | - 'MC_key' => wpinv_clean( $invoice->get_key() ), |
|
156 | + 'amount' => wpinv_sanitize_amount($invoice->get_total()), // mandatory |
|
157 | + 'cartId' => wpinv_clean($invoice->get_number()), // mandatory reference for the item purchased |
|
158 | + 'currency' => wpinv_clean($invoice->get_currency()), // mandatory |
|
159 | + 'instId' => wpinv_clean($this->get_option('instId', '')), // mandatory |
|
160 | + 'testMode' => $this->is_sandbox($invoice) ? 100 : 0, // mandatory |
|
161 | + 'name' => wpinv_clean($invoice->get_full_name()), |
|
162 | + 'address' => wpinv_clean($invoice->get_address()), |
|
163 | + 'postcode' => wpinv_clean($invoice->get_zip()), |
|
164 | + 'tel' => wpinv_clean($invoice->get_phone()), |
|
165 | + 'email' => sanitize_email($invoice->get_email()), |
|
166 | + 'country' => wpinv_clean($invoice->get_country()), |
|
167 | + 'desc' => sprintf(__('Payment for invoice %s.', 'invoicing'), wpinv_clean($invoice->get_number())), |
|
168 | + 'MC_description' => sprintf(__('Payment for invoice %s.', 'invoicing'), wpinv_clean($invoice->get_number())), |
|
169 | + 'MC_callback' => esc_url_raw($this->notify_url), |
|
170 | + 'resultfile' => esc_url_raw($this->get_return_url($invoice)), |
|
171 | + 'MC_key' => wpinv_clean($invoice->get_key()), |
|
172 | 172 | 'MC_invoice_id' => $invoice->get_id(), |
173 | - 'address1' => wpinv_clean( $invoice->get_address() ), |
|
174 | - 'town' => wpinv_clean( $invoice->get_city() ), |
|
175 | - 'region' => wpinv_clean( $invoice->get_state() ), |
|
176 | - 'amountString' => wpinv_price( $invoice->get_total(), $invoice->get_currency() ), |
|
177 | - 'countryString' => wpinv_clean( wpinv_country_name( $invoice->get_country() ) ), |
|
178 | - 'compName' => wpinv_clean( $invoice->get_company() ), |
|
173 | + 'address1' => wpinv_clean($invoice->get_address()), |
|
174 | + 'town' => wpinv_clean($invoice->get_city()), |
|
175 | + 'region' => wpinv_clean($invoice->get_state()), |
|
176 | + 'amountString' => wpinv_price($invoice->get_total(), $invoice->get_currency()), |
|
177 | + 'countryString' => wpinv_clean(wpinv_country_name($invoice->get_country())), |
|
178 | + 'compName' => wpinv_clean($invoice->get_company()), |
|
179 | 179 | ), |
180 | 180 | $invoice |
181 | 181 | ); |
@@ -188,17 +188,17 @@ discard block |
||
188 | 188 | * @param array $args Gateway args. |
189 | 189 | * @return array |
190 | 190 | */ |
191 | - public function hash_args( $args ) { |
|
191 | + public function hash_args($args) { |
|
192 | 192 | |
193 | - $md5_secret = $this->get_option( 'md5_secret' ); |
|
193 | + $md5_secret = $this->get_option('md5_secret'); |
|
194 | 194 | |
195 | 195 | // Abort if there is no secret. |
196 | - if ( empty( $md5_secret ) ) { |
|
196 | + if (empty($md5_secret)) { |
|
197 | 197 | return $args; |
198 | 198 | } |
199 | 199 | |
200 | 200 | // Hash the args. |
201 | - $args['signature'] = md5( "$md5_secret:{$args['instId']}:{$args['amount']}:{$args['currency']}:{$args['cartId']}" ); |
|
201 | + $args['signature'] = md5("$md5_secret:{$args['instId']}:{$args['amount']}:{$args['currency']}:{$args['cartId']}"); |
|
202 | 202 | |
203 | 203 | return $args; |
204 | 204 | } |
@@ -211,43 +211,43 @@ discard block |
||
211 | 211 | public function verify_ipn() { |
212 | 212 | |
213 | 213 | // Validate the IPN. |
214 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
215 | - wp_die( 'Worldpay IPN Request Failure', 'Worldpay IPN', array( 'response' => 500 ) ); |
|
214 | + if (empty($_POST) || !$this->validate_ipn()) { |
|
215 | + wp_die('Worldpay IPN Request Failure', 'Worldpay IPN', array('response' => 500)); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | // Process the IPN. |
219 | - $posted = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
220 | - $invoice = wpinv_get_invoice( $posted['MC_invoice_id'] ); |
|
219 | + $posted = wp_kses_post_deep(wp_unslash($_POST)); |
|
220 | + $invoice = wpinv_get_invoice($posted['MC_invoice_id']); |
|
221 | 221 | |
222 | - if ( $invoice && $this->id == $invoice->get_gateway() ) { |
|
222 | + if ($invoice && $this->id == $invoice->get_gateway()) { |
|
223 | 223 | |
224 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number() ); |
|
225 | - wpinv_error_log( 'Payment status:' . $posted['transStatus'] ); |
|
224 | + wpinv_error_log('Found invoice #' . $invoice->get_number()); |
|
225 | + wpinv_error_log('Payment status:' . $posted['transStatus']); |
|
226 | 226 | |
227 | 227 | // Update the transaction id. |
228 | - if ( ! empty( $posted['transId'] ) ) { |
|
229 | - $invoice->set_transaction_id( wpinv_clean( $posted['transId'] ) ); |
|
228 | + if (!empty($posted['transId'])) { |
|
229 | + $invoice->set_transaction_id(wpinv_clean($posted['transId'])); |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | // Update the ip address. |
233 | - if ( ! empty( $posted['ipAddress'] ) ) { |
|
234 | - $invoice->set_ip( wpinv_clean( $posted['ipAddress'] ) ); |
|
233 | + if (!empty($posted['ipAddress'])) { |
|
234 | + $invoice->set_ip(wpinv_clean($posted['ipAddress'])); |
|
235 | 235 | } |
236 | 236 | |
237 | - if ( $posted['transStatus'] == 'Y' ) { |
|
238 | - $invoice->set_completed_date( date( 'Y-m-d H:i:s', $posted['transTime'] ) ); |
|
237 | + if ($posted['transStatus'] == 'Y') { |
|
238 | + $invoice->set_completed_date(date('Y-m-d H:i:s', $posted['transTime'])); |
|
239 | 239 | $invoice->mark_paid(); |
240 | 240 | return; |
241 | 241 | } |
242 | 242 | |
243 | - if ( $posted['transStatus'] == 'C' ) { |
|
244 | - $invoice->set_status( 'wpi-failed' ); |
|
245 | - $invoice->add_note( __( 'Payment transaction failed while processing Worldpay payment.', 'invoicing' ), false, false, true ); |
|
243 | + if ($posted['transStatus'] == 'C') { |
|
244 | + $invoice->set_status('wpi-failed'); |
|
245 | + $invoice->add_note(__('Payment transaction failed while processing Worldpay payment.', 'invoicing'), false, false, true); |
|
246 | 246 | $invoice->save(); |
247 | 247 | return; |
248 | 248 | } |
249 | 249 | |
250 | - wpinv_error_log( 'Aborting, Invalid transaction status:' . $posted['transStatus'] ); |
|
250 | + wpinv_error_log('Aborting, Invalid transaction status:' . $posted['transStatus']); |
|
251 | 251 | $invoice->save(); |
252 | 252 | |
253 | 253 | } |
@@ -261,27 +261,27 @@ discard block |
||
261 | 261 | */ |
262 | 262 | public function validate_ipn() { |
263 | 263 | |
264 | - wpinv_error_log( 'Validating Worldpay IPN response' ); |
|
264 | + wpinv_error_log('Validating Worldpay IPN response'); |
|
265 | 265 | |
266 | - $data = wp_kses_post_deep( wp_unslash( $_POST ) ); |
|
266 | + $data = wp_kses_post_deep(wp_unslash($_POST)); |
|
267 | 267 | |
268 | 268 | // Verify installation. |
269 | - if ( empty( $data['instId'] ) || $data['instId'] != wpinv_clean( $this->get_option( 'instId', '' ) ) ) { |
|
270 | - wpinv_error_log( 'Received invalid installation ID from Worldpay IPN' ); |
|
269 | + if (empty($data['instId']) || $data['instId'] != wpinv_clean($this->get_option('instId', ''))) { |
|
270 | + wpinv_error_log('Received invalid installation ID from Worldpay IPN'); |
|
271 | 271 | return false; |
272 | 272 | } |
273 | 273 | |
274 | 274 | // Verify invoice. |
275 | - if ( empty( $data['cartId'] ) || ! wpinv_get_id_by_invoice_number( $data['cartId'] ) ) { |
|
276 | - wpinv_error_log( 'Received invalid invoice number from Worldpay IPN' ); |
|
275 | + if (empty($data['cartId']) || !wpinv_get_id_by_invoice_number($data['cartId'])) { |
|
276 | + wpinv_error_log('Received invalid invoice number from Worldpay IPN'); |
|
277 | 277 | return false; |
278 | 278 | } |
279 | 279 | |
280 | 280 | // (maybe) verify password. |
281 | - $password = $this->get_option( 'callback_password' ); |
|
281 | + $password = $this->get_option('callback_password'); |
|
282 | 282 | |
283 | - if ( ! empty( $password ) && ( empty( $data['callbackPW'] ) || $password != $data['callbackPW'] ) ) { |
|
284 | - wpinv_error_log( 'Received invalid invoice number from Worldpay IPN' ); |
|
283 | + if (!empty($password) && (empty($data['callbackPW']) || $password != $data['callbackPW'])) { |
|
284 | + wpinv_error_log('Received invalid invoice number from Worldpay IPN'); |
|
285 | 285 | return false; |
286 | 286 | } |
287 | 287 | |
@@ -292,10 +292,10 @@ discard block |
||
292 | 292 | /** |
293 | 293 | * Displays a notice on the checkout page if sandbox is enabled. |
294 | 294 | */ |
295 | - public function sandbox_notice( $description, $gateway ) { |
|
296 | - if ( 'worldpay' == $gateway && wpinv_is_test_mode( 'worldpay' ) ) { |
|
295 | + public function sandbox_notice($description, $gateway) { |
|
296 | + if ('worldpay' == $gateway && wpinv_is_test_mode('worldpay')) { |
|
297 | 297 | $description .= '<br>' . sprintf( |
298 | - __( 'SANDBOX ENABLED. See the %1$sWorldpay Sandbox Testing Guide%2$s for more details.', 'invoicing' ), |
|
298 | + __('SANDBOX ENABLED. See the %1$sWorldpay Sandbox Testing Guide%2$s for more details.', 'invoicing'), |
|
299 | 299 | '<a href="https://developer.worldpay.com/docs/wpg/directintegration/abouttesting">', |
300 | 300 | '</a>' |
301 | 301 | ); |
@@ -309,43 +309,43 @@ discard block |
||
309 | 309 | * |
310 | 310 | * @param array $admin_settings |
311 | 311 | */ |
312 | - public function admin_settings( $admin_settings ) { |
|
312 | + public function admin_settings($admin_settings) { |
|
313 | 313 | |
314 | 314 | $currencies = sprintf( |
315 | - __( 'Supported Currencies: %s', 'invoicing' ), |
|
316 | - implode( ', ', $this->currencies ) |
|
315 | + __('Supported Currencies: %s', 'invoicing'), |
|
316 | + implode(', ', $this->currencies) |
|
317 | 317 | ); |
318 | 318 | |
319 | 319 | $admin_settings['worldpay_active']['desc'] = $admin_settings['worldpay_active']['desc'] . " ($currencies)"; |
320 | - $admin_settings['worldpay_desc']['std'] = __( 'Pay securely via Worldpay using your PayPal account, credit or debit card.', 'invoicing' ); |
|
320 | + $admin_settings['worldpay_desc']['std'] = __('Pay securely via Worldpay using your PayPal account, credit or debit card.', 'invoicing'); |
|
321 | 321 | |
322 | 322 | $admin_settings['worldpay_instId'] = array( |
323 | 323 | 'type' => 'text', |
324 | 324 | 'id' => 'worldpay_instId', |
325 | - 'name' => __( 'Installation Id', 'invoicing' ), |
|
326 | - 'desc' => __( 'Your installation id. Ex: 211616', 'invoicing' ), |
|
325 | + 'name' => __('Installation Id', 'invoicing'), |
|
326 | + 'desc' => __('Your installation id. Ex: 211616', 'invoicing'), |
|
327 | 327 | ); |
328 | 328 | |
329 | 329 | $admin_settings['worldpay_md5_secret'] = array( |
330 | 330 | 'type' => 'text', |
331 | 331 | 'id' => 'worldpay_md5_secret', |
332 | - 'name' => __( 'MD5 secret', 'invoicing' ), |
|
333 | - 'desc' => __( 'Optionally enter your MD5 secret here. Next, open your installation settings and ensure that your SignatureFields parameter is set to ', 'invoicing' ) . '<code>instId:amount:currency:cartId</code>', |
|
332 | + 'name' => __('MD5 secret', 'invoicing'), |
|
333 | + 'desc' => __('Optionally enter your MD5 secret here. Next, open your installation settings and ensure that your SignatureFields parameter is set to ', 'invoicing') . '<code>instId:amount:currency:cartId</code>', |
|
334 | 334 | ); |
335 | 335 | |
336 | 336 | $admin_settings['worldpay_callbackPW'] = array( |
337 | 337 | 'type' => 'text', |
338 | 338 | 'id' => 'worldpay_callbackPW', |
339 | - 'name' => __( 'Payment Response password', 'invoicing' ), |
|
340 | - 'desc' => __( 'Recommended. Enter your WorldPay response password to validate payment notifications.', 'invoicing' ), |
|
339 | + 'name' => __('Payment Response password', 'invoicing'), |
|
340 | + 'desc' => __('Recommended. Enter your WorldPay response password to validate payment notifications.', 'invoicing'), |
|
341 | 341 | ); |
342 | 342 | |
343 | 343 | $admin_settings['worldpay_ipn_url'] = array( |
344 | 344 | 'type' => 'ipn_url', |
345 | 345 | 'id' => 'worldpay_ipn_url', |
346 | - 'name' => __( 'Payment Response URL', 'invoicing' ), |
|
346 | + 'name' => __('Payment Response URL', 'invoicing'), |
|
347 | 347 | 'std' => $this->notify_url, |
348 | - 'desc' => __( 'Login to your Worldpay Merchant Interface then enable Payment Response & Shopper Response. Next, go to the Payment Response URL field and enter the above URL.', 'invoicing' ), |
|
348 | + 'desc' => __('Login to your Worldpay Merchant Interface then enable Payment Response & Shopper Response. Next, go to the Payment Response URL field and enter the above URL.', 'invoicing'), |
|
349 | 349 | 'custom' => 'worldpay', |
350 | 350 | 'readonly' => true, |
351 | 351 | ); |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * The Subscriptions DB Class |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | 'transaction_id' => '', |
73 | 73 | 'parent_payment_id' => 0, |
74 | 74 | 'product_id' => 0, |
75 | - 'created' => date( 'Y-m-d H:i:s' ), |
|
76 | - 'expiration' => date( 'Y-m-d H:i:s' ), |
|
75 | + 'created' => date('Y-m-d H:i:s'), |
|
76 | + 'expiration' => date('Y-m-d H:i:s'), |
|
77 | 77 | 'trial_period' => '', |
78 | 78 | 'status' => '', |
79 | 79 | 'profile_id' => '', |
@@ -86,8 +86,8 @@ discard block |
||
86 | 86 | * @access public |
87 | 87 | * @since 1.0.0 |
88 | 88 | */ |
89 | - public function get_subscriptions( $args = array() ) { |
|
90 | - return getpaid_get_subscriptions( $args ); |
|
89 | + public function get_subscriptions($args = array()) { |
|
90 | + return getpaid_get_subscriptions($args); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
@@ -96,8 +96,8 @@ discard block |
||
96 | 96 | * @access public |
97 | 97 | * @since 1.0.0 |
98 | 98 | */ |
99 | - public function count( $args = array() ) { |
|
100 | - return getpaid_get_subscriptions( $args, 'count' ); |
|
99 | + public function count($args = array()) { |
|
100 | + return getpaid_get_subscriptions($args, 'count'); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | KEY customer_and_status ( customer_id, status) |
134 | 134 | ) CHARACTER SET utf8 COLLATE utf8_general_ci;"; |
135 | 135 | |
136 | - dbDelta( $sql ); |
|
136 | + dbDelta($sql); |
|
137 | 137 | |
138 | - update_option( $this->table_name . '_db_version', $this->version ); |
|
138 | + update_option($this->table_name . '_db_version', $this->version); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | } |
@@ -12,167 +12,167 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Discount { |
14 | 14 | |
15 | - /** |
|
16 | - * Submission discounts. |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $discounts = array(); |
|
15 | + /** |
|
16 | + * Submission discounts. |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $discounts = array(); |
|
20 | + |
|
21 | + /** |
|
22 | + * Class constructor |
|
23 | + * |
|
24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
25 | + * @param float $initial_total |
|
26 | + * @param float $recurring_total |
|
27 | + */ |
|
28 | + public function __construct( $submission, $initial_total, $recurring_total ) { |
|
29 | + |
|
30 | + // Process any existing invoice discounts. |
|
31 | + if ( $submission->has_invoice() ) { |
|
32 | + $this->discounts = $submission->get_invoice()->get_discounts(); |
|
33 | + } |
|
34 | + |
|
35 | + // Do we have a discount? |
|
36 | + $discount = $submission->get_field( 'discount' ); |
|
37 | + |
|
38 | + if ( empty( $discount ) ) { |
|
39 | + |
|
40 | + if ( isset( $this->discounts['discount_code'] ) ) { |
|
41 | + unset( $this->discounts['discount_code'] ); |
|
42 | + } |
|
43 | + |
|
44 | + return; |
|
45 | + } |
|
46 | + |
|
47 | + // Processes the discount code. |
|
48 | + $amount = max( $initial_total, $recurring_total ); |
|
49 | + $this->process_discount( $submission, $discount, $amount ); |
|
50 | + |
|
51 | + } |
|
20 | 52 | |
21 | 53 | /** |
22 | - * Class constructor |
|
23 | - * |
|
24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
25 | - * @param float $initial_total |
|
26 | - * @param float $recurring_total |
|
27 | - */ |
|
28 | - public function __construct( $submission, $initial_total, $recurring_total ) { |
|
29 | - |
|
30 | - // Process any existing invoice discounts. |
|
31 | - if ( $submission->has_invoice() ) { |
|
32 | - $this->discounts = $submission->get_invoice()->get_discounts(); |
|
33 | - } |
|
34 | - |
|
35 | - // Do we have a discount? |
|
36 | - $discount = $submission->get_field( 'discount' ); |
|
37 | - |
|
38 | - if ( empty( $discount ) ) { |
|
39 | - |
|
40 | - if ( isset( $this->discounts['discount_code'] ) ) { |
|
41 | - unset( $this->discounts['discount_code'] ); |
|
42 | - } |
|
43 | - |
|
44 | - return; |
|
45 | - } |
|
46 | - |
|
47 | - // Processes the discount code. |
|
48 | - $amount = max( $initial_total, $recurring_total ); |
|
49 | - $this->process_discount( $submission, $discount, $amount ); |
|
50 | - |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Processes a submission discount. |
|
55 | - * |
|
56 | - * @param GetPaid_Payment_Form_Submission $submission |
|
57 | - * @param string $discount |
|
58 | - * @param float $amount |
|
59 | - */ |
|
60 | - public function process_discount( $submission, $discount, $amount ) { |
|
61 | - |
|
62 | - // Fetch the discount. |
|
63 | - $discount = new WPInv_Discount( $discount ); |
|
64 | - |
|
65 | - // Ensure it is active. |
|
54 | + * Processes a submission discount. |
|
55 | + * |
|
56 | + * @param GetPaid_Payment_Form_Submission $submission |
|
57 | + * @param string $discount |
|
58 | + * @param float $amount |
|
59 | + */ |
|
60 | + public function process_discount( $submission, $discount, $amount ) { |
|
61 | + |
|
62 | + // Fetch the discount. |
|
63 | + $discount = new WPInv_Discount( $discount ); |
|
64 | + |
|
65 | + // Ensure it is active. |
|
66 | 66 | if ( ! $this->is_discount_active( $discount ) ) { |
67 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
68 | - } |
|
69 | - |
|
70 | - // Required items. |
|
71 | - if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) { |
|
72 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) ); |
|
73 | - } |
|
74 | - |
|
75 | - // Exceeded limit. |
|
76 | - if ( $discount->has_exceeded_limit() ) { |
|
77 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) ); |
|
78 | - } |
|
79 | - |
|
80 | - // Validate usages. |
|
81 | - $this->validate_single_use_discount( $submission, $discount ); |
|
82 | - |
|
83 | - // Validate amount. |
|
84 | - $this->validate_discount_amount( $submission, $discount, $amount ); |
|
85 | - |
|
86 | - // Save the discount. |
|
87 | - $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Validates a single use discount. |
|
92 | - * |
|
93 | - * @param WPInv_Discount $discount |
|
94 | - * @return bool |
|
95 | - */ |
|
96 | - public function is_discount_active( $discount ) { |
|
97 | - return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Returns a user's id or email. |
|
102 | - * |
|
103 | - * @param string $email |
|
104 | - * @return int|string|false |
|
105 | - */ |
|
106 | - public function get_user_id_or_email( $email ) { |
|
107 | - |
|
108 | - if ( is_user_logged_in() ) { |
|
109 | - return get_current_user_id(); |
|
110 | - } |
|
111 | - |
|
112 | - return empty( $email ) ? false : sanitize_email( $email ); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Validates a single use discount. |
|
117 | - * |
|
118 | - * @param GetPaid_Payment_Form_Submission $submission |
|
119 | - * @param WPInv_Discount $discount |
|
120 | - */ |
|
121 | - public function validate_single_use_discount( $submission, $discount ) { |
|
122 | - |
|
123 | - // Abort if it is not a single use discount. |
|
124 | - if ( ! $discount->is_single_use() ) { |
|
125 | - return; |
|
126 | - } |
|
127 | - |
|
128 | - // Ensure there is a valid billing email. |
|
129 | - $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
130 | - |
|
131 | - if ( empty( $user ) ) { |
|
132 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
133 | - } |
|
134 | - |
|
135 | - // Has the user used this discount code before? |
|
136 | - if ( ! $discount->is_valid_for_user( $user ) ) { |
|
137 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) ); |
|
138 | - } |
|
139 | - |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Validates the discount's amount. |
|
144 | - * |
|
145 | - * @param GetPaid_Payment_Form_Submission $submission |
|
146 | - * @param WPInv_Discount $discount |
|
147 | - * @param float $amount |
|
148 | - */ |
|
149 | - public function validate_discount_amount( $submission, $discount, $amount ) { |
|
150 | - |
|
151 | - // Validate minimum amount. |
|
152 | - if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
153 | - $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
154 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
155 | - } |
|
156 | - |
|
157 | - // Validate the maximum amount. |
|
158 | - if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
159 | - $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
160 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
161 | - } |
|
162 | - |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Calculates the discount code's amount. |
|
167 | - * |
|
168 | - * Ensure that the discount exists and has been validated before calling this method. |
|
169 | - * |
|
170 | - * @param GetPaid_Payment_Form_Submission $submission |
|
171 | - * @param WPInv_Discount $discount |
|
172 | - * @return array |
|
173 | - */ |
|
174 | - public function calculate_discount( $submission, $discount ) { |
|
175 | - return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
176 | - } |
|
67 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
68 | + } |
|
69 | + |
|
70 | + // Required items. |
|
71 | + if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) { |
|
72 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) ); |
|
73 | + } |
|
74 | + |
|
75 | + // Exceeded limit. |
|
76 | + if ( $discount->has_exceeded_limit() ) { |
|
77 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) ); |
|
78 | + } |
|
79 | + |
|
80 | + // Validate usages. |
|
81 | + $this->validate_single_use_discount( $submission, $discount ); |
|
82 | + |
|
83 | + // Validate amount. |
|
84 | + $this->validate_discount_amount( $submission, $discount, $amount ); |
|
85 | + |
|
86 | + // Save the discount. |
|
87 | + $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Validates a single use discount. |
|
92 | + * |
|
93 | + * @param WPInv_Discount $discount |
|
94 | + * @return bool |
|
95 | + */ |
|
96 | + public function is_discount_active( $discount ) { |
|
97 | + return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Returns a user's id or email. |
|
102 | + * |
|
103 | + * @param string $email |
|
104 | + * @return int|string|false |
|
105 | + */ |
|
106 | + public function get_user_id_or_email( $email ) { |
|
107 | + |
|
108 | + if ( is_user_logged_in() ) { |
|
109 | + return get_current_user_id(); |
|
110 | + } |
|
111 | + |
|
112 | + return empty( $email ) ? false : sanitize_email( $email ); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Validates a single use discount. |
|
117 | + * |
|
118 | + * @param GetPaid_Payment_Form_Submission $submission |
|
119 | + * @param WPInv_Discount $discount |
|
120 | + */ |
|
121 | + public function validate_single_use_discount( $submission, $discount ) { |
|
122 | + |
|
123 | + // Abort if it is not a single use discount. |
|
124 | + if ( ! $discount->is_single_use() ) { |
|
125 | + return; |
|
126 | + } |
|
127 | + |
|
128 | + // Ensure there is a valid billing email. |
|
129 | + $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
130 | + |
|
131 | + if ( empty( $user ) ) { |
|
132 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
133 | + } |
|
134 | + |
|
135 | + // Has the user used this discount code before? |
|
136 | + if ( ! $discount->is_valid_for_user( $user ) ) { |
|
137 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) ); |
|
138 | + } |
|
139 | + |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Validates the discount's amount. |
|
144 | + * |
|
145 | + * @param GetPaid_Payment_Form_Submission $submission |
|
146 | + * @param WPInv_Discount $discount |
|
147 | + * @param float $amount |
|
148 | + */ |
|
149 | + public function validate_discount_amount( $submission, $discount, $amount ) { |
|
150 | + |
|
151 | + // Validate minimum amount. |
|
152 | + if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
153 | + $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
154 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
155 | + } |
|
156 | + |
|
157 | + // Validate the maximum amount. |
|
158 | + if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
159 | + $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
160 | + throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
161 | + } |
|
162 | + |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Calculates the discount code's amount. |
|
167 | + * |
|
168 | + * Ensure that the discount exists and has been validated before calling this method. |
|
169 | + * |
|
170 | + * @param GetPaid_Payment_Form_Submission $submission |
|
171 | + * @param WPInv_Discount $discount |
|
172 | + * @return array |
|
173 | + */ |
|
174 | + public function calculate_discount( $submission, $discount ) { |
|
175 | + return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
176 | + } |
|
177 | 177 | |
178 | 178 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Payment form submission discount class |
@@ -25,28 +25,28 @@ discard block |
||
25 | 25 | * @param float $initial_total |
26 | 26 | * @param float $recurring_total |
27 | 27 | */ |
28 | - public function __construct( $submission, $initial_total, $recurring_total ) { |
|
28 | + public function __construct($submission, $initial_total, $recurring_total) { |
|
29 | 29 | |
30 | 30 | // Process any existing invoice discounts. |
31 | - if ( $submission->has_invoice() ) { |
|
31 | + if ($submission->has_invoice()) { |
|
32 | 32 | $this->discounts = $submission->get_invoice()->get_discounts(); |
33 | 33 | } |
34 | 34 | |
35 | 35 | // Do we have a discount? |
36 | - $discount = $submission->get_field( 'discount' ); |
|
36 | + $discount = $submission->get_field('discount'); |
|
37 | 37 | |
38 | - if ( empty( $discount ) ) { |
|
38 | + if (empty($discount)) { |
|
39 | 39 | |
40 | - if ( isset( $this->discounts['discount_code'] ) ) { |
|
41 | - unset( $this->discounts['discount_code'] ); |
|
40 | + if (isset($this->discounts['discount_code'])) { |
|
41 | + unset($this->discounts['discount_code']); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | return; |
45 | 45 | } |
46 | 46 | |
47 | 47 | // Processes the discount code. |
48 | - $amount = max( $initial_total, $recurring_total ); |
|
49 | - $this->process_discount( $submission, $discount, $amount ); |
|
48 | + $amount = max($initial_total, $recurring_total); |
|
49 | + $this->process_discount($submission, $discount, $amount); |
|
50 | 50 | |
51 | 51 | } |
52 | 52 | |
@@ -57,34 +57,34 @@ discard block |
||
57 | 57 | * @param string $discount |
58 | 58 | * @param float $amount |
59 | 59 | */ |
60 | - public function process_discount( $submission, $discount, $amount ) { |
|
60 | + public function process_discount($submission, $discount, $amount) { |
|
61 | 61 | |
62 | 62 | // Fetch the discount. |
63 | - $discount = new WPInv_Discount( $discount ); |
|
63 | + $discount = new WPInv_Discount($discount); |
|
64 | 64 | |
65 | 65 | // Ensure it is active. |
66 | - if ( ! $this->is_discount_active( $discount ) ) { |
|
67 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
66 | + if (!$this->is_discount_active($discount)) { |
|
67 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', __('Invalid or expired discount code', 'invoicing')); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | // Required items. |
71 | - if ( ! $discount->is_required_items_met( array_keys( $submission->get_items() ) ) ) { |
|
72 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You are not allowed to use this discount code.', 'invoicing' ) ); |
|
71 | + if (!$discount->is_required_items_met(array_keys($submission->get_items()))) { |
|
72 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', __('You are not allowed to use this discount code.', 'invoicing')); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | // Exceeded limit. |
76 | - if ( $discount->has_exceeded_limit() ) { |
|
77 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'This discount code has been used up', 'invoicing' ) ); |
|
76 | + if ($discount->has_exceeded_limit()) { |
|
77 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', __('This discount code has been used up', 'invoicing')); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | // Validate usages. |
81 | - $this->validate_single_use_discount( $submission, $discount ); |
|
81 | + $this->validate_single_use_discount($submission, $discount); |
|
82 | 82 | |
83 | 83 | // Validate amount. |
84 | - $this->validate_discount_amount( $submission, $discount, $amount ); |
|
84 | + $this->validate_discount_amount($submission, $discount, $amount); |
|
85 | 85 | |
86 | 86 | // Save the discount. |
87 | - $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
87 | + $this->discounts['discount_code'] = $this->calculate_discount($submission, $discount); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
@@ -93,8 +93,8 @@ discard block |
||
93 | 93 | * @param WPInv_Discount $discount |
94 | 94 | * @return bool |
95 | 95 | */ |
96 | - public function is_discount_active( $discount ) { |
|
97 | - return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
96 | + public function is_discount_active($discount) { |
|
97 | + return $discount->exists() && $discount->is_active() && $discount->has_started() && !$discount->is_expired(); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -103,13 +103,13 @@ discard block |
||
103 | 103 | * @param string $email |
104 | 104 | * @return int|string|false |
105 | 105 | */ |
106 | - public function get_user_id_or_email( $email ) { |
|
106 | + public function get_user_id_or_email($email) { |
|
107 | 107 | |
108 | - if ( is_user_logged_in() ) { |
|
108 | + if (is_user_logged_in()) { |
|
109 | 109 | return get_current_user_id(); |
110 | 110 | } |
111 | 111 | |
112 | - return empty( $email ) ? false : sanitize_email( $email ); |
|
112 | + return empty($email) ? false : sanitize_email($email); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -118,23 +118,23 @@ discard block |
||
118 | 118 | * @param GetPaid_Payment_Form_Submission $submission |
119 | 119 | * @param WPInv_Discount $discount |
120 | 120 | */ |
121 | - public function validate_single_use_discount( $submission, $discount ) { |
|
121 | + public function validate_single_use_discount($submission, $discount) { |
|
122 | 122 | |
123 | 123 | // Abort if it is not a single use discount. |
124 | - if ( ! $discount->is_single_use() ) { |
|
124 | + if (!$discount->is_single_use()) { |
|
125 | 125 | return; |
126 | 126 | } |
127 | 127 | |
128 | 128 | // Ensure there is a valid billing email. |
129 | - $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
129 | + $user = $this->get_user_id_or_email($submission->get_billing_email()); |
|
130 | 130 | |
131 | - if ( empty( $user ) ) { |
|
132 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
131 | + if (empty($user)) { |
|
132 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', __('You need to either log in or enter your billing email before applying this discount', 'invoicing')); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | // Has the user used this discount code before? |
136 | - if ( ! $discount->is_valid_for_user( $user ) ) { |
|
137 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', __( 'You have already used this discount', 'invoicing' ) ); |
|
136 | + if (!$discount->is_valid_for_user($user)) { |
|
137 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', __('You have already used this discount', 'invoicing')); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | } |
@@ -146,18 +146,18 @@ discard block |
||
146 | 146 | * @param WPInv_Discount $discount |
147 | 147 | * @param float $amount |
148 | 148 | */ |
149 | - public function validate_discount_amount( $submission, $discount, $amount ) { |
|
149 | + public function validate_discount_amount($submission, $discount, $amount) { |
|
150 | 150 | |
151 | 151 | // Validate minimum amount. |
152 | - if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
153 | - $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
154 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
152 | + if (!$discount->is_minimum_amount_met($amount)) { |
|
153 | + $min = wpinv_price($discount->get_minimum_total(), $submission->get_currency()); |
|
154 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf(__('The minimum total for using this discount is %s', 'invoicing'), $min)); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | // Validate the maximum amount. |
158 | - if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
159 | - $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
160 | - throw new GetPaid_Payment_Exception( '.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
158 | + if (!$discount->is_maximum_amount_met($amount)) { |
|
159 | + $max = wpinv_price($discount->get_maximum_total(), $submission->get_currency()); |
|
160 | + throw new GetPaid_Payment_Exception('.getpaid-discount-field .getpaid-custom-payment-form-errors', sprintf(__('The maximum total for using this discount is %s', 'invoicing'), $max)); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | } |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | * @param WPInv_Discount $discount |
172 | 172 | * @return array |
173 | 173 | */ |
174 | - public function calculate_discount( $submission, $discount ) { |
|
175 | - return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
174 | + public function calculate_discount($submission, $discount) { |
|
175 | + return getpaid_calculate_invoice_discount($submission, $discount); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | } |
@@ -12,113 +12,113 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Fees { |
14 | 14 | |
15 | - /** |
|
16 | - * The fee validation error. |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - public $fee_error; |
|
20 | - |
|
21 | - /** |
|
22 | - * Submission fees. |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - public $fees = array(); |
|
15 | + /** |
|
16 | + * The fee validation error. |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + public $fee_error; |
|
20 | + |
|
21 | + /** |
|
22 | + * Submission fees. |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + public $fees = array(); |
|
26 | 26 | |
27 | 27 | /** |
28 | - * Class constructor |
|
29 | - * |
|
30 | - * @param GetPaid_Payment_Form_Submission $submission |
|
31 | - */ |
|
32 | - public function __construct( $submission ) { |
|
33 | - |
|
34 | - // Process any existing invoice fees. |
|
35 | - if ( $submission->has_invoice() ) { |
|
36 | - $this->fees = $submission->get_invoice()->get_fees(); |
|
37 | - } |
|
38 | - |
|
39 | - // Process price fields. |
|
40 | - $data = $submission->get_data(); |
|
41 | - $payment_form = $submission->get_payment_form(); |
|
42 | - |
|
43 | - foreach ( $payment_form->get_elements() as $element ) { |
|
44 | - |
|
45 | - if ( 'price_input' == $element['type'] ) { |
|
46 | - $this->process_price_input( $element, $data, $submission ); |
|
47 | - } |
|
48 | - |
|
49 | - if ( 'price_select' == $element['type'] ) { |
|
50 | - $this->process_price_select( $element, $data ); |
|
51 | - } |
|
28 | + * Class constructor |
|
29 | + * |
|
30 | + * @param GetPaid_Payment_Form_Submission $submission |
|
31 | + */ |
|
32 | + public function __construct( $submission ) { |
|
33 | + |
|
34 | + // Process any existing invoice fees. |
|
35 | + if ( $submission->has_invoice() ) { |
|
36 | + $this->fees = $submission->get_invoice()->get_fees(); |
|
37 | + } |
|
38 | + |
|
39 | + // Process price fields. |
|
40 | + $data = $submission->get_data(); |
|
41 | + $payment_form = $submission->get_payment_form(); |
|
42 | + |
|
43 | + foreach ( $payment_form->get_elements() as $element ) { |
|
44 | + |
|
45 | + if ( 'price_input' == $element['type'] ) { |
|
46 | + $this->process_price_input( $element, $data, $submission ); |
|
47 | + } |
|
48 | + |
|
49 | + if ( 'price_select' == $element['type'] ) { |
|
50 | + $this->process_price_select( $element, $data ); |
|
51 | + } |
|
52 | 52 | } |
53 | 53 | |
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Process a price input field. |
|
58 | - * |
|
59 | - * @param array $element |
|
60 | - * @param array $data |
|
61 | - * @param GetPaid_Payment_Form_Submission $submission |
|
62 | - */ |
|
63 | - public function process_price_input( $element, $data, $submission ) { |
|
64 | - |
|
65 | - // Abort if not passed. |
|
66 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
67 | - return; |
|
68 | - } |
|
69 | - |
|
70 | - $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
71 | - $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
72 | - |
|
73 | - if ( $amount < $minimum ) { |
|
74 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) ); |
|
75 | - } |
|
76 | - |
|
77 | - $this->fees[ $element['label'] ] = array( |
|
78 | - 'name' => $element['label'], |
|
79 | - 'initial_fee' => $amount, |
|
80 | - 'recurring_fee' => 0, |
|
81 | - ); |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Process a price select field. |
|
87 | - * |
|
88 | - * @param array $element |
|
89 | - * @param array $data |
|
90 | - */ |
|
91 | - public function process_price_select( $element, $data ) { |
|
92 | - |
|
93 | - // Abort if not passed. |
|
94 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
95 | - return; |
|
96 | - } |
|
97 | - |
|
98 | - $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
99 | - $selected = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) ); |
|
100 | - $total = 0; |
|
101 | - $sub_labels = array(); |
|
102 | - |
|
103 | - foreach ( $selected as $price ) { |
|
104 | - |
|
105 | - if ( ! isset( $options[ $price ] ) ) { |
|
106 | - throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
107 | - } |
|
108 | - |
|
109 | - $price = explode( '|', $price ); |
|
110 | - |
|
111 | - $sub_labels[] = $price[0]; |
|
112 | - $total += (float) wpinv_sanitize_amount( $price[1] ); |
|
113 | - } |
|
114 | - |
|
115 | - $this->fees[ $element['label'] ] = array( |
|
116 | - 'name' => $element['label'], |
|
117 | - 'initial_fee' => $total, |
|
118 | - 'recurring_fee' => 0, |
|
119 | - 'description' => implode( ', ', $sub_labels ), |
|
120 | - ); |
|
121 | - |
|
122 | - } |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Process a price input field. |
|
58 | + * |
|
59 | + * @param array $element |
|
60 | + * @param array $data |
|
61 | + * @param GetPaid_Payment_Form_Submission $submission |
|
62 | + */ |
|
63 | + public function process_price_input( $element, $data, $submission ) { |
|
64 | + |
|
65 | + // Abort if not passed. |
|
66 | + if ( empty( $data[ $element['id'] ] ) ) { |
|
67 | + return; |
|
68 | + } |
|
69 | + |
|
70 | + $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
71 | + $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
72 | + |
|
73 | + if ( $amount < $minimum ) { |
|
74 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) ); |
|
75 | + } |
|
76 | + |
|
77 | + $this->fees[ $element['label'] ] = array( |
|
78 | + 'name' => $element['label'], |
|
79 | + 'initial_fee' => $amount, |
|
80 | + 'recurring_fee' => 0, |
|
81 | + ); |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Process a price select field. |
|
87 | + * |
|
88 | + * @param array $element |
|
89 | + * @param array $data |
|
90 | + */ |
|
91 | + public function process_price_select( $element, $data ) { |
|
92 | + |
|
93 | + // Abort if not passed. |
|
94 | + if ( empty( $data[ $element['id'] ] ) ) { |
|
95 | + return; |
|
96 | + } |
|
97 | + |
|
98 | + $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
99 | + $selected = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) ); |
|
100 | + $total = 0; |
|
101 | + $sub_labels = array(); |
|
102 | + |
|
103 | + foreach ( $selected as $price ) { |
|
104 | + |
|
105 | + if ( ! isset( $options[ $price ] ) ) { |
|
106 | + throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
107 | + } |
|
108 | + |
|
109 | + $price = explode( '|', $price ); |
|
110 | + |
|
111 | + $sub_labels[] = $price[0]; |
|
112 | + $total += (float) wpinv_sanitize_amount( $price[1] ); |
|
113 | + } |
|
114 | + |
|
115 | + $this->fees[ $element['label'] ] = array( |
|
116 | + 'name' => $element['label'], |
|
117 | + 'initial_fee' => $total, |
|
118 | + 'recurring_fee' => 0, |
|
119 | + 'description' => implode( ', ', $sub_labels ), |
|
120 | + ); |
|
121 | + |
|
122 | + } |
|
123 | 123 | |
124 | 124 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Payment form submission fees class |
@@ -29,10 +29,10 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @param GetPaid_Payment_Form_Submission $submission |
31 | 31 | */ |
32 | - public function __construct( $submission ) { |
|
32 | + public function __construct($submission) { |
|
33 | 33 | |
34 | 34 | // Process any existing invoice fees. |
35 | - if ( $submission->has_invoice() ) { |
|
35 | + if ($submission->has_invoice()) { |
|
36 | 36 | $this->fees = $submission->get_invoice()->get_fees(); |
37 | 37 | } |
38 | 38 | |
@@ -40,14 +40,14 @@ discard block |
||
40 | 40 | $data = $submission->get_data(); |
41 | 41 | $payment_form = $submission->get_payment_form(); |
42 | 42 | |
43 | - foreach ( $payment_form->get_elements() as $element ) { |
|
43 | + foreach ($payment_form->get_elements() as $element) { |
|
44 | 44 | |
45 | - if ( 'price_input' == $element['type'] ) { |
|
46 | - $this->process_price_input( $element, $data, $submission ); |
|
45 | + if ('price_input' == $element['type']) { |
|
46 | + $this->process_price_input($element, $data, $submission); |
|
47 | 47 | } |
48 | 48 | |
49 | - if ( 'price_select' == $element['type'] ) { |
|
50 | - $this->process_price_select( $element, $data ); |
|
49 | + if ('price_select' == $element['type']) { |
|
50 | + $this->process_price_select($element, $data); |
|
51 | 51 | } |
52 | 52 | } |
53 | 53 | |
@@ -60,21 +60,21 @@ discard block |
||
60 | 60 | * @param array $data |
61 | 61 | * @param GetPaid_Payment_Form_Submission $submission |
62 | 62 | */ |
63 | - public function process_price_input( $element, $data, $submission ) { |
|
63 | + public function process_price_input($element, $data, $submission) { |
|
64 | 64 | |
65 | 65 | // Abort if not passed. |
66 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
66 | + if (empty($data[$element['id']])) { |
|
67 | 67 | return; |
68 | 68 | } |
69 | 69 | |
70 | - $amount = (float) wpinv_sanitize_amount( $data[ $element['id'] ] ); |
|
71 | - $minimum = empty( $element['minimum'] ) ? 0 : (float) wpinv_sanitize_amount( $element['minimum'] ); |
|
70 | + $amount = (float) wpinv_sanitize_amount($data[$element['id']]); |
|
71 | + $minimum = empty($element['minimum']) ? 0 : (float) wpinv_sanitize_amount($element['minimum']); |
|
72 | 72 | |
73 | - if ( $amount < $minimum ) { |
|
74 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $minimum, $submission->get_currency() ) ) ); |
|
73 | + if ($amount < $minimum) { |
|
74 | + throw new Exception(sprintf(__('The minimum allowed amount is %s', 'invoicing'), getpaid_unstandardize_amount($minimum, $submission->get_currency()))); |
|
75 | 75 | } |
76 | 76 | |
77 | - $this->fees[ $element['label'] ] = array( |
|
77 | + $this->fees[$element['label']] = array( |
|
78 | 78 | 'name' => $element['label'], |
79 | 79 | 'initial_fee' => $amount, |
80 | 80 | 'recurring_fee' => 0, |
@@ -88,35 +88,35 @@ discard block |
||
88 | 88 | * @param array $element |
89 | 89 | * @param array $data |
90 | 90 | */ |
91 | - public function process_price_select( $element, $data ) { |
|
91 | + public function process_price_select($element, $data) { |
|
92 | 92 | |
93 | 93 | // Abort if not passed. |
94 | - if ( empty( $data[ $element['id'] ] ) ) { |
|
94 | + if (empty($data[$element['id']])) { |
|
95 | 95 | return; |
96 | 96 | } |
97 | 97 | |
98 | - $options = getpaid_convert_price_string_to_options( $element['options'] ); |
|
99 | - $selected = array_filter( array_map( 'trim', explode( ',', $data[ $element['id'] ] ) ) ); |
|
98 | + $options = getpaid_convert_price_string_to_options($element['options']); |
|
99 | + $selected = array_filter(array_map('trim', explode(',', $data[$element['id']]))); |
|
100 | 100 | $total = 0; |
101 | 101 | $sub_labels = array(); |
102 | 102 | |
103 | - foreach ( $selected as $price ) { |
|
103 | + foreach ($selected as $price) { |
|
104 | 104 | |
105 | - if ( ! isset( $options[ $price ] ) ) { |
|
106 | - throw new Exception( __( 'You have selected an invalid amount', 'invoicing' ) ); |
|
105 | + if (!isset($options[$price])) { |
|
106 | + throw new Exception(__('You have selected an invalid amount', 'invoicing')); |
|
107 | 107 | } |
108 | 108 | |
109 | - $price = explode( '|', $price ); |
|
109 | + $price = explode('|', $price); |
|
110 | 110 | |
111 | 111 | $sub_labels[] = $price[0]; |
112 | - $total += (float) wpinv_sanitize_amount( $price[1] ); |
|
112 | + $total += (float) wpinv_sanitize_amount($price[1]); |
|
113 | 113 | } |
114 | 114 | |
115 | - $this->fees[ $element['label'] ] = array( |
|
115 | + $this->fees[$element['label']] = array( |
|
116 | 116 | 'name' => $element['label'], |
117 | 117 | 'initial_fee' => $total, |
118 | 118 | 'recurring_fee' => 0, |
119 | - 'description' => implode( ', ', $sub_labels ), |
|
119 | + 'description' => implode(', ', $sub_labels), |
|
120 | 120 | ); |
121 | 121 | |
122 | 122 | } |
@@ -12,227 +12,227 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Payment_Form_Submission_Taxes { |
14 | 14 | |
15 | - /** |
|
16 | - * Submission taxes. |
|
17 | - * @var array |
|
18 | - */ |
|
19 | - public $taxes = array(); |
|
20 | - |
|
21 | - /** |
|
22 | - * Whether or not we should skip the taxes. |
|
23 | - * @var bool |
|
24 | - */ |
|
25 | - protected $skip_taxes = false; |
|
15 | + /** |
|
16 | + * Submission taxes. |
|
17 | + * @var array |
|
18 | + */ |
|
19 | + public $taxes = array(); |
|
20 | + |
|
21 | + /** |
|
22 | + * Whether or not we should skip the taxes. |
|
23 | + * @var bool |
|
24 | + */ |
|
25 | + protected $skip_taxes = false; |
|
26 | + |
|
27 | + /** |
|
28 | + * Class constructor |
|
29 | + * |
|
30 | + * @param GetPaid_Payment_Form_Submission $submission |
|
31 | + */ |
|
32 | + public function __construct( $submission ) { |
|
33 | + |
|
34 | + // Validate VAT number. |
|
35 | + $this->validate_vat( $submission ); |
|
36 | + |
|
37 | + if ( $this->skip_taxes ) { |
|
38 | + return; |
|
39 | + } |
|
40 | + |
|
41 | + foreach ( $submission->get_items() as $item ) { |
|
42 | + $this->process_item_tax( $item, $submission ); |
|
43 | + } |
|
44 | + |
|
45 | + // Process any existing invoice taxes. |
|
46 | + if ( $submission->has_invoice() ) { |
|
47 | + $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
48 | + } |
|
49 | + |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Maybe process tax. |
|
54 | + * |
|
55 | + * @since 1.0.19 |
|
56 | + * @param GetPaid_Form_Item $item |
|
57 | + * @param GetPaid_Payment_Form_Submission $submission |
|
58 | + */ |
|
59 | + public function process_item_tax( $item, $submission ) { |
|
60 | + |
|
61 | + $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
62 | + $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
63 | + $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
64 | + $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
65 | + |
|
66 | + foreach ( $taxes as $name => $amount ) { |
|
67 | + $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
68 | + $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
69 | + |
|
70 | + $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
71 | + |
|
72 | + if ( ! isset( $this->taxes[ $name ] ) ) { |
|
73 | + $this->taxes[ $name ] = $tax; |
|
74 | + continue; |
|
75 | + } |
|
76 | + |
|
77 | + $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
78 | + $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
79 | + |
|
80 | + } |
|
81 | + |
|
82 | + } |
|
26 | 83 | |
27 | 84 | /** |
28 | - * Class constructor |
|
29 | - * |
|
30 | - * @param GetPaid_Payment_Form_Submission $submission |
|
31 | - */ |
|
32 | - public function __construct( $submission ) { |
|
33 | - |
|
34 | - // Validate VAT number. |
|
35 | - $this->validate_vat( $submission ); |
|
36 | - |
|
37 | - if ( $this->skip_taxes ) { |
|
38 | - return; |
|
39 | - } |
|
40 | - |
|
41 | - foreach ( $submission->get_items() as $item ) { |
|
42 | - $this->process_item_tax( $item, $submission ); |
|
43 | - } |
|
44 | - |
|
45 | - // Process any existing invoice taxes. |
|
46 | - if ( $submission->has_invoice() ) { |
|
47 | - $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
48 | - } |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Maybe process tax. |
|
54 | - * |
|
55 | - * @since 1.0.19 |
|
56 | - * @param GetPaid_Form_Item $item |
|
57 | - * @param GetPaid_Payment_Form_Submission $submission |
|
58 | - */ |
|
59 | - public function process_item_tax( $item, $submission ) { |
|
60 | - |
|
61 | - $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
62 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
63 | - $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
64 | - $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
65 | - |
|
66 | - foreach ( $taxes as $name => $amount ) { |
|
67 | - $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
68 | - $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
69 | - |
|
70 | - $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
71 | - |
|
72 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
73 | - $this->taxes[ $name ] = $tax; |
|
74 | - continue; |
|
75 | - } |
|
76 | - |
|
77 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
78 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
79 | - |
|
80 | - } |
|
81 | - |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Checks if the submission has a digital item. |
|
86 | - * |
|
87 | - * @param GetPaid_Payment_Form_Submission $submission |
|
88 | - * @since 1.0.19 |
|
89 | - * @return bool |
|
90 | - */ |
|
91 | - public function has_digital_item( $submission ) { |
|
92 | - |
|
93 | - foreach ( $submission->get_items() as $item ) { |
|
94 | - |
|
95 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
96 | - return true; |
|
97 | - } |
|
85 | + * Checks if the submission has a digital item. |
|
86 | + * |
|
87 | + * @param GetPaid_Payment_Form_Submission $submission |
|
88 | + * @since 1.0.19 |
|
89 | + * @return bool |
|
90 | + */ |
|
91 | + public function has_digital_item( $submission ) { |
|
92 | + |
|
93 | + foreach ( $submission->get_items() as $item ) { |
|
94 | + |
|
95 | + if ( 'digital' == $item->get_vat_rule() ) { |
|
96 | + return true; |
|
97 | + } |
|
98 | 98 | } |
99 | 99 | |
100 | - return false; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Checks if this is an eu store. |
|
105 | - * |
|
106 | - * @since 1.0.19 |
|
107 | - * @return bool |
|
108 | - */ |
|
109 | - public static function is_eu_store() { |
|
110 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Checks if this is an eu country. |
|
115 | - * |
|
116 | - * @param string $country |
|
117 | - * @since 1.0.19 |
|
118 | - * @return bool |
|
119 | - */ |
|
120 | - public static function is_eu_country( $country ) { |
|
121 | - return getpaid_is_eu_state( $country ); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Checks if this is an eu purchase. |
|
126 | - * |
|
127 | - * @param string $customer_country |
|
128 | - * @since 1.0.19 |
|
129 | - * @return bool |
|
130 | - */ |
|
131 | - public static function is_eu_transaction( $customer_country ) { |
|
132 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Retrieves the vat number. |
|
137 | - * |
|
138 | - * @param GetPaid_Payment_Form_Submission $submission |
|
139 | - * @since 1.0.19 |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public function get_vat_number( $submission ) { |
|
143 | - |
|
144 | - // Retrieve from the posted number. |
|
145 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
146 | - if ( ! is_null( $vat_number ) ) { |
|
147 | - return wpinv_clean( $vat_number ); |
|
148 | - } |
|
149 | - |
|
150 | - return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Retrieves the company. |
|
155 | - * |
|
156 | - * @param GetPaid_Payment_Form_Submission $submission |
|
157 | - * @since 1.0.19 |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - public function get_company( $submission ) { |
|
161 | - |
|
162 | - // Retrieve from the posted data. |
|
163 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
164 | - if ( ! empty( $company ) ) { |
|
165 | - return wpinv_clean( $company ); |
|
166 | - } |
|
167 | - |
|
168 | - // Retrieve from the invoice. |
|
169 | - return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Checks if we require a VAT number. |
|
174 | - * |
|
175 | - * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
176 | - * @param bool $country_in_eu Whether the customer country is from the EU |
|
177 | - * @since 1.0.19 |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
181 | - |
|
182 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
183 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
184 | - $is_eu = $ip_in_eu || $country_in_eu; |
|
185 | - |
|
186 | - return $prevent_b2c && $is_eu; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Validate VAT data. |
|
191 | - * |
|
192 | - * @param GetPaid_Payment_Form_Submission $submission |
|
193 | - * @since 1.0.19 |
|
194 | - */ |
|
195 | - public function validate_vat( $submission ) { |
|
196 | - |
|
197 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
198 | - |
|
199 | - // Abort if we are not validating vat numbers. |
|
200 | - if ( ! $in_eu ) { |
|
100 | + return false; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Checks if this is an eu store. |
|
105 | + * |
|
106 | + * @since 1.0.19 |
|
107 | + * @return bool |
|
108 | + */ |
|
109 | + public static function is_eu_store() { |
|
110 | + return self::is_eu_country( wpinv_get_default_country() ); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Checks if this is an eu country. |
|
115 | + * |
|
116 | + * @param string $country |
|
117 | + * @since 1.0.19 |
|
118 | + * @return bool |
|
119 | + */ |
|
120 | + public static function is_eu_country( $country ) { |
|
121 | + return getpaid_is_eu_state( $country ); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Checks if this is an eu purchase. |
|
126 | + * |
|
127 | + * @param string $customer_country |
|
128 | + * @since 1.0.19 |
|
129 | + * @return bool |
|
130 | + */ |
|
131 | + public static function is_eu_transaction( $customer_country ) { |
|
132 | + return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Retrieves the vat number. |
|
137 | + * |
|
138 | + * @param GetPaid_Payment_Form_Submission $submission |
|
139 | + * @since 1.0.19 |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public function get_vat_number( $submission ) { |
|
143 | + |
|
144 | + // Retrieve from the posted number. |
|
145 | + $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
146 | + if ( ! is_null( $vat_number ) ) { |
|
147 | + return wpinv_clean( $vat_number ); |
|
148 | + } |
|
149 | + |
|
150 | + return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Retrieves the company. |
|
155 | + * |
|
156 | + * @param GetPaid_Payment_Form_Submission $submission |
|
157 | + * @since 1.0.19 |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + public function get_company( $submission ) { |
|
161 | + |
|
162 | + // Retrieve from the posted data. |
|
163 | + $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
164 | + if ( ! empty( $company ) ) { |
|
165 | + return wpinv_clean( $company ); |
|
166 | + } |
|
167 | + |
|
168 | + // Retrieve from the invoice. |
|
169 | + return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Checks if we require a VAT number. |
|
174 | + * |
|
175 | + * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
176 | + * @param bool $country_in_eu Whether the customer country is from the EU |
|
177 | + * @since 1.0.19 |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
181 | + |
|
182 | + $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
183 | + $prevent_b2c = ! empty( $prevent_b2c ); |
|
184 | + $is_eu = $ip_in_eu || $country_in_eu; |
|
185 | + |
|
186 | + return $prevent_b2c && $is_eu; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Validate VAT data. |
|
191 | + * |
|
192 | + * @param GetPaid_Payment_Form_Submission $submission |
|
193 | + * @since 1.0.19 |
|
194 | + */ |
|
195 | + public function validate_vat( $submission ) { |
|
196 | + |
|
197 | + $in_eu = $this->is_eu_transaction( $submission->country ); |
|
198 | + |
|
199 | + // Abort if we are not validating vat numbers. |
|
200 | + if ( ! $in_eu ) { |
|
201 | 201 | return; |
202 | - } |
|
202 | + } |
|
203 | 203 | |
204 | - // Prepare variables. |
|
205 | - $vat_number = $this->get_vat_number( $submission ); |
|
206 | - $ip_country = getpaid_get_ip_country(); |
|
204 | + // Prepare variables. |
|
205 | + $vat_number = $this->get_vat_number( $submission ); |
|
206 | + $ip_country = getpaid_get_ip_country(); |
|
207 | 207 | $is_eu = $this->is_eu_country( $submission->country ); |
208 | 208 | $is_ip_eu = $this->is_eu_country( $ip_country ); |
209 | 209 | |
210 | - // Maybe abort early for initial fetches. |
|
211 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
212 | - return; |
|
213 | - } |
|
210 | + // Maybe abort early for initial fetches. |
|
211 | + if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
212 | + return; |
|
213 | + } |
|
214 | 214 | |
215 | - // If we're preventing business to consumer purchases, |
|
216 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
215 | + // If we're preventing business to consumer purchases, |
|
216 | + if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
217 | 217 | |
218 | - // Ensure that a vat number has been specified. |
|
219 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
218 | + // Ensure that a vat number has been specified. |
|
219 | + throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
220 | 220 | |
221 | - } |
|
221 | + } |
|
222 | 222 | |
223 | - if ( empty( $vat_number ) ) { |
|
224 | - return; |
|
225 | - } |
|
223 | + if ( empty( $vat_number ) ) { |
|
224 | + return; |
|
225 | + } |
|
226 | 226 | |
227 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
228 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
229 | - } |
|
227 | + if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
228 | + throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
229 | + } |
|
230 | 230 | |
231 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
232 | - return; |
|
233 | - } |
|
231 | + if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
232 | + return; |
|
233 | + } |
|
234 | 234 | |
235 | - $this->skip_taxes = true; |
|
236 | - } |
|
235 | + $this->skip_taxes = true; |
|
236 | + } |
|
237 | 237 | |
238 | 238 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * Payment form submission taxes class |
@@ -29,22 +29,22 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @param GetPaid_Payment_Form_Submission $submission |
31 | 31 | */ |
32 | - public function __construct( $submission ) { |
|
32 | + public function __construct($submission) { |
|
33 | 33 | |
34 | 34 | // Validate VAT number. |
35 | - $this->validate_vat( $submission ); |
|
35 | + $this->validate_vat($submission); |
|
36 | 36 | |
37 | - if ( $this->skip_taxes ) { |
|
37 | + if ($this->skip_taxes) { |
|
38 | 38 | return; |
39 | 39 | } |
40 | 40 | |
41 | - foreach ( $submission->get_items() as $item ) { |
|
42 | - $this->process_item_tax( $item, $submission ); |
|
41 | + foreach ($submission->get_items() as $item) { |
|
42 | + $this->process_item_tax($item, $submission); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | // Process any existing invoice taxes. |
46 | - if ( $submission->has_invoice() ) { |
|
47 | - $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
46 | + if ($submission->has_invoice()) { |
|
47 | + $this->taxes = array_replace($submission->get_invoice()->get_taxes(), $this->taxes); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | } |
@@ -56,26 +56,26 @@ discard block |
||
56 | 56 | * @param GetPaid_Form_Item $item |
57 | 57 | * @param GetPaid_Payment_Form_Submission $submission |
58 | 58 | */ |
59 | - public function process_item_tax( $item, $submission ) { |
|
59 | + public function process_item_tax($item, $submission) { |
|
60 | 60 | |
61 | - $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
62 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
63 | - $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
64 | - $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
61 | + $rates = getpaid_get_item_tax_rates($item, $submission->country, $submission->state); |
|
62 | + $rates = getpaid_filter_item_tax_rates($item, $rates); |
|
63 | + $taxes = getpaid_calculate_item_taxes(getpaid_get_taxable_amount($item, false), $rates); |
|
64 | + $r_taxes = getpaid_calculate_item_taxes(getpaid_get_taxable_amount($item, true), $rates); |
|
65 | 65 | |
66 | - foreach ( $taxes as $name => $amount ) { |
|
67 | - $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
68 | - $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
66 | + foreach ($taxes as $name => $amount) { |
|
67 | + $recurring = isset($r_taxes[$name]) ? $r_taxes[$name] : 0; |
|
68 | + $tax = getpaid_prepare_item_tax($item, $name, $amount, $recurring); |
|
69 | 69 | |
70 | - $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
70 | + $item->item_tax += wpinv_sanitize_amount($tax['initial_tax']); |
|
71 | 71 | |
72 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
73 | - $this->taxes[ $name ] = $tax; |
|
72 | + if (!isset($this->taxes[$name])) { |
|
73 | + $this->taxes[$name] = $tax; |
|
74 | 74 | continue; |
75 | 75 | } |
76 | 76 | |
77 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
78 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
77 | + $this->taxes[$name]['initial_tax'] += $tax['initial_tax']; |
|
78 | + $this->taxes[$name]['recurring_tax'] += $tax['recurring_tax']; |
|
79 | 79 | |
80 | 80 | } |
81 | 81 | |
@@ -88,11 +88,11 @@ discard block |
||
88 | 88 | * @since 1.0.19 |
89 | 89 | * @return bool |
90 | 90 | */ |
91 | - public function has_digital_item( $submission ) { |
|
91 | + public function has_digital_item($submission) { |
|
92 | 92 | |
93 | - foreach ( $submission->get_items() as $item ) { |
|
93 | + foreach ($submission->get_items() as $item) { |
|
94 | 94 | |
95 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
95 | + if ('digital' == $item->get_vat_rule()) { |
|
96 | 96 | return true; |
97 | 97 | } |
98 | 98 | } |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | * @return bool |
108 | 108 | */ |
109 | 109 | public static function is_eu_store() { |
110 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
110 | + return self::is_eu_country(wpinv_get_default_country()); |
|
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
@@ -117,8 +117,8 @@ discard block |
||
117 | 117 | * @since 1.0.19 |
118 | 118 | * @return bool |
119 | 119 | */ |
120 | - public static function is_eu_country( $country ) { |
|
121 | - return getpaid_is_eu_state( $country ); |
|
120 | + public static function is_eu_country($country) { |
|
121 | + return getpaid_is_eu_state($country); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
@@ -128,8 +128,8 @@ discard block |
||
128 | 128 | * @since 1.0.19 |
129 | 129 | * @return bool |
130 | 130 | */ |
131 | - public static function is_eu_transaction( $customer_country ) { |
|
132 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
131 | + public static function is_eu_transaction($customer_country) { |
|
132 | + return self::is_eu_country($customer_country) && self::is_eu_store(); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
@@ -139,12 +139,12 @@ discard block |
||
139 | 139 | * @since 1.0.19 |
140 | 140 | * @return string |
141 | 141 | */ |
142 | - public function get_vat_number( $submission ) { |
|
142 | + public function get_vat_number($submission) { |
|
143 | 143 | |
144 | 144 | // Retrieve from the posted number. |
145 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
146 | - if ( ! is_null( $vat_number ) ) { |
|
147 | - return wpinv_clean( $vat_number ); |
|
145 | + $vat_number = $submission->get_field('wpinv_vat_number', 'billing'); |
|
146 | + if (!is_null($vat_number)) { |
|
147 | + return wpinv_clean($vat_number); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
@@ -157,12 +157,12 @@ discard block |
||
157 | 157 | * @since 1.0.19 |
158 | 158 | * @return string |
159 | 159 | */ |
160 | - public function get_company( $submission ) { |
|
160 | + public function get_company($submission) { |
|
161 | 161 | |
162 | 162 | // Retrieve from the posted data. |
163 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
164 | - if ( ! empty( $company ) ) { |
|
165 | - return wpinv_clean( $company ); |
|
163 | + $company = $submission->get_field('wpinv_company', 'billing'); |
|
164 | + if (!empty($company)) { |
|
165 | + return wpinv_clean($company); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | // Retrieve from the invoice. |
@@ -177,10 +177,10 @@ discard block |
||
177 | 177 | * @since 1.0.19 |
178 | 178 | * @return string |
179 | 179 | */ |
180 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
180 | + public function requires_vat($ip_in_eu, $country_in_eu) { |
|
181 | 181 | |
182 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
183 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
182 | + $prevent_b2c = wpinv_get_option('vat_prevent_b2c_purchase'); |
|
183 | + $prevent_b2c = !empty($prevent_b2c); |
|
184 | 184 | $is_eu = $ip_in_eu || $country_in_eu; |
185 | 185 | |
186 | 186 | return $prevent_b2c && $is_eu; |
@@ -192,43 +192,43 @@ discard block |
||
192 | 192 | * @param GetPaid_Payment_Form_Submission $submission |
193 | 193 | * @since 1.0.19 |
194 | 194 | */ |
195 | - public function validate_vat( $submission ) { |
|
195 | + public function validate_vat($submission) { |
|
196 | 196 | |
197 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
197 | + $in_eu = $this->is_eu_transaction($submission->country); |
|
198 | 198 | |
199 | 199 | // Abort if we are not validating vat numbers. |
200 | - if ( ! $in_eu ) { |
|
200 | + if (!$in_eu) { |
|
201 | 201 | return; |
202 | 202 | } |
203 | 203 | |
204 | 204 | // Prepare variables. |
205 | - $vat_number = $this->get_vat_number( $submission ); |
|
205 | + $vat_number = $this->get_vat_number($submission); |
|
206 | 206 | $ip_country = getpaid_get_ip_country(); |
207 | - $is_eu = $this->is_eu_country( $submission->country ); |
|
208 | - $is_ip_eu = $this->is_eu_country( $ip_country ); |
|
207 | + $is_eu = $this->is_eu_country($submission->country); |
|
208 | + $is_ip_eu = $this->is_eu_country($ip_country); |
|
209 | 209 | |
210 | 210 | // Maybe abort early for initial fetches. |
211 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
211 | + if ($submission->is_initial_fetch() && empty($vat_number)) { |
|
212 | 212 | return; |
213 | 213 | } |
214 | 214 | |
215 | 215 | // If we're preventing business to consumer purchases, |
216 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
216 | + if ($this->requires_vat($is_ip_eu, $is_eu) && empty($vat_number)) { |
|
217 | 217 | |
218 | 218 | // Ensure that a vat number has been specified. |
219 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
219 | + throw new GetPaid_Payment_Exception('.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __('Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing')); |
|
220 | 220 | |
221 | 221 | } |
222 | 222 | |
223 | - if ( empty( $vat_number ) ) { |
|
223 | + if (empty($vat_number)) { |
|
224 | 224 | return; |
225 | 225 | } |
226 | 226 | |
227 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
228 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
227 | + if (wpinv_should_validate_vat_number() && !wpinv_validate_vat_number($vat_number, $submission->country)) { |
|
228 | + throw new GetPaid_Payment_Exception('.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __('Your VAT number is invalid', 'invoicing')); |
|
229 | 229 | } |
230 | 230 | |
231 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
231 | + if (wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option('vat_same_country_rule', 'vat_too')) { |
|
232 | 232 | return; |
233 | 233 | } |
234 | 234 |
@@ -12,69 +12,69 @@ discard block |
||
12 | 12 | */ |
13 | 13 | abstract class GetPaid_Reports_Abstract_Report { |
14 | 14 | |
15 | - /** |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - public $stats; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - */ |
|
24 | - public function __construct() { |
|
25 | - $this->prepare_stats(); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Retrieves the current range. |
|
30 | - * |
|
31 | - */ |
|
32 | - public function get_range() { |
|
33 | - $valid_ranges = $this->get_periods(); |
|
34 | - |
|
35 | - if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) { |
|
36 | - return sanitize_key( $_GET['date_range'] ); |
|
37 | - } |
|
38 | - |
|
39 | - return '7_days'; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Returns an array of date ranges. |
|
44 | - * |
|
45 | - * @return array |
|
46 | - */ |
|
47 | - public function get_periods() { |
|
48 | - |
|
49 | - $periods = array( |
|
15 | + /** |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + public $stats; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + */ |
|
24 | + public function __construct() { |
|
25 | + $this->prepare_stats(); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Retrieves the current range. |
|
30 | + * |
|
31 | + */ |
|
32 | + public function get_range() { |
|
33 | + $valid_ranges = $this->get_periods(); |
|
34 | + |
|
35 | + if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) { |
|
36 | + return sanitize_key( $_GET['date_range'] ); |
|
37 | + } |
|
38 | + |
|
39 | + return '7_days'; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Returns an array of date ranges. |
|
44 | + * |
|
45 | + * @return array |
|
46 | + */ |
|
47 | + public function get_periods() { |
|
48 | + |
|
49 | + $periods = array( |
|
50 | 50 | 'today' => __( 'Today', 'invoicing' ), |
51 | 51 | 'yesterday' => __( 'Yesterday', 'invoicing' ), |
52 | 52 | '7_days' => __( 'Last 7 days', 'invoicing' ), |
53 | - '30_days' => __( 'Last 30 days', 'invoicing' ), |
|
54 | - '60_days' => __( 'Last 60 days', 'invoicing' ), |
|
55 | - '90_days' => __( 'Last 90 days', 'invoicing' ), |
|
56 | - '180_days' => __( 'Last 180 days', 'invoicing' ), |
|
57 | - '360_days' => __( 'Last 360 days', 'invoicing' ), |
|
58 | - ); |
|
59 | - |
|
60 | - return apply_filters( 'getpaid_earning_periods', $periods ); |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Retrieves the current range's sql. |
|
65 | - * |
|
66 | - */ |
|
67 | - public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) { |
|
53 | + '30_days' => __( 'Last 30 days', 'invoicing' ), |
|
54 | + '60_days' => __( 'Last 60 days', 'invoicing' ), |
|
55 | + '90_days' => __( 'Last 90 days', 'invoicing' ), |
|
56 | + '180_days' => __( 'Last 180 days', 'invoicing' ), |
|
57 | + '360_days' => __( 'Last 360 days', 'invoicing' ), |
|
58 | + ); |
|
59 | + |
|
60 | + return apply_filters( 'getpaid_earning_periods', $periods ); |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Retrieves the current range's sql. |
|
65 | + * |
|
66 | + */ |
|
67 | + public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) { |
|
68 | 68 | |
69 | 69 | // Prepare durations. |
70 | 70 | $today = current_time( 'Y-m-d' ); |
71 | - $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); |
|
72 | - $seven_days_ago = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) ); |
|
73 | - $thirty_days_ago = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); |
|
74 | - $ninety_days_ago = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) ); |
|
75 | - $sixty_days_ago = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) ); |
|
76 | - $one_eighty_days_ago = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) ); |
|
77 | - $three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) ); |
|
71 | + $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); |
|
72 | + $seven_days_ago = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) ); |
|
73 | + $thirty_days_ago = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); |
|
74 | + $ninety_days_ago = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) ); |
|
75 | + $sixty_days_ago = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) ); |
|
76 | + $one_eighty_days_ago = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) ); |
|
77 | + $three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) ); |
|
78 | 78 | |
79 | 79 | $ranges = array( |
80 | 80 | |
@@ -91,130 +91,130 @@ discard block |
||
91 | 91 | '7_days' => array( |
92 | 92 | "DATE($datetime)", |
93 | 93 | "$date BETWEEN '$seven_days_ago' AND '$today'", |
94 | - ), |
|
94 | + ), |
|
95 | 95 | |
96 | - '30_days' => array( |
|
96 | + '30_days' => array( |
|
97 | 97 | "DATE($datetime)", |
98 | 98 | "$date BETWEEN '$thirty_days_ago' AND '$today'", |
99 | - ), |
|
99 | + ), |
|
100 | 100 | |
101 | - '60_days' => array( |
|
101 | + '60_days' => array( |
|
102 | 102 | "DATE($datetime)", |
103 | 103 | "$date BETWEEN '$sixty_days_ago' AND '$today'", |
104 | - ), |
|
104 | + ), |
|
105 | 105 | |
106 | - '90_days' => array( |
|
106 | + '90_days' => array( |
|
107 | 107 | "WEEK($datetime)", |
108 | 108 | "$date BETWEEN '$ninety_days_ago' AND '$today'", |
109 | - ), |
|
109 | + ), |
|
110 | 110 | |
111 | - '180_days' => array( |
|
111 | + '180_days' => array( |
|
112 | 112 | "WEEK($datetime)", |
113 | 113 | "$date BETWEEN '$one_eighty_days_ago' AND '$today'", |
114 | - ), |
|
114 | + ), |
|
115 | 115 | |
116 | - '360_days' => array( |
|
116 | + '360_days' => array( |
|
117 | 117 | "WEEK($datetime)", |
118 | 118 | "$date BETWEEN '$three_sixty_days_ago' AND '$today'", |
119 | 119 | ), |
120 | 120 | |
121 | 121 | ); |
122 | 122 | |
123 | - $sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days']; |
|
124 | - return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range ); |
|
125 | - |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Retrieves the hours in a day |
|
130 | - * |
|
131 | - */ |
|
132 | - public function get_hours_in_a_day() { |
|
133 | - |
|
134 | - return array( |
|
135 | - '12AM' => __( '12 AM', 'invoicing' ), |
|
136 | - '1AM' => __( '1 AM', 'invoicing' ), |
|
137 | - '2AM' => __( '2 AM', 'invoicing' ), |
|
138 | - '3AM' => __( '3 AM', 'invoicing' ), |
|
139 | - '4AM' => __( '4 AM', 'invoicing' ), |
|
140 | - '5AM' => __( '5 AM', 'invoicing' ), |
|
141 | - '6AM' => __( '6 AM', 'invoicing' ), |
|
142 | - '7AM' => __( '7 AM', 'invoicing' ), |
|
143 | - '8AM' => __( '8 AM', 'invoicing' ), |
|
144 | - '9AM' => __( '9 AM', 'invoicing' ), |
|
145 | - '10AM' => __( '10 AM', 'invoicing' ), |
|
146 | - '11AM' => __( '11 AM', 'invoicing' ), |
|
147 | - '12pm' => __( '12 PM', 'invoicing' ), |
|
148 | - '1PM' => __( '1 PM', 'invoicing' ), |
|
149 | - '2PM' => __( '2 PM', 'invoicing' ), |
|
150 | - '3PM' => __( '3 PM', 'invoicing' ), |
|
151 | - '4PM' => __( '4 PM', 'invoicing' ), |
|
152 | - '5PM' => __( '5 PM', 'invoicing' ), |
|
153 | - '6PM' => __( '6 PM', 'invoicing' ), |
|
154 | - '7PM' => __( '7 PM', 'invoicing' ), |
|
155 | - '8PM' => __( '8 PM', 'invoicing' ), |
|
156 | - '9PM' => __( '9 PM', 'invoicing' ), |
|
157 | - '10PM' => __( '10 PM', 'invoicing' ), |
|
158 | - '11PM' => __( '11 PM', 'invoicing' ), |
|
159 | - ); |
|
160 | - |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Retrieves the days in a period |
|
165 | - * |
|
166 | - */ |
|
167 | - public function get_days_in_period( $days ) { |
|
168 | - |
|
169 | - $return = array(); |
|
170 | - $format = 'Y-m-d'; |
|
171 | - |
|
172 | - if ( $days < 8 ) { |
|
173 | - $format = 'D'; |
|
174 | - } |
|
175 | - |
|
176 | - if ( $days < 32 ) { |
|
177 | - $format = 'M j'; |
|
178 | - } |
|
179 | - |
|
180 | - while ( $days > 0 ) { |
|
181 | - |
|
182 | - $key = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
183 | - $label = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
184 | - $return[ $key ] = $label; |
|
185 | - $days--; |
|
186 | - |
|
187 | - } |
|
188 | - |
|
189 | - return $return; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Retrieves the weeks in a period |
|
194 | - * |
|
195 | - */ |
|
196 | - public function get_weeks_in_period( $days ) { |
|
197 | - |
|
198 | - $return = array(); |
|
199 | - |
|
200 | - while ( $days > 0 ) { |
|
201 | - |
|
202 | - $key = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
203 | - $label = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
204 | - $return[ $key ] = $label; |
|
205 | - $days--; |
|
206 | - |
|
207 | - } |
|
208 | - |
|
209 | - return $return; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Displays the report card. |
|
214 | - * |
|
215 | - */ |
|
216 | - public function display() { |
|
217 | - ?> |
|
123 | + $sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days']; |
|
124 | + return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range ); |
|
125 | + |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Retrieves the hours in a day |
|
130 | + * |
|
131 | + */ |
|
132 | + public function get_hours_in_a_day() { |
|
133 | + |
|
134 | + return array( |
|
135 | + '12AM' => __( '12 AM', 'invoicing' ), |
|
136 | + '1AM' => __( '1 AM', 'invoicing' ), |
|
137 | + '2AM' => __( '2 AM', 'invoicing' ), |
|
138 | + '3AM' => __( '3 AM', 'invoicing' ), |
|
139 | + '4AM' => __( '4 AM', 'invoicing' ), |
|
140 | + '5AM' => __( '5 AM', 'invoicing' ), |
|
141 | + '6AM' => __( '6 AM', 'invoicing' ), |
|
142 | + '7AM' => __( '7 AM', 'invoicing' ), |
|
143 | + '8AM' => __( '8 AM', 'invoicing' ), |
|
144 | + '9AM' => __( '9 AM', 'invoicing' ), |
|
145 | + '10AM' => __( '10 AM', 'invoicing' ), |
|
146 | + '11AM' => __( '11 AM', 'invoicing' ), |
|
147 | + '12pm' => __( '12 PM', 'invoicing' ), |
|
148 | + '1PM' => __( '1 PM', 'invoicing' ), |
|
149 | + '2PM' => __( '2 PM', 'invoicing' ), |
|
150 | + '3PM' => __( '3 PM', 'invoicing' ), |
|
151 | + '4PM' => __( '4 PM', 'invoicing' ), |
|
152 | + '5PM' => __( '5 PM', 'invoicing' ), |
|
153 | + '6PM' => __( '6 PM', 'invoicing' ), |
|
154 | + '7PM' => __( '7 PM', 'invoicing' ), |
|
155 | + '8PM' => __( '8 PM', 'invoicing' ), |
|
156 | + '9PM' => __( '9 PM', 'invoicing' ), |
|
157 | + '10PM' => __( '10 PM', 'invoicing' ), |
|
158 | + '11PM' => __( '11 PM', 'invoicing' ), |
|
159 | + ); |
|
160 | + |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Retrieves the days in a period |
|
165 | + * |
|
166 | + */ |
|
167 | + public function get_days_in_period( $days ) { |
|
168 | + |
|
169 | + $return = array(); |
|
170 | + $format = 'Y-m-d'; |
|
171 | + |
|
172 | + if ( $days < 8 ) { |
|
173 | + $format = 'D'; |
|
174 | + } |
|
175 | + |
|
176 | + if ( $days < 32 ) { |
|
177 | + $format = 'M j'; |
|
178 | + } |
|
179 | + |
|
180 | + while ( $days > 0 ) { |
|
181 | + |
|
182 | + $key = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
183 | + $label = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
184 | + $return[ $key ] = $label; |
|
185 | + $days--; |
|
186 | + |
|
187 | + } |
|
188 | + |
|
189 | + return $return; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Retrieves the weeks in a period |
|
194 | + * |
|
195 | + */ |
|
196 | + public function get_weeks_in_period( $days ) { |
|
197 | + |
|
198 | + $return = array(); |
|
199 | + |
|
200 | + while ( $days > 0 ) { |
|
201 | + |
|
202 | + $key = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
203 | + $label = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
204 | + $return[ $key ] = $label; |
|
205 | + $days--; |
|
206 | + |
|
207 | + } |
|
208 | + |
|
209 | + return $return; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Displays the report card. |
|
214 | + * |
|
215 | + */ |
|
216 | + public function display() { |
|
217 | + ?> |
|
218 | 218 | |
219 | 219 | <div class="row"> |
220 | 220 | <div class="col-12"> |
@@ -228,20 +228,20 @@ discard block |
||
228 | 228 | |
229 | 229 | <?php |
230 | 230 | |
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Prepares the report stats. |
|
235 | - * |
|
236 | - * Extend this in child classes. |
|
237 | - */ |
|
238 | - abstract public function prepare_stats(); |
|
239 | - |
|
240 | - /** |
|
241 | - * Displays the actual report. |
|
242 | - * |
|
243 | - * Extend this in child classes. |
|
244 | - */ |
|
245 | - abstract public function display_stats(); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Prepares the report stats. |
|
235 | + * |
|
236 | + * Extend this in child classes. |
|
237 | + */ |
|
238 | + abstract public function prepare_stats(); |
|
239 | + |
|
240 | + /** |
|
241 | + * Displays the actual report. |
|
242 | + * |
|
243 | + * Extend this in child classes. |
|
244 | + */ |
|
245 | + abstract public function display_stats(); |
|
246 | 246 | |
247 | 247 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | |
8 | -defined( 'ABSPATH' ) || exit; |
|
8 | +defined('ABSPATH') || exit; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * GetPaid_Reports_Abstract_Report Class. |
@@ -32,8 +32,8 @@ discard block |
||
32 | 32 | public function get_range() { |
33 | 33 | $valid_ranges = $this->get_periods(); |
34 | 34 | |
35 | - if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) { |
|
36 | - return sanitize_key( $_GET['date_range'] ); |
|
35 | + if (isset($_GET['date_range']) && array_key_exists($_GET['date_range'], $valid_ranges)) { |
|
36 | + return sanitize_key($_GET['date_range']); |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | return '7_days'; |
@@ -47,34 +47,34 @@ discard block |
||
47 | 47 | public function get_periods() { |
48 | 48 | |
49 | 49 | $periods = array( |
50 | - 'today' => __( 'Today', 'invoicing' ), |
|
51 | - 'yesterday' => __( 'Yesterday', 'invoicing' ), |
|
52 | - '7_days' => __( 'Last 7 days', 'invoicing' ), |
|
53 | - '30_days' => __( 'Last 30 days', 'invoicing' ), |
|
54 | - '60_days' => __( 'Last 60 days', 'invoicing' ), |
|
55 | - '90_days' => __( 'Last 90 days', 'invoicing' ), |
|
56 | - '180_days' => __( 'Last 180 days', 'invoicing' ), |
|
57 | - '360_days' => __( 'Last 360 days', 'invoicing' ), |
|
50 | + 'today' => __('Today', 'invoicing'), |
|
51 | + 'yesterday' => __('Yesterday', 'invoicing'), |
|
52 | + '7_days' => __('Last 7 days', 'invoicing'), |
|
53 | + '30_days' => __('Last 30 days', 'invoicing'), |
|
54 | + '60_days' => __('Last 60 days', 'invoicing'), |
|
55 | + '90_days' => __('Last 90 days', 'invoicing'), |
|
56 | + '180_days' => __('Last 180 days', 'invoicing'), |
|
57 | + '360_days' => __('Last 360 days', 'invoicing'), |
|
58 | 58 | ); |
59 | 59 | |
60 | - return apply_filters( 'getpaid_earning_periods', $periods ); |
|
60 | + return apply_filters('getpaid_earning_periods', $periods); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Retrieves the current range's sql. |
65 | 65 | * |
66 | 66 | */ |
67 | - public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) { |
|
67 | + public function get_range_sql($range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date') { |
|
68 | 68 | |
69 | 69 | // Prepare durations. |
70 | - $today = current_time( 'Y-m-d' ); |
|
71 | - $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); |
|
72 | - $seven_days_ago = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) ); |
|
73 | - $thirty_days_ago = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); |
|
74 | - $ninety_days_ago = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) ); |
|
75 | - $sixty_days_ago = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) ); |
|
76 | - $one_eighty_days_ago = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) ); |
|
77 | - $three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) ); |
|
70 | + $today = current_time('Y-m-d'); |
|
71 | + $yesterday = date('Y-m-d', strtotime('-1 day', current_time('timestamp'))); |
|
72 | + $seven_days_ago = date('Y-m-d', strtotime('-7 days', current_time('timestamp'))); |
|
73 | + $thirty_days_ago = date('Y-m-d', strtotime('-30 days', current_time('timestamp'))); |
|
74 | + $ninety_days_ago = date('Y-m-d', strtotime('-90 days', current_time('timestamp'))); |
|
75 | + $sixty_days_ago = date('Y-m-d', strtotime('-60 days', current_time('timestamp'))); |
|
76 | + $one_eighty_days_ago = date('Y-m-d', strtotime('-180 days', current_time('timestamp'))); |
|
77 | + $three_sixty_days_ago = date('Y-m-d', strtotime('-360 days', current_time('timestamp'))); |
|
78 | 78 | |
79 | 79 | $ranges = array( |
80 | 80 | |
@@ -120,8 +120,8 @@ discard block |
||
120 | 120 | |
121 | 121 | ); |
122 | 122 | |
123 | - $sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days']; |
|
124 | - return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range ); |
|
123 | + $sql = isset($ranges[$range]) ? $ranges[$range] : $ranges['7_days']; |
|
124 | + return apply_filters('getpaid_earning_graphs_get_range_sql', $sql, $range); |
|
125 | 125 | |
126 | 126 | } |
127 | 127 | |
@@ -132,30 +132,30 @@ discard block |
||
132 | 132 | public function get_hours_in_a_day() { |
133 | 133 | |
134 | 134 | return array( |
135 | - '12AM' => __( '12 AM', 'invoicing' ), |
|
136 | - '1AM' => __( '1 AM', 'invoicing' ), |
|
137 | - '2AM' => __( '2 AM', 'invoicing' ), |
|
138 | - '3AM' => __( '3 AM', 'invoicing' ), |
|
139 | - '4AM' => __( '4 AM', 'invoicing' ), |
|
140 | - '5AM' => __( '5 AM', 'invoicing' ), |
|
141 | - '6AM' => __( '6 AM', 'invoicing' ), |
|
142 | - '7AM' => __( '7 AM', 'invoicing' ), |
|
143 | - '8AM' => __( '8 AM', 'invoicing' ), |
|
144 | - '9AM' => __( '9 AM', 'invoicing' ), |
|
145 | - '10AM' => __( '10 AM', 'invoicing' ), |
|
146 | - '11AM' => __( '11 AM', 'invoicing' ), |
|
147 | - '12pm' => __( '12 PM', 'invoicing' ), |
|
148 | - '1PM' => __( '1 PM', 'invoicing' ), |
|
149 | - '2PM' => __( '2 PM', 'invoicing' ), |
|
150 | - '3PM' => __( '3 PM', 'invoicing' ), |
|
151 | - '4PM' => __( '4 PM', 'invoicing' ), |
|
152 | - '5PM' => __( '5 PM', 'invoicing' ), |
|
153 | - '6PM' => __( '6 PM', 'invoicing' ), |
|
154 | - '7PM' => __( '7 PM', 'invoicing' ), |
|
155 | - '8PM' => __( '8 PM', 'invoicing' ), |
|
156 | - '9PM' => __( '9 PM', 'invoicing' ), |
|
157 | - '10PM' => __( '10 PM', 'invoicing' ), |
|
158 | - '11PM' => __( '11 PM', 'invoicing' ), |
|
135 | + '12AM' => __('12 AM', 'invoicing'), |
|
136 | + '1AM' => __('1 AM', 'invoicing'), |
|
137 | + '2AM' => __('2 AM', 'invoicing'), |
|
138 | + '3AM' => __('3 AM', 'invoicing'), |
|
139 | + '4AM' => __('4 AM', 'invoicing'), |
|
140 | + '5AM' => __('5 AM', 'invoicing'), |
|
141 | + '6AM' => __('6 AM', 'invoicing'), |
|
142 | + '7AM' => __('7 AM', 'invoicing'), |
|
143 | + '8AM' => __('8 AM', 'invoicing'), |
|
144 | + '9AM' => __('9 AM', 'invoicing'), |
|
145 | + '10AM' => __('10 AM', 'invoicing'), |
|
146 | + '11AM' => __('11 AM', 'invoicing'), |
|
147 | + '12pm' => __('12 PM', 'invoicing'), |
|
148 | + '1PM' => __('1 PM', 'invoicing'), |
|
149 | + '2PM' => __('2 PM', 'invoicing'), |
|
150 | + '3PM' => __('3 PM', 'invoicing'), |
|
151 | + '4PM' => __('4 PM', 'invoicing'), |
|
152 | + '5PM' => __('5 PM', 'invoicing'), |
|
153 | + '6PM' => __('6 PM', 'invoicing'), |
|
154 | + '7PM' => __('7 PM', 'invoicing'), |
|
155 | + '8PM' => __('8 PM', 'invoicing'), |
|
156 | + '9PM' => __('9 PM', 'invoicing'), |
|
157 | + '10PM' => __('10 PM', 'invoicing'), |
|
158 | + '11PM' => __('11 PM', 'invoicing'), |
|
159 | 159 | ); |
160 | 160 | |
161 | 161 | } |
@@ -164,24 +164,24 @@ discard block |
||
164 | 164 | * Retrieves the days in a period |
165 | 165 | * |
166 | 166 | */ |
167 | - public function get_days_in_period( $days ) { |
|
167 | + public function get_days_in_period($days) { |
|
168 | 168 | |
169 | 169 | $return = array(); |
170 | 170 | $format = 'Y-m-d'; |
171 | 171 | |
172 | - if ( $days < 8 ) { |
|
172 | + if ($days < 8) { |
|
173 | 173 | $format = 'D'; |
174 | 174 | } |
175 | 175 | |
176 | - if ( $days < 32 ) { |
|
176 | + if ($days < 32) { |
|
177 | 177 | $format = 'M j'; |
178 | 178 | } |
179 | 179 | |
180 | - while ( $days > 0 ) { |
|
180 | + while ($days > 0) { |
|
181 | 181 | |
182 | - $key = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
183 | - $label = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
184 | - $return[ $key ] = $label; |
|
182 | + $key = date('Y-m-d', strtotime("-$days days", current_time('timestamp'))); |
|
183 | + $label = date_i18n($format, strtotime("-$days days", current_time('timestamp'))); |
|
184 | + $return[$key] = $label; |
|
185 | 185 | $days--; |
186 | 186 | |
187 | 187 | } |
@@ -193,15 +193,15 @@ discard block |
||
193 | 193 | * Retrieves the weeks in a period |
194 | 194 | * |
195 | 195 | */ |
196 | - public function get_weeks_in_period( $days ) { |
|
196 | + public function get_weeks_in_period($days) { |
|
197 | 197 | |
198 | 198 | $return = array(); |
199 | 199 | |
200 | - while ( $days > 0 ) { |
|
200 | + while ($days > 0) { |
|
201 | 201 | |
202 | - $key = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
203 | - $label = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
204 | - $return[ $key ] = $label; |
|
202 | + $key = date('W', strtotime("-$days days", current_time('timestamp'))); |
|
203 | + $label = date_i18n('Y-m-d', strtotime("-$days days", current_time('timestamp'))); |
|
204 | + $return[$key] = $label; |
|
205 | 205 | $days--; |
206 | 206 | |
207 | 207 | } |
@@ -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 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | |
8 | -defined( 'ABSPATH' ) || exit; |
|
8 | +defined('ABSPATH') || exit; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * GetPaid_Reports_Report_Items Class. |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | * Retrieves the earning sql. |
22 | 22 | * |
23 | 23 | */ |
24 | - public function get_sql( $range ) { |
|
24 | + public function get_sql($range) { |
|
25 | 25 | global $wpdb; |
26 | 26 | |
27 | 27 | $table = $wpdb->prefix . 'getpaid_invoices'; |
28 | 28 | $table2 = $wpdb->prefix . 'getpaid_invoice_items'; |
29 | - $clauses = $this->get_range_sql( $range ); |
|
29 | + $clauses = $this->get_range_sql($range); |
|
30 | 30 | |
31 | 31 | $sql = "SELECT |
32 | 32 | item.item_name AS item_name, |
@@ -43,7 +43,7 @@ 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 | |
@@ -53,30 +53,30 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function prepare_stats() { |
55 | 55 | global $wpdb; |
56 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
57 | - $this->stats = $this->normalize_stats( $this->stats ); |
|
56 | + $this->stats = $wpdb->get_results($this->get_sql($this->get_range())); |
|
57 | + $this->stats = $this->normalize_stats($this->stats); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
61 | 61 | * Normalizes the report stats. |
62 | 62 | * |
63 | 63 | */ |
64 | - public function normalize_stats( $stats ) { |
|
64 | + public function normalize_stats($stats) { |
|
65 | 65 | $normalized = array(); |
66 | 66 | $others = 0; |
67 | 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 | 75 | } else { |
76 | 76 | |
77 | 77 | $normalized[] = array( |
78 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
79 | - 'item_name' => strip_tags( $stat->item_name ), |
|
78 | + 'total' => wpinv_round_amount(wpinv_sanitize_amount($stat->total)), |
|
79 | + 'item_name' => strip_tags($stat->item_name), |
|
80 | 80 | ); |
81 | 81 | |
82 | 82 | } |
@@ -84,11 +84,11 @@ discard block |
||
84 | 84 | $did++; |
85 | 85 | } |
86 | 86 | |
87 | - if ( $others > 0 ) { |
|
87 | + if ($others > 0) { |
|
88 | 88 | |
89 | 89 | $normalized[] = array( |
90 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
91 | - 'item_name' => esc_html__( 'Others', 'invoicing' ), |
|
90 | + 'total' => wpinv_round_amount(wpinv_sanitize_amount($others)), |
|
91 | + 'item_name' => esc_html__('Others', 'invoicing'), |
|
92 | 92 | ); |
93 | 93 | |
94 | 94 | } |
@@ -102,10 +102,10 @@ discard block |
||
102 | 102 | */ |
103 | 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 | 110 | return array( |
111 | 111 | 'data' => $data, |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | * |
120 | 120 | */ |
121 | 121 | public function get_labels() { |
122 | - return wp_list_pluck( $this->stats, 'item_name' ); |
|
122 | + return wp_list_pluck($this->stats, 'item_name'); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
@@ -140,8 +140,8 @@ discard block |
||
140 | 140 | { |
141 | 141 | type: 'doughnut', |
142 | 142 | data: { |
143 | - 'labels': <?php echo wp_json_encode( wp_kses_post_deep( $this->get_labels() ) ); ?>, |
|
144 | - 'datasets': [ <?php echo wp_json_encode( wp_kses_post_deep( $this->get_data() ) ); ?> ] |
|
143 | + 'labels': <?php echo wp_json_encode(wp_kses_post_deep($this->get_labels())); ?>, |
|
144 | + 'datasets': [ <?php echo wp_json_encode(wp_kses_post_deep($this->get_data())); ?> ] |
|
145 | 145 | }, |
146 | 146 | options: { |
147 | 147 | legend: { |