@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | */ |
10 | 10 | |
11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
12 | - exit; |
|
12 | + exit; |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
21 | 21 | */ |
22 | 22 | abstract class GetPaid_Data { |
23 | 23 | |
24 | - /** |
|
25 | - * ID for this object. |
|
26 | - * |
|
27 | - * @since 1.0.19 |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - protected $id = 0; |
|
31 | - |
|
32 | - /** |
|
33 | - * Core data for this object. Name value pairs (name + default value). |
|
34 | - * |
|
35 | - * @since 1.0.19 |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $data = array(); |
|
39 | - |
|
40 | - /** |
|
41 | - * Core data changes for this object. |
|
42 | - * |
|
43 | - * @since 1.0.19 |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $changes = array(); |
|
47 | - |
|
48 | - /** |
|
49 | - * This is false until the object is read from the DB. |
|
50 | - * |
|
51 | - * @since 1.0.19 |
|
52 | - * @var bool |
|
53 | - */ |
|
54 | - protected $object_read = false; |
|
55 | - |
|
56 | - /** |
|
57 | - * This is the name of this object type. |
|
58 | - * |
|
59 | - * @since 1.0.19 |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - protected $object_type = 'data'; |
|
63 | - |
|
64 | - /** |
|
65 | - * Extra data for this object. Name value pairs (name + default value). |
|
66 | - * Used as a standard way for sub classes (like item types) to add |
|
67 | - * additional information to an inherited class. |
|
68 | - * |
|
69 | - * @since 1.0.19 |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected $extra_data = array(); |
|
73 | - |
|
74 | - /** |
|
75 | - * Set to _data on construct so we can track and reset data if needed. |
|
76 | - * |
|
77 | - * @since 1.0.19 |
|
78 | - * @var array |
|
79 | - */ |
|
80 | - protected $default_data = array(); |
|
81 | - |
|
82 | - /** |
|
83 | - * Contains a reference to the data store for this class. |
|
84 | - * |
|
85 | - * @since 1.0.19 |
|
86 | - * @var GetPaid_Data_Store |
|
87 | - */ |
|
88 | - protected $data_store; |
|
89 | - |
|
90 | - /** |
|
91 | - * Stores meta in cache for future reads. |
|
92 | - * A group must be set to to enable caching. |
|
93 | - * |
|
94 | - * @since 1.0.19 |
|
95 | - * @var string |
|
96 | - */ |
|
97 | - protected $cache_group = ''; |
|
98 | - |
|
99 | - /** |
|
100 | - * Stores the last error. |
|
101 | - * |
|
102 | - * @since 1.0.19 |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - public $last_error = ''; |
|
106 | - |
|
107 | - /** |
|
108 | - * Stores additional meta data. |
|
109 | - * |
|
110 | - * @since 1.0.19 |
|
111 | - * @var array |
|
112 | - */ |
|
113 | - protected $meta_data = null; |
|
114 | - |
|
115 | - /** |
|
116 | - * Default constructor. |
|
117 | - * |
|
118 | - * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
119 | - */ |
|
120 | - public function __construct( $read = 0 ) { |
|
121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
122 | - $this->default_data = $this->data; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Only store the object ID to avoid serializing the data object instance. |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function __sleep() { |
|
131 | - return array( 'id' ); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Re-run the constructor with the object ID. |
|
136 | - * |
|
137 | - * If the object no longer exists, remove the ID. |
|
138 | - */ |
|
139 | - public function __wakeup() { |
|
140 | - $this->__construct( absint( $this->id ) ); |
|
141 | - |
|
142 | - if ( ! empty( $this->last_error ) ) { |
|
143 | - $this->set_id( 0 ); |
|
144 | - } |
|
145 | - |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
150 | - * |
|
151 | - * @since 1.0.19 |
|
152 | - */ |
|
153 | - public function __clone() { |
|
154 | - $this->maybe_read_meta_data(); |
|
155 | - if ( ! empty( $this->meta_data ) ) { |
|
156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
158 | - if ( ! empty( $meta->id ) ) { |
|
159 | - $this->meta_data[ $array_key ]->id = null; |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Get the data store. |
|
167 | - * |
|
168 | - * @since 1.0.19 |
|
169 | - * @return object |
|
170 | - */ |
|
171 | - public function get_data_store() { |
|
172 | - return $this->data_store; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Get the object type. |
|
177 | - * |
|
178 | - * @since 1.0.19 |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public function get_object_type() { |
|
182 | - return $this->object_type; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Returns the unique ID for this object. |
|
187 | - * |
|
188 | - * @since 1.0.19 |
|
189 | - * @return int |
|
190 | - */ |
|
191 | - public function get_id() { |
|
192 | - return $this->id; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Get form status. |
|
197 | - * |
|
198 | - * @since 1.0.19 |
|
199 | - * @param string $context View or edit context. |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - public function get_status( $context = 'view' ) { |
|
203 | - return $this->get_prop( 'status', $context ); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Delete an object, set the ID to 0, and return result. |
|
208 | - * |
|
209 | - * @since 1.0.19 |
|
210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
211 | - * @return bool result |
|
212 | - */ |
|
213 | - public function delete( $force_delete = false ) { |
|
214 | - if ( $this->data_store && $this->exists() ) { |
|
215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
216 | - $this->set_id( 0 ); |
|
217 | - return true; |
|
218 | - } |
|
219 | - return false; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Save should create or update based on object existence. |
|
224 | - * |
|
225 | - * @since 1.0.19 |
|
226 | - * @return int |
|
227 | - */ |
|
228 | - public function save() { |
|
229 | - if ( ! $this->data_store ) { |
|
230 | - return $this->get_id(); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
235 | - * |
|
236 | - * @param GetPaid_Data $this The object being saved. |
|
237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
238 | - */ |
|
239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
240 | - |
|
241 | - if ( $this->get_id() ) { |
|
242 | - $this->data_store->update( $this ); |
|
243 | - } else { |
|
244 | - $this->data_store->create( $this ); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Trigger action after saving to the DB. |
|
249 | - * |
|
250 | - * @param GetPaid_Data $this The object being saved. |
|
251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
252 | - */ |
|
253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
254 | - |
|
255 | - return $this->get_id(); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Change data to JSON format. |
|
260 | - * |
|
261 | - * @since 1.0.19 |
|
262 | - * @return string Data in JSON format. |
|
263 | - */ |
|
264 | - public function __toString() { |
|
265 | - return wp_json_encode( $this->get_data() ); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Returns all data for this object. |
|
270 | - * |
|
271 | - * @since 1.0.19 |
|
272 | - * @return array |
|
273 | - */ |
|
274 | - public function get_data() { |
|
275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Returns array of expected data keys for this object. |
|
280 | - * |
|
281 | - * @since 1.0.19 |
|
282 | - * @return array |
|
283 | - */ |
|
284 | - public function get_data_keys() { |
|
285 | - return array_keys( $this->data ); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
290 | - * |
|
291 | - * @since 1.0.19 |
|
292 | - * @return array |
|
293 | - */ |
|
294 | - public function get_extra_data_keys() { |
|
295 | - return array_keys( $this->extra_data ); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Filter null meta values from array. |
|
300 | - * |
|
301 | - * @since 1.0.19 |
|
302 | - * @param mixed $meta Meta value to check. |
|
303 | - * @return bool |
|
304 | - */ |
|
305 | - protected function filter_null_meta( $meta ) { |
|
306 | - return ! is_null( $meta->value ); |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Get All Meta Data. |
|
311 | - * |
|
312 | - * @since 1.0.19 |
|
313 | - * @return array of objects. |
|
314 | - */ |
|
315 | - public function get_meta_data() { |
|
316 | - $this->maybe_read_meta_data(); |
|
317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Check if the key is an internal one. |
|
322 | - * |
|
323 | - * @since 1.0.19 |
|
324 | - * @param string $key Key to check. |
|
325 | - * @return bool true if it's an internal key, false otherwise |
|
326 | - */ |
|
327 | - protected function is_internal_meta_key( $key ) { |
|
328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
329 | - |
|
330 | - if ( ! $internal_meta_key ) { |
|
331 | - return false; |
|
332 | - } |
|
333 | - |
|
334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
335 | - |
|
336 | - if ( ! $has_setter_or_getter ) { |
|
337 | - return false; |
|
338 | - } |
|
339 | - |
|
340 | - /* translators: %s: $key Key to check */ |
|
341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
342 | - |
|
343 | - return true; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Magic method for setting data fields. |
|
348 | - * |
|
349 | - * This method does not update custom fields in the database. |
|
350 | - * |
|
351 | - * @since 1.0.19 |
|
352 | - * @access public |
|
353 | - * |
|
354 | - */ |
|
355 | - public function __set( $key, $value ) { |
|
356 | - |
|
357 | - if ( 'id' == strtolower( $key ) ) { |
|
358 | - return $this->set_id( $value ); |
|
359 | - } |
|
360 | - |
|
361 | - if ( method_exists( $this, "set_$key") ) { |
|
362 | - |
|
363 | - /* translators: %s: $key Key to set */ |
|
364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
365 | - |
|
366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
367 | - } else { |
|
368 | - $this->set_prop( $key, $value ); |
|
369 | - } |
|
370 | - |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
24 | + /** |
|
25 | + * ID for this object. |
|
26 | + * |
|
27 | + * @since 1.0.19 |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + protected $id = 0; |
|
31 | + |
|
32 | + /** |
|
33 | + * Core data for this object. Name value pairs (name + default value). |
|
34 | + * |
|
35 | + * @since 1.0.19 |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $data = array(); |
|
39 | + |
|
40 | + /** |
|
41 | + * Core data changes for this object. |
|
42 | + * |
|
43 | + * @since 1.0.19 |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $changes = array(); |
|
47 | + |
|
48 | + /** |
|
49 | + * This is false until the object is read from the DB. |
|
50 | + * |
|
51 | + * @since 1.0.19 |
|
52 | + * @var bool |
|
53 | + */ |
|
54 | + protected $object_read = false; |
|
55 | + |
|
56 | + /** |
|
57 | + * This is the name of this object type. |
|
58 | + * |
|
59 | + * @since 1.0.19 |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + protected $object_type = 'data'; |
|
63 | + |
|
64 | + /** |
|
65 | + * Extra data for this object. Name value pairs (name + default value). |
|
66 | + * Used as a standard way for sub classes (like item types) to add |
|
67 | + * additional information to an inherited class. |
|
68 | + * |
|
69 | + * @since 1.0.19 |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected $extra_data = array(); |
|
73 | + |
|
74 | + /** |
|
75 | + * Set to _data on construct so we can track and reset data if needed. |
|
76 | + * |
|
77 | + * @since 1.0.19 |
|
78 | + * @var array |
|
79 | + */ |
|
80 | + protected $default_data = array(); |
|
81 | + |
|
82 | + /** |
|
83 | + * Contains a reference to the data store for this class. |
|
84 | + * |
|
85 | + * @since 1.0.19 |
|
86 | + * @var GetPaid_Data_Store |
|
87 | + */ |
|
88 | + protected $data_store; |
|
89 | + |
|
90 | + /** |
|
91 | + * Stores meta in cache for future reads. |
|
92 | + * A group must be set to to enable caching. |
|
93 | + * |
|
94 | + * @since 1.0.19 |
|
95 | + * @var string |
|
96 | + */ |
|
97 | + protected $cache_group = ''; |
|
98 | + |
|
99 | + /** |
|
100 | + * Stores the last error. |
|
101 | + * |
|
102 | + * @since 1.0.19 |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + public $last_error = ''; |
|
106 | + |
|
107 | + /** |
|
108 | + * Stores additional meta data. |
|
109 | + * |
|
110 | + * @since 1.0.19 |
|
111 | + * @var array |
|
112 | + */ |
|
113 | + protected $meta_data = null; |
|
114 | + |
|
115 | + /** |
|
116 | + * Default constructor. |
|
117 | + * |
|
118 | + * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
119 | + */ |
|
120 | + public function __construct( $read = 0 ) { |
|
121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
122 | + $this->default_data = $this->data; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Only store the object ID to avoid serializing the data object instance. |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function __sleep() { |
|
131 | + return array( 'id' ); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Re-run the constructor with the object ID. |
|
136 | + * |
|
137 | + * If the object no longer exists, remove the ID. |
|
138 | + */ |
|
139 | + public function __wakeup() { |
|
140 | + $this->__construct( absint( $this->id ) ); |
|
141 | + |
|
142 | + if ( ! empty( $this->last_error ) ) { |
|
143 | + $this->set_id( 0 ); |
|
144 | + } |
|
145 | + |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
150 | + * |
|
151 | + * @since 1.0.19 |
|
152 | + */ |
|
153 | + public function __clone() { |
|
154 | + $this->maybe_read_meta_data(); |
|
155 | + if ( ! empty( $this->meta_data ) ) { |
|
156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
158 | + if ( ! empty( $meta->id ) ) { |
|
159 | + $this->meta_data[ $array_key ]->id = null; |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Get the data store. |
|
167 | + * |
|
168 | + * @since 1.0.19 |
|
169 | + * @return object |
|
170 | + */ |
|
171 | + public function get_data_store() { |
|
172 | + return $this->data_store; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Get the object type. |
|
177 | + * |
|
178 | + * @since 1.0.19 |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public function get_object_type() { |
|
182 | + return $this->object_type; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Returns the unique ID for this object. |
|
187 | + * |
|
188 | + * @since 1.0.19 |
|
189 | + * @return int |
|
190 | + */ |
|
191 | + public function get_id() { |
|
192 | + return $this->id; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Get form status. |
|
197 | + * |
|
198 | + * @since 1.0.19 |
|
199 | + * @param string $context View or edit context. |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + public function get_status( $context = 'view' ) { |
|
203 | + return $this->get_prop( 'status', $context ); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Delete an object, set the ID to 0, and return result. |
|
208 | + * |
|
209 | + * @since 1.0.19 |
|
210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
211 | + * @return bool result |
|
212 | + */ |
|
213 | + public function delete( $force_delete = false ) { |
|
214 | + if ( $this->data_store && $this->exists() ) { |
|
215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
216 | + $this->set_id( 0 ); |
|
217 | + return true; |
|
218 | + } |
|
219 | + return false; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Save should create or update based on object existence. |
|
224 | + * |
|
225 | + * @since 1.0.19 |
|
226 | + * @return int |
|
227 | + */ |
|
228 | + public function save() { |
|
229 | + if ( ! $this->data_store ) { |
|
230 | + return $this->get_id(); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
235 | + * |
|
236 | + * @param GetPaid_Data $this The object being saved. |
|
237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
238 | + */ |
|
239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
240 | + |
|
241 | + if ( $this->get_id() ) { |
|
242 | + $this->data_store->update( $this ); |
|
243 | + } else { |
|
244 | + $this->data_store->create( $this ); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Trigger action after saving to the DB. |
|
249 | + * |
|
250 | + * @param GetPaid_Data $this The object being saved. |
|
251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
252 | + */ |
|
253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
254 | + |
|
255 | + return $this->get_id(); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Change data to JSON format. |
|
260 | + * |
|
261 | + * @since 1.0.19 |
|
262 | + * @return string Data in JSON format. |
|
263 | + */ |
|
264 | + public function __toString() { |
|
265 | + return wp_json_encode( $this->get_data() ); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Returns all data for this object. |
|
270 | + * |
|
271 | + * @since 1.0.19 |
|
272 | + * @return array |
|
273 | + */ |
|
274 | + public function get_data() { |
|
275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Returns array of expected data keys for this object. |
|
280 | + * |
|
281 | + * @since 1.0.19 |
|
282 | + * @return array |
|
283 | + */ |
|
284 | + public function get_data_keys() { |
|
285 | + return array_keys( $this->data ); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
290 | + * |
|
291 | + * @since 1.0.19 |
|
292 | + * @return array |
|
293 | + */ |
|
294 | + public function get_extra_data_keys() { |
|
295 | + return array_keys( $this->extra_data ); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Filter null meta values from array. |
|
300 | + * |
|
301 | + * @since 1.0.19 |
|
302 | + * @param mixed $meta Meta value to check. |
|
303 | + * @return bool |
|
304 | + */ |
|
305 | + protected function filter_null_meta( $meta ) { |
|
306 | + return ! is_null( $meta->value ); |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Get All Meta Data. |
|
311 | + * |
|
312 | + * @since 1.0.19 |
|
313 | + * @return array of objects. |
|
314 | + */ |
|
315 | + public function get_meta_data() { |
|
316 | + $this->maybe_read_meta_data(); |
|
317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Check if the key is an internal one. |
|
322 | + * |
|
323 | + * @since 1.0.19 |
|
324 | + * @param string $key Key to check. |
|
325 | + * @return bool true if it's an internal key, false otherwise |
|
326 | + */ |
|
327 | + protected function is_internal_meta_key( $key ) { |
|
328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
329 | + |
|
330 | + if ( ! $internal_meta_key ) { |
|
331 | + return false; |
|
332 | + } |
|
333 | + |
|
334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
335 | + |
|
336 | + if ( ! $has_setter_or_getter ) { |
|
337 | + return false; |
|
338 | + } |
|
339 | + |
|
340 | + /* translators: %s: $key Key to check */ |
|
341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
342 | + |
|
343 | + return true; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Magic method for setting data fields. |
|
348 | + * |
|
349 | + * This method does not update custom fields in the database. |
|
350 | + * |
|
351 | + * @since 1.0.19 |
|
352 | + * @access public |
|
353 | + * |
|
354 | + */ |
|
355 | + public function __set( $key, $value ) { |
|
356 | + |
|
357 | + if ( 'id' == strtolower( $key ) ) { |
|
358 | + return $this->set_id( $value ); |
|
359 | + } |
|
360 | + |
|
361 | + if ( method_exists( $this, "set_$key") ) { |
|
362 | + |
|
363 | + /* translators: %s: $key Key to set */ |
|
364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
365 | + |
|
366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
367 | + } else { |
|
368 | + $this->set_prop( $key, $value ); |
|
369 | + } |
|
370 | + |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | 374 | * Margic method for retrieving a property. |
375 | 375 | */ |
376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
378 | 378 | // Check if we have a helper method for that. |
379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
380 | 380 | |
381 | - if ( 'post_type' != $key ) { |
|
382 | - /* translators: %s: $key Key to set */ |
|
383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
384 | - } |
|
381 | + if ( 'post_type' != $key ) { |
|
382 | + /* translators: %s: $key Key to set */ |
|
383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
384 | + } |
|
385 | 385 | |
386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
387 | 387 | } |
@@ -391,515 +391,515 @@ discard block |
||
391 | 391 | return $this->post->$key; |
392 | 392 | } |
393 | 393 | |
394 | - return $this->get_prop( $key ); |
|
395 | - |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Get Meta Data by Key. |
|
400 | - * |
|
401 | - * @since 1.0.19 |
|
402 | - * @param string $key Meta Key. |
|
403 | - * @param bool $single return first found meta with key, or all with $key. |
|
404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
405 | - * @return mixed |
|
406 | - */ |
|
407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
408 | - |
|
409 | - // Check if this is an internal meta key. |
|
410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
412 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
413 | - $function = 'get_' . $_key; |
|
414 | - |
|
415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
416 | - return $this->{$function}(); |
|
417 | - } |
|
418 | - } |
|
419 | - |
|
420 | - // Read the meta data if not yet read. |
|
421 | - $this->maybe_read_meta_data(); |
|
422 | - $meta_data = $this->get_meta_data(); |
|
423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
424 | - $value = $single ? '' : array(); |
|
425 | - |
|
426 | - if ( ! empty( $array_keys ) ) { |
|
427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
428 | - if ( $single ) { |
|
429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
430 | - } else { |
|
431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - if ( 'view' === $context ) { |
|
436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
437 | - } |
|
438 | - |
|
439 | - return $value; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
444 | - * |
|
445 | - * @since 1.0.19 |
|
446 | - * @param string $key Meta Key. |
|
447 | - * @return boolean |
|
448 | - */ |
|
449 | - public function meta_exists( $key = '' ) { |
|
450 | - $this->maybe_read_meta_data(); |
|
451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
452 | - return in_array( $key, $array_keys, true ); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Set all meta data from array. |
|
457 | - * |
|
458 | - * @since 1.0.19 |
|
459 | - * @param array $data Key/Value pairs. |
|
460 | - */ |
|
461 | - public function set_meta_data( $data ) { |
|
462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
463 | - $this->maybe_read_meta_data(); |
|
464 | - foreach ( $data as $meta ) { |
|
465 | - $meta = (array) $meta; |
|
466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
468 | - array( |
|
469 | - 'id' => $meta['id'], |
|
470 | - 'key' => $meta['key'], |
|
471 | - 'value' => $meta['value'], |
|
472 | - ) |
|
473 | - ); |
|
474 | - } |
|
475 | - } |
|
476 | - } |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Add meta data. |
|
481 | - * |
|
482 | - * @since 1.0.19 |
|
483 | - * |
|
484 | - * @param string $key Meta key. |
|
485 | - * @param string|array $value Meta value. |
|
486 | - * @param bool $unique Should this be a unique key?. |
|
487 | - */ |
|
488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
490 | - $function = 'set_' . $key; |
|
491 | - |
|
492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
493 | - return $this->{$function}( $value ); |
|
494 | - } |
|
495 | - } |
|
496 | - |
|
497 | - $this->maybe_read_meta_data(); |
|
498 | - if ( $unique ) { |
|
499 | - $this->delete_meta_data( $key ); |
|
500 | - } |
|
501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
502 | - array( |
|
503 | - 'key' => $key, |
|
504 | - 'value' => $value, |
|
505 | - ) |
|
506 | - ); |
|
507 | - |
|
508 | - $this->save(); |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Update meta data by key or ID, if provided. |
|
513 | - * |
|
514 | - * @since 1.0.19 |
|
515 | - * |
|
516 | - * @param string $key Meta key. |
|
517 | - * @param string|array $value Meta value. |
|
518 | - * @param int $meta_id Meta ID. |
|
519 | - */ |
|
520 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
521 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
522 | - $function = 'set_' . $key; |
|
523 | - |
|
524 | - if ( is_callable( array( $this, $function ) ) ) { |
|
525 | - return $this->{$function}( $value ); |
|
526 | - } |
|
527 | - } |
|
528 | - |
|
529 | - $this->maybe_read_meta_data(); |
|
530 | - |
|
531 | - $array_key = false; |
|
532 | - |
|
533 | - if ( $meta_id ) { |
|
534 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
535 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
536 | - } else { |
|
537 | - // Find matches by key. |
|
538 | - $matches = array(); |
|
539 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
540 | - if ( $meta->key === $key ) { |
|
541 | - $matches[] = $meta_data_array_key; |
|
542 | - } |
|
543 | - } |
|
544 | - |
|
545 | - if ( ! empty( $matches ) ) { |
|
546 | - // Set matches to null so only one key gets the new value. |
|
547 | - foreach ( $matches as $meta_data_array_key ) { |
|
548 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
549 | - } |
|
550 | - $array_key = current( $matches ); |
|
551 | - } |
|
552 | - } |
|
553 | - |
|
554 | - if ( false !== $array_key ) { |
|
555 | - $meta = $this->meta_data[ $array_key ]; |
|
556 | - $meta->key = $key; |
|
557 | - $meta->value = $value; |
|
558 | - } else { |
|
559 | - $this->add_meta_data( $key, $value, true ); |
|
560 | - } |
|
561 | - } |
|
562 | - |
|
563 | - /** |
|
564 | - * Delete meta data. |
|
565 | - * |
|
566 | - * @since 1.0.19 |
|
567 | - * @param string $key Meta key. |
|
568 | - */ |
|
569 | - public function delete_meta_data( $key ) { |
|
570 | - $this->maybe_read_meta_data(); |
|
571 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
572 | - |
|
573 | - if ( $array_keys ) { |
|
574 | - foreach ( $array_keys as $array_key ) { |
|
575 | - $this->meta_data[ $array_key ]->value = null; |
|
576 | - } |
|
577 | - } |
|
578 | - } |
|
579 | - |
|
580 | - /** |
|
581 | - * Delete meta data. |
|
582 | - * |
|
583 | - * @since 1.0.19 |
|
584 | - * @param int $mid Meta ID. |
|
585 | - */ |
|
586 | - public function delete_meta_data_by_mid( $mid ) { |
|
587 | - $this->maybe_read_meta_data(); |
|
588 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
589 | - |
|
590 | - if ( $array_keys ) { |
|
591 | - foreach ( $array_keys as $array_key ) { |
|
592 | - $this->meta_data[ $array_key ]->value = null; |
|
593 | - } |
|
594 | - } |
|
595 | - } |
|
596 | - |
|
597 | - /** |
|
598 | - * Read meta data if null. |
|
599 | - * |
|
600 | - * @since 1.0.19 |
|
601 | - */ |
|
602 | - protected function maybe_read_meta_data() { |
|
603 | - if ( is_null( $this->meta_data ) ) { |
|
604 | - $this->read_meta_data(); |
|
605 | - } |
|
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * Read Meta Data from the database. Ignore any internal properties. |
|
610 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
611 | - * |
|
612 | - * @since 1.0.19 |
|
613 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
614 | - */ |
|
615 | - public function read_meta_data( $force_read = false ) { |
|
616 | - |
|
617 | - // Reset meta data. |
|
618 | - $this->meta_data = array(); |
|
619 | - |
|
620 | - // Maybe abort early. |
|
621 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
622 | - return; |
|
623 | - } |
|
624 | - |
|
625 | - // Only read from cache if the cache key is set. |
|
626 | - $cache_key = null; |
|
627 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
628 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
629 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
630 | - } |
|
631 | - |
|
632 | - // Should we force read? |
|
633 | - if ( empty( $raw_meta_data ) ) { |
|
634 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
635 | - |
|
636 | - if ( ! empty( $cache_key ) ) { |
|
637 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
638 | - } |
|
639 | - |
|
640 | - } |
|
641 | - |
|
642 | - // Set meta data. |
|
643 | - if ( is_array( $raw_meta_data ) ) { |
|
644 | - |
|
645 | - foreach ( $raw_meta_data as $meta ) { |
|
646 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
647 | - array( |
|
648 | - 'id' => (int) $meta->meta_id, |
|
649 | - 'key' => $meta->meta_key, |
|
650 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
651 | - ) |
|
652 | - ); |
|
653 | - } |
|
654 | - |
|
655 | - } |
|
656 | - |
|
657 | - } |
|
658 | - |
|
659 | - /** |
|
660 | - * Update Meta Data in the database. |
|
661 | - * |
|
662 | - * @since 1.0.19 |
|
663 | - */ |
|
664 | - public function save_meta_data() { |
|
665 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
666 | - return; |
|
667 | - } |
|
668 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
669 | - if ( is_null( $meta->value ) ) { |
|
670 | - if ( ! empty( $meta->id ) ) { |
|
671 | - $this->data_store->delete_meta( $this, $meta ); |
|
672 | - unset( $this->meta_data[ $array_key ] ); |
|
673 | - } |
|
674 | - } elseif ( empty( $meta->id ) ) { |
|
675 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
676 | - $meta->apply_changes(); |
|
677 | - } else { |
|
678 | - if ( $meta->get_changes() ) { |
|
679 | - $this->data_store->update_meta( $this, $meta ); |
|
680 | - $meta->apply_changes(); |
|
681 | - } |
|
682 | - } |
|
683 | - } |
|
684 | - if ( ! empty( $this->cache_group ) ) { |
|
685 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
686 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * Set ID. |
|
692 | - * |
|
693 | - * @since 1.0.19 |
|
694 | - * @param int $id ID. |
|
695 | - */ |
|
696 | - public function set_id( $id ) { |
|
697 | - $this->id = absint( $id ); |
|
698 | - } |
|
699 | - |
|
700 | - /** |
|
701 | - * Sets item status. |
|
702 | - * |
|
703 | - * @since 1.0.19 |
|
704 | - * @param string $status New status. |
|
705 | - * @return array details of change. |
|
706 | - */ |
|
707 | - public function set_status( $status ) { |
|
394 | + return $this->get_prop( $key ); |
|
395 | + |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Get Meta Data by Key. |
|
400 | + * |
|
401 | + * @since 1.0.19 |
|
402 | + * @param string $key Meta Key. |
|
403 | + * @param bool $single return first found meta with key, or all with $key. |
|
404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
405 | + * @return mixed |
|
406 | + */ |
|
407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
408 | + |
|
409 | + // Check if this is an internal meta key. |
|
410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
412 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
413 | + $function = 'get_' . $_key; |
|
414 | + |
|
415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
416 | + return $this->{$function}(); |
|
417 | + } |
|
418 | + } |
|
419 | + |
|
420 | + // Read the meta data if not yet read. |
|
421 | + $this->maybe_read_meta_data(); |
|
422 | + $meta_data = $this->get_meta_data(); |
|
423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
424 | + $value = $single ? '' : array(); |
|
425 | + |
|
426 | + if ( ! empty( $array_keys ) ) { |
|
427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
428 | + if ( $single ) { |
|
429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
430 | + } else { |
|
431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + if ( 'view' === $context ) { |
|
436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
437 | + } |
|
438 | + |
|
439 | + return $value; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
444 | + * |
|
445 | + * @since 1.0.19 |
|
446 | + * @param string $key Meta Key. |
|
447 | + * @return boolean |
|
448 | + */ |
|
449 | + public function meta_exists( $key = '' ) { |
|
450 | + $this->maybe_read_meta_data(); |
|
451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
452 | + return in_array( $key, $array_keys, true ); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Set all meta data from array. |
|
457 | + * |
|
458 | + * @since 1.0.19 |
|
459 | + * @param array $data Key/Value pairs. |
|
460 | + */ |
|
461 | + public function set_meta_data( $data ) { |
|
462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
463 | + $this->maybe_read_meta_data(); |
|
464 | + foreach ( $data as $meta ) { |
|
465 | + $meta = (array) $meta; |
|
466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
468 | + array( |
|
469 | + 'id' => $meta['id'], |
|
470 | + 'key' => $meta['key'], |
|
471 | + 'value' => $meta['value'], |
|
472 | + ) |
|
473 | + ); |
|
474 | + } |
|
475 | + } |
|
476 | + } |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Add meta data. |
|
481 | + * |
|
482 | + * @since 1.0.19 |
|
483 | + * |
|
484 | + * @param string $key Meta key. |
|
485 | + * @param string|array $value Meta value. |
|
486 | + * @param bool $unique Should this be a unique key?. |
|
487 | + */ |
|
488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
490 | + $function = 'set_' . $key; |
|
491 | + |
|
492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
493 | + return $this->{$function}( $value ); |
|
494 | + } |
|
495 | + } |
|
496 | + |
|
497 | + $this->maybe_read_meta_data(); |
|
498 | + if ( $unique ) { |
|
499 | + $this->delete_meta_data( $key ); |
|
500 | + } |
|
501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
502 | + array( |
|
503 | + 'key' => $key, |
|
504 | + 'value' => $value, |
|
505 | + ) |
|
506 | + ); |
|
507 | + |
|
508 | + $this->save(); |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Update meta data by key or ID, if provided. |
|
513 | + * |
|
514 | + * @since 1.0.19 |
|
515 | + * |
|
516 | + * @param string $key Meta key. |
|
517 | + * @param string|array $value Meta value. |
|
518 | + * @param int $meta_id Meta ID. |
|
519 | + */ |
|
520 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
521 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
522 | + $function = 'set_' . $key; |
|
523 | + |
|
524 | + if ( is_callable( array( $this, $function ) ) ) { |
|
525 | + return $this->{$function}( $value ); |
|
526 | + } |
|
527 | + } |
|
528 | + |
|
529 | + $this->maybe_read_meta_data(); |
|
530 | + |
|
531 | + $array_key = false; |
|
532 | + |
|
533 | + if ( $meta_id ) { |
|
534 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
535 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
536 | + } else { |
|
537 | + // Find matches by key. |
|
538 | + $matches = array(); |
|
539 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
540 | + if ( $meta->key === $key ) { |
|
541 | + $matches[] = $meta_data_array_key; |
|
542 | + } |
|
543 | + } |
|
544 | + |
|
545 | + if ( ! empty( $matches ) ) { |
|
546 | + // Set matches to null so only one key gets the new value. |
|
547 | + foreach ( $matches as $meta_data_array_key ) { |
|
548 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
549 | + } |
|
550 | + $array_key = current( $matches ); |
|
551 | + } |
|
552 | + } |
|
553 | + |
|
554 | + if ( false !== $array_key ) { |
|
555 | + $meta = $this->meta_data[ $array_key ]; |
|
556 | + $meta->key = $key; |
|
557 | + $meta->value = $value; |
|
558 | + } else { |
|
559 | + $this->add_meta_data( $key, $value, true ); |
|
560 | + } |
|
561 | + } |
|
562 | + |
|
563 | + /** |
|
564 | + * Delete meta data. |
|
565 | + * |
|
566 | + * @since 1.0.19 |
|
567 | + * @param string $key Meta key. |
|
568 | + */ |
|
569 | + public function delete_meta_data( $key ) { |
|
570 | + $this->maybe_read_meta_data(); |
|
571 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
572 | + |
|
573 | + if ( $array_keys ) { |
|
574 | + foreach ( $array_keys as $array_key ) { |
|
575 | + $this->meta_data[ $array_key ]->value = null; |
|
576 | + } |
|
577 | + } |
|
578 | + } |
|
579 | + |
|
580 | + /** |
|
581 | + * Delete meta data. |
|
582 | + * |
|
583 | + * @since 1.0.19 |
|
584 | + * @param int $mid Meta ID. |
|
585 | + */ |
|
586 | + public function delete_meta_data_by_mid( $mid ) { |
|
587 | + $this->maybe_read_meta_data(); |
|
588 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
589 | + |
|
590 | + if ( $array_keys ) { |
|
591 | + foreach ( $array_keys as $array_key ) { |
|
592 | + $this->meta_data[ $array_key ]->value = null; |
|
593 | + } |
|
594 | + } |
|
595 | + } |
|
596 | + |
|
597 | + /** |
|
598 | + * Read meta data if null. |
|
599 | + * |
|
600 | + * @since 1.0.19 |
|
601 | + */ |
|
602 | + protected function maybe_read_meta_data() { |
|
603 | + if ( is_null( $this->meta_data ) ) { |
|
604 | + $this->read_meta_data(); |
|
605 | + } |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * Read Meta Data from the database. Ignore any internal properties. |
|
610 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
611 | + * |
|
612 | + * @since 1.0.19 |
|
613 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
614 | + */ |
|
615 | + public function read_meta_data( $force_read = false ) { |
|
616 | + |
|
617 | + // Reset meta data. |
|
618 | + $this->meta_data = array(); |
|
619 | + |
|
620 | + // Maybe abort early. |
|
621 | + if ( ! $this->get_id() || ! $this->data_store ) { |
|
622 | + return; |
|
623 | + } |
|
624 | + |
|
625 | + // Only read from cache if the cache key is set. |
|
626 | + $cache_key = null; |
|
627 | + if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
628 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
629 | + $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
630 | + } |
|
631 | + |
|
632 | + // Should we force read? |
|
633 | + if ( empty( $raw_meta_data ) ) { |
|
634 | + $raw_meta_data = $this->data_store->read_meta( $this ); |
|
635 | + |
|
636 | + if ( ! empty( $cache_key ) ) { |
|
637 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
638 | + } |
|
639 | + |
|
640 | + } |
|
641 | + |
|
642 | + // Set meta data. |
|
643 | + if ( is_array( $raw_meta_data ) ) { |
|
644 | + |
|
645 | + foreach ( $raw_meta_data as $meta ) { |
|
646 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
647 | + array( |
|
648 | + 'id' => (int) $meta->meta_id, |
|
649 | + 'key' => $meta->meta_key, |
|
650 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
651 | + ) |
|
652 | + ); |
|
653 | + } |
|
654 | + |
|
655 | + } |
|
656 | + |
|
657 | + } |
|
658 | + |
|
659 | + /** |
|
660 | + * Update Meta Data in the database. |
|
661 | + * |
|
662 | + * @since 1.0.19 |
|
663 | + */ |
|
664 | + public function save_meta_data() { |
|
665 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
666 | + return; |
|
667 | + } |
|
668 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
669 | + if ( is_null( $meta->value ) ) { |
|
670 | + if ( ! empty( $meta->id ) ) { |
|
671 | + $this->data_store->delete_meta( $this, $meta ); |
|
672 | + unset( $this->meta_data[ $array_key ] ); |
|
673 | + } |
|
674 | + } elseif ( empty( $meta->id ) ) { |
|
675 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
676 | + $meta->apply_changes(); |
|
677 | + } else { |
|
678 | + if ( $meta->get_changes() ) { |
|
679 | + $this->data_store->update_meta( $this, $meta ); |
|
680 | + $meta->apply_changes(); |
|
681 | + } |
|
682 | + } |
|
683 | + } |
|
684 | + if ( ! empty( $this->cache_group ) ) { |
|
685 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
686 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * Set ID. |
|
692 | + * |
|
693 | + * @since 1.0.19 |
|
694 | + * @param int $id ID. |
|
695 | + */ |
|
696 | + public function set_id( $id ) { |
|
697 | + $this->id = absint( $id ); |
|
698 | + } |
|
699 | + |
|
700 | + /** |
|
701 | + * Sets item status. |
|
702 | + * |
|
703 | + * @since 1.0.19 |
|
704 | + * @param string $status New status. |
|
705 | + * @return array details of change. |
|
706 | + */ |
|
707 | + public function set_status( $status ) { |
|
708 | 708 | $old_status = $this->get_status(); |
709 | 709 | |
710 | - $this->set_prop( 'status', $status ); |
|
711 | - |
|
712 | - return array( |
|
713 | - 'from' => $old_status, |
|
714 | - 'to' => $status, |
|
715 | - ); |
|
716 | - } |
|
717 | - |
|
718 | - /** |
|
719 | - * Set all props to default values. |
|
720 | - * |
|
721 | - * @since 1.0.19 |
|
722 | - */ |
|
723 | - public function set_defaults() { |
|
724 | - $this->data = $this->default_data; |
|
725 | - $this->changes = array(); |
|
726 | - $this->set_object_read( false ); |
|
727 | - } |
|
728 | - |
|
729 | - /** |
|
730 | - * Set object read property. |
|
731 | - * |
|
732 | - * @since 1.0.19 |
|
733 | - * @param boolean $read Should read?. |
|
734 | - */ |
|
735 | - public function set_object_read( $read = true ) { |
|
736 | - $this->object_read = (bool) $read; |
|
737 | - } |
|
738 | - |
|
739 | - /** |
|
740 | - * Get object read property. |
|
741 | - * |
|
742 | - * @since 1.0.19 |
|
743 | - * @return boolean |
|
744 | - */ |
|
745 | - public function get_object_read() { |
|
746 | - return (bool) $this->object_read; |
|
747 | - } |
|
748 | - |
|
749 | - /** |
|
750 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
751 | - * Only sets using public methods. |
|
752 | - * |
|
753 | - * @since 1.0.19 |
|
754 | - * |
|
755 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
756 | - * @param string $context In what context to run this. |
|
757 | - * |
|
758 | - * @return bool|WP_Error |
|
759 | - */ |
|
760 | - public function set_props( $props, $context = 'set' ) { |
|
761 | - $errors = false; |
|
762 | - |
|
763 | - $props = wp_unslash( $props ); |
|
764 | - foreach ( $props as $prop => $value ) { |
|
765 | - try { |
|
766 | - /** |
|
767 | - * Checks if the prop being set is allowed, and the value is not null. |
|
768 | - */ |
|
769 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
770 | - continue; |
|
771 | - } |
|
772 | - $setter = "set_$prop"; |
|
773 | - |
|
774 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
775 | - $this->{$setter}( $value ); |
|
776 | - } |
|
777 | - } catch ( Exception $e ) { |
|
778 | - if ( ! $errors ) { |
|
779 | - $errors = new WP_Error(); |
|
780 | - } |
|
781 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
782 | - $this->last_error = $e->getMessage(); |
|
783 | - } |
|
784 | - } |
|
785 | - |
|
786 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
787 | - } |
|
788 | - |
|
789 | - /** |
|
790 | - * Sets a prop for a setter method. |
|
791 | - * |
|
792 | - * This stores changes in a special array so we can track what needs saving |
|
793 | - * the the DB later. |
|
794 | - * |
|
795 | - * @since 1.0.19 |
|
796 | - * @param string $prop Name of prop to set. |
|
797 | - * @param mixed $value Value of the prop. |
|
798 | - */ |
|
799 | - protected function set_prop( $prop, $value ) { |
|
800 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
801 | - if ( true === $this->object_read ) { |
|
802 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
803 | - $this->changes[ $prop ] = $value; |
|
804 | - } |
|
805 | - } else { |
|
806 | - $this->data[ $prop ] = $value; |
|
807 | - } |
|
808 | - } |
|
809 | - } |
|
810 | - |
|
811 | - /** |
|
812 | - * Return data changes only. |
|
813 | - * |
|
814 | - * @since 1.0.19 |
|
815 | - * @return array |
|
816 | - */ |
|
817 | - public function get_changes() { |
|
818 | - return $this->changes; |
|
819 | - } |
|
820 | - |
|
821 | - /** |
|
822 | - * Merge changes with data and clear. |
|
823 | - * |
|
824 | - * @since 1.0.19 |
|
825 | - */ |
|
826 | - public function apply_changes() { |
|
827 | - $this->data = array_replace( $this->data, $this->changes ); |
|
828 | - $this->changes = array(); |
|
829 | - } |
|
830 | - |
|
831 | - /** |
|
832 | - * Prefix for action and filter hooks on data. |
|
833 | - * |
|
834 | - * @since 1.0.19 |
|
835 | - * @return string |
|
836 | - */ |
|
837 | - protected function get_hook_prefix() { |
|
838 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
839 | - } |
|
840 | - |
|
841 | - /** |
|
842 | - * Gets a prop for a getter method. |
|
843 | - * |
|
844 | - * Gets the value from either current pending changes, or the data itself. |
|
845 | - * Context controls what happens to the value before it's returned. |
|
846 | - * |
|
847 | - * @since 1.0.19 |
|
848 | - * @param string $prop Name of prop to get. |
|
849 | - * @param string $context What the value is for. Valid values are view and edit. |
|
850 | - * @return mixed |
|
851 | - */ |
|
852 | - protected function get_prop( $prop, $context = 'view' ) { |
|
853 | - $value = null; |
|
854 | - |
|
855 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
856 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
857 | - |
|
858 | - if ( 'view' === $context ) { |
|
859 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
860 | - } |
|
861 | - } |
|
862 | - |
|
863 | - return $value; |
|
864 | - } |
|
865 | - |
|
866 | - /** |
|
867 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
868 | - * |
|
869 | - * @since 1.0.19 |
|
870 | - * @param string $prop Name of prop to set. |
|
871 | - * @param string|integer $value Value of the prop. |
|
872 | - */ |
|
873 | - protected function set_date_prop( $prop, $value ) { |
|
874 | - |
|
875 | - if ( empty( $value ) ) { |
|
876 | - $this->set_prop( $prop, null ); |
|
877 | - return; |
|
878 | - } |
|
879 | - $this->set_prop( $prop, $value ); |
|
880 | - |
|
881 | - } |
|
882 | - |
|
883 | - /** |
|
884 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
885 | - * |
|
886 | - * @since 1.0.19 |
|
887 | - * @param string $code Error code. |
|
888 | - * @param string $message Error message. |
|
889 | - */ |
|
890 | - protected function error( $code, $message ) { |
|
891 | - $this->last_error = $message; |
|
892 | - } |
|
893 | - |
|
894 | - /** |
|
895 | - * Checks if the object is saved in the database |
|
896 | - * |
|
897 | - * @since 1.0.19 |
|
898 | - * @return bool |
|
899 | - */ |
|
900 | - public function exists() { |
|
901 | - $id = $this->get_id(); |
|
902 | - return ! empty( $id ); |
|
903 | - } |
|
710 | + $this->set_prop( 'status', $status ); |
|
711 | + |
|
712 | + return array( |
|
713 | + 'from' => $old_status, |
|
714 | + 'to' => $status, |
|
715 | + ); |
|
716 | + } |
|
717 | + |
|
718 | + /** |
|
719 | + * Set all props to default values. |
|
720 | + * |
|
721 | + * @since 1.0.19 |
|
722 | + */ |
|
723 | + public function set_defaults() { |
|
724 | + $this->data = $this->default_data; |
|
725 | + $this->changes = array(); |
|
726 | + $this->set_object_read( false ); |
|
727 | + } |
|
728 | + |
|
729 | + /** |
|
730 | + * Set object read property. |
|
731 | + * |
|
732 | + * @since 1.0.19 |
|
733 | + * @param boolean $read Should read?. |
|
734 | + */ |
|
735 | + public function set_object_read( $read = true ) { |
|
736 | + $this->object_read = (bool) $read; |
|
737 | + } |
|
738 | + |
|
739 | + /** |
|
740 | + * Get object read property. |
|
741 | + * |
|
742 | + * @since 1.0.19 |
|
743 | + * @return boolean |
|
744 | + */ |
|
745 | + public function get_object_read() { |
|
746 | + return (bool) $this->object_read; |
|
747 | + } |
|
748 | + |
|
749 | + /** |
|
750 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
751 | + * Only sets using public methods. |
|
752 | + * |
|
753 | + * @since 1.0.19 |
|
754 | + * |
|
755 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
756 | + * @param string $context In what context to run this. |
|
757 | + * |
|
758 | + * @return bool|WP_Error |
|
759 | + */ |
|
760 | + public function set_props( $props, $context = 'set' ) { |
|
761 | + $errors = false; |
|
762 | + |
|
763 | + $props = wp_unslash( $props ); |
|
764 | + foreach ( $props as $prop => $value ) { |
|
765 | + try { |
|
766 | + /** |
|
767 | + * Checks if the prop being set is allowed, and the value is not null. |
|
768 | + */ |
|
769 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
770 | + continue; |
|
771 | + } |
|
772 | + $setter = "set_$prop"; |
|
773 | + |
|
774 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
775 | + $this->{$setter}( $value ); |
|
776 | + } |
|
777 | + } catch ( Exception $e ) { |
|
778 | + if ( ! $errors ) { |
|
779 | + $errors = new WP_Error(); |
|
780 | + } |
|
781 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
782 | + $this->last_error = $e->getMessage(); |
|
783 | + } |
|
784 | + } |
|
785 | + |
|
786 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
787 | + } |
|
788 | + |
|
789 | + /** |
|
790 | + * Sets a prop for a setter method. |
|
791 | + * |
|
792 | + * This stores changes in a special array so we can track what needs saving |
|
793 | + * the the DB later. |
|
794 | + * |
|
795 | + * @since 1.0.19 |
|
796 | + * @param string $prop Name of prop to set. |
|
797 | + * @param mixed $value Value of the prop. |
|
798 | + */ |
|
799 | + protected function set_prop( $prop, $value ) { |
|
800 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
801 | + if ( true === $this->object_read ) { |
|
802 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
803 | + $this->changes[ $prop ] = $value; |
|
804 | + } |
|
805 | + } else { |
|
806 | + $this->data[ $prop ] = $value; |
|
807 | + } |
|
808 | + } |
|
809 | + } |
|
810 | + |
|
811 | + /** |
|
812 | + * Return data changes only. |
|
813 | + * |
|
814 | + * @since 1.0.19 |
|
815 | + * @return array |
|
816 | + */ |
|
817 | + public function get_changes() { |
|
818 | + return $this->changes; |
|
819 | + } |
|
820 | + |
|
821 | + /** |
|
822 | + * Merge changes with data and clear. |
|
823 | + * |
|
824 | + * @since 1.0.19 |
|
825 | + */ |
|
826 | + public function apply_changes() { |
|
827 | + $this->data = array_replace( $this->data, $this->changes ); |
|
828 | + $this->changes = array(); |
|
829 | + } |
|
830 | + |
|
831 | + /** |
|
832 | + * Prefix for action and filter hooks on data. |
|
833 | + * |
|
834 | + * @since 1.0.19 |
|
835 | + * @return string |
|
836 | + */ |
|
837 | + protected function get_hook_prefix() { |
|
838 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
839 | + } |
|
840 | + |
|
841 | + /** |
|
842 | + * Gets a prop for a getter method. |
|
843 | + * |
|
844 | + * Gets the value from either current pending changes, or the data itself. |
|
845 | + * Context controls what happens to the value before it's returned. |
|
846 | + * |
|
847 | + * @since 1.0.19 |
|
848 | + * @param string $prop Name of prop to get. |
|
849 | + * @param string $context What the value is for. Valid values are view and edit. |
|
850 | + * @return mixed |
|
851 | + */ |
|
852 | + protected function get_prop( $prop, $context = 'view' ) { |
|
853 | + $value = null; |
|
854 | + |
|
855 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
856 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
857 | + |
|
858 | + if ( 'view' === $context ) { |
|
859 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
860 | + } |
|
861 | + } |
|
862 | + |
|
863 | + return $value; |
|
864 | + } |
|
865 | + |
|
866 | + /** |
|
867 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
868 | + * |
|
869 | + * @since 1.0.19 |
|
870 | + * @param string $prop Name of prop to set. |
|
871 | + * @param string|integer $value Value of the prop. |
|
872 | + */ |
|
873 | + protected function set_date_prop( $prop, $value ) { |
|
874 | + |
|
875 | + if ( empty( $value ) ) { |
|
876 | + $this->set_prop( $prop, null ); |
|
877 | + return; |
|
878 | + } |
|
879 | + $this->set_prop( $prop, $value ); |
|
880 | + |
|
881 | + } |
|
882 | + |
|
883 | + /** |
|
884 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
885 | + * |
|
886 | + * @since 1.0.19 |
|
887 | + * @param string $code Error code. |
|
888 | + * @param string $message Error message. |
|
889 | + */ |
|
890 | + protected function error( $code, $message ) { |
|
891 | + $this->last_error = $message; |
|
892 | + } |
|
893 | + |
|
894 | + /** |
|
895 | + * Checks if the object is saved in the database |
|
896 | + * |
|
897 | + * @since 1.0.19 |
|
898 | + * @return bool |
|
899 | + */ |
|
900 | + public function exists() { |
|
901 | + $id = $this->get_id(); |
|
902 | + return ! empty( $id ); |
|
903 | + } |
|
904 | 904 | |
905 | 905 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * |
9 | 9 | */ |
10 | 10 | |
11 | -if ( ! defined( 'ABSPATH' ) ) { |
|
11 | +if (!defined('ABSPATH')) { |
|
12 | 12 | exit; |
13 | 13 | } |
14 | 14 | |
@@ -117,8 +117,8 @@ discard block |
||
117 | 117 | * |
118 | 118 | * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
119 | 119 | */ |
120 | - public function __construct( $read = 0 ) { |
|
121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
120 | + public function __construct($read = 0) { |
|
121 | + $this->data = array_merge($this->data, $this->extra_data); |
|
122 | 122 | $this->default_data = $this->data; |
123 | 123 | } |
124 | 124 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | * @return array |
129 | 129 | */ |
130 | 130 | public function __sleep() { |
131 | - return array( 'id' ); |
|
131 | + return array('id'); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | /** |
@@ -137,10 +137,10 @@ discard block |
||
137 | 137 | * If the object no longer exists, remove the ID. |
138 | 138 | */ |
139 | 139 | public function __wakeup() { |
140 | - $this->__construct( absint( $this->id ) ); |
|
140 | + $this->__construct(absint($this->id)); |
|
141 | 141 | |
142 | - if ( ! empty( $this->last_error ) ) { |
|
143 | - $this->set_id( 0 ); |
|
142 | + if (!empty($this->last_error)) { |
|
143 | + $this->set_id(0); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | } |
@@ -152,11 +152,11 @@ discard block |
||
152 | 152 | */ |
153 | 153 | public function __clone() { |
154 | 154 | $this->maybe_read_meta_data(); |
155 | - if ( ! empty( $this->meta_data ) ) { |
|
156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
158 | - if ( ! empty( $meta->id ) ) { |
|
159 | - $this->meta_data[ $array_key ]->id = null; |
|
155 | + if (!empty($this->meta_data)) { |
|
156 | + foreach ($this->meta_data as $array_key => $meta) { |
|
157 | + $this->meta_data[$array_key] = clone $meta; |
|
158 | + if (!empty($meta->id)) { |
|
159 | + $this->meta_data[$array_key]->id = null; |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | } |
@@ -199,8 +199,8 @@ discard block |
||
199 | 199 | * @param string $context View or edit context. |
200 | 200 | * @return string |
201 | 201 | */ |
202 | - public function get_status( $context = 'view' ) { |
|
203 | - return $this->get_prop( 'status', $context ); |
|
202 | + public function get_status($context = 'view') { |
|
203 | + return $this->get_prop('status', $context); |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | /** |
@@ -210,10 +210,10 @@ discard block |
||
210 | 210 | * @param bool $force_delete Should the data be deleted permanently. |
211 | 211 | * @return bool result |
212 | 212 | */ |
213 | - public function delete( $force_delete = false ) { |
|
214 | - if ( $this->data_store && $this->exists() ) { |
|
215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
216 | - $this->set_id( 0 ); |
|
213 | + public function delete($force_delete = false) { |
|
214 | + if ($this->data_store && $this->exists()) { |
|
215 | + $this->data_store->delete($this, array('force_delete' => $force_delete)); |
|
216 | + $this->set_id(0); |
|
217 | 217 | return true; |
218 | 218 | } |
219 | 219 | return false; |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | * @return int |
227 | 227 | */ |
228 | 228 | public function save() { |
229 | - if ( ! $this->data_store ) { |
|
229 | + if (!$this->data_store) { |
|
230 | 230 | return $this->get_id(); |
231 | 231 | } |
232 | 232 | |
@@ -236,12 +236,12 @@ discard block |
||
236 | 236 | * @param GetPaid_Data $this The object being saved. |
237 | 237 | * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
238 | 238 | */ |
239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
239 | + do_action('getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store); |
|
240 | 240 | |
241 | - if ( $this->get_id() ) { |
|
242 | - $this->data_store->update( $this ); |
|
241 | + if ($this->get_id()) { |
|
242 | + $this->data_store->update($this); |
|
243 | 243 | } else { |
244 | - $this->data_store->create( $this ); |
|
244 | + $this->data_store->create($this); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | /** |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | * @param GetPaid_Data $this The object being saved. |
251 | 251 | * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
252 | 252 | */ |
253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
253 | + do_action('getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store); |
|
254 | 254 | |
255 | 255 | return $this->get_id(); |
256 | 256 | } |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | * @return string Data in JSON format. |
263 | 263 | */ |
264 | 264 | public function __toString() { |
265 | - return wp_json_encode( $this->get_data() ); |
|
265 | + return wp_json_encode($this->get_data()); |
|
266 | 266 | } |
267 | 267 | |
268 | 268 | /** |
@@ -272,7 +272,7 @@ discard block |
||
272 | 272 | * @return array |
273 | 273 | */ |
274 | 274 | public function get_data() { |
275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
275 | + return array_merge(array('id' => $this->get_id()), $this->data, array('meta_data' => $this->get_meta_data())); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | /** |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | * @return array |
283 | 283 | */ |
284 | 284 | public function get_data_keys() { |
285 | - return array_keys( $this->data ); |
|
285 | + return array_keys($this->data); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | /** |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | * @return array |
293 | 293 | */ |
294 | 294 | public function get_extra_data_keys() { |
295 | - return array_keys( $this->extra_data ); |
|
295 | + return array_keys($this->extra_data); |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | /** |
@@ -302,8 +302,8 @@ discard block |
||
302 | 302 | * @param mixed $meta Meta value to check. |
303 | 303 | * @return bool |
304 | 304 | */ |
305 | - protected function filter_null_meta( $meta ) { |
|
306 | - return ! is_null( $meta->value ); |
|
305 | + protected function filter_null_meta($meta) { |
|
306 | + return !is_null($meta->value); |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | /** |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | */ |
315 | 315 | public function get_meta_data() { |
316 | 316 | $this->maybe_read_meta_data(); |
317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
317 | + return array_values(array_filter($this->meta_data, array($this, 'filter_null_meta'))); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | /** |
@@ -324,21 +324,21 @@ discard block |
||
324 | 324 | * @param string $key Key to check. |
325 | 325 | * @return bool true if it's an internal key, false otherwise |
326 | 326 | */ |
327 | - protected function is_internal_meta_key( $key ) { |
|
328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
327 | + protected function is_internal_meta_key($key) { |
|
328 | + $internal_meta_key = !empty($key) && $this->data_store && in_array($key, $this->data_store->get_internal_meta_keys(), true); |
|
329 | 329 | |
330 | - if ( ! $internal_meta_key ) { |
|
330 | + if (!$internal_meta_key) { |
|
331 | 331 | return false; |
332 | 332 | } |
333 | 333 | |
334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
334 | + $has_setter_or_getter = is_callable(array($this, 'set_' . $key)) || is_callable(array($this, 'get_' . $key)); |
|
335 | 335 | |
336 | - if ( ! $has_setter_or_getter ) { |
|
336 | + if (!$has_setter_or_getter) { |
|
337 | 337 | return false; |
338 | 338 | } |
339 | 339 | |
340 | 340 | /* translators: %s: $key Key to check */ |
341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
341 | + getpaid_doing_it_wrong(__FUNCTION__, sprintf(__('Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing'), $key), '1.0.19'); |
|
342 | 342 | |
343 | 343 | return true; |
344 | 344 | } |
@@ -352,20 +352,20 @@ discard block |
||
352 | 352 | * @access public |
353 | 353 | * |
354 | 354 | */ |
355 | - public function __set( $key, $value ) { |
|
355 | + public function __set($key, $value) { |
|
356 | 356 | |
357 | - if ( 'id' == strtolower( $key ) ) { |
|
358 | - return $this->set_id( $value ); |
|
357 | + if ('id' == strtolower($key)) { |
|
358 | + return $this->set_id($value); |
|
359 | 359 | } |
360 | 360 | |
361 | - if ( method_exists( $this, "set_$key") ) { |
|
361 | + if (method_exists($this, "set_$key")) { |
|
362 | 362 | |
363 | 363 | /* translators: %s: $key Key to set */ |
364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
364 | + getpaid_doing_it_wrong(__FUNCTION__, sprintf(__('Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing'), $key), '1.0.19'); |
|
365 | 365 | |
366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
366 | + call_user_func(array($this, "set_$key"), $value); |
|
367 | 367 | } else { |
368 | - $this->set_prop( $key, $value ); |
|
368 | + $this->set_prop($key, $value); |
|
369 | 369 | } |
370 | 370 | |
371 | 371 | } |
@@ -373,25 +373,25 @@ discard block |
||
373 | 373 | /** |
374 | 374 | * Margic method for retrieving a property. |
375 | 375 | */ |
376 | - public function __get( $key ) { |
|
376 | + public function __get($key) { |
|
377 | 377 | |
378 | 378 | // Check if we have a helper method for that. |
379 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
379 | + if (method_exists($this, 'get_' . $key)) { |
|
380 | 380 | |
381 | - if ( 'post_type' != $key ) { |
|
381 | + if ('post_type' != $key) { |
|
382 | 382 | /* translators: %s: $key Key to set */ |
383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
383 | + getpaid_doing_it_wrong(__FUNCTION__, sprintf(__('Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing'), $key), '1.0.19'); |
|
384 | 384 | } |
385 | 385 | |
386 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
386 | + return call_user_func(array($this, 'get_' . $key)); |
|
387 | 387 | } |
388 | 388 | |
389 | 389 | // Check if the key is in the associated $post object. |
390 | - if ( ! empty( $this->post ) && isset( $this->post->$key ) ) { |
|
390 | + if (!empty($this->post) && isset($this->post->$key)) { |
|
391 | 391 | return $this->post->$key; |
392 | 392 | } |
393 | 393 | |
394 | - return $this->get_prop( $key ); |
|
394 | + return $this->get_prop($key); |
|
395 | 395 | |
396 | 396 | } |
397 | 397 | |
@@ -404,15 +404,15 @@ discard block |
||
404 | 404 | * @param string $context What the value is for. Valid values are view and edit. |
405 | 405 | * @return mixed |
406 | 406 | */ |
407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
407 | + public function get_meta($key = '', $single = true, $context = 'view') { |
|
408 | 408 | |
409 | 409 | // Check if this is an internal meta key. |
410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
412 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
410 | + $_key = str_replace('_wpinv', '', $key); |
|
411 | + $_key = str_replace('wpinv', '', $_key); |
|
412 | + if ($this->is_internal_meta_key($key)) { |
|
413 | 413 | $function = 'get_' . $_key; |
414 | 414 | |
415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
415 | + if (is_callable(array($this, $function))) { |
|
416 | 416 | return $this->{$function}(); |
417 | 417 | } |
418 | 418 | } |
@@ -420,20 +420,20 @@ discard block |
||
420 | 420 | // Read the meta data if not yet read. |
421 | 421 | $this->maybe_read_meta_data(); |
422 | 422 | $meta_data = $this->get_meta_data(); |
423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
423 | + $array_keys = array_keys(wp_list_pluck($meta_data, 'key'), $key, true); |
|
424 | 424 | $value = $single ? '' : array(); |
425 | 425 | |
426 | - if ( ! empty( $array_keys ) ) { |
|
426 | + if (!empty($array_keys)) { |
|
427 | 427 | // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
428 | - if ( $single ) { |
|
429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
428 | + if ($single) { |
|
429 | + $value = $meta_data[current($array_keys)]->value; |
|
430 | 430 | } else { |
431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
431 | + $value = array_intersect_key($meta_data, array_flip($array_keys)); |
|
432 | 432 | } |
433 | 433 | } |
434 | 434 | |
435 | - if ( 'view' === $context ) { |
|
436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
435 | + if ('view' === $context) { |
|
436 | + $value = apply_filters($this->get_hook_prefix() . $key, $value, $this); |
|
437 | 437 | } |
438 | 438 | |
439 | 439 | return $value; |
@@ -446,10 +446,10 @@ discard block |
||
446 | 446 | * @param string $key Meta Key. |
447 | 447 | * @return boolean |
448 | 448 | */ |
449 | - public function meta_exists( $key = '' ) { |
|
449 | + public function meta_exists($key = '') { |
|
450 | 450 | $this->maybe_read_meta_data(); |
451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
452 | - return in_array( $key, $array_keys, true ); |
|
451 | + $array_keys = wp_list_pluck($this->get_meta_data(), 'key'); |
|
452 | + return in_array($key, $array_keys, true); |
|
453 | 453 | } |
454 | 454 | |
455 | 455 | /** |
@@ -458,12 +458,12 @@ discard block |
||
458 | 458 | * @since 1.0.19 |
459 | 459 | * @param array $data Key/Value pairs. |
460 | 460 | */ |
461 | - public function set_meta_data( $data ) { |
|
462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
461 | + public function set_meta_data($data) { |
|
462 | + if (!empty($data) && is_array($data)) { |
|
463 | 463 | $this->maybe_read_meta_data(); |
464 | - foreach ( $data as $meta ) { |
|
464 | + foreach ($data as $meta) { |
|
465 | 465 | $meta = (array) $meta; |
466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
466 | + if (isset($meta['key'], $meta['value'], $meta['id'])) { |
|
467 | 467 | $this->meta_data[] = new GetPaid_Meta_Data( |
468 | 468 | array( |
469 | 469 | 'id' => $meta['id'], |
@@ -485,18 +485,18 @@ discard block |
||
485 | 485 | * @param string|array $value Meta value. |
486 | 486 | * @param bool $unique Should this be a unique key?. |
487 | 487 | */ |
488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
488 | + public function add_meta_data($key, $value, $unique = false) { |
|
489 | + if ($this->is_internal_meta_key($key)) { |
|
490 | 490 | $function = 'set_' . $key; |
491 | 491 | |
492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
493 | - return $this->{$function}( $value ); |
|
492 | + if (is_callable(array($this, $function))) { |
|
493 | + return $this->{$function}($value); |
|
494 | 494 | } |
495 | 495 | } |
496 | 496 | |
497 | 497 | $this->maybe_read_meta_data(); |
498 | - if ( $unique ) { |
|
499 | - $this->delete_meta_data( $key ); |
|
498 | + if ($unique) { |
|
499 | + $this->delete_meta_data($key); |
|
500 | 500 | } |
501 | 501 | $this->meta_data[] = new GetPaid_Meta_Data( |
502 | 502 | array( |
@@ -517,12 +517,12 @@ discard block |
||
517 | 517 | * @param string|array $value Meta value. |
518 | 518 | * @param int $meta_id Meta ID. |
519 | 519 | */ |
520 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
521 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
520 | + public function update_meta_data($key, $value, $meta_id = 0) { |
|
521 | + if ($this->is_internal_meta_key($key)) { |
|
522 | 522 | $function = 'set_' . $key; |
523 | 523 | |
524 | - if ( is_callable( array( $this, $function ) ) ) { |
|
525 | - return $this->{$function}( $value ); |
|
524 | + if (is_callable(array($this, $function))) { |
|
525 | + return $this->{$function}($value); |
|
526 | 526 | } |
527 | 527 | } |
528 | 528 | |
@@ -530,33 +530,33 @@ discard block |
||
530 | 530 | |
531 | 531 | $array_key = false; |
532 | 532 | |
533 | - if ( $meta_id ) { |
|
534 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
535 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
533 | + if ($meta_id) { |
|
534 | + $array_keys = array_keys(wp_list_pluck($this->meta_data, 'id'), $meta_id, true); |
|
535 | + $array_key = $array_keys ? current($array_keys) : false; |
|
536 | 536 | } else { |
537 | 537 | // Find matches by key. |
538 | 538 | $matches = array(); |
539 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
540 | - if ( $meta->key === $key ) { |
|
539 | + foreach ($this->meta_data as $meta_data_array_key => $meta) { |
|
540 | + if ($meta->key === $key) { |
|
541 | 541 | $matches[] = $meta_data_array_key; |
542 | 542 | } |
543 | 543 | } |
544 | 544 | |
545 | - if ( ! empty( $matches ) ) { |
|
545 | + if (!empty($matches)) { |
|
546 | 546 | // Set matches to null so only one key gets the new value. |
547 | - foreach ( $matches as $meta_data_array_key ) { |
|
548 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
547 | + foreach ($matches as $meta_data_array_key) { |
|
548 | + $this->meta_data[$meta_data_array_key]->value = null; |
|
549 | 549 | } |
550 | - $array_key = current( $matches ); |
|
550 | + $array_key = current($matches); |
|
551 | 551 | } |
552 | 552 | } |
553 | 553 | |
554 | - if ( false !== $array_key ) { |
|
555 | - $meta = $this->meta_data[ $array_key ]; |
|
554 | + if (false !== $array_key) { |
|
555 | + $meta = $this->meta_data[$array_key]; |
|
556 | 556 | $meta->key = $key; |
557 | 557 | $meta->value = $value; |
558 | 558 | } else { |
559 | - $this->add_meta_data( $key, $value, true ); |
|
559 | + $this->add_meta_data($key, $value, true); |
|
560 | 560 | } |
561 | 561 | } |
562 | 562 | |
@@ -566,13 +566,13 @@ discard block |
||
566 | 566 | * @since 1.0.19 |
567 | 567 | * @param string $key Meta key. |
568 | 568 | */ |
569 | - public function delete_meta_data( $key ) { |
|
569 | + public function delete_meta_data($key) { |
|
570 | 570 | $this->maybe_read_meta_data(); |
571 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
571 | + $array_keys = array_keys(wp_list_pluck($this->meta_data, 'key'), $key, true); |
|
572 | 572 | |
573 | - if ( $array_keys ) { |
|
574 | - foreach ( $array_keys as $array_key ) { |
|
575 | - $this->meta_data[ $array_key ]->value = null; |
|
573 | + if ($array_keys) { |
|
574 | + foreach ($array_keys as $array_key) { |
|
575 | + $this->meta_data[$array_key]->value = null; |
|
576 | 576 | } |
577 | 577 | } |
578 | 578 | } |
@@ -583,13 +583,13 @@ discard block |
||
583 | 583 | * @since 1.0.19 |
584 | 584 | * @param int $mid Meta ID. |
585 | 585 | */ |
586 | - public function delete_meta_data_by_mid( $mid ) { |
|
586 | + public function delete_meta_data_by_mid($mid) { |
|
587 | 587 | $this->maybe_read_meta_data(); |
588 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
588 | + $array_keys = array_keys(wp_list_pluck($this->meta_data, 'id'), (int) $mid, true); |
|
589 | 589 | |
590 | - if ( $array_keys ) { |
|
591 | - foreach ( $array_keys as $array_key ) { |
|
592 | - $this->meta_data[ $array_key ]->value = null; |
|
590 | + if ($array_keys) { |
|
591 | + foreach ($array_keys as $array_key) { |
|
592 | + $this->meta_data[$array_key]->value = null; |
|
593 | 593 | } |
594 | 594 | } |
595 | 595 | } |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | * @since 1.0.19 |
601 | 601 | */ |
602 | 602 | protected function maybe_read_meta_data() { |
603 | - if ( is_null( $this->meta_data ) ) { |
|
603 | + if (is_null($this->meta_data)) { |
|
604 | 604 | $this->read_meta_data(); |
605 | 605 | } |
606 | 606 | } |
@@ -612,42 +612,42 @@ discard block |
||
612 | 612 | * @since 1.0.19 |
613 | 613 | * @param bool $force_read True to force a new DB read (and update cache). |
614 | 614 | */ |
615 | - public function read_meta_data( $force_read = false ) { |
|
615 | + public function read_meta_data($force_read = false) { |
|
616 | 616 | |
617 | 617 | // Reset meta data. |
618 | 618 | $this->meta_data = array(); |
619 | 619 | |
620 | 620 | // Maybe abort early. |
621 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
621 | + if (!$this->get_id() || !$this->data_store) { |
|
622 | 622 | return; |
623 | 623 | } |
624 | 624 | |
625 | 625 | // Only read from cache if the cache key is set. |
626 | 626 | $cache_key = null; |
627 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
628 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
629 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
627 | + if (!$force_read && !empty($this->cache_group)) { |
|
628 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix($this->cache_group) . GetPaid_Cache_Helper::get_cache_prefix('object_' . $this->get_id()) . 'object_meta_' . $this->get_id(); |
|
629 | + $raw_meta_data = wp_cache_get($cache_key, $this->cache_group); |
|
630 | 630 | } |
631 | 631 | |
632 | 632 | // Should we force read? |
633 | - if ( empty( $raw_meta_data ) ) { |
|
634 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
633 | + if (empty($raw_meta_data)) { |
|
634 | + $raw_meta_data = $this->data_store->read_meta($this); |
|
635 | 635 | |
636 | - if ( ! empty( $cache_key ) ) { |
|
637 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
636 | + if (!empty($cache_key)) { |
|
637 | + wp_cache_set($cache_key, $raw_meta_data, $this->cache_group); |
|
638 | 638 | } |
639 | 639 | |
640 | 640 | } |
641 | 641 | |
642 | 642 | // Set meta data. |
643 | - if ( is_array( $raw_meta_data ) ) { |
|
643 | + if (is_array($raw_meta_data)) { |
|
644 | 644 | |
645 | - foreach ( $raw_meta_data as $meta ) { |
|
645 | + foreach ($raw_meta_data as $meta) { |
|
646 | 646 | $this->meta_data[] = new GetPaid_Meta_Data( |
647 | 647 | array( |
648 | 648 | 'id' => (int) $meta->meta_id, |
649 | 649 | 'key' => $meta->meta_key, |
650 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
650 | + 'value' => maybe_unserialize($meta->meta_value), |
|
651 | 651 | ) |
652 | 652 | ); |
653 | 653 | } |
@@ -662,28 +662,28 @@ discard block |
||
662 | 662 | * @since 1.0.19 |
663 | 663 | */ |
664 | 664 | public function save_meta_data() { |
665 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
665 | + if (!$this->data_store || is_null($this->meta_data)) { |
|
666 | 666 | return; |
667 | 667 | } |
668 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
669 | - if ( is_null( $meta->value ) ) { |
|
670 | - if ( ! empty( $meta->id ) ) { |
|
671 | - $this->data_store->delete_meta( $this, $meta ); |
|
672 | - unset( $this->meta_data[ $array_key ] ); |
|
668 | + foreach ($this->meta_data as $array_key => $meta) { |
|
669 | + if (is_null($meta->value)) { |
|
670 | + if (!empty($meta->id)) { |
|
671 | + $this->data_store->delete_meta($this, $meta); |
|
672 | + unset($this->meta_data[$array_key]); |
|
673 | 673 | } |
674 | - } elseif ( empty( $meta->id ) ) { |
|
675 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
674 | + } elseif (empty($meta->id)) { |
|
675 | + $meta->id = $this->data_store->add_meta($this, $meta); |
|
676 | 676 | $meta->apply_changes(); |
677 | 677 | } else { |
678 | - if ( $meta->get_changes() ) { |
|
679 | - $this->data_store->update_meta( $this, $meta ); |
|
678 | + if ($meta->get_changes()) { |
|
679 | + $this->data_store->update_meta($this, $meta); |
|
680 | 680 | $meta->apply_changes(); |
681 | 681 | } |
682 | 682 | } |
683 | 683 | } |
684 | - if ( ! empty( $this->cache_group ) ) { |
|
685 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
686 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
684 | + if (!empty($this->cache_group)) { |
|
685 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix($this->cache_group) . GetPaid_Cache_Helper::get_cache_prefix('object_' . $this->get_id()) . 'object_meta_' . $this->get_id(); |
|
686 | + wp_cache_delete($cache_key, $this->cache_group); |
|
687 | 687 | } |
688 | 688 | } |
689 | 689 | |
@@ -693,8 +693,8 @@ discard block |
||
693 | 693 | * @since 1.0.19 |
694 | 694 | * @param int $id ID. |
695 | 695 | */ |
696 | - public function set_id( $id ) { |
|
697 | - $this->id = absint( $id ); |
|
696 | + public function set_id($id) { |
|
697 | + $this->id = absint($id); |
|
698 | 698 | } |
699 | 699 | |
700 | 700 | /** |
@@ -704,10 +704,10 @@ discard block |
||
704 | 704 | * @param string $status New status. |
705 | 705 | * @return array details of change. |
706 | 706 | */ |
707 | - public function set_status( $status ) { |
|
707 | + public function set_status($status) { |
|
708 | 708 | $old_status = $this->get_status(); |
709 | 709 | |
710 | - $this->set_prop( 'status', $status ); |
|
710 | + $this->set_prop('status', $status); |
|
711 | 711 | |
712 | 712 | return array( |
713 | 713 | 'from' => $old_status, |
@@ -723,7 +723,7 @@ discard block |
||
723 | 723 | public function set_defaults() { |
724 | 724 | $this->data = $this->default_data; |
725 | 725 | $this->changes = array(); |
726 | - $this->set_object_read( false ); |
|
726 | + $this->set_object_read(false); |
|
727 | 727 | } |
728 | 728 | |
729 | 729 | /** |
@@ -732,7 +732,7 @@ discard block |
||
732 | 732 | * @since 1.0.19 |
733 | 733 | * @param boolean $read Should read?. |
734 | 734 | */ |
735 | - public function set_object_read( $read = true ) { |
|
735 | + public function set_object_read($read = true) { |
|
736 | 736 | $this->object_read = (bool) $read; |
737 | 737 | } |
738 | 738 | |
@@ -757,33 +757,33 @@ discard block |
||
757 | 757 | * |
758 | 758 | * @return bool|WP_Error |
759 | 759 | */ |
760 | - public function set_props( $props, $context = 'set' ) { |
|
760 | + public function set_props($props, $context = 'set') { |
|
761 | 761 | $errors = false; |
762 | 762 | |
763 | - $props = wp_unslash( $props ); |
|
764 | - foreach ( $props as $prop => $value ) { |
|
763 | + $props = wp_unslash($props); |
|
764 | + foreach ($props as $prop => $value) { |
|
765 | 765 | try { |
766 | 766 | /** |
767 | 767 | * Checks if the prop being set is allowed, and the value is not null. |
768 | 768 | */ |
769 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
769 | + if (is_null($value) || in_array($prop, array('prop', 'date_prop', 'meta_data'), true)) { |
|
770 | 770 | continue; |
771 | 771 | } |
772 | 772 | $setter = "set_$prop"; |
773 | 773 | |
774 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
775 | - $this->{$setter}( $value ); |
|
774 | + if (is_callable(array($this, $setter))) { |
|
775 | + $this->{$setter}($value); |
|
776 | 776 | } |
777 | - } catch ( Exception $e ) { |
|
778 | - if ( ! $errors ) { |
|
777 | + } catch (Exception $e) { |
|
778 | + if (!$errors) { |
|
779 | 779 | $errors = new WP_Error(); |
780 | 780 | } |
781 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
781 | + $errors->add($e->getCode(), $e->getMessage()); |
|
782 | 782 | $this->last_error = $e->getMessage(); |
783 | 783 | } |
784 | 784 | } |
785 | 785 | |
786 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
786 | + return $errors && count($errors->get_error_codes()) ? $errors : true; |
|
787 | 787 | } |
788 | 788 | |
789 | 789 | /** |
@@ -796,14 +796,14 @@ discard block |
||
796 | 796 | * @param string $prop Name of prop to set. |
797 | 797 | * @param mixed $value Value of the prop. |
798 | 798 | */ |
799 | - protected function set_prop( $prop, $value ) { |
|
800 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
801 | - if ( true === $this->object_read ) { |
|
802 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
803 | - $this->changes[ $prop ] = $value; |
|
799 | + protected function set_prop($prop, $value) { |
|
800 | + if (array_key_exists($prop, $this->data)) { |
|
801 | + if (true === $this->object_read) { |
|
802 | + if ($value !== $this->data[$prop] || array_key_exists($prop, $this->changes)) { |
|
803 | + $this->changes[$prop] = $value; |
|
804 | 804 | } |
805 | 805 | } else { |
806 | - $this->data[ $prop ] = $value; |
|
806 | + $this->data[$prop] = $value; |
|
807 | 807 | } |
808 | 808 | } |
809 | 809 | } |
@@ -824,7 +824,7 @@ discard block |
||
824 | 824 | * @since 1.0.19 |
825 | 825 | */ |
826 | 826 | public function apply_changes() { |
827 | - $this->data = array_replace( $this->data, $this->changes ); |
|
827 | + $this->data = array_replace($this->data, $this->changes); |
|
828 | 828 | $this->changes = array(); |
829 | 829 | } |
830 | 830 | |
@@ -849,14 +849,14 @@ discard block |
||
849 | 849 | * @param string $context What the value is for. Valid values are view and edit. |
850 | 850 | * @return mixed |
851 | 851 | */ |
852 | - protected function get_prop( $prop, $context = 'view' ) { |
|
852 | + protected function get_prop($prop, $context = 'view') { |
|
853 | 853 | $value = null; |
854 | 854 | |
855 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
856 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
855 | + if (array_key_exists($prop, $this->data)) { |
|
856 | + $value = array_key_exists($prop, $this->changes) ? $this->changes[$prop] : $this->data[$prop]; |
|
857 | 857 | |
858 | - if ( 'view' === $context ) { |
|
859 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
858 | + if ('view' === $context) { |
|
859 | + $value = apply_filters($this->get_hook_prefix() . $prop, $value, $this); |
|
860 | 860 | } |
861 | 861 | } |
862 | 862 | |
@@ -870,13 +870,13 @@ discard block |
||
870 | 870 | * @param string $prop Name of prop to set. |
871 | 871 | * @param string|integer $value Value of the prop. |
872 | 872 | */ |
873 | - protected function set_date_prop( $prop, $value ) { |
|
873 | + protected function set_date_prop($prop, $value) { |
|
874 | 874 | |
875 | - if ( empty( $value ) ) { |
|
876 | - $this->set_prop( $prop, null ); |
|
875 | + if (empty($value)) { |
|
876 | + $this->set_prop($prop, null); |
|
877 | 877 | return; |
878 | 878 | } |
879 | - $this->set_prop( $prop, $value ); |
|
879 | + $this->set_prop($prop, $value); |
|
880 | 880 | |
881 | 881 | } |
882 | 882 | |
@@ -887,7 +887,7 @@ discard block |
||
887 | 887 | * @param string $code Error code. |
888 | 888 | * @param string $message Error message. |
889 | 889 | */ |
890 | - protected function error( $code, $message ) { |
|
890 | + protected function error($code, $message) { |
|
891 | 891 | $this->last_error = $message; |
892 | 892 | } |
893 | 893 | |
@@ -899,7 +899,7 @@ discard block |
||
899 | 899 | */ |
900 | 900 | public function exists() { |
901 | 901 | $id = $this->get_id(); |
902 | - return ! empty( $id ); |
|
902 | + return !empty($id); |
|
903 | 903 | } |
904 | 904 | |
905 | 905 | } |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | * |
8 | 8 | */ |
9 | 9 | |
10 | -if ( ! defined( 'ABSPATH' ) ) { |
|
10 | +if (!defined('ABSPATH')) { |
|
11 | 11 | exit; // Exit if accessed directly |
12 | 12 | } |
13 | 13 | |
@@ -21,14 +21,14 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @param WP_Post $post |
23 | 23 | */ |
24 | - public static function output( $post ) { |
|
24 | + public static function output($post) { |
|
25 | 25 | |
26 | 26 | // Prepare the invoice. |
27 | - $invoice = new WPInv_Invoice( $post ); |
|
28 | - $customer = $invoice->exists() ? $invoice->get_user_id( 'edit' ) : get_current_user_id(); |
|
29 | - $customer = new WP_User( $customer ); |
|
30 | - $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ); |
|
31 | - wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' ); |
|
27 | + $invoice = new WPInv_Invoice($post); |
|
28 | + $customer = $invoice->exists() ? $invoice->get_user_id('edit') : get_current_user_id(); |
|
29 | + $customer = new WP_User($customer); |
|
30 | + $display = sprintf(_x('%1$s (%2$s)', 'user dropdown', 'invoicing'), $customer->display_name, $customer->user_email); |
|
31 | + wp_nonce_field('getpaid_meta_nonce', 'getpaid_meta_nonce'); |
|
32 | 32 | |
33 | 33 | ?> |
34 | 34 | |
@@ -43,11 +43,11 @@ discard block |
||
43 | 43 | <div class="col-12 col-sm-6"> |
44 | 44 | <div id="getpaid-invoice-user-id-wrapper" class="form-group"> |
45 | 45 | <div> |
46 | - <label for="post_author_override"><?php _e( 'Customer', 'invoicing' );?></label> |
|
46 | + <label for="post_author_override"><?php _e('Customer', 'invoicing'); ?></label> |
|
47 | 47 | </div> |
48 | 48 | <div> |
49 | - <select name="post_author_override" id="wpinv_post_author_override" class="getpaid-customer-search form-control regular-text" data-placeholder="<?php esc_attr_e( 'Search for a customer by email or name', 'invoicing' ); ?>"> |
|
50 | - <option selected="selected" value="<?php echo (int) $customer->ID; ?>"><?php echo sanitize_text_field( $display ); ?> </option>) |
|
49 | + <select name="post_author_override" id="wpinv_post_author_override" class="getpaid-customer-search form-control regular-text" data-placeholder="<?php esc_attr_e('Search for a customer by email or name', 'invoicing'); ?>"> |
|
50 | + <option selected="selected" value="<?php echo (int) $customer->ID; ?>"><?php echo sanitize_text_field($display); ?> </option>) |
|
51 | 51 | </select> |
52 | 52 | </div> |
53 | 53 | </div> |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | 'type' => 'text', |
61 | 61 | 'id' => 'getpaid-invoice-new-user-email', |
62 | 62 | 'name' => 'wpinv_email', |
63 | - 'label' => __( 'Email', 'invoicing' ) . '<span class="required">*</span>', |
|
63 | + 'label' => __('Email', 'invoicing') . '<span class="required">*</span>', |
|
64 | 64 | 'label_type' => 'vertical', |
65 | 65 | 'placeholder' => '[email protected]', |
66 | 66 | 'class' => 'form-control-sm', |
@@ -70,18 +70,18 @@ discard block |
||
70 | 70 | </div> |
71 | 71 | </div> |
72 | 72 | <div class="col-12 col-sm-6 form-group mt-sm-4"> |
73 | - <?php if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) : ?> |
|
73 | + <?php if (!$invoice->is_paid() && !$invoice->is_refunded()) : ?> |
|
74 | 74 | <a id="getpaid-invoice-fill-user-details" class="button button-small button-secondary" href="javascript:void(0)"> |
75 | 75 | <i aria-hidden="true" class="fa fa-refresh"></i> |
76 | - <?php _e( 'Fill User Details', 'invoicing' );?> |
|
76 | + <?php _e('Fill User Details', 'invoicing'); ?> |
|
77 | 77 | </a> |
78 | 78 | <a id="getpaid-invoice-create-new-user-button" class="button button-small button-secondary" href="javascript:void(0)"> |
79 | 79 | <i aria-hidden="true" class="fa fa-plus"></i> |
80 | - <?php _e( 'Add New User', 'invoicing' );?> |
|
80 | + <?php _e('Add New User', 'invoicing'); ?> |
|
81 | 81 | </a> |
82 | 82 | <a id="getpaid-invoice-cancel-create-new-user" class="button button-small button-secondary d-none" href="javascript:void(0)"> |
83 | 83 | <i aria-hidden="true" class="fa fa-close"></i> |
84 | - <?php _e( 'Cancel', 'invoicing' );?> |
|
84 | + <?php _e('Cancel', 'invoicing'); ?> |
|
85 | 85 | </a> |
86 | 86 | <?php endif; ?> |
87 | 87 | </div> |
@@ -94,11 +94,11 @@ discard block |
||
94 | 94 | 'type' => 'text', |
95 | 95 | 'id' => 'wpinv_first_name', |
96 | 96 | 'name' => 'wpinv_first_name', |
97 | - 'label' => __( 'First Name', 'invoicing' ), |
|
97 | + 'label' => __('First Name', 'invoicing'), |
|
98 | 98 | 'label_type' => 'vertical', |
99 | 99 | 'placeholder' => '', |
100 | 100 | 'class' => 'form-control-sm', |
101 | - 'value' => $invoice->get_first_name( 'edit' ), |
|
101 | + 'value' => $invoice->get_first_name('edit'), |
|
102 | 102 | ) |
103 | 103 | ); |
104 | 104 | ?> |
@@ -110,11 +110,11 @@ discard block |
||
110 | 110 | 'type' => 'text', |
111 | 111 | 'id' => 'wpinv_last_name', |
112 | 112 | 'name' => 'wpinv_last_name', |
113 | - 'label' => __( 'Last Name', 'invoicing' ), |
|
113 | + 'label' => __('Last Name', 'invoicing'), |
|
114 | 114 | 'label_type' => 'vertical', |
115 | 115 | 'placeholder' => '', |
116 | 116 | 'class' => 'form-control-sm', |
117 | - 'value' => $invoice->get_last_name( 'edit' ), |
|
117 | + 'value' => $invoice->get_last_name('edit'), |
|
118 | 118 | ) |
119 | 119 | ); |
120 | 120 | ?> |
@@ -129,11 +129,11 @@ discard block |
||
129 | 129 | 'type' => 'text', |
130 | 130 | 'id' => 'wpinv_company', |
131 | 131 | 'name' => 'wpinv_company', |
132 | - 'label' => __( 'Company', 'invoicing' ), |
|
132 | + 'label' => __('Company', 'invoicing'), |
|
133 | 133 | 'label_type' => 'vertical', |
134 | 134 | 'placeholder' => '', |
135 | 135 | 'class' => 'form-control-sm', |
136 | - 'value' => $invoice->get_company( 'edit' ), |
|
136 | + 'value' => $invoice->get_company('edit'), |
|
137 | 137 | ) |
138 | 138 | ); |
139 | 139 | ?> |
@@ -145,11 +145,11 @@ discard block |
||
145 | 145 | 'type' => 'text', |
146 | 146 | 'id' => 'wpinv_vat_number', |
147 | 147 | 'name' => 'wpinv_vat_number', |
148 | - 'label' => __( 'Vat Number', 'invoicing' ), |
|
148 | + 'label' => __('Vat Number', 'invoicing'), |
|
149 | 149 | 'label_type' => 'vertical', |
150 | 150 | 'placeholder' => '', |
151 | 151 | 'class' => 'form-control-sm', |
152 | - 'value' => $invoice->get_vat_number( 'edit' ), |
|
152 | + 'value' => $invoice->get_vat_number('edit'), |
|
153 | 153 | ) |
154 | 154 | ); |
155 | 155 | ?> |
@@ -164,11 +164,11 @@ discard block |
||
164 | 164 | 'type' => 'text', |
165 | 165 | 'id' => 'wpinv_address', |
166 | 166 | 'name' => 'wpinv_address', |
167 | - 'label' => __( 'Address', 'invoicing' ), |
|
167 | + 'label' => __('Address', 'invoicing'), |
|
168 | 168 | 'label_type' => 'vertical', |
169 | 169 | 'placeholder' => '', |
170 | 170 | 'class' => 'form-control-sm', |
171 | - 'value' => $invoice->get_address( 'edit' ), |
|
171 | + 'value' => $invoice->get_address('edit'), |
|
172 | 172 | ) |
173 | 173 | ); |
174 | 174 | ?> |
@@ -180,11 +180,11 @@ discard block |
||
180 | 180 | 'type' => 'text', |
181 | 181 | 'id' => 'wpinv_city', |
182 | 182 | 'name' => 'wpinv_city', |
183 | - 'label' => __( 'City', 'invoicing' ), |
|
183 | + 'label' => __('City', 'invoicing'), |
|
184 | 184 | 'label_type' => 'vertical', |
185 | 185 | 'placeholder' => '', |
186 | 186 | 'class' => 'form-control-sm', |
187 | - 'value' => $invoice->get_city( 'edit' ), |
|
187 | + 'value' => $invoice->get_city('edit'), |
|
188 | 188 | ) |
189 | 189 | ); |
190 | 190 | ?> |
@@ -198,11 +198,11 @@ discard block |
||
198 | 198 | array( |
199 | 199 | 'id' => 'wpinv_country', |
200 | 200 | 'name' => 'wpinv_country', |
201 | - 'label' => __( 'Country', 'invoicing' ), |
|
201 | + 'label' => __('Country', 'invoicing'), |
|
202 | 202 | 'label_type' => 'vertical', |
203 | - 'placeholder' => __( 'Choose a country', 'invoicing' ), |
|
203 | + 'placeholder' => __('Choose a country', 'invoicing'), |
|
204 | 204 | 'class' => 'form-control-sm', |
205 | - 'value' => $invoice->get_country( 'edit' ), |
|
205 | + 'value' => $invoice->get_country('edit'), |
|
206 | 206 | 'options' => wpinv_get_country_list(), |
207 | 207 | 'data-allow-clear' => 'false', |
208 | 208 | 'select2' => true, |
@@ -213,20 +213,20 @@ discard block |
||
213 | 213 | <div class="col-12 col-sm-6"> |
214 | 214 | <?php |
215 | 215 | |
216 | - $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) ); |
|
216 | + $states = wpinv_get_country_states($invoice->get_country('edit')); |
|
217 | 217 | |
218 | - if ( empty( $states ) ) { |
|
218 | + if (empty($states)) { |
|
219 | 219 | |
220 | 220 | echo aui()->input( |
221 | 221 | array( |
222 | 222 | 'type' => 'text', |
223 | 223 | 'id' => 'wpinv_state', |
224 | 224 | 'name' => 'wpinv_state', |
225 | - 'label' => __( 'State', 'invoicing' ), |
|
225 | + 'label' => __('State', 'invoicing'), |
|
226 | 226 | 'label_type' => 'vertical', |
227 | 227 | 'placeholder' => '', |
228 | 228 | 'class' => 'form-control-sm', |
229 | - 'value' => $invoice->get_state( 'edit' ), |
|
229 | + 'value' => $invoice->get_state('edit'), |
|
230 | 230 | ) |
231 | 231 | ); |
232 | 232 | |
@@ -236,11 +236,11 @@ discard block |
||
236 | 236 | array( |
237 | 237 | 'id' => 'wpinv_state', |
238 | 238 | 'name' => 'wpinv_state', |
239 | - 'label' => __( 'State', 'invoicing' ), |
|
239 | + 'label' => __('State', 'invoicing'), |
|
240 | 240 | 'label_type' => 'vertical', |
241 | - 'placeholder' => __( 'Select a state', 'invoicing' ), |
|
241 | + 'placeholder' => __('Select a state', 'invoicing'), |
|
242 | 242 | 'class' => 'form-control-sm', |
243 | - 'value' => $invoice->get_state( 'edit' ), |
|
243 | + 'value' => $invoice->get_state('edit'), |
|
244 | 244 | 'options' => $states, |
245 | 245 | 'data-allow-clear' => 'false', |
246 | 246 | 'select2' => true, |
@@ -261,11 +261,11 @@ discard block |
||
261 | 261 | 'type' => 'text', |
262 | 262 | 'id' => 'wpinv_zip', |
263 | 263 | 'name' => 'wpinv_zip', |
264 | - 'label' => __( 'Zip / Postal Code', 'invoicing' ), |
|
264 | + 'label' => __('Zip / Postal Code', 'invoicing'), |
|
265 | 265 | 'label_type' => 'vertical', |
266 | 266 | 'placeholder' => '', |
267 | 267 | 'class' => 'form-control-sm', |
268 | - 'value' => $invoice->get_zip( 'edit' ), |
|
268 | + 'value' => $invoice->get_zip('edit'), |
|
269 | 269 | ) |
270 | 270 | ); |
271 | 271 | ?> |
@@ -277,18 +277,18 @@ discard block |
||
277 | 277 | 'type' => 'text', |
278 | 278 | 'id' => 'wpinv_phone', |
279 | 279 | 'name' => 'wpinv_phone', |
280 | - 'label' => __( 'Phone', 'invoicing' ), |
|
280 | + 'label' => __('Phone', 'invoicing'), |
|
281 | 281 | 'label_type' => 'vertical', |
282 | 282 | 'placeholder' => '', |
283 | 283 | 'class' => 'form-control-sm', |
284 | - 'value' => $invoice->get_phone( 'edit' ), |
|
284 | + 'value' => $invoice->get_phone('edit'), |
|
285 | 285 | ) |
286 | 286 | ); |
287 | 287 | ?> |
288 | 288 | </div> |
289 | 289 | </div> |
290 | 290 | |
291 | - <?php do_action( 'getpaid_after_metabox_invoice_address', $invoice ); ?> |
|
291 | + <?php do_action('getpaid_after_metabox_invoice_address', $invoice); ?> |
|
292 | 292 | </div> |
293 | 293 | <?php |
294 | 294 | } |
@@ -298,50 +298,50 @@ discard block |
||
298 | 298 | * |
299 | 299 | * @param int $post_id |
300 | 300 | */ |
301 | - public static function save( $post_id ) { |
|
301 | + public static function save($post_id) { |
|
302 | 302 | |
303 | 303 | // Prepare the invoice. |
304 | - $invoice = new WPInv_Invoice( $post_id ); |
|
304 | + $invoice = new WPInv_Invoice($post_id); |
|
305 | 305 | |
306 | 306 | // Load new data. |
307 | 307 | $invoice->set_props( |
308 | 308 | array( |
309 | - 'template' => isset( $_POST['wpinv_template'] ) ? wpinv_clean( $_POST['wpinv_template'] ) : null, |
|
310 | - 'email_cc' => isset( $_POST['wpinv_cc'] ) ? wpinv_clean( $_POST['wpinv_cc'] ) : null, |
|
311 | - 'disable_taxes' => isset( $_POST['disable_taxes'] ), |
|
312 | - 'currency' => isset( $_POST['wpinv_currency'] ) ? wpinv_clean( $_POST['wpinv_currency'] ) : null, |
|
313 | - 'gateway' => ( $invoice->needs_payment() && isset( $_POST['wpinv_gateway'] ) ) ? wpinv_clean( $_POST['wpinv_gateway'] ) : null, |
|
314 | - 'address' => isset( $_POST['wpinv_address'] ) ? wpinv_clean( $_POST['wpinv_address'] ) : null, |
|
315 | - 'vat_number' => isset( $_POST['wpinv_vat_number'] ) ? wpinv_clean( $_POST['wpinv_vat_number'] ) : null, |
|
316 | - 'company' => isset( $_POST['wpinv_company'] ) ? wpinv_clean( $_POST['wpinv_company'] ) : null, |
|
317 | - 'zip' => isset( $_POST['wpinv_zip'] ) ? wpinv_clean( $_POST['wpinv_zip'] ) : null, |
|
318 | - 'state' => isset( $_POST['wpinv_state'] ) ? wpinv_clean( $_POST['wpinv_state'] ) : null, |
|
319 | - 'city' => isset( $_POST['wpinv_city'] ) ? wpinv_clean( $_POST['wpinv_city'] ) : null, |
|
320 | - 'country' => isset( $_POST['wpinv_country'] ) ? wpinv_clean( $_POST['wpinv_country'] ) : null, |
|
321 | - 'phone' => isset( $_POST['wpinv_phone'] ) ? wpinv_clean( $_POST['wpinv_phone'] ) : null, |
|
322 | - 'first_name' => isset( $_POST['wpinv_first_name'] ) ? wpinv_clean( $_POST['wpinv_first_name'] ) : null, |
|
323 | - 'last_name' => isset( $_POST['wpinv_last_name'] ) ? wpinv_clean( $_POST['wpinv_last_name'] ) : null, |
|
324 | - 'author' => isset( $_POST['post_author_override'] ) ? wpinv_clean( $_POST['post_author_override'] ) : null, |
|
325 | - 'date_created' => isset( $_POST['date_created'] ) ? wpinv_clean( $_POST['date_created'] ) : null, |
|
326 | - 'date_completed' => isset( $_POST['wpinv_date_completed'] ) ? wpinv_clean( $_POST['wpinv_date_completed'] ) : null, |
|
327 | - 'due_date' => isset( $_POST['wpinv_due_date'] ) ? wpinv_clean( $_POST['wpinv_due_date'] ) : null, |
|
328 | - 'number' => isset( $_POST['wpinv_number'] ) ? wpinv_clean( $_POST['wpinv_number'] ) : null, |
|
329 | - 'status' => isset( $_POST['wpinv_status'] ) ? wpinv_clean( $_POST['wpinv_status'] ) : null, |
|
309 | + 'template' => isset($_POST['wpinv_template']) ? wpinv_clean($_POST['wpinv_template']) : null, |
|
310 | + 'email_cc' => isset($_POST['wpinv_cc']) ? wpinv_clean($_POST['wpinv_cc']) : null, |
|
311 | + 'disable_taxes' => isset($_POST['disable_taxes']), |
|
312 | + 'currency' => isset($_POST['wpinv_currency']) ? wpinv_clean($_POST['wpinv_currency']) : null, |
|
313 | + 'gateway' => ($invoice->needs_payment() && isset($_POST['wpinv_gateway'])) ? wpinv_clean($_POST['wpinv_gateway']) : null, |
|
314 | + 'address' => isset($_POST['wpinv_address']) ? wpinv_clean($_POST['wpinv_address']) : null, |
|
315 | + 'vat_number' => isset($_POST['wpinv_vat_number']) ? wpinv_clean($_POST['wpinv_vat_number']) : null, |
|
316 | + 'company' => isset($_POST['wpinv_company']) ? wpinv_clean($_POST['wpinv_company']) : null, |
|
317 | + 'zip' => isset($_POST['wpinv_zip']) ? wpinv_clean($_POST['wpinv_zip']) : null, |
|
318 | + 'state' => isset($_POST['wpinv_state']) ? wpinv_clean($_POST['wpinv_state']) : null, |
|
319 | + 'city' => isset($_POST['wpinv_city']) ? wpinv_clean($_POST['wpinv_city']) : null, |
|
320 | + 'country' => isset($_POST['wpinv_country']) ? wpinv_clean($_POST['wpinv_country']) : null, |
|
321 | + 'phone' => isset($_POST['wpinv_phone']) ? wpinv_clean($_POST['wpinv_phone']) : null, |
|
322 | + 'first_name' => isset($_POST['wpinv_first_name']) ? wpinv_clean($_POST['wpinv_first_name']) : null, |
|
323 | + 'last_name' => isset($_POST['wpinv_last_name']) ? wpinv_clean($_POST['wpinv_last_name']) : null, |
|
324 | + 'author' => isset($_POST['post_author_override']) ? wpinv_clean($_POST['post_author_override']) : null, |
|
325 | + 'date_created' => isset($_POST['date_created']) ? wpinv_clean($_POST['date_created']) : null, |
|
326 | + 'date_completed' => isset($_POST['wpinv_date_completed']) ? wpinv_clean($_POST['wpinv_date_completed']) : null, |
|
327 | + 'due_date' => isset($_POST['wpinv_due_date']) ? wpinv_clean($_POST['wpinv_due_date']) : null, |
|
328 | + 'number' => isset($_POST['wpinv_number']) ? wpinv_clean($_POST['wpinv_number']) : null, |
|
329 | + 'status' => isset($_POST['wpinv_status']) ? wpinv_clean($_POST['wpinv_status']) : null, |
|
330 | 330 | ) |
331 | 331 | ); |
332 | 332 | |
333 | 333 | // Discount code. |
334 | - if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
334 | + if (!$invoice->is_paid() && !$invoice->is_refunded()) { |
|
335 | 335 | |
336 | - if ( isset( $_POST['wpinv_discount_code'] ) ) { |
|
337 | - $invoice->set_discount_code( wpinv_clean( $_POST['wpinv_discount_code'] ) ); |
|
336 | + if (isset($_POST['wpinv_discount_code'])) { |
|
337 | + $invoice->set_discount_code(wpinv_clean($_POST['wpinv_discount_code'])); |
|
338 | 338 | } |
339 | 339 | |
340 | - $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
341 | - if ( $discount->exists() ) { |
|
342 | - $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
340 | + $discount = new WPInv_Discount($invoice->get_discount_code()); |
|
341 | + if ($discount->exists()) { |
|
342 | + $invoice->add_discount(getpaid_calculate_invoice_discount($invoice, $discount)); |
|
343 | 343 | } else { |
344 | - $invoice->remove_discount( 'discount_code' ); |
|
344 | + $invoice->remove_discount('discount_code'); |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | // Recalculate totals. |
@@ -350,17 +350,17 @@ discard block |
||
350 | 350 | } |
351 | 351 | |
352 | 352 | // If we're creating a new user... |
353 | - if ( ! empty( $_POST['wpinv_new_user'] ) && is_email( stripslashes( $_POST['wpinv_email'] ) ) ) { |
|
353 | + if (!empty($_POST['wpinv_new_user']) && is_email(stripslashes($_POST['wpinv_email']))) { |
|
354 | 354 | |
355 | 355 | // Attempt to create the user. |
356 | - $user = wpinv_create_user( sanitize_email( stripslashes( $_POST['wpinv_email'] ) ) ); |
|
356 | + $user = wpinv_create_user(sanitize_email(stripslashes($_POST['wpinv_email']))); |
|
357 | 357 | |
358 | 358 | |
359 | 359 | // If successful, update the invoice author. |
360 | - if ( is_numeric( $user ) ) { |
|
361 | - $invoice->set_author( $user ); |
|
360 | + if (is_numeric($user)) { |
|
361 | + $invoice->set_author($user); |
|
362 | 362 | } else { |
363 | - wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
|
363 | + wpinv_error_log($user->get_error_message(), __('Invoice add new user', 'invoicing'), __FILE__, __LINE__); |
|
364 | 364 | } |
365 | 365 | } |
366 | 366 | |
@@ -374,24 +374,24 @@ discard block |
||
374 | 374 | $GLOBALS['wpinv_skip_invoice_notification'] = false; |
375 | 375 | |
376 | 376 | // (Maybe) send new user notification. |
377 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
378 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
|
379 | - wp_send_new_user_notifications( $user, 'user' ); |
|
377 | + $should_send_notification = wpinv_get_option('disable_new_user_emails'); |
|
378 | + if (!empty($user) && is_numeric($user) && apply_filters('getpaid_send_new_user_notification', empty($should_send_notification))) { |
|
379 | + wp_send_new_user_notifications($user, 'user'); |
|
380 | 380 | } |
381 | 381 | |
382 | - if ( ! empty( $_POST['send_to_customer'] ) && ! $invoice->is_draft() ) { |
|
382 | + if (!empty($_POST['send_to_customer']) && !$invoice->is_draft()) { |
|
383 | 383 | |
384 | - $sent = getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
|
384 | + $sent = getpaid()->get('invoice_emails')->user_invoice($invoice, true); |
|
385 | 385 | |
386 | - if ( $sent ) { |
|
387 | - getpaid_admin()->show_success( __( 'Invoice was successfully sent to the customer', 'invoicing' ) ); |
|
386 | + if ($sent) { |
|
387 | + getpaid_admin()->show_success(__('Invoice was successfully sent to the customer', 'invoicing')); |
|
388 | 388 | } else { |
389 | - getpaid_admin()->show_error( __( 'Could not send the invoice to the customer', 'invoicing' ) ); |
|
389 | + getpaid_admin()->show_error(__('Could not send the invoice to the customer', 'invoicing')); |
|
390 | 390 | } |
391 | 391 | |
392 | 392 | } |
393 | 393 | |
394 | 394 | // Fires after an invoice is saved. |
395 | - do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
|
395 | + do_action('wpinv_invoice_metabox_saved', $invoice); |
|
396 | 396 | } |
397 | 397 | } |
@@ -108,13 +108,13 @@ discard block |
||
108 | 108 | */ |
109 | 109 | function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
110 | 110 | |
111 | - $invoice_statuses = array( |
|
112 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
111 | + $invoice_statuses = array( |
|
112 | + 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
113 | 113 | 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
114 | 114 | 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
115 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
116 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
117 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
115 | + 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
116 | + 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
117 | + 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
118 | 118 | 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
119 | 119 | 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
120 | 120 | ); |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $invoice = $invoice->get_post_type(); |
132 | 132 | } |
133 | 133 | |
134 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
134 | + return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -249,25 +249,25 @@ discard block |
||
249 | 249 | * @return string |
250 | 250 | */ |
251 | 251 | function getpaid_get_price_format() { |
252 | - $currency_pos = wpinv_currency_position(); |
|
253 | - $format = '%1$s%2$s'; |
|
254 | - |
|
255 | - switch ( $currency_pos ) { |
|
256 | - case 'left': |
|
257 | - $format = '%1$s%2$s'; |
|
258 | - break; |
|
259 | - case 'right': |
|
260 | - $format = '%2$s%1$s'; |
|
261 | - break; |
|
262 | - case 'left_space': |
|
263 | - $format = '%1$s %2$s'; |
|
264 | - break; |
|
265 | - case 'right_space': |
|
266 | - $format = '%2$s %1$s'; |
|
267 | - break; |
|
268 | - } |
|
269 | - |
|
270 | - return apply_filters( 'getpaid_price_format', $format, $currency_pos ); |
|
252 | + $currency_pos = wpinv_currency_position(); |
|
253 | + $format = '%1$s%2$s'; |
|
254 | + |
|
255 | + switch ( $currency_pos ) { |
|
256 | + case 'left': |
|
257 | + $format = '%1$s%2$s'; |
|
258 | + break; |
|
259 | + case 'right': |
|
260 | + $format = '%2$s%1$s'; |
|
261 | + break; |
|
262 | + case 'left_space': |
|
263 | + $format = '%1$s %2$s'; |
|
264 | + break; |
|
265 | + case 'right_space': |
|
266 | + $format = '%2$s %1$s'; |
|
267 | + break; |
|
268 | + } |
|
269 | + |
|
270 | + return apply_filters( 'getpaid_price_format', $format, $currency_pos ); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | /** |
@@ -362,13 +362,13 @@ discard block |
||
362 | 362 | * @param mixed $value Value. |
363 | 363 | */ |
364 | 364 | function getpaid_maybe_define_constant( $name, $value ) { |
365 | - if ( ! defined( $name ) ) { |
|
366 | - define( $name, $value ); |
|
367 | - } |
|
365 | + if ( ! defined( $name ) ) { |
|
366 | + define( $name, $value ); |
|
367 | + } |
|
368 | 368 | } |
369 | 369 | |
370 | 370 | function wpinv_get_php_arg_separator_output() { |
371 | - return ini_get( 'arg_separator.output' ); |
|
371 | + return ini_get( 'arg_separator.output' ); |
|
372 | 372 | } |
373 | 373 | |
374 | 374 | function wpinv_rgb_from_hex( $color ) { |
@@ -722,11 +722,11 @@ discard block |
||
722 | 722 | $list = array(); |
723 | 723 | } |
724 | 724 | |
725 | - if ( ! is_array( $list ) ) { |
|
726 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
727 | - } |
|
725 | + if ( ! is_array( $list ) ) { |
|
726 | + return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
727 | + } |
|
728 | 728 | |
729 | - return $list; |
|
729 | + return $list; |
|
730 | 730 | } |
731 | 731 | |
732 | 732 | /** |
@@ -746,9 +746,9 @@ discard block |
||
746 | 746 | } |
747 | 747 | |
748 | 748 | $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
749 | - wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
749 | + wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
750 | 750 | |
751 | - return $data; |
|
751 | + return $data; |
|
752 | 752 | } |
753 | 753 | |
754 | 754 | /** |
@@ -777,17 +777,17 @@ discard block |
||
777 | 777 | */ |
778 | 778 | function wpinv_clean( $var ) { |
779 | 779 | |
780 | - if ( is_array( $var ) ) { |
|
781 | - return array_map( 'wpinv_clean', $var ); |
|
780 | + if ( is_array( $var ) ) { |
|
781 | + return array_map( 'wpinv_clean', $var ); |
|
782 | 782 | } |
783 | 783 | |
784 | 784 | if ( is_object( $var ) ) { |
785 | - $object_vars = get_object_vars( $var ); |
|
786 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
787 | - $var->$property_name = wpinv_clean( $property_value ); |
|
785 | + $object_vars = get_object_vars( $var ); |
|
786 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
787 | + $var->$property_name = wpinv_clean( $property_value ); |
|
788 | 788 | } |
789 | 789 | return $var; |
790 | - } |
|
790 | + } |
|
791 | 791 | |
792 | 792 | return is_string( $var ) ? sanitize_text_field( stripslashes( $var ) ) : $var; |
793 | 793 | } |
@@ -800,7 +800,7 @@ discard block |
||
800 | 800 | */ |
801 | 801 | function getpaid_convert_price_string_to_options( $str ) { |
802 | 802 | |
803 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
803 | + $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
804 | 804 | $options = array(); |
805 | 805 | |
806 | 806 | foreach ( $raw_options as $option ) { |
@@ -883,7 +883,7 @@ discard block |
||
883 | 883 | * @return string |
884 | 884 | */ |
885 | 885 | function getpaid_date_format() { |
886 | - return apply_filters( 'getpaid_date_format', get_option( 'date_format' ) ); |
|
886 | + return apply_filters( 'getpaid_date_format', get_option( 'date_format' ) ); |
|
887 | 887 | } |
888 | 888 | |
889 | 889 | /** |
@@ -892,7 +892,7 @@ discard block |
||
892 | 892 | * @return string |
893 | 893 | */ |
894 | 894 | function getpaid_time_format() { |
895 | - return apply_filters( 'getpaid_time_format', get_option( 'time_format' ) ); |
|
895 | + return apply_filters( 'getpaid_time_format', get_option( 'time_format' ) ); |
|
896 | 896 | } |
897 | 897 | |
898 | 898 | /** |
@@ -905,15 +905,15 @@ discard block |
||
905 | 905 | function getpaid_limit_length( $string, $limit ) { |
906 | 906 | $str_limit = $limit - 3; |
907 | 907 | |
908 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
909 | - if ( mb_strlen( $string ) > $limit ) { |
|
910 | - $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
911 | - } |
|
912 | - } else { |
|
913 | - if ( strlen( $string ) > $limit ) { |
|
914 | - $string = substr( $string, 0, $str_limit ) . '...'; |
|
915 | - } |
|
916 | - } |
|
908 | + if ( function_exists( 'mb_strimwidth' ) ) { |
|
909 | + if ( mb_strlen( $string ) > $limit ) { |
|
910 | + $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
911 | + } |
|
912 | + } else { |
|
913 | + if ( strlen( $string ) > $limit ) { |
|
914 | + $string = substr( $string, 0, $str_limit ) . '...'; |
|
915 | + } |
|
916 | + } |
|
917 | 917 | return $string; |
918 | 918 | |
919 | 919 | } |
@@ -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 | * Are we supporting item quantities? |
@@ -20,35 +20,35 @@ discard block |
||
20 | 20 | */ |
21 | 21 | function wpinv_get_ip() { |
22 | 22 | |
23 | - if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { |
|
24 | - return sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ); |
|
23 | + if (isset($_SERVER['HTTP_X_REAL_IP'])) { |
|
24 | + return sanitize_text_field(wp_unslash($_SERVER['HTTP_X_REAL_IP'])); |
|
25 | 25 | } |
26 | 26 | |
27 | - if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
|
27 | + if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
|
28 | 28 | // Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2 |
29 | 29 | // Make sure we always only send through the first IP in the list which should always be the client IP. |
30 | - return (string) rest_is_ip_address( trim( current( preg_split( '/,/', sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) ) ) ) ); |
|
30 | + return (string) rest_is_ip_address(trim(current(preg_split('/,/', sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR'])))))); |
|
31 | 31 | } |
32 | 32 | |
33 | - if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
|
34 | - return sanitize_text_field( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ) ); |
|
33 | + if (isset($_SERVER['HTTP_CLIENT_IP'])) { |
|
34 | + return sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); |
|
35 | 35 | } |
36 | 36 | |
37 | - if ( isset( $_SERVER['REMOTE_ADDR'] ) ) { |
|
38 | - return sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); |
|
37 | + if (isset($_SERVER['REMOTE_ADDR'])) { |
|
38 | + return sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | return ''; |
42 | 42 | } |
43 | 43 | |
44 | 44 | function wpinv_get_user_agent() { |
45 | - if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
46 | - $user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); |
|
45 | + if (!empty($_SERVER['HTTP_USER_AGENT'])) { |
|
46 | + $user_agent = sanitize_text_field($_SERVER['HTTP_USER_AGENT']); |
|
47 | 47 | } else { |
48 | 48 | $user_agent = ''; |
49 | 49 | } |
50 | 50 | |
51 | - return apply_filters( 'wpinv_get_user_agent', $user_agent ); |
|
51 | + return apply_filters('wpinv_get_user_agent', $user_agent); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
@@ -56,23 +56,23 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @param string $amount The amount to sanitize. |
58 | 58 | */ |
59 | -function wpinv_sanitize_amount( $amount ) { |
|
59 | +function wpinv_sanitize_amount($amount) { |
|
60 | 60 | |
61 | - if ( is_numeric( $amount ) ) { |
|
62 | - return floatval( $amount ); |
|
61 | + if (is_numeric($amount)) { |
|
62 | + return floatval($amount); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | // Separate the decimals and thousands. |
66 | - $amount = explode( wpinv_decimal_separator(), $amount ); |
|
66 | + $amount = explode(wpinv_decimal_separator(), $amount); |
|
67 | 67 | |
68 | 68 | // Remove thousands. |
69 | - $amount[0] = str_replace( wpinv_thousands_separator(), '', $amount[0] ); |
|
69 | + $amount[0] = str_replace(wpinv_thousands_separator(), '', $amount[0]); |
|
70 | 70 | |
71 | 71 | // Convert back to string. |
72 | - $amount = count( $amount ) > 1 ? "{$amount[0]}.{$amount[1]}" : $amount[0]; |
|
72 | + $amount = count($amount) > 1 ? "{$amount[0]}.{$amount[1]}" : $amount[0]; |
|
73 | 73 | |
74 | 74 | // Cast the remaining to a float. |
75 | - return (float) preg_replace( '/[^0-9\.\-]/', '', $amount ); |
|
75 | + return (float) preg_replace('/[^0-9\.\-]/', '', $amount); |
|
76 | 76 | |
77 | 77 | } |
78 | 78 | |
@@ -82,19 +82,19 @@ discard block |
||
82 | 82 | * @param float $amount |
83 | 83 | * @param float|string|int|null $decimals |
84 | 84 | */ |
85 | -function wpinv_round_amount( $amount, $decimals = null, $use_sprintf = false ) { |
|
85 | +function wpinv_round_amount($amount, $decimals = null, $use_sprintf = false) { |
|
86 | 86 | |
87 | - if ( $decimals === null ) { |
|
87 | + if ($decimals === null) { |
|
88 | 88 | $decimals = wpinv_decimals(); |
89 | 89 | } |
90 | 90 | |
91 | - if ( $use_sprintf ) { |
|
92 | - $amount = sprintf( "%.{$decimals}f", (float) $amount ); |
|
91 | + if ($use_sprintf) { |
|
92 | + $amount = sprintf("%.{$decimals}f", (float) $amount); |
|
93 | 93 | } else { |
94 | - $amount = round( (float) $amount, absint( $decimals ) ); |
|
94 | + $amount = round((float) $amount, absint($decimals)); |
|
95 | 95 | } |
96 | 96 | |
97 | - return apply_filters( 'wpinv_round_amount', $amount, $decimals ); |
|
97 | + return apply_filters('wpinv_round_amount', $amount, $decimals); |
|
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
@@ -106,32 +106,32 @@ discard block |
||
106 | 106 | * @param string|WPInv_Invoice $invoice The invoice object|post type|type |
107 | 107 | * @return array |
108 | 108 | */ |
109 | -function wpinv_get_invoice_statuses( $draft = false, $trashed = false, $invoice = false ) { |
|
109 | +function wpinv_get_invoice_statuses($draft = false, $trashed = false, $invoice = false) { |
|
110 | 110 | |
111 | 111 | $invoice_statuses = array( |
112 | - 'wpi-pending' => _x( 'Pending payment', 'Invoice status', 'invoicing' ), |
|
113 | - 'publish' => _x( 'Paid', 'Invoice status', 'invoicing' ), |
|
114 | - 'wpi-processing' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
|
115 | - 'wpi-onhold' => _x( 'On hold', 'Invoice status', 'invoicing' ), |
|
116 | - 'wpi-cancelled' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
117 | - 'wpi-refunded' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
118 | - 'wpi-failed' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
|
119 | - 'wpi-renewal' => _x( 'Renewal Payment', 'Invoice status', 'invoicing' ), |
|
112 | + 'wpi-pending' => _x('Pending payment', 'Invoice status', 'invoicing'), |
|
113 | + 'publish' => _x('Paid', 'Invoice status', 'invoicing'), |
|
114 | + 'wpi-processing' => _x('Processing', 'Invoice status', 'invoicing'), |
|
115 | + 'wpi-onhold' => _x('On hold', 'Invoice status', 'invoicing'), |
|
116 | + 'wpi-cancelled' => _x('Cancelled', 'Invoice status', 'invoicing'), |
|
117 | + 'wpi-refunded' => _x('Refunded', 'Invoice status', 'invoicing'), |
|
118 | + 'wpi-failed' => _x('Failed', 'Invoice status', 'invoicing'), |
|
119 | + 'wpi-renewal' => _x('Renewal Payment', 'Invoice status', 'invoicing'), |
|
120 | 120 | ); |
121 | 121 | |
122 | - if ( $draft ) { |
|
123 | - $invoice_statuses['draft'] = __( 'Draft', 'invoicing' ); |
|
122 | + if ($draft) { |
|
123 | + $invoice_statuses['draft'] = __('Draft', 'invoicing'); |
|
124 | 124 | } |
125 | 125 | |
126 | - if ( $trashed ) { |
|
127 | - $invoice_statuses['trash'] = __( 'Trash', 'invoicing' ); |
|
126 | + if ($trashed) { |
|
127 | + $invoice_statuses['trash'] = __('Trash', 'invoicing'); |
|
128 | 128 | } |
129 | 129 | |
130 | - if ( $invoice instanceof WPInv_Invoice ) { |
|
130 | + if ($invoice instanceof WPInv_Invoice) { |
|
131 | 131 | $invoice = $invoice->get_post_type(); |
132 | 132 | } |
133 | 133 | |
134 | - return apply_filters( 'wpinv_statuses', $invoice_statuses, $invoice ); |
|
134 | + return apply_filters('wpinv_statuses', $invoice_statuses, $invoice); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -140,11 +140,11 @@ discard block |
||
140 | 140 | * @param string $status The raw status |
141 | 141 | * @param string|WPInv_Invoice $invoice The invoice object|post type|type |
142 | 142 | */ |
143 | -function wpinv_status_nicename( $status, $invoice = false ) { |
|
144 | - $statuses = wpinv_get_invoice_statuses( true, true, $invoice ); |
|
145 | - $status = isset( $statuses[$status] ) ? $statuses[$status] : $status; |
|
143 | +function wpinv_status_nicename($status, $invoice = false) { |
|
144 | + $statuses = wpinv_get_invoice_statuses(true, true, $invoice); |
|
145 | + $status = isset($statuses[$status]) ? $statuses[$status] : $status; |
|
146 | 146 | |
147 | - return sanitize_text_field( $status ); |
|
147 | + return sanitize_text_field($status); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | /** |
@@ -152,13 +152,13 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @param string $current |
154 | 154 | */ |
155 | -function wpinv_get_currency( $current = '' ) { |
|
155 | +function wpinv_get_currency($current = '') { |
|
156 | 156 | |
157 | - if ( empty( $current ) ) { |
|
158 | - $current = apply_filters( 'wpinv_currency', wpinv_get_option( 'currency', 'USD' ) ); |
|
157 | + if (empty($current)) { |
|
158 | + $current = apply_filters('wpinv_currency', wpinv_get_option('currency', 'USD')); |
|
159 | 159 | } |
160 | 160 | |
161 | - return trim( strtoupper( $current ) ); |
|
161 | + return trim(strtoupper($current)); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | /** |
@@ -166,25 +166,25 @@ discard block |
||
166 | 166 | * |
167 | 167 | * @param string|null $currency The currency code. Defaults to the default currency. |
168 | 168 | */ |
169 | -function wpinv_currency_symbol( $currency = null ) { |
|
169 | +function wpinv_currency_symbol($currency = null) { |
|
170 | 170 | |
171 | 171 | // Prepare the currency. |
172 | - $currency = empty( $currency ) ? wpinv_get_currency() : wpinv_clean( $currency ); |
|
172 | + $currency = empty($currency) ? wpinv_get_currency() : wpinv_clean($currency); |
|
173 | 173 | |
174 | 174 | // Fetch all symbols. |
175 | 175 | $symbols = wpinv_get_currency_symbols(); |
176 | 176 | |
177 | 177 | // Fetch this currencies symbol. |
178 | - $currency_symbol = isset( $symbols[$currency] ) ? $symbols[$currency] : $currency; |
|
178 | + $currency_symbol = isset($symbols[$currency]) ? $symbols[$currency] : $currency; |
|
179 | 179 | |
180 | 180 | // Filter the symbol. |
181 | - return apply_filters( 'wpinv_currency_symbol', $currency_symbol, $currency ); |
|
181 | + return apply_filters('wpinv_currency_symbol', $currency_symbol, $currency); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | function wpinv_currency_position() { |
185 | - $position = wpinv_get_option( 'currency_position', 'left' ); |
|
185 | + $position = wpinv_get_option('currency_position', 'left'); |
|
186 | 186 | |
187 | - return apply_filters( 'wpinv_currency_position', $position ); |
|
187 | + return apply_filters('wpinv_currency_position', $position); |
|
188 | 188 | } |
189 | 189 | |
190 | 190 | /** |
@@ -192,13 +192,13 @@ discard block |
||
192 | 192 | * |
193 | 193 | * @param $string|null $current |
194 | 194 | */ |
195 | -function wpinv_thousands_separator( $current = null ) { |
|
195 | +function wpinv_thousands_separator($current = null) { |
|
196 | 196 | |
197 | - if ( null == $current ) { |
|
198 | - $current = wpinv_get_option( 'thousands_separator', ',' ); |
|
197 | + if (null == $current) { |
|
198 | + $current = wpinv_get_option('thousands_separator', ','); |
|
199 | 199 | } |
200 | 200 | |
201 | - return trim( $current ); |
|
201 | + return trim($current); |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -206,13 +206,13 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @param $string|null $current |
208 | 208 | */ |
209 | -function wpinv_decimal_separator( $current = null ) { |
|
209 | +function wpinv_decimal_separator($current = null) { |
|
210 | 210 | |
211 | - if ( null == $current ) { |
|
212 | - $current = wpinv_get_option( 'decimal_separator', '.' ); |
|
211 | + if (null == $current) { |
|
212 | + $current = wpinv_get_option('decimal_separator', '.'); |
|
213 | 213 | } |
214 | 214 | |
215 | - return trim( $current ); |
|
215 | + return trim($current); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -220,27 +220,27 @@ discard block |
||
220 | 220 | * |
221 | 221 | * @param $string|null $current |
222 | 222 | */ |
223 | -function wpinv_decimals( $current = null ) { |
|
223 | +function wpinv_decimals($current = null) { |
|
224 | 224 | |
225 | - if ( null == $current ) { |
|
226 | - $current = wpinv_get_option( 'decimals', 2 ); |
|
225 | + if (null == $current) { |
|
226 | + $current = wpinv_get_option('decimals', 2); |
|
227 | 227 | } |
228 | 228 | |
229 | - return absint( $current ); |
|
229 | + return absint($current); |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | /** |
233 | 233 | * Retrieves a list of all supported currencies. |
234 | 234 | */ |
235 | 235 | function wpinv_get_currencies() { |
236 | - return apply_filters( 'wpinv_currencies', wpinv_get_data( 'currencies' ) ); |
|
236 | + return apply_filters('wpinv_currencies', wpinv_get_data('currencies')); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
240 | 240 | * Retrieves a list of all currency symbols. |
241 | 241 | */ |
242 | 242 | function wpinv_get_currency_symbols() { |
243 | - return apply_filters( 'wpinv_currency_symbols', wpinv_get_data( 'currency-symbols' ) ); |
|
243 | + return apply_filters('wpinv_currency_symbols', wpinv_get_data('currency-symbols')); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | /** |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | $currency_pos = wpinv_currency_position(); |
253 | 253 | $format = '%1$s%2$s'; |
254 | 254 | |
255 | - switch ( $currency_pos ) { |
|
255 | + switch ($currency_pos) { |
|
256 | 256 | case 'left': |
257 | 257 | $format = '%1$s%2$s'; |
258 | 258 | break; |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | break; |
268 | 268 | } |
269 | 269 | |
270 | - return apply_filters( 'getpaid_price_format', $format, $currency_pos ); |
|
270 | + return apply_filters('getpaid_price_format', $format, $currency_pos); |
|
271 | 271 | } |
272 | 272 | |
273 | 273 | /** |
@@ -277,25 +277,25 @@ discard block |
||
277 | 277 | * @param string $currency Currency. |
278 | 278 | * @return string |
279 | 279 | */ |
280 | -function wpinv_price( $amount = 0, $currency = '' ) { |
|
280 | +function wpinv_price($amount = 0, $currency = '') { |
|
281 | 281 | |
282 | 282 | // Backwards compatibility. |
283 | - $amount = wpinv_sanitize_amount( $amount ); |
|
283 | + $amount = wpinv_sanitize_amount($amount); |
|
284 | 284 | |
285 | 285 | // Prepare variables. |
286 | - $currency = wpinv_get_currency( $currency ); |
|
286 | + $currency = wpinv_get_currency($currency); |
|
287 | 287 | $amount = (float) $amount; |
288 | 288 | $unformatted_amount = $amount; |
289 | 289 | $negative = $amount < 0; |
290 | - $amount = apply_filters( 'getpaid_raw_amount', floatval( $negative ? $amount * -1 : $amount ) ); |
|
291 | - $amount = wpinv_format_amount( $amount ); |
|
290 | + $amount = apply_filters('getpaid_raw_amount', floatval($negative ? $amount * -1 : $amount)); |
|
291 | + $amount = wpinv_format_amount($amount); |
|
292 | 292 | |
293 | 293 | // Format the amount. |
294 | 294 | $format = getpaid_get_price_format(); |
295 | - $formatted_amount = ( $negative ? '-' : '' ) . sprintf( $format, '<span class="getpaid-currency__symbol">' . wpinv_currency_symbol( $currency ) . '</span>', $amount ); |
|
295 | + $formatted_amount = ($negative ? '-' : '') . sprintf($format, '<span class="getpaid-currency__symbol">' . wpinv_currency_symbol($currency) . '</span>', $amount); |
|
296 | 296 | |
297 | 297 | // Filter the formatting. |
298 | - return apply_filters( 'wpinv_price', $formatted_amount, $amount, $currency, $unformatted_amount ); |
|
298 | + return apply_filters('wpinv_price', $formatted_amount, $amount, $currency, $unformatted_amount); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | /** |
@@ -306,25 +306,25 @@ discard block |
||
306 | 306 | * @param bool $calculate Whether or not to apply separators. |
307 | 307 | * @return string |
308 | 308 | */ |
309 | -function wpinv_format_amount( $amount, $decimals = null, $calculate = false ) { |
|
309 | +function wpinv_format_amount($amount, $decimals = null, $calculate = false) { |
|
310 | 310 | $thousands_sep = wpinv_thousands_separator(); |
311 | 311 | $decimal_sep = wpinv_decimal_separator(); |
312 | - $decimals = wpinv_decimals( $decimals ); |
|
313 | - $amount = wpinv_sanitize_amount( $amount ); |
|
312 | + $decimals = wpinv_decimals($decimals); |
|
313 | + $amount = wpinv_sanitize_amount($amount); |
|
314 | 314 | |
315 | - if ( $calculate ) { |
|
315 | + if ($calculate) { |
|
316 | 316 | return $amount; |
317 | 317 | } |
318 | 318 | |
319 | 319 | // Fomart the amount. |
320 | - return number_format( $amount, $decimals, $decimal_sep, $thousands_sep ); |
|
320 | + return number_format($amount, $decimals, $decimal_sep, $thousands_sep); |
|
321 | 321 | } |
322 | 322 | |
323 | -function wpinv_sanitize_key( $key ) { |
|
323 | +function wpinv_sanitize_key($key) { |
|
324 | 324 | $raw_key = $key; |
325 | - $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key ); |
|
325 | + $key = preg_replace('/[^a-zA-Z0-9_\-\.\:\/]/', '', $key); |
|
326 | 326 | |
327 | - return apply_filters( 'wpinv_sanitize_key', $key, $raw_key ); |
|
327 | + return apply_filters('wpinv_sanitize_key', $key, $raw_key); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | /** |
@@ -332,8 +332,8 @@ discard block |
||
332 | 332 | * |
333 | 333 | * @param $str the file whose extension should be retrieved. |
334 | 334 | */ |
335 | -function wpinv_get_file_extension( $str ) { |
|
336 | - $filetype = wp_check_filetype( $str ); |
|
335 | +function wpinv_get_file_extension($str) { |
|
336 | + $filetype = wp_check_filetype($str); |
|
337 | 337 | return $filetype['ext']; |
338 | 338 | } |
339 | 339 | |
@@ -342,16 +342,16 @@ discard block |
||
342 | 342 | * |
343 | 343 | * @param string $string |
344 | 344 | */ |
345 | -function wpinv_string_is_image_url( $string ) { |
|
346 | - $extension = strtolower( wpinv_get_file_extension( $string ) ); |
|
347 | - return in_array( $extension, array( 'jpeg', 'jpg', 'png', 'gif', 'ico' ), true ); |
|
345 | +function wpinv_string_is_image_url($string) { |
|
346 | + $extension = strtolower(wpinv_get_file_extension($string)); |
|
347 | + return in_array($extension, array('jpeg', 'jpg', 'png', 'gif', 'ico'), true); |
|
348 | 348 | } |
349 | 349 | |
350 | 350 | /** |
351 | 351 | * Returns the current URL. |
352 | 352 | */ |
353 | 353 | function wpinv_get_current_page_url() { |
354 | - return ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
354 | + return (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
@@ -361,46 +361,46 @@ discard block |
||
361 | 361 | * @param string $name Constant name. |
362 | 362 | * @param mixed $value Value. |
363 | 363 | */ |
364 | -function getpaid_maybe_define_constant( $name, $value ) { |
|
365 | - if ( ! defined( $name ) ) { |
|
366 | - define( $name, $value ); |
|
364 | +function getpaid_maybe_define_constant($name, $value) { |
|
365 | + if (!defined($name)) { |
|
366 | + define($name, $value); |
|
367 | 367 | } |
368 | 368 | } |
369 | 369 | |
370 | 370 | function wpinv_get_php_arg_separator_output() { |
371 | - return ini_get( 'arg_separator.output' ); |
|
371 | + return ini_get('arg_separator.output'); |
|
372 | 372 | } |
373 | 373 | |
374 | -function wpinv_rgb_from_hex( $color ) { |
|
375 | - $color = str_replace( '#', '', $color ); |
|
374 | +function wpinv_rgb_from_hex($color) { |
|
375 | + $color = str_replace('#', '', $color); |
|
376 | 376 | |
377 | 377 | // Convert shorthand colors to full format, e.g. "FFF" -> "FFFFFF" |
378 | - $color = preg_replace( '~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color ); |
|
379 | - if ( empty( $color ) ) { |
|
378 | + $color = preg_replace('~^(.)(.)(.)$~', '$1$1$2$2$3$3', $color); |
|
379 | + if (empty($color)) { |
|
380 | 380 | return NULL; |
381 | 381 | } |
382 | 382 | |
383 | - $color = str_split( $color ); |
|
383 | + $color = str_split($color); |
|
384 | 384 | |
385 | 385 | $rgb = array(); |
386 | - $rgb['R'] = hexdec( $color[0] . $color[1] ); |
|
387 | - $rgb['G'] = hexdec( $color[2] . $color[3] ); |
|
388 | - $rgb['B'] = hexdec( $color[4] . $color[5] ); |
|
386 | + $rgb['R'] = hexdec($color[0] . $color[1]); |
|
387 | + $rgb['G'] = hexdec($color[2] . $color[3]); |
|
388 | + $rgb['B'] = hexdec($color[4] . $color[5]); |
|
389 | 389 | |
390 | 390 | return $rgb; |
391 | 391 | } |
392 | 392 | |
393 | -function wpinv_hex_darker( $color, $factor = 30 ) { |
|
394 | - $base = wpinv_rgb_from_hex( $color ); |
|
393 | +function wpinv_hex_darker($color, $factor = 30) { |
|
394 | + $base = wpinv_rgb_from_hex($color); |
|
395 | 395 | $color = '#'; |
396 | 396 | |
397 | - foreach ( $base as $k => $v ) { |
|
397 | + foreach ($base as $k => $v) { |
|
398 | 398 | $amount = $v / 100; |
399 | - $amount = round( $amount * $factor ); |
|
399 | + $amount = round($amount * $factor); |
|
400 | 400 | $new_decimal = $v - $amount; |
401 | 401 | |
402 | - $new_hex_component = dechex( $new_decimal ); |
|
403 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
402 | + $new_hex_component = dechex($new_decimal); |
|
403 | + if (strlen($new_hex_component) < 2) { |
|
404 | 404 | $new_hex_component = "0" . $new_hex_component; |
405 | 405 | } |
406 | 406 | $color .= $new_hex_component; |
@@ -409,18 +409,18 @@ discard block |
||
409 | 409 | return $color; |
410 | 410 | } |
411 | 411 | |
412 | -function wpinv_hex_lighter( $color, $factor = 30 ) { |
|
413 | - $base = wpinv_rgb_from_hex( $color ); |
|
412 | +function wpinv_hex_lighter($color, $factor = 30) { |
|
413 | + $base = wpinv_rgb_from_hex($color); |
|
414 | 414 | $color = '#'; |
415 | 415 | |
416 | - foreach ( $base as $k => $v ) { |
|
416 | + foreach ($base as $k => $v) { |
|
417 | 417 | $amount = 255 - $v; |
418 | 418 | $amount = $amount / 100; |
419 | - $amount = round( $amount * $factor ); |
|
419 | + $amount = round($amount * $factor); |
|
420 | 420 | $new_decimal = $v + $amount; |
421 | 421 | |
422 | - $new_hex_component = dechex( $new_decimal ); |
|
423 | - if ( strlen( $new_hex_component ) < 2 ) { |
|
422 | + $new_hex_component = dechex($new_decimal); |
|
423 | + if (strlen($new_hex_component) < 2) { |
|
424 | 424 | $new_hex_component = "0" . $new_hex_component; |
425 | 425 | } |
426 | 426 | $color .= $new_hex_component; |
@@ -429,22 +429,22 @@ discard block |
||
429 | 429 | return $color; |
430 | 430 | } |
431 | 431 | |
432 | -function wpinv_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
|
433 | - $hex = str_replace( '#', '', $color ); |
|
432 | +function wpinv_light_or_dark($color, $dark = '#000000', $light = '#FFFFFF') { |
|
433 | + $hex = str_replace('#', '', $color); |
|
434 | 434 | |
435 | - $c_r = hexdec( substr( $hex, 0, 2 ) ); |
|
436 | - $c_g = hexdec( substr( $hex, 2, 2 ) ); |
|
437 | - $c_b = hexdec( substr( $hex, 4, 2 ) ); |
|
435 | + $c_r = hexdec(substr($hex, 0, 2)); |
|
436 | + $c_g = hexdec(substr($hex, 2, 2)); |
|
437 | + $c_b = hexdec(substr($hex, 4, 2)); |
|
438 | 438 | |
439 | - $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
|
439 | + $brightness = (($c_r * 299) + ($c_g * 587) + ($c_b * 114)) / 1000; |
|
440 | 440 | |
441 | 441 | return $brightness > 155 ? $dark : $light; |
442 | 442 | } |
443 | 443 | |
444 | -function wpinv_format_hex( $hex ) { |
|
445 | - $hex = trim( str_replace( '#', '', $hex ) ); |
|
444 | +function wpinv_format_hex($hex) { |
|
445 | + $hex = trim(str_replace('#', '', $hex)); |
|
446 | 446 | |
447 | - if ( strlen( $hex ) == 3 ) { |
|
447 | + if (strlen($hex) == 3) { |
|
448 | 448 | $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
449 | 449 | } |
450 | 450 | |
@@ -464,12 +464,12 @@ discard block |
||
464 | 464 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
465 | 465 | * @return string |
466 | 466 | */ |
467 | -function wpinv_utf8_strimwidth( $str, $start, $width, $trimmaker = '', $encoding = 'UTF-8' ) { |
|
468 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
469 | - return mb_strimwidth( $str, $start, $width, $trimmaker, $encoding ); |
|
467 | +function wpinv_utf8_strimwidth($str, $start, $width, $trimmaker = '', $encoding = 'UTF-8') { |
|
468 | + if (function_exists('mb_strimwidth')) { |
|
469 | + return mb_strimwidth($str, $start, $width, $trimmaker, $encoding); |
|
470 | 470 | } |
471 | 471 | |
472 | - return wpinv_utf8_substr( $str, $start, $width, $encoding ) . $trimmaker; |
|
472 | + return wpinv_utf8_substr($str, $start, $width, $encoding) . $trimmaker; |
|
473 | 473 | } |
474 | 474 | |
475 | 475 | /** |
@@ -481,28 +481,28 @@ discard block |
||
481 | 481 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
482 | 482 | * @return int Returns the number of characters in string. |
483 | 483 | */ |
484 | -function wpinv_utf8_strlen( $str, $encoding = 'UTF-8' ) { |
|
485 | - if ( function_exists( 'mb_strlen' ) ) { |
|
486 | - return mb_strlen( $str, $encoding ); |
|
484 | +function wpinv_utf8_strlen($str, $encoding = 'UTF-8') { |
|
485 | + if (function_exists('mb_strlen')) { |
|
486 | + return mb_strlen($str, $encoding); |
|
487 | 487 | } |
488 | 488 | |
489 | - return strlen( $str ); |
|
489 | + return strlen($str); |
|
490 | 490 | } |
491 | 491 | |
492 | -function wpinv_utf8_strtolower( $str, $encoding = 'UTF-8' ) { |
|
493 | - if ( function_exists( 'mb_strtolower' ) ) { |
|
494 | - return mb_strtolower( $str, $encoding ); |
|
492 | +function wpinv_utf8_strtolower($str, $encoding = 'UTF-8') { |
|
493 | + if (function_exists('mb_strtolower')) { |
|
494 | + return mb_strtolower($str, $encoding); |
|
495 | 495 | } |
496 | 496 | |
497 | - return strtolower( $str ); |
|
497 | + return strtolower($str); |
|
498 | 498 | } |
499 | 499 | |
500 | -function wpinv_utf8_strtoupper( $str, $encoding = 'UTF-8' ) { |
|
501 | - if ( function_exists( 'mb_strtoupper' ) ) { |
|
502 | - return mb_strtoupper( $str, $encoding ); |
|
500 | +function wpinv_utf8_strtoupper($str, $encoding = 'UTF-8') { |
|
501 | + if (function_exists('mb_strtoupper')) { |
|
502 | + return mb_strtoupper($str, $encoding); |
|
503 | 503 | } |
504 | 504 | |
505 | - return strtoupper( $str ); |
|
505 | + return strtoupper($str); |
|
506 | 506 | } |
507 | 507 | |
508 | 508 | /** |
@@ -516,12 +516,12 @@ discard block |
||
516 | 516 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
517 | 517 | * @return int Returns the position of the first occurrence of search in the string. |
518 | 518 | */ |
519 | -function wpinv_utf8_strpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
520 | - if ( function_exists( 'mb_strpos' ) ) { |
|
521 | - return mb_strpos( $str, $find, $offset, $encoding ); |
|
519 | +function wpinv_utf8_strpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
520 | + if (function_exists('mb_strpos')) { |
|
521 | + return mb_strpos($str, $find, $offset, $encoding); |
|
522 | 522 | } |
523 | 523 | |
524 | - return strpos( $str, $find, $offset ); |
|
524 | + return strpos($str, $find, $offset); |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | /** |
@@ -535,12 +535,12 @@ discard block |
||
535 | 535 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
536 | 536 | * @return int Returns the position of the last occurrence of search. |
537 | 537 | */ |
538 | -function wpinv_utf8_strrpos( $str, $find, $offset = 0, $encoding = 'UTF-8' ) { |
|
539 | - if ( function_exists( 'mb_strrpos' ) ) { |
|
540 | - return mb_strrpos( $str, $find, $offset, $encoding ); |
|
538 | +function wpinv_utf8_strrpos($str, $find, $offset = 0, $encoding = 'UTF-8') { |
|
539 | + if (function_exists('mb_strrpos')) { |
|
540 | + return mb_strrpos($str, $find, $offset, $encoding); |
|
541 | 541 | } |
542 | 542 | |
543 | - return strrpos( $str, $find, $offset ); |
|
543 | + return strrpos($str, $find, $offset); |
|
544 | 544 | } |
545 | 545 | |
546 | 546 | /** |
@@ -555,16 +555,16 @@ discard block |
||
555 | 555 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
556 | 556 | * @return string |
557 | 557 | */ |
558 | -function wpinv_utf8_substr( $str, $start, $length = null, $encoding = 'UTF-8' ) { |
|
559 | - if ( function_exists( 'mb_substr' ) ) { |
|
560 | - if ( $length === null ) { |
|
561 | - return mb_substr( $str, $start, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
558 | +function wpinv_utf8_substr($str, $start, $length = null, $encoding = 'UTF-8') { |
|
559 | + if (function_exists('mb_substr')) { |
|
560 | + if ($length === null) { |
|
561 | + return mb_substr($str, $start, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
562 | 562 | } else { |
563 | - return mb_substr( $str, $start, $length, $encoding ); |
|
563 | + return mb_substr($str, $start, $length, $encoding); |
|
564 | 564 | } |
565 | 565 | } |
566 | 566 | |
567 | - return substr( $str, $start, $length ); |
|
567 | + return substr($str, $start, $length); |
|
568 | 568 | } |
569 | 569 | |
570 | 570 | /** |
@@ -576,48 +576,48 @@ discard block |
||
576 | 576 | * @param string $encoding The encoding parameter is the character encoding. Default "UTF-8". |
577 | 577 | * @return string The width of string. |
578 | 578 | */ |
579 | -function wpinv_utf8_strwidth( $str, $encoding = 'UTF-8' ) { |
|
580 | - if ( function_exists( 'mb_strwidth' ) ) { |
|
581 | - return mb_strwidth( $str, $encoding ); |
|
579 | +function wpinv_utf8_strwidth($str, $encoding = 'UTF-8') { |
|
580 | + if (function_exists('mb_strwidth')) { |
|
581 | + return mb_strwidth($str, $encoding); |
|
582 | 582 | } |
583 | 583 | |
584 | - return wpinv_utf8_strlen( $str, $encoding ); |
|
584 | + return wpinv_utf8_strlen($str, $encoding); |
|
585 | 585 | } |
586 | 586 | |
587 | -function wpinv_utf8_ucfirst( $str, $lower_str_end = false, $encoding = 'UTF-8' ) { |
|
588 | - if ( function_exists( 'mb_strlen' ) ) { |
|
589 | - $first_letter = wpinv_utf8_strtoupper( wpinv_utf8_substr( $str, 0, 1, $encoding ), $encoding ); |
|
587 | +function wpinv_utf8_ucfirst($str, $lower_str_end = false, $encoding = 'UTF-8') { |
|
588 | + if (function_exists('mb_strlen')) { |
|
589 | + $first_letter = wpinv_utf8_strtoupper(wpinv_utf8_substr($str, 0, 1, $encoding), $encoding); |
|
590 | 590 | $str_end = ""; |
591 | 591 | |
592 | - if ( $lower_str_end ) { |
|
593 | - $str_end = wpinv_utf8_strtolower( wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ), $encoding ); |
|
592 | + if ($lower_str_end) { |
|
593 | + $str_end = wpinv_utf8_strtolower(wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding), $encoding); |
|
594 | 594 | } else { |
595 | - $str_end = wpinv_utf8_substr( $str, 1, wpinv_utf8_strlen( $str, $encoding ), $encoding ); |
|
595 | + $str_end = wpinv_utf8_substr($str, 1, wpinv_utf8_strlen($str, $encoding), $encoding); |
|
596 | 596 | } |
597 | 597 | |
598 | 598 | return $first_letter . $str_end; |
599 | 599 | } |
600 | 600 | |
601 | - return ucfirst( $str ); |
|
601 | + return ucfirst($str); |
|
602 | 602 | } |
603 | 603 | |
604 | -function wpinv_utf8_ucwords( $str, $encoding = 'UTF-8' ) { |
|
605 | - if ( function_exists( 'mb_convert_case' ) ) { |
|
606 | - return mb_convert_case( $str, MB_CASE_TITLE, $encoding ); |
|
604 | +function wpinv_utf8_ucwords($str, $encoding = 'UTF-8') { |
|
605 | + if (function_exists('mb_convert_case')) { |
|
606 | + return mb_convert_case($str, MB_CASE_TITLE, $encoding); |
|
607 | 607 | } |
608 | 608 | |
609 | - return ucwords( $str ); |
|
609 | + return ucwords($str); |
|
610 | 610 | } |
611 | 611 | |
612 | -function wpinv_period_in_days( $period, $unit ) { |
|
613 | - $period = absint( $period ); |
|
612 | +function wpinv_period_in_days($period, $unit) { |
|
613 | + $period = absint($period); |
|
614 | 614 | |
615 | - if ( $period > 0 ) { |
|
616 | - if ( in_array( strtolower( $unit ), array( 'w', 'week', 'weeks' ) ) ) { |
|
615 | + if ($period > 0) { |
|
616 | + if (in_array(strtolower($unit), array('w', 'week', 'weeks'))) { |
|
617 | 617 | $period = $period * 7; |
618 | - } else if ( in_array( strtolower( $unit ), array( 'm', 'month', 'months' ) ) ) { |
|
618 | + } else if (in_array(strtolower($unit), array('m', 'month', 'months'))) { |
|
619 | 619 | $period = $period * 30; |
620 | - } else if ( in_array( strtolower( $unit ), array( 'y', 'year', 'years' ) ) ) { |
|
620 | + } else if (in_array(strtolower($unit), array('y', 'year', 'years'))) { |
|
621 | 621 | $period = $period * 365; |
622 | 622 | } |
623 | 623 | } |
@@ -625,14 +625,14 @@ discard block |
||
625 | 625 | return $period; |
626 | 626 | } |
627 | 627 | |
628 | -function wpinv_cal_days_in_month( $calendar, $month, $year ) { |
|
629 | - if ( function_exists( 'cal_days_in_month' ) ) { |
|
630 | - return cal_days_in_month( $calendar, $month, $year ); |
|
628 | +function wpinv_cal_days_in_month($calendar, $month, $year) { |
|
629 | + if (function_exists('cal_days_in_month')) { |
|
630 | + return cal_days_in_month($calendar, $month, $year); |
|
631 | 631 | } |
632 | 632 | |
633 | 633 | // Fallback in case the calendar extension is not loaded in PHP |
634 | 634 | // Only supports Gregorian calendar |
635 | - return date( 't', mktime( 0, 0, 0, $month, 1, $year ) ); |
|
635 | + return date('t', mktime(0, 0, 0, $month, 1, $year)); |
|
636 | 636 | } |
637 | 637 | |
638 | 638 | /** |
@@ -643,12 +643,12 @@ discard block |
||
643 | 643 | * |
644 | 644 | * @return string |
645 | 645 | */ |
646 | -function wpi_help_tip( $tip, $allow_html = false ) { |
|
646 | +function wpi_help_tip($tip, $allow_html = false) { |
|
647 | 647 | |
648 | - if ( $allow_html ) { |
|
649 | - $tip = wpi_sanitize_tooltip( $tip ); |
|
648 | + if ($allow_html) { |
|
649 | + $tip = wpi_sanitize_tooltip($tip); |
|
650 | 650 | } else { |
651 | - $tip = esc_attr( $tip ); |
|
651 | + $tip = esc_attr($tip); |
|
652 | 652 | } |
653 | 653 | |
654 | 654 | return '<span class="wpi-help-tip dashicons dashicons-editor-help" title="' . $tip . '"></span>'; |
@@ -662,8 +662,8 @@ discard block |
||
662 | 662 | * @param string $var |
663 | 663 | * @return string |
664 | 664 | */ |
665 | -function wpi_sanitize_tooltip( $var ) { |
|
666 | - return wp_kses( html_entity_decode( $var ), array( |
|
665 | +function wpi_sanitize_tooltip($var) { |
|
666 | + return wp_kses(html_entity_decode($var), array( |
|
667 | 667 | 'br' => array(), |
668 | 668 | 'em' => array(), |
669 | 669 | 'strong' => array(), |
@@ -674,7 +674,7 @@ discard block |
||
674 | 674 | 'li' => array(), |
675 | 675 | 'ol' => array(), |
676 | 676 | 'p' => array(), |
677 | - ) ); |
|
677 | + )); |
|
678 | 678 | } |
679 | 679 | |
680 | 680 | /** |
@@ -684,7 +684,7 @@ discard block |
||
684 | 684 | */ |
685 | 685 | function wpinv_get_screen_ids() { |
686 | 686 | |
687 | - $screen_id = sanitize_title( __( 'Invoicing', 'invoicing' ) ); |
|
687 | + $screen_id = sanitize_title(__('Invoicing', 'invoicing')); |
|
688 | 688 | |
689 | 689 | $screen_ids = array( |
690 | 690 | 'toplevel_page_' . $screen_id, |
@@ -705,7 +705,7 @@ discard block |
||
705 | 705 | 'getpaid_page_wpinv-customers', |
706 | 706 | ); |
707 | 707 | |
708 | - return apply_filters( 'wpinv_screen_ids', $screen_ids ); |
|
708 | + return apply_filters('wpinv_screen_ids', $screen_ids); |
|
709 | 709 | } |
710 | 710 | |
711 | 711 | /** |
@@ -716,14 +716,14 @@ discard block |
||
716 | 716 | * @param array|string $list List of values. |
717 | 717 | * @return array Sanitized array of values. |
718 | 718 | */ |
719 | -function wpinv_parse_list( $list ) { |
|
719 | +function wpinv_parse_list($list) { |
|
720 | 720 | |
721 | - if ( empty( $list ) ) { |
|
721 | + if (empty($list)) { |
|
722 | 722 | $list = array(); |
723 | 723 | } |
724 | 724 | |
725 | - if ( ! is_array( $list ) ) { |
|
726 | - return preg_split( '/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY ); |
|
725 | + if (!is_array($list)) { |
|
726 | + return preg_split('/[\s,]+/', $list, -1, PREG_SPLIT_NO_EMPTY); |
|
727 | 727 | } |
728 | 728 | |
729 | 729 | return $list; |
@@ -737,16 +737,16 @@ discard block |
||
737 | 737 | * @param string $key Type of data to fetch. |
738 | 738 | * @return mixed Fetched data. |
739 | 739 | */ |
740 | -function wpinv_get_data( $key ) { |
|
740 | +function wpinv_get_data($key) { |
|
741 | 741 | |
742 | 742 | // Try fetching it from the cache. |
743 | - $data = wp_cache_get( "wpinv-data-$key", 'wpinv' ); |
|
744 | - if( $data ) { |
|
743 | + $data = wp_cache_get("wpinv-data-$key", 'wpinv'); |
|
744 | + if ($data) { |
|
745 | 745 | return $data; |
746 | 746 | } |
747 | 747 | |
748 | - $data = apply_filters( "wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php" ); |
|
749 | - wp_cache_set( "wpinv-data-$key", $data, 'wpinv' ); |
|
748 | + $data = apply_filters("wpinv_get_$key", include WPINV_PLUGIN_DIR . "includes/data/$key.php"); |
|
749 | + wp_cache_set("wpinv-data-$key", $data, 'wpinv'); |
|
750 | 750 | |
751 | 751 | return $data; |
752 | 752 | } |
@@ -760,10 +760,10 @@ discard block |
||
760 | 760 | * @param bool $first_empty Whether or not the first item in the list should be empty |
761 | 761 | * @return mixed Fetched data. |
762 | 762 | */ |
763 | -function wpinv_maybe_add_empty_option( $options, $first_empty ) { |
|
763 | +function wpinv_maybe_add_empty_option($options, $first_empty) { |
|
764 | 764 | |
765 | - if ( ! empty( $options ) && $first_empty ) { |
|
766 | - return array_merge( array( '' => '' ), $options ); |
|
765 | + if (!empty($options) && $first_empty) { |
|
766 | + return array_merge(array('' => ''), $options); |
|
767 | 767 | } |
768 | 768 | return $options; |
769 | 769 | |
@@ -775,21 +775,21 @@ discard block |
||
775 | 775 | * @param mixed $var Data to sanitize. |
776 | 776 | * @return string|array |
777 | 777 | */ |
778 | -function wpinv_clean( $var ) { |
|
778 | +function wpinv_clean($var) { |
|
779 | 779 | |
780 | - if ( is_array( $var ) ) { |
|
781 | - return array_map( 'wpinv_clean', $var ); |
|
780 | + if (is_array($var)) { |
|
781 | + return array_map('wpinv_clean', $var); |
|
782 | 782 | } |
783 | 783 | |
784 | - if ( is_object( $var ) ) { |
|
785 | - $object_vars = get_object_vars( $var ); |
|
786 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
787 | - $var->$property_name = wpinv_clean( $property_value ); |
|
784 | + if (is_object($var)) { |
|
785 | + $object_vars = get_object_vars($var); |
|
786 | + foreach ($object_vars as $property_name => $property_value) { |
|
787 | + $var->$property_name = wpinv_clean($property_value); |
|
788 | 788 | } |
789 | 789 | return $var; |
790 | 790 | } |
791 | 791 | |
792 | - return is_string( $var ) ? sanitize_text_field( stripslashes( $var ) ) : $var; |
|
792 | + return is_string($var) ? sanitize_text_field(stripslashes($var)) : $var; |
|
793 | 793 | } |
794 | 794 | |
795 | 795 | /** |
@@ -798,43 +798,43 @@ discard block |
||
798 | 798 | * @param string $str Data to convert. |
799 | 799 | * @return string|array |
800 | 800 | */ |
801 | -function getpaid_convert_price_string_to_options( $str ) { |
|
801 | +function getpaid_convert_price_string_to_options($str) { |
|
802 | 802 | |
803 | - $raw_options = array_map( 'trim', explode( ',', $str ) ); |
|
804 | - $options = array(); |
|
803 | + $raw_options = array_map('trim', explode(',', $str)); |
|
804 | + $options = array(); |
|
805 | 805 | |
806 | - foreach ( $raw_options as $option ) { |
|
806 | + foreach ($raw_options as $option) { |
|
807 | 807 | |
808 | - if ( '' == $option ) { |
|
808 | + if ('' == $option) { |
|
809 | 809 | continue; |
810 | 810 | } |
811 | 811 | |
812 | - $option = array_map( 'trim', explode( '|', $option ) ); |
|
812 | + $option = array_map('trim', explode('|', $option)); |
|
813 | 813 | |
814 | 814 | $price = null; |
815 | 815 | $label = null; |
816 | 816 | |
817 | - if ( isset( $option[0] ) && '' != $option[0] ) { |
|
818 | - $label = $option[0]; |
|
817 | + if (isset($option[0]) && '' != $option[0]) { |
|
818 | + $label = $option[0]; |
|
819 | 819 | } |
820 | 820 | |
821 | - if ( isset( $option[1] ) && '' != $option[1] ) { |
|
821 | + if (isset($option[1]) && '' != $option[1]) { |
|
822 | 822 | $price = $option[1]; |
823 | 823 | } |
824 | 824 | |
825 | - if ( ! isset( $price ) ) { |
|
825 | + if (!isset($price)) { |
|
826 | 826 | $price = $label; |
827 | 827 | } |
828 | 828 | |
829 | - if ( ! isset( $price ) || ! is_numeric( $price ) ) { |
|
829 | + if (!isset($price) || !is_numeric($price)) { |
|
830 | 830 | continue; |
831 | 831 | } |
832 | 832 | |
833 | - if ( ! isset( $label ) ) { |
|
833 | + if (!isset($label)) { |
|
834 | 834 | $label = $price; |
835 | 835 | } |
836 | 836 | |
837 | - $options[ "$label|$price" ] = $label; |
|
837 | + $options["$label|$price"] = $label; |
|
838 | 838 | } |
839 | 839 | |
840 | 840 | return $options; |
@@ -843,27 +843,27 @@ discard block |
||
843 | 843 | /** |
844 | 844 | * Returns the help tip. |
845 | 845 | */ |
846 | -function getpaid_get_help_tip( $tip, $additional_classes = '' ) { |
|
847 | - $additional_classes = sanitize_html_class( $additional_classes ); |
|
848 | - $tip = esc_attr__( $tip ); |
|
846 | +function getpaid_get_help_tip($tip, $additional_classes = '') { |
|
847 | + $additional_classes = sanitize_html_class($additional_classes); |
|
848 | + $tip = esc_attr__($tip); |
|
849 | 849 | return "<span class='wpi-help-tip dashicons dashicons-editor-help $additional_classes' title='$tip'></span>"; |
850 | 850 | } |
851 | 851 | |
852 | 852 | /** |
853 | 853 | * Formats a date |
854 | 854 | */ |
855 | -function getpaid_format_date( $date, $with_time = false ) { |
|
855 | +function getpaid_format_date($date, $with_time = false) { |
|
856 | 856 | |
857 | - if ( empty( $date ) || $date == '0000-00-00 00:00:00' ) { |
|
857 | + if (empty($date) || $date == '0000-00-00 00:00:00') { |
|
858 | 858 | return ''; |
859 | 859 | } |
860 | 860 | |
861 | 861 | $format = getpaid_date_format(); |
862 | 862 | |
863 | - if ( $with_time ) { |
|
863 | + if ($with_time) { |
|
864 | 864 | $format .= ' ' . getpaid_time_format(); |
865 | 865 | } |
866 | - return date_i18n( $format, strtotime( $date ) ); |
|
866 | + return date_i18n($format, strtotime($date)); |
|
867 | 867 | |
868 | 868 | } |
869 | 869 | |
@@ -872,9 +872,9 @@ discard block |
||
872 | 872 | * |
873 | 873 | * @return string |
874 | 874 | */ |
875 | -function getpaid_format_date_value( $date, $default = "—", $with_time = false ) { |
|
876 | - $date = getpaid_format_date( $date, $with_time ); |
|
877 | - return empty( $date ) ? $default : $date; |
|
875 | +function getpaid_format_date_value($date, $default = "—", $with_time = false) { |
|
876 | + $date = getpaid_format_date($date, $with_time); |
|
877 | + return empty($date) ? $default : $date; |
|
878 | 878 | } |
879 | 879 | |
880 | 880 | /** |
@@ -883,7 +883,7 @@ discard block |
||
883 | 883 | * @return string |
884 | 884 | */ |
885 | 885 | function getpaid_date_format() { |
886 | - return apply_filters( 'getpaid_date_format', get_option( 'date_format' ) ); |
|
886 | + return apply_filters('getpaid_date_format', get_option('date_format')); |
|
887 | 887 | } |
888 | 888 | |
889 | 889 | /** |
@@ -892,7 +892,7 @@ discard block |
||
892 | 892 | * @return string |
893 | 893 | */ |
894 | 894 | function getpaid_time_format() { |
895 | - return apply_filters( 'getpaid_time_format', get_option( 'time_format' ) ); |
|
895 | + return apply_filters('getpaid_time_format', get_option('time_format')); |
|
896 | 896 | } |
897 | 897 | |
898 | 898 | /** |
@@ -902,16 +902,16 @@ discard block |
||
902 | 902 | * @param integer $limit Limit size in characters. |
903 | 903 | * @return string |
904 | 904 | */ |
905 | -function getpaid_limit_length( $string, $limit ) { |
|
905 | +function getpaid_limit_length($string, $limit) { |
|
906 | 906 | $str_limit = $limit - 3; |
907 | 907 | |
908 | - if ( function_exists( 'mb_strimwidth' ) ) { |
|
909 | - if ( mb_strlen( $string ) > $limit ) { |
|
910 | - $string = mb_strimwidth( $string, 0, $str_limit ) . '...'; |
|
908 | + if (function_exists('mb_strimwidth')) { |
|
909 | + if (mb_strlen($string) > $limit) { |
|
910 | + $string = mb_strimwidth($string, 0, $str_limit) . '...'; |
|
911 | 911 | } |
912 | 912 | } else { |
913 | - if ( strlen( $string ) > $limit ) { |
|
914 | - $string = substr( $string, 0, $str_limit ) . '...'; |
|
913 | + if (strlen($string) > $limit) { |
|
914 | + $string = substr($string, 0, $str_limit) . '...'; |
|
915 | 915 | } |
916 | 916 | } |
917 | 917 | return $string; |
@@ -925,7 +925,7 @@ discard block |
||
925 | 925 | * @since 1.0.19 |
926 | 926 | */ |
927 | 927 | function getpaid_api() { |
928 | - return getpaid()->get( 'api' ); |
|
928 | + return getpaid()->get('api'); |
|
929 | 929 | } |
930 | 930 | |
931 | 931 | /** |
@@ -935,7 +935,7 @@ discard block |
||
935 | 935 | * @since 1.0.19 |
936 | 936 | */ |
937 | 937 | function getpaid_post_types() { |
938 | - return getpaid()->get( 'post_types' ); |
|
938 | + return getpaid()->get('post_types'); |
|
939 | 939 | } |
940 | 940 | |
941 | 941 | /** |
@@ -945,7 +945,7 @@ discard block |
||
945 | 945 | * @since 1.0.19 |
946 | 946 | */ |
947 | 947 | function getpaid_session() { |
948 | - return getpaid()->get( 'session' ); |
|
948 | + return getpaid()->get('session'); |
|
949 | 949 | } |
950 | 950 | |
951 | 951 | /** |
@@ -955,7 +955,7 @@ discard block |
||
955 | 955 | * @since 1.0.19 |
956 | 956 | */ |
957 | 957 | function getpaid_notes() { |
958 | - return getpaid()->get( 'notes' ); |
|
958 | + return getpaid()->get('notes'); |
|
959 | 959 | } |
960 | 960 | |
961 | 961 | /** |
@@ -964,7 +964,7 @@ discard block |
||
964 | 964 | * @return GetPaid_Admin |
965 | 965 | */ |
966 | 966 | function getpaid_admin() { |
967 | - return getpaid()->get( 'admin' ); |
|
967 | + return getpaid()->get('admin'); |
|
968 | 968 | } |
969 | 969 | |
970 | 970 | /** |
@@ -974,8 +974,8 @@ discard block |
||
974 | 974 | * @param string $base the base url |
975 | 975 | * @return string |
976 | 976 | */ |
977 | -function getpaid_get_authenticated_action_url( $action, $base = false ) { |
|
978 | - return wp_nonce_url( add_query_arg( 'getpaid-action', $action, $base ), 'getpaid-nonce', 'getpaid-nonce' ); |
|
977 | +function getpaid_get_authenticated_action_url($action, $base = false) { |
|
978 | + return wp_nonce_url(add_query_arg('getpaid-action', $action, $base), 'getpaid-nonce', 'getpaid-nonce'); |
|
979 | 979 | } |
980 | 980 | |
981 | 981 | /** |
@@ -983,11 +983,11 @@ discard block |
||
983 | 983 | * |
984 | 984 | * @return string |
985 | 985 | */ |
986 | -function getpaid_get_post_type_label( $post_type, $plural = true ) { |
|
986 | +function getpaid_get_post_type_label($post_type, $plural = true) { |
|
987 | 987 | |
988 | - $post_type = get_post_type_object( $post_type ); |
|
988 | + $post_type = get_post_type_object($post_type); |
|
989 | 989 | |
990 | - if ( ! is_object( $post_type ) ) { |
|
990 | + if (!is_object($post_type)) { |
|
991 | 991 | return null; |
992 | 992 | } |
993 | 993 | |
@@ -1000,18 +1000,18 @@ discard block |
||
1000 | 1000 | * |
1001 | 1001 | * @return mixed|null |
1002 | 1002 | */ |
1003 | -function getpaid_get_array_field( $array, $key, $secondary_key = null ) { |
|
1003 | +function getpaid_get_array_field($array, $key, $secondary_key = null) { |
|
1004 | 1004 | |
1005 | - if ( ! is_array( $array ) ) { |
|
1005 | + if (!is_array($array)) { |
|
1006 | 1006 | return null; |
1007 | 1007 | } |
1008 | 1008 | |
1009 | - if ( ! empty( $secondary_key ) ) { |
|
1010 | - $array = isset( $array[ $secondary_key ] ) ? $array[ $secondary_key ] : array(); |
|
1011 | - return getpaid_get_array_field( $array, $key ); |
|
1009 | + if (!empty($secondary_key)) { |
|
1010 | + $array = isset($array[$secondary_key]) ? $array[$secondary_key] : array(); |
|
1011 | + return getpaid_get_array_field($array, $key); |
|
1012 | 1012 | } |
1013 | 1013 | |
1014 | - return isset( $array[ $key ] ) ? $array[ $key ] : null; |
|
1014 | + return isset($array[$key]) ? $array[$key] : null; |
|
1015 | 1015 | |
1016 | 1016 | } |
1017 | 1017 | |
@@ -1020,12 +1020,12 @@ discard block |
||
1020 | 1020 | * |
1021 | 1021 | * @return array |
1022 | 1022 | */ |
1023 | -function getpaid_array_merge_if_empty( $args, $defaults ) { |
|
1023 | +function getpaid_array_merge_if_empty($args, $defaults) { |
|
1024 | 1024 | |
1025 | - foreach ( $defaults as $key => $value ) { |
|
1025 | + foreach ($defaults as $key => $value) { |
|
1026 | 1026 | |
1027 | - if ( array_key_exists( $key, $args ) && empty( $args[ $key ] ) ) { |
|
1028 | - $args[ $key ] = $value; |
|
1027 | + if (array_key_exists($key, $args) && empty($args[$key])) { |
|
1028 | + $args[$key] = $value; |
|
1029 | 1029 | } |
1030 | 1030 | |
1031 | 1031 | } |