@@ -14,346 +14,346 @@ |
||
14 | 14 | */ |
15 | 15 | class GetPaid_Data_Store_WP { |
16 | 16 | |
17 | - /** |
|
18 | - * Meta type. This should match up with |
|
19 | - * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
20 | - * WP defines 'post', 'user', 'comment', and 'term'. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $meta_type = 'post'; |
|
25 | - |
|
26 | - /** |
|
27 | - * This only needs set if you are using a custom metadata type. |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - protected $object_id_field_for_meta = ''; |
|
32 | - |
|
33 | - /** |
|
34 | - * Data stored in meta keys, but not considered "meta" for an object. |
|
35 | - * |
|
36 | - * @since 1.0.19 |
|
37 | - * |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - protected $internal_meta_keys = array(); |
|
41 | - |
|
42 | - /** |
|
43 | - * Meta data which should exist in the DB, even if empty. |
|
44 | - * |
|
45 | - * @since 1.0.19 |
|
46 | - * |
|
47 | - * @var array |
|
48 | - */ |
|
49 | - protected $must_exist_meta_keys = array(); |
|
50 | - |
|
51 | - /** |
|
52 | - * A map of meta keys to data props. |
|
53 | - * |
|
54 | - * @since 1.0.19 |
|
55 | - * |
|
56 | - * @var array |
|
57 | - */ |
|
58 | - protected $meta_key_to_props = array(); |
|
59 | - |
|
60 | - /** |
|
61 | - * Returns an array of meta for an object. |
|
62 | - * |
|
63 | - * @since 1.0.19 |
|
64 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public function read_meta( &$object ) { |
|
68 | - global $wpdb; |
|
69 | - $db_info = $this->get_db_info(); |
|
70 | - $raw_meta_data = $wpdb->get_results( |
|
71 | - $wpdb->prepare( |
|
72 | - "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
17 | + /** |
|
18 | + * Meta type. This should match up with |
|
19 | + * the types available at https://developer.wordpress.org/reference/functions/add_metadata/. |
|
20 | + * WP defines 'post', 'user', 'comment', and 'term'. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $meta_type = 'post'; |
|
25 | + |
|
26 | + /** |
|
27 | + * This only needs set if you are using a custom metadata type. |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + protected $object_id_field_for_meta = ''; |
|
32 | + |
|
33 | + /** |
|
34 | + * Data stored in meta keys, but not considered "meta" for an object. |
|
35 | + * |
|
36 | + * @since 1.0.19 |
|
37 | + * |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + protected $internal_meta_keys = array(); |
|
41 | + |
|
42 | + /** |
|
43 | + * Meta data which should exist in the DB, even if empty. |
|
44 | + * |
|
45 | + * @since 1.0.19 |
|
46 | + * |
|
47 | + * @var array |
|
48 | + */ |
|
49 | + protected $must_exist_meta_keys = array(); |
|
50 | + |
|
51 | + /** |
|
52 | + * A map of meta keys to data props. |
|
53 | + * |
|
54 | + * @since 1.0.19 |
|
55 | + * |
|
56 | + * @var array |
|
57 | + */ |
|
58 | + protected $meta_key_to_props = array(); |
|
59 | + |
|
60 | + /** |
|
61 | + * Returns an array of meta for an object. |
|
62 | + * |
|
63 | + * @since 1.0.19 |
|
64 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public function read_meta( &$object ) { |
|
68 | + global $wpdb; |
|
69 | + $db_info = $this->get_db_info(); |
|
70 | + $raw_meta_data = $wpdb->get_results( |
|
71 | + $wpdb->prepare( |
|
72 | + "SELECT {$db_info['meta_id_field']} as meta_id, meta_key, meta_value |
|
73 | 73 | FROM {$db_info['table']} |
74 | 74 | WHERE {$db_info['object_id_field']} = %d |
75 | 75 | ORDER BY {$db_info['meta_id_field']}", |
76 | - $object->get_id() |
|
77 | - ) |
|
78 | - ); |
|
79 | - |
|
80 | - $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
81 | - $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
82 | - return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Deletes meta based on meta ID. |
|
87 | - * |
|
88 | - * @since 1.0.19 |
|
89 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
90 | - * @param stdClass $meta (containing at least ->id). |
|
91 | - */ |
|
92 | - public function delete_meta( &$object, $meta ) { |
|
93 | - delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Add new piece of meta. |
|
98 | - * |
|
99 | - * @since 1.0.19 |
|
100 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
101 | - * @param stdClass $meta (containing ->key and ->value). |
|
102 | - * @return int meta ID |
|
103 | - */ |
|
104 | - public function add_meta( &$object, $meta ) { |
|
105 | - return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Update meta. |
|
110 | - * |
|
111 | - * @since 1.0.19 |
|
112 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
113 | - * @param stdClass $meta (containing ->id, ->key and ->value). |
|
114 | - */ |
|
115 | - public function update_meta( &$object, $meta ) { |
|
116 | - update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Table structure is slightly different between meta types, this function will return what we need to know. |
|
121 | - * |
|
122 | - * @since 1.0.19 |
|
123 | - * @return array Array elements: table, object_id_field, meta_id_field |
|
124 | - */ |
|
125 | - protected function get_db_info() { |
|
126 | - global $wpdb; |
|
127 | - |
|
128 | - $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well. |
|
129 | - $table = $wpdb->prefix; |
|
130 | - |
|
131 | - // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
132 | - if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
133 | - $table .= 'getpaid_'; |
|
134 | - } |
|
135 | - |
|
136 | - $table .= $this->meta_type . 'meta'; |
|
137 | - $object_id_field = $this->meta_type . '_id'; |
|
138 | - |
|
139 | - // Figure out our field names. |
|
140 | - if ( 'user' === $this->meta_type ) { |
|
141 | - $meta_id_field = 'umeta_id'; |
|
142 | - $table = $wpdb->usermeta; |
|
143 | - } |
|
144 | - |
|
145 | - if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
146 | - $object_id_field = $this->object_id_field_for_meta; |
|
147 | - } |
|
148 | - |
|
149 | - return array( |
|
150 | - 'table' => $table, |
|
151 | - 'object_id_field' => $object_id_field, |
|
152 | - 'meta_id_field' => $meta_id_field, |
|
153 | - ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
158 | - * addition to all data props with _ prefix. |
|
159 | - * |
|
160 | - * @since 1.0.19 |
|
161 | - * |
|
162 | - * @param string $key Prefix to be added to meta keys. |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - protected function prefix_key( $key ) { |
|
166 | - return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Callback to remove unwanted meta data. |
|
171 | - * |
|
172 | - * @param object $meta Meta object to check if it should be excluded or not. |
|
173 | - * @return bool |
|
174 | - */ |
|
175 | - protected function exclude_internal_meta_keys( $meta ) { |
|
176 | - return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Gets a list of props and meta keys that need updated based on change state |
|
181 | - * or if they are present in the database or not. |
|
182 | - * |
|
183 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
184 | - * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
185 | - * @param string $meta_type The internal WP meta type (post, user, etc). |
|
186 | - * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
187 | - */ |
|
188 | - protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
189 | - $props_to_update = array(); |
|
190 | - $changed_props = $object->get_changes(); |
|
191 | - |
|
192 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
193 | - foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
194 | - if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
195 | - $props_to_update[ $meta_key ] = $prop; |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - return $props_to_update; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Read object data. |
|
204 | - * |
|
205 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
206 | - * @param WP_Post $post_object Post object. |
|
207 | - * @since 1.0.19 |
|
208 | - */ |
|
209 | - protected function read_object_data( &$object, $post_object ) { |
|
210 | - $id = $object->get_id(); |
|
211 | - $props = array(); |
|
212 | - |
|
213 | - foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
214 | - $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
215 | - } |
|
216 | - |
|
217 | - // Set object properties. |
|
218 | - $object->set_props( $props ); |
|
219 | - |
|
220 | - // Gets extra data associated with the object if needed. |
|
221 | - foreach ( $object->get_extra_data_keys() as $key ) { |
|
222 | - $function = 'set_' . $key; |
|
223 | - if ( is_callable( array( $object, $function ) ) ) { |
|
224 | - $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
225 | - } |
|
226 | - } |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class. |
|
231 | - * |
|
232 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
233 | - * @since 1.0.19 |
|
234 | - */ |
|
235 | - protected function update_post_meta( &$object ) { |
|
236 | - |
|
237 | - $updated_props = array(); |
|
238 | - $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
239 | - $object_type = $object->get_object_type(); |
|
240 | - |
|
241 | - foreach ( $props_to_update as $meta_key => $prop ) { |
|
242 | - $value = $object->{"get_$prop"}( 'edit' ); |
|
243 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
244 | - |
|
245 | - $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
246 | - |
|
247 | - if ( $updated ) { |
|
248 | - $updated_props[] = $prop; |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Update meta data in, or delete it from, the database. |
|
257 | - * |
|
258 | - * Avoids storing meta when it's either an empty string or empty array or null. |
|
259 | - * Other empty values such as numeric 0 should still be stored. |
|
260 | - * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
261 | - * |
|
262 | - * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
263 | - * |
|
264 | - * @param GetPaid_Data $object The GetPaid_Data object. |
|
265 | - * @param string $meta_key Meta key to update. |
|
266 | - * @param mixed $meta_value Value to save. |
|
267 | - * |
|
268 | - * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
269 | - * |
|
270 | - * @return bool True if updated/deleted. |
|
271 | - */ |
|
272 | - protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
273 | - if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
274 | - $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
275 | - } else { |
|
276 | - $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
277 | - } |
|
278 | - |
|
279 | - return (bool) $updated; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Return list of internal meta keys. |
|
284 | - * |
|
285 | - * @since 1.0.19 |
|
286 | - * @return array |
|
287 | - */ |
|
288 | - public function get_internal_meta_keys() { |
|
289 | - return $this->internal_meta_keys; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Clear any caches. |
|
294 | - * |
|
295 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
296 | - * @since 1.0.19 |
|
297 | - */ |
|
298 | - protected function clear_caches( &$object ) { |
|
299 | - clean_post_cache( $object->get_id() ); |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Method to delete a data object from the database. |
|
304 | - * |
|
305 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
306 | - * @param array $args Array of args to pass to the delete method. |
|
307 | - * |
|
308 | - * @return void |
|
309 | - */ |
|
310 | - public function delete( &$object, $args = array() ) { |
|
311 | - $id = $object->get_id(); |
|
312 | - $object_type = $object->get_object_type(); |
|
313 | - |
|
314 | - if ( 'invoice' == $object_type ) { |
|
315 | - $object_type = $object->get_type(); |
|
316 | - } |
|
317 | - |
|
318 | - $args = wp_parse_args( |
|
319 | - $args, |
|
320 | - array( |
|
321 | - 'force_delete' => false, |
|
322 | - ) |
|
323 | - ); |
|
324 | - |
|
325 | - if ( ! $id ) { |
|
326 | - return; |
|
327 | - } |
|
328 | - |
|
329 | - if ( $args['force_delete'] ) { |
|
330 | - do_action( "getpaid_delete_$object_type", $object ); |
|
331 | - wp_delete_post( $id, true ); |
|
332 | - $object->set_id( 0 ); |
|
333 | - } else { |
|
334 | - do_action( "getpaid_trash_$object_type", $object ); |
|
335 | - wp_trash_post( $id ); |
|
336 | - $object->set_status( 'trash' ); |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Get the status to save to the post object. |
|
342 | - * |
|
343 | - * |
|
344 | - * @since 1.0.19 |
|
345 | - * @param GetPaid_Data $object GetPaid_Data object. |
|
346 | - * @return string |
|
347 | - */ |
|
348 | - protected function get_post_status( $object ) { |
|
349 | - $object_status = $object->get_status( 'edit' ); |
|
350 | - $object_type = $object->get_object_type(); |
|
351 | - |
|
352 | - if ( ! $object_status ) { |
|
353 | - $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
354 | - } |
|
355 | - |
|
356 | - return $object_status; |
|
357 | - } |
|
76 | + $object->get_id() |
|
77 | + ) |
|
78 | + ); |
|
79 | + |
|
80 | + $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
81 | + $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
82 | + return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Deletes meta based on meta ID. |
|
87 | + * |
|
88 | + * @since 1.0.19 |
|
89 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
90 | + * @param stdClass $meta (containing at least ->id). |
|
91 | + */ |
|
92 | + public function delete_meta( &$object, $meta ) { |
|
93 | + delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Add new piece of meta. |
|
98 | + * |
|
99 | + * @since 1.0.19 |
|
100 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
101 | + * @param stdClass $meta (containing ->key and ->value). |
|
102 | + * @return int meta ID |
|
103 | + */ |
|
104 | + public function add_meta( &$object, $meta ) { |
|
105 | + return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Update meta. |
|
110 | + * |
|
111 | + * @since 1.0.19 |
|
112 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
113 | + * @param stdClass $meta (containing ->id, ->key and ->value). |
|
114 | + */ |
|
115 | + public function update_meta( &$object, $meta ) { |
|
116 | + update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Table structure is slightly different between meta types, this function will return what we need to know. |
|
121 | + * |
|
122 | + * @since 1.0.19 |
|
123 | + * @return array Array elements: table, object_id_field, meta_id_field |
|
124 | + */ |
|
125 | + protected function get_db_info() { |
|
126 | + global $wpdb; |
|
127 | + |
|
128 | + $meta_id_field = 'meta_id'; // users table calls this umeta_id so we need to track this as well. |
|
129 | + $table = $wpdb->prefix; |
|
130 | + |
|
131 | + // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
|
132 | + if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
133 | + $table .= 'getpaid_'; |
|
134 | + } |
|
135 | + |
|
136 | + $table .= $this->meta_type . 'meta'; |
|
137 | + $object_id_field = $this->meta_type . '_id'; |
|
138 | + |
|
139 | + // Figure out our field names. |
|
140 | + if ( 'user' === $this->meta_type ) { |
|
141 | + $meta_id_field = 'umeta_id'; |
|
142 | + $table = $wpdb->usermeta; |
|
143 | + } |
|
144 | + |
|
145 | + if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
146 | + $object_id_field = $this->object_id_field_for_meta; |
|
147 | + } |
|
148 | + |
|
149 | + return array( |
|
150 | + 'table' => $table, |
|
151 | + 'object_id_field' => $object_id_field, |
|
152 | + 'meta_id_field' => $meta_id_field, |
|
153 | + ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Internal meta keys we don't want exposed as part of meta_data. This is in |
|
158 | + * addition to all data props with _ prefix. |
|
159 | + * |
|
160 | + * @since 1.0.19 |
|
161 | + * |
|
162 | + * @param string $key Prefix to be added to meta keys. |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + protected function prefix_key( $key ) { |
|
166 | + return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Callback to remove unwanted meta data. |
|
171 | + * |
|
172 | + * @param object $meta Meta object to check if it should be excluded or not. |
|
173 | + * @return bool |
|
174 | + */ |
|
175 | + protected function exclude_internal_meta_keys( $meta ) { |
|
176 | + return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Gets a list of props and meta keys that need updated based on change state |
|
181 | + * or if they are present in the database or not. |
|
182 | + * |
|
183 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
184 | + * @param array $meta_key_to_props A mapping of meta keys => prop names. |
|
185 | + * @param string $meta_type The internal WP meta type (post, user, etc). |
|
186 | + * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
|
187 | + */ |
|
188 | + protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
189 | + $props_to_update = array(); |
|
190 | + $changed_props = $object->get_changes(); |
|
191 | + |
|
192 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
193 | + foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
194 | + if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
195 | + $props_to_update[ $meta_key ] = $prop; |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + return $props_to_update; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Read object data. |
|
204 | + * |
|
205 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
206 | + * @param WP_Post $post_object Post object. |
|
207 | + * @since 1.0.19 |
|
208 | + */ |
|
209 | + protected function read_object_data( &$object, $post_object ) { |
|
210 | + $id = $object->get_id(); |
|
211 | + $props = array(); |
|
212 | + |
|
213 | + foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
214 | + $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
215 | + } |
|
216 | + |
|
217 | + // Set object properties. |
|
218 | + $object->set_props( $props ); |
|
219 | + |
|
220 | + // Gets extra data associated with the object if needed. |
|
221 | + foreach ( $object->get_extra_data_keys() as $key ) { |
|
222 | + $function = 'set_' . $key; |
|
223 | + if ( is_callable( array( $object, $function ) ) ) { |
|
224 | + $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
225 | + } |
|
226 | + } |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Helper method that updates all the post meta for an object based on it's settings in the GetPaid_Data class. |
|
231 | + * |
|
232 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
233 | + * @since 1.0.19 |
|
234 | + */ |
|
235 | + protected function update_post_meta( &$object ) { |
|
236 | + |
|
237 | + $updated_props = array(); |
|
238 | + $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
239 | + $object_type = $object->get_object_type(); |
|
240 | + |
|
241 | + foreach ( $props_to_update as $meta_key => $prop ) { |
|
242 | + $value = $object->{"get_$prop"}( 'edit' ); |
|
243 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
244 | + |
|
245 | + $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
246 | + |
|
247 | + if ( $updated ) { |
|
248 | + $updated_props[] = $prop; |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Update meta data in, or delete it from, the database. |
|
257 | + * |
|
258 | + * Avoids storing meta when it's either an empty string or empty array or null. |
|
259 | + * Other empty values such as numeric 0 should still be stored. |
|
260 | + * Data-stores can force meta to exist using `must_exist_meta_keys`. |
|
261 | + * |
|
262 | + * Note: WordPress `get_metadata` function returns an empty string when meta data does not exist. |
|
263 | + * |
|
264 | + * @param GetPaid_Data $object The GetPaid_Data object. |
|
265 | + * @param string $meta_key Meta key to update. |
|
266 | + * @param mixed $meta_value Value to save. |
|
267 | + * |
|
268 | + * @since 1.0.19 Added to prevent empty meta being stored unless required. |
|
269 | + * |
|
270 | + * @return bool True if updated/deleted. |
|
271 | + */ |
|
272 | + protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
273 | + if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
274 | + $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
275 | + } else { |
|
276 | + $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
277 | + } |
|
278 | + |
|
279 | + return (bool) $updated; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Return list of internal meta keys. |
|
284 | + * |
|
285 | + * @since 1.0.19 |
|
286 | + * @return array |
|
287 | + */ |
|
288 | + public function get_internal_meta_keys() { |
|
289 | + return $this->internal_meta_keys; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Clear any caches. |
|
294 | + * |
|
295 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
296 | + * @since 1.0.19 |
|
297 | + */ |
|
298 | + protected function clear_caches( &$object ) { |
|
299 | + clean_post_cache( $object->get_id() ); |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Method to delete a data object from the database. |
|
304 | + * |
|
305 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
306 | + * @param array $args Array of args to pass to the delete method. |
|
307 | + * |
|
308 | + * @return void |
|
309 | + */ |
|
310 | + public function delete( &$object, $args = array() ) { |
|
311 | + $id = $object->get_id(); |
|
312 | + $object_type = $object->get_object_type(); |
|
313 | + |
|
314 | + if ( 'invoice' == $object_type ) { |
|
315 | + $object_type = $object->get_type(); |
|
316 | + } |
|
317 | + |
|
318 | + $args = wp_parse_args( |
|
319 | + $args, |
|
320 | + array( |
|
321 | + 'force_delete' => false, |
|
322 | + ) |
|
323 | + ); |
|
324 | + |
|
325 | + if ( ! $id ) { |
|
326 | + return; |
|
327 | + } |
|
328 | + |
|
329 | + if ( $args['force_delete'] ) { |
|
330 | + do_action( "getpaid_delete_$object_type", $object ); |
|
331 | + wp_delete_post( $id, true ); |
|
332 | + $object->set_id( 0 ); |
|
333 | + } else { |
|
334 | + do_action( "getpaid_trash_$object_type", $object ); |
|
335 | + wp_trash_post( $id ); |
|
336 | + $object->set_status( 'trash' ); |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Get the status to save to the post object. |
|
342 | + * |
|
343 | + * |
|
344 | + * @since 1.0.19 |
|
345 | + * @param GetPaid_Data $object GetPaid_Data object. |
|
346 | + * @return string |
|
347 | + */ |
|
348 | + protected function get_post_status( $object ) { |
|
349 | + $object_status = $object->get_status( 'edit' ); |
|
350 | + $object_type = $object->get_object_type(); |
|
351 | + |
|
352 | + if ( ! $object_status ) { |
|
353 | + $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
354 | + } |
|
355 | + |
|
356 | + return $object_status; |
|
357 | + } |
|
358 | 358 | |
359 | 359 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * @version 1.0.19 |
6 | 6 | */ |
7 | 7 | |
8 | -defined( 'ABSPATH' ) || exit; |
|
8 | +defined('ABSPATH') || exit; |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * GetPaid_Data_Store_WP class. |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @param GetPaid_Data $object GetPaid_Data object. |
65 | 65 | * @return array |
66 | 66 | */ |
67 | - public function read_meta( &$object ) { |
|
67 | + public function read_meta(&$object) { |
|
68 | 68 | global $wpdb; |
69 | 69 | $db_info = $this->get_db_info(); |
70 | 70 | $raw_meta_data = $wpdb->get_results( |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | ) |
78 | 78 | ); |
79 | 79 | |
80 | - $this->internal_meta_keys = array_merge( array_map( array( $this, 'prefix_key' ), $object->get_data_keys() ), $this->internal_meta_keys ); |
|
81 | - $meta_data = array_filter( $raw_meta_data, array( $this, 'exclude_internal_meta_keys' ) ); |
|
82 | - return apply_filters( "getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this ); |
|
80 | + $this->internal_meta_keys = array_merge(array_map(array($this, 'prefix_key'), $object->get_data_keys()), $this->internal_meta_keys); |
|
81 | + $meta_data = array_filter($raw_meta_data, array($this, 'exclude_internal_meta_keys')); |
|
82 | + return apply_filters("getpaid_data_store_wp_{$this->meta_type}_read_meta", $meta_data, $object, $this); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | * @param GetPaid_Data $object GetPaid_Data object. |
90 | 90 | * @param stdClass $meta (containing at least ->id). |
91 | 91 | */ |
92 | - public function delete_meta( &$object, $meta ) { |
|
93 | - delete_metadata_by_mid( $this->meta_type, $meta->id ); |
|
92 | + public function delete_meta(&$object, $meta) { |
|
93 | + delete_metadata_by_mid($this->meta_type, $meta->id); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -101,8 +101,8 @@ discard block |
||
101 | 101 | * @param stdClass $meta (containing ->key and ->value). |
102 | 102 | * @return int meta ID |
103 | 103 | */ |
104 | - public function add_meta( &$object, $meta ) { |
|
105 | - return add_metadata( $this->meta_type, $object->get_id(), $meta->key, is_string( $meta->value ) ? wp_slash( $meta->value ) : $meta->value, false ); |
|
104 | + public function add_meta(&$object, $meta) { |
|
105 | + return add_metadata($this->meta_type, $object->get_id(), $meta->key, is_string($meta->value) ? wp_slash($meta->value) : $meta->value, false); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -112,8 +112,8 @@ discard block |
||
112 | 112 | * @param GetPaid_Data $object GetPaid_Data object. |
113 | 113 | * @param stdClass $meta (containing ->id, ->key and ->value). |
114 | 114 | */ |
115 | - public function update_meta( &$object, $meta ) { |
|
116 | - update_metadata_by_mid( $this->meta_type, $meta->id, $meta->value, $meta->key ); |
|
115 | + public function update_meta(&$object, $meta) { |
|
116 | + update_metadata_by_mid($this->meta_type, $meta->id, $meta->value, $meta->key); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | $table = $wpdb->prefix; |
130 | 130 | |
131 | 131 | // If we are dealing with a type of metadata that is not a core type, the table should be prefixed. |
132 | - if ( ! in_array( $this->meta_type, array( 'post', 'user', 'comment', 'term' ), true ) ) { |
|
132 | + if (!in_array($this->meta_type, array('post', 'user', 'comment', 'term'), true)) { |
|
133 | 133 | $table .= 'getpaid_'; |
134 | 134 | } |
135 | 135 | |
@@ -137,12 +137,12 @@ discard block |
||
137 | 137 | $object_id_field = $this->meta_type . '_id'; |
138 | 138 | |
139 | 139 | // Figure out our field names. |
140 | - if ( 'user' === $this->meta_type ) { |
|
140 | + if ('user' === $this->meta_type) { |
|
141 | 141 | $meta_id_field = 'umeta_id'; |
142 | 142 | $table = $wpdb->usermeta; |
143 | 143 | } |
144 | 144 | |
145 | - if ( ! empty( $this->object_id_field_for_meta ) ) { |
|
145 | + if (!empty($this->object_id_field_for_meta)) { |
|
146 | 146 | $object_id_field = $this->object_id_field_for_meta; |
147 | 147 | } |
148 | 148 | |
@@ -162,8 +162,8 @@ discard block |
||
162 | 162 | * @param string $key Prefix to be added to meta keys. |
163 | 163 | * @return string |
164 | 164 | */ |
165 | - protected function prefix_key( $key ) { |
|
166 | - return '_' === substr( $key, 0, 1 ) ? $key : '_' . $key; |
|
165 | + protected function prefix_key($key) { |
|
166 | + return '_' === substr($key, 0, 1) ? $key : '_' . $key; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -172,8 +172,8 @@ discard block |
||
172 | 172 | * @param object $meta Meta object to check if it should be excluded or not. |
173 | 173 | * @return bool |
174 | 174 | */ |
175 | - protected function exclude_internal_meta_keys( $meta ) { |
|
176 | - return ! in_array( $meta->meta_key, $this->internal_meta_keys, true ) && 0 !== stripos( $meta->meta_key, 'wp_' ); |
|
175 | + protected function exclude_internal_meta_keys($meta) { |
|
176 | + return !in_array($meta->meta_key, $this->internal_meta_keys, true) && 0 !== stripos($meta->meta_key, 'wp_'); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | /** |
@@ -185,14 +185,14 @@ discard block |
||
185 | 185 | * @param string $meta_type The internal WP meta type (post, user, etc). |
186 | 186 | * @return array A mapping of meta keys => prop names, filtered by ones that should be updated. |
187 | 187 | */ |
188 | - protected function get_props_to_update( $object, $meta_key_to_props, $meta_type = 'post' ) { |
|
188 | + protected function get_props_to_update($object, $meta_key_to_props, $meta_type = 'post') { |
|
189 | 189 | $props_to_update = array(); |
190 | 190 | $changed_props = $object->get_changes(); |
191 | 191 | |
192 | 192 | // Props should be updated if they are a part of the $changed array or don't exist yet. |
193 | - foreach ( $meta_key_to_props as $meta_key => $prop ) { |
|
194 | - if ( array_key_exists( $prop, $changed_props ) || ! metadata_exists( $meta_type, $object->get_id(), $meta_key ) ) { |
|
195 | - $props_to_update[ $meta_key ] = $prop; |
|
193 | + foreach ($meta_key_to_props as $meta_key => $prop) { |
|
194 | + if (array_key_exists($prop, $changed_props) || !metadata_exists($meta_type, $object->get_id(), $meta_key)) { |
|
195 | + $props_to_update[$meta_key] = $prop; |
|
196 | 196 | } |
197 | 197 | } |
198 | 198 | |
@@ -206,22 +206,22 @@ discard block |
||
206 | 206 | * @param WP_Post $post_object Post object. |
207 | 207 | * @since 1.0.19 |
208 | 208 | */ |
209 | - protected function read_object_data( &$object, $post_object ) { |
|
209 | + protected function read_object_data(&$object, $post_object) { |
|
210 | 210 | $id = $object->get_id(); |
211 | 211 | $props = array(); |
212 | 212 | |
213 | - foreach ( $this->meta_key_to_props as $meta_key => $prop ) { |
|
214 | - $props[ $prop ] = get_post_meta( $id, $meta_key, true ); |
|
213 | + foreach ($this->meta_key_to_props as $meta_key => $prop) { |
|
214 | + $props[$prop] = get_post_meta($id, $meta_key, true); |
|
215 | 215 | } |
216 | 216 | |
217 | 217 | // Set object properties. |
218 | - $object->set_props( $props ); |
|
218 | + $object->set_props($props); |
|
219 | 219 | |
220 | 220 | // Gets extra data associated with the object if needed. |
221 | - foreach ( $object->get_extra_data_keys() as $key ) { |
|
221 | + foreach ($object->get_extra_data_keys() as $key) { |
|
222 | 222 | $function = 'set_' . $key; |
223 | - if ( is_callable( array( $object, $function ) ) ) { |
|
224 | - $object->{$function}( get_post_meta( $object->get_id(), $key, true ) ); |
|
223 | + if (is_callable(array($object, $function))) { |
|
224 | + $object->{$function}(get_post_meta($object->get_id(), $key, true)); |
|
225 | 225 | } |
226 | 226 | } |
227 | 227 | } |
@@ -232,24 +232,24 @@ discard block |
||
232 | 232 | * @param GetPaid_Data $object GetPaid_Data object. |
233 | 233 | * @since 1.0.19 |
234 | 234 | */ |
235 | - protected function update_post_meta( &$object ) { |
|
235 | + protected function update_post_meta(&$object) { |
|
236 | 236 | |
237 | 237 | $updated_props = array(); |
238 | - $props_to_update = $this->get_props_to_update( $object, $this->meta_key_to_props ); |
|
238 | + $props_to_update = $this->get_props_to_update($object, $this->meta_key_to_props); |
|
239 | 239 | $object_type = $object->get_object_type(); |
240 | 240 | |
241 | - foreach ( $props_to_update as $meta_key => $prop ) { |
|
242 | - $value = $object->{"get_$prop"}( 'edit' ); |
|
243 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
241 | + foreach ($props_to_update as $meta_key => $prop) { |
|
242 | + $value = $object->{"get_$prop"}('edit'); |
|
243 | + $value = is_string($value) ? wp_slash($value) : $value; |
|
244 | 244 | |
245 | - $updated = $this->update_or_delete_post_meta( $object, $meta_key, $value ); |
|
245 | + $updated = $this->update_or_delete_post_meta($object, $meta_key, $value); |
|
246 | 246 | |
247 | - if ( $updated ) { |
|
247 | + if ($updated) { |
|
248 | 248 | $updated_props[] = $prop; |
249 | 249 | } |
250 | 250 | } |
251 | 251 | |
252 | - do_action( "getpaid_{$object_type}_object_updated_props", $object, $updated_props ); |
|
252 | + do_action("getpaid_{$object_type}_object_updated_props", $object, $updated_props); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | /** |
@@ -269,11 +269,11 @@ discard block |
||
269 | 269 | * |
270 | 270 | * @return bool True if updated/deleted. |
271 | 271 | */ |
272 | - protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) { |
|
273 | - if ( in_array( $meta_value, array( array(), '', null ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) { |
|
274 | - $updated = delete_post_meta( $object->get_id(), $meta_key ); |
|
272 | + protected function update_or_delete_post_meta($object, $meta_key, $meta_value) { |
|
273 | + if (in_array($meta_value, array(array(), '', null), true) && !in_array($meta_key, $this->must_exist_meta_keys, true)) { |
|
274 | + $updated = delete_post_meta($object->get_id(), $meta_key); |
|
275 | 275 | } else { |
276 | - $updated = update_post_meta( $object->get_id(), $meta_key, $meta_value ); |
|
276 | + $updated = update_post_meta($object->get_id(), $meta_key, $meta_value); |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | return (bool) $updated; |
@@ -295,8 +295,8 @@ discard block |
||
295 | 295 | * @param GetPaid_Data $object GetPaid_Data object. |
296 | 296 | * @since 1.0.19 |
297 | 297 | */ |
298 | - protected function clear_caches( &$object ) { |
|
299 | - clean_post_cache( $object->get_id() ); |
|
298 | + protected function clear_caches(&$object) { |
|
299 | + clean_post_cache($object->get_id()); |
|
300 | 300 | } |
301 | 301 | |
302 | 302 | /** |
@@ -307,33 +307,33 @@ discard block |
||
307 | 307 | * |
308 | 308 | * @return void |
309 | 309 | */ |
310 | - public function delete( &$object, $args = array() ) { |
|
310 | + public function delete(&$object, $args = array()) { |
|
311 | 311 | $id = $object->get_id(); |
312 | 312 | $object_type = $object->get_object_type(); |
313 | 313 | |
314 | - if ( 'invoice' == $object_type ) { |
|
314 | + if ('invoice' == $object_type) { |
|
315 | 315 | $object_type = $object->get_type(); |
316 | 316 | } |
317 | 317 | |
318 | - $args = wp_parse_args( |
|
318 | + $args = wp_parse_args( |
|
319 | 319 | $args, |
320 | 320 | array( |
321 | 321 | 'force_delete' => false, |
322 | 322 | ) |
323 | 323 | ); |
324 | 324 | |
325 | - if ( ! $id ) { |
|
325 | + if (!$id) { |
|
326 | 326 | return; |
327 | 327 | } |
328 | 328 | |
329 | - if ( $args['force_delete'] ) { |
|
330 | - do_action( "getpaid_delete_$object_type", $object ); |
|
331 | - wp_delete_post( $id, true ); |
|
332 | - $object->set_id( 0 ); |
|
329 | + if ($args['force_delete']) { |
|
330 | + do_action("getpaid_delete_$object_type", $object); |
|
331 | + wp_delete_post($id, true); |
|
332 | + $object->set_id(0); |
|
333 | 333 | } else { |
334 | - do_action( "getpaid_trash_$object_type", $object ); |
|
335 | - wp_trash_post( $id ); |
|
336 | - $object->set_status( 'trash' ); |
|
334 | + do_action("getpaid_trash_$object_type", $object); |
|
335 | + wp_trash_post($id); |
|
336 | + $object->set_status('trash'); |
|
337 | 337 | } |
338 | 338 | } |
339 | 339 | |
@@ -345,12 +345,12 @@ discard block |
||
345 | 345 | * @param GetPaid_Data $object GetPaid_Data object. |
346 | 346 | * @return string |
347 | 347 | */ |
348 | - protected function get_post_status( $object ) { |
|
349 | - $object_status = $object->get_status( 'edit' ); |
|
348 | + protected function get_post_status($object) { |
|
349 | + $object_status = $object->get_status('edit'); |
|
350 | 350 | $object_type = $object->get_object_type(); |
351 | 351 | |
352 | - if ( ! $object_status ) { |
|
353 | - $object_status = apply_filters( "getpaid_default_{$object_type}_status", 'draft' ); |
|
352 | + if (!$object_status) { |
|
353 | + $object_status = apply_filters("getpaid_default_{$object_type}_status", 'draft'); |
|
354 | 354 | } |
355 | 355 | |
356 | 356 | return $object_status; |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
8 | - exit; |
|
8 | + exit; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -15,196 +15,196 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Subscription_Data_Store { |
17 | 17 | |
18 | - /** |
|
19 | - * A map of database fields to data types. |
|
20 | - * |
|
21 | - * @since 1.0.19 |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $database_fields_to_data_type = array( |
|
25 | - 'id' => '%d', |
|
26 | - 'customer_id' => '%d', |
|
27 | - 'frequency' => '%d', |
|
28 | - 'period' => '%s', |
|
29 | - 'initial_amount' => '%s', |
|
30 | - 'recurring_amount' => '%s', |
|
31 | - 'bill_times' => '%d', |
|
32 | - 'transaction_id' => '%s', |
|
33 | - 'parent_payment_id' => '%d', |
|
34 | - 'product_id' => '%d', |
|
35 | - 'created' => '%s', |
|
36 | - 'expiration' => '%s', |
|
37 | - 'trial_period' => '%s', |
|
38 | - 'status' => '%s', |
|
39 | - 'profile_id' => '%s', |
|
40 | - ); |
|
41 | - |
|
42 | - /* |
|
18 | + /** |
|
19 | + * A map of database fields to data types. |
|
20 | + * |
|
21 | + * @since 1.0.19 |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $database_fields_to_data_type = array( |
|
25 | + 'id' => '%d', |
|
26 | + 'customer_id' => '%d', |
|
27 | + 'frequency' => '%d', |
|
28 | + 'period' => '%s', |
|
29 | + 'initial_amount' => '%s', |
|
30 | + 'recurring_amount' => '%s', |
|
31 | + 'bill_times' => '%d', |
|
32 | + 'transaction_id' => '%s', |
|
33 | + 'parent_payment_id' => '%d', |
|
34 | + 'product_id' => '%d', |
|
35 | + 'created' => '%s', |
|
36 | + 'expiration' => '%s', |
|
37 | + 'trial_period' => '%s', |
|
38 | + 'status' => '%s', |
|
39 | + 'profile_id' => '%s', |
|
40 | + ); |
|
41 | + |
|
42 | + /* |
|
43 | 43 | |-------------------------------------------------------------------------- |
44 | 44 | | CRUD Methods |
45 | 45 | |-------------------------------------------------------------------------- |
46 | 46 | */ |
47 | 47 | |
48 | - /** |
|
49 | - * Method to create a new subscription in the database. |
|
50 | - * |
|
51 | - * @param WPInv_Subscription $subscription Subscription object. |
|
52 | - */ |
|
53 | - public function create( &$subscription ) { |
|
54 | - global $wpdb; |
|
55 | - |
|
56 | - $values = array(); |
|
57 | - $formats = array(); |
|
58 | - |
|
59 | - $fields = $this->database_fields_to_data_type; |
|
60 | - unset( $fields['id'] ); |
|
61 | - |
|
62 | - foreach ( $fields as $key => $format ) { |
|
63 | - $method = "get_$key"; |
|
64 | - $values[$key] = $subscription->$method( 'edit' ); |
|
65 | - $formats[] = $format; |
|
66 | - } |
|
67 | - |
|
68 | - $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $fields, $formats ); |
|
69 | - |
|
70 | - if ( $result ) { |
|
71 | - $subscription->set_id( $wpdb->insert_id ); |
|
72 | - $subscription->apply_changes(); |
|
73 | - $subscription->clear_cache(); |
|
74 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
75 | - do_action( 'getpaid_new_subscription', $subscription ); |
|
76 | - return true; |
|
77 | - } |
|
78 | - |
|
79 | - return false; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Method to read a subscription from the database. |
|
84 | - * |
|
85 | - * @param WPInv_Subscription $subscription Subscription object. |
|
86 | - * |
|
87 | - */ |
|
88 | - public function read( &$subscription ) { |
|
89 | - global $wpdb; |
|
90 | - |
|
91 | - $subscription->set_defaults(); |
|
92 | - |
|
93 | - if ( ! $subscription->get_id() ) { |
|
94 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
95 | - $subscription->set_id( 0 ); |
|
96 | - return false; |
|
97 | - } |
|
98 | - |
|
99 | - // Maybe retrieve from the cache. |
|
100 | - $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
101 | - |
|
102 | - // If not found, retrieve from the db. |
|
103 | - if ( false === $raw_subscription ) { |
|
104 | - |
|
105 | - $raw_subscription = $wpdb->get_row( |
|
106 | - $wpdb->prepare( |
|
107 | - "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
108 | - $subscription->get_id() |
|
109 | - ) |
|
110 | - ); |
|
111 | - |
|
112 | - // Update the cache with our data |
|
113 | - wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
114 | - |
|
115 | - } |
|
116 | - |
|
117 | - if ( ! $raw_subscription ) { |
|
118 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
119 | - return false; |
|
120 | - } |
|
121 | - |
|
122 | - foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
123 | - $method = "set_$key"; |
|
124 | - $subscription->$method( $raw_subscription->$key ); |
|
125 | - } |
|
126 | - |
|
127 | - $subscription->set_object_read( true ); |
|
128 | - do_action( 'getpaid_read_subscription', $subscription ); |
|
129 | - |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Method to update a subscription in the database. |
|
134 | - * |
|
135 | - * @param WPInv_Subscription $subscription Subscription object. |
|
136 | - */ |
|
137 | - public function update( &$subscription ) { |
|
138 | - global $wpdb; |
|
139 | - |
|
140 | - $changes = $subscription->get_changes(); |
|
141 | - $values = array(); |
|
142 | - $format = array(); |
|
143 | - |
|
144 | - foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
145 | - if ( array_key_exists( $key, $changes ) ) { |
|
146 | - $method = "get_$key"; |
|
147 | - $values[$key] = $subscription->$method( 'edit' ); |
|
148 | - $formats[] = $format; |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - if ( empty( $values ) ) { |
|
153 | - return; |
|
154 | - } |
|
155 | - |
|
156 | - $wpdb->update( |
|
157 | - $wpdb->prefix . 'wpinv_subscriptions', |
|
158 | - $values, |
|
159 | - array( |
|
160 | - 'id' => $subscription->get_id(), |
|
161 | - ), |
|
162 | - $formats, |
|
163 | - '%d' |
|
164 | - ); |
|
165 | - |
|
166 | - // Apply the changes. |
|
167 | - $subscription->apply_changes(); |
|
168 | - |
|
169 | - // Delete cache. |
|
170 | - $subscription->clear_cache(); |
|
171 | - |
|
172 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
173 | - |
|
174 | - // Fire a hook. |
|
175 | - do_action( 'getpaid_update_subscription', $subscription ); |
|
176 | - |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Method to delete a subscription from the database. |
|
181 | - * |
|
182 | - * @param WPInv_Subscription $subscription |
|
183 | - */ |
|
184 | - public function delete( &$subscription ) { |
|
185 | - global $wpdb; |
|
186 | - |
|
187 | - $wpdb->query( |
|
188 | - $wpdb->prepare( |
|
189 | - "DELETE FROM {$wpdb->prefix}getpaid_subscriptions |
|
48 | + /** |
|
49 | + * Method to create a new subscription in the database. |
|
50 | + * |
|
51 | + * @param WPInv_Subscription $subscription Subscription object. |
|
52 | + */ |
|
53 | + public function create( &$subscription ) { |
|
54 | + global $wpdb; |
|
55 | + |
|
56 | + $values = array(); |
|
57 | + $formats = array(); |
|
58 | + |
|
59 | + $fields = $this->database_fields_to_data_type; |
|
60 | + unset( $fields['id'] ); |
|
61 | + |
|
62 | + foreach ( $fields as $key => $format ) { |
|
63 | + $method = "get_$key"; |
|
64 | + $values[$key] = $subscription->$method( 'edit' ); |
|
65 | + $formats[] = $format; |
|
66 | + } |
|
67 | + |
|
68 | + $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $fields, $formats ); |
|
69 | + |
|
70 | + if ( $result ) { |
|
71 | + $subscription->set_id( $wpdb->insert_id ); |
|
72 | + $subscription->apply_changes(); |
|
73 | + $subscription->clear_cache(); |
|
74 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
75 | + do_action( 'getpaid_new_subscription', $subscription ); |
|
76 | + return true; |
|
77 | + } |
|
78 | + |
|
79 | + return false; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Method to read a subscription from the database. |
|
84 | + * |
|
85 | + * @param WPInv_Subscription $subscription Subscription object. |
|
86 | + * |
|
87 | + */ |
|
88 | + public function read( &$subscription ) { |
|
89 | + global $wpdb; |
|
90 | + |
|
91 | + $subscription->set_defaults(); |
|
92 | + |
|
93 | + if ( ! $subscription->get_id() ) { |
|
94 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
95 | + $subscription->set_id( 0 ); |
|
96 | + return false; |
|
97 | + } |
|
98 | + |
|
99 | + // Maybe retrieve from the cache. |
|
100 | + $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
101 | + |
|
102 | + // If not found, retrieve from the db. |
|
103 | + if ( false === $raw_subscription ) { |
|
104 | + |
|
105 | + $raw_subscription = $wpdb->get_row( |
|
106 | + $wpdb->prepare( |
|
107 | + "SELECT * FROM {$wpdb->prefix}wpinv_subscriptions WHERE id = %d", |
|
108 | + $subscription->get_id() |
|
109 | + ) |
|
110 | + ); |
|
111 | + |
|
112 | + // Update the cache with our data |
|
113 | + wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
114 | + |
|
115 | + } |
|
116 | + |
|
117 | + if ( ! $raw_subscription ) { |
|
118 | + $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
119 | + return false; |
|
120 | + } |
|
121 | + |
|
122 | + foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
123 | + $method = "set_$key"; |
|
124 | + $subscription->$method( $raw_subscription->$key ); |
|
125 | + } |
|
126 | + |
|
127 | + $subscription->set_object_read( true ); |
|
128 | + do_action( 'getpaid_read_subscription', $subscription ); |
|
129 | + |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Method to update a subscription in the database. |
|
134 | + * |
|
135 | + * @param WPInv_Subscription $subscription Subscription object. |
|
136 | + */ |
|
137 | + public function update( &$subscription ) { |
|
138 | + global $wpdb; |
|
139 | + |
|
140 | + $changes = $subscription->get_changes(); |
|
141 | + $values = array(); |
|
142 | + $format = array(); |
|
143 | + |
|
144 | + foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
145 | + if ( array_key_exists( $key, $changes ) ) { |
|
146 | + $method = "get_$key"; |
|
147 | + $values[$key] = $subscription->$method( 'edit' ); |
|
148 | + $formats[] = $format; |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + if ( empty( $values ) ) { |
|
153 | + return; |
|
154 | + } |
|
155 | + |
|
156 | + $wpdb->update( |
|
157 | + $wpdb->prefix . 'wpinv_subscriptions', |
|
158 | + $values, |
|
159 | + array( |
|
160 | + 'id' => $subscription->get_id(), |
|
161 | + ), |
|
162 | + $formats, |
|
163 | + '%d' |
|
164 | + ); |
|
165 | + |
|
166 | + // Apply the changes. |
|
167 | + $subscription->apply_changes(); |
|
168 | + |
|
169 | + // Delete cache. |
|
170 | + $subscription->clear_cache(); |
|
171 | + |
|
172 | + update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
173 | + |
|
174 | + // Fire a hook. |
|
175 | + do_action( 'getpaid_update_subscription', $subscription ); |
|
176 | + |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Method to delete a subscription from the database. |
|
181 | + * |
|
182 | + * @param WPInv_Subscription $subscription |
|
183 | + */ |
|
184 | + public function delete( &$subscription ) { |
|
185 | + global $wpdb; |
|
186 | + |
|
187 | + $wpdb->query( |
|
188 | + $wpdb->prepare( |
|
189 | + "DELETE FROM {$wpdb->prefix}getpaid_subscriptions |
|
190 | 190 | WHERE id = %d", |
191 | - $subscription->get_id() |
|
192 | - ) |
|
193 | - ); |
|
191 | + $subscription->get_id() |
|
192 | + ) |
|
193 | + ); |
|
194 | 194 | |
195 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
196 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
195 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
196 | + delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
197 | 197 | |
198 | - // Delete cache. |
|
199 | - $subscription->clear_cache(); |
|
198 | + // Delete cache. |
|
199 | + $subscription->clear_cache(); |
|
200 | 200 | |
201 | - // Fire a hook. |
|
202 | - do_action( 'getpaid_delete_subscription', $subscription ); |
|
201 | + // Fire a hook. |
|
202 | + do_action( 'getpaid_delete_subscription', $subscription ); |
|
203 | 203 | |
204 | - $subscription->set_id( 0 ); |
|
205 | - } |
|
204 | + $subscription->set_id( 0 ); |
|
205 | + } |
|
206 | 206 | |
207 | - /* |
|
207 | + /* |
|
208 | 208 | |-------------------------------------------------------------------------- |
209 | 209 | | Additional Methods |
210 | 210 | |-------------------------------------------------------------------------- |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * GetPaid_Subscription_Data_Store class file. |
5 | 5 | * |
6 | 6 | */ |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -50,29 +50,29 @@ discard block |
||
50 | 50 | * |
51 | 51 | * @param WPInv_Subscription $subscription Subscription object. |
52 | 52 | */ |
53 | - public function create( &$subscription ) { |
|
53 | + public function create(&$subscription) { |
|
54 | 54 | global $wpdb; |
55 | 55 | |
56 | 56 | $values = array(); |
57 | 57 | $formats = array(); |
58 | 58 | |
59 | 59 | $fields = $this->database_fields_to_data_type; |
60 | - unset( $fields['id'] ); |
|
60 | + unset($fields['id']); |
|
61 | 61 | |
62 | - foreach ( $fields as $key => $format ) { |
|
62 | + foreach ($fields as $key => $format) { |
|
63 | 63 | $method = "get_$key"; |
64 | - $values[$key] = $subscription->$method( 'edit' ); |
|
64 | + $values[$key] = $subscription->$method('edit'); |
|
65 | 65 | $formats[] = $format; |
66 | 66 | } |
67 | 67 | |
68 | - $result = $wpdb->insert( $wpdb->prefix . 'wpinv_subscriptions', $fields, $formats ); |
|
68 | + $result = $wpdb->insert($wpdb->prefix . 'wpinv_subscriptions', $fields, $formats); |
|
69 | 69 | |
70 | - if ( $result ) { |
|
71 | - $subscription->set_id( $wpdb->insert_id ); |
|
70 | + if ($result) { |
|
71 | + $subscription->set_id($wpdb->insert_id); |
|
72 | 72 | $subscription->apply_changes(); |
73 | 73 | $subscription->clear_cache(); |
74 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id() ); |
|
75 | - do_action( 'getpaid_new_subscription', $subscription ); |
|
74 | + update_post_meta($subscription->get_parent_invoice_id(), '_wpinv_subscription_id', $subscription->get_id()); |
|
75 | + do_action('getpaid_new_subscription', $subscription); |
|
76 | 76 | return true; |
77 | 77 | } |
78 | 78 | |
@@ -85,22 +85,22 @@ discard block |
||
85 | 85 | * @param WPInv_Subscription $subscription Subscription object. |
86 | 86 | * |
87 | 87 | */ |
88 | - public function read( &$subscription ) { |
|
88 | + public function read(&$subscription) { |
|
89 | 89 | global $wpdb; |
90 | 90 | |
91 | 91 | $subscription->set_defaults(); |
92 | 92 | |
93 | - if ( ! $subscription->get_id() ) { |
|
94 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
95 | - $subscription->set_id( 0 ); |
|
93 | + if (!$subscription->get_id()) { |
|
94 | + $subscription->last_error = __('Invalid subscription ID.', 'invoicing'); |
|
95 | + $subscription->set_id(0); |
|
96 | 96 | return false; |
97 | 97 | } |
98 | 98 | |
99 | 99 | // Maybe retrieve from the cache. |
100 | - $raw_subscription = wp_cache_get( $subscription->get_id(), 'getpaid_subscriptions' ); |
|
100 | + $raw_subscription = wp_cache_get($subscription->get_id(), 'getpaid_subscriptions'); |
|
101 | 101 | |
102 | 102 | // If not found, retrieve from the db. |
103 | - if ( false === $raw_subscription ) { |
|
103 | + if (false === $raw_subscription) { |
|
104 | 104 | |
105 | 105 | $raw_subscription = $wpdb->get_row( |
106 | 106 | $wpdb->prepare( |
@@ -110,22 +110,22 @@ discard block |
||
110 | 110 | ); |
111 | 111 | |
112 | 112 | // Update the cache with our data |
113 | - wp_cache_set( $subscription->get_id(), $raw_subscription, 'getpaid_subscriptions' ); |
|
113 | + wp_cache_set($subscription->get_id(), $raw_subscription, 'getpaid_subscriptions'); |
|
114 | 114 | |
115 | 115 | } |
116 | 116 | |
117 | - if ( ! $raw_subscription ) { |
|
118 | - $subscription->last_error = __( 'Invalid subscription ID.', 'invoicing' ); |
|
117 | + if (!$raw_subscription) { |
|
118 | + $subscription->last_error = __('Invalid subscription ID.', 'invoicing'); |
|
119 | 119 | return false; |
120 | 120 | } |
121 | 121 | |
122 | - foreach ( array_keys( $this->database_fields_to_data_type ) as $key ) { |
|
123 | - $method = "set_$key"; |
|
124 | - $subscription->$method( $raw_subscription->$key ); |
|
122 | + foreach (array_keys($this->database_fields_to_data_type) as $key) { |
|
123 | + $method = "set_$key"; |
|
124 | + $subscription->$method($raw_subscription->$key); |
|
125 | 125 | } |
126 | 126 | |
127 | - $subscription->set_object_read( true ); |
|
128 | - do_action( 'getpaid_read_subscription', $subscription ); |
|
127 | + $subscription->set_object_read(true); |
|
128 | + do_action('getpaid_read_subscription', $subscription); |
|
129 | 129 | |
130 | 130 | } |
131 | 131 | |
@@ -134,22 +134,22 @@ discard block |
||
134 | 134 | * |
135 | 135 | * @param WPInv_Subscription $subscription Subscription object. |
136 | 136 | */ |
137 | - public function update( &$subscription ) { |
|
137 | + public function update(&$subscription) { |
|
138 | 138 | global $wpdb; |
139 | 139 | |
140 | 140 | $changes = $subscription->get_changes(); |
141 | 141 | $values = array(); |
142 | 142 | $format = array(); |
143 | 143 | |
144 | - foreach ( $this->database_fields_to_data_type as $key => $format ) { |
|
145 | - if ( array_key_exists( $key, $changes ) ) { |
|
144 | + foreach ($this->database_fields_to_data_type as $key => $format) { |
|
145 | + if (array_key_exists($key, $changes)) { |
|
146 | 146 | $method = "get_$key"; |
147 | - $values[$key] = $subscription->$method( 'edit' ); |
|
147 | + $values[$key] = $subscription->$method('edit'); |
|
148 | 148 | $formats[] = $format; |
149 | 149 | } |
150 | 150 | } |
151 | 151 | |
152 | - if ( empty( $values ) ) { |
|
152 | + if (empty($values)) { |
|
153 | 153 | return; |
154 | 154 | } |
155 | 155 | |
@@ -169,10 +169,10 @@ discard block |
||
169 | 169 | // Delete cache. |
170 | 170 | $subscription->clear_cache(); |
171 | 171 | |
172 | - update_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id() ); |
|
172 | + update_post_meta($subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id', $subscription->get_profile_id()); |
|
173 | 173 | |
174 | 174 | // Fire a hook. |
175 | - do_action( 'getpaid_update_subscription', $subscription ); |
|
175 | + do_action('getpaid_update_subscription', $subscription); |
|
176 | 176 | |
177 | 177 | } |
178 | 178 | |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | * |
182 | 182 | * @param WPInv_Subscription $subscription |
183 | 183 | */ |
184 | - public function delete( &$subscription ) { |
|
184 | + public function delete(&$subscription) { |
|
185 | 185 | global $wpdb; |
186 | 186 | |
187 | 187 | $wpdb->query( |
@@ -192,16 +192,16 @@ discard block |
||
192 | 192 | ) |
193 | 193 | ); |
194 | 194 | |
195 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id' ); |
|
196 | - delete_post_meta( $subscription->get_parent_invoice_id(), '_wpinv_subscription_id' ); |
|
195 | + delete_post_meta($subscription->get_parent_invoice_id(), '_wpinv_subscr_profile_id'); |
|
196 | + delete_post_meta($subscription->get_parent_invoice_id(), '_wpinv_subscription_id'); |
|
197 | 197 | |
198 | 198 | // Delete cache. |
199 | 199 | $subscription->clear_cache(); |
200 | 200 | |
201 | 201 | // Fire a hook. |
202 | - do_action( 'getpaid_delete_subscription', $subscription ); |
|
202 | + do_action('getpaid_delete_subscription', $subscription); |
|
203 | 203 | |
204 | - $subscription->set_id( 0 ); |
|
204 | + $subscription->set_id(0); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | /* |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
8 | - exit; |
|
8 | + exit; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -15,512 +15,512 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP { |
17 | 17 | |
18 | - /** |
|
19 | - * Data stored in meta keys, but not considered "meta" for a discount. |
|
20 | - * |
|
21 | - * @since 1.0.19 |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $internal_meta_keys = array( |
|
25 | - '_wpinv_subscr_profile_id', |
|
26 | - '_wpinv_subscription_id', |
|
27 | - '_wpinv_taxes', |
|
28 | - '_wpinv_fees', |
|
29 | - '_wpinv_discounts', |
|
30 | - '_wpinv_submission_id', |
|
31 | - '_wpinv_payment_form', |
|
32 | - '_wpinv_is_viewed', |
|
33 | - 'wpinv_email_cc', |
|
34 | - 'wpinv_template' |
|
35 | - ); |
|
36 | - |
|
37 | - /** |
|
38 | - * A map of meta keys to data props. |
|
39 | - * |
|
40 | - * @since 1.0.19 |
|
41 | - * |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected $meta_key_to_props = array( |
|
45 | - '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
46 | - '_wpinv_subscription_id' => 'subscription_id', |
|
47 | - '_wpinv_taxes' => 'taxes', |
|
48 | - '_wpinv_fees' => 'fees', |
|
49 | - '_wpinv_discounts' => 'discounts', |
|
50 | - '_wpinv_submission_id' => 'submission_id', |
|
51 | - '_wpinv_payment_form' => 'payment_form', |
|
52 | - '_wpinv_is_viewed' => 'is_viewed', |
|
53 | - 'wpinv_email_cc' => 'email_cc', |
|
54 | - 'wpinv_template' => 'template', |
|
55 | - ); |
|
56 | - |
|
57 | - /** |
|
58 | - * A map of database fields to data props. |
|
59 | - * |
|
60 | - * @since 1.0.19 |
|
61 | - * |
|
62 | - * @var array |
|
63 | - */ |
|
64 | - protected $database_fields_to_props = array( |
|
65 | - 'post_id' => 'id', |
|
66 | - 'number' => 'number', |
|
67 | - 'currency' => 'currency', |
|
68 | - 'key' => 'key', |
|
69 | - 'type' => 'type', |
|
70 | - 'mode' => 'mode', |
|
71 | - 'user_ip' => 'user_ip', |
|
72 | - 'first_name' => 'first_name', |
|
73 | - 'last_name' => 'last_name', |
|
74 | - 'address' => 'address', |
|
75 | - 'city' => 'city', |
|
76 | - 'state' => 'state', |
|
77 | - 'country' => 'country', |
|
78 | - 'zip' => 'zip', |
|
79 | - 'zip' => 'zip', |
|
80 | - 'adddress_confirmed' => 'address_confirmed', |
|
81 | - 'gateway' => 'gateway', |
|
82 | - 'transaction_id' => 'transaction_id', |
|
83 | - 'currency' => 'currency', |
|
84 | - 'subtotal' => 'subtotal', |
|
85 | - 'tax' => 'total_tax', |
|
86 | - 'fees_total' => 'total_fees', |
|
87 | - 'discount' => 'total_discount', |
|
88 | - 'total' => 'total', |
|
89 | - 'discount_code' => 'discount_code', |
|
90 | - 'disable_taxes' => 'disable_taxes', |
|
91 | - 'due_date' => 'due_date', |
|
92 | - 'completed_date' => 'completed_date', |
|
93 | - 'company' => 'company', |
|
94 | - 'vat_number' => 'vat_number', |
|
95 | - 'vat_rate' => 'vat_rate', |
|
96 | - ); |
|
97 | - |
|
98 | - /* |
|
18 | + /** |
|
19 | + * Data stored in meta keys, but not considered "meta" for a discount. |
|
20 | + * |
|
21 | + * @since 1.0.19 |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $internal_meta_keys = array( |
|
25 | + '_wpinv_subscr_profile_id', |
|
26 | + '_wpinv_subscription_id', |
|
27 | + '_wpinv_taxes', |
|
28 | + '_wpinv_fees', |
|
29 | + '_wpinv_discounts', |
|
30 | + '_wpinv_submission_id', |
|
31 | + '_wpinv_payment_form', |
|
32 | + '_wpinv_is_viewed', |
|
33 | + 'wpinv_email_cc', |
|
34 | + 'wpinv_template' |
|
35 | + ); |
|
36 | + |
|
37 | + /** |
|
38 | + * A map of meta keys to data props. |
|
39 | + * |
|
40 | + * @since 1.0.19 |
|
41 | + * |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected $meta_key_to_props = array( |
|
45 | + '_wpinv_subscr_profile_id' => 'remote_subscription_id', |
|
46 | + '_wpinv_subscription_id' => 'subscription_id', |
|
47 | + '_wpinv_taxes' => 'taxes', |
|
48 | + '_wpinv_fees' => 'fees', |
|
49 | + '_wpinv_discounts' => 'discounts', |
|
50 | + '_wpinv_submission_id' => 'submission_id', |
|
51 | + '_wpinv_payment_form' => 'payment_form', |
|
52 | + '_wpinv_is_viewed' => 'is_viewed', |
|
53 | + 'wpinv_email_cc' => 'email_cc', |
|
54 | + 'wpinv_template' => 'template', |
|
55 | + ); |
|
56 | + |
|
57 | + /** |
|
58 | + * A map of database fields to data props. |
|
59 | + * |
|
60 | + * @since 1.0.19 |
|
61 | + * |
|
62 | + * @var array |
|
63 | + */ |
|
64 | + protected $database_fields_to_props = array( |
|
65 | + 'post_id' => 'id', |
|
66 | + 'number' => 'number', |
|
67 | + 'currency' => 'currency', |
|
68 | + 'key' => 'key', |
|
69 | + 'type' => 'type', |
|
70 | + 'mode' => 'mode', |
|
71 | + 'user_ip' => 'user_ip', |
|
72 | + 'first_name' => 'first_name', |
|
73 | + 'last_name' => 'last_name', |
|
74 | + 'address' => 'address', |
|
75 | + 'city' => 'city', |
|
76 | + 'state' => 'state', |
|
77 | + 'country' => 'country', |
|
78 | + 'zip' => 'zip', |
|
79 | + 'zip' => 'zip', |
|
80 | + 'adddress_confirmed' => 'address_confirmed', |
|
81 | + 'gateway' => 'gateway', |
|
82 | + 'transaction_id' => 'transaction_id', |
|
83 | + 'currency' => 'currency', |
|
84 | + 'subtotal' => 'subtotal', |
|
85 | + 'tax' => 'total_tax', |
|
86 | + 'fees_total' => 'total_fees', |
|
87 | + 'discount' => 'total_discount', |
|
88 | + 'total' => 'total', |
|
89 | + 'discount_code' => 'discount_code', |
|
90 | + 'disable_taxes' => 'disable_taxes', |
|
91 | + 'due_date' => 'due_date', |
|
92 | + 'completed_date' => 'completed_date', |
|
93 | + 'company' => 'company', |
|
94 | + 'vat_number' => 'vat_number', |
|
95 | + 'vat_rate' => 'vat_rate', |
|
96 | + ); |
|
97 | + |
|
98 | + /* |
|
99 | 99 | |-------------------------------------------------------------------------- |
100 | 100 | | CRUD Methods |
101 | 101 | |-------------------------------------------------------------------------- |
102 | 102 | */ |
103 | - /** |
|
104 | - * Method to create a new invoice in the database. |
|
105 | - * |
|
106 | - * @param WPInv_Invoice $invoice Invoice object. |
|
107 | - */ |
|
108 | - public function create( &$invoice ) { |
|
109 | - $invoice->set_version( WPINV_VERSION ); |
|
110 | - $invoice->set_date_created( current_time('mysql') ); |
|
111 | - |
|
112 | - // Create a new post. |
|
113 | - $id = wp_insert_post( |
|
114 | - apply_filters( |
|
115 | - 'getpaid_new_invoice_data', |
|
116 | - array( |
|
117 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
118 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
119 | - 'post_status' => $this->get_post_status( $invoice ), |
|
120 | - 'ping_status' => 'closed', |
|
121 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
122 | - 'post_title' => $invoice->get_title( 'edit' ), |
|
123 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
124 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
125 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
126 | - ) |
|
127 | - ), |
|
128 | - true |
|
129 | - ); |
|
130 | - |
|
131 | - if ( $id && ! is_wp_error( $id ) ) { |
|
132 | - |
|
133 | - // Update the new id and regenerate a title. |
|
134 | - $invoice->set_id( $id ); |
|
135 | - wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) ); |
|
136 | - |
|
137 | - // Ensure both the key and number are set. |
|
138 | - $invoice->get_key(); |
|
139 | - $invoice->get_number(); |
|
140 | - |
|
141 | - // Save special fields and items. |
|
142 | - $this->save_special_fields( $invoice ); |
|
143 | - $this->save_items( $invoice ); |
|
144 | - |
|
145 | - // Update meta data. |
|
146 | - $this->update_post_meta( $invoice ); |
|
147 | - $invoice->save_meta_data(); |
|
148 | - |
|
149 | - // Apply changes. |
|
150 | - $invoice->apply_changes(); |
|
151 | - $this->clear_caches( $invoice ); |
|
152 | - |
|
153 | - // Fires after a new invoice is created. |
|
154 | - do_action( 'getpaid_new_' . $invoice->get_type(), $invoice ); |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - if ( is_wp_error( $id ) ) { |
|
159 | - $invoice->last_error = $id->get_error_message(); |
|
160 | - } |
|
161 | - |
|
162 | - return false; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Method to read an invoice from the database. |
|
167 | - * |
|
168 | - * @param WPInv_Invoice $invoice Invoice object. |
|
169 | - * |
|
170 | - */ |
|
171 | - public function read( &$invoice ) { |
|
172 | - |
|
173 | - $invoice->set_defaults(); |
|
174 | - $invoice_object = get_post( $invoice->get_id() ); |
|
175 | - |
|
176 | - if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
177 | - $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
178 | - $invoice->set_id( 0 ); |
|
179 | - return false; |
|
180 | - } |
|
181 | - |
|
182 | - $invoice->set_props( |
|
183 | - array( |
|
184 | - 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
185 | - 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
186 | - 'status' => $invoice_object->post_status, |
|
187 | - 'author' => $invoice_object->post_author, |
|
188 | - 'description' => $invoice_object->post_excerpt, |
|
189 | - 'parent_id' => $invoice_object->post_parent, |
|
190 | - 'name' => $invoice_object->post_title, |
|
191 | - 'path' => $invoice_object->post_name, |
|
192 | - 'post_type' => $invoice_object->post_type, |
|
193 | - ) |
|
194 | - ); |
|
195 | - |
|
196 | - $invoice->set_type( $invoice_object->post_type ); |
|
197 | - |
|
198 | - $this->read_object_data( $invoice, $invoice_object ); |
|
199 | - $this->add_special_fields( $invoice ); |
|
200 | - $this->add_items( $invoice ); |
|
201 | - $invoice->read_meta_data(); |
|
202 | - $invoice->set_object_read( true ); |
|
203 | - do_action( 'getpaid_read_' . $invoice->get_type(), $invoice ); |
|
204 | - |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Method to update an invoice in the database. |
|
209 | - * |
|
210 | - * @param WPInv_Invoice $invoice Invoice object. |
|
211 | - */ |
|
212 | - public function update( &$invoice ) { |
|
213 | - $invoice->save_meta_data(); |
|
214 | - $invoice->set_version( WPINV_VERSION ); |
|
215 | - |
|
216 | - if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
217 | - $invoice->set_date_created( current_time('mysql') ); |
|
218 | - } |
|
219 | - |
|
220 | - // Ensure both the key and number are set. |
|
221 | - $invoice->get_key(); |
|
222 | - $invoice->get_number(); |
|
223 | - |
|
224 | - // Grab the current status so we can compare. |
|
225 | - $previous_status = get_post_status( $invoice->get_id() ); |
|
226 | - |
|
227 | - $changes = $invoice->get_changes(); |
|
228 | - |
|
229 | - // Only update the post when the post data changes. |
|
230 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
231 | - $post_data = array( |
|
232 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
233 | - 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
234 | - 'post_status' => $invoice->get_status( 'edit' ), |
|
235 | - 'post_title' => $invoice->get_name( 'edit' ), |
|
236 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
237 | - 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
238 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
239 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
240 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
241 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
242 | - ); |
|
243 | - |
|
244 | - /** |
|
245 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
246 | - * to update data, since wp_update_post spawns more calls to the |
|
247 | - * save_post action. |
|
248 | - * |
|
249 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
250 | - * or an update purely from CRUD. |
|
251 | - */ |
|
252 | - if ( doing_action( 'save_post' ) ) { |
|
253 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
254 | - clean_post_cache( $invoice->get_id() ); |
|
255 | - } else { |
|
256 | - wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
257 | - } |
|
258 | - $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
259 | - } |
|
260 | - |
|
261 | - // Update meta data. |
|
262 | - $this->update_post_meta( $invoice ); |
|
263 | - |
|
264 | - // Save special fields and items. |
|
265 | - $this->save_special_fields( $invoice ); |
|
266 | - $this->save_items( $invoice ); |
|
267 | - |
|
268 | - // Apply the changes. |
|
269 | - $invoice->apply_changes(); |
|
270 | - |
|
271 | - // Clear caches. |
|
272 | - $this->clear_caches( $invoice ); |
|
273 | - |
|
274 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
275 | - $new_status = $invoice->get_status( 'edit' ); |
|
276 | - |
|
277 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
278 | - do_action( 'getpaid_new_' . $invoice->get_type(), $invoice ); |
|
279 | - } else { |
|
280 | - do_action( 'getpaid_update_' . $invoice->get_type(), $invoice ); |
|
281 | - } |
|
282 | - |
|
283 | - } |
|
284 | - |
|
285 | - /* |
|
103 | + /** |
|
104 | + * Method to create a new invoice in the database. |
|
105 | + * |
|
106 | + * @param WPInv_Invoice $invoice Invoice object. |
|
107 | + */ |
|
108 | + public function create( &$invoice ) { |
|
109 | + $invoice->set_version( WPINV_VERSION ); |
|
110 | + $invoice->set_date_created( current_time('mysql') ); |
|
111 | + |
|
112 | + // Create a new post. |
|
113 | + $id = wp_insert_post( |
|
114 | + apply_filters( |
|
115 | + 'getpaid_new_invoice_data', |
|
116 | + array( |
|
117 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
118 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
119 | + 'post_status' => $this->get_post_status( $invoice ), |
|
120 | + 'ping_status' => 'closed', |
|
121 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
122 | + 'post_title' => $invoice->get_title( 'edit' ), |
|
123 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
124 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
125 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
126 | + ) |
|
127 | + ), |
|
128 | + true |
|
129 | + ); |
|
130 | + |
|
131 | + if ( $id && ! is_wp_error( $id ) ) { |
|
132 | + |
|
133 | + // Update the new id and regenerate a title. |
|
134 | + $invoice->set_id( $id ); |
|
135 | + wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) ); |
|
136 | + |
|
137 | + // Ensure both the key and number are set. |
|
138 | + $invoice->get_key(); |
|
139 | + $invoice->get_number(); |
|
140 | + |
|
141 | + // Save special fields and items. |
|
142 | + $this->save_special_fields( $invoice ); |
|
143 | + $this->save_items( $invoice ); |
|
144 | + |
|
145 | + // Update meta data. |
|
146 | + $this->update_post_meta( $invoice ); |
|
147 | + $invoice->save_meta_data(); |
|
148 | + |
|
149 | + // Apply changes. |
|
150 | + $invoice->apply_changes(); |
|
151 | + $this->clear_caches( $invoice ); |
|
152 | + |
|
153 | + // Fires after a new invoice is created. |
|
154 | + do_action( 'getpaid_new_' . $invoice->get_type(), $invoice ); |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + if ( is_wp_error( $id ) ) { |
|
159 | + $invoice->last_error = $id->get_error_message(); |
|
160 | + } |
|
161 | + |
|
162 | + return false; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Method to read an invoice from the database. |
|
167 | + * |
|
168 | + * @param WPInv_Invoice $invoice Invoice object. |
|
169 | + * |
|
170 | + */ |
|
171 | + public function read( &$invoice ) { |
|
172 | + |
|
173 | + $invoice->set_defaults(); |
|
174 | + $invoice_object = get_post( $invoice->get_id() ); |
|
175 | + |
|
176 | + if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
177 | + $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
178 | + $invoice->set_id( 0 ); |
|
179 | + return false; |
|
180 | + } |
|
181 | + |
|
182 | + $invoice->set_props( |
|
183 | + array( |
|
184 | + 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
185 | + 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
186 | + 'status' => $invoice_object->post_status, |
|
187 | + 'author' => $invoice_object->post_author, |
|
188 | + 'description' => $invoice_object->post_excerpt, |
|
189 | + 'parent_id' => $invoice_object->post_parent, |
|
190 | + 'name' => $invoice_object->post_title, |
|
191 | + 'path' => $invoice_object->post_name, |
|
192 | + 'post_type' => $invoice_object->post_type, |
|
193 | + ) |
|
194 | + ); |
|
195 | + |
|
196 | + $invoice->set_type( $invoice_object->post_type ); |
|
197 | + |
|
198 | + $this->read_object_data( $invoice, $invoice_object ); |
|
199 | + $this->add_special_fields( $invoice ); |
|
200 | + $this->add_items( $invoice ); |
|
201 | + $invoice->read_meta_data(); |
|
202 | + $invoice->set_object_read( true ); |
|
203 | + do_action( 'getpaid_read_' . $invoice->get_type(), $invoice ); |
|
204 | + |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Method to update an invoice in the database. |
|
209 | + * |
|
210 | + * @param WPInv_Invoice $invoice Invoice object. |
|
211 | + */ |
|
212 | + public function update( &$invoice ) { |
|
213 | + $invoice->save_meta_data(); |
|
214 | + $invoice->set_version( WPINV_VERSION ); |
|
215 | + |
|
216 | + if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
217 | + $invoice->set_date_created( current_time('mysql') ); |
|
218 | + } |
|
219 | + |
|
220 | + // Ensure both the key and number are set. |
|
221 | + $invoice->get_key(); |
|
222 | + $invoice->get_number(); |
|
223 | + |
|
224 | + // Grab the current status so we can compare. |
|
225 | + $previous_status = get_post_status( $invoice->get_id() ); |
|
226 | + |
|
227 | + $changes = $invoice->get_changes(); |
|
228 | + |
|
229 | + // Only update the post when the post data changes. |
|
230 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
231 | + $post_data = array( |
|
232 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
233 | + 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
234 | + 'post_status' => $invoice->get_status( 'edit' ), |
|
235 | + 'post_title' => $invoice->get_name( 'edit' ), |
|
236 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
237 | + 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
238 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
239 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
240 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
241 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
242 | + ); |
|
243 | + |
|
244 | + /** |
|
245 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
246 | + * to update data, since wp_update_post spawns more calls to the |
|
247 | + * save_post action. |
|
248 | + * |
|
249 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
250 | + * or an update purely from CRUD. |
|
251 | + */ |
|
252 | + if ( doing_action( 'save_post' ) ) { |
|
253 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
254 | + clean_post_cache( $invoice->get_id() ); |
|
255 | + } else { |
|
256 | + wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
257 | + } |
|
258 | + $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
259 | + } |
|
260 | + |
|
261 | + // Update meta data. |
|
262 | + $this->update_post_meta( $invoice ); |
|
263 | + |
|
264 | + // Save special fields and items. |
|
265 | + $this->save_special_fields( $invoice ); |
|
266 | + $this->save_items( $invoice ); |
|
267 | + |
|
268 | + // Apply the changes. |
|
269 | + $invoice->apply_changes(); |
|
270 | + |
|
271 | + // Clear caches. |
|
272 | + $this->clear_caches( $invoice ); |
|
273 | + |
|
274 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
275 | + $new_status = $invoice->get_status( 'edit' ); |
|
276 | + |
|
277 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
278 | + do_action( 'getpaid_new_' . $invoice->get_type(), $invoice ); |
|
279 | + } else { |
|
280 | + do_action( 'getpaid_update_' . $invoice->get_type(), $invoice ); |
|
281 | + } |
|
282 | + |
|
283 | + } |
|
284 | + |
|
285 | + /* |
|
286 | 286 | |-------------------------------------------------------------------------- |
287 | 287 | | Additional Methods |
288 | 288 | |-------------------------------------------------------------------------- |
289 | 289 | */ |
290 | 290 | |
291 | - /** |
|
291 | + /** |
|
292 | 292 | * Retrieves special fields and adds to the invoice. |
293 | - * |
|
294 | - * @param WPInv_Invoice $invoice Invoice object. |
|
293 | + * |
|
294 | + * @param WPInv_Invoice $invoice Invoice object. |
|
295 | 295 | */ |
296 | 296 | public function add_special_fields( &$invoice ) { |
297 | - global $wpdb; |
|
297 | + global $wpdb; |
|
298 | 298 | |
299 | - // Maybe retrieve from the cache. |
|
300 | - $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
299 | + // Maybe retrieve from the cache. |
|
300 | + $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
301 | 301 | |
302 | - // If not found, retrieve from the db. |
|
303 | - if ( false === $data ) { |
|
304 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
302 | + // If not found, retrieve from the db. |
|
303 | + if ( false === $data ) { |
|
304 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
305 | 305 | |
306 | - $data = $wpdb->get_row( |
|
307 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
308 | - ARRAY_A |
|
309 | - ); |
|
306 | + $data = $wpdb->get_row( |
|
307 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
308 | + ARRAY_A |
|
309 | + ); |
|
310 | 310 | |
311 | - // Update the cache with our data |
|
312 | - wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
311 | + // Update the cache with our data |
|
312 | + wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
313 | 313 | |
314 | - } |
|
314 | + } |
|
315 | 315 | |
316 | - // Abort if the data does not exist. |
|
317 | - if ( empty( $data ) ) { |
|
318 | - $invoice->set_object_read( true ); |
|
319 | - $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
320 | - return; |
|
321 | - } |
|
316 | + // Abort if the data does not exist. |
|
317 | + if ( empty( $data ) ) { |
|
318 | + $invoice->set_object_read( true ); |
|
319 | + $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
320 | + return; |
|
321 | + } |
|
322 | 322 | |
323 | - $props = array(); |
|
323 | + $props = array(); |
|
324 | 324 | |
325 | - foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
325 | + foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
326 | 326 | |
327 | - if ( $db_field == 'post_id' ) { |
|
328 | - continue; |
|
329 | - } |
|
330 | - |
|
331 | - $props[ $prop ] = $data[ $db_field ]; |
|
332 | - } |
|
333 | - |
|
334 | - $invoice->set_props( $props ); |
|
335 | - |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * Gets a list of special fields that need updated based on change state |
|
340 | - * or if they are present in the database or not. |
|
341 | - * |
|
342 | - * @param WPInv_Invoice $invoice The Invoice object. |
|
343 | - * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
344 | - */ |
|
345 | - protected function get_special_fields_to_update( $invoice ) { |
|
346 | - $fields_to_update = array(); |
|
347 | - $changed_props = $invoice->get_changes(); |
|
348 | - |
|
349 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
350 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
351 | - if ( array_key_exists( $prop, $changed_props ) ) { |
|
352 | - $fields_to_update[ $database_field ] = $prop; |
|
353 | - } |
|
354 | - } |
|
355 | - |
|
356 | - return $fields_to_update; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
361 | - * |
|
362 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
363 | - * @since 1.0.19 |
|
364 | - */ |
|
365 | - protected function update_special_fields( &$invoice ) { |
|
366 | - global $wpdb; |
|
367 | - |
|
368 | - $updated_props = array(); |
|
369 | - $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
370 | - |
|
371 | - foreach ( $fields_to_update as $database_field => $prop ) { |
|
372 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
373 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
374 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
375 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
376 | - } |
|
377 | - |
|
378 | - if ( ! empty( $updated_props ) ) { |
|
379 | - |
|
380 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
381 | - $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
382 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
383 | - do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
384 | - |
|
385 | - } |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Helper method that inserts special fields to the database. |
|
391 | - * |
|
392 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
393 | - * @since 1.0.19 |
|
394 | - */ |
|
395 | - protected function insert_special_fields( &$invoice ) { |
|
396 | - global $wpdb; |
|
397 | - |
|
398 | - $updated_props = array(); |
|
399 | - |
|
400 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
401 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
402 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
403 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
404 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
405 | - } |
|
406 | - |
|
407 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
408 | - $wpdb->insert( $table, $updated_props ); |
|
409 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
410 | - do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
411 | - |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
327 | + if ( $db_field == 'post_id' ) { |
|
328 | + continue; |
|
329 | + } |
|
330 | + |
|
331 | + $props[ $prop ] = $data[ $db_field ]; |
|
332 | + } |
|
333 | + |
|
334 | + $invoice->set_props( $props ); |
|
335 | + |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * Gets a list of special fields that need updated based on change state |
|
340 | + * or if they are present in the database or not. |
|
341 | + * |
|
342 | + * @param WPInv_Invoice $invoice The Invoice object. |
|
343 | + * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
344 | + */ |
|
345 | + protected function get_special_fields_to_update( $invoice ) { |
|
346 | + $fields_to_update = array(); |
|
347 | + $changed_props = $invoice->get_changes(); |
|
348 | + |
|
349 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
350 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
351 | + if ( array_key_exists( $prop, $changed_props ) ) { |
|
352 | + $fields_to_update[ $database_field ] = $prop; |
|
353 | + } |
|
354 | + } |
|
355 | + |
|
356 | + return $fields_to_update; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
361 | + * |
|
362 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
363 | + * @since 1.0.19 |
|
364 | + */ |
|
365 | + protected function update_special_fields( &$invoice ) { |
|
366 | + global $wpdb; |
|
367 | + |
|
368 | + $updated_props = array(); |
|
369 | + $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
370 | + |
|
371 | + foreach ( $fields_to_update as $database_field => $prop ) { |
|
372 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
373 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
374 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
375 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
376 | + } |
|
377 | + |
|
378 | + if ( ! empty( $updated_props ) ) { |
|
379 | + |
|
380 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
381 | + $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
382 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
383 | + do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
384 | + |
|
385 | + } |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Helper method that inserts special fields to the database. |
|
391 | + * |
|
392 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
393 | + * @since 1.0.19 |
|
394 | + */ |
|
395 | + protected function insert_special_fields( &$invoice ) { |
|
396 | + global $wpdb; |
|
397 | + |
|
398 | + $updated_props = array(); |
|
399 | + |
|
400 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
401 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
402 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
403 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
404 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
405 | + } |
|
406 | + |
|
407 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
408 | + $wpdb->insert( $table, $updated_props ); |
|
409 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
410 | + do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
411 | + |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | 415 | * Saves all special fields. |
416 | - * |
|
417 | - * @param WPInv_Invoice $invoice Invoice object. |
|
416 | + * |
|
417 | + * @param WPInv_Invoice $invoice Invoice object. |
|
418 | 418 | */ |
419 | 419 | public function save_special_fields( $invoice ) { |
420 | - global $wpdb; |
|
420 | + global $wpdb; |
|
421 | 421 | |
422 | - // The invoices table. |
|
423 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
424 | - $id = (int) $invoice->get_id(); |
|
422 | + // The invoices table. |
|
423 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
424 | + $id = (int) $invoice->get_id(); |
|
425 | 425 | |
426 | - if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
426 | + if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
427 | 427 | |
428 | - $this->update_special_fields( $invoice ); |
|
428 | + $this->update_special_fields( $invoice ); |
|
429 | 429 | |
430 | - } else { |
|
430 | + } else { |
|
431 | 431 | |
432 | - $this->insert_special_fields( $invoice ); |
|
432 | + $this->insert_special_fields( $invoice ); |
|
433 | 433 | |
434 | - } |
|
434 | + } |
|
435 | 435 | |
436 | - } |
|
436 | + } |
|
437 | 437 | |
438 | - /** |
|
438 | + /** |
|
439 | 439 | * Set's up cart details. |
440 | - * |
|
441 | - * @param WPInv_Invoice $invoice Invoice object. |
|
440 | + * |
|
441 | + * @param WPInv_Invoice $invoice Invoice object. |
|
442 | 442 | */ |
443 | 443 | public function add_items( &$invoice ) { |
444 | - global $wpdb; |
|
444 | + global $wpdb; |
|
445 | 445 | |
446 | - // Maybe retrieve from the cache. |
|
447 | - $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
446 | + // Maybe retrieve from the cache. |
|
447 | + $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
448 | 448 | |
449 | - // If not found, retrieve from the db. |
|
450 | - if ( false === $items ) { |
|
451 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
449 | + // If not found, retrieve from the db. |
|
450 | + if ( false === $items ) { |
|
451 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
452 | 452 | |
453 | - $items = $wpdb->get_results( |
|
454 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
455 | - ); |
|
453 | + $items = $wpdb->get_results( |
|
454 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
455 | + ); |
|
456 | 456 | |
457 | - // Update the cache with our data |
|
458 | - wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
457 | + // Update the cache with our data |
|
458 | + wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
459 | 459 | |
460 | - } |
|
460 | + } |
|
461 | 461 | |
462 | - // Abort if no items found. |
|
462 | + // Abort if no items found. |
|
463 | 463 | if ( empty( $items ) ) { |
464 | 464 | return; |
465 | - } |
|
465 | + } |
|
466 | 466 | |
467 | - foreach ( $items as $item_data ) { |
|
468 | - $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
467 | + foreach ( $items as $item_data ) { |
|
468 | + $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
469 | 469 | |
470 | - // Set item data. |
|
471 | - $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
472 | - $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
473 | - $item->set_name( $item_data->item_name ); |
|
474 | - $item->set_description( $item_data->item_description ); |
|
475 | - $item->set_price( $item_data->item_price ); |
|
476 | - $item->set_quantity( $item_data->quantity ); |
|
477 | - $item->set_item_meta( $item_data->meta ); |
|
470 | + // Set item data. |
|
471 | + $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
472 | + $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
473 | + $item->set_name( $item_data->item_name ); |
|
474 | + $item->set_description( $item_data->item_description ); |
|
475 | + $item->set_price( $item_data->item_price ); |
|
476 | + $item->set_quantity( $item_data->quantity ); |
|
477 | + $item->set_item_meta( $item_data->meta ); |
|
478 | 478 | |
479 | - $invoice->add_item( $item ); |
|
480 | - } |
|
479 | + $invoice->add_item( $item ); |
|
480 | + } |
|
481 | 481 | |
482 | - } |
|
482 | + } |
|
483 | 483 | |
484 | - /** |
|
484 | + /** |
|
485 | 485 | * Saves cart details. |
486 | - * |
|
487 | - * @param WPInv_Invoice $invoice Invoice object. |
|
486 | + * |
|
487 | + * @param WPInv_Invoice $invoice Invoice object. |
|
488 | 488 | */ |
489 | 489 | public function save_items( $invoice ) { |
490 | 490 | |
491 | - // Delete previously existing items. |
|
492 | - $this->delete_items( $invoice ); |
|
491 | + // Delete previously existing items. |
|
492 | + $this->delete_items( $invoice ); |
|
493 | 493 | |
494 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
494 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
495 | 495 | |
496 | - foreach ( $invoice->get_cart_details() as $item_data ) { |
|
497 | - $item_data = array_map( 'maybe_serialize', $item_data ); |
|
498 | - $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
499 | - } |
|
496 | + foreach ( $invoice->get_cart_details() as $item_data ) { |
|
497 | + $item_data = array_map( 'maybe_serialize', $item_data ); |
|
498 | + $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
499 | + } |
|
500 | 500 | |
501 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
502 | - do_action( "getpaid_invoice_save_items", $invoice ); |
|
501 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
502 | + do_action( "getpaid_invoice_save_items", $invoice ); |
|
503 | 503 | |
504 | - } |
|
504 | + } |
|
505 | 505 | |
506 | - /** |
|
506 | + /** |
|
507 | 507 | * Deletes an invoice's cart details from the database. |
508 | - * |
|
509 | - * @param WPInv_Invoice $invoice Invoice object. |
|
508 | + * |
|
509 | + * @param WPInv_Invoice $invoice Invoice object. |
|
510 | 510 | */ |
511 | 511 | public function delete_items( $invoice ) { |
512 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
513 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
514 | - } |
|
512 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
513 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
514 | + } |
|
515 | 515 | |
516 | - /** |
|
516 | + /** |
|
517 | 517 | * Deletes an invoice's special fields from the database. |
518 | - * |
|
519 | - * @param WPInv_Invoice $invoice Invoice object. |
|
518 | + * |
|
519 | + * @param WPInv_Invoice $invoice Invoice object. |
|
520 | 520 | */ |
521 | 521 | public function delete_special_fields( $invoice ) { |
522 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
523 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
522 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
523 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
524 | 524 | } |
525 | 525 | |
526 | 526 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * GetPaid_Invoice_Data_Store class file. |
5 | 5 | * |
6 | 6 | */ |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -105,57 +105,57 @@ discard block |
||
105 | 105 | * |
106 | 106 | * @param WPInv_Invoice $invoice Invoice object. |
107 | 107 | */ |
108 | - public function create( &$invoice ) { |
|
109 | - $invoice->set_version( WPINV_VERSION ); |
|
110 | - $invoice->set_date_created( current_time('mysql') ); |
|
108 | + public function create(&$invoice) { |
|
109 | + $invoice->set_version(WPINV_VERSION); |
|
110 | + $invoice->set_date_created(current_time('mysql')); |
|
111 | 111 | |
112 | 112 | // Create a new post. |
113 | 113 | $id = wp_insert_post( |
114 | 114 | apply_filters( |
115 | 115 | 'getpaid_new_invoice_data', |
116 | 116 | array( |
117 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
118 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
119 | - 'post_status' => $this->get_post_status( $invoice ), |
|
117 | + 'post_date' => $invoice->get_date_created('edit'), |
|
118 | + 'post_type' => $invoice->get_post_type('edit'), |
|
119 | + 'post_status' => $this->get_post_status($invoice), |
|
120 | 120 | 'ping_status' => 'closed', |
121 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
122 | - 'post_title' => $invoice->get_title( 'edit' ), |
|
123 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
124 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
125 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
121 | + 'post_author' => $invoice->get_user_id('edit'), |
|
122 | + 'post_title' => $invoice->get_title('edit'), |
|
123 | + 'post_excerpt' => $invoice->get_description('edit'), |
|
124 | + 'post_parent' => $invoice->get_parent_id('edit'), |
|
125 | + 'post_name' => $invoice->get_path('edit'), |
|
126 | 126 | ) |
127 | 127 | ), |
128 | 128 | true |
129 | 129 | ); |
130 | 130 | |
131 | - if ( $id && ! is_wp_error( $id ) ) { |
|
131 | + if ($id && !is_wp_error($id)) { |
|
132 | 132 | |
133 | 133 | // Update the new id and regenerate a title. |
134 | - $invoice->set_id( $id ); |
|
135 | - wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) ); |
|
134 | + $invoice->set_id($id); |
|
135 | + wp_update_post(array('ID' => $invoice->get_id(), 'post_title' => $invoice->get_number('edit'))); |
|
136 | 136 | |
137 | 137 | // Ensure both the key and number are set. |
138 | 138 | $invoice->get_key(); |
139 | 139 | $invoice->get_number(); |
140 | 140 | |
141 | 141 | // Save special fields and items. |
142 | - $this->save_special_fields( $invoice ); |
|
143 | - $this->save_items( $invoice ); |
|
142 | + $this->save_special_fields($invoice); |
|
143 | + $this->save_items($invoice); |
|
144 | 144 | |
145 | 145 | // Update meta data. |
146 | - $this->update_post_meta( $invoice ); |
|
146 | + $this->update_post_meta($invoice); |
|
147 | 147 | $invoice->save_meta_data(); |
148 | 148 | |
149 | 149 | // Apply changes. |
150 | 150 | $invoice->apply_changes(); |
151 | - $this->clear_caches( $invoice ); |
|
151 | + $this->clear_caches($invoice); |
|
152 | 152 | |
153 | 153 | // Fires after a new invoice is created. |
154 | - do_action( 'getpaid_new_' . $invoice->get_type(), $invoice ); |
|
154 | + do_action('getpaid_new_' . $invoice->get_type(), $invoice); |
|
155 | 155 | return true; |
156 | 156 | } |
157 | 157 | |
158 | - if ( is_wp_error( $id ) ) { |
|
158 | + if (is_wp_error($id)) { |
|
159 | 159 | $invoice->last_error = $id->get_error_message(); |
160 | 160 | } |
161 | 161 | |
@@ -168,14 +168,14 @@ discard block |
||
168 | 168 | * @param WPInv_Invoice $invoice Invoice object. |
169 | 169 | * |
170 | 170 | */ |
171 | - public function read( &$invoice ) { |
|
171 | + public function read(&$invoice) { |
|
172 | 172 | |
173 | 173 | $invoice->set_defaults(); |
174 | - $invoice_object = get_post( $invoice->get_id() ); |
|
174 | + $invoice_object = get_post($invoice->get_id()); |
|
175 | 175 | |
176 | - if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
177 | - $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
178 | - $invoice->set_id( 0 ); |
|
176 | + if (!$invoice->get_id() || !$invoice_object || !getpaid_is_invoice_post_type($invoice_object->post_type)) { |
|
177 | + $invoice->last_error = __('Invalid invoice.', 'invoicing'); |
|
178 | + $invoice->set_id(0); |
|
179 | 179 | return false; |
180 | 180 | } |
181 | 181 | |
@@ -193,14 +193,14 @@ discard block |
||
193 | 193 | ) |
194 | 194 | ); |
195 | 195 | |
196 | - $invoice->set_type( $invoice_object->post_type ); |
|
196 | + $invoice->set_type($invoice_object->post_type); |
|
197 | 197 | |
198 | - $this->read_object_data( $invoice, $invoice_object ); |
|
199 | - $this->add_special_fields( $invoice ); |
|
200 | - $this->add_items( $invoice ); |
|
198 | + $this->read_object_data($invoice, $invoice_object); |
|
199 | + $this->add_special_fields($invoice); |
|
200 | + $this->add_items($invoice); |
|
201 | 201 | $invoice->read_meta_data(); |
202 | - $invoice->set_object_read( true ); |
|
203 | - do_action( 'getpaid_read_' . $invoice->get_type(), $invoice ); |
|
202 | + $invoice->set_object_read(true); |
|
203 | + do_action('getpaid_read_' . $invoice->get_type(), $invoice); |
|
204 | 204 | |
205 | 205 | } |
206 | 206 | |
@@ -209,12 +209,12 @@ discard block |
||
209 | 209 | * |
210 | 210 | * @param WPInv_Invoice $invoice Invoice object. |
211 | 211 | */ |
212 | - public function update( &$invoice ) { |
|
212 | + public function update(&$invoice) { |
|
213 | 213 | $invoice->save_meta_data(); |
214 | - $invoice->set_version( WPINV_VERSION ); |
|
214 | + $invoice->set_version(WPINV_VERSION); |
|
215 | 215 | |
216 | - if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
217 | - $invoice->set_date_created( current_time('mysql') ); |
|
216 | + if (null === $invoice->get_date_created('edit')) { |
|
217 | + $invoice->set_date_created(current_time('mysql')); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | // Ensure both the key and number are set. |
@@ -222,23 +222,23 @@ discard block |
||
222 | 222 | $invoice->get_number(); |
223 | 223 | |
224 | 224 | // Grab the current status so we can compare. |
225 | - $previous_status = get_post_status( $invoice->get_id() ); |
|
225 | + $previous_status = get_post_status($invoice->get_id()); |
|
226 | 226 | |
227 | 227 | $changes = $invoice->get_changes(); |
228 | 228 | |
229 | 229 | // Only update the post when the post data changes. |
230 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
230 | + if (array_intersect(array('date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path'), array_keys($changes))) { |
|
231 | 231 | $post_data = array( |
232 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
233 | - 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
234 | - 'post_status' => $invoice->get_status( 'edit' ), |
|
235 | - 'post_title' => $invoice->get_name( 'edit' ), |
|
236 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
237 | - 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
238 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
239 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
240 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
241 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
232 | + 'post_date' => $invoice->get_date_created('edit'), |
|
233 | + 'post_date_gmt' => $invoice->get_date_created_gmt('edit'), |
|
234 | + 'post_status' => $invoice->get_status('edit'), |
|
235 | + 'post_title' => $invoice->get_name('edit'), |
|
236 | + 'post_author' => $invoice->get_user_id('edit'), |
|
237 | + 'post_modified' => $invoice->get_date_modified('edit'), |
|
238 | + 'post_excerpt' => $invoice->get_description('edit'), |
|
239 | + 'post_parent' => $invoice->get_parent_id('edit'), |
|
240 | + 'post_name' => $invoice->get_path('edit'), |
|
241 | + 'post_type' => $invoice->get_post_type('edit'), |
|
242 | 242 | ); |
243 | 243 | |
244 | 244 | /** |
@@ -249,35 +249,35 @@ discard block |
||
249 | 249 | * This ensures hooks are fired by either WP itself (admin screen save), |
250 | 250 | * or an update purely from CRUD. |
251 | 251 | */ |
252 | - if ( doing_action( 'save_post' ) ) { |
|
253 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
254 | - clean_post_cache( $invoice->get_id() ); |
|
252 | + if (doing_action('save_post')) { |
|
253 | + $GLOBALS['wpdb']->update($GLOBALS['wpdb']->posts, $post_data, array('ID' => $invoice->get_id())); |
|
254 | + clean_post_cache($invoice->get_id()); |
|
255 | 255 | } else { |
256 | - wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
256 | + wp_update_post(array_merge(array('ID' => $invoice->get_id()), $post_data)); |
|
257 | 257 | } |
258 | - $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
258 | + $invoice->read_meta_data(true); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | // Update meta data. |
262 | - $this->update_post_meta( $invoice ); |
|
262 | + $this->update_post_meta($invoice); |
|
263 | 263 | |
264 | 264 | // Save special fields and items. |
265 | - $this->save_special_fields( $invoice ); |
|
266 | - $this->save_items( $invoice ); |
|
265 | + $this->save_special_fields($invoice); |
|
266 | + $this->save_items($invoice); |
|
267 | 267 | |
268 | 268 | // Apply the changes. |
269 | 269 | $invoice->apply_changes(); |
270 | 270 | |
271 | 271 | // Clear caches. |
272 | - $this->clear_caches( $invoice ); |
|
272 | + $this->clear_caches($invoice); |
|
273 | 273 | |
274 | 274 | // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
275 | - $new_status = $invoice->get_status( 'edit' ); |
|
275 | + $new_status = $invoice->get_status('edit'); |
|
276 | 276 | |
277 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
278 | - do_action( 'getpaid_new_' . $invoice->get_type(), $invoice ); |
|
277 | + if ($new_status !== $previous_status && in_array($previous_status, array('new', 'auto-draft', 'draft'), true)) { |
|
278 | + do_action('getpaid_new_' . $invoice->get_type(), $invoice); |
|
279 | 279 | } else { |
280 | - do_action( 'getpaid_update_' . $invoice->get_type(), $invoice ); |
|
280 | + do_action('getpaid_update_' . $invoice->get_type(), $invoice); |
|
281 | 281 | } |
282 | 282 | |
283 | 283 | } |
@@ -293,45 +293,45 @@ discard block |
||
293 | 293 | * |
294 | 294 | * @param WPInv_Invoice $invoice Invoice object. |
295 | 295 | */ |
296 | - public function add_special_fields( &$invoice ) { |
|
296 | + public function add_special_fields(&$invoice) { |
|
297 | 297 | global $wpdb; |
298 | 298 | |
299 | 299 | // Maybe retrieve from the cache. |
300 | - $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
300 | + $data = wp_cache_get($invoice->get_id(), 'getpaid_invoice_special_fields'); |
|
301 | 301 | |
302 | 302 | // If not found, retrieve from the db. |
303 | - if ( false === $data ) { |
|
304 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
303 | + if (false === $data) { |
|
304 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
305 | 305 | |
306 | 306 | $data = $wpdb->get_row( |
307 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
307 | + $wpdb->prepare("SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id()), |
|
308 | 308 | ARRAY_A |
309 | 309 | ); |
310 | 310 | |
311 | 311 | // Update the cache with our data |
312 | - wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
312 | + wp_cache_set($invoice->get_id(), $data, 'getpaid_invoice_special_fields'); |
|
313 | 313 | |
314 | 314 | } |
315 | 315 | |
316 | 316 | // Abort if the data does not exist. |
317 | - if ( empty( $data ) ) { |
|
318 | - $invoice->set_object_read( true ); |
|
319 | - $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
317 | + if (empty($data)) { |
|
318 | + $invoice->set_object_read(true); |
|
319 | + $invoice->set_props(wpinv_get_user_address($invoice->get_user_id())); |
|
320 | 320 | return; |
321 | 321 | } |
322 | 322 | |
323 | 323 | $props = array(); |
324 | 324 | |
325 | - foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
325 | + foreach ($this->database_fields_to_props as $db_field => $prop) { |
|
326 | 326 | |
327 | - if ( $db_field == 'post_id' ) { |
|
327 | + if ($db_field == 'post_id') { |
|
328 | 328 | continue; |
329 | 329 | } |
330 | 330 | |
331 | - $props[ $prop ] = $data[ $db_field ]; |
|
331 | + $props[$prop] = $data[$db_field]; |
|
332 | 332 | } |
333 | 333 | |
334 | - $invoice->set_props( $props ); |
|
334 | + $invoice->set_props($props); |
|
335 | 335 | |
336 | 336 | } |
337 | 337 | |
@@ -342,14 +342,14 @@ discard block |
||
342 | 342 | * @param WPInv_Invoice $invoice The Invoice object. |
343 | 343 | * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
344 | 344 | */ |
345 | - protected function get_special_fields_to_update( $invoice ) { |
|
345 | + protected function get_special_fields_to_update($invoice) { |
|
346 | 346 | $fields_to_update = array(); |
347 | - $changed_props = $invoice->get_changes(); |
|
347 | + $changed_props = $invoice->get_changes(); |
|
348 | 348 | |
349 | 349 | // Props should be updated if they are a part of the $changed array or don't exist yet. |
350 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
351 | - if ( array_key_exists( $prop, $changed_props ) ) { |
|
352 | - $fields_to_update[ $database_field ] = $prop; |
|
350 | + foreach ($this->database_fields_to_props as $database_field => $prop) { |
|
351 | + if (array_key_exists($prop, $changed_props)) { |
|
352 | + $fields_to_update[$database_field] = $prop; |
|
353 | 353 | } |
354 | 354 | } |
355 | 355 | |
@@ -362,25 +362,25 @@ discard block |
||
362 | 362 | * @param WPInv_Invoice $invoice WPInv_Invoice object. |
363 | 363 | * @since 1.0.19 |
364 | 364 | */ |
365 | - protected function update_special_fields( &$invoice ) { |
|
365 | + protected function update_special_fields(&$invoice) { |
|
366 | 366 | global $wpdb; |
367 | 367 | |
368 | 368 | $updated_props = array(); |
369 | - $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
369 | + $fields_to_update = $this->get_special_fields_to_update($invoice); |
|
370 | 370 | |
371 | - foreach ( $fields_to_update as $database_field => $prop ) { |
|
372 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
373 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
374 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
375 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
371 | + foreach ($fields_to_update as $database_field => $prop) { |
|
372 | + $value = $invoice->{"get_$prop"}('edit'); |
|
373 | + $value = is_string($value) ? wp_slash($value) : $value; |
|
374 | + $value = is_bool($value) ? (int) $value : $value; |
|
375 | + $updated_props[$database_field] = maybe_serialize($value); |
|
376 | 376 | } |
377 | 377 | |
378 | - if ( ! empty( $updated_props ) ) { |
|
378 | + if (!empty($updated_props)) { |
|
379 | 379 | |
380 | 380 | $table = $wpdb->prefix . 'getpaid_invoices'; |
381 | - $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
382 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
383 | - do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
381 | + $wpdb->update($table, $updated_props, array('post_id' => $invoice->get_id())); |
|
382 | + wp_cache_delete($invoice->get_id(), 'getpaid_invoice_special_fields'); |
|
383 | + do_action("getpaid_invoice_update_database_fields", $invoice, $updated_props); |
|
384 | 384 | |
385 | 385 | } |
386 | 386 | |
@@ -392,22 +392,22 @@ discard block |
||
392 | 392 | * @param WPInv_Invoice $invoice WPInv_Invoice object. |
393 | 393 | * @since 1.0.19 |
394 | 394 | */ |
395 | - protected function insert_special_fields( &$invoice ) { |
|
395 | + protected function insert_special_fields(&$invoice) { |
|
396 | 396 | global $wpdb; |
397 | 397 | |
398 | - $updated_props = array(); |
|
398 | + $updated_props = array(); |
|
399 | 399 | |
400 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
401 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
402 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
403 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
404 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
400 | + foreach ($this->database_fields_to_props as $database_field => $prop) { |
|
401 | + $value = $invoice->{"get_$prop"}('edit'); |
|
402 | + $value = is_string($value) ? wp_slash($value) : $value; |
|
403 | + $value = is_bool($value) ? (int) $value : $value; |
|
404 | + $updated_props[$database_field] = maybe_serialize($value); |
|
405 | 405 | } |
406 | 406 | |
407 | 407 | $table = $wpdb->prefix . 'getpaid_invoices'; |
408 | - $wpdb->insert( $table, $updated_props ); |
|
409 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
410 | - do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
408 | + $wpdb->insert($table, $updated_props); |
|
409 | + wp_cache_delete($invoice->get_id(), 'getpaid_invoice_special_fields'); |
|
410 | + do_action("getpaid_invoice_insert_database_fields", $invoice, $updated_props); |
|
411 | 411 | |
412 | 412 | } |
413 | 413 | |
@@ -416,20 +416,20 @@ discard block |
||
416 | 416 | * |
417 | 417 | * @param WPInv_Invoice $invoice Invoice object. |
418 | 418 | */ |
419 | - public function save_special_fields( $invoice ) { |
|
419 | + public function save_special_fields($invoice) { |
|
420 | 420 | global $wpdb; |
421 | 421 | |
422 | 422 | // The invoices table. |
423 | 423 | $table = $wpdb->prefix . 'getpaid_invoices'; |
424 | 424 | $id = (int) $invoice->get_id(); |
425 | 425 | |
426 | - if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
426 | + if ($wpdb->get_var("SELECT `post_id` FROM $table WHERE `post_id`= $id")) { |
|
427 | 427 | |
428 | - $this->update_special_fields( $invoice ); |
|
428 | + $this->update_special_fields($invoice); |
|
429 | 429 | |
430 | 430 | } else { |
431 | 431 | |
432 | - $this->insert_special_fields( $invoice ); |
|
432 | + $this->insert_special_fields($invoice); |
|
433 | 433 | |
434 | 434 | } |
435 | 435 | |
@@ -440,43 +440,43 @@ discard block |
||
440 | 440 | * |
441 | 441 | * @param WPInv_Invoice $invoice Invoice object. |
442 | 442 | */ |
443 | - public function add_items( &$invoice ) { |
|
443 | + public function add_items(&$invoice) { |
|
444 | 444 | global $wpdb; |
445 | 445 | |
446 | 446 | // Maybe retrieve from the cache. |
447 | - $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
447 | + $items = wp_cache_get($invoice->get_id(), 'getpaid_invoice_cart_details'); |
|
448 | 448 | |
449 | 449 | // If not found, retrieve from the db. |
450 | - if ( false === $items ) { |
|
451 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
450 | + if (false === $items) { |
|
451 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
452 | 452 | |
453 | 453 | $items = $wpdb->get_results( |
454 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
454 | + $wpdb->prepare("SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id()) |
|
455 | 455 | ); |
456 | 456 | |
457 | 457 | // Update the cache with our data |
458 | - wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
458 | + wp_cache_set($invoice->get_id(), $items, 'getpaid_invoice_cart_details'); |
|
459 | 459 | |
460 | 460 | } |
461 | 461 | |
462 | 462 | // Abort if no items found. |
463 | - if ( empty( $items ) ) { |
|
463 | + if (empty($items)) { |
|
464 | 464 | return; |
465 | 465 | } |
466 | 466 | |
467 | - foreach ( $items as $item_data ) { |
|
468 | - $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
467 | + foreach ($items as $item_data) { |
|
468 | + $item = new GetPaid_Form_Item($item_data->item_id); |
|
469 | 469 | |
470 | 470 | // Set item data. |
471 | - $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
472 | - $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
473 | - $item->set_name( $item_data->item_name ); |
|
474 | - $item->set_description( $item_data->item_description ); |
|
475 | - $item->set_price( $item_data->item_price ); |
|
476 | - $item->set_quantity( $item_data->quantity ); |
|
477 | - $item->set_item_meta( $item_data->meta ); |
|
478 | - |
|
479 | - $invoice->add_item( $item ); |
|
471 | + $item->item_tax = wpinv_sanitize_amount($item_data->tax); |
|
472 | + $item->item_discount = wpinv_sanitize_amount($item_data->discount); |
|
473 | + $item->set_name($item_data->item_name); |
|
474 | + $item->set_description($item_data->item_description); |
|
475 | + $item->set_price($item_data->item_price); |
|
476 | + $item->set_quantity($item_data->quantity); |
|
477 | + $item->set_item_meta($item_data->meta); |
|
478 | + |
|
479 | + $invoice->add_item($item); |
|
480 | 480 | } |
481 | 481 | |
482 | 482 | } |
@@ -486,20 +486,20 @@ discard block |
||
486 | 486 | * |
487 | 487 | * @param WPInv_Invoice $invoice Invoice object. |
488 | 488 | */ |
489 | - public function save_items( $invoice ) { |
|
489 | + public function save_items($invoice) { |
|
490 | 490 | |
491 | 491 | // Delete previously existing items. |
492 | - $this->delete_items( $invoice ); |
|
492 | + $this->delete_items($invoice); |
|
493 | 493 | |
494 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
494 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
495 | 495 | |
496 | - foreach ( $invoice->get_cart_details() as $item_data ) { |
|
497 | - $item_data = array_map( 'maybe_serialize', $item_data ); |
|
498 | - $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
496 | + foreach ($invoice->get_cart_details() as $item_data) { |
|
497 | + $item_data = array_map('maybe_serialize', $item_data); |
|
498 | + $GLOBALS['wpdb']->insert($table, $item_data); |
|
499 | 499 | } |
500 | 500 | |
501 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
502 | - do_action( "getpaid_invoice_save_items", $invoice ); |
|
501 | + wp_cache_delete($invoice->get_id(), 'getpaid_invoice_cart_details'); |
|
502 | + do_action("getpaid_invoice_save_items", $invoice); |
|
503 | 503 | |
504 | 504 | } |
505 | 505 | |
@@ -508,9 +508,9 @@ discard block |
||
508 | 508 | * |
509 | 509 | * @param WPInv_Invoice $invoice Invoice object. |
510 | 510 | */ |
511 | - public function delete_items( $invoice ) { |
|
512 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
513 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
511 | + public function delete_items($invoice) { |
|
512 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
513 | + return $GLOBALS['wpdb']->delete($table, array('post_id' => $invoice->get_id())); |
|
514 | 514 | } |
515 | 515 | |
516 | 516 | /** |
@@ -518,9 +518,9 @@ discard block |
||
518 | 518 | * |
519 | 519 | * @param WPInv_Invoice $invoice Invoice object. |
520 | 520 | */ |
521 | - public function delete_special_fields( $invoice ) { |
|
522 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
523 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
521 | + public function delete_special_fields($invoice) { |
|
522 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
523 | + return $GLOBALS['wpdb']->delete($table, array('post_id' => $invoice->get_id())); |
|
524 | 524 | } |
525 | 525 | |
526 | 526 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if (!defined('ABSPATH')) { |
|
3 | 3 | exit; // Exit if accessed directly |
4 | 4 | } |
5 | 5 | |
@@ -21,79 +21,79 @@ discard block |
||
21 | 21 | public function init() { |
22 | 22 | global $wp_filesystem; |
23 | 23 | |
24 | - if ( empty( $wp_filesystem ) ) { |
|
25 | - require_once( ABSPATH . '/wp-admin/includes/file.php' ); |
|
24 | + if (empty($wp_filesystem)) { |
|
25 | + require_once(ABSPATH . '/wp-admin/includes/file.php'); |
|
26 | 26 | WP_Filesystem(); |
27 | 27 | global $wp_filesystem; |
28 | 28 | } |
29 | 29 | $this->wp_filesystem = $wp_filesystem; |
30 | 30 | |
31 | 31 | $this->export_dir = $this->export_location(); |
32 | - $this->export_url = $this->export_location( true ); |
|
32 | + $this->export_url = $this->export_location(true); |
|
33 | 33 | $this->export = 'invoicing'; |
34 | 34 | $this->filetype = 'csv'; |
35 | 35 | $this->per_page = 20; |
36 | 36 | |
37 | - do_action( 'wpinv_class_reports_init', $this ); |
|
37 | + do_action('wpinv_class_reports_init', $this); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | public function includes() { |
41 | - do_action( 'wpinv_class_reports_includes', $this ); |
|
41 | + do_action('wpinv_class_reports_includes', $this); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | public function actions() { |
45 | - if ( is_admin() ) { |
|
46 | - add_action( 'admin_menu', array( $this, 'add_submenu' ), 20 ); |
|
47 | - add_action( 'wpinv_reports_tab_reports', array( $this, 'reports' ) ); |
|
48 | - add_action( 'wpinv_reports_tab_export', array( $this, 'export' ) ); |
|
49 | - add_action( 'wp_ajax_wpinv_ajax_export', array( $this, 'ajax_export' ) ); |
|
50 | - add_action( 'wp_ajax_wpinv_ajax_discount_use_export', array( $this, 'discount_use_export' ) ); |
|
45 | + if (is_admin()) { |
|
46 | + add_action('admin_menu', array($this, 'add_submenu'), 20); |
|
47 | + add_action('wpinv_reports_tab_reports', array($this, 'reports')); |
|
48 | + add_action('wpinv_reports_tab_export', array($this, 'export')); |
|
49 | + add_action('wp_ajax_wpinv_ajax_export', array($this, 'ajax_export')); |
|
50 | + add_action('wp_ajax_wpinv_ajax_discount_use_export', array($this, 'discount_use_export')); |
|
51 | 51 | |
52 | 52 | // Export Invoices. |
53 | - add_action( 'wpinv_export_set_params_invoices', array( $this, 'set_invoices_export' ) ); |
|
54 | - add_filter( 'wpinv_export_get_columns_invoices', array( $this, 'get_invoices_columns' ) ); |
|
55 | - add_filter( 'wpinv_export_get_data_invoices', array( $this, 'get_invoices_data' ) ); |
|
56 | - add_filter( 'wpinv_get_export_status_invoices', array( $this, 'invoices_export_status' ) ); |
|
53 | + add_action('wpinv_export_set_params_invoices', array($this, 'set_invoices_export')); |
|
54 | + add_filter('wpinv_export_get_columns_invoices', array($this, 'get_invoices_columns')); |
|
55 | + add_filter('wpinv_export_get_data_invoices', array($this, 'get_invoices_data')); |
|
56 | + add_filter('wpinv_get_export_status_invoices', array($this, 'invoices_export_status')); |
|
57 | 57 | |
58 | 58 | // Reports. |
59 | - add_action( 'wpinv_reports_view_earnings', array( $this, 'earnings_report' ) ); |
|
60 | - add_action( 'wpinv_reports_view_gateways', array( $this, 'gateways_report' ) ); |
|
61 | - add_action( 'wpinv_reports_view_items', array( $this, 'items_report' ) ); |
|
62 | - add_action( 'wpinv_reports_view_taxes', array( $this, 'tax_report' ) ); |
|
59 | + add_action('wpinv_reports_view_earnings', array($this, 'earnings_report')); |
|
60 | + add_action('wpinv_reports_view_gateways', array($this, 'gateways_report')); |
|
61 | + add_action('wpinv_reports_view_items', array($this, 'items_report')); |
|
62 | + add_action('wpinv_reports_view_taxes', array($this, 'tax_report')); |
|
63 | 63 | } |
64 | - do_action( 'wpinv_class_reports_actions', $this ); |
|
64 | + do_action('wpinv_class_reports_actions', $this); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | public function add_submenu() { |
68 | 68 | global $wpi_reports_page; |
69 | - $wpi_reports_page = add_submenu_page( 'wpinv', __( 'Reports', 'invoicing' ), __( 'Reports', 'invoicing' ), wpinv_get_capability(), 'wpinv-reports', array( $this, 'reports_page' ) ); |
|
69 | + $wpi_reports_page = add_submenu_page('wpinv', __('Reports', 'invoicing'), __('Reports', 'invoicing'), wpinv_get_capability(), 'wpinv-reports', array($this, 'reports_page')); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | public function reports_page() { |
73 | 73 | |
74 | - if ( !wp_script_is( 'postbox', 'enqueued' ) ) { |
|
75 | - wp_enqueue_script( 'postbox' ); |
|
74 | + if (!wp_script_is('postbox', 'enqueued')) { |
|
75 | + wp_enqueue_script('postbox'); |
|
76 | 76 | } |
77 | 77 | |
78 | - if ( !wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) ) { |
|
79 | - wp_enqueue_script( 'jquery-ui-datepicker' ); |
|
78 | + if (!wp_script_is('jquery-ui-datepicker', 'enqueued')) { |
|
79 | + wp_enqueue_script('jquery-ui-datepicker'); |
|
80 | 80 | } |
81 | 81 | |
82 | - $current_page = admin_url( 'admin.php?page=wpinv-reports' ); |
|
83 | - $active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'reports'; |
|
82 | + $current_page = admin_url('admin.php?page=wpinv-reports'); |
|
83 | + $active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'reports'; |
|
84 | 84 | ?> |
85 | 85 | <div class="wrap wpi-reports-wrap"> |
86 | - <h1><?php echo esc_html( __( 'Reports', 'invoicing' ) ); ?></h1> |
|
86 | + <h1><?php echo esc_html(__('Reports', 'invoicing')); ?></h1> |
|
87 | 87 | <h2 class="nav-tab-wrapper wp-clearfix"> |
88 | - <a href="<?php echo add_query_arg( array( 'tab' => 'reports', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'reports' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Reports', 'invoicing' ); ?></a> |
|
89 | - <a href="<?php echo add_query_arg( array( 'tab' => 'export', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'export' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Export', 'invoicing' ); ?></a> |
|
90 | - <?php do_action( 'wpinv_reports_page_tabs' ); ;?> |
|
88 | + <a href="<?php echo add_query_arg(array('tab' => 'reports', 'settings-updated' => false), $current_page); ?>" class="nav-tab <?php echo $active_tab == 'reports' ? 'nav-tab-active' : ''; ?>"><?php _e('Reports', 'invoicing'); ?></a> |
|
89 | + <a href="<?php echo add_query_arg(array('tab' => 'export', 'settings-updated' => false), $current_page); ?>" class="nav-tab <?php echo $active_tab == 'export' ? 'nav-tab-active' : ''; ?>"><?php _e('Export', 'invoicing'); ?></a> |
|
90 | + <?php do_action('wpinv_reports_page_tabs'); ;?> |
|
91 | 91 | </h2> |
92 | - <div class="wpi-reports-content wpi-reports-<?php echo esc_attr( $active_tab ); ?>"> |
|
92 | + <div class="wpi-reports-content wpi-reports-<?php echo esc_attr($active_tab); ?>"> |
|
93 | 93 | <?php |
94 | - do_action( 'wpinv_reports_page_top' ); |
|
95 | - do_action( 'wpinv_reports_tab_' . $active_tab ); |
|
96 | - do_action( 'wpinv_reports_page_bottom' ); |
|
94 | + do_action('wpinv_reports_page_top'); |
|
95 | + do_action('wpinv_reports_tab_' . $active_tab); |
|
96 | + do_action('wpinv_reports_page_bottom'); |
|
97 | 97 | ?> |
98 | 98 | </div> |
99 | 99 | </div> |
@@ -106,139 +106,139 @@ discard block |
||
106 | 106 | public function reports() { |
107 | 107 | |
108 | 108 | $views = array( |
109 | - 'earnings' => __( 'Earnings', 'invoicing' ), |
|
110 | - 'items' => __( 'Items', 'invoicing' ), |
|
111 | - 'gateways' => __( 'Payment Methods', 'invoicing' ), |
|
112 | - 'taxes' => __( 'Taxes', 'invoicing' ), |
|
109 | + 'earnings' => __('Earnings', 'invoicing'), |
|
110 | + 'items' => __('Items', 'invoicing'), |
|
111 | + 'gateways' => __('Payment Methods', 'invoicing'), |
|
112 | + 'taxes' => __('Taxes', 'invoicing'), |
|
113 | 113 | ); |
114 | 114 | |
115 | - $views = apply_filters( 'wpinv_report_views', $views ); |
|
115 | + $views = apply_filters('wpinv_report_views', $views); |
|
116 | 116 | $current = 'earnings'; |
117 | 117 | |
118 | - if ( isset( $_GET['view'] ) && array_key_exists( $_GET['view'], $views ) ) |
|
118 | + if (isset($_GET['view']) && array_key_exists($_GET['view'], $views)) |
|
119 | 119 | $current = $_GET['view']; |
120 | 120 | |
121 | 121 | ?> |
122 | 122 | <form id="wpinv-reports-filter" method="get" class="tablenav"> |
123 | 123 | <select id="wpinv-reports-view" name="view"> |
124 | - <option value="-1" disabled><?php _e( 'Report Type', 'invoicing' ); ?></option> |
|
125 | - <?php foreach ( $views as $view_id => $label ) : ?> |
|
126 | - <option value="<?php echo esc_attr( $view_id ); ?>" <?php selected( $view_id, $current ); ?>><?php echo $label; ?></option> |
|
124 | + <option value="-1" disabled><?php _e('Report Type', 'invoicing'); ?></option> |
|
125 | + <?php foreach ($views as $view_id => $label) : ?> |
|
126 | + <option value="<?php echo esc_attr($view_id); ?>" <?php selected($view_id, $current); ?>><?php echo $label; ?></option> |
|
127 | 127 | <?php endforeach; ?> |
128 | 128 | </select> |
129 | 129 | |
130 | - <?php do_action( 'wpinv_report_view_actions' ); ?> |
|
130 | + <?php do_action('wpinv_report_view_actions'); ?> |
|
131 | 131 | |
132 | 132 | <input type="hidden" name="page" value="wpinv-reports"/> |
133 | - <?php submit_button( __( 'Show', 'invoicing' ), 'secondary', 'submit', false ); ?> |
|
133 | + <?php submit_button(__('Show', 'invoicing'), 'secondary', 'submit', false); ?> |
|
134 | 134 | </form> |
135 | 135 | <?php |
136 | 136 | |
137 | - do_action( 'wpinv_reports_view_' . $current ); |
|
137 | + do_action('wpinv_reports_view_' . $current); |
|
138 | 138 | |
139 | 139 | } |
140 | 140 | |
141 | 141 | public function export() { |
142 | - $statuses = wpinv_get_invoice_statuses( true ); |
|
143 | - $statuses = array_merge( array( 'any' => __( 'All Statuses', 'invoicing' ) ), $statuses ); |
|
142 | + $statuses = wpinv_get_invoice_statuses(true); |
|
143 | + $statuses = array_merge(array('any' => __('All Statuses', 'invoicing')), $statuses); |
|
144 | 144 | ?> |
145 | 145 | <div class="metabox-holder"> |
146 | 146 | <div id="post-body"> |
147 | 147 | <div id="post-body-content"> |
148 | - <?php do_action( 'wpinv_reports_tab_export_content_top' ); ?> |
|
148 | + <?php do_action('wpinv_reports_tab_export_content_top'); ?> |
|
149 | 149 | |
150 | 150 | <div class="postbox wpi-export-invoices"> |
151 | - <h2 class="hndle ui-sortabled-handle"><span><?php _e( 'Invoices','invoicing' ); ?></span></h2> |
|
151 | + <h2 class="hndle ui-sortabled-handle"><span><?php _e('Invoices', 'invoicing'); ?></span></h2> |
|
152 | 152 | <div class="inside"> |
153 | - <p><?php _e( 'Download a CSV of all payment invoices.', 'invoicing' ); ?></p> |
|
153 | + <p><?php _e('Download a CSV of all payment invoices.', 'invoicing'); ?></p> |
|
154 | 154 | <form id="wpi-export-invoices" class="wpi-export-form" method="post"> |
155 | - <?php echo wpinv_html_date_field( array( |
|
155 | + <?php echo wpinv_html_date_field(array( |
|
156 | 156 | 'id' => 'wpi_export_from_date', |
157 | 157 | 'name' => 'from_date', |
158 | 158 | 'data' => array( |
159 | 159 | 'dateFormat' => 'yy-mm-dd' |
160 | 160 | ), |
161 | - 'placeholder' => __( 'From date', 'invoicing' ) ) |
|
161 | + 'placeholder' => __('From date', 'invoicing') ) |
|
162 | 162 | ); ?> |
163 | - <?php echo wpinv_html_date_field( array( |
|
163 | + <?php echo wpinv_html_date_field(array( |
|
164 | 164 | 'id' => 'wpi_export_to_date', |
165 | 165 | 'name' => 'to_date', |
166 | 166 | 'data' => array( |
167 | 167 | 'dateFormat' => 'yy-mm-dd' |
168 | 168 | ), |
169 | - 'placeholder' => __( 'To date', 'invoicing' ) ) |
|
169 | + 'placeholder' => __('To date', 'invoicing') ) |
|
170 | 170 | ); ?> |
171 | 171 | <span id="wpinv-status-wrap"> |
172 | - <?php echo wpinv_html_select( array( |
|
172 | + <?php echo wpinv_html_select(array( |
|
173 | 173 | 'options' => $statuses, |
174 | 174 | 'name' => 'status', |
175 | 175 | 'id' => 'wpi_export_status', |
176 | 176 | 'show_option_all' => false, |
177 | 177 | 'show_option_none' => false, |
178 | 178 | 'class' => 'wpi_select2', |
179 | - ) ); ?> |
|
180 | - <?php wp_nonce_field( 'wpi_ajax_export', 'wpi_ajax_export' ); ?> |
|
179 | + )); ?> |
|
180 | + <?php wp_nonce_field('wpi_ajax_export', 'wpi_ajax_export'); ?> |
|
181 | 181 | </span> |
182 | 182 | <span id="wpinv-submit-wrap"> |
183 | 183 | <input type="hidden" value="invoices" name="export" /> |
184 | - <input type="submit" value="<?php _e( 'Generate CSV', 'invoicing' ); ?>" class="button-primary" /> |
|
184 | + <input type="submit" value="<?php _e('Generate CSV', 'invoicing'); ?>" class="button-primary" /> |
|
185 | 185 | </span> |
186 | 186 | </form> |
187 | 187 | </div> |
188 | 188 | </div> |
189 | 189 | |
190 | 190 | <div class="postbox wpi-export-discount-uses"> |
191 | - <h2 class="hndle ui-sortabled-handle"><span><?php _e( 'Discount Use','invoicing' ); ?></span></h2> |
|
191 | + <h2 class="hndle ui-sortabled-handle"><span><?php _e('Discount Use', 'invoicing'); ?></span></h2> |
|
192 | 192 | <div class="inside"> |
193 | - <p><?php _e( 'Download a CSV of discount uses.', 'invoicing' ); ?></p> |
|
194 | - <a class="button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-ajax.php?action=wpinv_ajax_discount_use_export' ), 'wpi_discount_ajax_export', 'wpi_discount_ajax_export' ) ); ?>"><?php _e( 'Generate CSV', 'invoicing' ); ?></a> |
|
193 | + <p><?php _e('Download a CSV of discount uses.', 'invoicing'); ?></p> |
|
194 | + <a class="button-primary" href="<?php echo esc_url(wp_nonce_url(admin_url('admin-ajax.php?action=wpinv_ajax_discount_use_export'), 'wpi_discount_ajax_export', 'wpi_discount_ajax_export')); ?>"><?php _e('Generate CSV', 'invoicing'); ?></a> |
|
195 | 195 | </div> |
196 | 196 | </div> |
197 | 197 | |
198 | - <?php do_action( 'wpinv_reports_tab_export_content_bottom' ); ?> |
|
198 | + <?php do_action('wpinv_reports_tab_export_content_bottom'); ?> |
|
199 | 199 | </div> |
200 | 200 | </div> |
201 | 201 | </div> |
202 | 202 | <?php |
203 | 203 | } |
204 | 204 | |
205 | - public function export_location( $relative = false ) { |
|
205 | + public function export_location($relative = false) { |
|
206 | 206 | $upload_dir = wp_upload_dir(); |
207 | - $export_location = $relative ? trailingslashit( $upload_dir['baseurl'] ) . 'cache' : trailingslashit( $upload_dir['basedir'] ) . 'cache'; |
|
208 | - $export_location = apply_filters( 'wpinv_export_location', $export_location, $relative ); |
|
207 | + $export_location = $relative ? trailingslashit($upload_dir['baseurl']) . 'cache' : trailingslashit($upload_dir['basedir']) . 'cache'; |
|
208 | + $export_location = apply_filters('wpinv_export_location', $export_location, $relative); |
|
209 | 209 | |
210 | - return trailingslashit( $export_location ); |
|
210 | + return trailingslashit($export_location); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | public function check_export_location() { |
214 | 214 | try { |
215 | - if ( empty( $this->wp_filesystem ) ) { |
|
216 | - return __( 'Filesystem ERROR: Could not access filesystem.', 'invoicing' ); |
|
215 | + if (empty($this->wp_filesystem)) { |
|
216 | + return __('Filesystem ERROR: Could not access filesystem.', 'invoicing'); |
|
217 | 217 | } |
218 | 218 | |
219 | - if ( is_wp_error( $this->wp_filesystem ) ) { |
|
220 | - return __( 'Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'invoicing' ); |
|
219 | + if (is_wp_error($this->wp_filesystem)) { |
|
220 | + return __('Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'invoicing'); |
|
221 | 221 | } |
222 | 222 | |
223 | - $is_dir = $this->wp_filesystem->is_dir( $this->export_dir ); |
|
224 | - $is_writeable = $is_dir && is_writeable( $this->export_dir ); |
|
223 | + $is_dir = $this->wp_filesystem->is_dir($this->export_dir); |
|
224 | + $is_writeable = $is_dir && is_writeable($this->export_dir); |
|
225 | 225 | |
226 | - if ( $is_dir && $is_writeable ) { |
|
226 | + if ($is_dir && $is_writeable) { |
|
227 | 227 | return true; |
228 | - } else if ( $is_dir && !$is_writeable ) { |
|
229 | - if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) { |
|
230 | - return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'invoicing' ), $this->export_dir ); |
|
228 | + } else if ($is_dir && !$is_writeable) { |
|
229 | + if (!$this->wp_filesystem->chmod($this->export_dir, FS_CHMOD_DIR)) { |
|
230 | + return wp_sprintf(__('Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'invoicing'), $this->export_dir); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | return true; |
234 | 234 | } else { |
235 | - if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) { |
|
236 | - return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'invoicing' ), $this->export_dir ); |
|
235 | + if (!$this->wp_filesystem->mkdir($this->export_dir, FS_CHMOD_DIR)) { |
|
236 | + return wp_sprintf(__('Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'invoicing'), $this->export_dir); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | return true; |
240 | 240 | } |
241 | - } catch ( Exception $e ) { |
|
241 | + } catch (Exception $e) { |
|
242 | 242 | return $e->getMessage(); |
243 | 243 | } |
244 | 244 | } |
@@ -246,59 +246,59 @@ discard block |
||
246 | 246 | public function ajax_export() { |
247 | 247 | $response = array(); |
248 | 248 | $response['success'] = false; |
249 | - $response['msg'] = __( 'Invalid export request found.', 'invoicing' ); |
|
249 | + $response['msg'] = __('Invalid export request found.', 'invoicing'); |
|
250 | 250 | |
251 | - if ( empty( $_POST['data'] ) || ! wpinv_current_user_can_manage_invoicing() ) { |
|
252 | - wp_send_json( $response ); |
|
251 | + if (empty($_POST['data']) || !wpinv_current_user_can_manage_invoicing()) { |
|
252 | + wp_send_json($response); |
|
253 | 253 | } |
254 | 254 | |
255 | - parse_str( $_POST['data'], $data ); |
|
255 | + parse_str($_POST['data'], $data); |
|
256 | 256 | |
257 | - $data['step'] = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; |
|
257 | + $data['step'] = !empty($_POST['step']) ? absint($_POST['step']) : 1; |
|
258 | 258 | |
259 | - $_REQUEST = (array)$data; |
|
260 | - if ( !( !empty( $_REQUEST['wpi_ajax_export'] ) && wp_verify_nonce( $_REQUEST['wpi_ajax_export'], 'wpi_ajax_export' ) ) ) { |
|
261 | - $response['msg'] = __( 'Security check failed.', 'invoicing' ); |
|
262 | - wp_send_json( $response ); |
|
259 | + $_REQUEST = (array) $data; |
|
260 | + if (!(!empty($_REQUEST['wpi_ajax_export']) && wp_verify_nonce($_REQUEST['wpi_ajax_export'], 'wpi_ajax_export'))) { |
|
261 | + $response['msg'] = __('Security check failed.', 'invoicing'); |
|
262 | + wp_send_json($response); |
|
263 | 263 | } |
264 | 264 | |
265 | - if ( ( $error = $this->check_export_location( true ) ) !== true ) { |
|
266 | - $response['msg'] = __( 'Filesystem ERROR: ' . $error, 'invoicing' ); |
|
267 | - wp_send_json( $response ); |
|
265 | + if (($error = $this->check_export_location(true)) !== true) { |
|
266 | + $response['msg'] = __('Filesystem ERROR: ' . $error, 'invoicing'); |
|
267 | + wp_send_json($response); |
|
268 | 268 | } |
269 | 269 | |
270 | - $this->set_export_params( $_REQUEST ); |
|
270 | + $this->set_export_params($_REQUEST); |
|
271 | 271 | |
272 | 272 | $return = $this->process_export_step(); |
273 | 273 | $done = $this->get_export_status(); |
274 | 274 | |
275 | - if ( $return ) { |
|
275 | + if ($return) { |
|
276 | 276 | $this->step += 1; |
277 | 277 | |
278 | 278 | $response['success'] = true; |
279 | 279 | $response['msg'] = ''; |
280 | 280 | |
281 | - if ( $done >= 100 ) { |
|
281 | + if ($done >= 100) { |
|
282 | 282 | $this->step = 'done'; |
283 | - $new_filename = 'wpi-' . $this->export . '-' . date( 'y-m-d-H-i' ) . '.' . $this->filetype; |
|
283 | + $new_filename = 'wpi-' . $this->export . '-' . date('y-m-d-H-i') . '.' . $this->filetype; |
|
284 | 284 | $new_file = $this->export_dir . $new_filename; |
285 | 285 | |
286 | - if ( file_exists( $this->file ) ) { |
|
287 | - $this->wp_filesystem->move( $this->file, $new_file, true ); |
|
286 | + if (file_exists($this->file)) { |
|
287 | + $this->wp_filesystem->move($this->file, $new_file, true); |
|
288 | 288 | } |
289 | 289 | |
290 | - if ( file_exists( $new_file ) ) { |
|
291 | - $response['data']['file'] = array( 'u' => $this->export_url . $new_filename, 's' => size_format( filesize( $new_file ), 2 ) ); |
|
290 | + if (file_exists($new_file)) { |
|
291 | + $response['data']['file'] = array('u' => $this->export_url . $new_filename, 's' => size_format(filesize($new_file), 2)); |
|
292 | 292 | } |
293 | 293 | } |
294 | 294 | |
295 | 295 | $response['data']['step'] = $this->step; |
296 | 296 | $response['data']['done'] = $done; |
297 | 297 | } else { |
298 | - $response['msg'] = __( 'No data found for export.', 'invoicing' ); |
|
298 | + $response['msg'] = __('No data found for export.', 'invoicing'); |
|
299 | 299 | } |
300 | 300 | |
301 | - wp_send_json( $response ); |
|
301 | + wp_send_json($response); |
|
302 | 302 | } |
303 | 303 | |
304 | 304 | /** |
@@ -306,7 +306,7 @@ discard block |
||
306 | 306 | */ |
307 | 307 | public function discount_use_export() { |
308 | 308 | |
309 | - if ( ! wp_verify_nonce( $_GET['wpi_discount_ajax_export'], 'wpi_discount_ajax_export' ) || ! wpinv_current_user_can_manage_invoicing() ) { |
|
309 | + if (!wp_verify_nonce($_GET['wpi_discount_ajax_export'], 'wpi_discount_ajax_export') || !wpinv_current_user_can_manage_invoicing()) { |
|
310 | 310 | wp_die( -1, 403 ); |
311 | 311 | } |
312 | 312 | |
@@ -316,115 +316,115 @@ discard block |
||
316 | 316 | 'posts_per_page' => -1, |
317 | 317 | ); |
318 | 318 | |
319 | - $discounts = get_posts( $args ); |
|
319 | + $discounts = get_posts($args); |
|
320 | 320 | |
321 | - if ( empty( $discounts ) ) { |
|
322 | - die ( __( 'You have not set up any discounts', 'invoicing' ) ); |
|
321 | + if (empty($discounts)) { |
|
322 | + die (__('You have not set up any discounts', 'invoicing')); |
|
323 | 323 | } |
324 | 324 | |
325 | - $output = fopen( 'php://output', 'w' ) or die( 'Unsupported server' ); |
|
325 | + $output = fopen('php://output', 'w') or die('Unsupported server'); |
|
326 | 326 | |
327 | 327 | // Let the browser know what content we're streaming and how it should save the content. |
328 | 328 | $name = time(); |
329 | - header( "Content-Type:application/csv" ); |
|
330 | - header( "Content-Disposition:attachment;filename=noptin-subscribers-$name.csv" ); |
|
329 | + header("Content-Type:application/csv"); |
|
330 | + header("Content-Disposition:attachment;filename=noptin-subscribers-$name.csv"); |
|
331 | 331 | |
332 | 332 | // Output the csv column headers. |
333 | 333 | fputcsv( |
334 | 334 | $output, |
335 | 335 | array( |
336 | - __( 'Discount Id', 'invoicing' ), |
|
337 | - __( 'Discount Code', 'invoicing' ), |
|
338 | - __( 'Discount Type', 'invoicing' ), |
|
339 | - __( 'Discount Amount', 'invoicing' ), |
|
340 | - __( 'Uses', 'invoicing' ), |
|
336 | + __('Discount Id', 'invoicing'), |
|
337 | + __('Discount Code', 'invoicing'), |
|
338 | + __('Discount Type', 'invoicing'), |
|
339 | + __('Discount Amount', 'invoicing'), |
|
340 | + __('Uses', 'invoicing'), |
|
341 | 341 | ) |
342 | 342 | ); |
343 | 343 | |
344 | - foreach ( $discounts as $discount ) { |
|
344 | + foreach ($discounts as $discount) { |
|
345 | 345 | |
346 | 346 | $discount = (int) $discount; |
347 | 347 | $row = array( |
348 | 348 | $discount, |
349 | - get_post_meta( $discount, '_wpi_discount_code', true ), |
|
350 | - get_post_meta( $discount, '_wpi_discount_type', true ), |
|
351 | - get_post_meta( $discount, '_wpi_discount_amount', true ), |
|
352 | - (int) get_post_meta( $discount, '_wpi_discount_uses', true ) |
|
349 | + get_post_meta($discount, '_wpi_discount_code', true), |
|
350 | + get_post_meta($discount, '_wpi_discount_type', true), |
|
351 | + get_post_meta($discount, '_wpi_discount_amount', true), |
|
352 | + (int) get_post_meta($discount, '_wpi_discount_uses', true) |
|
353 | 353 | ); |
354 | - fputcsv( $output, $row ); |
|
354 | + fputcsv($output, $row); |
|
355 | 355 | } |
356 | 356 | |
357 | - fclose( $output ); |
|
357 | + fclose($output); |
|
358 | 358 | exit; |
359 | 359 | |
360 | 360 | } |
361 | 361 | |
362 | - public function set_export_params( $request ) { |
|
362 | + public function set_export_params($request) { |
|
363 | 363 | $this->empty = false; |
364 | - $this->step = !empty( $request['step'] ) ? absint( $request['step'] ) : 1; |
|
365 | - $this->export = !empty( $request['export'] ) ? $request['export'] : $this->export; |
|
364 | + $this->step = !empty($request['step']) ? absint($request['step']) : 1; |
|
365 | + $this->export = !empty($request['export']) ? $request['export'] : $this->export; |
|
366 | 366 | $this->filename = 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype; |
367 | 367 | $this->file = $this->export_dir . $this->filename; |
368 | 368 | |
369 | - do_action( 'wpinv_export_set_params_' . $this->export, $request ); |
|
369 | + do_action('wpinv_export_set_params_' . $this->export, $request); |
|
370 | 370 | } |
371 | 371 | |
372 | 372 | public function get_columns() { |
373 | 373 | $columns = array(); |
374 | 374 | |
375 | - return apply_filters( 'wpinv_export_get_columns_' . $this->export, $columns ); |
|
375 | + return apply_filters('wpinv_export_get_columns_' . $this->export, $columns); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | protected function get_export_file() { |
379 | 379 | $file = ''; |
380 | 380 | |
381 | - if ( $this->wp_filesystem->exists( $this->file ) ) { |
|
382 | - $file = $this->wp_filesystem->get_contents( $this->file ); |
|
381 | + if ($this->wp_filesystem->exists($this->file)) { |
|
382 | + $file = $this->wp_filesystem->get_contents($this->file); |
|
383 | 383 | } else { |
384 | - $this->wp_filesystem->put_contents( $this->file, '' ); |
|
384 | + $this->wp_filesystem->put_contents($this->file, ''); |
|
385 | 385 | } |
386 | 386 | |
387 | 387 | return $file; |
388 | 388 | } |
389 | 389 | |
390 | - protected function attach_export_data( $data = '' ) { |
|
391 | - $filedata = $this->get_export_file(); |
|
392 | - $filedata .= $data; |
|
390 | + protected function attach_export_data($data = '') { |
|
391 | + $filedata = $this->get_export_file(); |
|
392 | + $filedata .= $data; |
|
393 | 393 | |
394 | - $this->wp_filesystem->put_contents( $this->file, $filedata ); |
|
394 | + $this->wp_filesystem->put_contents($this->file, $filedata); |
|
395 | 395 | |
396 | - $rows = file( $this->file, FILE_SKIP_EMPTY_LINES ); |
|
396 | + $rows = file($this->file, FILE_SKIP_EMPTY_LINES); |
|
397 | 397 | $columns = $this->get_columns(); |
398 | - $columns = empty( $columns ) ? 0 : 1; |
|
398 | + $columns = empty($columns) ? 0 : 1; |
|
399 | 399 | |
400 | - $this->empty = count( $rows ) == $columns ? true : false; |
|
400 | + $this->empty = count($rows) == $columns ? true : false; |
|
401 | 401 | } |
402 | 402 | |
403 | 403 | public function print_columns() { |
404 | 404 | $column_data = ''; |
405 | 405 | $columns = $this->get_columns(); |
406 | 406 | $i = 1; |
407 | - foreach( $columns as $key => $column ) { |
|
408 | - $column_data .= '"' . addslashes( $column ) . '"'; |
|
409 | - $column_data .= $i == count( $columns ) ? '' : ','; |
|
407 | + foreach ($columns as $key => $column) { |
|
408 | + $column_data .= '"' . addslashes($column) . '"'; |
|
409 | + $column_data .= $i == count($columns) ? '' : ','; |
|
410 | 410 | $i++; |
411 | 411 | } |
412 | 412 | $column_data .= "\r\n"; |
413 | 413 | |
414 | - $this->attach_export_data( $column_data ); |
|
414 | + $this->attach_export_data($column_data); |
|
415 | 415 | |
416 | 416 | return $column_data; |
417 | 417 | } |
418 | 418 | |
419 | 419 | public function process_export_step() { |
420 | - if ( $this->step < 2 ) { |
|
421 | - /** @scrutinizer ignore-unhandled */ @unlink( $this->file ); |
|
420 | + if ($this->step < 2) { |
|
421 | + /** @scrutinizer ignore-unhandled */ @unlink($this->file); |
|
422 | 422 | $this->print_columns(); |
423 | 423 | } |
424 | 424 | |
425 | 425 | $return = $this->print_rows(); |
426 | 426 | |
427 | - if ( $return ) { |
|
427 | + if ($return) { |
|
428 | 428 | return true; |
429 | 429 | } else { |
430 | 430 | return false; |
@@ -433,14 +433,14 @@ discard block |
||
433 | 433 | |
434 | 434 | public function get_export_status() { |
435 | 435 | $status = 100; |
436 | - return apply_filters( 'wpinv_get_export_status_' . $this->export, $status ); |
|
436 | + return apply_filters('wpinv_get_export_status_' . $this->export, $status); |
|
437 | 437 | } |
438 | 438 | |
439 | 439 | public function get_export_data() { |
440 | 440 | $data = array(); |
441 | 441 | |
442 | - $data = apply_filters( 'wpinv_export_get_data', $data ); |
|
443 | - $data = apply_filters( 'wpinv_export_get_data_' . $this->export, $data ); |
|
442 | + $data = apply_filters('wpinv_export_get_data', $data); |
|
443 | + $data = apply_filters('wpinv_export_get_data_' . $this->export, $data); |
|
444 | 444 | |
445 | 445 | return $data; |
446 | 446 | } |
@@ -450,20 +450,20 @@ discard block |
||
450 | 450 | $data = $this->get_export_data(); |
451 | 451 | $columns = $this->get_columns(); |
452 | 452 | |
453 | - if ( $data ) { |
|
454 | - foreach ( $data as $row ) { |
|
453 | + if ($data) { |
|
454 | + foreach ($data as $row) { |
|
455 | 455 | $i = 1; |
456 | - foreach ( $row as $key => $column ) { |
|
457 | - if ( array_key_exists( $key, $columns ) ) { |
|
458 | - $row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"'; |
|
459 | - $row_data .= $i == count( $columns ) ? '' : ','; |
|
456 | + foreach ($row as $key => $column) { |
|
457 | + if (array_key_exists($key, $columns)) { |
|
458 | + $row_data .= '"' . addslashes(preg_replace("/\"/", "'", $column)) . '"'; |
|
459 | + $row_data .= $i == count($columns) ? '' : ','; |
|
460 | 460 | $i++; |
461 | 461 | } |
462 | 462 | } |
463 | 463 | $row_data .= "\r\n"; |
464 | 464 | } |
465 | 465 | |
466 | - $this->attach_export_data( $row_data ); |
|
466 | + $this->attach_export_data($row_data); |
|
467 | 467 | |
468 | 468 | return $row_data; |
469 | 469 | } |
@@ -472,48 +472,48 @@ discard block |
||
472 | 472 | } |
473 | 473 | |
474 | 474 | // Export Invoices. |
475 | - public function set_invoices_export( $request ) { |
|
476 | - $this->from_date = isset( $request['from_date'] ) ? sanitize_text_field( $request['from_date'] ) : ''; |
|
477 | - $this->to_date = isset( $request['to_date'] ) ? sanitize_text_field( $request['to_date'] ) : ''; |
|
478 | - $this->status = isset( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'publish'; |
|
475 | + public function set_invoices_export($request) { |
|
476 | + $this->from_date = isset($request['from_date']) ? sanitize_text_field($request['from_date']) : ''; |
|
477 | + $this->to_date = isset($request['to_date']) ? sanitize_text_field($request['to_date']) : ''; |
|
478 | + $this->status = isset($request['status']) ? sanitize_text_field($request['status']) : 'publish'; |
|
479 | 479 | } |
480 | 480 | |
481 | - public function get_invoices_columns( $columns = array() ) { |
|
481 | + public function get_invoices_columns($columns = array()) { |
|
482 | 482 | $columns = array( |
483 | - 'id' => __( 'ID', 'invoicing' ), |
|
484 | - 'number' => __( 'Number', 'invoicing' ), |
|
485 | - 'date' => __( 'Date', 'invoicing' ), |
|
486 | - 'due_date' => __( 'Due Date', 'invoicing' ), |
|
487 | - 'completed_date'=> __( 'Payment Done Date', 'invoicing' ), |
|
488 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
489 | - 'currency' => __( 'Currency', 'invoicing' ), |
|
490 | - 'items' => __( 'Items', 'invoicing' ), |
|
491 | - 'status_nicename' => __( 'Status Nicename', 'invoicing' ), |
|
492 | - 'status' => __( 'Status', 'invoicing' ), |
|
493 | - 'tax' => __( 'Tax', 'invoicing' ), |
|
494 | - 'discount' => __( 'Discount', 'invoicing' ), |
|
495 | - 'user_id' => __( 'User ID', 'invoicing' ), |
|
496 | - 'email' => __( 'Email', 'invoicing' ), |
|
497 | - 'first_name' => __( 'First Name', 'invoicing' ), |
|
498 | - 'last_name' => __( 'Last Name', 'invoicing' ), |
|
499 | - 'address' => __( 'Address', 'invoicing' ), |
|
500 | - 'city' => __( 'City', 'invoicing' ), |
|
501 | - 'state' => __( 'State', 'invoicing' ), |
|
502 | - 'country' => __( 'Country', 'invoicing' ), |
|
503 | - 'zip' => __( 'Zipcode', 'invoicing' ), |
|
504 | - 'phone' => __( 'Phone', 'invoicing' ), |
|
505 | - 'company' => __( 'Company', 'invoicing' ), |
|
506 | - 'vat_number' => __( 'Vat Number', 'invoicing' ), |
|
507 | - 'ip' => __( 'IP', 'invoicing' ), |
|
508 | - 'gateway' => __( 'Gateway', 'invoicing' ), |
|
509 | - 'gateway_nicename' => __( 'Gateway Nicename', 'invoicing' ), |
|
510 | - 'transaction_id'=> __( 'Transaction ID', 'invoicing' ), |
|
483 | + 'id' => __('ID', 'invoicing'), |
|
484 | + 'number' => __('Number', 'invoicing'), |
|
485 | + 'date' => __('Date', 'invoicing'), |
|
486 | + 'due_date' => __('Due Date', 'invoicing'), |
|
487 | + 'completed_date'=> __('Payment Done Date', 'invoicing'), |
|
488 | + 'amount' => __('Amount', 'invoicing'), |
|
489 | + 'currency' => __('Currency', 'invoicing'), |
|
490 | + 'items' => __('Items', 'invoicing'), |
|
491 | + 'status_nicename' => __('Status Nicename', 'invoicing'), |
|
492 | + 'status' => __('Status', 'invoicing'), |
|
493 | + 'tax' => __('Tax', 'invoicing'), |
|
494 | + 'discount' => __('Discount', 'invoicing'), |
|
495 | + 'user_id' => __('User ID', 'invoicing'), |
|
496 | + 'email' => __('Email', 'invoicing'), |
|
497 | + 'first_name' => __('First Name', 'invoicing'), |
|
498 | + 'last_name' => __('Last Name', 'invoicing'), |
|
499 | + 'address' => __('Address', 'invoicing'), |
|
500 | + 'city' => __('City', 'invoicing'), |
|
501 | + 'state' => __('State', 'invoicing'), |
|
502 | + 'country' => __('Country', 'invoicing'), |
|
503 | + 'zip' => __('Zipcode', 'invoicing'), |
|
504 | + 'phone' => __('Phone', 'invoicing'), |
|
505 | + 'company' => __('Company', 'invoicing'), |
|
506 | + 'vat_number' => __('Vat Number', 'invoicing'), |
|
507 | + 'ip' => __('IP', 'invoicing'), |
|
508 | + 'gateway' => __('Gateway', 'invoicing'), |
|
509 | + 'gateway_nicename' => __('Gateway Nicename', 'invoicing'), |
|
510 | + 'transaction_id'=> __('Transaction ID', 'invoicing'), |
|
511 | 511 | ); |
512 | 512 | |
513 | 513 | return $columns; |
514 | 514 | } |
515 | 515 | |
516 | - public function get_invoices_data( $response = array() ) { |
|
516 | + public function get_invoices_data($response = array()) { |
|
517 | 517 | $args = array( |
518 | 518 | 'limit' => $this->per_page, |
519 | 519 | 'page' => $this->step, |
@@ -521,42 +521,42 @@ discard block |
||
521 | 521 | 'orderby' => 'date', |
522 | 522 | ); |
523 | 523 | |
524 | - if ( $this->status != 'any' ) { |
|
524 | + if ($this->status != 'any') { |
|
525 | 525 | $args['status'] = $this->status; |
526 | 526 | } else { |
527 | - $args['status'] = array_keys( wpinv_get_invoice_statuses( true ) ); |
|
527 | + $args['status'] = array_keys(wpinv_get_invoice_statuses(true)); |
|
528 | 528 | } |
529 | 529 | |
530 | - if ( !empty( $this->from_date ) || !empty( $this->to_date ) ) { |
|
530 | + if (!empty($this->from_date) || !empty($this->to_date)) { |
|
531 | 531 | $args['date_query'] = array( |
532 | 532 | array( |
533 | - 'after' => date( 'Y-n-d 00:00:00', strtotime( $this->from_date ) ), |
|
534 | - 'before' => date( 'Y-n-d 23:59:59', strtotime( $this->to_date ) ), |
|
533 | + 'after' => date('Y-n-d 00:00:00', strtotime($this->from_date)), |
|
534 | + 'before' => date('Y-n-d 23:59:59', strtotime($this->to_date)), |
|
535 | 535 | 'inclusive' => true |
536 | 536 | ) |
537 | 537 | ); |
538 | 538 | } |
539 | 539 | |
540 | - $invoices = wpinv_get_invoices( $args ); |
|
540 | + $invoices = wpinv_get_invoices($args); |
|
541 | 541 | |
542 | 542 | $data = array(); |
543 | 543 | |
544 | - if ( !empty( $invoices ) ) { |
|
545 | - foreach ( $invoices as $invoice ) { |
|
544 | + if (!empty($invoices)) { |
|
545 | + foreach ($invoices as $invoice) { |
|
546 | 546 | $items = $this->get_invoice_items($invoice); |
547 | 547 | $row = array( |
548 | 548 | 'id' => $invoice->ID, |
549 | 549 | 'number' => $invoice->get_number(), |
550 | - 'date' => $invoice->get_invoice_date( false ), |
|
551 | - 'due_date' => $invoice->get_due_date( false ), |
|
550 | + 'date' => $invoice->get_invoice_date(false), |
|
551 | + 'due_date' => $invoice->get_due_date(false), |
|
552 | 552 | 'completed_date'=> $invoice->get_completed_date(), |
553 | - 'amount' => wpinv_round_amount( $invoice->get_total() ), |
|
553 | + 'amount' => wpinv_round_amount($invoice->get_total()), |
|
554 | 554 | 'currency' => $invoice->get_currency(), |
555 | 555 | 'items' => $items, |
556 | - 'status_nicename' => $invoice->get_status( true ), |
|
556 | + 'status_nicename' => $invoice->get_status(true), |
|
557 | 557 | 'status' => $invoice->get_status(), |
558 | - 'tax' => $invoice->get_tax() > 0 ? wpinv_round_amount( $invoice->get_tax() ) : '', |
|
559 | - 'discount' => $invoice->get_discount() > 0 ? wpinv_round_amount( $invoice->get_discount() ) : '', |
|
558 | + 'tax' => $invoice->get_tax() > 0 ? wpinv_round_amount($invoice->get_tax()) : '', |
|
559 | + 'discount' => $invoice->get_discount() > 0 ? wpinv_round_amount($invoice->get_discount()) : '', |
|
560 | 560 | 'user_id' => $invoice->get_user_id(), |
561 | 561 | 'email' => $invoice->get_email(), |
562 | 562 | 'first_name' => $invoice->get_first_name(), |
@@ -575,7 +575,7 @@ discard block |
||
575 | 575 | 'transaction_id'=> $invoice->gateway ? $invoice->get_transaction_id() : '', |
576 | 576 | ); |
577 | 577 | |
578 | - $data[] = apply_filters( 'wpinv_export_invoice_row', $row, $invoice ); |
|
578 | + $data[] = apply_filters('wpinv_export_invoice_row', $row, $invoice); |
|
579 | 579 | } |
580 | 580 | |
581 | 581 | return $data; |
@@ -591,44 +591,44 @@ discard block |
||
591 | 591 | 'return' => 'ids', |
592 | 592 | ); |
593 | 593 | |
594 | - if ( $this->status != 'any' ) { |
|
594 | + if ($this->status != 'any') { |
|
595 | 595 | $args['status'] = $this->status; |
596 | 596 | } else { |
597 | - $args['status'] = array_keys( wpinv_get_invoice_statuses( true ) ); |
|
597 | + $args['status'] = array_keys(wpinv_get_invoice_statuses(true)); |
|
598 | 598 | } |
599 | 599 | |
600 | - if ( !empty( $this->from_date ) || !empty( $this->to_date ) ) { |
|
600 | + if (!empty($this->from_date) || !empty($this->to_date)) { |
|
601 | 601 | $args['date_query'] = array( |
602 | 602 | array( |
603 | - 'after' => date( 'Y-n-d 00:00:00', strtotime( $this->from_date ) ), |
|
604 | - 'before' => date( 'Y-n-d 23:59:59', strtotime( $this->to_date ) ), |
|
603 | + 'after' => date('Y-n-d 00:00:00', strtotime($this->from_date)), |
|
604 | + 'before' => date('Y-n-d 23:59:59', strtotime($this->to_date)), |
|
605 | 605 | 'inclusive' => true |
606 | 606 | ) |
607 | 607 | ); |
608 | 608 | } |
609 | 609 | |
610 | - $invoices = wpinv_get_invoices( $args ); |
|
611 | - $total = !empty( $invoices ) ? count( $invoices ) : 0; |
|
610 | + $invoices = wpinv_get_invoices($args); |
|
611 | + $total = !empty($invoices) ? count($invoices) : 0; |
|
612 | 612 | $status = 100; |
613 | 613 | |
614 | - if ( $total > 0 ) { |
|
615 | - $status = ( ( $this->per_page * $this->step ) / $total ) * 100; |
|
614 | + if ($total > 0) { |
|
615 | + $status = (($this->per_page * $this->step) / $total) * 100; |
|
616 | 616 | } |
617 | 617 | |
618 | - if ( $status > 100 ) { |
|
618 | + if ($status > 100) { |
|
619 | 619 | $status = 100; |
620 | 620 | } |
621 | 621 | |
622 | 622 | return $status; |
623 | 623 | } |
624 | 624 | |
625 | - public function get_invoice_items($invoice){ |
|
626 | - if(!$invoice){ |
|
625 | + public function get_invoice_items($invoice) { |
|
626 | + if (!$invoice) { |
|
627 | 627 | return ''; |
628 | 628 | } |
629 | 629 | |
630 | 630 | $cart_details = $invoice->get_cart_details(); |
631 | - if(!empty($cart_details)){ |
|
631 | + if (!empty($cart_details)) { |
|
632 | 632 | $cart_details = maybe_serialize($cart_details); |
633 | 633 | } else { |
634 | 634 | $cart_details = ''; |
@@ -640,14 +640,14 @@ discard block |
||
640 | 640 | /** |
641 | 641 | * Returns the periods filter. |
642 | 642 | */ |
643 | - public function period_filter( $args = array() ) { |
|
643 | + public function period_filter($args = array()) { |
|
644 | 644 | |
645 | 645 | ob_start(); |
646 | 646 | |
647 | 647 | echo '<form id="wpinv-graphs-filter" method="get" style="margin-bottom: 10px;" class="tablenav">'; |
648 | 648 | echo '<input type="hidden" name="page" value="wpinv-reports">'; |
649 | 649 | |
650 | - foreach ( $args as $key => $val ) { |
|
650 | + foreach ($args as $key => $val) { |
|
651 | 651 | $key = esc_attr($key); |
652 | 652 | $val = esc_attr($val); |
653 | 653 | echo "<input type='hidden' name='$key' value='$val'>"; |
@@ -656,21 +656,21 @@ discard block |
||
656 | 656 | echo '<select id="wpinv-graphs-date-options" name="range" style="min-width: 200px;" onChange="this.form.submit()">'; |
657 | 657 | |
658 | 658 | $ranges = array( |
659 | - 'today' => __( 'Today', 'invoicing' ), |
|
660 | - 'yesterday' => __( 'Yesterday', 'invoicing' ), |
|
661 | - 'this_week' => __( 'This Week', 'invoicing' ), |
|
662 | - 'last_week' => __( 'Last Week', 'invoicing' ), |
|
663 | - '7_days_ago' => __( 'Last 7 Days', 'invoicing' ), |
|
664 | - '30_days_ago' => __( 'Last 30 Days', 'invoicing' ), |
|
665 | - 'this_month' => __( 'This Month', 'invoicing' ), |
|
666 | - 'this_year' => __( 'This Year', 'invoicing' ), |
|
667 | - 'last_year' => __( 'Last Year', 'invoicing' ), |
|
659 | + 'today' => __('Today', 'invoicing'), |
|
660 | + 'yesterday' => __('Yesterday', 'invoicing'), |
|
661 | + 'this_week' => __('This Week', 'invoicing'), |
|
662 | + 'last_week' => __('Last Week', 'invoicing'), |
|
663 | + '7_days_ago' => __('Last 7 Days', 'invoicing'), |
|
664 | + '30_days_ago' => __('Last 30 Days', 'invoicing'), |
|
665 | + 'this_month' => __('This Month', 'invoicing'), |
|
666 | + 'this_year' => __('This Year', 'invoicing'), |
|
667 | + 'last_year' => __('Last Year', 'invoicing'), |
|
668 | 668 | ); |
669 | 669 | |
670 | - $range = isset( $_GET['range'] ) && isset( $ranges[ $_GET['range'] ] ) ? $_GET['range'] : '7_days_ago'; |
|
670 | + $range = isset($_GET['range']) && isset($ranges[$_GET['range']]) ? $_GET['range'] : '7_days_ago'; |
|
671 | 671 | |
672 | - foreach ( $ranges as $val => $label ) { |
|
673 | - $selected = selected( $range, $val, false ); |
|
672 | + foreach ($ranges as $val => $label) { |
|
673 | + $selected = selected($range, $val, false); |
|
674 | 674 | echo "<option value='$val' $selected>$label</option>"; |
675 | 675 | } |
676 | 676 | |
@@ -682,28 +682,28 @@ discard block |
||
682 | 682 | /** |
683 | 683 | * Returns the the current date range. |
684 | 684 | */ |
685 | - public function get_sql_clauses( $range ) { |
|
685 | + public function get_sql_clauses($range) { |
|
686 | 686 | |
687 | 687 | $date = 'CAST(meta.completed_date AS DATE)'; |
688 | 688 | $datetime = 'meta.completed_date'; |
689 | 689 | |
690 | 690 | // Prepare durations. |
691 | - $today = current_time( 'Y-m-d' ); |
|
692 | - $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); |
|
693 | - $sunday = date( 'Y-m-d', strtotime( 'sunday this week', current_time( 'timestamp' ) ) ); |
|
694 | - $monday = date( 'Y-m-d', strtotime( 'monday this week', current_time( 'timestamp' ) ) ); |
|
695 | - $last_sunday = date( 'Y-m-d', strtotime( 'sunday last week', current_time( 'timestamp' ) ) ); |
|
696 | - $last_monday = date( 'Y-m-d', strtotime( 'monday last week', current_time( 'timestamp' ) ) ); |
|
697 | - $seven_days_ago = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) ); |
|
698 | - $thirty_days_ago = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); |
|
699 | - $first_day_month = date( 'Y-m-1', current_time( 'timestamp' ) ); |
|
700 | - $last_day_month = date( 'Y-m-t', current_time( 'timestamp' ) ); |
|
701 | - $first_day_last_month = date( 'Y-m-d', strtotime( 'first day of last month', current_time( 'timestamp' ) ) ); |
|
702 | - $last_day_last_month = date( 'Y-m-d', strtotime( 'last day of last month', current_time( 'timestamp' ) ) ); |
|
703 | - $first_day_year = date( 'Y-1-1', current_time( 'timestamp' ) ); |
|
704 | - $last_day_year = date( 'Y-12-31', current_time( 'timestamp' ) ); |
|
705 | - $first_day_last_year = date( 'Y-m-d', strtotime( 'first day of last year', current_time( 'timestamp' ) ) ); |
|
706 | - $last_day_last_year = date( 'Y-m-d', strtotime( 'last day of last year', current_time( 'timestamp' ) ) ); |
|
691 | + $today = current_time('Y-m-d'); |
|
692 | + $yesterday = date('Y-m-d', strtotime('-1 day', current_time('timestamp'))); |
|
693 | + $sunday = date('Y-m-d', strtotime('sunday this week', current_time('timestamp'))); |
|
694 | + $monday = date('Y-m-d', strtotime('monday this week', current_time('timestamp'))); |
|
695 | + $last_sunday = date('Y-m-d', strtotime('sunday last week', current_time('timestamp'))); |
|
696 | + $last_monday = date('Y-m-d', strtotime('monday last week', current_time('timestamp'))); |
|
697 | + $seven_days_ago = date('Y-m-d', strtotime('-7 days', current_time('timestamp'))); |
|
698 | + $thirty_days_ago = date('Y-m-d', strtotime('-30 days', current_time('timestamp'))); |
|
699 | + $first_day_month = date('Y-m-1', current_time('timestamp')); |
|
700 | + $last_day_month = date('Y-m-t', current_time('timestamp')); |
|
701 | + $first_day_last_month = date('Y-m-d', strtotime('first day of last month', current_time('timestamp'))); |
|
702 | + $last_day_last_month = date('Y-m-d', strtotime('last day of last month', current_time('timestamp'))); |
|
703 | + $first_day_year = date('Y-1-1', current_time('timestamp')); |
|
704 | + $last_day_year = date('Y-12-31', current_time('timestamp')); |
|
705 | + $first_day_last_year = date('Y-m-d', strtotime('first day of last year', current_time('timestamp'))); |
|
706 | + $last_day_last_year = date('Y-m-d', strtotime('last day of last year', current_time('timestamp'))); |
|
707 | 707 | |
708 | 708 | $ranges = array( |
709 | 709 | |
@@ -759,21 +759,21 @@ discard block |
||
759 | 759 | |
760 | 760 | ); |
761 | 761 | |
762 | - if ( ! isset( $ranges[ $range ] ) ) { |
|
762 | + if (!isset($ranges[$range])) { |
|
763 | 763 | return $ranges['7_days_ago']; |
764 | 764 | } |
765 | - return $ranges[ $range ]; |
|
765 | + return $ranges[$range]; |
|
766 | 766 | |
767 | 767 | } |
768 | 768 | |
769 | 769 | /** |
770 | 770 | * Returns the the current date ranges results. |
771 | 771 | */ |
772 | - public function get_report_results( $range ) { |
|
772 | + public function get_report_results($range) { |
|
773 | 773 | global $wpdb; |
774 | 774 | |
775 | 775 | $table = $wpdb->prefix . 'getpaid_invoices'; |
776 | - $clauses = $this->get_sql_clauses( $range ); |
|
776 | + $clauses = $this->get_sql_clauses($range); |
|
777 | 777 | $sql = "SELECT |
778 | 778 | {$clauses[0]} AS completed_date, |
779 | 779 | SUM( meta.total ) AS total, |
@@ -789,30 +789,30 @@ discard block |
||
789 | 789 | GROUP BY {$clauses[0]} |
790 | 790 | "; |
791 | 791 | |
792 | - return $wpdb->get_results( $sql ); |
|
792 | + return $wpdb->get_results($sql); |
|
793 | 793 | } |
794 | 794 | |
795 | 795 | /** |
796 | 796 | * Fill nulls. |
797 | 797 | */ |
798 | - public function fill_nulls( $data, $range ) { |
|
798 | + public function fill_nulls($data, $range) { |
|
799 | 799 | |
800 | 800 | $return = array(); |
801 | 801 | $time = current_time('timestamp'); |
802 | 802 | |
803 | - switch ( $range ) { |
|
803 | + switch ($range) { |
|
804 | 804 | case 'today' : |
805 | 805 | case 'yesterday' : |
806 | - $hour = 0; |
|
806 | + $hour = 0; |
|
807 | 807 | |
808 | - while ( $hour < 23 ) { |
|
808 | + while ($hour < 23) { |
|
809 | 809 | $amount = 0; |
810 | - if ( isset( $data[$hour] ) ) { |
|
811 | - $amount = floatval( $data[$hour] ); |
|
810 | + if (isset($data[$hour])) { |
|
811 | + $amount = floatval($data[$hour]); |
|
812 | 812 | } |
813 | 813 | |
814 | - $time = strtotime( "$range $hour:00:00" ) * 1000; |
|
815 | - $return[] = array( $time, $amount ); |
|
814 | + $time = strtotime("$range $hour:00:00") * 1000; |
|
815 | + $return[] = array($time, $amount); |
|
816 | 816 | $hour++; |
817 | 817 | } |
818 | 818 | |
@@ -820,24 +820,24 @@ discard block |
||
820 | 820 | |
821 | 821 | case 'this_month' : |
822 | 822 | case 'last_month' : |
823 | - $_range = str_replace( '_', ' ', $range ); |
|
824 | - $month = date( 'n', strtotime( $_range, $time ) ); |
|
825 | - $year = date( 'Y', strtotime( $_range, $time ) ); |
|
823 | + $_range = str_replace('_', ' ', $range); |
|
824 | + $month = date('n', strtotime($_range, $time)); |
|
825 | + $year = date('Y', strtotime($_range, $time)); |
|
826 | 826 | $days = cal_days_in_month( |
827 | - defined( 'CAL_GREGORIAN' ) ? CAL_GREGORIAN : 1, |
|
827 | + defined('CAL_GREGORIAN') ? CAL_GREGORIAN : 1, |
|
828 | 828 | $month, |
829 | 829 | $year |
830 | 830 | ); |
831 | 831 | |
832 | 832 | $day = 1; |
833 | - while ( $days != $day ) { |
|
833 | + while ($days != $day) { |
|
834 | 834 | $amount = 0; |
835 | - if ( isset( $data[$day] ) ) { |
|
836 | - $amount = floatval( $data[$day] ); |
|
835 | + if (isset($data[$day])) { |
|
836 | + $amount = floatval($data[$day]); |
|
837 | 837 | } |
838 | 838 | |
839 | - $time = strtotime( "$year-$month-$day" ) * 1000; |
|
840 | - $return[] = array( $time, $amount ); |
|
839 | + $time = strtotime("$year-$month-$day") * 1000; |
|
840 | + $return[] = array($time, $amount); |
|
841 | 841 | $day++; |
842 | 842 | } |
843 | 843 | |
@@ -845,52 +845,52 @@ discard block |
||
845 | 845 | |
846 | 846 | case 'this_week' : |
847 | 847 | case 'last_week' : |
848 | - $_range = str_replace( '_', ' ', $range ); |
|
849 | - $days = array( 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday' ); |
|
848 | + $_range = str_replace('_', ' ', $range); |
|
849 | + $days = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'); |
|
850 | 850 | |
851 | - foreach ( $days as $day ) { |
|
851 | + foreach ($days as $day) { |
|
852 | 852 | |
853 | 853 | $amount = 0; |
854 | - if ( isset( $data[ ucfirst( $day ) ] ) ) { |
|
855 | - $amount = floatval( $data[ ucfirst( $day ) ] ); |
|
854 | + if (isset($data[ucfirst($day)])) { |
|
855 | + $amount = floatval($data[ucfirst($day)]); |
|
856 | 856 | } |
857 | 857 | |
858 | - $time = strtotime( "$_range $day" ) * 1000; |
|
859 | - $return[] = array( $time, $amount ); |
|
858 | + $time = strtotime("$_range $day") * 1000; |
|
859 | + $return[] = array($time, $amount); |
|
860 | 860 | } |
861 | 861 | |
862 | 862 | break; |
863 | 863 | |
864 | 864 | case 'this_year' : |
865 | 865 | case 'last_year' : |
866 | - $_range = str_replace( '_', ' ', $range ); |
|
867 | - $year = date( 'Y', strtotime( $_range, $time ) ); |
|
868 | - $months = array( '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' ); |
|
866 | + $_range = str_replace('_', ' ', $range); |
|
867 | + $year = date('Y', strtotime($_range, $time)); |
|
868 | + $months = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'); |
|
869 | 869 | |
870 | - foreach ( $months as $month ) { |
|
870 | + foreach ($months as $month) { |
|
871 | 871 | |
872 | 872 | $amount = 0; |
873 | - if ( isset( $data[$month] ) ) { |
|
874 | - $amount = floatval( $data[$month] ); |
|
873 | + if (isset($data[$month])) { |
|
874 | + $amount = floatval($data[$month]); |
|
875 | 875 | } |
876 | 876 | |
877 | - $_time = strtotime("$year-$month-01") * 1000; |
|
878 | - $return[] = array( $_time, $amount ); |
|
877 | + $_time = strtotime("$year-$month-01") * 1000; |
|
878 | + $return[] = array($_time, $amount); |
|
879 | 879 | } |
880 | 880 | |
881 | 881 | break; |
882 | 882 | case '30_days_ago' : |
883 | 883 | $days = 30; |
884 | 884 | |
885 | - while ( $days > 1 ) { |
|
885 | + while ($days > 1) { |
|
886 | 886 | $amount = 0; |
887 | - $date = date( 'j', strtotime( "-$days days", $time ) ); |
|
888 | - if ( isset( $data[$date] ) ) { |
|
889 | - $amount = floatval( $data[$date] ); |
|
887 | + $date = date('j', strtotime("-$days days", $time)); |
|
888 | + if (isset($data[$date])) { |
|
889 | + $amount = floatval($data[$date]); |
|
890 | 890 | } |
891 | 891 | |
892 | - $_time = strtotime( "-$days days", $time ) * 1000; |
|
893 | - $return[] = array( $_time, $amount ); |
|
892 | + $_time = strtotime("-$days days", $time) * 1000; |
|
893 | + $return[] = array($_time, $amount); |
|
894 | 894 | $days--; |
895 | 895 | } |
896 | 896 | |
@@ -899,15 +899,15 @@ discard block |
||
899 | 899 | default: |
900 | 900 | $days = 7; |
901 | 901 | |
902 | - while ( $days > 1 ) { |
|
902 | + while ($days > 1) { |
|
903 | 903 | $amount = 0; |
904 | - $date = date( 'j', strtotime( "-$days days", $time ) ); |
|
905 | - if ( isset( $data[$date] ) ) { |
|
906 | - $amount = floatval( $data[$date] ); |
|
904 | + $date = date('j', strtotime("-$days days", $time)); |
|
905 | + if (isset($data[$date])) { |
|
906 | + $amount = floatval($data[$date]); |
|
907 | 907 | } |
908 | 908 | |
909 | - $_time = strtotime( "-$days days", $time ) * 1000; |
|
910 | - $return[] = array( $_time, $amount ); |
|
909 | + $_time = strtotime("-$days days", $time) * 1000; |
|
910 | + $return[] = array($_time, $amount); |
|
911 | 911 | $days--; |
912 | 912 | } |
913 | 913 | |
@@ -922,33 +922,33 @@ discard block |
||
922 | 922 | * Retrieves the stats. |
923 | 923 | */ |
924 | 924 | public function get_stats() { |
925 | - $range = isset( $_GET['range'] ) ? $_GET['range'] : '7_days_ago'; |
|
926 | - $results = $this->get_report_results( $range ); |
|
927 | - $earnings = wp_list_pluck( $results, 'total', 'completed_date' ); |
|
928 | - $taxes = wp_list_pluck( $results, 'tax', 'completed_date' ); |
|
929 | - $discounts = wp_list_pluck( $results, 'discount', 'completed_date' ); |
|
930 | - $fees = wp_list_pluck( $results, 'fees_total', 'completed_date' ); |
|
925 | + $range = isset($_GET['range']) ? $_GET['range'] : '7_days_ago'; |
|
926 | + $results = $this->get_report_results($range); |
|
927 | + $earnings = wp_list_pluck($results, 'total', 'completed_date'); |
|
928 | + $taxes = wp_list_pluck($results, 'tax', 'completed_date'); |
|
929 | + $discounts = wp_list_pluck($results, 'discount', 'completed_date'); |
|
930 | + $fees = wp_list_pluck($results, 'fees_total', 'completed_date'); |
|
931 | 931 | |
932 | 932 | return array( |
933 | 933 | |
934 | 934 | array( |
935 | - 'label' => __( 'Earnings', 'invoicing' ), |
|
936 | - 'data' => $this->fill_nulls( $earnings, $range ), |
|
935 | + 'label' => __('Earnings', 'invoicing'), |
|
936 | + 'data' => $this->fill_nulls($earnings, $range), |
|
937 | 937 | ), |
938 | 938 | |
939 | 939 | array( |
940 | - 'label' => __( 'Taxes', 'invoicing' ), |
|
941 | - 'data' => $this->fill_nulls( $taxes, $range ), |
|
940 | + 'label' => __('Taxes', 'invoicing'), |
|
941 | + 'data' => $this->fill_nulls($taxes, $range), |
|
942 | 942 | ), |
943 | 943 | |
944 | 944 | array( |
945 | - 'label' => __( 'Discounts', 'invoicing' ), |
|
946 | - 'data' => $this->fill_nulls( $discounts, $range ), |
|
945 | + 'label' => __('Discounts', 'invoicing'), |
|
946 | + 'data' => $this->fill_nulls($discounts, $range), |
|
947 | 947 | ), |
948 | 948 | |
949 | 949 | array( |
950 | - 'label' => __( 'Fees', 'invoicing' ), |
|
951 | - 'data' => $this->fill_nulls( $fees, $range ), |
|
950 | + 'label' => __('Fees', 'invoicing'), |
|
951 | + 'data' => $this->fill_nulls($fees, $range), |
|
952 | 952 | ) |
953 | 953 | ); |
954 | 954 | |
@@ -958,34 +958,34 @@ discard block |
||
958 | 958 | * Retrieves the time format for stats. |
959 | 959 | */ |
960 | 960 | public function get_time_format() { |
961 | - $range = isset( $_GET['range'] ) ? $_GET['range'] : '7_days_ago'; |
|
961 | + $range = isset($_GET['range']) ? $_GET['range'] : '7_days_ago'; |
|
962 | 962 | |
963 | - switch ( $range ) { |
|
963 | + switch ($range) { |
|
964 | 964 | case 'today' : |
965 | 965 | case 'yesterday' : |
966 | - return array( 'hour', '%h %p' ); |
|
966 | + return array('hour', '%h %p'); |
|
967 | 967 | break; |
968 | 968 | |
969 | 969 | case 'this_month' : |
970 | 970 | case 'last_month' : |
971 | - return array( 'day', '%b %d' ); |
|
971 | + return array('day', '%b %d'); |
|
972 | 972 | break; |
973 | 973 | |
974 | 974 | case 'this_week' : |
975 | 975 | case 'last_week' : |
976 | - return array( 'day', '%b %d' ); |
|
976 | + return array('day', '%b %d'); |
|
977 | 977 | break; |
978 | 978 | |
979 | 979 | case 'this_year' : |
980 | 980 | case 'last_year' : |
981 | - return array( 'month', '%b' ); |
|
981 | + return array('month', '%b'); |
|
982 | 982 | break; |
983 | 983 | case '30_days_ago' : |
984 | - return array( 'day', '%b %d' ); |
|
984 | + return array('day', '%b %d'); |
|
985 | 985 | break; |
986 | 986 | |
987 | 987 | default: |
988 | - return array( 'day', '%b %d' ); |
|
988 | + return array('day', '%b %d'); |
|
989 | 989 | break; |
990 | 990 | |
991 | 991 | } |
@@ -996,11 +996,11 @@ discard block |
||
996 | 996 | */ |
997 | 997 | public function earnings_report() { |
998 | 998 | |
999 | - $data = wp_json_encode( $this->get_stats() ); |
|
999 | + $data = wp_json_encode($this->get_stats()); |
|
1000 | 1000 | $time_format = $this->get_time_format(); |
1001 | 1001 | echo ' |
1002 | 1002 | <div class="wpinv-report-container"> |
1003 | - <h3><span>' . __( 'Earnings Over Time', 'invoicing' ) .'</span></h3> |
|
1003 | + <h3><span>' . __('Earnings Over Time', 'invoicing') . '</span></h3> |
|
1004 | 1004 | ' . $this->period_filter() . ' |
1005 | 1005 | <div id="wpinv_report_graph" style="height: 450px;"></div> |
1006 | 1006 | </div> |
@@ -1009,12 +1009,12 @@ discard block |
||
1009 | 1009 | jQuery(document).ready( function() { |
1010 | 1010 | jQuery.plot( |
1011 | 1011 | jQuery("#wpinv_report_graph"), |
1012 | - ' . $data .', |
|
1012 | + ' . $data . ', |
|
1013 | 1013 | { |
1014 | 1014 | xaxis:{ |
1015 | 1015 | mode: "time", |
1016 | - timeformat: "' . $time_format[1] .'", |
|
1017 | - minTickSize: [0.5, "' . $time_format[0] .'"] |
|
1016 | + timeformat: "' . $time_format[1] . '", |
|
1017 | + minTickSize: [0.5, "' . $time_format[0] . '"] |
|
1018 | 1018 | }, |
1019 | 1019 | |
1020 | 1020 | yaxis: { |
@@ -1042,7 +1042,7 @@ discard block |
||
1042 | 1042 | * Displays the gateways report. |
1043 | 1043 | */ |
1044 | 1044 | public function gateways_report() { |
1045 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-gateways-report-table.php' ); |
|
1045 | + require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-gateways-report-table.php'); |
|
1046 | 1046 | |
1047 | 1047 | $table = new WPInv_Gateways_Report_Table(); |
1048 | 1048 | $table->prepare_items(); |
@@ -1053,12 +1053,12 @@ discard block |
||
1053 | 1053 | * Displays the items report. |
1054 | 1054 | */ |
1055 | 1055 | public function items_report() { |
1056 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-items-report-table.php' ); |
|
1056 | + require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-items-report-table.php'); |
|
1057 | 1057 | |
1058 | 1058 | $table = new WPInv_Items_Report_Table(); |
1059 | 1059 | $table->prepare_items(); |
1060 | 1060 | $table->display(); |
1061 | - echo __( '* Items with no sales not shown.', 'invoicing' ); |
|
1061 | + echo __('* Items with no sales not shown.', 'invoicing'); |
|
1062 | 1062 | } |
1063 | 1063 | |
1064 | 1064 | /** |
@@ -1068,27 +1068,27 @@ discard block |
||
1068 | 1068 | */ |
1069 | 1069 | public function tax_report() { |
1070 | 1070 | |
1071 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-taxes-report-table.php' ); |
|
1071 | + require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-taxes-report-table.php'); |
|
1072 | 1072 | $table = new WPInv_Taxes_Reports_Table(); |
1073 | 1073 | $table->prepare_items(); |
1074 | - $year = isset( $_GET['year'] ) ? absint( $_GET['year'] ) : date( 'Y' ); |
|
1074 | + $year = isset($_GET['year']) ? absint($_GET['year']) : date('Y'); |
|
1075 | 1075 | ?> |
1076 | 1076 | |
1077 | 1077 | <div class="metabox-holder" style="padding-top: 0;"> |
1078 | 1078 | <div class="postbox"> |
1079 | - <h3><span><?php _e('Tax Report','invoicing' ); ?></span></h3> |
|
1079 | + <h3><span><?php _e('Tax Report', 'invoicing'); ?></span></h3> |
|
1080 | 1080 | <div class="inside"> |
1081 | - <p><?php _e( 'This report shows the total amount collected in sales tax for the given year.', 'invoicing' ); ?></p> |
|
1081 | + <p><?php _e('This report shows the total amount collected in sales tax for the given year.', 'invoicing'); ?></p> |
|
1082 | 1082 | <form method="get"> |
1083 | - <span><?php echo $year; ?></span>: <strong><?php echo wpinv_sales_tax_for_year( $year ); ?></strong> — |
|
1083 | + <span><?php echo $year; ?></span>: <strong><?php echo wpinv_sales_tax_for_year($year); ?></strong> — |
|
1084 | 1084 | <select name="year"> |
1085 | - <?php for ( $i = 2014; $i <= date( 'Y' ); $i++ ) : ?> |
|
1086 | - <option value="<?php echo $i; ?>"<?php selected( $year, $i ); ?>><?php echo $i; ?></option> |
|
1085 | + <?php for ($i = 2014; $i <= date('Y'); $i++) : ?> |
|
1086 | + <option value="<?php echo $i; ?>"<?php selected($year, $i); ?>><?php echo $i; ?></option> |
|
1087 | 1087 | <?php endfor; ?> |
1088 | 1088 | </select> |
1089 | 1089 | <input type="hidden" name="view" value="taxes" /> |
1090 | 1090 | <input type="hidden" name="page" value="wpinv-reports"/> |
1091 | - <?php submit_button( __( 'Submit', 'invoicing' ), 'secondary', 'submit', false ); ?> |
|
1091 | + <?php submit_button(__('Submit', 'invoicing'), 'secondary', 'submit', false); ?> |
|
1092 | 1092 | </form> |
1093 | 1093 | </div><!-- .inside --> |
1094 | 1094 | </div><!-- .postbox --> |
@@ -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 Payment Gateway class. |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | * |
25 | 25 | * @var array |
26 | 26 | */ |
27 | - protected $supports = array( 'subscription', 'sandbox', 'tokens' ); |
|
27 | + protected $supports = array('subscription', 'sandbox', 'tokens'); |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Payment method order. |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @var array |
54 | 54 | */ |
55 | - public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
55 | + public $currencies = array('USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD'); |
|
56 | 56 | |
57 | 57 | /** |
58 | 58 | * URL to view a transaction. |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | */ |
67 | 67 | public function __construct() { |
68 | 68 | |
69 | - $this->title = __( 'Credit Card / Debit Card', 'invoicing' ); |
|
70 | - $this->method_title = __( 'Authorize.Net', 'invoicing' ); |
|
71 | - $this->notify_url = wpinv_get_ipn_url( $this->id ); |
|
69 | + $this->title = __('Credit Card / Debit Card', 'invoicing'); |
|
70 | + $this->method_title = __('Authorize.Net', 'invoicing'); |
|
71 | + $this->notify_url = wpinv_get_ipn_url($this->id); |
|
72 | 72 | |
73 | - add_filter( 'wpinv_renew_authorizenet_subscription_profile', array( $this, 'renew_subscription' ) ); |
|
74 | - add_filter( 'wpinv_gateway_description', array( $this, 'sandbox_notice' ), 10, 2 ); |
|
73 | + add_filter('wpinv_renew_authorizenet_subscription_profile', array($this, 'renew_subscription')); |
|
74 | + add_filter('wpinv_gateway_description', array($this, 'sandbox_notice'), 10, 2); |
|
75 | 75 | parent::__construct(); |
76 | 76 | } |
77 | 77 | |
@@ -81,13 +81,13 @@ discard block |
||
81 | 81 | * @param int $invoice_id 0 or invoice id. |
82 | 82 | * @param GetPaid_Payment_Form $form Current payment form. |
83 | 83 | */ |
84 | - public function payment_fields( $invoice_id, $form ) { |
|
84 | + public function payment_fields($invoice_id, $form) { |
|
85 | 85 | |
86 | 86 | // Let the user select a payment method. |
87 | 87 | echo $this->saved_payment_methods(); |
88 | 88 | |
89 | 89 | // Show the credit card entry form. |
90 | - echo $this->new_payment_method_entry( $this->get_cc_form( true ) ); |
|
90 | + echo $this->new_payment_method_entry($this->get_cc_form(true)); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
@@ -100,64 +100,64 @@ discard block |
||
100 | 100 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
101 | 101 | * @return string|WP_Error Payment profile id. |
102 | 102 | */ |
103 | - public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
103 | + public function create_customer_profile($invoice, $submission_data, $save = true) { |
|
104 | 104 | |
105 | 105 | // Remove non-digits from the number |
106 | - $submission_data['authorizenet']['cc_number'] = preg_replace('/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
|
106 | + $submission_data['authorizenet']['cc_number'] = preg_replace('/\D/', '', $submission_data['authorizenet']['cc_number']); |
|
107 | 107 | |
108 | 108 | // Generate args. |
109 | 109 | $args = array( |
110 | 110 | 'createCustomerProfileRequest' => array( |
111 | 111 | 'merchantAuthentication' => $this->get_auth_params(), |
112 | 112 | 'profile' => array( |
113 | - 'merchantCustomerId' => getpaid_limit_length( $invoice->get_user_id(), 20 ), |
|
114 | - 'description' => getpaid_limit_length( $invoice->get_full_name(), 255 ), |
|
115 | - 'email' => getpaid_limit_length( $invoice->get_email(), 255 ), |
|
113 | + 'merchantCustomerId' => getpaid_limit_length($invoice->get_user_id(), 20), |
|
114 | + 'description' => getpaid_limit_length($invoice->get_full_name(), 255), |
|
115 | + 'email' => getpaid_limit_length($invoice->get_email(), 255), |
|
116 | 116 | 'paymentProfiles' => array( |
117 | 117 | 'customerType' => 'individual', |
118 | 118 | |
119 | 119 | // Billing information. |
120 | 120 | 'billTo' => array( |
121 | - 'firstName' => getpaid_limit_length( $invoice->get_first_name(), 50 ), |
|
122 | - 'lastName' => getpaid_limit_length( $invoice->get_last_name(), 50 ), |
|
123 | - 'address' => getpaid_limit_length( $invoice->get_last_name(), 60 ), |
|
124 | - 'city' => getpaid_limit_length( $invoice->get_city(), 40 ), |
|
125 | - 'state' => getpaid_limit_length( $invoice->get_state(), 40 ), |
|
126 | - 'zip' => getpaid_limit_length( $invoice->get_zip(), 20 ), |
|
127 | - 'country' => getpaid_limit_length( $invoice->get_country(), 60 ), |
|
121 | + 'firstName' => getpaid_limit_length($invoice->get_first_name(), 50), |
|
122 | + 'lastName' => getpaid_limit_length($invoice->get_last_name(), 50), |
|
123 | + 'address' => getpaid_limit_length($invoice->get_last_name(), 60), |
|
124 | + 'city' => getpaid_limit_length($invoice->get_city(), 40), |
|
125 | + 'state' => getpaid_limit_length($invoice->get_state(), 40), |
|
126 | + 'zip' => getpaid_limit_length($invoice->get_zip(), 20), |
|
127 | + 'country' => getpaid_limit_length($invoice->get_country(), 60), |
|
128 | 128 | ), |
129 | 129 | |
130 | 130 | // Payment information. |
131 | - 'payment' => $this->get_payment_information( $submission_data['authorizenet'] ), |
|
131 | + 'payment' => $this->get_payment_information($submission_data['authorizenet']), |
|
132 | 132 | ) |
133 | 133 | ), |
134 | - 'validationMode' => $this->is_sandbox( $invoice ) ? 'testMode' : 'liveMode', |
|
134 | + 'validationMode' => $this->is_sandbox($invoice) ? 'testMode' : 'liveMode', |
|
135 | 135 | ) |
136 | 136 | ); |
137 | 137 | |
138 | - $response = $this->post( apply_filters( 'getpaid_authorizenet_customer_profile_args', $args, $invoice ), $invoice ); |
|
138 | + $response = $this->post(apply_filters('getpaid_authorizenet_customer_profile_args', $args, $invoice), $invoice); |
|
139 | 139 | |
140 | - if ( is_wp_error( $response ) ) { |
|
140 | + if (is_wp_error($response)) { |
|
141 | 141 | return $response; |
142 | 142 | } |
143 | 143 | |
144 | - update_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), $response->customerProfileId ); |
|
144 | + update_user_meta($invoice->get_user_id(), $this->get_customer_profile_meta_name($invoice), $response->customerProfileId); |
|
145 | 145 | |
146 | 146 | // Save the payment token. |
147 | - if ( $save ) { |
|
147 | + if ($save) { |
|
148 | 148 | $this->save_token( |
149 | 149 | array( |
150 | 150 | 'id' => $response->customerPaymentProfileIdList[0], |
151 | - 'name' => $this->get_card_name( $submission_data['authorizenet']['cc_number'] ) . '····' . substr( $submission_data['authorizenet']['cc_number'], -4 ), |
|
151 | + 'name' => $this->get_card_name($submission_data['authorizenet']['cc_number']) . '····' . substr($submission_data['authorizenet']['cc_number'], -4), |
|
152 | 152 | 'default' => true, |
153 | - 'type' => $this->is_sandbox( $invoice ) ? 'sandbox' : 'live', |
|
153 | + 'type' => $this->is_sandbox($invoice) ? 'sandbox' : 'live', |
|
154 | 154 | ) |
155 | 155 | ); |
156 | 156 | } |
157 | 157 | |
158 | 158 | // Add a note about the validation response. |
159 | 159 | $invoice->add_note( |
160 | - sprintf( __( 'Created Authorize.NET customer profile: %s', 'invoicing' ), $response->validationDirectResponseList[0] ), |
|
160 | + sprintf(__('Created Authorize.NET customer profile: %s', 'invoicing'), $response->validationDirectResponseList[0]), |
|
161 | 161 | false, |
162 | 162 | false, |
163 | 163 | true |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | * @return string|WP_Error Profile id. |
175 | 175 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-profile |
176 | 176 | */ |
177 | - public function get_customer_profile( $profile_id ) { |
|
177 | + public function get_customer_profile($profile_id) { |
|
178 | 178 | |
179 | 179 | // Generate args. |
180 | 180 | $args = array( |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | ) |
185 | 185 | ); |
186 | 186 | |
187 | - return $this->post( $args, false ); |
|
187 | + return $this->post($args, false); |
|
188 | 188 | |
189 | 189 | } |
190 | 190 | |
@@ -199,10 +199,10 @@ discard block |
||
199 | 199 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
200 | 200 | * @return string|WP_Error Profile id. |
201 | 201 | */ |
202 | - public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
202 | + public function create_customer_payment_profile($customer_profile, $invoice, $submission_data, $save) { |
|
203 | 203 | |
204 | 204 | // Remove non-digits from the number |
205 | - $submission_data['authorizenet']['cc_number'] = preg_replace('/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
|
205 | + $submission_data['authorizenet']['cc_number'] = preg_replace('/\D/', '', $submission_data['authorizenet']['cc_number']); |
|
206 | 206 | |
207 | 207 | // Generate args. |
208 | 208 | $args = array( |
@@ -213,34 +213,34 @@ discard block |
||
213 | 213 | |
214 | 214 | // Billing information. |
215 | 215 | 'billTo' => array( |
216 | - 'firstName' => getpaid_limit_length( $invoice->get_first_name(), 50 ), |
|
217 | - 'lastName' => getpaid_limit_length( $invoice->get_last_name(), 50 ), |
|
218 | - 'address' => getpaid_limit_length( $invoice->get_last_name(), 60 ), |
|
219 | - 'city' => getpaid_limit_length( $invoice->get_city(), 40 ), |
|
220 | - 'state' => getpaid_limit_length( $invoice->get_state(), 40 ), |
|
221 | - 'zip' => getpaid_limit_length( $invoice->get_zip(), 20 ), |
|
222 | - 'country' => getpaid_limit_length( $invoice->get_country(), 60 ), |
|
216 | + 'firstName' => getpaid_limit_length($invoice->get_first_name(), 50), |
|
217 | + 'lastName' => getpaid_limit_length($invoice->get_last_name(), 50), |
|
218 | + 'address' => getpaid_limit_length($invoice->get_last_name(), 60), |
|
219 | + 'city' => getpaid_limit_length($invoice->get_city(), 40), |
|
220 | + 'state' => getpaid_limit_length($invoice->get_state(), 40), |
|
221 | + 'zip' => getpaid_limit_length($invoice->get_zip(), 20), |
|
222 | + 'country' => getpaid_limit_length($invoice->get_country(), 60), |
|
223 | 223 | ), |
224 | 224 | |
225 | 225 | // Payment information. |
226 | - 'payment' => $this->get_payment_information( $submission_data['authorizenet'] ) |
|
226 | + 'payment' => $this->get_payment_information($submission_data['authorizenet']) |
|
227 | 227 | ), |
228 | - 'validationMode' => $this->is_sandbox( $invoice ) ? 'testMode' : 'liveMode', |
|
228 | + 'validationMode' => $this->is_sandbox($invoice) ? 'testMode' : 'liveMode', |
|
229 | 229 | ) |
230 | 230 | ); |
231 | 231 | |
232 | - $response = $this->post( apply_filters( 'getpaid_authorizenet_create_customer_payment_profile_args', $args, $invoice ), $invoice ); |
|
232 | + $response = $this->post(apply_filters('getpaid_authorizenet_create_customer_payment_profile_args', $args, $invoice), $invoice); |
|
233 | 233 | |
234 | - if ( is_wp_error( $response ) ) { |
|
234 | + if (is_wp_error($response)) { |
|
235 | 235 | return $response; |
236 | 236 | } |
237 | 237 | |
238 | 238 | // Save the payment token. |
239 | - if ( $save ) { |
|
239 | + if ($save) { |
|
240 | 240 | $this->save_token( |
241 | 241 | array( |
242 | 242 | 'id' => $response->customerPaymentProfileId, |
243 | - 'name' => $this->get_card_name( $submission_data['authorizenet']['cc_number'] ) . ' ···· ' . substr( $submission_data['authorizenet']['cc_number'], -4 ), |
|
243 | + 'name' => $this->get_card_name($submission_data['authorizenet']['cc_number']) . ' ···· ' . substr($submission_data['authorizenet']['cc_number'], -4), |
|
244 | 244 | 'default' => true |
245 | 245 | ) |
246 | 246 | ); |
@@ -248,7 +248,7 @@ discard block |
||
248 | 248 | |
249 | 249 | // Add a note about the validation response. |
250 | 250 | $invoice->add_note( |
251 | - sprintf( __( 'Saved Authorize.NET payment profile: %s', 'invoicing' ), $response->validationDirectResponse ), |
|
251 | + sprintf(__('Saved Authorize.NET payment profile: %s', 'invoicing'), $response->validationDirectResponse), |
|
252 | 252 | false, |
253 | 253 | false, |
254 | 254 | true |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | * @return string|WP_Error Profile id. |
268 | 268 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-payment-profile |
269 | 269 | */ |
270 | - public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
270 | + public function get_customer_payment_profile($customer_profile_id, $payment_profile_id) { |
|
271 | 271 | |
272 | 272 | // Generate args. |
273 | 273 | $args = array( |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | ) |
279 | 279 | ); |
280 | 280 | |
281 | - return $this->post( $args, false ); |
|
281 | + return $this->post($args, false); |
|
282 | 282 | |
283 | 283 | } |
284 | 284 | |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | * @link https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-customer-profile |
292 | 292 | * @return WP_Error|object |
293 | 293 | */ |
294 | - public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
294 | + public function charge_customer_payment_profile($customer_profile_id, $payment_profile_id, $invoice) { |
|
295 | 295 | |
296 | 296 | // Generate args. |
297 | 297 | $args = array( |
@@ -311,24 +311,24 @@ discard block |
||
311 | 311 | ) |
312 | 312 | ), |
313 | 313 | 'order' => array( |
314 | - 'invoiceNumber' => getpaid_limit_length( $invoice->get_number(), 20 ), |
|
314 | + 'invoiceNumber' => getpaid_limit_length($invoice->get_number(), 20), |
|
315 | 315 | ), |
316 | - 'lineItems' => array( 'lineItem' => $this->get_line_items( $invoice ) ), |
|
316 | + 'lineItems' => array('lineItem' => $this->get_line_items($invoice)), |
|
317 | 317 | 'tax' => array( |
318 | 318 | 'amount' => $invoice->get_total_tax(), |
319 | 319 | 'name' => getpaid_tax()->get_vat_name(), |
320 | 320 | ), |
321 | - 'poNumber' => getpaid_limit_length( $invoice->get_number(), 25 ), |
|
321 | + 'poNumber' => getpaid_limit_length($invoice->get_number(), 25), |
|
322 | 322 | 'customer' => array( |
323 | - 'id' => getpaid_limit_length( $invoice->get_user_id(), 25 ), |
|
324 | - 'email' => getpaid_limit_length( $invoice->get_email(), 25 ), |
|
323 | + 'id' => getpaid_limit_length($invoice->get_user_id(), 25), |
|
324 | + 'email' => getpaid_limit_length($invoice->get_email(), 25), |
|
325 | 325 | ), |
326 | 326 | 'customerIP' => $invoice->get_ip(), |
327 | 327 | ) |
328 | 328 | ) |
329 | 329 | ); |
330 | -log_noptin_message( $args ); |
|
331 | - return $this->post( apply_filters( 'getpaid_authorizenet_charge_customer_payment_profile_args', $args, $invoice ), $invoice ); |
|
330 | +log_noptin_message($args); |
|
331 | + return $this->post(apply_filters('getpaid_authorizenet_charge_customer_payment_profile_args', $args, $invoice), $invoice); |
|
332 | 332 | |
333 | 333 | } |
334 | 334 | |
@@ -338,43 +338,43 @@ discard block |
||
338 | 338 | * @param stdClass $result Api response. |
339 | 339 | * @param WPInv_Invoice $invoice Invoice. |
340 | 340 | */ |
341 | - public function process_charge_response( $result, $invoice ) { |
|
341 | + public function process_charge_response($result, $invoice) { |
|
342 | 342 | |
343 | 343 | wpinv_clear_errors(); |
344 | 344 | |
345 | - switch ( (int) $result->transactionResponse->responseCode ) { |
|
345 | + switch ((int) $result->transactionResponse->responseCode) { |
|
346 | 346 | |
347 | 347 | case 1: |
348 | 348 | case 4: |
349 | 349 | |
350 | - if ( ! empty( $result->transactionResponse->transId ) ) { |
|
351 | - $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
350 | + if (!empty($result->transactionResponse->transId)) { |
|
351 | + $invoice->set_transaction_id($result->transactionResponse->transId); |
|
352 | 352 | } |
353 | 353 | |
354 | - if ( 1 == (int) $result->transactionResponse->responseCode ) { |
|
354 | + if (1 == (int) $result->transactionResponse->responseCode) { |
|
355 | 355 | $invoice->mark_paid(); |
356 | 356 | } else { |
357 | - $invoice->set_status( 'wpi-onhold' ); |
|
357 | + $invoice->set_status('wpi-onhold'); |
|
358 | 358 | $invoice->add_note( |
359 | 359 | sprintf( |
360 | - __( 'Held for review: %s', 'invoicing' ), |
|
360 | + __('Held for review: %s', 'invoicing'), |
|
361 | 361 | $result->transactionResponse->messages->message[0]->description |
362 | 362 | ) |
363 | 363 | ); |
364 | 364 | } |
365 | 365 | |
366 | - $invoice->add_note( sprintf( __( 'Authentication code: %s (%s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
366 | + $invoice->add_note(sprintf(__('Authentication code: %s (%s).', 'invoicing'), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber), false, false, true); |
|
367 | 367 | $invoice->save(); |
368 | 368 | |
369 | 369 | return; |
370 | 370 | |
371 | 371 | case 2: |
372 | 372 | case 3: |
373 | - wpinv_set_error( 'card_declined', __( 'Credit card declined.', 'invoicing' ) ); |
|
373 | + wpinv_set_error('card_declined', __('Credit card declined.', 'invoicing')); |
|
374 | 374 | |
375 | - if ( ! empty( $result->transactionResponse->errors ) ) { |
|
375 | + if (!empty($result->transactionResponse->errors)) { |
|
376 | 376 | $errors = (object) $result->transactionResponse->errors; |
377 | - wpinv_set_error( $errors->error[0]->errorCode, $errors->error[0]->errorText ); |
|
377 | + wpinv_set_error($errors->error[0]->errorCode, $errors->error[0]->errorText); |
|
378 | 378 | } |
379 | 379 | |
380 | 380 | return; |
@@ -390,10 +390,10 @@ discard block |
||
390 | 390 | * @param array $card Card details. |
391 | 391 | * @return array |
392 | 392 | */ |
393 | - public function get_payment_information( $card ) { |
|
393 | + public function get_payment_information($card) { |
|
394 | 394 | return array( |
395 | 395 | |
396 | - 'creditCard' => array ( |
|
396 | + 'creditCard' => array( |
|
397 | 397 | 'cardNumber' => $card['cc_number'], |
398 | 398 | 'expirationDate' => $card['cc_expire_year'] . '-' . $card['cc_expire_month'], |
399 | 399 | 'cardCode' => $card['cc_cvv2'], |
@@ -409,30 +409,30 @@ discard block |
||
409 | 409 | * @param string $card_number Card number. |
410 | 410 | * @return string |
411 | 411 | */ |
412 | - public function get_card_name( $card_number ) { |
|
412 | + public function get_card_name($card_number) { |
|
413 | 413 | |
414 | - switch( $card_number ) { |
|
414 | + switch ($card_number) { |
|
415 | 415 | |
416 | - case( preg_match ( '/^4/', $card_number ) >= 1 ): |
|
417 | - return __( 'Visa', 'invoicing' ); |
|
416 | + case(preg_match('/^4/', $card_number) >= 1): |
|
417 | + return __('Visa', 'invoicing'); |
|
418 | 418 | |
419 | - case( preg_match ( '/^5[1-5]/', $card_number ) >= 1 ): |
|
420 | - return __( 'Mastercard', 'invoicing' ); |
|
419 | + case(preg_match('/^5[1-5]/', $card_number) >= 1): |
|
420 | + return __('Mastercard', 'invoicing'); |
|
421 | 421 | |
422 | - case( preg_match ( '/^3[47]/', $card_number ) >= 1 ): |
|
423 | - return __( 'Amex', 'invoicing' ); |
|
422 | + case(preg_match('/^3[47]/', $card_number) >= 1): |
|
423 | + return __('Amex', 'invoicing'); |
|
424 | 424 | |
425 | - case( preg_match ( '/^3(?:0[0-5]|[68])/', $card_number ) >= 1 ): |
|
426 | - return __( 'Diners Club', 'invoicing' ); |
|
425 | + case(preg_match('/^3(?:0[0-5]|[68])/', $card_number) >= 1): |
|
426 | + return __('Diners Club', 'invoicing'); |
|
427 | 427 | |
428 | - case( preg_match ( '/^6(?:011|5)/', $card_number ) >= 1 ): |
|
429 | - return __( 'Discover', 'invoicing' ); |
|
428 | + case(preg_match('/^6(?:011|5)/', $card_number) >= 1): |
|
429 | + return __('Discover', 'invoicing'); |
|
430 | 430 | |
431 | - case( preg_match ( '/^(?:2131|1800|35\d{3})/', $card_number ) >= 1 ): |
|
432 | - return __( 'JCB', 'invoicing' ); |
|
431 | + case(preg_match('/^(?:2131|1800|35\d{3})/', $card_number) >= 1): |
|
432 | + return __('JCB', 'invoicing'); |
|
433 | 433 | |
434 | 434 | default: |
435 | - return __( 'Card', 'invoicing' ); |
|
435 | + return __('Card', 'invoicing'); |
|
436 | 436 | break; |
437 | 437 | } |
438 | 438 | |
@@ -445,8 +445,8 @@ discard block |
||
445 | 445 | * @param WPInv_Invoice $invoice Invoice. |
446 | 446 | * @return string |
447 | 447 | */ |
448 | - public function get_customer_profile_meta_name( $invoice ) { |
|
449 | - return $this->is_sandbox( $invoice ) ? 'getpaid_authorizenet_sandbox_customer_profile_id' : 'getpaid_authorizenet_customer_profile_id'; |
|
448 | + public function get_customer_profile_meta_name($invoice) { |
|
449 | + return $this->is_sandbox($invoice) ? 'getpaid_authorizenet_sandbox_customer_profile_id' : 'getpaid_authorizenet_customer_profile_id'; |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | /** |
@@ -456,8 +456,8 @@ discard block |
||
456 | 456 | * @param WPInv_Invoice $invoice Invoice. |
457 | 457 | * @return string |
458 | 458 | */ |
459 | - public function get_api_url( $invoice ) { |
|
460 | - return $this->is_sandbox( $invoice ) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
|
459 | + public function get_api_url($invoice) { |
|
460 | + return $this->is_sandbox($invoice) ? 'https://apitest.authorize.net/xml/v1/request.api' : 'https://api.authorize.net/xml/v1/request.api'; |
|
461 | 461 | } |
462 | 462 | |
463 | 463 | /** |
@@ -469,8 +469,8 @@ discard block |
||
469 | 469 | public function get_auth_params() { |
470 | 470 | |
471 | 471 | return array( |
472 | - 'name' => $this->get_option( 'login_id' ), |
|
473 | - 'transactionKey' => $this->get_option( 'transaction_key' ), |
|
472 | + 'name' => $this->get_option('login_id'), |
|
473 | + 'transactionKey' => $this->get_option('transaction_key'), |
|
474 | 474 | ); |
475 | 475 | |
476 | 476 | } |
@@ -483,34 +483,34 @@ discard block |
||
483 | 483 | * @param WPInv_Invoice $invoice |
484 | 484 | * @return WP_Error|string The payment profile id |
485 | 485 | */ |
486 | - public function validate_submission_data( $submission_data, $invoice ) { |
|
486 | + public function validate_submission_data($submission_data, $invoice) { |
|
487 | 487 | |
488 | 488 | // Validate authentication details. |
489 | 489 | $auth = $this->get_auth_params(); |
490 | 490 | |
491 | - if ( empty( $auth['name'] ) || empty( $auth['transactionKey'] ) ) { |
|
492 | - return new WP_Error( 'invalid_settings', __( 'This gateway has not been set up.', 'invoicing') ); |
|
491 | + if (empty($auth['name']) || empty($auth['transactionKey'])) { |
|
492 | + return new WP_Error('invalid_settings', __('This gateway has not been set up.', 'invoicing')); |
|
493 | 493 | } |
494 | 494 | |
495 | 495 | // Validate the payment method. |
496 | - if ( empty( $submission_data['getpaid-authorizenet-payment-method'] ) ) { |
|
497 | - return new WP_Error( 'invalid_payment_method', __( 'Please select a different payment method or add a new card.', 'invoicing') ); |
|
496 | + if (empty($submission_data['getpaid-authorizenet-payment-method'])) { |
|
497 | + return new WP_Error('invalid_payment_method', __('Please select a different payment method or add a new card.', 'invoicing')); |
|
498 | 498 | } |
499 | 499 | |
500 | 500 | // Are we adding a new payment method? |
501 | - if ( 'new' != $submission_data['getpaid-authorizenet-payment-method'] ) { |
|
501 | + if ('new' != $submission_data['getpaid-authorizenet-payment-method']) { |
|
502 | 502 | return $submission_data['getpaid-authorizenet-payment-method']; |
503 | 503 | } |
504 | 504 | |
505 | 505 | // Retrieve the customer profile id. |
506 | - $profile_id = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
506 | + $profile_id = get_user_meta($invoice->get_user_id(), $this->get_customer_profile_meta_name($invoice), true); |
|
507 | 507 | |
508 | 508 | // Create payment method. |
509 | - if ( empty( $profile_id ) ) { |
|
510 | - return $this->create_customer_profile( $invoice, $submission_data, ! empty( $submission_data['getpaid-authorizenet-new-payment-method'] ) ); |
|
509 | + if (empty($profile_id)) { |
|
510 | + return $this->create_customer_profile($invoice, $submission_data, !empty($submission_data['getpaid-authorizenet-new-payment-method'])); |
|
511 | 511 | } |
512 | 512 | |
513 | - return $this->create_customer_payment_profile( $profile_id, $invoice, $submission_data, ! empty( $submission_data['getpaid-authorizenet-new-payment-method'] ) ); |
|
513 | + return $this->create_customer_payment_profile($profile_id, $invoice, $submission_data, !empty($submission_data['getpaid-authorizenet-new-payment-method'])); |
|
514 | 514 | |
515 | 515 | } |
516 | 516 | |
@@ -521,15 +521,15 @@ discard block |
||
521 | 521 | * @param WPInv_Invoice $invoice Invoice. |
522 | 522 | * @return array |
523 | 523 | */ |
524 | - public function get_line_items( $invoice ) { |
|
524 | + public function get_line_items($invoice) { |
|
525 | 525 | $items = array(); |
526 | 526 | |
527 | - foreach ( $invoice->get_items() as $item ) { |
|
527 | + foreach ($invoice->get_items() as $item) { |
|
528 | 528 | |
529 | 529 | $items[] = array( |
530 | - 'itemId' => getpaid_limit_length( $item->get_id(), 31 ), |
|
531 | - 'name' => getpaid_limit_length( $item->get_raw_name(), 31 ), |
|
532 | - 'description' => getpaid_limit_length( $item->get_description(), 255 ), |
|
530 | + 'itemId' => getpaid_limit_length($item->get_id(), 31), |
|
531 | + 'name' => getpaid_limit_length($item->get_raw_name(), 31), |
|
532 | + 'description' => getpaid_limit_length($item->get_description(), 255), |
|
533 | 533 | 'quantity' => (int) $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(), |
534 | 534 | 'unitPrice' => (float) $item->get_price(), |
535 | 535 | 'taxable' => wpinv_use_taxes() && $invoice->is_taxable() && 'tax-exempt' != $item->get_vat_rule(), |
@@ -548,40 +548,40 @@ discard block |
||
548 | 548 | * @param WPInv_Invoice $invoice Invoice. |
549 | 549 | * @return stdClass|WP_Error |
550 | 550 | */ |
551 | - public function post( $post, $invoice ){ |
|
551 | + public function post($post, $invoice) { |
|
552 | 552 | |
553 | - $url = $this->get_api_url( $invoice ); |
|
553 | + $url = $this->get_api_url($invoice); |
|
554 | 554 | $response = wp_remote_post( |
555 | 555 | $url, |
556 | 556 | array( |
557 | 557 | 'headers' => array( |
558 | 558 | 'Content-Type' => 'application/json; charset=utf-8' |
559 | 559 | ), |
560 | - 'body' => json_encode( $post ), |
|
560 | + 'body' => json_encode($post), |
|
561 | 561 | 'method' => 'POST' |
562 | 562 | ) |
563 | 563 | ); |
564 | 564 | |
565 | - if ( is_wp_error( $response ) ) { |
|
565 | + if (is_wp_error($response)) { |
|
566 | 566 | return $response; |
567 | 567 | } |
568 | 568 | |
569 | - $response = wp_unslash( wp_remote_retrieve_body( $response ) ); |
|
569 | + $response = wp_unslash(wp_remote_retrieve_body($response)); |
|
570 | 570 | $response = preg_replace('/\xEF\xBB\xBF/', '', $response); // https://community.developer.authorize.net/t5/Integration-and-Testing/JSON-issues/td-p/48851 |
571 | - $response = json_decode( $response ); |
|
571 | + $response = json_decode($response); |
|
572 | 572 | |
573 | - if ( empty( $response ) ) { |
|
574 | - return new WP_Error( 'invalid_reponse', __( 'Invalid response', 'invoicing' ) ); |
|
573 | + if (empty($response)) { |
|
574 | + return new WP_Error('invalid_reponse', __('Invalid response', 'invoicing')); |
|
575 | 575 | } |
576 | 576 | |
577 | - if ( $response->messages->resultCode == 'Error' ) { |
|
577 | + if ($response->messages->resultCode == 'Error') { |
|
578 | 578 | |
579 | - if ( ! empty( $response->transactionResponse ) && ! empty( $response->transactionResponse->errors ) ) { |
|
579 | + if (!empty($response->transactionResponse) && !empty($response->transactionResponse->errors)) { |
|
580 | 580 | $error = $response->transactionResponse->errors[0]; |
581 | - return new WP_Error( $error->errorCode, $error->errorText ); |
|
581 | + return new WP_Error($error->errorCode, $error->errorText); |
|
582 | 582 | } |
583 | 583 | |
584 | - return new WP_Error( $response->messages->message[0]->code, $response->messages->message[0]->text ); |
|
584 | + return new WP_Error($response->messages->message[0]->code, $response->messages->message[0]->text); |
|
585 | 585 | } |
586 | 586 | |
587 | 587 | return $response; |
@@ -597,49 +597,49 @@ discard block |
||
597 | 597 | * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
598 | 598 | * @return array |
599 | 599 | */ |
600 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
600 | + public function process_payment($invoice, $submission_data, $submission) { |
|
601 | 601 | |
602 | 602 | // Validate the submitted data. |
603 | - $payment_profile_id = $this->validate_submission_data( $submission_data, $invoice ); |
|
603 | + $payment_profile_id = $this->validate_submission_data($submission_data, $invoice); |
|
604 | 604 | |
605 | 605 | // Do we have an error? |
606 | - if ( is_wp_error( $payment_profile_id ) ) { |
|
607 | - wpinv_set_error( $payment_profile_id->get_error_code(), $payment_profile_id->get_error_message() ); |
|
606 | + if (is_wp_error($payment_profile_id)) { |
|
607 | + wpinv_set_error($payment_profile_id->get_error_code(), $payment_profile_id->get_error_message()); |
|
608 | 608 | wpinv_send_back_to_checkout(); |
609 | 609 | } |
610 | 610 | |
611 | 611 | // Save the payment method to the order. |
612 | - update_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', $payment_profile_id ); |
|
612 | + update_post_meta($invoice->get_id(), 'getpaid_authorizenet_profile_id', $payment_profile_id); |
|
613 | 613 | |
614 | 614 | // Check if this is a subscription or not. |
615 | - if ( $invoice->is_recurring() && $subscription = wpinv_get_subscription( $invoice ) ) { |
|
616 | - $this->process_subscription( $invoice, $subscription ); |
|
615 | + if ($invoice->is_recurring() && $subscription = wpinv_get_subscription($invoice)) { |
|
616 | + $this->process_subscription($invoice, $subscription); |
|
617 | 617 | } |
618 | 618 | |
619 | 619 | // If it is free, send to the success page. |
620 | - if ( ! $invoice->needs_payment() ) { |
|
620 | + if (!$invoice->needs_payment()) { |
|
621 | 621 | $invoice->mark_paid(); |
622 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
622 | + wpinv_send_to_success_page(array('invoice_key' => $invoice->get_key())); |
|
623 | 623 | } |
624 | 624 | |
625 | 625 | // Charge the payment profile. |
626 | - $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
627 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
626 | + $customer_profile = get_user_meta($invoice->get_user_id(), $this->get_customer_profile_meta_name($invoice), true); |
|
627 | + $result = $this->charge_customer_payment_profile($customer_profile, $payment_profile_id, $invoice); |
|
628 | 628 | |
629 | 629 | // Do we have an error? |
630 | - if ( is_wp_error( $result ) ) { |
|
631 | - wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
630 | + if (is_wp_error($result)) { |
|
631 | + wpinv_set_error($result->get_error_code(), $result->get_error_message()); |
|
632 | 632 | wpinv_send_back_to_checkout(); |
633 | 633 | } |
634 | 634 | |
635 | 635 | // Process the response. |
636 | - $this->process_charge_response( $result, $invoice ); |
|
636 | + $this->process_charge_response($result, $invoice); |
|
637 | 637 | |
638 | - if ( wpinv_get_errors() ) { |
|
638 | + if (wpinv_get_errors()) { |
|
639 | 639 | wpinv_send_back_to_checkout(); |
640 | 640 | } |
641 | 641 | |
642 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
642 | + wpinv_send_to_success_page(array('invoice_key' => $invoice->get_key())); |
|
643 | 643 | |
644 | 644 | exit; |
645 | 645 | |
@@ -651,40 +651,40 @@ discard block |
||
651 | 651 | * @param WPInv_Invoice $invoice Invoice. |
652 | 652 | * @param WPInv_Subscription $subscription Subscription. |
653 | 653 | */ |
654 | - public function process_subscription( $invoice, $subscription ) { |
|
654 | + public function process_subscription($invoice, $subscription) { |
|
655 | 655 | |
656 | 656 | // Check if there is an initial amount to charge. |
657 | - if ( (float) $invoice->get_total() > 0 ) { |
|
657 | + if ((float) $invoice->get_total() > 0) { |
|
658 | 658 | |
659 | 659 | // Retrieve the payment method. |
660 | - $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
661 | - $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
662 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
660 | + $payment_profile_id = get_post_meta($invoice->get_id(), 'getpaid_authorizenet_profile_id', true); |
|
661 | + $customer_profile = get_user_meta($invoice->get_user_id(), $this->get_customer_profile_meta_name($invoice), true); |
|
662 | + $result = $this->charge_customer_payment_profile($customer_profile, $payment_profile_id, $invoice); |
|
663 | 663 | |
664 | 664 | // Do we have an error? |
665 | - if ( is_wp_error( $result ) ) { |
|
666 | - wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
665 | + if (is_wp_error($result)) { |
|
666 | + wpinv_set_error($result->get_error_code(), $result->get_error_message()); |
|
667 | 667 | wpinv_send_back_to_checkout(); |
668 | 668 | } |
669 | 669 | |
670 | 670 | // Process the response. |
671 | - $this->process_charge_response( $result, $invoice ); |
|
671 | + $this->process_charge_response($result, $invoice); |
|
672 | 672 | |
673 | - if ( wpinv_get_errors() ) { |
|
673 | + if (wpinv_get_errors()) { |
|
674 | 674 | wpinv_send_back_to_checkout(); |
675 | 675 | } |
676 | 676 | |
677 | 677 | } |
678 | 678 | |
679 | 679 | // Recalculate the new subscription expiry. |
680 | - $duration = strtotime( $subscription->expiration ) - strtotime( $subscription->created ); |
|
681 | - $expiry = date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ); |
|
680 | + $duration = strtotime($subscription->expiration) - strtotime($subscription->created); |
|
681 | + $expiry = date('Y-m-d H:i:s', (current_time('timestamp') + $duration)); |
|
682 | 682 | |
683 | 683 | // Schedule an action to run when the subscription expires. |
684 | 684 | $action_id = as_schedule_single_action( |
685 | - strtotime( $expiry ), |
|
685 | + strtotime($expiry), |
|
686 | 686 | 'wpinv_renew_authorizenet_subscription_profile', |
687 | - array( $invoice->get_id() ), |
|
687 | + array($invoice->get_id()), |
|
688 | 688 | 'invoicing' |
689 | 689 | ); |
690 | 690 | |
@@ -693,12 +693,12 @@ discard block |
||
693 | 693 | array( |
694 | 694 | 'profile_id' => $action_id, |
695 | 695 | 'status' => 'trialling' == $subscription->status ? 'trialling' : 'active', |
696 | - 'created' => current_time( 'mysql' ), |
|
696 | + 'created' => current_time('mysql'), |
|
697 | 697 | 'expiration' => $expiry, |
698 | 698 | ) |
699 | 699 | ); |
700 | 700 | |
701 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
701 | + wpinv_send_to_success_page(array('invoice_key' => $invoice->get_key())); |
|
702 | 702 | |
703 | 703 | } |
704 | 704 | |
@@ -707,45 +707,45 @@ discard block |
||
707 | 707 | * |
708 | 708 | * @param int $invoice Invoice id. |
709 | 709 | */ |
710 | - public function renew_subscription( $invoice ) { |
|
710 | + public function renew_subscription($invoice) { |
|
711 | 711 | |
712 | 712 | // Retrieve the subscription. |
713 | - $subscription = wpinv_get_subscription( $invoice ); |
|
714 | - if ( empty( $subscription ) ) { |
|
713 | + $subscription = wpinv_get_subscription($invoice); |
|
714 | + if (empty($subscription)) { |
|
715 | 715 | return; |
716 | 716 | } |
717 | 717 | |
718 | 718 | // Abort if it is canceled or complete. |
719 | - if ( $subscription->status == 'completed' || $subscription->status == 'cancelled' ) { |
|
719 | + if ($subscription->status == 'completed' || $subscription->status == 'cancelled') { |
|
720 | 720 | return; |
721 | 721 | } |
722 | 722 | |
723 | 723 | // Retrieve the invoice. |
724 | - $invoice = new WPInv_Invoice( $invoice ); |
|
724 | + $invoice = new WPInv_Invoice($invoice); |
|
725 | 725 | |
726 | 726 | // If we have not maxed out on bill times... |
727 | 727 | $times_billed = $subscription->get_times_billed(); |
728 | 728 | $max_bills = $subscription->bill_times; |
729 | 729 | |
730 | - if ( empty( $max_bills ) || $max_bills > $times_billed ) { |
|
730 | + if (empty($max_bills) || $max_bills > $times_billed) { |
|
731 | 731 | |
732 | 732 | $new_invoice = $subscription->create_payment(); |
733 | 733 | |
734 | - if ( empty( $new_invoice ) ) { |
|
735 | - $invoice->add_note( __( 'Error generating a renewal invoice.', 'invoicing' ), false, false, false ); |
|
734 | + if (empty($new_invoice)) { |
|
735 | + $invoice->add_note(__('Error generating a renewal invoice.', 'invoicing'), false, false, false); |
|
736 | 736 | $subscription->failing(); |
737 | 737 | return; |
738 | 738 | } |
739 | 739 | |
740 | 740 | // retrieve the payment method. |
741 | - $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
742 | - $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
743 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
741 | + $payment_profile_id = get_post_meta($invoice->get_id(), 'getpaid_authorizenet_profile_id', true); |
|
742 | + $customer_profile = get_user_meta($invoice->get_user_id(), $this->get_customer_profile_meta_name($invoice), true); |
|
743 | + $result = $this->charge_customer_payment_profile($customer_profile, $payment_profile_id, $new_invoice); |
|
744 | 744 | |
745 | 745 | // Do we have an error? |
746 | - if ( is_wp_error( $result ) ) { |
|
746 | + if (is_wp_error($result)) { |
|
747 | 747 | $invoice->add_note( |
748 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
748 | + sprintf(__('Error renewing subscription : ( %s ).', 'invoicing'), $result->get_error_message()), |
|
749 | 749 | true, |
750 | 750 | false, |
751 | 751 | true |
@@ -755,12 +755,12 @@ discard block |
||
755 | 755 | } |
756 | 756 | |
757 | 757 | // Process the response. |
758 | - $this->process_charge_response( $result, $new_invoice ); |
|
758 | + $this->process_charge_response($result, $new_invoice); |
|
759 | 759 | |
760 | - if ( wpinv_get_errors() ) { |
|
760 | + if (wpinv_get_errors()) { |
|
761 | 761 | |
762 | 762 | $invoice->add_note( |
763 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
763 | + sprintf(__('Error renewing subscription : ( %s ).', 'invoicing'), getpaid_get_errors_html()), |
|
764 | 764 | true, |
765 | 765 | false, |
766 | 766 | true |
@@ -782,17 +782,17 @@ discard block |
||
782 | 782 | // Renew/Complete the subscription. |
783 | 783 | $subscription->renew(); |
784 | 784 | |
785 | - if ( 'completed' != $subscription->status ) { |
|
785 | + if ('completed' != $subscription->status) { |
|
786 | 786 | |
787 | 787 | // Schedule an action to run when the subscription expires. |
788 | 788 | $action_id = as_schedule_single_action( |
789 | - strtotime( $subscription->expiration ), |
|
789 | + strtotime($subscription->expiration), |
|
790 | 790 | 'wpinv_renew_authorizenet_subscription_profile', |
791 | - array( $invoice->get_id() ), |
|
791 | + array($invoice->get_id()), |
|
792 | 792 | 'invoicing' |
793 | 793 | ); |
794 | 794 | |
795 | - $subscription->update( array( 'profile_id' => $action_id, ) ); |
|
795 | + $subscription->update(array('profile_id' => $action_id,)); |
|
796 | 796 | |
797 | 797 | } |
798 | 798 | |
@@ -806,9 +806,9 @@ discard block |
||
806 | 806 | * @param WPInv_Subscription $subscription Subscription. |
807 | 807 | * @param WPInv_Invoice $invoice Invoice. |
808 | 808 | */ |
809 | - public function cancel_subscription( $subscription, $invoice ) { |
|
809 | + public function cancel_subscription($subscription, $invoice) { |
|
810 | 810 | |
811 | - if ( as_unschedule_action( 'wpinv_renew_authorizenet_subscription_profile', array( $invoice->get_id() ), 'invoicing' ) ) { |
|
811 | + if (as_unschedule_action('wpinv_renew_authorizenet_subscription_profile', array($invoice->get_id()), 'invoicing')) { |
|
812 | 812 | return; |
813 | 813 | } |
814 | 814 | |
@@ -835,45 +835,45 @@ discard block |
||
835 | 835 | $this->maybe_process_old_ipn(); |
836 | 836 | |
837 | 837 | // Validate the IPN. |
838 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
839 | - wp_die( 'Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array( 'response' => 500 ) ); |
|
838 | + if (empty($_POST) || !$this->validate_ipn()) { |
|
839 | + wp_die('Authorize.NET IPN Request Failure', 'Authorize.NET IPN', array('response' => 500)); |
|
840 | 840 | } |
841 | 841 | |
842 | 842 | // Event type. |
843 | - $posted = json_decode( file_get_contents('php://input') ); |
|
844 | - if ( empty( $posted ) ) { |
|
845 | - wp_die( 'Invalid JSON', 'Authorize.NET IPN', array( 'response' => 500 ) ); |
|
843 | + $posted = json_decode(file_get_contents('php://input')); |
|
844 | + if (empty($posted)) { |
|
845 | + wp_die('Invalid JSON', 'Authorize.NET IPN', array('response' => 500)); |
|
846 | 846 | } |
847 | 847 | |
848 | 848 | // Process the IPN. |
849 | - $posted = (object) wp_unslash( $posted ); |
|
849 | + $posted = (object) wp_unslash($posted); |
|
850 | 850 | |
851 | 851 | // Process refunds. |
852 | - if ( 'net.authorize.payment.refund.created' == $posted->eventType ) { |
|
853 | - $invoice = new WPInv_Invoice( $posted->payload->merchantReferenceId ); |
|
852 | + if ('net.authorize.payment.refund.created' == $posted->eventType) { |
|
853 | + $invoice = new WPInv_Invoice($posted->payload->merchantReferenceId); |
|
854 | 854 | |
855 | - if ( $invoice->get_id() && $posted->payload->id == $invoice->get_transaction_id() ) { |
|
855 | + if ($invoice->get_id() && $posted->payload->id == $invoice->get_transaction_id()) { |
|
856 | 856 | $invoice->refund(); |
857 | 857 | } |
858 | 858 | |
859 | 859 | } |
860 | 860 | |
861 | 861 | // Held funds approved. |
862 | - if ( 'net.authorize.payment.fraud.approved' == $posted->eventType ) { |
|
863 | - $invoice = new WPInv_Invoice( $posted->payload->id ); |
|
862 | + if ('net.authorize.payment.fraud.approved' == $posted->eventType) { |
|
863 | + $invoice = new WPInv_Invoice($posted->payload->id); |
|
864 | 864 | |
865 | - if ( $invoice->get_id() && $posted->payload->id == $invoice->get_transaction_id() ) { |
|
866 | - $invoice->mark_paid( false, __( 'Payment released', 'invoicing' )); |
|
865 | + if ($invoice->get_id() && $posted->payload->id == $invoice->get_transaction_id()) { |
|
866 | + $invoice->mark_paid(false, __('Payment released', 'invoicing')); |
|
867 | 867 | } |
868 | 868 | |
869 | 869 | } |
870 | 870 | |
871 | 871 | // Held funds declined. |
872 | - if ( 'net.authorize.payment.fraud.declined' == $posted->eventType ) { |
|
873 | - $invoice = new WPInv_Invoice( $posted->payload->id ); |
|
872 | + if ('net.authorize.payment.fraud.declined' == $posted->eventType) { |
|
873 | + $invoice = new WPInv_Invoice($posted->payload->id); |
|
874 | 874 | |
875 | - if ( $invoice->get_id() && $posted->payload->id == $invoice->get_transaction_id() ) { |
|
876 | - $invoice->set_status( 'wpi-failed', __( 'Payment desclined', 'invoicing' ) ); |
|
875 | + if ($invoice->get_id() && $posted->payload->id == $invoice->get_transaction_id()) { |
|
876 | + $invoice->set_status('wpi-failed', __('Payment desclined', 'invoicing')); |
|
877 | 877 | $invoice->save(); |
878 | 878 | } |
879 | 879 | |
@@ -891,41 +891,41 @@ discard block |
||
891 | 891 | public function maybe_process_old_ipn() { |
892 | 892 | |
893 | 893 | // Ensure that we are using the old subscriptions. |
894 | - if ( empty( $_POST['x_subscription_id'] ) ) { |
|
894 | + if (empty($_POST['x_subscription_id'])) { |
|
895 | 895 | return; |
896 | 896 | } |
897 | 897 | |
898 | 898 | // Check validity. |
899 | - $signature = $this->get_option( 'signature_key' ); |
|
900 | - if ( ! empty( $signature ) ) { |
|
901 | - $login_id = $this->get_option( 'login_id' ); |
|
899 | + $signature = $this->get_option('signature_key'); |
|
900 | + if (!empty($signature)) { |
|
901 | + $login_id = $this->get_option('login_id'); |
|
902 | 902 | $trans_id = $_POST['x_trans_id']; |
903 | 903 | $amount = $_POST['x_amount']; |
904 | - $hash = hash_hmac ( 'sha512', "^$login_id^$trans_id^$amount^", hex2bin( $signature ) ); |
|
904 | + $hash = hash_hmac('sha512', "^$login_id^$trans_id^$amount^", hex2bin($signature)); |
|
905 | 905 | |
906 | - if ( ! hash_equals( $hash, $_POST['x_SHA2_Hash'] ) ) { |
|
906 | + if (!hash_equals($hash, $_POST['x_SHA2_Hash'])) { |
|
907 | 907 | exit; |
908 | 908 | } |
909 | 909 | |
910 | 910 | } |
911 | 911 | |
912 | 912 | // Fetch the associated subscription. |
913 | - $subscription_id = WPInv_Subscription::get_subscription_id_by_field( $_POST['x_subscription_id'] ); |
|
914 | - $subscription = new WPInv_Subscription( $subscription_id ); |
|
913 | + $subscription_id = WPInv_Subscription::get_subscription_id_by_field($_POST['x_subscription_id']); |
|
914 | + $subscription = new WPInv_Subscription($subscription_id); |
|
915 | 915 | |
916 | 916 | // Abort if it is missing or completed. |
917 | - if ( ! $subscription->get_id() || $subscription->has_status( 'completed' ) ) { |
|
917 | + if (!$subscription->get_id() || $subscription->has_status('completed')) { |
|
918 | 918 | return; |
919 | 919 | } |
920 | 920 | |
921 | 921 | // Payment status. |
922 | - if ( 1 == $_POST['x_response_code'] ) { |
|
922 | + if (1 == $_POST['x_response_code']) { |
|
923 | 923 | |
924 | 924 | $invoice = $subscription->create_payment(); |
925 | - $invoice->set_transaction_id( sanitize_text_field( $_POST['x_trans_id'] ) ); |
|
926 | - $invoice->set_status( 'wpi-renewal' ); |
|
925 | + $invoice->set_transaction_id(sanitize_text_field($_POST['x_trans_id'])); |
|
926 | + $invoice->set_status('wpi-renewal'); |
|
927 | 927 | $invoice->save(); |
928 | - $subscription->add_payment( array(), $invoice ); |
|
928 | + $subscription->add_payment(array(), $invoice); |
|
929 | 929 | $subscription->renew(); |
930 | 930 | |
931 | 931 | } else { |
@@ -941,28 +941,28 @@ discard block |
||
941 | 941 | */ |
942 | 942 | public function validate_ipn() { |
943 | 943 | |
944 | - wpinv_error_log( 'Validating Authorize.NET IPN response' ); |
|
944 | + wpinv_error_log('Validating Authorize.NET IPN response'); |
|
945 | 945 | |
946 | - if ( empty( $_SERVER['HTTP_X_ANET_SIGNATURE'] ) ) { |
|
946 | + if (empty($_SERVER['HTTP_X_ANET_SIGNATURE'])) { |
|
947 | 947 | return false; |
948 | 948 | } |
949 | 949 | |
950 | - $signature = $this->get_option( 'signature_key' ); |
|
950 | + $signature = $this->get_option('signature_key'); |
|
951 | 951 | |
952 | - if ( empty( $signature ) ) { |
|
953 | - wpinv_error_log( 'Error: You have not set a signature key' ); |
|
952 | + if (empty($signature)) { |
|
953 | + wpinv_error_log('Error: You have not set a signature key'); |
|
954 | 954 | return false; |
955 | 955 | } |
956 | 956 | |
957 | - $hash = hash_hmac ( 'sha512', file_get_contents('php://input'), hex2bin( $signature ) ); |
|
957 | + $hash = hash_hmac('sha512', file_get_contents('php://input'), hex2bin($signature)); |
|
958 | 958 | |
959 | - if ( hash_equals( $hash, $_SERVER['HTTP_X_ANET_SIGNATURE'] ) ) { |
|
960 | - wpinv_error_log( 'Successfully validated the IPN' ); |
|
959 | + if (hash_equals($hash, $_SERVER['HTTP_X_ANET_SIGNATURE'])) { |
|
960 | + wpinv_error_log('Successfully validated the IPN'); |
|
961 | 961 | return true; |
962 | 962 | } |
963 | 963 | |
964 | - wpinv_error_log( 'IPN hash is not valid' ); |
|
965 | - wpinv_error_log( $_SERVER['HTTP_X_ANET_SIGNATURE'] ); |
|
964 | + wpinv_error_log('IPN hash is not valid'); |
|
965 | + wpinv_error_log($_SERVER['HTTP_X_ANET_SIGNATURE']); |
|
966 | 966 | return false; |
967 | 967 | |
968 | 968 | } |
@@ -970,11 +970,11 @@ discard block |
||
970 | 970 | /** |
971 | 971 | * Displays a notice on the checkout page if sandbox is enabled. |
972 | 972 | */ |
973 | - public function sandbox_notice( $description, $gateway ) { |
|
973 | + public function sandbox_notice($description, $gateway) { |
|
974 | 974 | |
975 | - if ( $this->id == $gateway && wpinv_is_test_mode( $this->id ) ) { |
|
975 | + if ($this->id == $gateway && wpinv_is_test_mode($this->id)) { |
|
976 | 976 | $description .= '<br>' . sprintf( |
977 | - __( 'SANDBOX ENABLED. You can use sandbox testing details only. See the %sAuthorize.NET Sandbox Testing Guide%s for more details.', 'invoicing' ), |
|
977 | + __('SANDBOX ENABLED. You can use sandbox testing details only. See the %sAuthorize.NET Sandbox Testing Guide%s for more details.', 'invoicing'), |
|
978 | 978 | '<a href="https://developer.authorize.net/hello_world/testing_guide.html">', |
979 | 979 | '</a>' |
980 | 980 | ); |
@@ -988,42 +988,42 @@ discard block |
||
988 | 988 | * |
989 | 989 | * @param array $admin_settings |
990 | 990 | */ |
991 | - public function admin_settings( $admin_settings ) { |
|
991 | + public function admin_settings($admin_settings) { |
|
992 | 992 | |
993 | 993 | $currencies = sprintf( |
994 | - __( 'Supported Currencies: %s', 'invoicing' ), |
|
995 | - implode( ', ', $this->currencies ) |
|
994 | + __('Supported Currencies: %s', 'invoicing'), |
|
995 | + implode(', ', $this->currencies) |
|
996 | 996 | ); |
997 | 997 | |
998 | 998 | $admin_settings['authorizenet_active']['desc'] .= $admin_settings['authorizenet_active']['desc'] . " ($currencies)"; |
999 | - $admin_settings['authorizenet_desc']['std'] = __( 'Pay securely using your credit or debit card.', 'invoicing' ); |
|
999 | + $admin_settings['authorizenet_desc']['std'] = __('Pay securely using your credit or debit card.', 'invoicing'); |
|
1000 | 1000 | |
1001 | 1001 | $admin_settings['authorizenet_login_id'] = array( |
1002 | 1002 | 'type' => 'text', |
1003 | 1003 | 'id' => 'authorizenet_login_id', |
1004 | - 'name' => __( 'API Login ID', 'invoicing' ), |
|
1005 | - 'desc' => '<a href="https://support.authorize.net/s/article/How-do-I-obtain-my-API-Login-ID-and-Transaction-Key"><em>' . __( 'How do I obtain my API Login ID and Transaction Key?', 'invoicing' ) . '</em></a>', |
|
1004 | + 'name' => __('API Login ID', 'invoicing'), |
|
1005 | + 'desc' => '<a href="https://support.authorize.net/s/article/How-do-I-obtain-my-API-Login-ID-and-Transaction-Key"><em>' . __('How do I obtain my API Login ID and Transaction Key?', 'invoicing') . '</em></a>', |
|
1006 | 1006 | ); |
1007 | 1007 | |
1008 | 1008 | $admin_settings['authorizenet_transaction_key'] = array( |
1009 | 1009 | 'type' => 'text', |
1010 | 1010 | 'id' => 'authorizenet_transaction_key', |
1011 | - 'name' => __( 'Transaction Key', 'invoicing' ), |
|
1011 | + 'name' => __('Transaction Key', 'invoicing'), |
|
1012 | 1012 | ); |
1013 | 1013 | |
1014 | 1014 | $admin_settings['authorizenet_signature_key'] = array( |
1015 | 1015 | 'type' => 'text', |
1016 | 1016 | 'id' => 'authorizenet_signature_key', |
1017 | - 'name' => __( 'Signature Key', 'invoicing' ), |
|
1018 | - 'desc' => '<a href="https://support.authorize.net/s/article/What-is-a-Signature-Key"><em>' . __( 'Learn more.', 'invoicing' ) . '</em></a>', |
|
1017 | + 'name' => __('Signature Key', 'invoicing'), |
|
1018 | + 'desc' => '<a href="https://support.authorize.net/s/article/What-is-a-Signature-Key"><em>' . __('Learn more.', 'invoicing') . '</em></a>', |
|
1019 | 1019 | ); |
1020 | 1020 | |
1021 | 1021 | $admin_settings['authorizenet_ipn_url'] = array( |
1022 | 1022 | 'type' => 'ipn_url', |
1023 | 1023 | 'id' => 'authorizenet_ipn_url', |
1024 | - 'name' => __( 'Webhook URL', 'invoicing' ), |
|
1024 | + 'name' => __('Webhook URL', 'invoicing'), |
|
1025 | 1025 | 'std' => $this->notify_url, |
1026 | - 'desc' => __( 'Create a new webhook using this URL as the endpoint URL and set it to receive all payment events.', 'invoicing' ) . ' <a href="https://support.authorize.net/s/article/How-do-I-add-edit-Webhook-notification-end-points"><em>' . __( 'Learn more.', 'invoicing' ) . '</em></a>', |
|
1026 | + 'desc' => __('Create a new webhook using this URL as the endpoint URL and set it to receive all payment events.', 'invoicing') . ' <a href="https://support.authorize.net/s/article/How-do-I-add-edit-Webhook-notification-end-points"><em>' . __('Learn more.', 'invoicing') . '</em></a>', |
|
1027 | 1027 | 'custom' => 'authorizenet', |
1028 | 1028 | 'readonly' => true, |
1029 | 1029 | ); |
@@ -13,17 +13,17 @@ discard block |
||
13 | 13 | class GetPaid_Notification_Email_Sender { |
14 | 14 | |
15 | 15 | /** |
16 | - * Whether or not we should inline CSS into the email. |
|
17 | - */ |
|
18 | - public $inline_css = true; |
|
16 | + * Whether or not we should inline CSS into the email. |
|
17 | + */ |
|
18 | + public $inline_css = true; |
|
19 | 19 | |
20 | 20 | /** |
21 | - * The wp_mail() data. |
|
22 | - */ |
|
21 | + * The wp_mail() data. |
|
22 | + */ |
|
23 | 23 | public $wp_mail_data = null; |
24 | 24 | |
25 | 25 | /** |
26 | - * Sends a new email. |
|
26 | + * Sends a new email. |
|
27 | 27 | * |
28 | 28 | * @param string|array $to The recipients email or an array of recipient emails. |
29 | 29 | * @param string $subject The email's subject. |
@@ -31,35 +31,35 @@ discard block |
||
31 | 31 | * @param array $attachments The email attachments. |
32 | 32 | * |
33 | 33 | * @return bool |
34 | - */ |
|
35 | - public function send( $to, $subject, $email, $attachments = array() ) { |
|
34 | + */ |
|
35 | + public function send( $to, $subject, $email, $attachments = array() ) { |
|
36 | 36 | |
37 | - /* |
|
37 | + /* |
|
38 | 38 | * Allow to filter data on per-email basis. |
39 | 39 | */ |
40 | - $data = apply_filters( |
|
41 | - 'getpaid_email_data', |
|
42 | - array( |
|
43 | - 'to' => array_filter( wpinv_parse_list( $to ) ), |
|
44 | - 'subject' => $subject, |
|
45 | - 'email' => $email, |
|
46 | - 'headers' => $this->get_headers(), |
|
47 | - 'attachments' => $attachments, |
|
48 | - ), |
|
49 | - $this |
|
50 | - ); |
|
40 | + $data = apply_filters( |
|
41 | + 'getpaid_email_data', |
|
42 | + array( |
|
43 | + 'to' => array_filter( wpinv_parse_list( $to ) ), |
|
44 | + 'subject' => $subject, |
|
45 | + 'email' => $email, |
|
46 | + 'headers' => $this->get_headers(), |
|
47 | + 'attachments' => $attachments, |
|
48 | + ), |
|
49 | + $this |
|
50 | + ); |
|
51 | 51 | |
52 | 52 | // Remove slashes. |
53 | 53 | $data = (array) wp_unslash( $data ); |
54 | 54 | |
55 | 55 | // Cache it. |
56 | - $this->wp_mail_data = $data; |
|
56 | + $this->wp_mail_data = $data; |
|
57 | 57 | |
58 | - // Attach our own hooks. |
|
59 | - $this->before_sending(); |
|
58 | + // Attach our own hooks. |
|
59 | + $this->before_sending(); |
|
60 | 60 | |
61 | - // Prepare the sending function. |
|
62 | - $sending_function = apply_filters( 'getpaid_email_email_sending_function', 'wp_mail' ); |
|
61 | + // Prepare the sending function. |
|
62 | + $sending_function = apply_filters( 'getpaid_email_email_sending_function', 'wp_mail' ); |
|
63 | 63 | $result = false; |
64 | 64 | |
65 | 65 | foreach ( $this->wp_mail_data['to'] as $to ) { |
@@ -76,65 +76,65 @@ discard block |
||
76 | 76 | |
77 | 77 | } |
78 | 78 | |
79 | - // Remove our hooks. |
|
80 | - $this->after_sending(); |
|
79 | + // Remove our hooks. |
|
80 | + $this->after_sending(); |
|
81 | 81 | |
82 | - $this->wp_mail_data = null; |
|
82 | + $this->wp_mail_data = null; |
|
83 | 83 | |
84 | - return $result; |
|
84 | + return $result; |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
88 | - * Retrieves email headers. |
|
89 | - */ |
|
90 | - public function get_headers() { |
|
88 | + * Retrieves email headers. |
|
89 | + */ |
|
90 | + public function get_headers() { |
|
91 | 91 | |
92 | - $name = $this->get_from_name(); |
|
93 | - $reply_to = $this->get_reply_to(); |
|
94 | - $headers = array( "Reply-To:$name <$reply_to>" ); |
|
92 | + $name = $this->get_from_name(); |
|
93 | + $reply_to = $this->get_reply_to(); |
|
94 | + $headers = array( "Reply-To:$name <$reply_to>" ); |
|
95 | 95 | |
96 | - return apply_filters( 'getpaid_email_headers', $headers, $this ); |
|
96 | + return apply_filters( 'getpaid_email_headers', $headers, $this ); |
|
97 | 97 | |
98 | - } |
|
98 | + } |
|
99 | 99 | |
100 | 100 | /** |
101 | - * Fires before an email is sent |
|
102 | - * |
|
103 | - * @since 1.0.0 |
|
104 | - */ |
|
105 | - public function before_sending() { |
|
101 | + * Fires before an email is sent |
|
102 | + * |
|
103 | + * @since 1.0.0 |
|
104 | + */ |
|
105 | + public function before_sending() { |
|
106 | 106 | |
107 | 107 | do_action( 'getpaid_before_send_email', $this ); |
108 | - add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 ); |
|
109 | - add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 ); |
|
110 | - add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 ); |
|
111 | - add_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 ); |
|
108 | + add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 ); |
|
109 | + add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 ); |
|
110 | + add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 ); |
|
111 | + add_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 ); |
|
112 | 112 | |
113 | - } |
|
113 | + } |
|
114 | 114 | |
115 | 115 | /** |
116 | - * Returns the from name. |
|
117 | - */ |
|
118 | - public function get_from_name() { |
|
116 | + * Returns the from name. |
|
117 | + */ |
|
118 | + public function get_from_name() { |
|
119 | 119 | |
120 | 120 | $from_name = wpinv_get_option( 'email_from_name', get_bloginfo( 'name' ) ); |
121 | 121 | |
122 | - if ( empty( $from_name ) ) { |
|
123 | - $from_name = get_bloginfo( 'name' ); |
|
122 | + if ( empty( $from_name ) ) { |
|
123 | + $from_name = get_bloginfo( 'name' ); |
|
124 | 124 | } |
125 | 125 | |
126 | - return wp_specialchars_decode( $from_name, ENT_QUOTES ); |
|
126 | + return wp_specialchars_decode( $from_name, ENT_QUOTES ); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
130 | - * Returns the from email. |
|
131 | - */ |
|
132 | - public function get_from_address() { |
|
130 | + * Returns the from email. |
|
131 | + */ |
|
132 | + public function get_from_address() { |
|
133 | 133 | |
134 | 134 | $from_address = wpinv_get_option( 'email_from', $this->default_from_address() ); |
135 | 135 | |
136 | - if ( ! is_email( $from_address ) ) { |
|
137 | - $from_address = $this->default_from_address(); |
|
136 | + if ( ! is_email( $from_address ) ) { |
|
137 | + $from_address = $this->default_from_address(); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | return $from_address; |
@@ -142,75 +142,75 @@ discard block |
||
142 | 142 | } |
143 | 143 | |
144 | 144 | /** |
145 | - * The default emails from address. |
|
146 | - * |
|
147 | - * Defaults to wordpress@$sitename |
|
148 | - * Some hosts will block outgoing mail from this address if it doesn't exist, |
|
149 | - * but there's no easy alternative. Defaulting to admin_email might appear to be |
|
150 | - * another option, but some hosts may refuse to relay mail from an unknown domain. |
|
151 | - * |
|
152 | - */ |
|
153 | - public function default_from_address() { |
|
154 | - |
|
155 | - // Get the site domain and get rid of www. |
|
156 | - $sitename = strtolower( $_SERVER['SERVER_NAME'] ); |
|
157 | - if ( substr( $sitename, 0, 4 ) == 'www.' ) { |
|
158 | - $sitename = substr( $sitename, 4 ); |
|
159 | - } |
|
160 | - |
|
161 | - $from_email = 'wordpress@' . $sitename; |
|
162 | - |
|
163 | - return apply_filters( 'getpaid_default_from_address', $from_email ); |
|
145 | + * The default emails from address. |
|
146 | + * |
|
147 | + * Defaults to wordpress@$sitename |
|
148 | + * Some hosts will block outgoing mail from this address if it doesn't exist, |
|
149 | + * but there's no easy alternative. Defaulting to admin_email might appear to be |
|
150 | + * another option, but some hosts may refuse to relay mail from an unknown domain. |
|
151 | + * |
|
152 | + */ |
|
153 | + public function default_from_address() { |
|
154 | + |
|
155 | + // Get the site domain and get rid of www. |
|
156 | + $sitename = strtolower( $_SERVER['SERVER_NAME'] ); |
|
157 | + if ( substr( $sitename, 0, 4 ) == 'www.' ) { |
|
158 | + $sitename = substr( $sitename, 4 ); |
|
159 | + } |
|
160 | + |
|
161 | + $from_email = 'wordpress@' . $sitename; |
|
162 | + |
|
163 | + return apply_filters( 'getpaid_default_from_address', $from_email ); |
|
164 | 164 | |
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
168 | - * Get the email reply-to. |
|
169 | - * |
|
170 | - * |
|
171 | - * @return string The email reply-to address. |
|
172 | - */ |
|
173 | - public function get_reply_to() { |
|
168 | + * Get the email reply-to. |
|
169 | + * |
|
170 | + * |
|
171 | + * @return string The email reply-to address. |
|
172 | + */ |
|
173 | + public function get_reply_to() { |
|
174 | 174 | |
175 | - $reply_to = wpinv_get_admin_email(); |
|
175 | + $reply_to = wpinv_get_admin_email(); |
|
176 | 176 | |
177 | - if ( ! is_email( $reply_to ) ) { |
|
178 | - $reply_to = get_option( 'admin_email' ); |
|
179 | - } |
|
177 | + if ( ! is_email( $reply_to ) ) { |
|
178 | + $reply_to = get_option( 'admin_email' ); |
|
179 | + } |
|
180 | 180 | |
181 | - return $reply_to; |
|
181 | + return $reply_to; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
185 | - * Get the email content type. |
|
186 | - * |
|
187 | - */ |
|
188 | - public function get_content_type() { |
|
189 | - return apply_filters( 'getpaid_email_content_type', 'text/html', $this ); |
|
185 | + * Get the email content type. |
|
186 | + * |
|
187 | + */ |
|
188 | + public function get_content_type() { |
|
189 | + return apply_filters( 'getpaid_email_content_type', 'text/html', $this ); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
193 | - * Ensures that our email messages are not messed up by template plugins. |
|
194 | - * |
|
195 | - * @return array wp_mail_data. |
|
196 | - */ |
|
197 | - public function ensure_email_content( $args ) { |
|
198 | - $args['message'] = $this->wp_mail_data['email']; |
|
199 | - return $args; |
|
193 | + * Ensures that our email messages are not messed up by template plugins. |
|
194 | + * |
|
195 | + * @return array wp_mail_data. |
|
196 | + */ |
|
197 | + public function ensure_email_content( $args ) { |
|
198 | + $args['message'] = $this->wp_mail_data['email']; |
|
199 | + return $args; |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
203 | - * A little house keeping after an email is sent. |
|
204 | - * |
|
205 | - */ |
|
206 | - public function after_sending() { |
|
203 | + * A little house keeping after an email is sent. |
|
204 | + * |
|
205 | + */ |
|
206 | + public function after_sending() { |
|
207 | 207 | |
208 | 208 | do_action( 'after_noptin_sends_email', $this->wp_mail_data ); |
209 | - remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 ); |
|
210 | - remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 ); |
|
211 | - remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 ); |
|
212 | - remove_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 ); |
|
209 | + remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 ); |
|
210 | + remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 ); |
|
211 | + remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 ); |
|
212 | + remove_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 ); |
|
213 | 213 | |
214 | - } |
|
214 | + } |
|
215 | 215 | |
216 | 216 | } |
@@ -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 | * This function is responsible for sending emails. |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @return bool |
34 | 34 | */ |
35 | - public function send( $to, $subject, $email, $attachments = array() ) { |
|
35 | + public function send($to, $subject, $email, $attachments = array()) { |
|
36 | 36 | |
37 | 37 | /* |
38 | 38 | * Allow to filter data on per-email basis. |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | $data = apply_filters( |
41 | 41 | 'getpaid_email_data', |
42 | 42 | array( |
43 | - 'to' => array_filter( wpinv_parse_list( $to ) ), |
|
43 | + 'to' => array_filter(wpinv_parse_list($to)), |
|
44 | 44 | 'subject' => $subject, |
45 | 45 | 'email' => $email, |
46 | 46 | 'headers' => $this->get_headers(), |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | ); |
51 | 51 | |
52 | 52 | // Remove slashes. |
53 | - $data = (array) wp_unslash( $data ); |
|
53 | + $data = (array) wp_unslash($data); |
|
54 | 54 | |
55 | 55 | // Cache it. |
56 | 56 | $this->wp_mail_data = $data; |
@@ -59,16 +59,16 @@ discard block |
||
59 | 59 | $this->before_sending(); |
60 | 60 | |
61 | 61 | // Prepare the sending function. |
62 | - $sending_function = apply_filters( 'getpaid_email_email_sending_function', 'wp_mail' ); |
|
63 | - $result = false; |
|
62 | + $sending_function = apply_filters('getpaid_email_email_sending_function', 'wp_mail'); |
|
63 | + $result = false; |
|
64 | 64 | |
65 | - foreach ( $this->wp_mail_data['to'] as $to ) { |
|
65 | + foreach ($this->wp_mail_data['to'] as $to) { |
|
66 | 66 | |
67 | 67 | // Send the actual email. |
68 | 68 | $result = call_user_func( |
69 | 69 | $sending_function, |
70 | 70 | $to, |
71 | - html_entity_decode( $data['subject'], ENT_QUOTES, get_bloginfo( 'charset' ) ), |
|
71 | + html_entity_decode($data['subject'], ENT_QUOTES, get_bloginfo('charset')), |
|
72 | 72 | $data['email'], |
73 | 73 | $data['headers'], |
74 | 74 | $data['attachments'] |
@@ -91,9 +91,9 @@ discard block |
||
91 | 91 | |
92 | 92 | $name = $this->get_from_name(); |
93 | 93 | $reply_to = $this->get_reply_to(); |
94 | - $headers = array( "Reply-To:$name <$reply_to>" ); |
|
94 | + $headers = array("Reply-To:$name <$reply_to>"); |
|
95 | 95 | |
96 | - return apply_filters( 'getpaid_email_headers', $headers, $this ); |
|
96 | + return apply_filters('getpaid_email_headers', $headers, $this); |
|
97 | 97 | |
98 | 98 | } |
99 | 99 | |
@@ -104,11 +104,11 @@ discard block |
||
104 | 104 | */ |
105 | 105 | public function before_sending() { |
106 | 106 | |
107 | - do_action( 'getpaid_before_send_email', $this ); |
|
108 | - add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 ); |
|
109 | - add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 ); |
|
110 | - add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 ); |
|
111 | - add_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 ); |
|
107 | + do_action('getpaid_before_send_email', $this); |
|
108 | + add_filter('wp_mail_from', array($this, 'get_from_address'), 1000); |
|
109 | + add_filter('wp_mail_from_name', array($this, 'get_from_name'), 1000); |
|
110 | + add_filter('wp_mail_content_type', array($this, 'get_content_type'), 1000); |
|
111 | + add_filter('wp_mail', array($this, 'ensure_email_content'), 1000000); |
|
112 | 112 | |
113 | 113 | } |
114 | 114 | |
@@ -117,13 +117,13 @@ discard block |
||
117 | 117 | */ |
118 | 118 | public function get_from_name() { |
119 | 119 | |
120 | - $from_name = wpinv_get_option( 'email_from_name', get_bloginfo( 'name' ) ); |
|
120 | + $from_name = wpinv_get_option('email_from_name', get_bloginfo('name')); |
|
121 | 121 | |
122 | - if ( empty( $from_name ) ) { |
|
123 | - $from_name = get_bloginfo( 'name' ); |
|
122 | + if (empty($from_name)) { |
|
123 | + $from_name = get_bloginfo('name'); |
|
124 | 124 | } |
125 | 125 | |
126 | - return wp_specialchars_decode( $from_name, ENT_QUOTES ); |
|
126 | + return wp_specialchars_decode($from_name, ENT_QUOTES); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
@@ -131,10 +131,10 @@ discard block |
||
131 | 131 | */ |
132 | 132 | public function get_from_address() { |
133 | 133 | |
134 | - $from_address = wpinv_get_option( 'email_from', $this->default_from_address() ); |
|
134 | + $from_address = wpinv_get_option('email_from', $this->default_from_address()); |
|
135 | 135 | |
136 | - if ( ! is_email( $from_address ) ) { |
|
137 | - $from_address = $this->default_from_address(); |
|
136 | + if (!is_email($from_address)) { |
|
137 | + $from_address = $this->default_from_address(); |
|
138 | 138 | } |
139 | 139 | |
140 | 140 | return $from_address; |
@@ -153,14 +153,14 @@ discard block |
||
153 | 153 | public function default_from_address() { |
154 | 154 | |
155 | 155 | // Get the site domain and get rid of www. |
156 | - $sitename = strtolower( $_SERVER['SERVER_NAME'] ); |
|
157 | - if ( substr( $sitename, 0, 4 ) == 'www.' ) { |
|
158 | - $sitename = substr( $sitename, 4 ); |
|
156 | + $sitename = strtolower($_SERVER['SERVER_NAME']); |
|
157 | + if (substr($sitename, 0, 4) == 'www.') { |
|
158 | + $sitename = substr($sitename, 4); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | $from_email = 'wordpress@' . $sitename; |
162 | 162 | |
163 | - return apply_filters( 'getpaid_default_from_address', $from_email ); |
|
163 | + return apply_filters('getpaid_default_from_address', $from_email); |
|
164 | 164 | |
165 | 165 | } |
166 | 166 | |
@@ -174,8 +174,8 @@ discard block |
||
174 | 174 | |
175 | 175 | $reply_to = wpinv_get_admin_email(); |
176 | 176 | |
177 | - if ( ! is_email( $reply_to ) ) { |
|
178 | - $reply_to = get_option( 'admin_email' ); |
|
177 | + if (!is_email($reply_to)) { |
|
178 | + $reply_to = get_option('admin_email'); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | return $reply_to; |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | * |
187 | 187 | */ |
188 | 188 | public function get_content_type() { |
189 | - return apply_filters( 'getpaid_email_content_type', 'text/html', $this ); |
|
189 | + return apply_filters('getpaid_email_content_type', 'text/html', $this); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | * |
195 | 195 | * @return array wp_mail_data. |
196 | 196 | */ |
197 | - public function ensure_email_content( $args ) { |
|
197 | + public function ensure_email_content($args) { |
|
198 | 198 | $args['message'] = $this->wp_mail_data['email']; |
199 | 199 | return $args; |
200 | 200 | } |
@@ -205,11 +205,11 @@ discard block |
||
205 | 205 | */ |
206 | 206 | public function after_sending() { |
207 | 207 | |
208 | - do_action( 'after_noptin_sends_email', $this->wp_mail_data ); |
|
209 | - remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 ); |
|
210 | - remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 ); |
|
211 | - remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 ); |
|
212 | - remove_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000000 ); |
|
208 | + do_action('after_noptin_sends_email', $this->wp_mail_data); |
|
209 | + remove_filter('wp_mail_from', array($this, 'get_from_address'), 1000); |
|
210 | + remove_filter('wp_mail_from_name', array($this, 'get_from_name'), 1000); |
|
211 | + remove_filter('wp_mail_content_type', array($this, 'get_content_type'), 1000); |
|
212 | + remove_filter('wp_mail', array($this, 'ensure_email_content'), 1000000); |
|
213 | 213 | |
214 | 214 | } |
215 | 215 |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | * @package Invoicing |
7 | 7 | */ |
8 | 8 | |
9 | -defined( 'ABSPATH' ) || exit; |
|
9 | +defined('ABSPATH') || exit; |
|
10 | 10 | |
11 | 11 | /** |
12 | 12 | * Returns the errors as html |
@@ -14,27 +14,27 @@ discard block |
||
14 | 14 | * @param clear whether or not to clear the errors. |
15 | 15 | * @since 1.0.19 |
16 | 16 | */ |
17 | -function getpaid_get_errors_html( $clear = true ) { |
|
17 | +function getpaid_get_errors_html($clear = true) { |
|
18 | 18 | |
19 | 19 | $errors = ''; |
20 | - foreach ( wpinv_get_errors() as $error ) { |
|
21 | - $type = 'error'; |
|
20 | + foreach (wpinv_get_errors() as $error) { |
|
21 | + $type = 'error'; |
|
22 | 22 | |
23 | - if ( is_array( $error ) ) { |
|
23 | + if (is_array($error)) { |
|
24 | 24 | $type = $error['type']; |
25 | 25 | $error = $error['text']; |
26 | 26 | } |
27 | 27 | |
28 | 28 | $errors .= aui()->alert( |
29 | 29 | array( |
30 | - 'content' => wp_kses_post( $error ), |
|
30 | + 'content' => wp_kses_post($error), |
|
31 | 31 | 'type' => $type, |
32 | 32 | ) |
33 | 33 | ); |
34 | 34 | |
35 | 35 | } |
36 | 36 | |
37 | - if ( $clear ){ |
|
37 | + if ($clear) { |
|
38 | 38 | wpinv_clear_errors(); |
39 | 39 | } |
40 | 40 | |
@@ -55,8 +55,8 @@ discard block |
||
55 | 55 | * @return array |
56 | 56 | */ |
57 | 57 | function wpinv_get_errors() { |
58 | - $errors = getpaid_session()->get( 'wpinv_errors' ); |
|
59 | - return is_array( $errors ) ? $errors : array(); |
|
58 | + $errors = getpaid_session()->get('wpinv_errors'); |
|
59 | + return is_array($errors) ? $errors : array(); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | /** |
@@ -66,15 +66,15 @@ discard block |
||
66 | 66 | * @param string $error_message The error message. |
67 | 67 | * @param string $type Either error, info, warning, primary, dark, light or success. |
68 | 68 | */ |
69 | -function wpinv_set_error( $error_id, $error_message, $type = 'error' ) { |
|
69 | +function wpinv_set_error($error_id, $error_message, $type = 'error') { |
|
70 | 70 | |
71 | 71 | $errors = wpinv_get_errors(); |
72 | - $errors[ $error_id ] = array( |
|
72 | + $errors[$error_id] = array( |
|
73 | 73 | 'type' => $type, |
74 | 74 | 'text' => $error_message, |
75 | 75 | ); |
76 | 76 | |
77 | - getpaid()->session->set( 'wpinv_errors', $errors ); |
|
77 | + getpaid()->session->set('wpinv_errors', $errors); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | * |
83 | 83 | */ |
84 | 84 | function wpinv_has_errors() { |
85 | - return count( wpinv_get_errors() ) > 0; |
|
85 | + return count(wpinv_get_errors()) > 0; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -90,21 +90,21 @@ discard block |
||
90 | 90 | * |
91 | 91 | */ |
92 | 92 | function wpinv_clear_errors() { |
93 | - getpaid_session()->set( 'wpinv_errors', null ); |
|
93 | + getpaid_session()->set('wpinv_errors', null); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
97 | 97 | * Clears a single error. |
98 | 98 | * |
99 | 99 | */ |
100 | -function wpinv_unset_error( $error_id ) { |
|
100 | +function wpinv_unset_error($error_id) { |
|
101 | 101 | $errors = wpinv_get_errors(); |
102 | 102 | |
103 | - if ( isset( $errors[ $error_id ] ) ) { |
|
104 | - unset( $errors[ $error_id ] ); |
|
103 | + if (isset($errors[$error_id])) { |
|
104 | + unset($errors[$error_id]); |
|
105 | 105 | } |
106 | 106 | |
107 | - getpaid_session()->set( 'wpinv_errors', $errors ); |
|
107 | + getpaid_session()->set('wpinv_errors', $errors); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
@@ -115,15 +115,15 @@ discard block |
||
115 | 115 | * @param string $message Message to log. |
116 | 116 | * @param string $version Version the message was added in. |
117 | 117 | */ |
118 | -function getpaid_doing_it_wrong( $function, $message, $version ) { |
|
118 | +function getpaid_doing_it_wrong($function, $message, $version) { |
|
119 | 119 | |
120 | 120 | $message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
121 | 121 | |
122 | - if ( is_ajax() || defined( 'REST_REQUEST' ) ) { |
|
123 | - do_action( 'doing_it_wrong_run', $function, $message, $version ); |
|
124 | - error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
|
122 | + if (is_ajax() || defined('REST_REQUEST')) { |
|
123 | + do_action('doing_it_wrong_run', $function, $message, $version); |
|
124 | + error_log("{$function} was called incorrectly. {$message}. This message was added in version {$version}."); |
|
125 | 125 | } else { |
126 | - _doing_it_wrong( $function, $message, $version ); |
|
126 | + _doing_it_wrong($function, $message, $version); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | } |
@@ -12,119 +12,119 @@ |
||
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 | - // Fires after notes are loaded. |
|
25 | - do_action( 'wpinv_notes_init', $this ); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Filters invoice notes query to only include our notes. |
|
30 | - * |
|
31 | - * @param WP_Comment_Query $query |
|
32 | - */ |
|
33 | - public function set_invoice_note_type( $query ) { |
|
34 | - $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
35 | - |
|
36 | - if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
37 | - $query->query_vars['type'] = 'wpinv_note'; |
|
38 | - } else { |
|
39 | - |
|
40 | - if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
41 | - $query->query_vars['type__not_in'] = array(); |
|
42 | - } |
|
43 | - |
|
44 | - $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
45 | - $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
46 | - } |
|
47 | - |
|
48 | - return $query; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Exclude notes from the comments feed. |
|
53 | - */ |
|
54 | - function wpinv_comment_feed_where( $where ){ |
|
55 | - return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Returns an array of invoice notes. |
|
60 | - * |
|
61 | - * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
62 | - * @param string $type Optional. Pass in customer to only return customer notes. |
|
63 | - * @return WP_Comment[] |
|
64 | - */ |
|
65 | - public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
66 | - |
|
67 | - // Default comment args. |
|
68 | - $args = array( |
|
69 | - 'post_id' => $invoice_id, |
|
70 | - 'orderby' => 'comment_ID', |
|
71 | - 'order' => 'ASC', |
|
72 | - ); |
|
73 | - |
|
74 | - // Maybe only show customer comments. |
|
75 | - if ( $type == 'customer' ) { |
|
76 | - $args['meta_key'] = '_wpi_customer_note'; |
|
77 | - $args['meta_value'] = 1; |
|
78 | - } |
|
79 | - |
|
80 | - $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
81 | - |
|
82 | - return get_comments( $args ); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Saves an invoice comment. |
|
87 | - * |
|
88 | - * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
89 | - * @param string $note The note content. |
|
90 | - * @param string $note_author The name of the author of the note. |
|
91 | - * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
92 | - * @return int|false The new note's ID on success, false on failure. |
|
93 | - */ |
|
94 | - function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
95 | - |
|
96 | - do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
97 | - |
|
98 | - /** |
|
99 | - * Insert the comment. |
|
100 | - */ |
|
101 | - $note_id = wp_insert_comment( |
|
102 | - wp_filter_comment( |
|
103 | - array( |
|
104 | - 'comment_post_ID' => $invoice->get_id(), |
|
105 | - 'comment_content' => $note, |
|
106 | - 'comment_agent' => 'Invoicing', |
|
107 | - 'user_id' => get_current_user_id(), |
|
108 | - 'comment_author' => $note_author, |
|
109 | - 'comment_author_IP' => wpinv_get_ip(), |
|
110 | - 'comment_author_email' => $author_email, |
|
111 | - 'comment_author_url' => $invoice->get_view_url(), |
|
112 | - 'comment_type' => 'wpinv_note', |
|
113 | - ) |
|
114 | - ) |
|
115 | - ); |
|
116 | - |
|
117 | - do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
118 | - |
|
119 | - // Are we notifying the customer? |
|
120 | - if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
121 | - return $note_id; |
|
122 | - } |
|
123 | - |
|
124 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
125 | - do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
126 | - do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
127 | - return $note_id; |
|
128 | - } |
|
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 | + // Fires after notes are loaded. |
|
25 | + do_action( 'wpinv_notes_init', $this ); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Filters invoice notes query to only include our notes. |
|
30 | + * |
|
31 | + * @param WP_Comment_Query $query |
|
32 | + */ |
|
33 | + public function set_invoice_note_type( $query ) { |
|
34 | + $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
35 | + |
|
36 | + if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
37 | + $query->query_vars['type'] = 'wpinv_note'; |
|
38 | + } else { |
|
39 | + |
|
40 | + if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
41 | + $query->query_vars['type__not_in'] = array(); |
|
42 | + } |
|
43 | + |
|
44 | + $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
45 | + $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
46 | + } |
|
47 | + |
|
48 | + return $query; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Exclude notes from the comments feed. |
|
53 | + */ |
|
54 | + function wpinv_comment_feed_where( $where ){ |
|
55 | + return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Returns an array of invoice notes. |
|
60 | + * |
|
61 | + * @param int $invoice_id The invoice ID whose notes to retrieve. |
|
62 | + * @param string $type Optional. Pass in customer to only return customer notes. |
|
63 | + * @return WP_Comment[] |
|
64 | + */ |
|
65 | + public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
66 | + |
|
67 | + // Default comment args. |
|
68 | + $args = array( |
|
69 | + 'post_id' => $invoice_id, |
|
70 | + 'orderby' => 'comment_ID', |
|
71 | + 'order' => 'ASC', |
|
72 | + ); |
|
73 | + |
|
74 | + // Maybe only show customer comments. |
|
75 | + if ( $type == 'customer' ) { |
|
76 | + $args['meta_key'] = '_wpi_customer_note'; |
|
77 | + $args['meta_value'] = 1; |
|
78 | + } |
|
79 | + |
|
80 | + $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
81 | + |
|
82 | + return get_comments( $args ); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Saves an invoice comment. |
|
87 | + * |
|
88 | + * @param WPInv_Invoice $invoice The invoice to add the comment to. |
|
89 | + * @param string $note The note content. |
|
90 | + * @param string $note_author The name of the author of the note. |
|
91 | + * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
|
92 | + * @return int|false The new note's ID on success, false on failure. |
|
93 | + */ |
|
94 | + function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
95 | + |
|
96 | + do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
97 | + |
|
98 | + /** |
|
99 | + * Insert the comment. |
|
100 | + */ |
|
101 | + $note_id = wp_insert_comment( |
|
102 | + wp_filter_comment( |
|
103 | + array( |
|
104 | + 'comment_post_ID' => $invoice->get_id(), |
|
105 | + 'comment_content' => $note, |
|
106 | + 'comment_agent' => 'Invoicing', |
|
107 | + 'user_id' => get_current_user_id(), |
|
108 | + 'comment_author' => $note_author, |
|
109 | + 'comment_author_IP' => wpinv_get_ip(), |
|
110 | + 'comment_author_email' => $author_email, |
|
111 | + 'comment_author_url' => $invoice->get_view_url(), |
|
112 | + 'comment_type' => 'wpinv_note', |
|
113 | + ) |
|
114 | + ) |
|
115 | + ); |
|
116 | + |
|
117 | + do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
118 | + |
|
119 | + // Are we notifying the customer? |
|
120 | + if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
121 | + return $note_id; |
|
122 | + } |
|
123 | + |
|
124 | + add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
125 | + do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
126 | + do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
127 | + return $note_id; |
|
128 | + } |
|
129 | 129 | |
130 | 130 | } |
@@ -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,11 +18,11 @@ 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 | // Fires after notes are loaded. |
25 | - do_action( 'wpinv_notes_init', $this ); |
|
25 | + do_action('wpinv_notes_init', $this); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |
@@ -30,19 +30,19 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @param WP_Comment_Query $query |
32 | 32 | */ |
33 | - public function set_invoice_note_type( $query ) { |
|
34 | - $post_id = ! empty( $query->query_vars['post_ID'] ) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
33 | + public function set_invoice_note_type($query) { |
|
34 | + $post_id = !empty($query->query_vars['post_ID']) ? $query->query_vars['post_ID'] : $query->query_vars['post_id']; |
|
35 | 35 | |
36 | - if ( $post_id && getpaid_is_invoice_post_type( get_post_type( $post_id ) ) ) { |
|
36 | + if ($post_id && getpaid_is_invoice_post_type(get_post_type($post_id))) { |
|
37 | 37 | $query->query_vars['type'] = 'wpinv_note'; |
38 | 38 | } else { |
39 | 39 | |
40 | - if ( empty( $query->query_vars['type__not_in'] ) ) { |
|
40 | + if (empty($query->query_vars['type__not_in'])) { |
|
41 | 41 | $query->query_vars['type__not_in'] = array(); |
42 | 42 | } |
43 | 43 | |
44 | - $query->query_vars['type__not_in'] = wpinv_parse_list( $query->query_vars['type__not_in'] ); |
|
45 | - $query->query_vars['type__not_in'] = array_merge( array( 'wpinv_note' ), $query->query_vars['type__not_in'] ); |
|
44 | + $query->query_vars['type__not_in'] = wpinv_parse_list($query->query_vars['type__not_in']); |
|
45 | + $query->query_vars['type__not_in'] = array_merge(array('wpinv_note'), $query->query_vars['type__not_in']); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | return $query; |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | /** |
52 | 52 | * Exclude notes from the comments feed. |
53 | 53 | */ |
54 | - function wpinv_comment_feed_where( $where ){ |
|
55 | - return $where . ( $where ? ' AND ' : '' ) . " comment_type != 'wpinv_note' "; |
|
54 | + function wpinv_comment_feed_where($where) { |
|
55 | + return $where . ($where ? ' AND ' : '') . " comment_type != 'wpinv_note' "; |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * @param string $type Optional. Pass in customer to only return customer notes. |
63 | 63 | * @return WP_Comment[] |
64 | 64 | */ |
65 | - public function get_invoice_notes( $invoice_id = 0, $type = 'all' ) { |
|
65 | + public function get_invoice_notes($invoice_id = 0, $type = 'all') { |
|
66 | 66 | |
67 | 67 | // Default comment args. |
68 | 68 | $args = array( |
@@ -72,14 +72,14 @@ discard block |
||
72 | 72 | ); |
73 | 73 | |
74 | 74 | // Maybe only show customer comments. |
75 | - if ( $type == 'customer' ) { |
|
75 | + if ($type == 'customer') { |
|
76 | 76 | $args['meta_key'] = '_wpi_customer_note'; |
77 | 77 | $args['meta_value'] = 1; |
78 | 78 | } |
79 | 79 | |
80 | - $args = apply_filters( 'wpinv_invoice_notes_args', $args, $this, $invoice_id, $type ); |
|
80 | + $args = apply_filters('wpinv_invoice_notes_args', $args, $this, $invoice_id, $type); |
|
81 | 81 | |
82 | - return get_comments( $args ); |
|
82 | + return get_comments($args); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -91,9 +91,9 @@ discard block |
||
91 | 91 | * @param bool $for_customer Whether or not this comment is meant to be sent to the customer. |
92 | 92 | * @return int|false The new note's ID on success, false on failure. |
93 | 93 | */ |
94 | - function add_invoice_note( $invoice, $note, $note_author, $author_email, $for_customer = false ){ |
|
94 | + function add_invoice_note($invoice, $note, $note_author, $author_email, $for_customer = false) { |
|
95 | 95 | |
96 | - do_action( 'wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer ); |
|
96 | + do_action('wpinv_pre_insert_invoice_note', $invoice->get_id(), $note, $for_customer); |
|
97 | 97 | |
98 | 98 | /** |
99 | 99 | * Insert the comment. |
@@ -114,16 +114,16 @@ discard block |
||
114 | 114 | ) |
115 | 115 | ); |
116 | 116 | |
117 | - do_action( 'wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer ); |
|
117 | + do_action('wpinv_insert_payment_note', $note_id, $invoice->get_id(), $note, $for_customer); |
|
118 | 118 | |
119 | 119 | // Are we notifying the customer? |
120 | - if ( empty( $note_id ) || empty( $for_customer ) ) { |
|
120 | + if (empty($note_id) || empty($for_customer)) { |
|
121 | 121 | return $note_id; |
122 | 122 | } |
123 | 123 | |
124 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
125 | - do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $invoice->get_id(), 'user_note' => $note ) ); |
|
126 | - do_action( 'getpaid_new_customer_note', $invoice, $note ); |
|
124 | + add_comment_meta($note_id, '_wpi_customer_note', 1); |
|
125 | + do_action('wpinv_new_customer_note', array('invoice_id' => $invoice->get_id(), 'user_note' => $note)); |
|
126 | + do_action('getpaid_new_customer_note', $invoice, $note); |
|
127 | 127 | return $note_id; |
128 | 128 | } |
129 | 129 |
@@ -15,314 +15,314 @@ |
||
15 | 15 | class GetPaid_Invoice_Notification_Emails { |
16 | 16 | |
17 | 17 | /** |
18 | - * The array of invoice email actions. |
|
19 | - * |
|
20 | - * @param array |
|
21 | - */ |
|
22 | - public $invoice_actions; |
|
18 | + * The array of invoice email actions. |
|
19 | + * |
|
20 | + * @param array |
|
21 | + */ |
|
22 | + public $invoice_actions; |
|
23 | 23 | |
24 | 24 | /** |
25 | - * Class constructor |
|
25 | + * Class constructor |
|
26 | 26 | * |
27 | - */ |
|
28 | - public function __construct() { |
|
29 | - |
|
30 | - $this->invoice_actions = apply_filters( |
|
31 | - 'getpaid_notification_email_invoice_triggers', |
|
32 | - array( |
|
33 | - 'getpaid_new_invoice' => 'new_invoice', |
|
34 | - 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
35 | - 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
36 | - 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
37 | - 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
38 | - 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
39 | - 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
40 | - 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
41 | - 'getpaid_new_invoice' => 'user_invoice', |
|
42 | - 'getpaid_new_customer_note' => 'user_note', |
|
43 | - 'getpaid_subscriptions_daily_cron' => 'overdue', |
|
27 | + */ |
|
28 | + public function __construct() { |
|
29 | + |
|
30 | + $this->invoice_actions = apply_filters( |
|
31 | + 'getpaid_notification_email_invoice_triggers', |
|
32 | + array( |
|
33 | + 'getpaid_new_invoice' => 'new_invoice', |
|
34 | + 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
35 | + 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
36 | + 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
37 | + 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
38 | + 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
39 | + 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
40 | + 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
41 | + 'getpaid_new_invoice' => 'user_invoice', |
|
42 | + 'getpaid_new_customer_note' => 'user_note', |
|
43 | + 'getpaid_subscriptions_daily_cron' => 'overdue', |
|
44 | 44 | |
45 | - ) |
|
46 | - ); |
|
45 | + ) |
|
46 | + ); |
|
47 | 47 | |
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
51 | - * Registers email hooks. |
|
52 | - */ |
|
53 | - public function init_hooks() { |
|
54 | - |
|
55 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 3 ); |
|
56 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
57 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
58 | - |
|
59 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
60 | - |
|
61 | - if ( $email->is_active() && method_exists( $this, $email_type ) ) { |
|
62 | - add_action( $hook, array( $this, $email_type ), 10, 2 ); |
|
63 | - } else { |
|
64 | - do_action( 'getpaid_hook_invoice_notification_email_invoice_trigger', $email ); |
|
65 | - } |
|
66 | - |
|
67 | - } |
|
68 | - |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Filters invoice merge tags. |
|
73 | - * |
|
74 | - * @param array $merge_tags |
|
75 | - * @param string $email_type |
|
76 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
77 | - */ |
|
78 | - public function invoice_merge_tags( $merge_tags, $email_type, $object ) { |
|
79 | - |
|
80 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
81 | - $merge_tags = array_merge( |
|
82 | - $merge_tags, |
|
83 | - $this->get_invoice_merge_tags( $object ) |
|
84 | - ); |
|
85 | - } |
|
86 | - |
|
87 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
88 | - $merge_tags = array_merge( |
|
89 | - $merge_tags, |
|
90 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
91 | - ); |
|
92 | - } |
|
93 | - |
|
94 | - return apply_filters( 'getpaid_invoice_notification_merge_tags', $merge_tags, $object, $email_type, $this ); |
|
95 | - |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Generates invoice merge tags. |
|
100 | - * |
|
101 | - * @param WPInv_Invoice $invoice |
|
102 | - * @return array |
|
103 | - */ |
|
104 | - public function get_invoice_merge_tags( $invoice ) { |
|
105 | - |
|
106 | - // Abort if it does not exist. |
|
107 | - if ( ! $invoice->get_id() ) { |
|
108 | - return array(); |
|
109 | - } |
|
110 | - |
|
111 | - return array( |
|
112 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
113 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
114 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
115 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
116 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
117 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
118 | - '{invoice_total}' => wpinv_price( wpinv_format_amount( $invoice->get_total() ) ), |
|
119 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
120 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
121 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
122 | - '{invoice_date}' => date( get_option( 'date_format' ), strtotime( $invoice->get_date_created(), current_time( 'timestamp' ) ) ), |
|
123 | - '{invoice_due_date}' => date( get_option( 'date_format' ), strtotime( $invoice->get_due_date(), current_time( 'timestamp' ) ) ), |
|
124 | - '{invoice_quote}' => sanitize_text_field( $invoice->get_type() ), |
|
125 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_type() ) ), |
|
126 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
127 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
128 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
129 | - ); |
|
130 | - |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Helper function to send an email. |
|
135 | - * |
|
136 | - * @param WPInv_Invoice $invoice |
|
137 | - * @param GetPaid_Notification_Email $email |
|
138 | - * @param string $type |
|
139 | - * @param string|array $recipients |
|
140 | - * @param array $extra_args Extra template args. |
|
141 | - */ |
|
142 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
143 | - |
|
144 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
145 | - |
|
146 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
147 | - $merge_tags = $email->get_merge_tags(); |
|
148 | - |
|
149 | - $result = $mailer->send( |
|
150 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
151 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
152 | - $email->get_content( $merge_tags, $extra_args ), |
|
153 | - $email->get_attachments() |
|
154 | - ); |
|
155 | - |
|
156 | - // Maybe send a copy to the admin. |
|
157 | - if ( $email->include_admin_bcc() ) { |
|
158 | - $mailer->send( |
|
159 | - wpinv_get_admin_email(), |
|
160 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
161 | - $email->get_content( $merge_tags ), |
|
162 | - $email->get_attachments() |
|
163 | - ); |
|
164 | - } |
|
165 | - |
|
166 | - if ( ! $result ) { |
|
167 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
168 | - } |
|
169 | - |
|
170 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
171 | - |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Also send emails to any cc users. |
|
176 | - * |
|
177 | - * @param array $recipients |
|
178 | - * @param GetPaid_Notification_Email $email |
|
179 | - */ |
|
180 | - public function filter_email_recipients( $recipients, $email ) { |
|
181 | - |
|
182 | - if ( ! $email->is_admin_email() ) { |
|
183 | - $cc = $email->object->get_email_cc(); |
|
184 | - |
|
185 | - if ( ! empty( $cc ) ) { |
|
186 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
187 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
188 | - } |
|
189 | - |
|
190 | - } |
|
191 | - |
|
192 | - return $recipients; |
|
193 | - |
|
194 | - } |
|
51 | + * Registers email hooks. |
|
52 | + */ |
|
53 | + public function init_hooks() { |
|
54 | + |
|
55 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 3 ); |
|
56 | + add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
57 | + foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
58 | + |
|
59 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
60 | + |
|
61 | + if ( $email->is_active() && method_exists( $this, $email_type ) ) { |
|
62 | + add_action( $hook, array( $this, $email_type ), 10, 2 ); |
|
63 | + } else { |
|
64 | + do_action( 'getpaid_hook_invoice_notification_email_invoice_trigger', $email ); |
|
65 | + } |
|
66 | + |
|
67 | + } |
|
68 | + |
|
69 | + } |
|
195 | 70 | |
196 | 71 | /** |
197 | - * Sends a new invoice notification. |
|
198 | - * |
|
199 | - * @param WPInv_Invoice $invoice |
|
200 | - */ |
|
201 | - public function new_invoice( $invoice ) { |
|
72 | + * Filters invoice merge tags. |
|
73 | + * |
|
74 | + * @param array $merge_tags |
|
75 | + * @param string $email_type |
|
76 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
77 | + */ |
|
78 | + public function invoice_merge_tags( $merge_tags, $email_type, $object ) { |
|
79 | + |
|
80 | + if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
81 | + $merge_tags = array_merge( |
|
82 | + $merge_tags, |
|
83 | + $this->get_invoice_merge_tags( $object ) |
|
84 | + ); |
|
85 | + } |
|
86 | + |
|
87 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
88 | + $merge_tags = array_merge( |
|
89 | + $merge_tags, |
|
90 | + $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
91 | + ); |
|
92 | + } |
|
93 | + |
|
94 | + return apply_filters( 'getpaid_invoice_notification_merge_tags', $merge_tags, $object, $email_type, $this ); |
|
202 | 95 | |
203 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
204 | - $recipient = wpinv_get_admin_email(); |
|
96 | + } |
|
205 | 97 | |
206 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
98 | + /** |
|
99 | + * Generates invoice merge tags. |
|
100 | + * |
|
101 | + * @param WPInv_Invoice $invoice |
|
102 | + * @return array |
|
103 | + */ |
|
104 | + public function get_invoice_merge_tags( $invoice ) { |
|
105 | + |
|
106 | + // Abort if it does not exist. |
|
107 | + if ( ! $invoice->get_id() ) { |
|
108 | + return array(); |
|
109 | + } |
|
110 | + |
|
111 | + return array( |
|
112 | + '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
113 | + '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
114 | + '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
115 | + '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
116 | + '{email}' => sanitize_email( $invoice->get_email() ), |
|
117 | + '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
118 | + '{invoice_total}' => wpinv_price( wpinv_format_amount( $invoice->get_total() ) ), |
|
119 | + '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
120 | + '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
121 | + '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
122 | + '{invoice_date}' => date( get_option( 'date_format' ), strtotime( $invoice->get_date_created(), current_time( 'timestamp' ) ) ), |
|
123 | + '{invoice_due_date}' => date( get_option( 'date_format' ), strtotime( $invoice->get_due_date(), current_time( 'timestamp' ) ) ), |
|
124 | + '{invoice_quote}' => sanitize_text_field( $invoice->get_type() ), |
|
125 | + '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_type() ) ), |
|
126 | + '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
127 | + '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
128 | + '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
129 | + ); |
|
207 | 130 | |
208 | - } |
|
131 | + } |
|
209 | 132 | |
210 | - /** |
|
211 | - * Sends a cancelled invoice notification. |
|
212 | - * |
|
213 | - * @param WPInv_Invoice $invoice |
|
214 | - */ |
|
215 | - public function cancelled_invoice( $invoice ) { |
|
133 | + /** |
|
134 | + * Helper function to send an email. |
|
135 | + * |
|
136 | + * @param WPInv_Invoice $invoice |
|
137 | + * @param GetPaid_Notification_Email $email |
|
138 | + * @param string $type |
|
139 | + * @param string|array $recipients |
|
140 | + * @param array $extra_args Extra template args. |
|
141 | + */ |
|
142 | + public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
143 | + |
|
144 | + do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
145 | + |
|
146 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
147 | + $merge_tags = $email->get_merge_tags(); |
|
148 | + |
|
149 | + $result = $mailer->send( |
|
150 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
151 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
152 | + $email->get_content( $merge_tags, $extra_args ), |
|
153 | + $email->get_attachments() |
|
154 | + ); |
|
155 | + |
|
156 | + // Maybe send a copy to the admin. |
|
157 | + if ( $email->include_admin_bcc() ) { |
|
158 | + $mailer->send( |
|
159 | + wpinv_get_admin_email(), |
|
160 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
161 | + $email->get_content( $merge_tags ), |
|
162 | + $email->get_attachments() |
|
163 | + ); |
|
164 | + } |
|
165 | + |
|
166 | + if ( ! $result ) { |
|
167 | + $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
168 | + } |
|
169 | + |
|
170 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
216 | 171 | |
217 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
218 | - $recipient = wpinv_get_admin_email(); |
|
172 | + } |
|
219 | 173 | |
220 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
174 | + /** |
|
175 | + * Also send emails to any cc users. |
|
176 | + * |
|
177 | + * @param array $recipients |
|
178 | + * @param GetPaid_Notification_Email $email |
|
179 | + */ |
|
180 | + public function filter_email_recipients( $recipients, $email ) { |
|
221 | 181 | |
222 | - } |
|
182 | + if ( ! $email->is_admin_email() ) { |
|
183 | + $cc = $email->object->get_email_cc(); |
|
223 | 184 | |
224 | - /** |
|
225 | - * Sends a failed invoice notification. |
|
226 | - * |
|
227 | - * @param WPInv_Invoice $invoice |
|
228 | - */ |
|
229 | - public function failed_invoice( $invoice ) { |
|
185 | + if ( ! empty( $cc ) ) { |
|
186 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
187 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
188 | + } |
|
230 | 189 | |
231 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
232 | - $recipient = wpinv_get_admin_email(); |
|
190 | + } |
|
233 | 191 | |
234 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
192 | + return $recipients; |
|
235 | 193 | |
236 | - } |
|
194 | + } |
|
237 | 195 | |
238 | - /** |
|
239 | - * Sends a notification whenever an invoice is put on hold. |
|
240 | - * |
|
241 | - * @param WPInv_Invoice $invoice |
|
242 | - */ |
|
243 | - public function onhold_invoice( $invoice ) { |
|
196 | + /** |
|
197 | + * Sends a new invoice notification. |
|
198 | + * |
|
199 | + * @param WPInv_Invoice $invoice |
|
200 | + */ |
|
201 | + public function new_invoice( $invoice ) { |
|
244 | 202 | |
245 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
246 | - $recipient = $invoice->get_email(); |
|
203 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
204 | + $recipient = wpinv_get_admin_email(); |
|
247 | 205 | |
248 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
206 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
249 | 207 | |
250 | - } |
|
208 | + } |
|
251 | 209 | |
252 | - /** |
|
253 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
254 | - * |
|
255 | - * @param WPInv_Invoice $invoice |
|
256 | - */ |
|
257 | - public function processing_invoice( $invoice ) { |
|
210 | + /** |
|
211 | + * Sends a cancelled invoice notification. |
|
212 | + * |
|
213 | + * @param WPInv_Invoice $invoice |
|
214 | + */ |
|
215 | + public function cancelled_invoice( $invoice ) { |
|
258 | 216 | |
259 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
260 | - $recipient = $invoice->get_email(); |
|
217 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
218 | + $recipient = wpinv_get_admin_email(); |
|
261 | 219 | |
262 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
220 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
263 | 221 | |
264 | - } |
|
222 | + } |
|
265 | 223 | |
266 | - /** |
|
267 | - * Sends a notification whenever an invoice is paid. |
|
268 | - * |
|
269 | - * @param WPInv_Invoice $invoice |
|
270 | - */ |
|
271 | - public function completed_invoice( $invoice ) { |
|
224 | + /** |
|
225 | + * Sends a failed invoice notification. |
|
226 | + * |
|
227 | + * @param WPInv_Invoice $invoice |
|
228 | + */ |
|
229 | + public function failed_invoice( $invoice ) { |
|
272 | 230 | |
273 | - // (Maybe) abort if it is a renewal invoice. |
|
274 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
275 | - return; |
|
276 | - } |
|
231 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
232 | + $recipient = wpinv_get_admin_email(); |
|
277 | 233 | |
278 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
279 | - $recipient = $invoice->get_email(); |
|
234 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
280 | 235 | |
281 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
236 | + } |
|
282 | 237 | |
283 | - } |
|
238 | + /** |
|
239 | + * Sends a notification whenever an invoice is put on hold. |
|
240 | + * |
|
241 | + * @param WPInv_Invoice $invoice |
|
242 | + */ |
|
243 | + public function onhold_invoice( $invoice ) { |
|
284 | 244 | |
285 | - /** |
|
286 | - * Sends a notification whenever an invoice is refunded. |
|
287 | - * |
|
288 | - * @param WPInv_Invoice $invoice |
|
289 | - */ |
|
290 | - public function refunded_invoice( $invoice ) { |
|
245 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
246 | + $recipient = $invoice->get_email(); |
|
291 | 247 | |
292 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
293 | - $recipient = $invoice->get_email(); |
|
248 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
294 | 249 | |
295 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
254 | + * |
|
255 | + * @param WPInv_Invoice $invoice |
|
256 | + */ |
|
257 | + public function processing_invoice( $invoice ) { |
|
258 | + |
|
259 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
260 | + $recipient = $invoice->get_email(); |
|
261 | + |
|
262 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
263 | + |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Sends a notification whenever an invoice is paid. |
|
268 | + * |
|
269 | + * @param WPInv_Invoice $invoice |
|
270 | + */ |
|
271 | + public function completed_invoice( $invoice ) { |
|
272 | + |
|
273 | + // (Maybe) abort if it is a renewal invoice. |
|
274 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
275 | + return; |
|
276 | + } |
|
277 | + |
|
278 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
279 | + $recipient = $invoice->get_email(); |
|
280 | + |
|
281 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
296 | 282 | |
297 | - } |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Sends a notification whenever an invoice is refunded. |
|
287 | + * |
|
288 | + * @param WPInv_Invoice $invoice |
|
289 | + */ |
|
290 | + public function refunded_invoice( $invoice ) { |
|
291 | + |
|
292 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
293 | + $recipient = $invoice->get_email(); |
|
294 | + |
|
295 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
296 | + |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Notifies a user about new invoices |
|
301 | - * |
|
302 | - * @param WPInv_Invoice $invoice |
|
303 | - */ |
|
304 | - public function user_invoice( $invoice ) { |
|
299 | + /** |
|
300 | + * Notifies a user about new invoices |
|
301 | + * |
|
302 | + * @param WPInv_Invoice $invoice |
|
303 | + */ |
|
304 | + public function user_invoice( $invoice ) { |
|
305 | 305 | |
306 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
307 | - $recipient = $invoice->get_email(); |
|
306 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
307 | + $recipient = $invoice->get_email(); |
|
308 | 308 | |
309 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
309 | + $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
310 | 310 | |
311 | - } |
|
311 | + } |
|
312 | 312 | |
313 | - /** |
|
314 | - * Notifies admin about new invoice notes |
|
315 | - * |
|
316 | - * @param WPInv_Invoice $invoice |
|
317 | - * @param string $note |
|
318 | - */ |
|
319 | - public function user_note( $invoice, $note ) { |
|
313 | + /** |
|
314 | + * Notifies admin about new invoice notes |
|
315 | + * |
|
316 | + * @param WPInv_Invoice $invoice |
|
317 | + * @param string $note |
|
318 | + */ |
|
319 | + public function user_note( $invoice, $note ) { |
|
320 | 320 | |
321 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
322 | - $recipient = $invoice->get_email(); |
|
321 | + $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
322 | + $recipient = $invoice->get_email(); |
|
323 | 323 | |
324 | - $this->send_email( $invoice, $email, __METHOD__, $recipient, array( 'customer_note' => $note ) ); |
|
324 | + $this->send_email( $invoice, $email, __METHOD__, $recipient, array( 'customer_note' => $note ) ); |
|
325 | 325 | |
326 | - } |
|
326 | + } |
|
327 | 327 | |
328 | 328 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | |
7 | 7 | use function SimplePay\Core\Payments\Payment_Confirmation\get_content; |
8 | 8 | |
9 | -defined( 'ABSPATH' ) || exit; |
|
9 | +defined('ABSPATH') || exit; |
|
10 | 10 | |
11 | 11 | /** |
12 | 12 | * This class handles invoice notificaiton emails. |
@@ -52,16 +52,16 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function init_hooks() { |
54 | 54 | |
55 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 3 ); |
|
56 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
57 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
55 | + add_filter('getpaid_get_email_merge_tags', array($this, 'invoice_merge_tags'), 10, 3); |
|
56 | + add_filter('getpaid_invoice_email_recipients', array($this, 'filter_email_recipients'), 10, 2); |
|
57 | + foreach ($this->invoice_actions as $hook => $email_type) { |
|
58 | 58 | |
59 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
59 | + $email = new GetPaid_Notification_Email($email_type); |
|
60 | 60 | |
61 | - if ( $email->is_active() && method_exists( $this, $email_type ) ) { |
|
62 | - add_action( $hook, array( $this, $email_type ), 10, 2 ); |
|
61 | + if ($email->is_active() && method_exists($this, $email_type)) { |
|
62 | + add_action($hook, array($this, $email_type), 10, 2); |
|
63 | 63 | } else { |
64 | - do_action( 'getpaid_hook_invoice_notification_email_invoice_trigger', $email ); |
|
64 | + do_action('getpaid_hook_invoice_notification_email_invoice_trigger', $email); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | } |
@@ -75,23 +75,23 @@ discard block |
||
75 | 75 | * @param string $email_type |
76 | 76 | * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
77 | 77 | */ |
78 | - public function invoice_merge_tags( $merge_tags, $email_type, $object ) { |
|
78 | + public function invoice_merge_tags($merge_tags, $email_type, $object) { |
|
79 | 79 | |
80 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
80 | + if (is_a($object, 'WPInv_Invoice')) { |
|
81 | 81 | $merge_tags = array_merge( |
82 | 82 | $merge_tags, |
83 | - $this->get_invoice_merge_tags( $object ) |
|
83 | + $this->get_invoice_merge_tags($object) |
|
84 | 84 | ); |
85 | 85 | } |
86 | 86 | |
87 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
87 | + if (is_a($object, 'WPInv_Subscription')) { |
|
88 | 88 | $merge_tags = array_merge( |
89 | 89 | $merge_tags, |
90 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
90 | + $this->get_invoice_merge_tags($object->get_parent_payment()) |
|
91 | 91 | ); |
92 | 92 | } |
93 | 93 | |
94 | - return apply_filters( 'getpaid_invoice_notification_merge_tags', $merge_tags, $object, $email_type, $this ); |
|
94 | + return apply_filters('getpaid_invoice_notification_merge_tags', $merge_tags, $object, $email_type, $this); |
|
95 | 95 | |
96 | 96 | } |
97 | 97 | |
@@ -101,31 +101,31 @@ discard block |
||
101 | 101 | * @param WPInv_Invoice $invoice |
102 | 102 | * @return array |
103 | 103 | */ |
104 | - public function get_invoice_merge_tags( $invoice ) { |
|
104 | + public function get_invoice_merge_tags($invoice) { |
|
105 | 105 | |
106 | 106 | // Abort if it does not exist. |
107 | - if ( ! $invoice->get_id() ) { |
|
107 | + if (!$invoice->get_id()) { |
|
108 | 108 | return array(); |
109 | 109 | } |
110 | 110 | |
111 | 111 | return array( |
112 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
113 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
114 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
115 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
116 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
117 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
118 | - '{invoice_total}' => wpinv_price( wpinv_format_amount( $invoice->get_total() ) ), |
|
119 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
120 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
121 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
122 | - '{invoice_date}' => date( get_option( 'date_format' ), strtotime( $invoice->get_date_created(), current_time( 'timestamp' ) ) ), |
|
123 | - '{invoice_due_date}' => date( get_option( 'date_format' ), strtotime( $invoice->get_due_date(), current_time( 'timestamp' ) ) ), |
|
124 | - '{invoice_quote}' => sanitize_text_field( $invoice->get_type() ), |
|
125 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_type() ) ), |
|
126 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
127 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
128 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
112 | + '{name}' => sanitize_text_field($invoice->get_user_full_name()), |
|
113 | + '{full_name}' => sanitize_text_field($invoice->get_user_full_name()), |
|
114 | + '{first_name}' => sanitize_text_field($invoice->get_first_name()), |
|
115 | + '{last_name}' => sanitize_text_field($invoice->get_last_name()), |
|
116 | + '{email}' => sanitize_email($invoice->get_email()), |
|
117 | + '{invoice_number}' => sanitize_text_field($invoice->get_number()), |
|
118 | + '{invoice_total}' => wpinv_price(wpinv_format_amount($invoice->get_total())), |
|
119 | + '{invoice_link}' => esc_url($invoice->get_view_url()), |
|
120 | + '{invoice_pay_link}' => esc_url($invoice->get_checkout_payment_url()), |
|
121 | + '{invoice_receipt_link}'=> esc_url($invoice->get_receipt_url()), |
|
122 | + '{invoice_date}' => date(get_option('date_format'), strtotime($invoice->get_date_created(), current_time('timestamp'))), |
|
123 | + '{invoice_due_date}' => date(get_option('date_format'), strtotime($invoice->get_due_date(), current_time('timestamp'))), |
|
124 | + '{invoice_quote}' => sanitize_text_field($invoice->get_type()), |
|
125 | + '{invoice_label}' => sanitize_text_field(ucfirst($invoice->get_type())), |
|
126 | + '{invoice_description}' => wp_kses_post($invoice->get_description()), |
|
127 | + '{subscription_name}' => wp_kses_post($invoice->get_subscription_name()), |
|
128 | + '{is_was}' => strtotime($invoice->get_due_date()) < current_time('timestamp') ? __('was', 'invoicing') : __('is', 'invoicing'), |
|
129 | 129 | ); |
130 | 130 | |
131 | 131 | } |
@@ -139,35 +139,35 @@ discard block |
||
139 | 139 | * @param string|array $recipients |
140 | 140 | * @param array $extra_args Extra template args. |
141 | 141 | */ |
142 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
142 | + public function send_email($invoice, $email, $type, $recipients, $extra_args = array()) { |
|
143 | 143 | |
144 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
144 | + do_action('getpaid_before_send_invoice_notification', $type, $invoice, $email); |
|
145 | 145 | |
146 | 146 | $mailer = new GetPaid_Notification_Email_Sender(); |
147 | 147 | $merge_tags = $email->get_merge_tags(); |
148 | 148 | |
149 | 149 | $result = $mailer->send( |
150 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
151 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
152 | - $email->get_content( $merge_tags, $extra_args ), |
|
150 | + apply_filters('getpaid_invoice_email_recipients', wpinv_parse_list($recipients), $email), |
|
151 | + $email->add_merge_tags($email->get_subject(), $merge_tags), |
|
152 | + $email->get_content($merge_tags, $extra_args), |
|
153 | 153 | $email->get_attachments() |
154 | 154 | ); |
155 | 155 | |
156 | 156 | // Maybe send a copy to the admin. |
157 | - if ( $email->include_admin_bcc() ) { |
|
157 | + if ($email->include_admin_bcc()) { |
|
158 | 158 | $mailer->send( |
159 | 159 | wpinv_get_admin_email(), |
160 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
161 | - $email->get_content( $merge_tags ), |
|
160 | + $email->add_merge_tags($email->get_subject() . __(' - ADMIN BCC COPY', 'invoicing'), $merge_tags), |
|
161 | + $email->get_content($merge_tags), |
|
162 | 162 | $email->get_attachments() |
163 | 163 | ); |
164 | 164 | } |
165 | 165 | |
166 | - if ( ! $result ) { |
|
167 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
166 | + if (!$result) { |
|
167 | + $invoice->add_note(sprintf(__('Failed sending %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
168 | 168 | } |
169 | 169 | |
170 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
170 | + do_action('getpaid_after_send_invoice_notification', $type, $invoice, $email); |
|
171 | 171 | |
172 | 172 | } |
173 | 173 | |
@@ -177,14 +177,14 @@ discard block |
||
177 | 177 | * @param array $recipients |
178 | 178 | * @param GetPaid_Notification_Email $email |
179 | 179 | */ |
180 | - public function filter_email_recipients( $recipients, $email ) { |
|
180 | + public function filter_email_recipients($recipients, $email) { |
|
181 | 181 | |
182 | - if ( ! $email->is_admin_email() ) { |
|
182 | + if (!$email->is_admin_email()) { |
|
183 | 183 | $cc = $email->object->get_email_cc(); |
184 | 184 | |
185 | - if ( ! empty( $cc ) ) { |
|
186 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
187 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
185 | + if (!empty($cc)) { |
|
186 | + $cc = array_map('sanitize_email', wpinv_parse_list($cc)); |
|
187 | + $recipients = array_filter(array_unique(array_merge($recipients, $cc))); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | } |
@@ -198,12 +198,12 @@ discard block |
||
198 | 198 | * |
199 | 199 | * @param WPInv_Invoice $invoice |
200 | 200 | */ |
201 | - public function new_invoice( $invoice ) { |
|
201 | + public function new_invoice($invoice) { |
|
202 | 202 | |
203 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
203 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
204 | 204 | $recipient = wpinv_get_admin_email(); |
205 | 205 | |
206 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
206 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
207 | 207 | |
208 | 208 | } |
209 | 209 | |
@@ -212,12 +212,12 @@ discard block |
||
212 | 212 | * |
213 | 213 | * @param WPInv_Invoice $invoice |
214 | 214 | */ |
215 | - public function cancelled_invoice( $invoice ) { |
|
215 | + public function cancelled_invoice($invoice) { |
|
216 | 216 | |
217 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
217 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
218 | 218 | $recipient = wpinv_get_admin_email(); |
219 | 219 | |
220 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
220 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
221 | 221 | |
222 | 222 | } |
223 | 223 | |
@@ -226,12 +226,12 @@ discard block |
||
226 | 226 | * |
227 | 227 | * @param WPInv_Invoice $invoice |
228 | 228 | */ |
229 | - public function failed_invoice( $invoice ) { |
|
229 | + public function failed_invoice($invoice) { |
|
230 | 230 | |
231 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
231 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
232 | 232 | $recipient = wpinv_get_admin_email(); |
233 | 233 | |
234 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
234 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
235 | 235 | |
236 | 236 | } |
237 | 237 | |
@@ -240,12 +240,12 @@ discard block |
||
240 | 240 | * |
241 | 241 | * @param WPInv_Invoice $invoice |
242 | 242 | */ |
243 | - public function onhold_invoice( $invoice ) { |
|
243 | + public function onhold_invoice($invoice) { |
|
244 | 244 | |
245 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
245 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
246 | 246 | $recipient = $invoice->get_email(); |
247 | 247 | |
248 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
248 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
249 | 249 | |
250 | 250 | } |
251 | 251 | |
@@ -254,12 +254,12 @@ discard block |
||
254 | 254 | * |
255 | 255 | * @param WPInv_Invoice $invoice |
256 | 256 | */ |
257 | - public function processing_invoice( $invoice ) { |
|
257 | + public function processing_invoice($invoice) { |
|
258 | 258 | |
259 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
259 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
260 | 260 | $recipient = $invoice->get_email(); |
261 | 261 | |
262 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
262 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
263 | 263 | |
264 | 264 | } |
265 | 265 | |
@@ -268,17 +268,17 @@ discard block |
||
268 | 268 | * |
269 | 269 | * @param WPInv_Invoice $invoice |
270 | 270 | */ |
271 | - public function completed_invoice( $invoice ) { |
|
271 | + public function completed_invoice($invoice) { |
|
272 | 272 | |
273 | 273 | // (Maybe) abort if it is a renewal invoice. |
274 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
274 | + if ($invoice->is_renewal() && !wpinv_get_option('email_completed_invoice_renewal_active', false)) { |
|
275 | 275 | return; |
276 | 276 | } |
277 | 277 | |
278 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
278 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
279 | 279 | $recipient = $invoice->get_email(); |
280 | 280 | |
281 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
281 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
282 | 282 | |
283 | 283 | } |
284 | 284 | |
@@ -287,12 +287,12 @@ discard block |
||
287 | 287 | * |
288 | 288 | * @param WPInv_Invoice $invoice |
289 | 289 | */ |
290 | - public function refunded_invoice( $invoice ) { |
|
290 | + public function refunded_invoice($invoice) { |
|
291 | 291 | |
292 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
292 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
293 | 293 | $recipient = $invoice->get_email(); |
294 | 294 | |
295 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
295 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
296 | 296 | |
297 | 297 | } |
298 | 298 | |
@@ -301,12 +301,12 @@ discard block |
||
301 | 301 | * |
302 | 302 | * @param WPInv_Invoice $invoice |
303 | 303 | */ |
304 | - public function user_invoice( $invoice ) { |
|
304 | + public function user_invoice($invoice) { |
|
305 | 305 | |
306 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
306 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
307 | 307 | $recipient = $invoice->get_email(); |
308 | 308 | |
309 | - $this->send_email( $invoice, $email, __METHOD__, $recipient ); |
|
309 | + $this->send_email($invoice, $email, __METHOD__, $recipient); |
|
310 | 310 | |
311 | 311 | } |
312 | 312 | |
@@ -316,12 +316,12 @@ discard block |
||
316 | 316 | * @param WPInv_Invoice $invoice |
317 | 317 | * @param string $note |
318 | 318 | */ |
319 | - public function user_note( $invoice, $note ) { |
|
319 | + public function user_note($invoice, $note) { |
|
320 | 320 | |
321 | - $email = new GetPaid_Notification_Email( __METHOD__, $invoice ); |
|
321 | + $email = new GetPaid_Notification_Email(__METHOD__, $invoice); |
|
322 | 322 | $recipient = $invoice->get_email(); |
323 | 323 | |
324 | - $this->send_email( $invoice, $email, __METHOD__, $recipient, array( 'customer_note' => $note ) ); |
|
324 | + $this->send_email($invoice, $email, __METHOD__, $recipient, array('customer_note' => $note)); |
|
325 | 325 | |
326 | 326 | } |
327 | 327 |