@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,372 +11,372 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Helper { |
13 | 13 | |
14 | - /** |
|
15 | - * A component helper for generating a input name. |
|
16 | - * |
|
17 | - * @param $text |
|
18 | - * @param $multiple bool If the name is set to be multiple but no brackets found then we add some. |
|
19 | - * |
|
20 | - * @return string |
|
21 | - */ |
|
22 | - public static function name($text,$multiple = false){ |
|
23 | - $output = ''; |
|
24 | - |
|
25 | - if($text){ |
|
26 | - $is_multiple = strpos($text, '[') === false && $multiple ? '[]' : ''; |
|
27 | - $output = ' name="'.esc_attr($text).$is_multiple.'" '; |
|
28 | - } |
|
29 | - |
|
30 | - return $output; |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * A component helper for generating a item id. |
|
35 | - * |
|
36 | - * @param $text string The text to be used as the value. |
|
37 | - * |
|
38 | - * @return string The sanitized item. |
|
39 | - */ |
|
40 | - public static function id($text){ |
|
41 | - $output = ''; |
|
42 | - |
|
43 | - if($text){ |
|
44 | - $output = ' id="'.sanitize_html_class($text).'" '; |
|
45 | - } |
|
46 | - |
|
47 | - return $output; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * A component helper for generating a item title. |
|
52 | - * |
|
53 | - * @param $text string The text to be used as the value. |
|
54 | - * |
|
55 | - * @return string The sanitized item. |
|
56 | - */ |
|
57 | - public static function title($text){ |
|
58 | - $output = ''; |
|
59 | - |
|
60 | - if($text){ |
|
61 | - $output = ' title="'.esc_attr($text).'" '; |
|
62 | - } |
|
63 | - |
|
64 | - return $output; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * A component helper for generating a item value. |
|
69 | - * |
|
70 | - * @param $text string The text to be used as the value. |
|
71 | - * |
|
72 | - * @return string The sanitized item. |
|
73 | - */ |
|
74 | - public static function value($text){ |
|
75 | - $output = ''; |
|
76 | - |
|
77 | - if($text){ |
|
78 | - $output = ' value="'.sanitize_text_field($text).'" '; |
|
79 | - } |
|
80 | - |
|
81 | - return $output; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * A component helper for generating a item class attribute. |
|
86 | - * |
|
87 | - * @param $text string The text to be used as the value. |
|
88 | - * |
|
89 | - * @return string The sanitized item. |
|
90 | - */ |
|
91 | - public static function class_attr($text){ |
|
92 | - $output = ''; |
|
93 | - |
|
94 | - if($text){ |
|
95 | - $classes = self::esc_classes($text); |
|
96 | - if(!empty($classes)){ |
|
97 | - $output = ' class="'.$classes.'" '; |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - return $output; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Escape a string of classes. |
|
106 | - * |
|
107 | - * @param $text |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public static function esc_classes($text){ |
|
112 | - $output = ''; |
|
113 | - |
|
114 | - if($text){ |
|
115 | - $classes = explode(" ",$text); |
|
116 | - $classes = array_map("trim",$classes); |
|
117 | - $classes = array_map("sanitize_html_class",$classes); |
|
118 | - if(!empty($classes)){ |
|
119 | - $output = implode(" ",$classes); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - return $output; |
|
124 | - |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param $args |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public static function data_attributes($args){ |
|
133 | - $output = ''; |
|
134 | - |
|
135 | - if(!empty($args)){ |
|
136 | - |
|
137 | - foreach($args as $key => $val){ |
|
138 | - if(substr( $key, 0, 5 ) === "data-"){ |
|
139 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
140 | - } |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - return $output; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param $args |
|
149 | - * |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - public static function aria_attributes($args){ |
|
153 | - $output = ''; |
|
154 | - |
|
155 | - if(!empty($args)){ |
|
156 | - |
|
157 | - foreach($args as $key => $val){ |
|
158 | - if(substr( $key, 0, 5 ) === "aria-"){ |
|
159 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - return $output; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Build a font awesome icon from a class. |
|
169 | - * |
|
170 | - * @param $class |
|
171 | - * @param bool $space_after |
|
172 | - * @param array $extra_attributes An array of extra attributes. |
|
173 | - * |
|
174 | - * @return string |
|
175 | - */ |
|
176 | - public static function icon($class,$space_after = false, $extra_attributes = array()){ |
|
177 | - $output = ''; |
|
178 | - |
|
179 | - if($class){ |
|
180 | - $classes = self::esc_classes($class); |
|
181 | - if(!empty($classes)){ |
|
182 | - $output = '<i class="'.$classes.'" '; |
|
183 | - // extra attributes |
|
184 | - if(!empty($extra_attributes)){ |
|
185 | - $output .= AUI_Component_Helper::extra_attributes($extra_attributes); |
|
186 | - } |
|
187 | - $output .= '></i>'; |
|
188 | - if($space_after){ |
|
189 | - $output .= " "; |
|
190 | - } |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - return $output; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param $args |
|
199 | - * |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - public static function extra_attributes($args){ |
|
203 | - $output = ''; |
|
204 | - |
|
205 | - if(!empty($args)){ |
|
206 | - |
|
207 | - if( is_array($args) ){ |
|
208 | - foreach($args as $key => $val){ |
|
209 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
210 | - } |
|
211 | - }else{ |
|
212 | - $output .= ' '.$args.' '; |
|
213 | - } |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - return $output; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @param $args |
|
222 | - * |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public static function help_text($text){ |
|
226 | - $output = ''; |
|
227 | - |
|
228 | - if($text){ |
|
229 | - $output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>'; |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - return $output; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Replace element require context with JS. |
|
238 | - * |
|
239 | - * @param $input |
|
240 | - * |
|
241 | - * @return string|void |
|
242 | - */ |
|
243 | - public static function element_require( $input ) { |
|
244 | - |
|
245 | - $input = str_replace( "'", '"', $input );// we only want double quotes |
|
246 | - |
|
247 | - $output = esc_attr( str_replace( array( "[%", "%]", "%:checked]" ), array( |
|
248 | - "jQuery(form).find('[data-argument=\"", |
|
249 | - "\"]').find('input,select,textarea').val()", |
|
250 | - "\"]').find('input:checked').val()", |
|
251 | - ), $input ) ); |
|
252 | - |
|
253 | - if($output){ |
|
254 | - $output = ' data-element-require="'.$output.'" '; |
|
255 | - } |
|
256 | - |
|
257 | - return $output; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Returns an array of allowed HTML tags and attributes for a given context. |
|
262 | - * |
|
263 | - * @since 0.1.41 |
|
264 | - * |
|
265 | - * @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
|
266 | - * 'strip', 'data', 'entities', or the name of a field filter such as |
|
267 | - * 'pre_user_description'. |
|
268 | - * @param array $input Input. |
|
269 | - * @return array Array of allowed HTML tags and their allowed attributes. |
|
270 | - */ |
|
271 | - public static function kses_allowed_html( $context = 'post', $input = array() ) { |
|
272 | - $allowed_html = wp_kses_allowed_html( $context ); |
|
273 | - |
|
274 | - if ( is_array( $allowed_html ) ) { |
|
275 | - // <iframe> |
|
276 | - if ( ! isset( $allowed_html['iframe'] ) && $context == 'post' ) { |
|
277 | - $allowed_html['iframe'] = array( |
|
278 | - 'class' => true, |
|
279 | - 'id' => true, |
|
280 | - 'src' => true, |
|
281 | - 'width' => true, |
|
282 | - 'height' => true, |
|
283 | - 'frameborder' => true, |
|
284 | - 'marginwidth' => true, |
|
285 | - 'marginheight' => true, |
|
286 | - 'scrolling' => true, |
|
287 | - 'style' => true, |
|
288 | - 'title' => true, |
|
289 | - 'allow' => true, |
|
290 | - 'allowfullscreen' => true, |
|
291 | - 'data-*' => true, |
|
292 | - ); |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Filters the allowed html tags. |
|
298 | - * |
|
299 | - * @since 0.1.41 |
|
300 | - * |
|
301 | - * @param array[]|string $allowed_html Allowed html tags. |
|
302 | - * @param @param string|array $context The context for which to retrieve tags. |
|
303 | - * @param array $input Input field. |
|
304 | - */ |
|
305 | - return apply_filters( 'ayecode_ui_kses_allowed_html', $allowed_html, $context, $input ); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Filters content and keeps only allowable HTML elements. |
|
310 | - * |
|
311 | - * This function makes sure that only the allowed HTML element names, attribute |
|
312 | - * names and attribute values plus only sane HTML entities will occur in |
|
313 | - * $string. You have to remove any slashes from PHP's magic quotes before you |
|
314 | - * call this function. |
|
315 | - * |
|
316 | - * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news', |
|
317 | - * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This |
|
318 | - * covers all common link protocols, except for 'javascript' which should not |
|
319 | - * be allowed for untrusted users. |
|
320 | - * |
|
321 | - * @since 0.1.41 |
|
322 | - * |
|
323 | - * @param string|array $value Content to filter through kses. |
|
324 | - * @param array $input Input Field. |
|
325 | - * @return string Filtered content with only allowed HTML elements. |
|
326 | - */ |
|
327 | - public static function _sanitize_html_field( $value, $input = array() ) { |
|
328 | - if ( $value === '' ) { |
|
329 | - return $value; |
|
330 | - } |
|
331 | - |
|
332 | - $allowed_html = self::kses_allowed_html( 'post', $input ); |
|
333 | - |
|
334 | - if ( ! is_array( $allowed_html ) ) { |
|
335 | - $allowed_html = wp_kses_allowed_html( 'post' ); |
|
336 | - } |
|
337 | - |
|
338 | - $filtered = trim( wp_unslash( $value ) ); |
|
339 | - $filtered = wp_kses( $filtered, $allowed_html ); |
|
340 | - $filtered = balanceTags( $filtered ); // Balances tags |
|
341 | - |
|
342 | - return $filtered; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Navigates through an array, object, or scalar, and removes slashes from the values. |
|
347 | - * |
|
348 | - * @since 0.1.41 |
|
349 | - * |
|
350 | - * @param mixed $value The value to be stripped. |
|
351 | - * @param array $input Input Field. |
|
352 | - * @return mixed Stripped value. |
|
353 | - */ |
|
354 | - public static function sanitize_html_field( $value, $input = array() ) { |
|
355 | - $original = $value; |
|
356 | - |
|
357 | - if ( is_array( $value ) ) { |
|
358 | - foreach ( $value as $index => $item ) { |
|
359 | - $value[ $index ] = self::_sanitize_html_field( $value, $input ); |
|
360 | - } |
|
361 | - } elseif ( is_object( $value ) ) { |
|
362 | - $object_vars = get_object_vars( $value ); |
|
363 | - |
|
364 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
365 | - $value->$property_name = self::_sanitize_html_field( $property_value, $input ); |
|
366 | - } |
|
367 | - } else { |
|
368 | - $value = self::_sanitize_html_field( $value, $input ); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Filters content and keeps only allowable HTML elements. |
|
373 | - * |
|
374 | - * @since 0.1.41 |
|
375 | - * |
|
376 | - * @param string|array $value Content to filter through kses. |
|
377 | - * @param string|array $value Original content without filter. |
|
378 | - * @param array $input Input Field. |
|
379 | - */ |
|
380 | - return apply_filters( 'ayecode_ui_sanitize_html_field', $value, $original, $input ); |
|
381 | - } |
|
14 | + /** |
|
15 | + * A component helper for generating a input name. |
|
16 | + * |
|
17 | + * @param $text |
|
18 | + * @param $multiple bool If the name is set to be multiple but no brackets found then we add some. |
|
19 | + * |
|
20 | + * @return string |
|
21 | + */ |
|
22 | + public static function name($text,$multiple = false){ |
|
23 | + $output = ''; |
|
24 | + |
|
25 | + if($text){ |
|
26 | + $is_multiple = strpos($text, '[') === false && $multiple ? '[]' : ''; |
|
27 | + $output = ' name="'.esc_attr($text).$is_multiple.'" '; |
|
28 | + } |
|
29 | + |
|
30 | + return $output; |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * A component helper for generating a item id. |
|
35 | + * |
|
36 | + * @param $text string The text to be used as the value. |
|
37 | + * |
|
38 | + * @return string The sanitized item. |
|
39 | + */ |
|
40 | + public static function id($text){ |
|
41 | + $output = ''; |
|
42 | + |
|
43 | + if($text){ |
|
44 | + $output = ' id="'.sanitize_html_class($text).'" '; |
|
45 | + } |
|
46 | + |
|
47 | + return $output; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * A component helper for generating a item title. |
|
52 | + * |
|
53 | + * @param $text string The text to be used as the value. |
|
54 | + * |
|
55 | + * @return string The sanitized item. |
|
56 | + */ |
|
57 | + public static function title($text){ |
|
58 | + $output = ''; |
|
59 | + |
|
60 | + if($text){ |
|
61 | + $output = ' title="'.esc_attr($text).'" '; |
|
62 | + } |
|
63 | + |
|
64 | + return $output; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * A component helper for generating a item value. |
|
69 | + * |
|
70 | + * @param $text string The text to be used as the value. |
|
71 | + * |
|
72 | + * @return string The sanitized item. |
|
73 | + */ |
|
74 | + public static function value($text){ |
|
75 | + $output = ''; |
|
76 | + |
|
77 | + if($text){ |
|
78 | + $output = ' value="'.sanitize_text_field($text).'" '; |
|
79 | + } |
|
80 | + |
|
81 | + return $output; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * A component helper for generating a item class attribute. |
|
86 | + * |
|
87 | + * @param $text string The text to be used as the value. |
|
88 | + * |
|
89 | + * @return string The sanitized item. |
|
90 | + */ |
|
91 | + public static function class_attr($text){ |
|
92 | + $output = ''; |
|
93 | + |
|
94 | + if($text){ |
|
95 | + $classes = self::esc_classes($text); |
|
96 | + if(!empty($classes)){ |
|
97 | + $output = ' class="'.$classes.'" '; |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + return $output; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Escape a string of classes. |
|
106 | + * |
|
107 | + * @param $text |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public static function esc_classes($text){ |
|
112 | + $output = ''; |
|
113 | + |
|
114 | + if($text){ |
|
115 | + $classes = explode(" ",$text); |
|
116 | + $classes = array_map("trim",$classes); |
|
117 | + $classes = array_map("sanitize_html_class",$classes); |
|
118 | + if(!empty($classes)){ |
|
119 | + $output = implode(" ",$classes); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + return $output; |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param $args |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public static function data_attributes($args){ |
|
133 | + $output = ''; |
|
134 | + |
|
135 | + if(!empty($args)){ |
|
136 | + |
|
137 | + foreach($args as $key => $val){ |
|
138 | + if(substr( $key, 0, 5 ) === "data-"){ |
|
139 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
140 | + } |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + return $output; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param $args |
|
149 | + * |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + public static function aria_attributes($args){ |
|
153 | + $output = ''; |
|
154 | + |
|
155 | + if(!empty($args)){ |
|
156 | + |
|
157 | + foreach($args as $key => $val){ |
|
158 | + if(substr( $key, 0, 5 ) === "aria-"){ |
|
159 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + return $output; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Build a font awesome icon from a class. |
|
169 | + * |
|
170 | + * @param $class |
|
171 | + * @param bool $space_after |
|
172 | + * @param array $extra_attributes An array of extra attributes. |
|
173 | + * |
|
174 | + * @return string |
|
175 | + */ |
|
176 | + public static function icon($class,$space_after = false, $extra_attributes = array()){ |
|
177 | + $output = ''; |
|
178 | + |
|
179 | + if($class){ |
|
180 | + $classes = self::esc_classes($class); |
|
181 | + if(!empty($classes)){ |
|
182 | + $output = '<i class="'.$classes.'" '; |
|
183 | + // extra attributes |
|
184 | + if(!empty($extra_attributes)){ |
|
185 | + $output .= AUI_Component_Helper::extra_attributes($extra_attributes); |
|
186 | + } |
|
187 | + $output .= '></i>'; |
|
188 | + if($space_after){ |
|
189 | + $output .= " "; |
|
190 | + } |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + return $output; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param $args |
|
199 | + * |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + public static function extra_attributes($args){ |
|
203 | + $output = ''; |
|
204 | + |
|
205 | + if(!empty($args)){ |
|
206 | + |
|
207 | + if( is_array($args) ){ |
|
208 | + foreach($args as $key => $val){ |
|
209 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
210 | + } |
|
211 | + }else{ |
|
212 | + $output .= ' '.$args.' '; |
|
213 | + } |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + return $output; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @param $args |
|
222 | + * |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public static function help_text($text){ |
|
226 | + $output = ''; |
|
227 | + |
|
228 | + if($text){ |
|
229 | + $output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>'; |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + return $output; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Replace element require context with JS. |
|
238 | + * |
|
239 | + * @param $input |
|
240 | + * |
|
241 | + * @return string|void |
|
242 | + */ |
|
243 | + public static function element_require( $input ) { |
|
244 | + |
|
245 | + $input = str_replace( "'", '"', $input );// we only want double quotes |
|
246 | + |
|
247 | + $output = esc_attr( str_replace( array( "[%", "%]", "%:checked]" ), array( |
|
248 | + "jQuery(form).find('[data-argument=\"", |
|
249 | + "\"]').find('input,select,textarea').val()", |
|
250 | + "\"]').find('input:checked').val()", |
|
251 | + ), $input ) ); |
|
252 | + |
|
253 | + if($output){ |
|
254 | + $output = ' data-element-require="'.$output.'" '; |
|
255 | + } |
|
256 | + |
|
257 | + return $output; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Returns an array of allowed HTML tags and attributes for a given context. |
|
262 | + * |
|
263 | + * @since 0.1.41 |
|
264 | + * |
|
265 | + * @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
|
266 | + * 'strip', 'data', 'entities', or the name of a field filter such as |
|
267 | + * 'pre_user_description'. |
|
268 | + * @param array $input Input. |
|
269 | + * @return array Array of allowed HTML tags and their allowed attributes. |
|
270 | + */ |
|
271 | + public static function kses_allowed_html( $context = 'post', $input = array() ) { |
|
272 | + $allowed_html = wp_kses_allowed_html( $context ); |
|
273 | + |
|
274 | + if ( is_array( $allowed_html ) ) { |
|
275 | + // <iframe> |
|
276 | + if ( ! isset( $allowed_html['iframe'] ) && $context == 'post' ) { |
|
277 | + $allowed_html['iframe'] = array( |
|
278 | + 'class' => true, |
|
279 | + 'id' => true, |
|
280 | + 'src' => true, |
|
281 | + 'width' => true, |
|
282 | + 'height' => true, |
|
283 | + 'frameborder' => true, |
|
284 | + 'marginwidth' => true, |
|
285 | + 'marginheight' => true, |
|
286 | + 'scrolling' => true, |
|
287 | + 'style' => true, |
|
288 | + 'title' => true, |
|
289 | + 'allow' => true, |
|
290 | + 'allowfullscreen' => true, |
|
291 | + 'data-*' => true, |
|
292 | + ); |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Filters the allowed html tags. |
|
298 | + * |
|
299 | + * @since 0.1.41 |
|
300 | + * |
|
301 | + * @param array[]|string $allowed_html Allowed html tags. |
|
302 | + * @param @param string|array $context The context for which to retrieve tags. |
|
303 | + * @param array $input Input field. |
|
304 | + */ |
|
305 | + return apply_filters( 'ayecode_ui_kses_allowed_html', $allowed_html, $context, $input ); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Filters content and keeps only allowable HTML elements. |
|
310 | + * |
|
311 | + * This function makes sure that only the allowed HTML element names, attribute |
|
312 | + * names and attribute values plus only sane HTML entities will occur in |
|
313 | + * $string. You have to remove any slashes from PHP's magic quotes before you |
|
314 | + * call this function. |
|
315 | + * |
|
316 | + * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news', |
|
317 | + * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This |
|
318 | + * covers all common link protocols, except for 'javascript' which should not |
|
319 | + * be allowed for untrusted users. |
|
320 | + * |
|
321 | + * @since 0.1.41 |
|
322 | + * |
|
323 | + * @param string|array $value Content to filter through kses. |
|
324 | + * @param array $input Input Field. |
|
325 | + * @return string Filtered content with only allowed HTML elements. |
|
326 | + */ |
|
327 | + public static function _sanitize_html_field( $value, $input = array() ) { |
|
328 | + if ( $value === '' ) { |
|
329 | + return $value; |
|
330 | + } |
|
331 | + |
|
332 | + $allowed_html = self::kses_allowed_html( 'post', $input ); |
|
333 | + |
|
334 | + if ( ! is_array( $allowed_html ) ) { |
|
335 | + $allowed_html = wp_kses_allowed_html( 'post' ); |
|
336 | + } |
|
337 | + |
|
338 | + $filtered = trim( wp_unslash( $value ) ); |
|
339 | + $filtered = wp_kses( $filtered, $allowed_html ); |
|
340 | + $filtered = balanceTags( $filtered ); // Balances tags |
|
341 | + |
|
342 | + return $filtered; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Navigates through an array, object, or scalar, and removes slashes from the values. |
|
347 | + * |
|
348 | + * @since 0.1.41 |
|
349 | + * |
|
350 | + * @param mixed $value The value to be stripped. |
|
351 | + * @param array $input Input Field. |
|
352 | + * @return mixed Stripped value. |
|
353 | + */ |
|
354 | + public static function sanitize_html_field( $value, $input = array() ) { |
|
355 | + $original = $value; |
|
356 | + |
|
357 | + if ( is_array( $value ) ) { |
|
358 | + foreach ( $value as $index => $item ) { |
|
359 | + $value[ $index ] = self::_sanitize_html_field( $value, $input ); |
|
360 | + } |
|
361 | + } elseif ( is_object( $value ) ) { |
|
362 | + $object_vars = get_object_vars( $value ); |
|
363 | + |
|
364 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
365 | + $value->$property_name = self::_sanitize_html_field( $property_value, $input ); |
|
366 | + } |
|
367 | + } else { |
|
368 | + $value = self::_sanitize_html_field( $value, $input ); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Filters content and keeps only allowable HTML elements. |
|
373 | + * |
|
374 | + * @since 0.1.41 |
|
375 | + * |
|
376 | + * @param string|array $value Content to filter through kses. |
|
377 | + * @param string|array $value Original content without filter. |
|
378 | + * @param array $input Input Field. |
|
379 | + */ |
|
380 | + return apply_filters( 'ayecode_ui_sanitize_html_field', $value, $original, $input ); |
|
381 | + } |
|
382 | 382 | } |
383 | 383 | \ No newline at end of file |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,1022 +11,1022 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Input { |
13 | 13 | |
14 | - /** |
|
15 | - * Build the component. |
|
16 | - * |
|
17 | - * @param array $args |
|
18 | - * |
|
19 | - * @return string The rendered component. |
|
20 | - */ |
|
21 | - public static function input($args = array()){ |
|
22 | - $defaults = array( |
|
23 | - 'type' => 'text', |
|
24 | - 'name' => '', |
|
25 | - 'class' => '', |
|
26 | - 'wrap_class' => '', |
|
27 | - 'id' => '', |
|
28 | - 'placeholder'=> '', |
|
29 | - 'title' => '', |
|
30 | - 'value' => '', |
|
31 | - 'required' => false, |
|
32 | - 'label' => '', |
|
33 | - 'label_after'=> false, |
|
34 | - 'label_class'=> '', |
|
35 | - 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
36 | - 'help_text' => '', |
|
37 | - 'validation_text' => '', |
|
38 | - 'validation_pattern' => '', |
|
39 | - 'no_wrap' => false, |
|
40 | - 'input_group_right' => '', |
|
41 | - 'input_group_left' => '', |
|
42 | - 'input_group_right_inside' => false, // forces the input group inside the input |
|
43 | - 'input_group_left_inside' => false, // forces the input group inside the input |
|
44 | - 'step' => '', |
|
45 | - 'switch' => false, // to show checkbox as a switch |
|
46 | - 'checked' => false, // set a checkbox or radio as selected |
|
47 | - 'password_toggle' => true, // toggle view/hide password |
|
48 | - 'element_require' => '', // [%element_id%] == "1" |
|
49 | - 'extra_attributes' => array() // an array of extra attributes |
|
50 | - ); |
|
51 | - |
|
52 | - /** |
|
53 | - * Parse incoming $args into an array and merge it with $defaults |
|
54 | - */ |
|
55 | - $args = wp_parse_args( $args, $defaults ); |
|
56 | - $output = ''; |
|
57 | - if ( ! empty( $args['type'] ) ) { |
|
58 | - // hidden label option needs to be empty |
|
59 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
60 | - |
|
61 | - $type = sanitize_html_class( $args['type'] ); |
|
62 | - |
|
63 | - $help_text = ''; |
|
64 | - $label = ''; |
|
65 | - $label_after = $args['label_after']; |
|
66 | - $label_args = array( |
|
67 | - 'title'=> $args['label'], |
|
68 | - 'for'=> $args['id'], |
|
69 | - 'class' => $args['label_class']." ", |
|
70 | - 'label_type' => $args['label_type'] |
|
71 | - ); |
|
72 | - |
|
73 | - // floating labels need label after |
|
74 | - if( $args['label_type'] == 'floating' && $type != 'checkbox' ){ |
|
75 | - $label_after = true; |
|
76 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
77 | - } |
|
78 | - |
|
79 | - // Some special sauce for files |
|
80 | - if($type=='file' ){ |
|
81 | - $label_after = true; // if type file we need the label after |
|
82 | - $args['class'] .= ' custom-file-input '; |
|
83 | - }elseif($type=='checkbox'){ |
|
84 | - $label_after = true; // if type file we need the label after |
|
85 | - $args['class'] .= ' custom-control-input '; |
|
86 | - }elseif($type=='datepicker' || $type=='timepicker'){ |
|
87 | - $type = 'text'; |
|
88 | - //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
89 | - $args['class'] .= ' bg-initial '; |
|
90 | - |
|
91 | - $args['extra_attributes']['data-aui-init'] = 'flatpickr'; |
|
92 | - // enqueue the script |
|
93 | - $aui_settings = AyeCode_UI_Settings::instance(); |
|
94 | - $aui_settings->enqueue_flatpickr(); |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - // open/type |
|
99 | - $output .= '<input type="' . $type . '" '; |
|
100 | - |
|
101 | - // name |
|
102 | - if(!empty($args['name'])){ |
|
103 | - $output .= ' name="'.esc_attr($args['name']).'" '; |
|
104 | - } |
|
105 | - |
|
106 | - // id |
|
107 | - if(!empty($args['id'])){ |
|
108 | - $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
109 | - } |
|
110 | - |
|
111 | - // placeholder |
|
112 | - if(isset($args['placeholder']) && '' != $args['placeholder'] ){ |
|
113 | - $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
114 | - } |
|
115 | - |
|
116 | - // title |
|
117 | - if(!empty($args['title'])){ |
|
118 | - $output .= ' title="'.esc_attr($args['title']).'" '; |
|
119 | - } |
|
120 | - |
|
121 | - // value |
|
122 | - if(!empty($args['value'])){ |
|
123 | - $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
124 | - } |
|
125 | - |
|
126 | - // checked, for radio and checkboxes |
|
127 | - if( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ){ |
|
128 | - $output .= ' checked '; |
|
129 | - } |
|
130 | - |
|
131 | - // validation text |
|
132 | - if(!empty($args['validation_text'])){ |
|
133 | - $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
134 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
135 | - } |
|
136 | - |
|
137 | - // validation_pattern |
|
138 | - if(!empty($args['validation_pattern'])){ |
|
139 | - $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
140 | - } |
|
141 | - |
|
142 | - // step (for numbers) |
|
143 | - if(!empty($args['step'])){ |
|
144 | - $output .= ' step="'.$args['step'].'" '; |
|
145 | - } |
|
146 | - |
|
147 | - // required |
|
148 | - if(!empty($args['required'])){ |
|
149 | - $output .= ' required '; |
|
150 | - } |
|
151 | - |
|
152 | - // class |
|
153 | - $class = !empty($args['class']) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
154 | - $output .= ' class="form-control '.$class.'" '; |
|
155 | - |
|
156 | - // data-attributes |
|
157 | - $output .= AUI_Component_Helper::data_attributes($args); |
|
158 | - |
|
159 | - // extra attributes |
|
160 | - if(!empty($args['extra_attributes'])){ |
|
161 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
162 | - } |
|
163 | - |
|
164 | - // close |
|
165 | - $output .= ' >'; |
|
166 | - |
|
167 | - |
|
168 | - // label |
|
169 | - if(!empty($args['label'])){ |
|
170 | - if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
|
171 | - elseif($type == 'checkbox'){$label_args['class'] .= 'custom-control-label';} |
|
172 | - $label = self::label( $label_args, $type ); |
|
173 | - } |
|
174 | - |
|
175 | - // help text |
|
176 | - if(!empty($args['help_text'])){ |
|
177 | - $help_text = AUI_Component_Helper::help_text($args['help_text']); |
|
178 | - } |
|
179 | - |
|
180 | - |
|
181 | - // set help text in the correct possition |
|
182 | - if($label_after){ |
|
183 | - $output .= $label . $help_text; |
|
184 | - } |
|
185 | - |
|
186 | - // some input types need a separate wrap |
|
187 | - if($type == 'file') { |
|
188 | - $output = self::wrap( array( |
|
189 | - 'content' => $output, |
|
190 | - 'class' => 'form-group custom-file' |
|
191 | - ) ); |
|
192 | - }elseif($type == 'checkbox'){ |
|
193 | - $wrap_class = $args['switch'] ? 'custom-switch' : 'custom-checkbox'; |
|
194 | - $output = self::wrap( array( |
|
195 | - 'content' => $output, |
|
196 | - 'class' => 'custom-control '.$wrap_class |
|
197 | - ) ); |
|
198 | - |
|
199 | - if($args['label_type']=='horizontal'){ |
|
200 | - $output = '<div class="col-sm-2 col-form-label"></div><div class="col-sm-10">' . $output . '</div>'; |
|
201 | - } |
|
202 | - }elseif($type == 'password' && $args['password_toggle'] && !$args['input_group_right']){ |
|
203 | - |
|
204 | - |
|
205 | - // allow password field to toggle view |
|
206 | - $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
14 | + /** |
|
15 | + * Build the component. |
|
16 | + * |
|
17 | + * @param array $args |
|
18 | + * |
|
19 | + * @return string The rendered component. |
|
20 | + */ |
|
21 | + public static function input($args = array()){ |
|
22 | + $defaults = array( |
|
23 | + 'type' => 'text', |
|
24 | + 'name' => '', |
|
25 | + 'class' => '', |
|
26 | + 'wrap_class' => '', |
|
27 | + 'id' => '', |
|
28 | + 'placeholder'=> '', |
|
29 | + 'title' => '', |
|
30 | + 'value' => '', |
|
31 | + 'required' => false, |
|
32 | + 'label' => '', |
|
33 | + 'label_after'=> false, |
|
34 | + 'label_class'=> '', |
|
35 | + 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
36 | + 'help_text' => '', |
|
37 | + 'validation_text' => '', |
|
38 | + 'validation_pattern' => '', |
|
39 | + 'no_wrap' => false, |
|
40 | + 'input_group_right' => '', |
|
41 | + 'input_group_left' => '', |
|
42 | + 'input_group_right_inside' => false, // forces the input group inside the input |
|
43 | + 'input_group_left_inside' => false, // forces the input group inside the input |
|
44 | + 'step' => '', |
|
45 | + 'switch' => false, // to show checkbox as a switch |
|
46 | + 'checked' => false, // set a checkbox or radio as selected |
|
47 | + 'password_toggle' => true, // toggle view/hide password |
|
48 | + 'element_require' => '', // [%element_id%] == "1" |
|
49 | + 'extra_attributes' => array() // an array of extra attributes |
|
50 | + ); |
|
51 | + |
|
52 | + /** |
|
53 | + * Parse incoming $args into an array and merge it with $defaults |
|
54 | + */ |
|
55 | + $args = wp_parse_args( $args, $defaults ); |
|
56 | + $output = ''; |
|
57 | + if ( ! empty( $args['type'] ) ) { |
|
58 | + // hidden label option needs to be empty |
|
59 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
60 | + |
|
61 | + $type = sanitize_html_class( $args['type'] ); |
|
62 | + |
|
63 | + $help_text = ''; |
|
64 | + $label = ''; |
|
65 | + $label_after = $args['label_after']; |
|
66 | + $label_args = array( |
|
67 | + 'title'=> $args['label'], |
|
68 | + 'for'=> $args['id'], |
|
69 | + 'class' => $args['label_class']." ", |
|
70 | + 'label_type' => $args['label_type'] |
|
71 | + ); |
|
72 | + |
|
73 | + // floating labels need label after |
|
74 | + if( $args['label_type'] == 'floating' && $type != 'checkbox' ){ |
|
75 | + $label_after = true; |
|
76 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
77 | + } |
|
78 | + |
|
79 | + // Some special sauce for files |
|
80 | + if($type=='file' ){ |
|
81 | + $label_after = true; // if type file we need the label after |
|
82 | + $args['class'] .= ' custom-file-input '; |
|
83 | + }elseif($type=='checkbox'){ |
|
84 | + $label_after = true; // if type file we need the label after |
|
85 | + $args['class'] .= ' custom-control-input '; |
|
86 | + }elseif($type=='datepicker' || $type=='timepicker'){ |
|
87 | + $type = 'text'; |
|
88 | + //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
89 | + $args['class'] .= ' bg-initial '; |
|
90 | + |
|
91 | + $args['extra_attributes']['data-aui-init'] = 'flatpickr'; |
|
92 | + // enqueue the script |
|
93 | + $aui_settings = AyeCode_UI_Settings::instance(); |
|
94 | + $aui_settings->enqueue_flatpickr(); |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + // open/type |
|
99 | + $output .= '<input type="' . $type . '" '; |
|
100 | + |
|
101 | + // name |
|
102 | + if(!empty($args['name'])){ |
|
103 | + $output .= ' name="'.esc_attr($args['name']).'" '; |
|
104 | + } |
|
105 | + |
|
106 | + // id |
|
107 | + if(!empty($args['id'])){ |
|
108 | + $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
109 | + } |
|
110 | + |
|
111 | + // placeholder |
|
112 | + if(isset($args['placeholder']) && '' != $args['placeholder'] ){ |
|
113 | + $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
114 | + } |
|
115 | + |
|
116 | + // title |
|
117 | + if(!empty($args['title'])){ |
|
118 | + $output .= ' title="'.esc_attr($args['title']).'" '; |
|
119 | + } |
|
120 | + |
|
121 | + // value |
|
122 | + if(!empty($args['value'])){ |
|
123 | + $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
124 | + } |
|
125 | + |
|
126 | + // checked, for radio and checkboxes |
|
127 | + if( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ){ |
|
128 | + $output .= ' checked '; |
|
129 | + } |
|
130 | + |
|
131 | + // validation text |
|
132 | + if(!empty($args['validation_text'])){ |
|
133 | + $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
134 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
135 | + } |
|
136 | + |
|
137 | + // validation_pattern |
|
138 | + if(!empty($args['validation_pattern'])){ |
|
139 | + $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
140 | + } |
|
141 | + |
|
142 | + // step (for numbers) |
|
143 | + if(!empty($args['step'])){ |
|
144 | + $output .= ' step="'.$args['step'].'" '; |
|
145 | + } |
|
146 | + |
|
147 | + // required |
|
148 | + if(!empty($args['required'])){ |
|
149 | + $output .= ' required '; |
|
150 | + } |
|
151 | + |
|
152 | + // class |
|
153 | + $class = !empty($args['class']) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
154 | + $output .= ' class="form-control '.$class.'" '; |
|
155 | + |
|
156 | + // data-attributes |
|
157 | + $output .= AUI_Component_Helper::data_attributes($args); |
|
158 | + |
|
159 | + // extra attributes |
|
160 | + if(!empty($args['extra_attributes'])){ |
|
161 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
162 | + } |
|
163 | + |
|
164 | + // close |
|
165 | + $output .= ' >'; |
|
166 | + |
|
167 | + |
|
168 | + // label |
|
169 | + if(!empty($args['label'])){ |
|
170 | + if($type == 'file'){$label_args['class'] .= 'custom-file-label';} |
|
171 | + elseif($type == 'checkbox'){$label_args['class'] .= 'custom-control-label';} |
|
172 | + $label = self::label( $label_args, $type ); |
|
173 | + } |
|
174 | + |
|
175 | + // help text |
|
176 | + if(!empty($args['help_text'])){ |
|
177 | + $help_text = AUI_Component_Helper::help_text($args['help_text']); |
|
178 | + } |
|
179 | + |
|
180 | + |
|
181 | + // set help text in the correct possition |
|
182 | + if($label_after){ |
|
183 | + $output .= $label . $help_text; |
|
184 | + } |
|
185 | + |
|
186 | + // some input types need a separate wrap |
|
187 | + if($type == 'file') { |
|
188 | + $output = self::wrap( array( |
|
189 | + 'content' => $output, |
|
190 | + 'class' => 'form-group custom-file' |
|
191 | + ) ); |
|
192 | + }elseif($type == 'checkbox'){ |
|
193 | + $wrap_class = $args['switch'] ? 'custom-switch' : 'custom-checkbox'; |
|
194 | + $output = self::wrap( array( |
|
195 | + 'content' => $output, |
|
196 | + 'class' => 'custom-control '.$wrap_class |
|
197 | + ) ); |
|
198 | + |
|
199 | + if($args['label_type']=='horizontal'){ |
|
200 | + $output = '<div class="col-sm-2 col-form-label"></div><div class="col-sm-10">' . $output . '</div>'; |
|
201 | + } |
|
202 | + }elseif($type == 'password' && $args['password_toggle'] && !$args['input_group_right']){ |
|
203 | + |
|
204 | + |
|
205 | + // allow password field to toggle view |
|
206 | + $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
207 | 207 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); |
208 | 208 | var $eli = jQuery(this).parent().parent().find(\'input\'); |
209 | 209 | if($el.hasClass(\'fa-eye\')) |
210 | 210 | {$eli.attr(\'type\',\'text\');} |
211 | 211 | else{$eli.attr(\'type\',\'password\');}" |
212 | 212 | ><i class="far fa-fw fa-eye-slash"></i></span>'; |
213 | - } |
|
214 | - |
|
215 | - // input group wraps |
|
216 | - if($args['input_group_left'] || $args['input_group_right']){ |
|
217 | - $w100 = strpos($args['class'], 'w-100') !== false ? ' w-100' : ''; |
|
218 | - if($args['input_group_left']){ |
|
219 | - $output = self::wrap( array( |
|
220 | - 'content' => $output, |
|
221 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative'.$w100 : 'input-group', |
|
222 | - 'input_group_left' => $args['input_group_left'], |
|
223 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
224 | - ) ); |
|
225 | - } |
|
226 | - if($args['input_group_right']){ |
|
227 | - $output = self::wrap( array( |
|
228 | - 'content' => $output, |
|
229 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative'.$w100 : 'input-group', |
|
230 | - 'input_group_right' => $args['input_group_right'], |
|
231 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
232 | - ) ); |
|
233 | - } |
|
234 | - |
|
235 | - } |
|
236 | - |
|
237 | - if(!$label_after){ |
|
238 | - $output .= $help_text; |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
243 | - $output = self::wrap( array( |
|
244 | - 'content' => $output, |
|
245 | - 'class' => 'col-sm-10', |
|
246 | - ) ); |
|
247 | - } |
|
248 | - |
|
249 | - if(!$label_after){ |
|
250 | - $output = $label . $output; |
|
251 | - } |
|
252 | - |
|
253 | - // wrap |
|
254 | - if(!$args['no_wrap']){ |
|
255 | - |
|
256 | - $form_group_class = $args['label_type']=='floating' && $type != 'checkbox' ? 'form-label-group' : 'form-group'; |
|
257 | - $wrap_class = $args['label_type']=='horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
258 | - $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
259 | - $output = self::wrap(array( |
|
260 | - 'content' => $output, |
|
261 | - 'class' => $wrap_class, |
|
262 | - 'element_require' => $args['element_require'], |
|
263 | - 'argument_id' => $args['id'] |
|
264 | - )); |
|
265 | - } |
|
266 | - |
|
267 | - |
|
268 | - |
|
269 | - } |
|
270 | - |
|
271 | - return $output; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Build the component. |
|
276 | - * |
|
277 | - * @param array $args |
|
278 | - * |
|
279 | - * @return string The rendered component. |
|
280 | - */ |
|
281 | - public static function textarea($args = array()){ |
|
282 | - $defaults = array( |
|
283 | - 'name' => '', |
|
284 | - 'class' => '', |
|
285 | - 'wrap_class' => '', |
|
286 | - 'id' => '', |
|
287 | - 'placeholder'=> '', |
|
288 | - 'title' => '', |
|
289 | - 'value' => '', |
|
290 | - 'required' => false, |
|
291 | - 'label' => '', |
|
292 | - 'label_after'=> false, |
|
293 | - 'label_class' => '', |
|
294 | - 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
295 | - 'help_text' => '', |
|
296 | - 'validation_text' => '', |
|
297 | - 'validation_pattern' => '', |
|
298 | - 'no_wrap' => false, |
|
299 | - 'rows' => '', |
|
300 | - 'wysiwyg' => false, |
|
301 | - 'allow_tags' => false, // Allow HTML tags |
|
302 | - 'element_require' => '', // [%element_id%] == "1" |
|
303 | - 'extra_attributes' => array(), // an array of extra attributes |
|
304 | - ); |
|
305 | - |
|
306 | - /** |
|
307 | - * Parse incoming $args into an array and merge it with $defaults |
|
308 | - */ |
|
309 | - $args = wp_parse_args( $args, $defaults ); |
|
310 | - $output = ''; |
|
311 | - |
|
312 | - // hidden label option needs to be empty |
|
313 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
314 | - |
|
315 | - // floating labels don't work with wysiwyg so set it as top |
|
316 | - if($args['label_type'] == 'floating' && !empty($args['wysiwyg'])){ |
|
317 | - $args['label_type'] = 'top'; |
|
318 | - } |
|
319 | - |
|
320 | - $label_after = $args['label_after']; |
|
321 | - |
|
322 | - // floating labels need label after |
|
323 | - if( $args['label_type'] == 'floating' && empty($args['wysiwyg']) ){ |
|
324 | - $label_after = true; |
|
325 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
326 | - } |
|
327 | - |
|
328 | - // label |
|
329 | - if(!empty($args['label']) && is_array($args['label'])){ |
|
330 | - }elseif(!empty($args['label']) && !$label_after){ |
|
331 | - $label_args = array( |
|
332 | - 'title'=> $args['label'], |
|
333 | - 'for'=> $args['id'], |
|
334 | - 'class' => $args['label_class']." ", |
|
335 | - 'label_type' => $args['label_type'] |
|
336 | - ); |
|
337 | - $output .= self::label( $label_args ); |
|
338 | - } |
|
339 | - |
|
340 | - // maybe horizontal label |
|
341 | - if($args['label_type']=='horizontal'){ |
|
342 | - $output .= '<div class="col-sm-10">'; |
|
343 | - } |
|
344 | - |
|
345 | - if(!empty($args['wysiwyg'])){ |
|
346 | - ob_start(); |
|
347 | - $content = $args['value']; |
|
348 | - $editor_id = !empty($args['id']) ? sanitize_html_class($args['id']) : 'wp_editor'; |
|
349 | - $settings = array( |
|
350 | - 'textarea_rows' => !empty(absint($args['rows'])) ? absint($args['rows']) : 4, |
|
351 | - 'quicktags' => false, |
|
352 | - 'media_buttons' => false, |
|
353 | - 'editor_class' => 'form-control', |
|
354 | - 'textarea_name' => !empty($args['name']) ? sanitize_html_class($args['name']) : sanitize_html_class($args['id']), |
|
355 | - 'teeny' => true, |
|
356 | - ); |
|
357 | - |
|
358 | - // maybe set settings if array |
|
359 | - if(is_array($args['wysiwyg'])){ |
|
360 | - $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
361 | - } |
|
362 | - |
|
363 | - wp_editor( $content, $editor_id, $settings ); |
|
364 | - $output .= ob_get_clean(); |
|
365 | - }else{ |
|
366 | - |
|
367 | - // open |
|
368 | - $output .= '<textarea '; |
|
369 | - |
|
370 | - // name |
|
371 | - if(!empty($args['name'])){ |
|
372 | - $output .= ' name="'.sanitize_html_class($args['name']).'" '; |
|
373 | - } |
|
374 | - |
|
375 | - // id |
|
376 | - if(!empty($args['id'])){ |
|
377 | - $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
378 | - } |
|
379 | - |
|
380 | - // placeholder |
|
381 | - if(isset($args['placeholder']) && '' != $args['placeholder']){ |
|
382 | - $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
383 | - } |
|
384 | - |
|
385 | - // title |
|
386 | - if(!empty($args['title'])){ |
|
387 | - $output .= ' title="'.esc_attr($args['title']).'" '; |
|
388 | - } |
|
389 | - |
|
390 | - // validation text |
|
391 | - if(!empty($args['validation_text'])){ |
|
392 | - $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
393 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
394 | - } |
|
395 | - |
|
396 | - // validation_pattern |
|
397 | - if(!empty($args['validation_pattern'])){ |
|
398 | - $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
399 | - } |
|
400 | - |
|
401 | - // required |
|
402 | - if(!empty($args['required'])){ |
|
403 | - $output .= ' required '; |
|
404 | - } |
|
405 | - |
|
406 | - // rows |
|
407 | - if(!empty($args['rows'])){ |
|
408 | - $output .= ' rows="'.absint($args['rows']).'" '; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - // class |
|
413 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
414 | - $output .= ' class="form-control '.$class.'" '; |
|
415 | - |
|
416 | - // extra attributes |
|
417 | - if(!empty($args['extra_attributes'])){ |
|
418 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
419 | - } |
|
420 | - |
|
421 | - // close tag |
|
422 | - $output .= ' >'; |
|
423 | - |
|
424 | - // value |
|
425 | - if ( ! empty( $args['value'] ) ) { |
|
426 | - if ( ! empty( $args['allow_tags'] ) ) { |
|
427 | - $output .= AUI_Component_Helper::sanitize_html_field( $args['value'], $args ); // Sanitize HTML. |
|
428 | - } else { |
|
429 | - $output .= sanitize_textarea_field( $args['value'] ); |
|
430 | - } |
|
431 | - } |
|
432 | - |
|
433 | - // closing tag |
|
434 | - $output .= '</textarea>'; |
|
435 | - |
|
436 | - } |
|
437 | - |
|
438 | - if(!empty($args['label']) && $label_after){ |
|
439 | - $label_args = array( |
|
440 | - 'title'=> $args['label'], |
|
441 | - 'for'=> $args['id'], |
|
442 | - 'class' => $args['label_class']." ", |
|
443 | - 'label_type' => $args['label_type'] |
|
444 | - ); |
|
445 | - $output .= self::label( $label_args ); |
|
446 | - } |
|
447 | - |
|
448 | - // help text |
|
449 | - if(!empty($args['help_text'])){ |
|
450 | - $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
451 | - } |
|
452 | - |
|
453 | - // maybe horizontal label |
|
454 | - if($args['label_type']=='horizontal'){ |
|
455 | - $output .= '</div>'; |
|
456 | - } |
|
457 | - |
|
458 | - |
|
459 | - // wrap |
|
460 | - if(!$args['no_wrap']){ |
|
461 | - $form_group_class = $args['label_type']=='floating' ? 'form-label-group' : 'form-group'; |
|
462 | - $wrap_class = $args['label_type']=='horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
463 | - $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
464 | - $output = self::wrap(array( |
|
465 | - 'content' => $output, |
|
466 | - 'class' => $wrap_class, |
|
467 | - 'element_require' => $args['element_require'], |
|
468 | - 'argument_id' => $args['id'] |
|
469 | - )); |
|
470 | - } |
|
471 | - |
|
472 | - |
|
473 | - return $output; |
|
474 | - } |
|
475 | - |
|
476 | - public static function label($args = array(), $type = ''){ |
|
477 | - //<label for="exampleInputEmail1">Email address</label> |
|
478 | - $defaults = array( |
|
479 | - 'title' => 'div', |
|
480 | - 'for' => '', |
|
481 | - 'class' => '', |
|
482 | - 'label_type' => '', // empty = hidden, top, horizontal |
|
483 | - ); |
|
484 | - |
|
485 | - /** |
|
486 | - * Parse incoming $args into an array and merge it with $defaults |
|
487 | - */ |
|
488 | - $args = wp_parse_args( $args, $defaults ); |
|
489 | - $output = ''; |
|
490 | - |
|
491 | - if($args['title']){ |
|
492 | - |
|
493 | - // maybe hide labels //@todo set a global option for visibility class |
|
494 | - if($type == 'file' || $type == 'checkbox' || $type == 'radio' || !empty($args['label_type']) ){ |
|
495 | - $class = $args['class']; |
|
496 | - }else{ |
|
497 | - $class = 'sr-only '.$args['class']; |
|
498 | - } |
|
499 | - |
|
500 | - // maybe horizontal |
|
501 | - if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
502 | - $class .= ' col-sm-2 col-form-label'; |
|
503 | - } |
|
504 | - |
|
505 | - // open |
|
506 | - $output .= '<label '; |
|
507 | - |
|
508 | - // for |
|
509 | - if(!empty($args['for'])){ |
|
510 | - $output .= ' for="'.sanitize_text_field($args['for']).'" '; |
|
511 | - } |
|
512 | - |
|
513 | - // class |
|
514 | - $class = $class ? AUI_Component_Helper::esc_classes( $class ) : ''; |
|
515 | - $output .= ' class="'.$class.'" '; |
|
516 | - |
|
517 | - // close |
|
518 | - $output .= '>'; |
|
519 | - |
|
520 | - |
|
521 | - // title, don't escape fully as can contain html |
|
522 | - if(!empty($args['title'])){ |
|
523 | - $output .= wp_kses_post($args['title']); |
|
524 | - } |
|
525 | - |
|
526 | - // close wrap |
|
527 | - $output .= '</label>'; |
|
528 | - |
|
529 | - |
|
530 | - } |
|
531 | - |
|
532 | - |
|
533 | - return $output; |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * Wrap some content in a HTML wrapper. |
|
538 | - * |
|
539 | - * @param array $args |
|
540 | - * |
|
541 | - * @return string |
|
542 | - */ |
|
543 | - public static function wrap($args = array()){ |
|
544 | - $defaults = array( |
|
545 | - 'type' => 'div', |
|
546 | - 'class' => 'form-group', |
|
547 | - 'content' => '', |
|
548 | - 'input_group_left' => '', |
|
549 | - 'input_group_right' => '', |
|
550 | - 'input_group_left_inside' => false, |
|
551 | - 'input_group_right_inside' => false, |
|
552 | - 'element_require' => '', |
|
553 | - 'argument_id' => '', |
|
554 | - ); |
|
555 | - |
|
556 | - /** |
|
557 | - * Parse incoming $args into an array and merge it with $defaults |
|
558 | - */ |
|
559 | - $args = wp_parse_args( $args, $defaults ); |
|
560 | - $output = ''; |
|
561 | - if($args['type']){ |
|
562 | - |
|
563 | - // open |
|
564 | - $output .= '<'.sanitize_html_class($args['type']); |
|
565 | - |
|
566 | - // element require |
|
567 | - if(!empty($args['element_require'])){ |
|
568 | - $output .= AUI_Component_Helper::element_require($args['element_require']); |
|
569 | - $args['class'] .= " aui-conditional-field"; |
|
570 | - } |
|
571 | - |
|
572 | - // argument_id |
|
573 | - if( !empty($args['argument_id']) ){ |
|
574 | - $output .= ' data-argument="'.esc_attr($args['argument_id']).'"'; |
|
575 | - } |
|
576 | - |
|
577 | - // class |
|
578 | - $class = !empty($args['class']) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
579 | - $output .= ' class="'.$class.'" '; |
|
580 | - |
|
581 | - // close wrap |
|
582 | - $output .= ' >'; |
|
583 | - |
|
584 | - |
|
585 | - // Input group left |
|
586 | - if(!empty($args['input_group_left'])){ |
|
587 | - $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute h-100' : ''; |
|
588 | - $input_group_left = strpos($args['input_group_left'], '<') !== false ? $args['input_group_left'] : '<span class="input-group-text">'.$args['input_group_left'].'</span>'; |
|
589 | - $output .= '<div class="input-group-prepend '.$position_class.'">'.$input_group_left.'</div>'; |
|
590 | - } |
|
591 | - |
|
592 | - // content |
|
593 | - $output .= $args['content']; |
|
594 | - |
|
595 | - // Input group right |
|
596 | - if(!empty($args['input_group_right'])){ |
|
597 | - $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute h-100' : ''; |
|
598 | - $input_group_right = strpos($args['input_group_right'], '<') !== false ? $args['input_group_right'] : '<span class="input-group-text">'.$args['input_group_right'].'</span>'; |
|
599 | - $output .= '<div class="input-group-append '.$position_class.'">'.$input_group_right.'</div>'; |
|
600 | - } |
|
601 | - |
|
602 | - |
|
603 | - // close wrap |
|
604 | - $output .= '</'.sanitize_html_class($args['type']).'>'; |
|
605 | - |
|
606 | - |
|
607 | - }else{ |
|
608 | - $output = $args['content']; |
|
609 | - } |
|
610 | - |
|
611 | - return $output; |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * Build the component. |
|
616 | - * |
|
617 | - * @param array $args |
|
618 | - * |
|
619 | - * @return string The rendered component. |
|
620 | - */ |
|
621 | - public static function select($args = array()){ |
|
622 | - $defaults = array( |
|
623 | - 'class' => '', |
|
624 | - 'wrap_class' => '', |
|
625 | - 'id' => '', |
|
626 | - 'title' => '', |
|
627 | - 'value' => '', // can be an array or a string |
|
628 | - 'required' => false, |
|
629 | - 'label' => '', |
|
630 | - 'label_after'=> false, |
|
631 | - 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
632 | - 'label_class' => '', |
|
633 | - 'help_text' => '', |
|
634 | - 'placeholder'=> '', |
|
635 | - 'options' => array(), // array or string |
|
636 | - 'icon' => '', |
|
637 | - 'multiple' => false, |
|
638 | - 'select2' => false, |
|
639 | - 'no_wrap' => false, |
|
640 | - 'element_require' => '', // [%element_id%] == "1" |
|
641 | - 'extra_attributes' => array(), // an array of extra attributes |
|
642 | - ); |
|
643 | - |
|
644 | - /** |
|
645 | - * Parse incoming $args into an array and merge it with $defaults |
|
646 | - */ |
|
647 | - $args = wp_parse_args( $args, $defaults ); |
|
648 | - $output = ''; |
|
649 | - |
|
650 | - // for now lets hide floating labels |
|
651 | - if( $args['label_type'] == 'floating' ){$args['label_type'] = 'hidden';} |
|
652 | - |
|
653 | - // hidden label option needs to be empty |
|
654 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
655 | - |
|
656 | - |
|
657 | - $label_after = $args['label_after']; |
|
658 | - |
|
659 | - // floating labels need label after |
|
660 | - if( $args['label_type'] == 'floating' ){ |
|
661 | - $label_after = true; |
|
662 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
663 | - } |
|
664 | - |
|
665 | - // Maybe setup select2 |
|
666 | - $is_select2 = false; |
|
667 | - if(!empty($args['select2'])){ |
|
668 | - $args['class'] .= ' aui-select2'; |
|
669 | - $is_select2 = true; |
|
670 | - }elseif( strpos($args['class'], 'aui-select2') !== false){ |
|
671 | - $is_select2 = true; |
|
672 | - } |
|
673 | - |
|
674 | - // select2 tags |
|
675 | - if( !empty($args['select2']) && $args['select2'] === 'tags'){ // triple equals needed here for some reason |
|
676 | - $args['data-tags'] = 'true'; |
|
677 | - $args['data-token-separators'] = "[',']"; |
|
678 | - $args['multiple'] = true; |
|
679 | - } |
|
680 | - |
|
681 | - // select2 placeholder |
|
682 | - if($is_select2 && isset($args['placeholder']) && '' != $args['placeholder'] && empty($args['data-placeholder'])){ |
|
683 | - $args['data-placeholder'] = esc_attr($args['placeholder']); |
|
684 | - $args['data-allow-clear'] = isset($args['data-allow-clear']) ? (bool) $args['data-allow-clear'] : true; |
|
685 | - } |
|
686 | - |
|
687 | - // label |
|
688 | - if(!empty($args['label']) && is_array($args['label'])){ |
|
689 | - }elseif(!empty($args['label']) && !$label_after){ |
|
690 | - $label_args = array( |
|
691 | - 'title'=> $args['label'], |
|
692 | - 'for'=> $args['id'], |
|
693 | - 'class' => $args['label_class']." ", |
|
694 | - 'label_type' => $args['label_type'] |
|
695 | - ); |
|
696 | - $output .= self::label($label_args); |
|
697 | - } |
|
698 | - |
|
699 | - // maybe horizontal label |
|
700 | - if($args['label_type']=='horizontal'){ |
|
701 | - $output .= '<div class="col-sm-10">'; |
|
702 | - } |
|
703 | - |
|
704 | - // open/type |
|
705 | - $output .= '<select '; |
|
706 | - |
|
707 | - // style |
|
708 | - if($is_select2){ |
|
709 | - $output .= " style='width:100%;' "; |
|
710 | - } |
|
711 | - |
|
712 | - // element require |
|
713 | - if(!empty($args['element_require'])){ |
|
714 | - $output .= AUI_Component_Helper::element_require($args['element_require']); |
|
715 | - $args['class'] .= " aui-conditional-field"; |
|
716 | - } |
|
717 | - |
|
718 | - // class |
|
719 | - $class = !empty($args['class']) ? $args['class'] : ''; |
|
720 | - $output .= AUI_Component_Helper::class_attr('custom-select '.$class); |
|
721 | - |
|
722 | - // name |
|
723 | - if(!empty($args['name'])){ |
|
724 | - $output .= AUI_Component_Helper::name($args['name'],$args['multiple']); |
|
725 | - } |
|
726 | - |
|
727 | - // id |
|
728 | - if(!empty($args['id'])){ |
|
729 | - $output .= AUI_Component_Helper::id($args['id']); |
|
730 | - } |
|
731 | - |
|
732 | - // title |
|
733 | - if(!empty($args['title'])){ |
|
734 | - $output .= AUI_Component_Helper::title($args['title']); |
|
735 | - } |
|
736 | - |
|
737 | - // data-attributes |
|
738 | - $output .= AUI_Component_Helper::data_attributes($args); |
|
739 | - |
|
740 | - // aria-attributes |
|
741 | - $output .= AUI_Component_Helper::aria_attributes($args); |
|
742 | - |
|
743 | - // extra attributes |
|
744 | - if(!empty($args['extra_attributes'])){ |
|
745 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
746 | - } |
|
747 | - |
|
748 | - // required |
|
749 | - if(!empty($args['required'])){ |
|
750 | - $output .= ' required '; |
|
751 | - } |
|
752 | - |
|
753 | - // multiple |
|
754 | - if(!empty($args['multiple'])){ |
|
755 | - $output .= ' multiple '; |
|
756 | - } |
|
757 | - |
|
758 | - // close opening tag |
|
759 | - $output .= ' >'; |
|
760 | - |
|
761 | - // placeholder |
|
762 | - if(isset($args['placeholder']) && '' != $args['placeholder'] && !$is_select2){ |
|
763 | - $output .= '<option value="" disabled selected hidden>'.esc_attr($args['placeholder']).'</option>'; |
|
764 | - }elseif($is_select2 && !empty($args['placeholder'])){ |
|
765 | - $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder |
|
766 | - } |
|
767 | - |
|
768 | - // Options |
|
769 | - if(!empty($args['options'])){ |
|
770 | - |
|
771 | - if(!is_array($args['options'])){ |
|
772 | - $output .= $args['options']; // not the preferred way but an option |
|
773 | - }else{ |
|
774 | - foreach($args['options'] as $val => $name){ |
|
775 | - $selected = ''; |
|
776 | - if(is_array($name)){ |
|
777 | - if (isset($name['optgroup']) && ($name['optgroup'] == 'start' || $name['optgroup'] == 'end')) { |
|
778 | - $option_label = isset($name['label']) ? $name['label'] : ''; |
|
779 | - |
|
780 | - $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr($option_label) . '">' : '</optgroup>'; |
|
781 | - } else { |
|
782 | - $option_label = isset($name['label']) ? $name['label'] : ''; |
|
783 | - $option_value = isset($name['value']) ? $name['value'] : ''; |
|
784 | - if(!empty($args['multiple']) && !empty($args['value']) && is_array($args['value']) ){ |
|
785 | - $selected = in_array($option_value, stripslashes_deep($args['value'])) ? "selected" : ""; |
|
786 | - } elseif(!empty($args['value'])) { |
|
787 | - $selected = selected($option_value,stripslashes_deep($args['value']), false); |
|
788 | - } |
|
789 | - |
|
790 | - $output .= '<option value="' . esc_attr($option_value) . '" ' . $selected . '>' . $option_label . '</option>'; |
|
791 | - } |
|
792 | - }else{ |
|
793 | - if(!empty($args['value'])){ |
|
794 | - if(is_array($args['value'])){ |
|
795 | - $selected = in_array($val,$args['value']) ? 'selected="selected"' : ''; |
|
796 | - } elseif(!empty($args['value'])) { |
|
797 | - $selected = selected( $args['value'], $val, false); |
|
798 | - } |
|
799 | - } |
|
800 | - $output .= '<option value="'.esc_attr($val).'" '.$selected.'>'.esc_attr($name).'</option>'; |
|
801 | - } |
|
802 | - } |
|
803 | - } |
|
804 | - |
|
805 | - } |
|
806 | - |
|
807 | - // closing tag |
|
808 | - $output .= '</select>'; |
|
809 | - |
|
810 | - if(!empty($args['label']) && $label_after){ |
|
811 | - $label_args = array( |
|
812 | - 'title'=> $args['label'], |
|
813 | - 'for'=> $args['id'], |
|
814 | - 'class' => $args['label_class']." ", |
|
815 | - 'label_type' => $args['label_type'] |
|
816 | - ); |
|
817 | - $output .= self::label($label_args); |
|
818 | - } |
|
819 | - |
|
820 | - // help text |
|
821 | - if(!empty($args['help_text'])){ |
|
822 | - $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
823 | - } |
|
824 | - |
|
825 | - // maybe horizontal label |
|
826 | - if($args['label_type']=='horizontal'){ |
|
827 | - $output .= '</div>'; |
|
828 | - } |
|
829 | - |
|
830 | - |
|
831 | - // wrap |
|
832 | - if(!$args['no_wrap']){ |
|
833 | - $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
834 | - $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
835 | - $output = self::wrap(array( |
|
836 | - 'content' => $output, |
|
837 | - 'class' => $wrap_class, |
|
838 | - 'element_require' => $args['element_require'], |
|
839 | - 'argument_id' => $args['id'] |
|
840 | - )); |
|
841 | - } |
|
842 | - |
|
843 | - |
|
844 | - return $output; |
|
845 | - } |
|
846 | - |
|
847 | - /** |
|
848 | - * Build the component. |
|
849 | - * |
|
850 | - * @param array $args |
|
851 | - * |
|
852 | - * @return string The rendered component. |
|
853 | - */ |
|
854 | - public static function radio($args = array()){ |
|
855 | - $defaults = array( |
|
856 | - 'class' => '', |
|
857 | - 'wrap_class' => '', |
|
858 | - 'id' => '', |
|
859 | - 'title' => '', |
|
860 | - 'horizontal' => false, // sets the lable horizontal |
|
861 | - 'value' => '', |
|
862 | - 'label' => '', |
|
863 | - 'label_class'=> '', |
|
864 | - 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
865 | - 'inline' => true, |
|
866 | - 'required' => false, |
|
867 | - 'options' => array(), |
|
868 | - 'icon' => '', |
|
869 | - 'no_wrap' => false, |
|
870 | - 'element_require' => '', // [%element_id%] == "1" |
|
871 | - 'extra_attributes' => array() // an array of extra attributes |
|
872 | - ); |
|
873 | - |
|
874 | - /** |
|
875 | - * Parse incoming $args into an array and merge it with $defaults |
|
876 | - */ |
|
877 | - $args = wp_parse_args( $args, $defaults ); |
|
878 | - |
|
879 | - // for now lets use horizontal for floating |
|
880 | - if( $args['label_type'] == 'floating' ){$args['label_type'] = 'horizontal';} |
|
881 | - |
|
882 | - $label_args = array( |
|
883 | - 'title'=> $args['label'], |
|
884 | - 'class' => $args['label_class']." pt-0 ", |
|
885 | - 'label_type' => $args['label_type'] |
|
886 | - ); |
|
887 | - |
|
888 | - $output = ''; |
|
889 | - |
|
890 | - |
|
891 | - |
|
892 | - // label before |
|
893 | - if(!empty($args['label'])){ |
|
894 | - $output .= self::label( $label_args, 'radio' ); |
|
895 | - } |
|
896 | - |
|
897 | - // maybe horizontal label |
|
898 | - if($args['label_type']=='horizontal'){ |
|
899 | - $output .= '<div class="col-sm-10">'; |
|
900 | - } |
|
901 | - |
|
902 | - if(!empty($args['options'])){ |
|
903 | - $count = 0; |
|
904 | - foreach($args['options'] as $value => $label){ |
|
905 | - $option_args = $args; |
|
906 | - $option_args['value'] = $value; |
|
907 | - $option_args['label'] = $label; |
|
908 | - $option_args['checked'] = $value == $args['value'] ? true : false; |
|
909 | - $output .= self::radio_option($option_args,$count); |
|
910 | - $count++; |
|
911 | - } |
|
912 | - } |
|
913 | - |
|
914 | - // maybe horizontal label |
|
915 | - if($args['label_type']=='horizontal'){ |
|
916 | - $output .= '</div>'; |
|
917 | - } |
|
918 | - |
|
919 | - |
|
920 | - // wrap |
|
921 | - $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
922 | - $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
923 | - $output = self::wrap(array( |
|
924 | - 'content' => $output, |
|
925 | - 'class' => $wrap_class, |
|
926 | - 'element_require' => $args['element_require'], |
|
927 | - 'argument_id' => $args['id'] |
|
928 | - )); |
|
929 | - |
|
930 | - |
|
931 | - return $output; |
|
932 | - } |
|
933 | - |
|
934 | - /** |
|
935 | - * Build the component. |
|
936 | - * |
|
937 | - * @param array $args |
|
938 | - * |
|
939 | - * @return string The rendered component. |
|
940 | - */ |
|
941 | - public static function radio_option($args = array(),$count = ''){ |
|
942 | - $defaults = array( |
|
943 | - 'class' => '', |
|
944 | - 'id' => '', |
|
945 | - 'title' => '', |
|
946 | - 'value' => '', |
|
947 | - 'required' => false, |
|
948 | - 'inline' => true, |
|
949 | - 'label' => '', |
|
950 | - 'options' => array(), |
|
951 | - 'icon' => '', |
|
952 | - 'no_wrap' => false, |
|
953 | - 'extra_attributes' => array() // an array of extra attributes |
|
954 | - ); |
|
955 | - |
|
956 | - /** |
|
957 | - * Parse incoming $args into an array and merge it with $defaults |
|
958 | - */ |
|
959 | - $args = wp_parse_args( $args, $defaults ); |
|
960 | - |
|
961 | - $output = ''; |
|
962 | - |
|
963 | - // open/type |
|
964 | - $output .= '<input type="radio"'; |
|
965 | - |
|
966 | - // class |
|
967 | - $output .= ' class="form-check-input" '; |
|
968 | - |
|
969 | - // name |
|
970 | - if(!empty($args['name'])){ |
|
971 | - $output .= AUI_Component_Helper::name($args['name']); |
|
972 | - } |
|
973 | - |
|
974 | - // id |
|
975 | - if(!empty($args['id'])){ |
|
976 | - $output .= AUI_Component_Helper::id($args['id'].$count); |
|
977 | - } |
|
978 | - |
|
979 | - // title |
|
980 | - if(!empty($args['title'])){ |
|
981 | - $output .= AUI_Component_Helper::title($args['title']); |
|
982 | - } |
|
983 | - |
|
984 | - // value |
|
985 | - if(isset($args['value'])){ |
|
986 | - $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
987 | - } |
|
988 | - |
|
989 | - // checked, for radio and checkboxes |
|
990 | - if( $args['checked'] ){ |
|
991 | - $output .= ' checked '; |
|
992 | - } |
|
993 | - |
|
994 | - // data-attributes |
|
995 | - $output .= AUI_Component_Helper::data_attributes($args); |
|
996 | - |
|
997 | - // aria-attributes |
|
998 | - $output .= AUI_Component_Helper::aria_attributes($args); |
|
999 | - |
|
1000 | - // extra attributes |
|
1001 | - if(!empty($args['extra_attributes'])){ |
|
1002 | - $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
1003 | - } |
|
1004 | - |
|
1005 | - // required |
|
1006 | - if(!empty($args['required'])){ |
|
1007 | - $output .= ' required '; |
|
1008 | - } |
|
1009 | - |
|
1010 | - // close opening tag |
|
1011 | - $output .= ' >'; |
|
1012 | - |
|
1013 | - // label |
|
1014 | - if(!empty($args['label']) && is_array($args['label'])){ |
|
1015 | - }elseif(!empty($args['label'])){ |
|
1016 | - $output .= self::label(array('title'=>$args['label'],'for'=>$args['id'].$count,'class'=>'form-check-label'),'radio'); |
|
1017 | - } |
|
1018 | - |
|
1019 | - // wrap |
|
1020 | - if(!$args['no_wrap']){ |
|
1021 | - $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
1022 | - $output = self::wrap(array( |
|
1023 | - 'content' => $output, |
|
1024 | - 'class' => $wrap_class |
|
1025 | - )); |
|
1026 | - } |
|
1027 | - |
|
1028 | - |
|
1029 | - return $output; |
|
1030 | - } |
|
213 | + } |
|
214 | + |
|
215 | + // input group wraps |
|
216 | + if($args['input_group_left'] || $args['input_group_right']){ |
|
217 | + $w100 = strpos($args['class'], 'w-100') !== false ? ' w-100' : ''; |
|
218 | + if($args['input_group_left']){ |
|
219 | + $output = self::wrap( array( |
|
220 | + 'content' => $output, |
|
221 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative'.$w100 : 'input-group', |
|
222 | + 'input_group_left' => $args['input_group_left'], |
|
223 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
224 | + ) ); |
|
225 | + } |
|
226 | + if($args['input_group_right']){ |
|
227 | + $output = self::wrap( array( |
|
228 | + 'content' => $output, |
|
229 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative'.$w100 : 'input-group', |
|
230 | + 'input_group_right' => $args['input_group_right'], |
|
231 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
232 | + ) ); |
|
233 | + } |
|
234 | + |
|
235 | + } |
|
236 | + |
|
237 | + if(!$label_after){ |
|
238 | + $output .= $help_text; |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
243 | + $output = self::wrap( array( |
|
244 | + 'content' => $output, |
|
245 | + 'class' => 'col-sm-10', |
|
246 | + ) ); |
|
247 | + } |
|
248 | + |
|
249 | + if(!$label_after){ |
|
250 | + $output = $label . $output; |
|
251 | + } |
|
252 | + |
|
253 | + // wrap |
|
254 | + if(!$args['no_wrap']){ |
|
255 | + |
|
256 | + $form_group_class = $args['label_type']=='floating' && $type != 'checkbox' ? 'form-label-group' : 'form-group'; |
|
257 | + $wrap_class = $args['label_type']=='horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
258 | + $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
259 | + $output = self::wrap(array( |
|
260 | + 'content' => $output, |
|
261 | + 'class' => $wrap_class, |
|
262 | + 'element_require' => $args['element_require'], |
|
263 | + 'argument_id' => $args['id'] |
|
264 | + )); |
|
265 | + } |
|
266 | + |
|
267 | + |
|
268 | + |
|
269 | + } |
|
270 | + |
|
271 | + return $output; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Build the component. |
|
276 | + * |
|
277 | + * @param array $args |
|
278 | + * |
|
279 | + * @return string The rendered component. |
|
280 | + */ |
|
281 | + public static function textarea($args = array()){ |
|
282 | + $defaults = array( |
|
283 | + 'name' => '', |
|
284 | + 'class' => '', |
|
285 | + 'wrap_class' => '', |
|
286 | + 'id' => '', |
|
287 | + 'placeholder'=> '', |
|
288 | + 'title' => '', |
|
289 | + 'value' => '', |
|
290 | + 'required' => false, |
|
291 | + 'label' => '', |
|
292 | + 'label_after'=> false, |
|
293 | + 'label_class' => '', |
|
294 | + 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
295 | + 'help_text' => '', |
|
296 | + 'validation_text' => '', |
|
297 | + 'validation_pattern' => '', |
|
298 | + 'no_wrap' => false, |
|
299 | + 'rows' => '', |
|
300 | + 'wysiwyg' => false, |
|
301 | + 'allow_tags' => false, // Allow HTML tags |
|
302 | + 'element_require' => '', // [%element_id%] == "1" |
|
303 | + 'extra_attributes' => array(), // an array of extra attributes |
|
304 | + ); |
|
305 | + |
|
306 | + /** |
|
307 | + * Parse incoming $args into an array and merge it with $defaults |
|
308 | + */ |
|
309 | + $args = wp_parse_args( $args, $defaults ); |
|
310 | + $output = ''; |
|
311 | + |
|
312 | + // hidden label option needs to be empty |
|
313 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
314 | + |
|
315 | + // floating labels don't work with wysiwyg so set it as top |
|
316 | + if($args['label_type'] == 'floating' && !empty($args['wysiwyg'])){ |
|
317 | + $args['label_type'] = 'top'; |
|
318 | + } |
|
319 | + |
|
320 | + $label_after = $args['label_after']; |
|
321 | + |
|
322 | + // floating labels need label after |
|
323 | + if( $args['label_type'] == 'floating' && empty($args['wysiwyg']) ){ |
|
324 | + $label_after = true; |
|
325 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
326 | + } |
|
327 | + |
|
328 | + // label |
|
329 | + if(!empty($args['label']) && is_array($args['label'])){ |
|
330 | + }elseif(!empty($args['label']) && !$label_after){ |
|
331 | + $label_args = array( |
|
332 | + 'title'=> $args['label'], |
|
333 | + 'for'=> $args['id'], |
|
334 | + 'class' => $args['label_class']." ", |
|
335 | + 'label_type' => $args['label_type'] |
|
336 | + ); |
|
337 | + $output .= self::label( $label_args ); |
|
338 | + } |
|
339 | + |
|
340 | + // maybe horizontal label |
|
341 | + if($args['label_type']=='horizontal'){ |
|
342 | + $output .= '<div class="col-sm-10">'; |
|
343 | + } |
|
344 | + |
|
345 | + if(!empty($args['wysiwyg'])){ |
|
346 | + ob_start(); |
|
347 | + $content = $args['value']; |
|
348 | + $editor_id = !empty($args['id']) ? sanitize_html_class($args['id']) : 'wp_editor'; |
|
349 | + $settings = array( |
|
350 | + 'textarea_rows' => !empty(absint($args['rows'])) ? absint($args['rows']) : 4, |
|
351 | + 'quicktags' => false, |
|
352 | + 'media_buttons' => false, |
|
353 | + 'editor_class' => 'form-control', |
|
354 | + 'textarea_name' => !empty($args['name']) ? sanitize_html_class($args['name']) : sanitize_html_class($args['id']), |
|
355 | + 'teeny' => true, |
|
356 | + ); |
|
357 | + |
|
358 | + // maybe set settings if array |
|
359 | + if(is_array($args['wysiwyg'])){ |
|
360 | + $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
361 | + } |
|
362 | + |
|
363 | + wp_editor( $content, $editor_id, $settings ); |
|
364 | + $output .= ob_get_clean(); |
|
365 | + }else{ |
|
366 | + |
|
367 | + // open |
|
368 | + $output .= '<textarea '; |
|
369 | + |
|
370 | + // name |
|
371 | + if(!empty($args['name'])){ |
|
372 | + $output .= ' name="'.sanitize_html_class($args['name']).'" '; |
|
373 | + } |
|
374 | + |
|
375 | + // id |
|
376 | + if(!empty($args['id'])){ |
|
377 | + $output .= ' id="'.sanitize_html_class($args['id']).'" '; |
|
378 | + } |
|
379 | + |
|
380 | + // placeholder |
|
381 | + if(isset($args['placeholder']) && '' != $args['placeholder']){ |
|
382 | + $output .= ' placeholder="'.esc_attr($args['placeholder']).'" '; |
|
383 | + } |
|
384 | + |
|
385 | + // title |
|
386 | + if(!empty($args['title'])){ |
|
387 | + $output .= ' title="'.esc_attr($args['title']).'" '; |
|
388 | + } |
|
389 | + |
|
390 | + // validation text |
|
391 | + if(!empty($args['validation_text'])){ |
|
392 | + $output .= ' oninvalid="setCustomValidity(\''.esc_attr($args['validation_text']).'\')" '; |
|
393 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
394 | + } |
|
395 | + |
|
396 | + // validation_pattern |
|
397 | + if(!empty($args['validation_pattern'])){ |
|
398 | + $output .= ' pattern="'.$args['validation_pattern'].'" '; |
|
399 | + } |
|
400 | + |
|
401 | + // required |
|
402 | + if(!empty($args['required'])){ |
|
403 | + $output .= ' required '; |
|
404 | + } |
|
405 | + |
|
406 | + // rows |
|
407 | + if(!empty($args['rows'])){ |
|
408 | + $output .= ' rows="'.absint($args['rows']).'" '; |
|
409 | + } |
|
410 | + |
|
411 | + |
|
412 | + // class |
|
413 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
414 | + $output .= ' class="form-control '.$class.'" '; |
|
415 | + |
|
416 | + // extra attributes |
|
417 | + if(!empty($args['extra_attributes'])){ |
|
418 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
419 | + } |
|
420 | + |
|
421 | + // close tag |
|
422 | + $output .= ' >'; |
|
423 | + |
|
424 | + // value |
|
425 | + if ( ! empty( $args['value'] ) ) { |
|
426 | + if ( ! empty( $args['allow_tags'] ) ) { |
|
427 | + $output .= AUI_Component_Helper::sanitize_html_field( $args['value'], $args ); // Sanitize HTML. |
|
428 | + } else { |
|
429 | + $output .= sanitize_textarea_field( $args['value'] ); |
|
430 | + } |
|
431 | + } |
|
432 | + |
|
433 | + // closing tag |
|
434 | + $output .= '</textarea>'; |
|
435 | + |
|
436 | + } |
|
437 | + |
|
438 | + if(!empty($args['label']) && $label_after){ |
|
439 | + $label_args = array( |
|
440 | + 'title'=> $args['label'], |
|
441 | + 'for'=> $args['id'], |
|
442 | + 'class' => $args['label_class']." ", |
|
443 | + 'label_type' => $args['label_type'] |
|
444 | + ); |
|
445 | + $output .= self::label( $label_args ); |
|
446 | + } |
|
447 | + |
|
448 | + // help text |
|
449 | + if(!empty($args['help_text'])){ |
|
450 | + $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
451 | + } |
|
452 | + |
|
453 | + // maybe horizontal label |
|
454 | + if($args['label_type']=='horizontal'){ |
|
455 | + $output .= '</div>'; |
|
456 | + } |
|
457 | + |
|
458 | + |
|
459 | + // wrap |
|
460 | + if(!$args['no_wrap']){ |
|
461 | + $form_group_class = $args['label_type']=='floating' ? 'form-label-group' : 'form-group'; |
|
462 | + $wrap_class = $args['label_type']=='horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
463 | + $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
464 | + $output = self::wrap(array( |
|
465 | + 'content' => $output, |
|
466 | + 'class' => $wrap_class, |
|
467 | + 'element_require' => $args['element_require'], |
|
468 | + 'argument_id' => $args['id'] |
|
469 | + )); |
|
470 | + } |
|
471 | + |
|
472 | + |
|
473 | + return $output; |
|
474 | + } |
|
475 | + |
|
476 | + public static function label($args = array(), $type = ''){ |
|
477 | + //<label for="exampleInputEmail1">Email address</label> |
|
478 | + $defaults = array( |
|
479 | + 'title' => 'div', |
|
480 | + 'for' => '', |
|
481 | + 'class' => '', |
|
482 | + 'label_type' => '', // empty = hidden, top, horizontal |
|
483 | + ); |
|
484 | + |
|
485 | + /** |
|
486 | + * Parse incoming $args into an array and merge it with $defaults |
|
487 | + */ |
|
488 | + $args = wp_parse_args( $args, $defaults ); |
|
489 | + $output = ''; |
|
490 | + |
|
491 | + if($args['title']){ |
|
492 | + |
|
493 | + // maybe hide labels //@todo set a global option for visibility class |
|
494 | + if($type == 'file' || $type == 'checkbox' || $type == 'radio' || !empty($args['label_type']) ){ |
|
495 | + $class = $args['class']; |
|
496 | + }else{ |
|
497 | + $class = 'sr-only '.$args['class']; |
|
498 | + } |
|
499 | + |
|
500 | + // maybe horizontal |
|
501 | + if($args['label_type']=='horizontal' && $type != 'checkbox'){ |
|
502 | + $class .= ' col-sm-2 col-form-label'; |
|
503 | + } |
|
504 | + |
|
505 | + // open |
|
506 | + $output .= '<label '; |
|
507 | + |
|
508 | + // for |
|
509 | + if(!empty($args['for'])){ |
|
510 | + $output .= ' for="'.sanitize_text_field($args['for']).'" '; |
|
511 | + } |
|
512 | + |
|
513 | + // class |
|
514 | + $class = $class ? AUI_Component_Helper::esc_classes( $class ) : ''; |
|
515 | + $output .= ' class="'.$class.'" '; |
|
516 | + |
|
517 | + // close |
|
518 | + $output .= '>'; |
|
519 | + |
|
520 | + |
|
521 | + // title, don't escape fully as can contain html |
|
522 | + if(!empty($args['title'])){ |
|
523 | + $output .= wp_kses_post($args['title']); |
|
524 | + } |
|
525 | + |
|
526 | + // close wrap |
|
527 | + $output .= '</label>'; |
|
528 | + |
|
529 | + |
|
530 | + } |
|
531 | + |
|
532 | + |
|
533 | + return $output; |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * Wrap some content in a HTML wrapper. |
|
538 | + * |
|
539 | + * @param array $args |
|
540 | + * |
|
541 | + * @return string |
|
542 | + */ |
|
543 | + public static function wrap($args = array()){ |
|
544 | + $defaults = array( |
|
545 | + 'type' => 'div', |
|
546 | + 'class' => 'form-group', |
|
547 | + 'content' => '', |
|
548 | + 'input_group_left' => '', |
|
549 | + 'input_group_right' => '', |
|
550 | + 'input_group_left_inside' => false, |
|
551 | + 'input_group_right_inside' => false, |
|
552 | + 'element_require' => '', |
|
553 | + 'argument_id' => '', |
|
554 | + ); |
|
555 | + |
|
556 | + /** |
|
557 | + * Parse incoming $args into an array and merge it with $defaults |
|
558 | + */ |
|
559 | + $args = wp_parse_args( $args, $defaults ); |
|
560 | + $output = ''; |
|
561 | + if($args['type']){ |
|
562 | + |
|
563 | + // open |
|
564 | + $output .= '<'.sanitize_html_class($args['type']); |
|
565 | + |
|
566 | + // element require |
|
567 | + if(!empty($args['element_require'])){ |
|
568 | + $output .= AUI_Component_Helper::element_require($args['element_require']); |
|
569 | + $args['class'] .= " aui-conditional-field"; |
|
570 | + } |
|
571 | + |
|
572 | + // argument_id |
|
573 | + if( !empty($args['argument_id']) ){ |
|
574 | + $output .= ' data-argument="'.esc_attr($args['argument_id']).'"'; |
|
575 | + } |
|
576 | + |
|
577 | + // class |
|
578 | + $class = !empty($args['class']) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
579 | + $output .= ' class="'.$class.'" '; |
|
580 | + |
|
581 | + // close wrap |
|
582 | + $output .= ' >'; |
|
583 | + |
|
584 | + |
|
585 | + // Input group left |
|
586 | + if(!empty($args['input_group_left'])){ |
|
587 | + $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute h-100' : ''; |
|
588 | + $input_group_left = strpos($args['input_group_left'], '<') !== false ? $args['input_group_left'] : '<span class="input-group-text">'.$args['input_group_left'].'</span>'; |
|
589 | + $output .= '<div class="input-group-prepend '.$position_class.'">'.$input_group_left.'</div>'; |
|
590 | + } |
|
591 | + |
|
592 | + // content |
|
593 | + $output .= $args['content']; |
|
594 | + |
|
595 | + // Input group right |
|
596 | + if(!empty($args['input_group_right'])){ |
|
597 | + $position_class = !empty($args['input_group_left_inside']) ? 'position-absolute h-100' : ''; |
|
598 | + $input_group_right = strpos($args['input_group_right'], '<') !== false ? $args['input_group_right'] : '<span class="input-group-text">'.$args['input_group_right'].'</span>'; |
|
599 | + $output .= '<div class="input-group-append '.$position_class.'">'.$input_group_right.'</div>'; |
|
600 | + } |
|
601 | + |
|
602 | + |
|
603 | + // close wrap |
|
604 | + $output .= '</'.sanitize_html_class($args['type']).'>'; |
|
605 | + |
|
606 | + |
|
607 | + }else{ |
|
608 | + $output = $args['content']; |
|
609 | + } |
|
610 | + |
|
611 | + return $output; |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * Build the component. |
|
616 | + * |
|
617 | + * @param array $args |
|
618 | + * |
|
619 | + * @return string The rendered component. |
|
620 | + */ |
|
621 | + public static function select($args = array()){ |
|
622 | + $defaults = array( |
|
623 | + 'class' => '', |
|
624 | + 'wrap_class' => '', |
|
625 | + 'id' => '', |
|
626 | + 'title' => '', |
|
627 | + 'value' => '', // can be an array or a string |
|
628 | + 'required' => false, |
|
629 | + 'label' => '', |
|
630 | + 'label_after'=> false, |
|
631 | + 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
632 | + 'label_class' => '', |
|
633 | + 'help_text' => '', |
|
634 | + 'placeholder'=> '', |
|
635 | + 'options' => array(), // array or string |
|
636 | + 'icon' => '', |
|
637 | + 'multiple' => false, |
|
638 | + 'select2' => false, |
|
639 | + 'no_wrap' => false, |
|
640 | + 'element_require' => '', // [%element_id%] == "1" |
|
641 | + 'extra_attributes' => array(), // an array of extra attributes |
|
642 | + ); |
|
643 | + |
|
644 | + /** |
|
645 | + * Parse incoming $args into an array and merge it with $defaults |
|
646 | + */ |
|
647 | + $args = wp_parse_args( $args, $defaults ); |
|
648 | + $output = ''; |
|
649 | + |
|
650 | + // for now lets hide floating labels |
|
651 | + if( $args['label_type'] == 'floating' ){$args['label_type'] = 'hidden';} |
|
652 | + |
|
653 | + // hidden label option needs to be empty |
|
654 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
655 | + |
|
656 | + |
|
657 | + $label_after = $args['label_after']; |
|
658 | + |
|
659 | + // floating labels need label after |
|
660 | + if( $args['label_type'] == 'floating' ){ |
|
661 | + $label_after = true; |
|
662 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
663 | + } |
|
664 | + |
|
665 | + // Maybe setup select2 |
|
666 | + $is_select2 = false; |
|
667 | + if(!empty($args['select2'])){ |
|
668 | + $args['class'] .= ' aui-select2'; |
|
669 | + $is_select2 = true; |
|
670 | + }elseif( strpos($args['class'], 'aui-select2') !== false){ |
|
671 | + $is_select2 = true; |
|
672 | + } |
|
673 | + |
|
674 | + // select2 tags |
|
675 | + if( !empty($args['select2']) && $args['select2'] === 'tags'){ // triple equals needed here for some reason |
|
676 | + $args['data-tags'] = 'true'; |
|
677 | + $args['data-token-separators'] = "[',']"; |
|
678 | + $args['multiple'] = true; |
|
679 | + } |
|
680 | + |
|
681 | + // select2 placeholder |
|
682 | + if($is_select2 && isset($args['placeholder']) && '' != $args['placeholder'] && empty($args['data-placeholder'])){ |
|
683 | + $args['data-placeholder'] = esc_attr($args['placeholder']); |
|
684 | + $args['data-allow-clear'] = isset($args['data-allow-clear']) ? (bool) $args['data-allow-clear'] : true; |
|
685 | + } |
|
686 | + |
|
687 | + // label |
|
688 | + if(!empty($args['label']) && is_array($args['label'])){ |
|
689 | + }elseif(!empty($args['label']) && !$label_after){ |
|
690 | + $label_args = array( |
|
691 | + 'title'=> $args['label'], |
|
692 | + 'for'=> $args['id'], |
|
693 | + 'class' => $args['label_class']." ", |
|
694 | + 'label_type' => $args['label_type'] |
|
695 | + ); |
|
696 | + $output .= self::label($label_args); |
|
697 | + } |
|
698 | + |
|
699 | + // maybe horizontal label |
|
700 | + if($args['label_type']=='horizontal'){ |
|
701 | + $output .= '<div class="col-sm-10">'; |
|
702 | + } |
|
703 | + |
|
704 | + // open/type |
|
705 | + $output .= '<select '; |
|
706 | + |
|
707 | + // style |
|
708 | + if($is_select2){ |
|
709 | + $output .= " style='width:100%;' "; |
|
710 | + } |
|
711 | + |
|
712 | + // element require |
|
713 | + if(!empty($args['element_require'])){ |
|
714 | + $output .= AUI_Component_Helper::element_require($args['element_require']); |
|
715 | + $args['class'] .= " aui-conditional-field"; |
|
716 | + } |
|
717 | + |
|
718 | + // class |
|
719 | + $class = !empty($args['class']) ? $args['class'] : ''; |
|
720 | + $output .= AUI_Component_Helper::class_attr('custom-select '.$class); |
|
721 | + |
|
722 | + // name |
|
723 | + if(!empty($args['name'])){ |
|
724 | + $output .= AUI_Component_Helper::name($args['name'],$args['multiple']); |
|
725 | + } |
|
726 | + |
|
727 | + // id |
|
728 | + if(!empty($args['id'])){ |
|
729 | + $output .= AUI_Component_Helper::id($args['id']); |
|
730 | + } |
|
731 | + |
|
732 | + // title |
|
733 | + if(!empty($args['title'])){ |
|
734 | + $output .= AUI_Component_Helper::title($args['title']); |
|
735 | + } |
|
736 | + |
|
737 | + // data-attributes |
|
738 | + $output .= AUI_Component_Helper::data_attributes($args); |
|
739 | + |
|
740 | + // aria-attributes |
|
741 | + $output .= AUI_Component_Helper::aria_attributes($args); |
|
742 | + |
|
743 | + // extra attributes |
|
744 | + if(!empty($args['extra_attributes'])){ |
|
745 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
746 | + } |
|
747 | + |
|
748 | + // required |
|
749 | + if(!empty($args['required'])){ |
|
750 | + $output .= ' required '; |
|
751 | + } |
|
752 | + |
|
753 | + // multiple |
|
754 | + if(!empty($args['multiple'])){ |
|
755 | + $output .= ' multiple '; |
|
756 | + } |
|
757 | + |
|
758 | + // close opening tag |
|
759 | + $output .= ' >'; |
|
760 | + |
|
761 | + // placeholder |
|
762 | + if(isset($args['placeholder']) && '' != $args['placeholder'] && !$is_select2){ |
|
763 | + $output .= '<option value="" disabled selected hidden>'.esc_attr($args['placeholder']).'</option>'; |
|
764 | + }elseif($is_select2 && !empty($args['placeholder'])){ |
|
765 | + $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder |
|
766 | + } |
|
767 | + |
|
768 | + // Options |
|
769 | + if(!empty($args['options'])){ |
|
770 | + |
|
771 | + if(!is_array($args['options'])){ |
|
772 | + $output .= $args['options']; // not the preferred way but an option |
|
773 | + }else{ |
|
774 | + foreach($args['options'] as $val => $name){ |
|
775 | + $selected = ''; |
|
776 | + if(is_array($name)){ |
|
777 | + if (isset($name['optgroup']) && ($name['optgroup'] == 'start' || $name['optgroup'] == 'end')) { |
|
778 | + $option_label = isset($name['label']) ? $name['label'] : ''; |
|
779 | + |
|
780 | + $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr($option_label) . '">' : '</optgroup>'; |
|
781 | + } else { |
|
782 | + $option_label = isset($name['label']) ? $name['label'] : ''; |
|
783 | + $option_value = isset($name['value']) ? $name['value'] : ''; |
|
784 | + if(!empty($args['multiple']) && !empty($args['value']) && is_array($args['value']) ){ |
|
785 | + $selected = in_array($option_value, stripslashes_deep($args['value'])) ? "selected" : ""; |
|
786 | + } elseif(!empty($args['value'])) { |
|
787 | + $selected = selected($option_value,stripslashes_deep($args['value']), false); |
|
788 | + } |
|
789 | + |
|
790 | + $output .= '<option value="' . esc_attr($option_value) . '" ' . $selected . '>' . $option_label . '</option>'; |
|
791 | + } |
|
792 | + }else{ |
|
793 | + if(!empty($args['value'])){ |
|
794 | + if(is_array($args['value'])){ |
|
795 | + $selected = in_array($val,$args['value']) ? 'selected="selected"' : ''; |
|
796 | + } elseif(!empty($args['value'])) { |
|
797 | + $selected = selected( $args['value'], $val, false); |
|
798 | + } |
|
799 | + } |
|
800 | + $output .= '<option value="'.esc_attr($val).'" '.$selected.'>'.esc_attr($name).'</option>'; |
|
801 | + } |
|
802 | + } |
|
803 | + } |
|
804 | + |
|
805 | + } |
|
806 | + |
|
807 | + // closing tag |
|
808 | + $output .= '</select>'; |
|
809 | + |
|
810 | + if(!empty($args['label']) && $label_after){ |
|
811 | + $label_args = array( |
|
812 | + 'title'=> $args['label'], |
|
813 | + 'for'=> $args['id'], |
|
814 | + 'class' => $args['label_class']." ", |
|
815 | + 'label_type' => $args['label_type'] |
|
816 | + ); |
|
817 | + $output .= self::label($label_args); |
|
818 | + } |
|
819 | + |
|
820 | + // help text |
|
821 | + if(!empty($args['help_text'])){ |
|
822 | + $output .= AUI_Component_Helper::help_text($args['help_text']); |
|
823 | + } |
|
824 | + |
|
825 | + // maybe horizontal label |
|
826 | + if($args['label_type']=='horizontal'){ |
|
827 | + $output .= '</div>'; |
|
828 | + } |
|
829 | + |
|
830 | + |
|
831 | + // wrap |
|
832 | + if(!$args['no_wrap']){ |
|
833 | + $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
834 | + $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
835 | + $output = self::wrap(array( |
|
836 | + 'content' => $output, |
|
837 | + 'class' => $wrap_class, |
|
838 | + 'element_require' => $args['element_require'], |
|
839 | + 'argument_id' => $args['id'] |
|
840 | + )); |
|
841 | + } |
|
842 | + |
|
843 | + |
|
844 | + return $output; |
|
845 | + } |
|
846 | + |
|
847 | + /** |
|
848 | + * Build the component. |
|
849 | + * |
|
850 | + * @param array $args |
|
851 | + * |
|
852 | + * @return string The rendered component. |
|
853 | + */ |
|
854 | + public static function radio($args = array()){ |
|
855 | + $defaults = array( |
|
856 | + 'class' => '', |
|
857 | + 'wrap_class' => '', |
|
858 | + 'id' => '', |
|
859 | + 'title' => '', |
|
860 | + 'horizontal' => false, // sets the lable horizontal |
|
861 | + 'value' => '', |
|
862 | + 'label' => '', |
|
863 | + 'label_class'=> '', |
|
864 | + 'label_type' => '', // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
865 | + 'inline' => true, |
|
866 | + 'required' => false, |
|
867 | + 'options' => array(), |
|
868 | + 'icon' => '', |
|
869 | + 'no_wrap' => false, |
|
870 | + 'element_require' => '', // [%element_id%] == "1" |
|
871 | + 'extra_attributes' => array() // an array of extra attributes |
|
872 | + ); |
|
873 | + |
|
874 | + /** |
|
875 | + * Parse incoming $args into an array and merge it with $defaults |
|
876 | + */ |
|
877 | + $args = wp_parse_args( $args, $defaults ); |
|
878 | + |
|
879 | + // for now lets use horizontal for floating |
|
880 | + if( $args['label_type'] == 'floating' ){$args['label_type'] = 'horizontal';} |
|
881 | + |
|
882 | + $label_args = array( |
|
883 | + 'title'=> $args['label'], |
|
884 | + 'class' => $args['label_class']." pt-0 ", |
|
885 | + 'label_type' => $args['label_type'] |
|
886 | + ); |
|
887 | + |
|
888 | + $output = ''; |
|
889 | + |
|
890 | + |
|
891 | + |
|
892 | + // label before |
|
893 | + if(!empty($args['label'])){ |
|
894 | + $output .= self::label( $label_args, 'radio' ); |
|
895 | + } |
|
896 | + |
|
897 | + // maybe horizontal label |
|
898 | + if($args['label_type']=='horizontal'){ |
|
899 | + $output .= '<div class="col-sm-10">'; |
|
900 | + } |
|
901 | + |
|
902 | + if(!empty($args['options'])){ |
|
903 | + $count = 0; |
|
904 | + foreach($args['options'] as $value => $label){ |
|
905 | + $option_args = $args; |
|
906 | + $option_args['value'] = $value; |
|
907 | + $option_args['label'] = $label; |
|
908 | + $option_args['checked'] = $value == $args['value'] ? true : false; |
|
909 | + $output .= self::radio_option($option_args,$count); |
|
910 | + $count++; |
|
911 | + } |
|
912 | + } |
|
913 | + |
|
914 | + // maybe horizontal label |
|
915 | + if($args['label_type']=='horizontal'){ |
|
916 | + $output .= '</div>'; |
|
917 | + } |
|
918 | + |
|
919 | + |
|
920 | + // wrap |
|
921 | + $wrap_class = $args['label_type']=='horizontal' ? 'form-group row' : 'form-group'; |
|
922 | + $wrap_class = !empty($args['wrap_class']) ? $wrap_class." ".$args['wrap_class'] : $wrap_class; |
|
923 | + $output = self::wrap(array( |
|
924 | + 'content' => $output, |
|
925 | + 'class' => $wrap_class, |
|
926 | + 'element_require' => $args['element_require'], |
|
927 | + 'argument_id' => $args['id'] |
|
928 | + )); |
|
929 | + |
|
930 | + |
|
931 | + return $output; |
|
932 | + } |
|
933 | + |
|
934 | + /** |
|
935 | + * Build the component. |
|
936 | + * |
|
937 | + * @param array $args |
|
938 | + * |
|
939 | + * @return string The rendered component. |
|
940 | + */ |
|
941 | + public static function radio_option($args = array(),$count = ''){ |
|
942 | + $defaults = array( |
|
943 | + 'class' => '', |
|
944 | + 'id' => '', |
|
945 | + 'title' => '', |
|
946 | + 'value' => '', |
|
947 | + 'required' => false, |
|
948 | + 'inline' => true, |
|
949 | + 'label' => '', |
|
950 | + 'options' => array(), |
|
951 | + 'icon' => '', |
|
952 | + 'no_wrap' => false, |
|
953 | + 'extra_attributes' => array() // an array of extra attributes |
|
954 | + ); |
|
955 | + |
|
956 | + /** |
|
957 | + * Parse incoming $args into an array and merge it with $defaults |
|
958 | + */ |
|
959 | + $args = wp_parse_args( $args, $defaults ); |
|
960 | + |
|
961 | + $output = ''; |
|
962 | + |
|
963 | + // open/type |
|
964 | + $output .= '<input type="radio"'; |
|
965 | + |
|
966 | + // class |
|
967 | + $output .= ' class="form-check-input" '; |
|
968 | + |
|
969 | + // name |
|
970 | + if(!empty($args['name'])){ |
|
971 | + $output .= AUI_Component_Helper::name($args['name']); |
|
972 | + } |
|
973 | + |
|
974 | + // id |
|
975 | + if(!empty($args['id'])){ |
|
976 | + $output .= AUI_Component_Helper::id($args['id'].$count); |
|
977 | + } |
|
978 | + |
|
979 | + // title |
|
980 | + if(!empty($args['title'])){ |
|
981 | + $output .= AUI_Component_Helper::title($args['title']); |
|
982 | + } |
|
983 | + |
|
984 | + // value |
|
985 | + if(isset($args['value'])){ |
|
986 | + $output .= ' value="'.sanitize_text_field($args['value']).'" '; |
|
987 | + } |
|
988 | + |
|
989 | + // checked, for radio and checkboxes |
|
990 | + if( $args['checked'] ){ |
|
991 | + $output .= ' checked '; |
|
992 | + } |
|
993 | + |
|
994 | + // data-attributes |
|
995 | + $output .= AUI_Component_Helper::data_attributes($args); |
|
996 | + |
|
997 | + // aria-attributes |
|
998 | + $output .= AUI_Component_Helper::aria_attributes($args); |
|
999 | + |
|
1000 | + // extra attributes |
|
1001 | + if(!empty($args['extra_attributes'])){ |
|
1002 | + $output .= AUI_Component_Helper::extra_attributes($args['extra_attributes']); |
|
1003 | + } |
|
1004 | + |
|
1005 | + // required |
|
1006 | + if(!empty($args['required'])){ |
|
1007 | + $output .= ' required '; |
|
1008 | + } |
|
1009 | + |
|
1010 | + // close opening tag |
|
1011 | + $output .= ' >'; |
|
1012 | + |
|
1013 | + // label |
|
1014 | + if(!empty($args['label']) && is_array($args['label'])){ |
|
1015 | + }elseif(!empty($args['label'])){ |
|
1016 | + $output .= self::label(array('title'=>$args['label'],'for'=>$args['id'].$count,'class'=>'form-check-label'),'radio'); |
|
1017 | + } |
|
1018 | + |
|
1019 | + // wrap |
|
1020 | + if(!$args['no_wrap']){ |
|
1021 | + $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
1022 | + $output = self::wrap(array( |
|
1023 | + 'content' => $output, |
|
1024 | + 'class' => $wrap_class |
|
1025 | + )); |
|
1026 | + } |
|
1027 | + |
|
1028 | + |
|
1029 | + return $output; |
|
1030 | + } |
|
1031 | 1031 | |
1032 | 1032 | } |
1033 | 1033 | \ No newline at end of file |
@@ -15,30 +15,30 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class WPInv_Discount extends GetPaid_Data { |
17 | 17 | |
18 | - /** |
|
19 | - * Which data store to load. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
18 | + /** |
|
19 | + * Which data store to load. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | 23 | protected $data_store_name = 'discount'; |
24 | 24 | |
25 | 25 | /** |
26 | - * This is the name of this object type. |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $object_type = 'discount'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Discount Data array. This is the core item data exposed in APIs. |
|
34 | - * |
|
35 | - * @since 1.0.19 |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $data = array( |
|
39 | - 'status' => 'draft', |
|
40 | - 'version' => '', |
|
41 | - 'date_created' => null, |
|
26 | + * This is the name of this object type. |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $object_type = 'discount'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Discount Data array. This is the core item data exposed in APIs. |
|
34 | + * |
|
35 | + * @since 1.0.19 |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $data = array( |
|
39 | + 'status' => 'draft', |
|
40 | + 'version' => '', |
|
41 | + 'date_created' => null, |
|
42 | 42 | 'date_modified' => null, |
43 | 43 | 'name' => 'no-name', |
44 | 44 | 'description' => '', |
@@ -58,144 +58,144 @@ discard block |
||
58 | 58 | 'amount' => null, |
59 | 59 | ); |
60 | 60 | |
61 | - /** |
|
62 | - * Stores meta in cache for future reads. |
|
63 | - * |
|
64 | - * A group must be set to to enable caching. |
|
65 | - * |
|
66 | - * @var string |
|
67 | - */ |
|
68 | - protected $cache_group = 'getpaid_discounts'; |
|
61 | + /** |
|
62 | + * Stores meta in cache for future reads. |
|
63 | + * |
|
64 | + * A group must be set to to enable caching. |
|
65 | + * |
|
66 | + * @var string |
|
67 | + */ |
|
68 | + protected $cache_group = 'getpaid_discounts'; |
|
69 | 69 | |
70 | 70 | /** |
71 | 71 | * Stores a reference to the original WP_Post object |
72 | 72 | * |
73 | 73 | * @var WP_Post |
74 | 74 | */ |
75 | - protected $post = null; |
|
76 | - |
|
77 | - /** |
|
78 | - * Get the discount if ID is passed, otherwise the discount is new and empty. |
|
79 | - * |
|
80 | - * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code. |
|
81 | - */ |
|
82 | - public function __construct( $discount = 0 ) { |
|
83 | - parent::__construct( $discount ); |
|
84 | - |
|
85 | - if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) { |
|
86 | - $this->set_id( $discount ); |
|
87 | - } elseif ( $discount instanceof self ) { |
|
88 | - $this->set_id( $discount->get_id() ); |
|
89 | - } elseif ( ! empty( $discount->ID ) ) { |
|
90 | - $this->set_id( $discount->ID ); |
|
91 | - } elseif ( is_array( $discount ) ) { |
|
92 | - $this->set_props( $discount ); |
|
93 | - |
|
94 | - if ( isset( $discount['ID'] ) ) { |
|
95 | - $this->set_id( $discount['ID'] ); |
|
96 | - } |
|
97 | - |
|
98 | - } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) { |
|
99 | - $this->set_id( $discount ); |
|
100 | - } else { |
|
101 | - $this->set_object_read( true ); |
|
102 | - } |
|
75 | + protected $post = null; |
|
76 | + |
|
77 | + /** |
|
78 | + * Get the discount if ID is passed, otherwise the discount is new and empty. |
|
79 | + * |
|
80 | + * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code. |
|
81 | + */ |
|
82 | + public function __construct( $discount = 0 ) { |
|
83 | + parent::__construct( $discount ); |
|
84 | + |
|
85 | + if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) { |
|
86 | + $this->set_id( $discount ); |
|
87 | + } elseif ( $discount instanceof self ) { |
|
88 | + $this->set_id( $discount->get_id() ); |
|
89 | + } elseif ( ! empty( $discount->ID ) ) { |
|
90 | + $this->set_id( $discount->ID ); |
|
91 | + } elseif ( is_array( $discount ) ) { |
|
92 | + $this->set_props( $discount ); |
|
93 | + |
|
94 | + if ( isset( $discount['ID'] ) ) { |
|
95 | + $this->set_id( $discount['ID'] ); |
|
96 | + } |
|
97 | + |
|
98 | + } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) { |
|
99 | + $this->set_id( $discount ); |
|
100 | + } else { |
|
101 | + $this->set_object_read( true ); |
|
102 | + } |
|
103 | 103 | |
104 | 104 | // Load the datastore. |
105 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
105 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
106 | 106 | |
107 | - if ( $this->get_id() > 0 ) { |
|
107 | + if ( $this->get_id() > 0 ) { |
|
108 | 108 | $this->post = get_post( $this->get_id() ); |
109 | 109 | $this->ID = $this->get_id(); |
110 | - $this->data_store->read( $this ); |
|
110 | + $this->data_store->read( $this ); |
|
111 | + } |
|
112 | + |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Fetch a discount from the db/cache |
|
117 | + * |
|
118 | + * |
|
119 | + * @static |
|
120 | + * @param string $field The field to query against: 'ID', 'discount_code' |
|
121 | + * @param string|int $value The field value |
|
122 | + * @deprecated |
|
123 | + * @since 1.0.15 |
|
124 | + * @return array|bool array of discount details on success. False otherwise. |
|
125 | + */ |
|
126 | + public static function get_data_by( $field, $value ) { |
|
127 | + |
|
128 | + if ( 'id' == strtolower( $field ) ) { |
|
129 | + // Make sure the value is numeric to avoid casting objects, for example, |
|
130 | + // to int 1. |
|
131 | + if ( ! is_numeric( $value ) ) |
|
132 | + return false; |
|
133 | + $value = intval( $value ); |
|
134 | + if ( $value < 1 ) |
|
135 | + return false; |
|
136 | + } |
|
137 | + |
|
138 | + if ( ! $value || ! is_string( $field ) ) { |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + $field = trim( $field ); |
|
143 | + |
|
144 | + // prepare query args |
|
145 | + switch ( strtolower( $field ) ) { |
|
146 | + case 'id': |
|
147 | + $discount_id = $value; |
|
148 | + $args = array( 'include' => array( $value ) ); |
|
149 | + break; |
|
150 | + case 'discount_code': |
|
151 | + case 'code': |
|
152 | + $value = trim( $value ); |
|
153 | + $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
154 | + $args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
155 | + break; |
|
156 | + case 'name': |
|
157 | + $discount_id = 0; |
|
158 | + $args = array( 'name' => trim( $value ) ); |
|
159 | + break; |
|
160 | + default: |
|
161 | + $args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
162 | + if ( ! is_array( $args ) ) { |
|
163 | + return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
164 | + } |
|
165 | + |
|
166 | + } |
|
167 | + |
|
168 | + // Check if there is a cached value. |
|
169 | + if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
170 | + return $discount; |
|
171 | + } |
|
172 | + |
|
173 | + $args = array_merge( |
|
174 | + $args, |
|
175 | + array( |
|
176 | + 'post_type' => 'wpi_discount', |
|
177 | + 'posts_per_page' => 1, |
|
178 | + 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
179 | + ) |
|
180 | + ); |
|
181 | + |
|
182 | + $discount = get_posts( $args ); |
|
183 | + |
|
184 | + if( empty( $discount ) ) { |
|
185 | + return false; |
|
111 | 186 | } |
112 | 187 | |
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Fetch a discount from the db/cache |
|
117 | - * |
|
118 | - * |
|
119 | - * @static |
|
120 | - * @param string $field The field to query against: 'ID', 'discount_code' |
|
121 | - * @param string|int $value The field value |
|
122 | - * @deprecated |
|
123 | - * @since 1.0.15 |
|
124 | - * @return array|bool array of discount details on success. False otherwise. |
|
125 | - */ |
|
126 | - public static function get_data_by( $field, $value ) { |
|
127 | - |
|
128 | - if ( 'id' == strtolower( $field ) ) { |
|
129 | - // Make sure the value is numeric to avoid casting objects, for example, |
|
130 | - // to int 1. |
|
131 | - if ( ! is_numeric( $value ) ) |
|
132 | - return false; |
|
133 | - $value = intval( $value ); |
|
134 | - if ( $value < 1 ) |
|
135 | - return false; |
|
136 | - } |
|
137 | - |
|
138 | - if ( ! $value || ! is_string( $field ) ) { |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - $field = trim( $field ); |
|
143 | - |
|
144 | - // prepare query args |
|
145 | - switch ( strtolower( $field ) ) { |
|
146 | - case 'id': |
|
147 | - $discount_id = $value; |
|
148 | - $args = array( 'include' => array( $value ) ); |
|
149 | - break; |
|
150 | - case 'discount_code': |
|
151 | - case 'code': |
|
152 | - $value = trim( $value ); |
|
153 | - $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
154 | - $args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
155 | - break; |
|
156 | - case 'name': |
|
157 | - $discount_id = 0; |
|
158 | - $args = array( 'name' => trim( $value ) ); |
|
159 | - break; |
|
160 | - default: |
|
161 | - $args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
162 | - if ( ! is_array( $args ) ) { |
|
163 | - return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
164 | - } |
|
165 | - |
|
166 | - } |
|
167 | - |
|
168 | - // Check if there is a cached value. |
|
169 | - if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
170 | - return $discount; |
|
171 | - } |
|
172 | - |
|
173 | - $args = array_merge( |
|
174 | - $args, |
|
175 | - array( |
|
176 | - 'post_type' => 'wpi_discount', |
|
177 | - 'posts_per_page' => 1, |
|
178 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
179 | - ) |
|
180 | - ); |
|
181 | - |
|
182 | - $discount = get_posts( $args ); |
|
183 | - |
|
184 | - if( empty( $discount ) ) { |
|
185 | - return false; |
|
186 | - } |
|
187 | - |
|
188 | - $discount = $discount[0]; |
|
189 | - |
|
190 | - // Prepare the return data. |
|
191 | - $return = array( |
|
188 | + $discount = $discount[0]; |
|
189 | + |
|
190 | + // Prepare the return data. |
|
191 | + $return = array( |
|
192 | 192 | 'ID' => $discount->ID, |
193 | 193 | 'code' => get_post_meta( $discount->ID, '_wpi_discount_code', true ), |
194 | 194 | 'amount' => get_post_meta( $discount->ID, '_wpi_discount_amount', true ), |
195 | 195 | 'date_created' => $discount->post_date, |
196 | - 'date_modified' => $discount->post_modified, |
|
197 | - 'status' => $discount->post_status, |
|
198 | - 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
196 | + 'date_modified' => $discount->post_modified, |
|
197 | + 'status' => $discount->post_status, |
|
198 | + 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
199 | 199 | 'expiration' => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ), |
200 | 200 | 'type' => get_post_meta( $discount->ID, '_wpi_discount_type', true ), |
201 | 201 | 'description' => $discount->post_excerpt, |
@@ -209,77 +209,77 @@ discard block |
||
209 | 209 | 'max_total' => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ), |
210 | 210 | ); |
211 | 211 | |
212 | - $return = apply_filters( 'wpinv_discount_properties', $return ); |
|
213 | - |
|
214 | - // Update the cache with our data |
|
215 | - wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
216 | - wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
217 | - |
|
218 | - return $return; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Given a discount code, it returns a discount id. |
|
223 | - * |
|
224 | - * |
|
225 | - * @static |
|
226 | - * @param string $discount_code |
|
227 | - * @since 1.0.15 |
|
228 | - * @return int |
|
229 | - */ |
|
230 | - public static function get_discount_id_by_code( $discount_code ) { |
|
231 | - |
|
232 | - // Trim the code. |
|
233 | - $discount_code = trim( $discount_code ); |
|
234 | - |
|
235 | - // Ensure a value has been passed. |
|
236 | - if ( empty( $discount_code ) ) { |
|
237 | - return 0; |
|
238 | - } |
|
239 | - |
|
240 | - // Maybe retrieve from the cache. |
|
241 | - $discount_id = wp_cache_get( $discount_code, 'getpaid_discount_codes' ); |
|
242 | - if ( ! empty( $discount_id ) ) { |
|
243 | - return $discount_id; |
|
244 | - } |
|
245 | - |
|
246 | - // Fetch the first discount codes. |
|
247 | - $discounts = get_posts( |
|
248 | - array( |
|
249 | - 'meta_key' => '_wpi_discount_code', |
|
250 | - 'meta_value' => $discount_code, |
|
251 | - 'post_type' => 'wpi_discount', |
|
252 | - 'posts_per_page' => 1, |
|
253 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ), |
|
254 | - 'fields' => 'ids', |
|
255 | - ) |
|
256 | - ); |
|
257 | - |
|
258 | - if ( empty( $discounts ) ) { |
|
259 | - return 0; |
|
260 | - } |
|
261 | - |
|
262 | - $discount_id = $discounts[0]; |
|
263 | - |
|
264 | - // Update the cache with our data |
|
265 | - wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' ); |
|
266 | - |
|
267 | - return $discount_id; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Magic method for checking the existence of a certain custom field. |
|
272 | - * |
|
273 | - * @since 1.0.15 |
|
274 | - * @access public |
|
275 | - * |
|
276 | - * @return bool Whether the given discount field is set. |
|
277 | - */ |
|
278 | - public function __isset( $key ){ |
|
279 | - return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
280 | - } |
|
281 | - |
|
282 | - /* |
|
212 | + $return = apply_filters( 'wpinv_discount_properties', $return ); |
|
213 | + |
|
214 | + // Update the cache with our data |
|
215 | + wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
216 | + wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
217 | + |
|
218 | + return $return; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Given a discount code, it returns a discount id. |
|
223 | + * |
|
224 | + * |
|
225 | + * @static |
|
226 | + * @param string $discount_code |
|
227 | + * @since 1.0.15 |
|
228 | + * @return int |
|
229 | + */ |
|
230 | + public static function get_discount_id_by_code( $discount_code ) { |
|
231 | + |
|
232 | + // Trim the code. |
|
233 | + $discount_code = trim( $discount_code ); |
|
234 | + |
|
235 | + // Ensure a value has been passed. |
|
236 | + if ( empty( $discount_code ) ) { |
|
237 | + return 0; |
|
238 | + } |
|
239 | + |
|
240 | + // Maybe retrieve from the cache. |
|
241 | + $discount_id = wp_cache_get( $discount_code, 'getpaid_discount_codes' ); |
|
242 | + if ( ! empty( $discount_id ) ) { |
|
243 | + return $discount_id; |
|
244 | + } |
|
245 | + |
|
246 | + // Fetch the first discount codes. |
|
247 | + $discounts = get_posts( |
|
248 | + array( |
|
249 | + 'meta_key' => '_wpi_discount_code', |
|
250 | + 'meta_value' => $discount_code, |
|
251 | + 'post_type' => 'wpi_discount', |
|
252 | + 'posts_per_page' => 1, |
|
253 | + 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ), |
|
254 | + 'fields' => 'ids', |
|
255 | + ) |
|
256 | + ); |
|
257 | + |
|
258 | + if ( empty( $discounts ) ) { |
|
259 | + return 0; |
|
260 | + } |
|
261 | + |
|
262 | + $discount_id = $discounts[0]; |
|
263 | + |
|
264 | + // Update the cache with our data |
|
265 | + wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' ); |
|
266 | + |
|
267 | + return $discount_id; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Magic method for checking the existence of a certain custom field. |
|
272 | + * |
|
273 | + * @since 1.0.15 |
|
274 | + * @access public |
|
275 | + * |
|
276 | + * @return bool Whether the given discount field is set. |
|
277 | + */ |
|
278 | + public function __isset( $key ){ |
|
279 | + return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
280 | + } |
|
281 | + |
|
282 | + /* |
|
283 | 283 | |-------------------------------------------------------------------------- |
284 | 284 | | CRUD methods |
285 | 285 | |-------------------------------------------------------------------------- |
@@ -294,430 +294,430 @@ discard block |
||
294 | 294 | |-------------------------------------------------------------------------- |
295 | 295 | */ |
296 | 296 | |
297 | - /** |
|
298 | - * Get discount status. |
|
299 | - * |
|
300 | - * @since 1.0.19 |
|
301 | - * @param string $context View or edit context. |
|
302 | - * @return string |
|
303 | - */ |
|
304 | - public function get_status( $context = 'view' ) { |
|
305 | - return $this->get_prop( 'status', $context ); |
|
297 | + /** |
|
298 | + * Get discount status. |
|
299 | + * |
|
300 | + * @since 1.0.19 |
|
301 | + * @param string $context View or edit context. |
|
302 | + * @return string |
|
303 | + */ |
|
304 | + public function get_status( $context = 'view' ) { |
|
305 | + return $this->get_prop( 'status', $context ); |
|
306 | 306 | } |
307 | 307 | |
308 | 308 | /** |
309 | - * Get plugin version when the discount was created. |
|
310 | - * |
|
311 | - * @since 1.0.19 |
|
312 | - * @param string $context View or edit context. |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - public function get_version( $context = 'view' ) { |
|
316 | - return $this->get_prop( 'version', $context ); |
|
309 | + * Get plugin version when the discount was created. |
|
310 | + * |
|
311 | + * @since 1.0.19 |
|
312 | + * @param string $context View or edit context. |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + public function get_version( $context = 'view' ) { |
|
316 | + return $this->get_prop( 'version', $context ); |
|
317 | 317 | } |
318 | 318 | |
319 | 319 | /** |
320 | - * Get date when the discount was created. |
|
321 | - * |
|
322 | - * @since 1.0.19 |
|
323 | - * @param string $context View or edit context. |
|
324 | - * @return string |
|
325 | - */ |
|
326 | - public function get_date_created( $context = 'view' ) { |
|
327 | - return $this->get_prop( 'date_created', $context ); |
|
320 | + * Get date when the discount was created. |
|
321 | + * |
|
322 | + * @since 1.0.19 |
|
323 | + * @param string $context View or edit context. |
|
324 | + * @return string |
|
325 | + */ |
|
326 | + public function get_date_created( $context = 'view' ) { |
|
327 | + return $this->get_prop( 'date_created', $context ); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | /** |
331 | - * Get GMT date when the discount was created. |
|
332 | - * |
|
333 | - * @since 1.0.19 |
|
334 | - * @param string $context View or edit context. |
|
335 | - * @return string |
|
336 | - */ |
|
337 | - public function get_date_created_gmt( $context = 'view' ) { |
|
331 | + * Get GMT date when the discount was created. |
|
332 | + * |
|
333 | + * @since 1.0.19 |
|
334 | + * @param string $context View or edit context. |
|
335 | + * @return string |
|
336 | + */ |
|
337 | + public function get_date_created_gmt( $context = 'view' ) { |
|
338 | 338 | $date = $this->get_date_created( $context ); |
339 | 339 | |
340 | 340 | if ( $date ) { |
341 | 341 | $date = get_gmt_from_date( $date ); |
342 | 342 | } |
343 | - return $date; |
|
343 | + return $date; |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | /** |
347 | - * Get date when the discount was last modified. |
|
348 | - * |
|
349 | - * @since 1.0.19 |
|
350 | - * @param string $context View or edit context. |
|
351 | - * @return string |
|
352 | - */ |
|
353 | - public function get_date_modified( $context = 'view' ) { |
|
354 | - return $this->get_prop( 'date_modified', $context ); |
|
347 | + * Get date when the discount was last modified. |
|
348 | + * |
|
349 | + * @since 1.0.19 |
|
350 | + * @param string $context View or edit context. |
|
351 | + * @return string |
|
352 | + */ |
|
353 | + public function get_date_modified( $context = 'view' ) { |
|
354 | + return $this->get_prop( 'date_modified', $context ); |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
358 | - * Get GMT date when the discount was last modified. |
|
359 | - * |
|
360 | - * @since 1.0.19 |
|
361 | - * @param string $context View or edit context. |
|
362 | - * @return string |
|
363 | - */ |
|
364 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
358 | + * Get GMT date when the discount was last modified. |
|
359 | + * |
|
360 | + * @since 1.0.19 |
|
361 | + * @param string $context View or edit context. |
|
362 | + * @return string |
|
363 | + */ |
|
364 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
365 | 365 | $date = $this->get_date_modified( $context ); |
366 | 366 | |
367 | 367 | if ( $date ) { |
368 | 368 | $date = get_gmt_from_date( $date ); |
369 | 369 | } |
370 | - return $date; |
|
370 | + return $date; |
|
371 | 371 | } |
372 | 372 | |
373 | 373 | /** |
374 | - * Get the discount name. |
|
375 | - * |
|
376 | - * @since 1.0.19 |
|
377 | - * @param string $context View or edit context. |
|
378 | - * @return string |
|
379 | - */ |
|
380 | - public function get_name( $context = 'view' ) { |
|
381 | - return $this->get_prop( 'name', $context ); |
|
374 | + * Get the discount name. |
|
375 | + * |
|
376 | + * @since 1.0.19 |
|
377 | + * @param string $context View or edit context. |
|
378 | + * @return string |
|
379 | + */ |
|
380 | + public function get_name( $context = 'view' ) { |
|
381 | + return $this->get_prop( 'name', $context ); |
|
382 | 382 | } |
383 | 383 | |
384 | 384 | /** |
385 | - * Alias of self::get_name(). |
|
386 | - * |
|
387 | - * @since 1.0.19 |
|
388 | - * @param string $context View or edit context. |
|
389 | - * @return string |
|
390 | - */ |
|
391 | - public function get_title( $context = 'view' ) { |
|
392 | - return $this->get_name( $context ); |
|
385 | + * Alias of self::get_name(). |
|
386 | + * |
|
387 | + * @since 1.0.19 |
|
388 | + * @param string $context View or edit context. |
|
389 | + * @return string |
|
390 | + */ |
|
391 | + public function get_title( $context = 'view' ) { |
|
392 | + return $this->get_name( $context ); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | /** |
396 | - * Get the discount description. |
|
397 | - * |
|
398 | - * @since 1.0.19 |
|
399 | - * @param string $context View or edit context. |
|
400 | - * @return string |
|
401 | - */ |
|
402 | - public function get_description( $context = 'view' ) { |
|
403 | - return $this->get_prop( 'description', $context ); |
|
396 | + * Get the discount description. |
|
397 | + * |
|
398 | + * @since 1.0.19 |
|
399 | + * @param string $context View or edit context. |
|
400 | + * @return string |
|
401 | + */ |
|
402 | + public function get_description( $context = 'view' ) { |
|
403 | + return $this->get_prop( 'description', $context ); |
|
404 | 404 | } |
405 | 405 | |
406 | 406 | /** |
407 | - * Alias of self::get_description(). |
|
408 | - * |
|
409 | - * @since 1.0.19 |
|
410 | - * @param string $context View or edit context. |
|
411 | - * @return string |
|
412 | - */ |
|
413 | - public function get_excerpt( $context = 'view' ) { |
|
414 | - return $this->get_description( $context ); |
|
407 | + * Alias of self::get_description(). |
|
408 | + * |
|
409 | + * @since 1.0.19 |
|
410 | + * @param string $context View or edit context. |
|
411 | + * @return string |
|
412 | + */ |
|
413 | + public function get_excerpt( $context = 'view' ) { |
|
414 | + return $this->get_description( $context ); |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | /** |
418 | - * Alias of self::get_description(). |
|
419 | - * |
|
420 | - * @since 1.0.19 |
|
421 | - * @param string $context View or edit context. |
|
422 | - * @return string |
|
423 | - */ |
|
424 | - public function get_summary( $context = 'view' ) { |
|
425 | - return $this->get_description( $context ); |
|
418 | + * Alias of self::get_description(). |
|
419 | + * |
|
420 | + * @since 1.0.19 |
|
421 | + * @param string $context View or edit context. |
|
422 | + * @return string |
|
423 | + */ |
|
424 | + public function get_summary( $context = 'view' ) { |
|
425 | + return $this->get_description( $context ); |
|
426 | 426 | } |
427 | 427 | |
428 | 428 | /** |
429 | - * Get the owner of the discount. |
|
430 | - * |
|
431 | - * @since 1.0.19 |
|
432 | - * @param string $context View or edit context. |
|
433 | - * @return string |
|
434 | - */ |
|
435 | - public function get_author( $context = 'view' ) { |
|
436 | - return (int) $this->get_prop( 'author', $context ); |
|
437 | - } |
|
429 | + * Get the owner of the discount. |
|
430 | + * |
|
431 | + * @since 1.0.19 |
|
432 | + * @param string $context View or edit context. |
|
433 | + * @return string |
|
434 | + */ |
|
435 | + public function get_author( $context = 'view' ) { |
|
436 | + return (int) $this->get_prop( 'author', $context ); |
|
437 | + } |
|
438 | 438 | |
439 | - /** |
|
440 | - * Get the discount code. |
|
441 | - * |
|
442 | - * @since 1.0.19 |
|
443 | - * @param string $context View or edit context. |
|
444 | - * @return string |
|
445 | - */ |
|
446 | - public function get_code( $context = 'view' ) { |
|
447 | - return $this->get_prop( 'code', $context ); |
|
448 | - } |
|
439 | + /** |
|
440 | + * Get the discount code. |
|
441 | + * |
|
442 | + * @since 1.0.19 |
|
443 | + * @param string $context View or edit context. |
|
444 | + * @return string |
|
445 | + */ |
|
446 | + public function get_code( $context = 'view' ) { |
|
447 | + return $this->get_prop( 'code', $context ); |
|
448 | + } |
|
449 | 449 | |
450 | - /** |
|
451 | - * Alias for self::get_code(). |
|
452 | - * |
|
453 | - * @since 1.0.19 |
|
454 | - * @param string $context View or edit context. |
|
455 | - * @return string |
|
456 | - */ |
|
457 | - public function get_coupon_code( $context = 'view' ) { |
|
458 | - return $this->get_code( $context ); |
|
459 | - } |
|
450 | + /** |
|
451 | + * Alias for self::get_code(). |
|
452 | + * |
|
453 | + * @since 1.0.19 |
|
454 | + * @param string $context View or edit context. |
|
455 | + * @return string |
|
456 | + */ |
|
457 | + public function get_coupon_code( $context = 'view' ) { |
|
458 | + return $this->get_code( $context ); |
|
459 | + } |
|
460 | 460 | |
461 | - /** |
|
462 | - * Alias for self::get_code(). |
|
463 | - * |
|
464 | - * @since 1.0.19 |
|
465 | - * @param string $context View or edit context. |
|
466 | - * @return string |
|
467 | - */ |
|
468 | - public function get_discount_code( $context = 'view' ) { |
|
469 | - return $this->get_code( $context ); |
|
470 | - } |
|
461 | + /** |
|
462 | + * Alias for self::get_code(). |
|
463 | + * |
|
464 | + * @since 1.0.19 |
|
465 | + * @param string $context View or edit context. |
|
466 | + * @return string |
|
467 | + */ |
|
468 | + public function get_discount_code( $context = 'view' ) { |
|
469 | + return $this->get_code( $context ); |
|
470 | + } |
|
471 | 471 | |
472 | - /** |
|
473 | - * Get the discount's amount. |
|
474 | - * |
|
475 | - * @since 1.0.19 |
|
476 | - * @param string $context View or edit context. |
|
477 | - * @return float |
|
478 | - */ |
|
479 | - public function get_amount( $context = 'view' ) { |
|
480 | - return $this->get_prop( 'amount', $context ); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Get the discount's formated amount/rate. |
|
485 | - * |
|
486 | - * @since 1.0.19 |
|
487 | - * @return string |
|
488 | - */ |
|
489 | - public function get_formatted_amount() { |
|
490 | - |
|
491 | - if ( $this->is_type( 'flat' ) ) { |
|
492 | - $rate = wpinv_price( $this->get_amount() ); |
|
493 | - } else { |
|
494 | - $rate = $this->get_amount() . '%'; |
|
495 | - } |
|
496 | - |
|
497 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() ); |
|
498 | - } |
|
472 | + /** |
|
473 | + * Get the discount's amount. |
|
474 | + * |
|
475 | + * @since 1.0.19 |
|
476 | + * @param string $context View or edit context. |
|
477 | + * @return float |
|
478 | + */ |
|
479 | + public function get_amount( $context = 'view' ) { |
|
480 | + return $this->get_prop( 'amount', $context ); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Get the discount's formated amount/rate. |
|
485 | + * |
|
486 | + * @since 1.0.19 |
|
487 | + * @return string |
|
488 | + */ |
|
489 | + public function get_formatted_amount() { |
|
490 | + |
|
491 | + if ( $this->is_type( 'flat' ) ) { |
|
492 | + $rate = wpinv_price( $this->get_amount() ); |
|
493 | + } else { |
|
494 | + $rate = $this->get_amount() . '%'; |
|
495 | + } |
|
496 | + |
|
497 | + return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() ); |
|
498 | + } |
|
499 | 499 | |
500 | - /** |
|
501 | - * Get the discount's start date. |
|
502 | - * |
|
503 | - * @since 1.0.19 |
|
504 | - * @param string $context View or edit context. |
|
505 | - * @return string |
|
506 | - */ |
|
507 | - public function get_start( $context = 'view' ) { |
|
508 | - return $this->get_prop( 'start', $context ); |
|
509 | - } |
|
500 | + /** |
|
501 | + * Get the discount's start date. |
|
502 | + * |
|
503 | + * @since 1.0.19 |
|
504 | + * @param string $context View or edit context. |
|
505 | + * @return string |
|
506 | + */ |
|
507 | + public function get_start( $context = 'view' ) { |
|
508 | + return $this->get_prop( 'start', $context ); |
|
509 | + } |
|
510 | 510 | |
511 | - /** |
|
512 | - * Alias for self::get_start(). |
|
513 | - * |
|
514 | - * @since 1.0.19 |
|
515 | - * @param string $context View or edit context. |
|
516 | - * @return string |
|
517 | - */ |
|
518 | - public function get_start_date( $context = 'view' ) { |
|
519 | - return $this->get_start( $context ); |
|
520 | - } |
|
511 | + /** |
|
512 | + * Alias for self::get_start(). |
|
513 | + * |
|
514 | + * @since 1.0.19 |
|
515 | + * @param string $context View or edit context. |
|
516 | + * @return string |
|
517 | + */ |
|
518 | + public function get_start_date( $context = 'view' ) { |
|
519 | + return $this->get_start( $context ); |
|
520 | + } |
|
521 | 521 | |
522 | - /** |
|
523 | - * Get the discount's expiration date. |
|
524 | - * |
|
525 | - * @since 1.0.19 |
|
526 | - * @param string $context View or edit context. |
|
527 | - * @return string |
|
528 | - */ |
|
529 | - public function get_expiration( $context = 'view' ) { |
|
530 | - return $this->get_prop( 'expiration', $context ); |
|
531 | - } |
|
522 | + /** |
|
523 | + * Get the discount's expiration date. |
|
524 | + * |
|
525 | + * @since 1.0.19 |
|
526 | + * @param string $context View or edit context. |
|
527 | + * @return string |
|
528 | + */ |
|
529 | + public function get_expiration( $context = 'view' ) { |
|
530 | + return $this->get_prop( 'expiration', $context ); |
|
531 | + } |
|
532 | 532 | |
533 | - /** |
|
534 | - * Alias for self::get_expiration(). |
|
535 | - * |
|
536 | - * @since 1.0.19 |
|
537 | - * @param string $context View or edit context. |
|
538 | - * @return string |
|
539 | - */ |
|
540 | - public function get_expiration_date( $context = 'view' ) { |
|
541 | - return $this->get_expiration( $context ); |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Alias for self::get_expiration(). |
|
546 | - * |
|
547 | - * @since 1.0.19 |
|
548 | - * @param string $context View or edit context. |
|
549 | - * @return string |
|
550 | - */ |
|
551 | - public function get_end_date( $context = 'view' ) { |
|
552 | - return $this->get_expiration( $context ); |
|
553 | - } |
|
533 | + /** |
|
534 | + * Alias for self::get_expiration(). |
|
535 | + * |
|
536 | + * @since 1.0.19 |
|
537 | + * @param string $context View or edit context. |
|
538 | + * @return string |
|
539 | + */ |
|
540 | + public function get_expiration_date( $context = 'view' ) { |
|
541 | + return $this->get_expiration( $context ); |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Alias for self::get_expiration(). |
|
546 | + * |
|
547 | + * @since 1.0.19 |
|
548 | + * @param string $context View or edit context. |
|
549 | + * @return string |
|
550 | + */ |
|
551 | + public function get_end_date( $context = 'view' ) { |
|
552 | + return $this->get_expiration( $context ); |
|
553 | + } |
|
554 | 554 | |
555 | - /** |
|
556 | - * Get the discount's type. |
|
557 | - * |
|
558 | - * @since 1.0.19 |
|
559 | - * @param string $context View or edit context. |
|
560 | - * @return string |
|
561 | - */ |
|
562 | - public function get_type( $context = 'view' ) { |
|
563 | - return $this->get_prop( 'type', $context ); |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Get the number of times a discount has been used. |
|
568 | - * |
|
569 | - * @since 1.0.19 |
|
570 | - * @param string $context View or edit context. |
|
571 | - * @return int |
|
572 | - */ |
|
573 | - public function get_uses( $context = 'view' ) { |
|
574 | - return (int) $this->get_prop( 'uses', $context ); |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Get the discount's usage, i.e uses / max uses. |
|
579 | - * |
|
580 | - * @since 1.0.19 |
|
581 | - * @return string |
|
582 | - */ |
|
583 | - public function get_usage() { |
|
584 | - |
|
585 | - if ( ! $this->has_limit() ) { |
|
586 | - return $this->get_uses() . ' / ' . ' ∞'; |
|
587 | - } |
|
588 | - |
|
589 | - return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
590 | - |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Get the maximum number of time a discount can be used. |
|
595 | - * |
|
596 | - * @since 1.0.19 |
|
597 | - * @param string $context View or edit context. |
|
598 | - * @return int |
|
599 | - */ |
|
600 | - public function get_max_uses( $context = 'view' ) { |
|
601 | - $max_uses = $this->get_prop( 'max_uses', $context ); |
|
602 | - return empty( $max_uses ) ? null : $max_uses; |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Checks if this is a single use discount or not. |
|
607 | - * |
|
608 | - * @since 1.0.19 |
|
609 | - * @param string $context View or edit context. |
|
610 | - * @return bool |
|
611 | - */ |
|
612 | - public function get_is_single_use( $context = 'view' ) { |
|
613 | - return $this->get_prop( 'is_single_use', $context ); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Get the items that can be used with this discount. |
|
618 | - * |
|
619 | - * @since 1.0.19 |
|
620 | - * @param string $context View or edit context. |
|
621 | - * @return array |
|
622 | - */ |
|
623 | - public function get_items( $context = 'view' ) { |
|
624 | - return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * Alias for self::get_items(). |
|
629 | - * |
|
630 | - * @since 1.0.19 |
|
631 | - * @param string $context View or edit context. |
|
632 | - * @return array |
|
633 | - */ |
|
634 | - public function get_allowed_items( $context = 'view' ) { |
|
635 | - return $this->get_items( $context ); |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * Get the items that are not allowed to use this discount. |
|
640 | - * |
|
641 | - * @since 1.0.19 |
|
642 | - * @param string $context View or edit context. |
|
643 | - * @return array |
|
644 | - */ |
|
645 | - public function get_excluded_items( $context = 'view' ) { |
|
646 | - return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * Checks if this is a recurring discount or not. |
|
651 | - * |
|
652 | - * @since 1.0.19 |
|
653 | - * @param string $context View or edit context. |
|
654 | - * @return int|string|bool |
|
655 | - */ |
|
656 | - public function get_is_recurring( $context = 'view' ) { |
|
657 | - return $this->get_prop( 'is_recurring', $context ); |
|
658 | - } |
|
659 | - |
|
660 | - /** |
|
661 | - * Get's the minimum total amount allowed for this discount. |
|
662 | - * |
|
663 | - * @since 1.0.19 |
|
664 | - * @param string $context View or edit context. |
|
665 | - * @return float |
|
666 | - */ |
|
667 | - public function get_min_total( $context = 'view' ) { |
|
668 | - $minimum = $this->get_prop( 'min_total', $context ); |
|
669 | - return empty( $minimum ) ? null : $minimum; |
|
670 | - } |
|
671 | - |
|
672 | - /** |
|
673 | - * Alias for self::get_min_total(). |
|
674 | - * |
|
675 | - * @since 1.0.19 |
|
676 | - * @param string $context View or edit context. |
|
677 | - * @return float |
|
678 | - */ |
|
679 | - public function get_minimum_total( $context = 'view' ) { |
|
680 | - return $this->get_min_total( $context ); |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * Get's the maximum total amount allowed for this discount. |
|
685 | - * |
|
686 | - * @since 1.0.19 |
|
687 | - * @param string $context View or edit context. |
|
688 | - * @return float |
|
689 | - */ |
|
690 | - public function get_max_total( $context = 'view' ) { |
|
691 | - $maximum = $this->get_prop( 'max_total', $context ); |
|
692 | - return empty( $maximum ) ? null : $maximum; |
|
693 | - } |
|
694 | - |
|
695 | - /** |
|
696 | - * Alias for self::get_max_total(). |
|
697 | - * |
|
698 | - * @since 1.0.19 |
|
699 | - * @param string $context View or edit context. |
|
700 | - * @return float |
|
701 | - */ |
|
702 | - public function get_maximum_total( $context = 'view' ) { |
|
703 | - return $this->get_max_total( $context ); |
|
704 | - } |
|
705 | - |
|
706 | - /** |
|
707 | - * Magic method for accessing discount properties. |
|
708 | - * |
|
709 | - * @since 1.0.15 |
|
710 | - * @access public |
|
711 | - * |
|
712 | - * @param string $key Discount data to retrieve |
|
713 | - * @param string $context View or edit context. |
|
714 | - * @return mixed Value of the given discount property (if set). |
|
715 | - */ |
|
716 | - public function get( $key, $context = 'view' ) { |
|
555 | + /** |
|
556 | + * Get the discount's type. |
|
557 | + * |
|
558 | + * @since 1.0.19 |
|
559 | + * @param string $context View or edit context. |
|
560 | + * @return string |
|
561 | + */ |
|
562 | + public function get_type( $context = 'view' ) { |
|
563 | + return $this->get_prop( 'type', $context ); |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Get the number of times a discount has been used. |
|
568 | + * |
|
569 | + * @since 1.0.19 |
|
570 | + * @param string $context View or edit context. |
|
571 | + * @return int |
|
572 | + */ |
|
573 | + public function get_uses( $context = 'view' ) { |
|
574 | + return (int) $this->get_prop( 'uses', $context ); |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Get the discount's usage, i.e uses / max uses. |
|
579 | + * |
|
580 | + * @since 1.0.19 |
|
581 | + * @return string |
|
582 | + */ |
|
583 | + public function get_usage() { |
|
584 | + |
|
585 | + if ( ! $this->has_limit() ) { |
|
586 | + return $this->get_uses() . ' / ' . ' ∞'; |
|
587 | + } |
|
588 | + |
|
589 | + return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
590 | + |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Get the maximum number of time a discount can be used. |
|
595 | + * |
|
596 | + * @since 1.0.19 |
|
597 | + * @param string $context View or edit context. |
|
598 | + * @return int |
|
599 | + */ |
|
600 | + public function get_max_uses( $context = 'view' ) { |
|
601 | + $max_uses = $this->get_prop( 'max_uses', $context ); |
|
602 | + return empty( $max_uses ) ? null : $max_uses; |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Checks if this is a single use discount or not. |
|
607 | + * |
|
608 | + * @since 1.0.19 |
|
609 | + * @param string $context View or edit context. |
|
610 | + * @return bool |
|
611 | + */ |
|
612 | + public function get_is_single_use( $context = 'view' ) { |
|
613 | + return $this->get_prop( 'is_single_use', $context ); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Get the items that can be used with this discount. |
|
618 | + * |
|
619 | + * @since 1.0.19 |
|
620 | + * @param string $context View or edit context. |
|
621 | + * @return array |
|
622 | + */ |
|
623 | + public function get_items( $context = 'view' ) { |
|
624 | + return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * Alias for self::get_items(). |
|
629 | + * |
|
630 | + * @since 1.0.19 |
|
631 | + * @param string $context View or edit context. |
|
632 | + * @return array |
|
633 | + */ |
|
634 | + public function get_allowed_items( $context = 'view' ) { |
|
635 | + return $this->get_items( $context ); |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * Get the items that are not allowed to use this discount. |
|
640 | + * |
|
641 | + * @since 1.0.19 |
|
642 | + * @param string $context View or edit context. |
|
643 | + * @return array |
|
644 | + */ |
|
645 | + public function get_excluded_items( $context = 'view' ) { |
|
646 | + return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * Checks if this is a recurring discount or not. |
|
651 | + * |
|
652 | + * @since 1.0.19 |
|
653 | + * @param string $context View or edit context. |
|
654 | + * @return int|string|bool |
|
655 | + */ |
|
656 | + public function get_is_recurring( $context = 'view' ) { |
|
657 | + return $this->get_prop( 'is_recurring', $context ); |
|
658 | + } |
|
659 | + |
|
660 | + /** |
|
661 | + * Get's the minimum total amount allowed for this discount. |
|
662 | + * |
|
663 | + * @since 1.0.19 |
|
664 | + * @param string $context View or edit context. |
|
665 | + * @return float |
|
666 | + */ |
|
667 | + public function get_min_total( $context = 'view' ) { |
|
668 | + $minimum = $this->get_prop( 'min_total', $context ); |
|
669 | + return empty( $minimum ) ? null : $minimum; |
|
670 | + } |
|
671 | + |
|
672 | + /** |
|
673 | + * Alias for self::get_min_total(). |
|
674 | + * |
|
675 | + * @since 1.0.19 |
|
676 | + * @param string $context View or edit context. |
|
677 | + * @return float |
|
678 | + */ |
|
679 | + public function get_minimum_total( $context = 'view' ) { |
|
680 | + return $this->get_min_total( $context ); |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * Get's the maximum total amount allowed for this discount. |
|
685 | + * |
|
686 | + * @since 1.0.19 |
|
687 | + * @param string $context View or edit context. |
|
688 | + * @return float |
|
689 | + */ |
|
690 | + public function get_max_total( $context = 'view' ) { |
|
691 | + $maximum = $this->get_prop( 'max_total', $context ); |
|
692 | + return empty( $maximum ) ? null : $maximum; |
|
693 | + } |
|
694 | + |
|
695 | + /** |
|
696 | + * Alias for self::get_max_total(). |
|
697 | + * |
|
698 | + * @since 1.0.19 |
|
699 | + * @param string $context View or edit context. |
|
700 | + * @return float |
|
701 | + */ |
|
702 | + public function get_maximum_total( $context = 'view' ) { |
|
703 | + return $this->get_max_total( $context ); |
|
704 | + } |
|
705 | + |
|
706 | + /** |
|
707 | + * Magic method for accessing discount properties. |
|
708 | + * |
|
709 | + * @since 1.0.15 |
|
710 | + * @access public |
|
711 | + * |
|
712 | + * @param string $key Discount data to retrieve |
|
713 | + * @param string $context View or edit context. |
|
714 | + * @return mixed Value of the given discount property (if set). |
|
715 | + */ |
|
716 | + public function get( $key, $context = 'view' ) { |
|
717 | 717 | return $this->get_prop( $key, $context ); |
718 | - } |
|
718 | + } |
|
719 | 719 | |
720 | - /* |
|
720 | + /* |
|
721 | 721 | |-------------------------------------------------------------------------- |
722 | 722 | | Setters |
723 | 723 | |-------------------------------------------------------------------------- |
@@ -727,41 +727,41 @@ discard block |
||
727 | 727 | | object. |
728 | 728 | */ |
729 | 729 | |
730 | - /** |
|
731 | - * Sets discount status. |
|
732 | - * |
|
733 | - * @since 1.0.19 |
|
734 | - * @param string $status New status. |
|
735 | - * @return array details of change. |
|
736 | - */ |
|
737 | - public function set_status( $status ) { |
|
730 | + /** |
|
731 | + * Sets discount status. |
|
732 | + * |
|
733 | + * @since 1.0.19 |
|
734 | + * @param string $status New status. |
|
735 | + * @return array details of change. |
|
736 | + */ |
|
737 | + public function set_status( $status ) { |
|
738 | 738 | $old_status = $this->get_status(); |
739 | 739 | |
740 | 740 | $this->set_prop( 'status', $status ); |
741 | 741 | |
742 | - return array( |
|
743 | - 'from' => $old_status, |
|
744 | - 'to' => $status, |
|
745 | - ); |
|
742 | + return array( |
|
743 | + 'from' => $old_status, |
|
744 | + 'to' => $status, |
|
745 | + ); |
|
746 | 746 | } |
747 | 747 | |
748 | 748 | /** |
749 | - * Set plugin version when the discount was created. |
|
750 | - * |
|
751 | - * @since 1.0.19 |
|
752 | - */ |
|
753 | - public function set_version( $value ) { |
|
754 | - $this->set_prop( 'version', $value ); |
|
749 | + * Set plugin version when the discount was created. |
|
750 | + * |
|
751 | + * @since 1.0.19 |
|
752 | + */ |
|
753 | + public function set_version( $value ) { |
|
754 | + $this->set_prop( 'version', $value ); |
|
755 | 755 | } |
756 | 756 | |
757 | 757 | /** |
758 | - * Set date when the discount was created. |
|
759 | - * |
|
760 | - * @since 1.0.19 |
|
761 | - * @param string $value Value to set. |
|
758 | + * Set date when the discount was created. |
|
759 | + * |
|
760 | + * @since 1.0.19 |
|
761 | + * @param string $value Value to set. |
|
762 | 762 | * @return bool Whether or not the date was set. |
763 | - */ |
|
764 | - public function set_date_created( $value ) { |
|
763 | + */ |
|
764 | + public function set_date_created( $value ) { |
|
765 | 765 | $date = strtotime( $value ); |
766 | 766 | |
767 | 767 | if ( $date ) { |
@@ -774,13 +774,13 @@ discard block |
||
774 | 774 | } |
775 | 775 | |
776 | 776 | /** |
777 | - * Set date when the discount was last modified. |
|
778 | - * |
|
779 | - * @since 1.0.19 |
|
780 | - * @param string $value Value to set. |
|
777 | + * Set date when the discount was last modified. |
|
778 | + * |
|
779 | + * @since 1.0.19 |
|
780 | + * @param string $value Value to set. |
|
781 | 781 | * @return bool Whether or not the date was set. |
782 | - */ |
|
783 | - public function set_date_modified( $value ) { |
|
782 | + */ |
|
783 | + public function set_date_modified( $value ) { |
|
784 | 784 | $date = strtotime( $value ); |
785 | 785 | |
786 | 786 | if ( $date ) { |
@@ -793,324 +793,324 @@ discard block |
||
793 | 793 | } |
794 | 794 | |
795 | 795 | /** |
796 | - * Set the discount name. |
|
797 | - * |
|
798 | - * @since 1.0.19 |
|
799 | - * @param string $value New name. |
|
800 | - */ |
|
801 | - public function set_name( $value ) { |
|
796 | + * Set the discount name. |
|
797 | + * |
|
798 | + * @since 1.0.19 |
|
799 | + * @param string $value New name. |
|
800 | + */ |
|
801 | + public function set_name( $value ) { |
|
802 | 802 | $name = sanitize_text_field( $value ); |
803 | - $this->set_prop( 'name', $name ); |
|
803 | + $this->set_prop( 'name', $name ); |
|
804 | 804 | } |
805 | 805 | |
806 | 806 | /** |
807 | - * Alias of self::set_name(). |
|
808 | - * |
|
809 | - * @since 1.0.19 |
|
810 | - * @param string $value New name. |
|
811 | - */ |
|
812 | - public function set_title( $value ) { |
|
813 | - $this->set_name( $value ); |
|
807 | + * Alias of self::set_name(). |
|
808 | + * |
|
809 | + * @since 1.0.19 |
|
810 | + * @param string $value New name. |
|
811 | + */ |
|
812 | + public function set_title( $value ) { |
|
813 | + $this->set_name( $value ); |
|
814 | 814 | } |
815 | 815 | |
816 | 816 | /** |
817 | - * Set the discount description. |
|
818 | - * |
|
819 | - * @since 1.0.19 |
|
820 | - * @param string $value New description. |
|
821 | - */ |
|
822 | - public function set_description( $value ) { |
|
817 | + * Set the discount description. |
|
818 | + * |
|
819 | + * @since 1.0.19 |
|
820 | + * @param string $value New description. |
|
821 | + */ |
|
822 | + public function set_description( $value ) { |
|
823 | 823 | $description = wp_kses_post( $value ); |
824 | - return $this->set_prop( 'description', $description ); |
|
824 | + return $this->set_prop( 'description', $description ); |
|
825 | 825 | } |
826 | 826 | |
827 | 827 | /** |
828 | - * Alias of self::set_description(). |
|
829 | - * |
|
830 | - * @since 1.0.19 |
|
831 | - * @param string $value New description. |
|
832 | - */ |
|
833 | - public function set_excerpt( $value ) { |
|
834 | - $this->set_description( $value ); |
|
828 | + * Alias of self::set_description(). |
|
829 | + * |
|
830 | + * @since 1.0.19 |
|
831 | + * @param string $value New description. |
|
832 | + */ |
|
833 | + public function set_excerpt( $value ) { |
|
834 | + $this->set_description( $value ); |
|
835 | 835 | } |
836 | 836 | |
837 | 837 | /** |
838 | - * Alias of self::set_description(). |
|
839 | - * |
|
840 | - * @since 1.0.19 |
|
841 | - * @param string $value New description. |
|
842 | - */ |
|
843 | - public function set_summary( $value ) { |
|
844 | - $this->set_description( $value ); |
|
838 | + * Alias of self::set_description(). |
|
839 | + * |
|
840 | + * @since 1.0.19 |
|
841 | + * @param string $value New description. |
|
842 | + */ |
|
843 | + public function set_summary( $value ) { |
|
844 | + $this->set_description( $value ); |
|
845 | 845 | } |
846 | 846 | |
847 | 847 | /** |
848 | - * Set the owner of the discount. |
|
849 | - * |
|
850 | - * @since 1.0.19 |
|
851 | - * @param int $value New author. |
|
852 | - */ |
|
853 | - public function set_author( $value ) { |
|
854 | - $this->set_prop( 'author', (int) $value ); |
|
855 | - } |
|
848 | + * Set the owner of the discount. |
|
849 | + * |
|
850 | + * @since 1.0.19 |
|
851 | + * @param int $value New author. |
|
852 | + */ |
|
853 | + public function set_author( $value ) { |
|
854 | + $this->set_prop( 'author', (int) $value ); |
|
855 | + } |
|
856 | 856 | |
857 | - /** |
|
858 | - * Sets the discount code. |
|
859 | - * |
|
860 | - * @since 1.0.19 |
|
861 | - * @param string $value New discount code. |
|
862 | - */ |
|
863 | - public function set_code( $value ) { |
|
864 | - $code = sanitize_text_field( $value ); |
|
865 | - $this->set_prop( 'code', $code ); |
|
866 | - } |
|
857 | + /** |
|
858 | + * Sets the discount code. |
|
859 | + * |
|
860 | + * @since 1.0.19 |
|
861 | + * @param string $value New discount code. |
|
862 | + */ |
|
863 | + public function set_code( $value ) { |
|
864 | + $code = sanitize_text_field( $value ); |
|
865 | + $this->set_prop( 'code', $code ); |
|
866 | + } |
|
867 | 867 | |
868 | - /** |
|
869 | - * Alias of self::set_code(). |
|
870 | - * |
|
871 | - * @since 1.0.19 |
|
872 | - * @param string $value New discount code. |
|
873 | - */ |
|
874 | - public function set_coupon_code( $value ) { |
|
875 | - $this->set_code( $value ); |
|
876 | - } |
|
868 | + /** |
|
869 | + * Alias of self::set_code(). |
|
870 | + * |
|
871 | + * @since 1.0.19 |
|
872 | + * @param string $value New discount code. |
|
873 | + */ |
|
874 | + public function set_coupon_code( $value ) { |
|
875 | + $this->set_code( $value ); |
|
876 | + } |
|
877 | 877 | |
878 | - /** |
|
879 | - * Alias of self::set_code(). |
|
880 | - * |
|
881 | - * @since 1.0.19 |
|
882 | - * @param string $value New discount code. |
|
883 | - */ |
|
884 | - public function set_discount_code( $value ) { |
|
885 | - $this->set_code( $value ); |
|
886 | - } |
|
878 | + /** |
|
879 | + * Alias of self::set_code(). |
|
880 | + * |
|
881 | + * @since 1.0.19 |
|
882 | + * @param string $value New discount code. |
|
883 | + */ |
|
884 | + public function set_discount_code( $value ) { |
|
885 | + $this->set_code( $value ); |
|
886 | + } |
|
887 | 887 | |
888 | - /** |
|
889 | - * Sets the discount amount. |
|
890 | - * |
|
891 | - * @since 1.0.19 |
|
892 | - * @param float $value New discount code. |
|
893 | - */ |
|
894 | - public function set_amount( $value ) { |
|
895 | - $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
896 | - $this->set_prop( 'amount', $amount ); |
|
897 | - } |
|
898 | - |
|
899 | - /** |
|
900 | - * Sets the discount's start date. |
|
901 | - * |
|
902 | - * @since 1.0.19 |
|
903 | - * @param float $value New start date. |
|
904 | - */ |
|
905 | - public function set_start( $value ) { |
|
906 | - $date = strtotime( $value ); |
|
888 | + /** |
|
889 | + * Sets the discount amount. |
|
890 | + * |
|
891 | + * @since 1.0.19 |
|
892 | + * @param float $value New discount code. |
|
893 | + */ |
|
894 | + public function set_amount( $value ) { |
|
895 | + $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
896 | + $this->set_prop( 'amount', $amount ); |
|
897 | + } |
|
907 | 898 | |
908 | - if ( $date ) { |
|
909 | - $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
|
910 | - return true; |
|
911 | - } |
|
899 | + /** |
|
900 | + * Sets the discount's start date. |
|
901 | + * |
|
902 | + * @since 1.0.19 |
|
903 | + * @param float $value New start date. |
|
904 | + */ |
|
905 | + public function set_start( $value ) { |
|
906 | + $date = strtotime( $value ); |
|
907 | + |
|
908 | + if ( $date ) { |
|
909 | + $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
|
910 | + return true; |
|
911 | + } |
|
912 | 912 | |
913 | - $this->set_prop( 'start', '' ); |
|
913 | + $this->set_prop( 'start', '' ); |
|
914 | 914 | |
915 | 915 | return false; |
916 | - } |
|
917 | - |
|
918 | - /** |
|
919 | - * Alias of self::set_start(). |
|
920 | - * |
|
921 | - * @since 1.0.19 |
|
922 | - * @param string $value New start date. |
|
923 | - */ |
|
924 | - public function set_start_date( $value ) { |
|
925 | - $this->set_start( $value ); |
|
926 | - } |
|
927 | - |
|
928 | - /** |
|
929 | - * Sets the discount's expiration date. |
|
930 | - * |
|
931 | - * @since 1.0.19 |
|
932 | - * @param float $value New expiration date. |
|
933 | - */ |
|
934 | - public function set_expiration( $value ) { |
|
935 | - $date = strtotime( $value ); |
|
916 | + } |
|
917 | + |
|
918 | + /** |
|
919 | + * Alias of self::set_start(). |
|
920 | + * |
|
921 | + * @since 1.0.19 |
|
922 | + * @param string $value New start date. |
|
923 | + */ |
|
924 | + public function set_start_date( $value ) { |
|
925 | + $this->set_start( $value ); |
|
926 | + } |
|
927 | + |
|
928 | + /** |
|
929 | + * Sets the discount's expiration date. |
|
930 | + * |
|
931 | + * @since 1.0.19 |
|
932 | + * @param float $value New expiration date. |
|
933 | + */ |
|
934 | + public function set_expiration( $value ) { |
|
935 | + $date = strtotime( $value ); |
|
936 | 936 | |
937 | 937 | if ( $date ) { |
938 | 938 | $this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) ); |
939 | 939 | return true; |
940 | 940 | } |
941 | 941 | |
942 | - $this->set_prop( 'expiration', '' ); |
|
942 | + $this->set_prop( 'expiration', '' ); |
|
943 | 943 | return false; |
944 | - } |
|
945 | - |
|
946 | - /** |
|
947 | - * Alias of self::set_expiration(). |
|
948 | - * |
|
949 | - * @since 1.0.19 |
|
950 | - * @param string $value New expiration date. |
|
951 | - */ |
|
952 | - public function set_expiration_date( $value ) { |
|
953 | - $this->set_expiration( $value ); |
|
954 | - } |
|
955 | - |
|
956 | - /** |
|
957 | - * Alias of self::set_expiration(). |
|
958 | - * |
|
959 | - * @since 1.0.19 |
|
960 | - * @param string $value New expiration date. |
|
961 | - */ |
|
962 | - public function set_end_date( $value ) { |
|
963 | - $this->set_expiration( $value ); |
|
964 | - } |
|
965 | - |
|
966 | - /** |
|
967 | - * Sets the discount type. |
|
968 | - * |
|
969 | - * @since 1.0.19 |
|
970 | - * @param string $value New discount type. |
|
971 | - */ |
|
972 | - public function set_type( $value ) { |
|
973 | - if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
974 | - $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
975 | - } |
|
976 | - } |
|
977 | - |
|
978 | - /** |
|
979 | - * Sets the number of times a discount has been used. |
|
980 | - * |
|
981 | - * @since 1.0.19 |
|
982 | - * @param int $value usage count. |
|
983 | - */ |
|
984 | - public function set_uses( $value ) { |
|
985 | - |
|
986 | - $value = (int) $value; |
|
987 | - |
|
988 | - if ( $value < 0 ) { |
|
989 | - $value = 0; |
|
990 | - } |
|
991 | - |
|
992 | - $this->set_prop( 'uses', (int) $value ); |
|
993 | - } |
|
994 | - |
|
995 | - /** |
|
996 | - * Sets the maximum number of times a discount can be used. |
|
997 | - * |
|
998 | - * @since 1.0.19 |
|
999 | - * @param int $value maximum usage count. |
|
1000 | - */ |
|
1001 | - public function set_max_uses( $value ) { |
|
1002 | - $this->set_prop( 'max_uses', absint( $value ) ); |
|
1003 | - } |
|
1004 | - |
|
1005 | - /** |
|
1006 | - * Sets if this is a single use discount or not. |
|
1007 | - * |
|
1008 | - * @since 1.0.19 |
|
1009 | - * @param int|bool $value is single use. |
|
1010 | - */ |
|
1011 | - public function set_is_single_use( $value ) { |
|
1012 | - $this->set_prop( 'is_single_use', (bool) $value ); |
|
1013 | - } |
|
1014 | - |
|
1015 | - /** |
|
1016 | - * Sets the items that can be used with this discount. |
|
1017 | - * |
|
1018 | - * @since 1.0.19 |
|
1019 | - * @param array $value items. |
|
1020 | - */ |
|
1021 | - public function set_items( $value ) { |
|
1022 | - $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
1023 | - } |
|
1024 | - |
|
1025 | - /** |
|
1026 | - * Alias for self::set_items(). |
|
1027 | - * |
|
1028 | - * @since 1.0.19 |
|
1029 | - * @param array $value items. |
|
1030 | - */ |
|
1031 | - public function set_allowed_items( $value ) { |
|
1032 | - $this->set_items( $value ); |
|
1033 | - } |
|
1034 | - |
|
1035 | - /** |
|
1036 | - * Sets the items that can not be used with this discount. |
|
1037 | - * |
|
1038 | - * @since 1.0.19 |
|
1039 | - * @param array $value items. |
|
1040 | - */ |
|
1041 | - public function set_excluded_items( $value ) { |
|
1042 | - $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
1043 | - } |
|
1044 | - |
|
1045 | - /** |
|
1046 | - * Sets if this is a recurring discounts or not. |
|
1047 | - * |
|
1048 | - * @since 1.0.19 |
|
1049 | - * @param int|bool $value is recurring. |
|
1050 | - */ |
|
1051 | - public function set_is_recurring( $value ) { |
|
1052 | - $this->set_prop( 'is_recurring', (bool) $value ); |
|
1053 | - } |
|
1054 | - |
|
1055 | - /** |
|
1056 | - * Sets the minimum total that can not be used with this discount. |
|
1057 | - * |
|
1058 | - * @since 1.0.19 |
|
1059 | - * @param float $value minimum total. |
|
1060 | - */ |
|
1061 | - public function set_min_total( $value ) { |
|
1062 | - $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1063 | - } |
|
1064 | - |
|
1065 | - /** |
|
1066 | - * Alias for self::set_min_total(). |
|
1067 | - * |
|
1068 | - * @since 1.0.19 |
|
1069 | - * @param float $value minimum total. |
|
1070 | - */ |
|
1071 | - public function set_minimum_total( $value ) { |
|
1072 | - $this->set_min_total( $value ); |
|
1073 | - } |
|
1074 | - |
|
1075 | - /** |
|
1076 | - * Sets the maximum total that can not be used with this discount. |
|
1077 | - * |
|
1078 | - * @since 1.0.19 |
|
1079 | - * @param float $value maximum total. |
|
1080 | - */ |
|
1081 | - public function set_max_total( $value ) { |
|
1082 | - $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1083 | - } |
|
1084 | - |
|
1085 | - /** |
|
1086 | - * Alias for self::set_max_total(). |
|
1087 | - * |
|
1088 | - * @since 1.0.19 |
|
1089 | - * @param float $value maximum total. |
|
1090 | - */ |
|
1091 | - public function set_maximum_total( $value ) { |
|
1092 | - $this->set_max_total( $value ); |
|
1093 | - } |
|
1094 | - |
|
1095 | - /** |
|
1096 | - * @deprecated |
|
1097 | - */ |
|
1098 | - public function refresh(){} |
|
1099 | - |
|
1100 | - /** |
|
1101 | - * @deprecated |
|
1102 | - * |
|
1103 | - */ |
|
1104 | - public function update_status( $status = 'publish' ){ |
|
1105 | - |
|
1106 | - if ( $this->exists() && $this->get_status() != $status ) { |
|
1107 | - $this->set_status( $status ); |
|
1108 | - $this->save(); |
|
1109 | - } |
|
1110 | - |
|
1111 | - } |
|
1112 | - |
|
1113 | - /* |
|
944 | + } |
|
945 | + |
|
946 | + /** |
|
947 | + * Alias of self::set_expiration(). |
|
948 | + * |
|
949 | + * @since 1.0.19 |
|
950 | + * @param string $value New expiration date. |
|
951 | + */ |
|
952 | + public function set_expiration_date( $value ) { |
|
953 | + $this->set_expiration( $value ); |
|
954 | + } |
|
955 | + |
|
956 | + /** |
|
957 | + * Alias of self::set_expiration(). |
|
958 | + * |
|
959 | + * @since 1.0.19 |
|
960 | + * @param string $value New expiration date. |
|
961 | + */ |
|
962 | + public function set_end_date( $value ) { |
|
963 | + $this->set_expiration( $value ); |
|
964 | + } |
|
965 | + |
|
966 | + /** |
|
967 | + * Sets the discount type. |
|
968 | + * |
|
969 | + * @since 1.0.19 |
|
970 | + * @param string $value New discount type. |
|
971 | + */ |
|
972 | + public function set_type( $value ) { |
|
973 | + if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
974 | + $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
975 | + } |
|
976 | + } |
|
977 | + |
|
978 | + /** |
|
979 | + * Sets the number of times a discount has been used. |
|
980 | + * |
|
981 | + * @since 1.0.19 |
|
982 | + * @param int $value usage count. |
|
983 | + */ |
|
984 | + public function set_uses( $value ) { |
|
985 | + |
|
986 | + $value = (int) $value; |
|
987 | + |
|
988 | + if ( $value < 0 ) { |
|
989 | + $value = 0; |
|
990 | + } |
|
991 | + |
|
992 | + $this->set_prop( 'uses', (int) $value ); |
|
993 | + } |
|
994 | + |
|
995 | + /** |
|
996 | + * Sets the maximum number of times a discount can be used. |
|
997 | + * |
|
998 | + * @since 1.0.19 |
|
999 | + * @param int $value maximum usage count. |
|
1000 | + */ |
|
1001 | + public function set_max_uses( $value ) { |
|
1002 | + $this->set_prop( 'max_uses', absint( $value ) ); |
|
1003 | + } |
|
1004 | + |
|
1005 | + /** |
|
1006 | + * Sets if this is a single use discount or not. |
|
1007 | + * |
|
1008 | + * @since 1.0.19 |
|
1009 | + * @param int|bool $value is single use. |
|
1010 | + */ |
|
1011 | + public function set_is_single_use( $value ) { |
|
1012 | + $this->set_prop( 'is_single_use', (bool) $value ); |
|
1013 | + } |
|
1014 | + |
|
1015 | + /** |
|
1016 | + * Sets the items that can be used with this discount. |
|
1017 | + * |
|
1018 | + * @since 1.0.19 |
|
1019 | + * @param array $value items. |
|
1020 | + */ |
|
1021 | + public function set_items( $value ) { |
|
1022 | + $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
1023 | + } |
|
1024 | + |
|
1025 | + /** |
|
1026 | + * Alias for self::set_items(). |
|
1027 | + * |
|
1028 | + * @since 1.0.19 |
|
1029 | + * @param array $value items. |
|
1030 | + */ |
|
1031 | + public function set_allowed_items( $value ) { |
|
1032 | + $this->set_items( $value ); |
|
1033 | + } |
|
1034 | + |
|
1035 | + /** |
|
1036 | + * Sets the items that can not be used with this discount. |
|
1037 | + * |
|
1038 | + * @since 1.0.19 |
|
1039 | + * @param array $value items. |
|
1040 | + */ |
|
1041 | + public function set_excluded_items( $value ) { |
|
1042 | + $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
1043 | + } |
|
1044 | + |
|
1045 | + /** |
|
1046 | + * Sets if this is a recurring discounts or not. |
|
1047 | + * |
|
1048 | + * @since 1.0.19 |
|
1049 | + * @param int|bool $value is recurring. |
|
1050 | + */ |
|
1051 | + public function set_is_recurring( $value ) { |
|
1052 | + $this->set_prop( 'is_recurring', (bool) $value ); |
|
1053 | + } |
|
1054 | + |
|
1055 | + /** |
|
1056 | + * Sets the minimum total that can not be used with this discount. |
|
1057 | + * |
|
1058 | + * @since 1.0.19 |
|
1059 | + * @param float $value minimum total. |
|
1060 | + */ |
|
1061 | + public function set_min_total( $value ) { |
|
1062 | + $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1063 | + } |
|
1064 | + |
|
1065 | + /** |
|
1066 | + * Alias for self::set_min_total(). |
|
1067 | + * |
|
1068 | + * @since 1.0.19 |
|
1069 | + * @param float $value minimum total. |
|
1070 | + */ |
|
1071 | + public function set_minimum_total( $value ) { |
|
1072 | + $this->set_min_total( $value ); |
|
1073 | + } |
|
1074 | + |
|
1075 | + /** |
|
1076 | + * Sets the maximum total that can not be used with this discount. |
|
1077 | + * |
|
1078 | + * @since 1.0.19 |
|
1079 | + * @param float $value maximum total. |
|
1080 | + */ |
|
1081 | + public function set_max_total( $value ) { |
|
1082 | + $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1083 | + } |
|
1084 | + |
|
1085 | + /** |
|
1086 | + * Alias for self::set_max_total(). |
|
1087 | + * |
|
1088 | + * @since 1.0.19 |
|
1089 | + * @param float $value maximum total. |
|
1090 | + */ |
|
1091 | + public function set_maximum_total( $value ) { |
|
1092 | + $this->set_max_total( $value ); |
|
1093 | + } |
|
1094 | + |
|
1095 | + /** |
|
1096 | + * @deprecated |
|
1097 | + */ |
|
1098 | + public function refresh(){} |
|
1099 | + |
|
1100 | + /** |
|
1101 | + * @deprecated |
|
1102 | + * |
|
1103 | + */ |
|
1104 | + public function update_status( $status = 'publish' ){ |
|
1105 | + |
|
1106 | + if ( $this->exists() && $this->get_status() != $status ) { |
|
1107 | + $this->set_status( $status ); |
|
1108 | + $this->save(); |
|
1109 | + } |
|
1110 | + |
|
1111 | + } |
|
1112 | + |
|
1113 | + /* |
|
1114 | 1114 | |-------------------------------------------------------------------------- |
1115 | 1115 | | Conditionals |
1116 | 1116 | |-------------------------------------------------------------------------- |
@@ -1119,263 +1119,263 @@ discard block |
||
1119 | 1119 | | |
1120 | 1120 | */ |
1121 | 1121 | |
1122 | - /** |
|
1123 | - * Checks whether a discount exists in the database or not |
|
1124 | - * |
|
1125 | - * @since 1.0.15 |
|
1126 | - */ |
|
1127 | - public function exists(){ |
|
1128 | - $id = $this->get_id(); |
|
1129 | - return ! empty( $id ); |
|
1130 | - } |
|
1131 | - |
|
1132 | - /** |
|
1133 | - * Checks the discount type. |
|
1134 | - * |
|
1135 | - * |
|
1136 | - * @param string $type the discount type to check against |
|
1137 | - * @since 1.0.15 |
|
1138 | - * @return bool |
|
1139 | - */ |
|
1140 | - public function is_type( $type ) { |
|
1141 | - return $this->get_type() == $type; |
|
1142 | - } |
|
1143 | - |
|
1144 | - /** |
|
1145 | - * Checks whether the discount is published or not |
|
1146 | - * |
|
1147 | - * @since 1.0.15 |
|
1148 | - * @return bool |
|
1149 | - */ |
|
1150 | - public function is_active() { |
|
1151 | - return $this->get_status() == 'publish'; |
|
1152 | - } |
|
1153 | - |
|
1154 | - /** |
|
1155 | - * Checks whether the discount has max uses |
|
1156 | - * |
|
1157 | - * @since 1.0.15 |
|
1158 | - * @return bool |
|
1159 | - */ |
|
1160 | - public function has_limit() { |
|
1161 | - $limit = $this->get_max_uses(); |
|
1162 | - return ! empty( $limit ); |
|
1163 | - } |
|
1164 | - |
|
1165 | - /** |
|
1166 | - * Checks whether the discount has ever been used. |
|
1167 | - * |
|
1168 | - * @since 1.0.15 |
|
1169 | - * @return bool |
|
1170 | - */ |
|
1171 | - public function has_uses() { |
|
1172 | - return $this->get_uses() > 0; |
|
1173 | - } |
|
1174 | - |
|
1175 | - /** |
|
1176 | - * Checks whether the discount is has exided the usage limit or not |
|
1177 | - * |
|
1178 | - * @since 1.0.15 |
|
1179 | - * @return bool |
|
1180 | - */ |
|
1181 | - public function has_exceeded_limit() { |
|
1182 | - |
|
1183 | - if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
1184 | - $exceeded = false ; |
|
1185 | - } else { |
|
1186 | - $exceeded = (int) $this->get_max_uses() <= $this->get_uses(); |
|
1187 | - } |
|
1188 | - |
|
1189 | - return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() ); |
|
1190 | - } |
|
1191 | - |
|
1192 | - /** |
|
1193 | - * Checks whether the discount has an expiration date. |
|
1194 | - * |
|
1195 | - * @since 1.0.15 |
|
1196 | - * @return bool |
|
1197 | - */ |
|
1198 | - public function has_expiration_date() { |
|
1199 | - $date = $this->get_expiration_date(); |
|
1200 | - return ! empty( $date ); |
|
1201 | - } |
|
1202 | - |
|
1203 | - /** |
|
1204 | - * Checks if the discount is expired |
|
1205 | - * |
|
1206 | - * @since 1.0.15 |
|
1207 | - * @return bool |
|
1208 | - */ |
|
1209 | - public function is_expired() { |
|
1210 | - $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
1211 | - return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() ); |
|
1212 | - } |
|
1213 | - |
|
1214 | - /** |
|
1215 | - * Checks whether the discount has a start date. |
|
1216 | - * |
|
1217 | - * @since 1.0.15 |
|
1218 | - * @return bool |
|
1219 | - */ |
|
1220 | - public function has_start_date() { |
|
1221 | - $date = $this->get_start_date(); |
|
1222 | - return ! empty( $date ); |
|
1223 | - } |
|
1224 | - |
|
1225 | - /** |
|
1226 | - * Checks the discount start date. |
|
1227 | - * |
|
1228 | - * @since 1.0.15 |
|
1229 | - * @return bool |
|
1230 | - */ |
|
1231 | - public function has_started() { |
|
1232 | - $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
1233 | - return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() ); |
|
1234 | - } |
|
1235 | - |
|
1236 | - /** |
|
1237 | - * Checks the discount has allowed items or not. |
|
1238 | - * |
|
1239 | - * @since 1.0.15 |
|
1240 | - * @return bool |
|
1241 | - */ |
|
1242 | - public function has_allowed_items() { |
|
1243 | - $allowed_items = $this->get_allowed_items(); |
|
1244 | - return ! empty( $allowed_items ); |
|
1245 | - } |
|
1246 | - |
|
1247 | - /** |
|
1248 | - * Checks the discount has excluded items or not. |
|
1249 | - * |
|
1250 | - * @since 1.0.15 |
|
1251 | - * @return bool |
|
1252 | - */ |
|
1253 | - public function has_excluded_items() { |
|
1254 | - $excluded_items = $this->get_excluded_items(); |
|
1255 | - return ! empty( $excluded_items ); |
|
1256 | - } |
|
1257 | - |
|
1258 | - /** |
|
1259 | - * Check if a discount is valid for a given item id. |
|
1260 | - * |
|
1261 | - * @param int|int[] $item_ids |
|
1262 | - * @since 1.0.15 |
|
1263 | - * @return boolean |
|
1264 | - */ |
|
1265 | - public function is_valid_for_items( $item_ids ) { |
|
1266 | - |
|
1267 | - $item_ids = wp_parse_id_list( $item_ids ); |
|
1268 | - $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
1269 | - $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
1270 | - |
|
1271 | - if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
1272 | - return false; |
|
1273 | - } |
|
1274 | - |
|
1275 | - if ( $this->has_allowed_items() && empty( $included ) ) { |
|
1276 | - return false; |
|
1277 | - } |
|
1278 | - |
|
1279 | - return true; |
|
1280 | - } |
|
1281 | - |
|
1282 | - /** |
|
1283 | - * Check if a discount is valid for the given amount |
|
1284 | - * |
|
1285 | - * @param float $amount The amount to check against |
|
1286 | - * @since 1.0.15 |
|
1287 | - * @return boolean |
|
1288 | - */ |
|
1289 | - public function is_valid_for_amount( $amount ) { |
|
1290 | - return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
1291 | - } |
|
1292 | - |
|
1293 | - /** |
|
1294 | - * Checks if the minimum amount is set |
|
1295 | - * |
|
1296 | - * @since 1.0.15 |
|
1297 | - * @return boolean |
|
1298 | - */ |
|
1299 | - public function has_minimum_amount() { |
|
1300 | - $minimum = $this->get_minimum_total(); |
|
1301 | - return ! empty( $minimum ); |
|
1302 | - } |
|
1303 | - |
|
1304 | - /** |
|
1305 | - * Checks if the minimum amount is met |
|
1306 | - * |
|
1307 | - * @param float $amount The amount to check against |
|
1308 | - * @since 1.0.15 |
|
1309 | - * @return boolean |
|
1310 | - */ |
|
1311 | - public function is_minimum_amount_met( $amount ) { |
|
1312 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1313 | - $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
1314 | - return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1315 | - } |
|
1316 | - |
|
1317 | - /** |
|
1318 | - * Checks if the maximum amount is set |
|
1319 | - * |
|
1320 | - * @since 1.0.15 |
|
1321 | - * @return boolean |
|
1322 | - */ |
|
1323 | - public function has_maximum_amount() { |
|
1324 | - $maximum = $this->get_maximum_total(); |
|
1325 | - return ! empty( $maximum ); |
|
1326 | - } |
|
1327 | - |
|
1328 | - /** |
|
1329 | - * Checks if the maximum amount is met |
|
1330 | - * |
|
1331 | - * @param float $amount The amount to check against |
|
1332 | - * @since 1.0.15 |
|
1333 | - * @return boolean |
|
1334 | - */ |
|
1335 | - public function is_maximum_amount_met( $amount ) { |
|
1336 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1337 | - $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
1338 | - return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1339 | - } |
|
1340 | - |
|
1341 | - /** |
|
1342 | - * Checks if the discount is recurring. |
|
1343 | - * |
|
1344 | - * @since 1.0.15 |
|
1345 | - * @return boolean |
|
1346 | - */ |
|
1347 | - public function is_recurring() { |
|
1348 | - $recurring = $this->get_is_recurring(); |
|
1349 | - return ! empty( $recurring ); |
|
1350 | - } |
|
1351 | - |
|
1352 | - /** |
|
1353 | - * Checks if the discount is single use. |
|
1354 | - * |
|
1355 | - * @since 1.0.15 |
|
1356 | - * @return boolean |
|
1357 | - */ |
|
1358 | - public function is_single_use() { |
|
1359 | - $usage = $this->get_is_single_use(); |
|
1360 | - return ! empty( $usage ); |
|
1361 | - } |
|
1362 | - |
|
1363 | - /** |
|
1364 | - * Check if a discount is valid for the given user |
|
1365 | - * |
|
1366 | - * @param int|string $user |
|
1367 | - * @since 1.0.15 |
|
1368 | - * @return boolean |
|
1369 | - */ |
|
1370 | - public function is_valid_for_user( $user ) { |
|
1371 | - |
|
1372 | - // Ensure that the discount is single use. |
|
1373 | - if ( empty( $user ) || ! $this->is_single_use() ) { |
|
1374 | - return true; |
|
1375 | - } |
|
1376 | - |
|
1377 | - // Prepare the user id. |
|
1378 | - $user_id = 0; |
|
1122 | + /** |
|
1123 | + * Checks whether a discount exists in the database or not |
|
1124 | + * |
|
1125 | + * @since 1.0.15 |
|
1126 | + */ |
|
1127 | + public function exists(){ |
|
1128 | + $id = $this->get_id(); |
|
1129 | + return ! empty( $id ); |
|
1130 | + } |
|
1131 | + |
|
1132 | + /** |
|
1133 | + * Checks the discount type. |
|
1134 | + * |
|
1135 | + * |
|
1136 | + * @param string $type the discount type to check against |
|
1137 | + * @since 1.0.15 |
|
1138 | + * @return bool |
|
1139 | + */ |
|
1140 | + public function is_type( $type ) { |
|
1141 | + return $this->get_type() == $type; |
|
1142 | + } |
|
1143 | + |
|
1144 | + /** |
|
1145 | + * Checks whether the discount is published or not |
|
1146 | + * |
|
1147 | + * @since 1.0.15 |
|
1148 | + * @return bool |
|
1149 | + */ |
|
1150 | + public function is_active() { |
|
1151 | + return $this->get_status() == 'publish'; |
|
1152 | + } |
|
1153 | + |
|
1154 | + /** |
|
1155 | + * Checks whether the discount has max uses |
|
1156 | + * |
|
1157 | + * @since 1.0.15 |
|
1158 | + * @return bool |
|
1159 | + */ |
|
1160 | + public function has_limit() { |
|
1161 | + $limit = $this->get_max_uses(); |
|
1162 | + return ! empty( $limit ); |
|
1163 | + } |
|
1164 | + |
|
1165 | + /** |
|
1166 | + * Checks whether the discount has ever been used. |
|
1167 | + * |
|
1168 | + * @since 1.0.15 |
|
1169 | + * @return bool |
|
1170 | + */ |
|
1171 | + public function has_uses() { |
|
1172 | + return $this->get_uses() > 0; |
|
1173 | + } |
|
1174 | + |
|
1175 | + /** |
|
1176 | + * Checks whether the discount is has exided the usage limit or not |
|
1177 | + * |
|
1178 | + * @since 1.0.15 |
|
1179 | + * @return bool |
|
1180 | + */ |
|
1181 | + public function has_exceeded_limit() { |
|
1182 | + |
|
1183 | + if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
1184 | + $exceeded = false ; |
|
1185 | + } else { |
|
1186 | + $exceeded = (int) $this->get_max_uses() <= $this->get_uses(); |
|
1187 | + } |
|
1188 | + |
|
1189 | + return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() ); |
|
1190 | + } |
|
1191 | + |
|
1192 | + /** |
|
1193 | + * Checks whether the discount has an expiration date. |
|
1194 | + * |
|
1195 | + * @since 1.0.15 |
|
1196 | + * @return bool |
|
1197 | + */ |
|
1198 | + public function has_expiration_date() { |
|
1199 | + $date = $this->get_expiration_date(); |
|
1200 | + return ! empty( $date ); |
|
1201 | + } |
|
1202 | + |
|
1203 | + /** |
|
1204 | + * Checks if the discount is expired |
|
1205 | + * |
|
1206 | + * @since 1.0.15 |
|
1207 | + * @return bool |
|
1208 | + */ |
|
1209 | + public function is_expired() { |
|
1210 | + $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
1211 | + return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() ); |
|
1212 | + } |
|
1213 | + |
|
1214 | + /** |
|
1215 | + * Checks whether the discount has a start date. |
|
1216 | + * |
|
1217 | + * @since 1.0.15 |
|
1218 | + * @return bool |
|
1219 | + */ |
|
1220 | + public function has_start_date() { |
|
1221 | + $date = $this->get_start_date(); |
|
1222 | + return ! empty( $date ); |
|
1223 | + } |
|
1224 | + |
|
1225 | + /** |
|
1226 | + * Checks the discount start date. |
|
1227 | + * |
|
1228 | + * @since 1.0.15 |
|
1229 | + * @return bool |
|
1230 | + */ |
|
1231 | + public function has_started() { |
|
1232 | + $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
1233 | + return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() ); |
|
1234 | + } |
|
1235 | + |
|
1236 | + /** |
|
1237 | + * Checks the discount has allowed items or not. |
|
1238 | + * |
|
1239 | + * @since 1.0.15 |
|
1240 | + * @return bool |
|
1241 | + */ |
|
1242 | + public function has_allowed_items() { |
|
1243 | + $allowed_items = $this->get_allowed_items(); |
|
1244 | + return ! empty( $allowed_items ); |
|
1245 | + } |
|
1246 | + |
|
1247 | + /** |
|
1248 | + * Checks the discount has excluded items or not. |
|
1249 | + * |
|
1250 | + * @since 1.0.15 |
|
1251 | + * @return bool |
|
1252 | + */ |
|
1253 | + public function has_excluded_items() { |
|
1254 | + $excluded_items = $this->get_excluded_items(); |
|
1255 | + return ! empty( $excluded_items ); |
|
1256 | + } |
|
1257 | + |
|
1258 | + /** |
|
1259 | + * Check if a discount is valid for a given item id. |
|
1260 | + * |
|
1261 | + * @param int|int[] $item_ids |
|
1262 | + * @since 1.0.15 |
|
1263 | + * @return boolean |
|
1264 | + */ |
|
1265 | + public function is_valid_for_items( $item_ids ) { |
|
1266 | + |
|
1267 | + $item_ids = wp_parse_id_list( $item_ids ); |
|
1268 | + $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
1269 | + $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
1270 | + |
|
1271 | + if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
1272 | + return false; |
|
1273 | + } |
|
1274 | + |
|
1275 | + if ( $this->has_allowed_items() && empty( $included ) ) { |
|
1276 | + return false; |
|
1277 | + } |
|
1278 | + |
|
1279 | + return true; |
|
1280 | + } |
|
1281 | + |
|
1282 | + /** |
|
1283 | + * Check if a discount is valid for the given amount |
|
1284 | + * |
|
1285 | + * @param float $amount The amount to check against |
|
1286 | + * @since 1.0.15 |
|
1287 | + * @return boolean |
|
1288 | + */ |
|
1289 | + public function is_valid_for_amount( $amount ) { |
|
1290 | + return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
1291 | + } |
|
1292 | + |
|
1293 | + /** |
|
1294 | + * Checks if the minimum amount is set |
|
1295 | + * |
|
1296 | + * @since 1.0.15 |
|
1297 | + * @return boolean |
|
1298 | + */ |
|
1299 | + public function has_minimum_amount() { |
|
1300 | + $minimum = $this->get_minimum_total(); |
|
1301 | + return ! empty( $minimum ); |
|
1302 | + } |
|
1303 | + |
|
1304 | + /** |
|
1305 | + * Checks if the minimum amount is met |
|
1306 | + * |
|
1307 | + * @param float $amount The amount to check against |
|
1308 | + * @since 1.0.15 |
|
1309 | + * @return boolean |
|
1310 | + */ |
|
1311 | + public function is_minimum_amount_met( $amount ) { |
|
1312 | + $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1313 | + $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
1314 | + return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1315 | + } |
|
1316 | + |
|
1317 | + /** |
|
1318 | + * Checks if the maximum amount is set |
|
1319 | + * |
|
1320 | + * @since 1.0.15 |
|
1321 | + * @return boolean |
|
1322 | + */ |
|
1323 | + public function has_maximum_amount() { |
|
1324 | + $maximum = $this->get_maximum_total(); |
|
1325 | + return ! empty( $maximum ); |
|
1326 | + } |
|
1327 | + |
|
1328 | + /** |
|
1329 | + * Checks if the maximum amount is met |
|
1330 | + * |
|
1331 | + * @param float $amount The amount to check against |
|
1332 | + * @since 1.0.15 |
|
1333 | + * @return boolean |
|
1334 | + */ |
|
1335 | + public function is_maximum_amount_met( $amount ) { |
|
1336 | + $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1337 | + $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
1338 | + return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1339 | + } |
|
1340 | + |
|
1341 | + /** |
|
1342 | + * Checks if the discount is recurring. |
|
1343 | + * |
|
1344 | + * @since 1.0.15 |
|
1345 | + * @return boolean |
|
1346 | + */ |
|
1347 | + public function is_recurring() { |
|
1348 | + $recurring = $this->get_is_recurring(); |
|
1349 | + return ! empty( $recurring ); |
|
1350 | + } |
|
1351 | + |
|
1352 | + /** |
|
1353 | + * Checks if the discount is single use. |
|
1354 | + * |
|
1355 | + * @since 1.0.15 |
|
1356 | + * @return boolean |
|
1357 | + */ |
|
1358 | + public function is_single_use() { |
|
1359 | + $usage = $this->get_is_single_use(); |
|
1360 | + return ! empty( $usage ); |
|
1361 | + } |
|
1362 | + |
|
1363 | + /** |
|
1364 | + * Check if a discount is valid for the given user |
|
1365 | + * |
|
1366 | + * @param int|string $user |
|
1367 | + * @since 1.0.15 |
|
1368 | + * @return boolean |
|
1369 | + */ |
|
1370 | + public function is_valid_for_user( $user ) { |
|
1371 | + |
|
1372 | + // Ensure that the discount is single use. |
|
1373 | + if ( empty( $user ) || ! $this->is_single_use() ) { |
|
1374 | + return true; |
|
1375 | + } |
|
1376 | + |
|
1377 | + // Prepare the user id. |
|
1378 | + $user_id = 0; |
|
1379 | 1379 | if ( is_numeric( $user ) ) { |
1380 | 1380 | $user_id = absint( $user ); |
1381 | 1381 | } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
@@ -1384,117 +1384,117 @@ discard block |
||
1384 | 1384 | $user_id = $user_data->ID; |
1385 | 1385 | } |
1386 | 1386 | |
1387 | - // Ensure that we have a user. |
|
1388 | - if ( empty( $user_id ) ) { |
|
1389 | - return true; |
|
1390 | - } |
|
1387 | + // Ensure that we have a user. |
|
1388 | + if ( empty( $user_id ) ) { |
|
1389 | + return true; |
|
1390 | + } |
|
1391 | 1391 | |
1392 | - // Get all payments with matching user id. |
|
1392 | + // Get all payments with matching user id. |
|
1393 | 1393 | $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false, 'paginate' => false ) ); |
1394 | - $code = strtolower( $this->get_code() ); |
|
1395 | - |
|
1396 | - // For each payment... |
|
1397 | - foreach ( $payments as $payment ) { |
|
1398 | - |
|
1399 | - // Only check for paid invoices. |
|
1400 | - if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) { |
|
1401 | - return false; |
|
1402 | - } |
|
1403 | - |
|
1404 | - } |
|
1405 | - |
|
1406 | - return true; |
|
1407 | - } |
|
1408 | - |
|
1409 | - /** |
|
1410 | - * Deletes the discount from the database |
|
1411 | - * |
|
1412 | - * @since 1.0.15 |
|
1413 | - * @return boolean |
|
1414 | - */ |
|
1415 | - public function remove() { |
|
1416 | - return $this->delete(); |
|
1417 | - } |
|
1418 | - |
|
1419 | - /** |
|
1420 | - * Increases a discount's usage. |
|
1421 | - * |
|
1422 | - * @since 1.0.15 |
|
1423 | - * @param int $by The number of usages to increas by. |
|
1424 | - * @return int |
|
1425 | - */ |
|
1426 | - public function increase_usage( $by = 1 ) { |
|
1427 | - |
|
1428 | - // Abort if zero. |
|
1429 | - if ( empty( $by ) ) { |
|
1430 | - return; |
|
1431 | - } |
|
1432 | - |
|
1433 | - // Increase the usage. |
|
1434 | - $this->set_uses( $this->get_uses() + (int) $by ); |
|
1435 | - |
|
1436 | - // Save the discount. |
|
1437 | - $this->save(); |
|
1438 | - |
|
1439 | - // Fire relevant hooks. |
|
1440 | - if( (int) $by > 0 ) { |
|
1441 | - do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1442 | - } else { |
|
1443 | - do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1444 | - } |
|
1445 | - |
|
1446 | - // Return the number of times the discount has been used. |
|
1447 | - return $this->get_uses(); |
|
1448 | - } |
|
1449 | - |
|
1450 | - /** |
|
1451 | - * Alias of self::__toString() |
|
1452 | - * |
|
1453 | - * @since 1.0.15 |
|
1454 | - * @return string|false |
|
1455 | - */ |
|
1456 | - public function get_data_as_json() { |
|
1457 | - return $this->__toString(); |
|
1458 | - } |
|
1459 | - |
|
1460 | - /** |
|
1461 | - * Returns a discount's discounted amount. |
|
1462 | - * |
|
1463 | - * @since 1.0.15 |
|
1464 | - * @param float $amount |
|
1465 | - * @return float |
|
1466 | - */ |
|
1467 | - public function get_discounted_amount( $amount ) { |
|
1468 | - |
|
1469 | - // Convert amount to float. |
|
1470 | - $amount = (float) $amount; |
|
1471 | - |
|
1472 | - // Get discount amount. |
|
1473 | - $discount_amount = $this->get_amount(); |
|
1474 | - |
|
1475 | - if ( empty( $discount_amount ) ) { |
|
1476 | - return 0; |
|
1477 | - } |
|
1478 | - |
|
1479 | - // Format the amount. |
|
1480 | - $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
1394 | + $code = strtolower( $this->get_code() ); |
|
1395 | + |
|
1396 | + // For each payment... |
|
1397 | + foreach ( $payments as $payment ) { |
|
1398 | + |
|
1399 | + // Only check for paid invoices. |
|
1400 | + if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) { |
|
1401 | + return false; |
|
1402 | + } |
|
1403 | + |
|
1404 | + } |
|
1405 | + |
|
1406 | + return true; |
|
1407 | + } |
|
1408 | + |
|
1409 | + /** |
|
1410 | + * Deletes the discount from the database |
|
1411 | + * |
|
1412 | + * @since 1.0.15 |
|
1413 | + * @return boolean |
|
1414 | + */ |
|
1415 | + public function remove() { |
|
1416 | + return $this->delete(); |
|
1417 | + } |
|
1418 | + |
|
1419 | + /** |
|
1420 | + * Increases a discount's usage. |
|
1421 | + * |
|
1422 | + * @since 1.0.15 |
|
1423 | + * @param int $by The number of usages to increas by. |
|
1424 | + * @return int |
|
1425 | + */ |
|
1426 | + public function increase_usage( $by = 1 ) { |
|
1427 | + |
|
1428 | + // Abort if zero. |
|
1429 | + if ( empty( $by ) ) { |
|
1430 | + return; |
|
1431 | + } |
|
1432 | + |
|
1433 | + // Increase the usage. |
|
1434 | + $this->set_uses( $this->get_uses() + (int) $by ); |
|
1435 | + |
|
1436 | + // Save the discount. |
|
1437 | + $this->save(); |
|
1438 | + |
|
1439 | + // Fire relevant hooks. |
|
1440 | + if( (int) $by > 0 ) { |
|
1441 | + do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1442 | + } else { |
|
1443 | + do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1444 | + } |
|
1445 | + |
|
1446 | + // Return the number of times the discount has been used. |
|
1447 | + return $this->get_uses(); |
|
1448 | + } |
|
1449 | + |
|
1450 | + /** |
|
1451 | + * Alias of self::__toString() |
|
1452 | + * |
|
1453 | + * @since 1.0.15 |
|
1454 | + * @return string|false |
|
1455 | + */ |
|
1456 | + public function get_data_as_json() { |
|
1457 | + return $this->__toString(); |
|
1458 | + } |
|
1459 | + |
|
1460 | + /** |
|
1461 | + * Returns a discount's discounted amount. |
|
1462 | + * |
|
1463 | + * @since 1.0.15 |
|
1464 | + * @param float $amount |
|
1465 | + * @return float |
|
1466 | + */ |
|
1467 | + public function get_discounted_amount( $amount ) { |
|
1468 | + |
|
1469 | + // Convert amount to float. |
|
1470 | + $amount = (float) $amount; |
|
1471 | + |
|
1472 | + // Get discount amount. |
|
1473 | + $discount_amount = $this->get_amount(); |
|
1474 | + |
|
1475 | + if ( empty( $discount_amount ) ) { |
|
1476 | + return 0; |
|
1477 | + } |
|
1478 | + |
|
1479 | + // Format the amount. |
|
1480 | + $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
1481 | 1481 | |
1482 | - // If this is a percentage discount. |
|
1483 | - if ( $this->is_type( 'percent' ) ) { |
|
1482 | + // If this is a percentage discount. |
|
1483 | + if ( $this->is_type( 'percent' ) ) { |
|
1484 | 1484 | $discount_amount = $amount * ( $discount_amount / 100 ); |
1485 | - } |
|
1485 | + } |
|
1486 | 1486 | |
1487 | - // Discount can not be less than zero... |
|
1488 | - if ( $discount_amount < 0 ) { |
|
1489 | - $discount_amount = 0; |
|
1490 | - } |
|
1487 | + // Discount can not be less than zero... |
|
1488 | + if ( $discount_amount < 0 ) { |
|
1489 | + $discount_amount = 0; |
|
1490 | + } |
|
1491 | 1491 | |
1492 | - // ... or more than the amount. |
|
1493 | - if ( $discount_amount > $amount ) { |
|
1494 | - $discount_amount = $amount; |
|
1495 | - } |
|
1492 | + // ... or more than the amount. |
|
1493 | + if ( $discount_amount > $amount ) { |
|
1494 | + $discount_amount = $amount; |
|
1495 | + } |
|
1496 | 1496 | |
1497 | - return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
1498 | - } |
|
1497 | + return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
1498 | + } |
|
1499 | 1499 | |
1500 | 1500 | } |
@@ -12,444 +12,444 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Invoice_Notification_Emails { |
14 | 14 | |
15 | - /** |
|
16 | - * The array of invoice email actions. |
|
17 | - * |
|
18 | - * @param array |
|
19 | - */ |
|
20 | - public $invoice_actions; |
|
21 | - |
|
22 | - /** |
|
23 | - * Class constructor |
|
24 | - * |
|
25 | - */ |
|
26 | - public function __construct() { |
|
27 | - |
|
28 | - $this->invoice_actions = apply_filters( |
|
29 | - 'getpaid_notification_email_invoice_triggers', |
|
30 | - array( |
|
31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
32 | - 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
33 | - 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
34 | - 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
35 | - 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
36 | - 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
37 | - 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
38 | - 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
39 | - 'getpaid_new_customer_note' => 'user_note', |
|
40 | - 'getpaid_daily_maintenance' => 'overdue', |
|
41 | - ) |
|
42 | - ); |
|
43 | - |
|
44 | - $this->init_hooks(); |
|
45 | - |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Registers email hooks. |
|
50 | - */ |
|
51 | - public function init_hooks() { |
|
52 | - |
|
53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
55 | - |
|
56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Registers an email hook for an invoice action. |
|
63 | - * |
|
64 | - * @param string $hook |
|
65 | - * @param string|array $email_type |
|
66 | - */ |
|
67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
68 | - |
|
69 | - $email_type = wpinv_parse_list( $email_type ); |
|
70 | - |
|
71 | - foreach ( $email_type as $type ) { |
|
72 | - |
|
73 | - $email = new GetPaid_Notification_Email( $type ); |
|
74 | - |
|
75 | - // Abort if it is not active. |
|
76 | - if ( ! $email->is_active() ) { |
|
77 | - continue; |
|
78 | - } |
|
79 | - |
|
80 | - if ( method_exists( $this, $type ) ) { |
|
81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
82 | - continue; |
|
83 | - } |
|
84 | - |
|
85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
86 | - } |
|
87 | - |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Filters invoice merge tags. |
|
92 | - * |
|
93 | - * @param array $merge_tags |
|
94 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
95 | - */ |
|
96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
97 | - |
|
98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
99 | - return array_merge( |
|
100 | - $merge_tags, |
|
101 | - $this->get_invoice_merge_tags( $object ) |
|
102 | - ); |
|
103 | - } |
|
104 | - |
|
105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
106 | - return array_merge( |
|
107 | - $merge_tags, |
|
108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
109 | - ); |
|
110 | - } |
|
111 | - |
|
112 | - return $merge_tags; |
|
113 | - |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Generates invoice merge tags. |
|
118 | - * |
|
119 | - * @param WPInv_Invoice $invoice |
|
120 | - * @return array |
|
121 | - */ |
|
122 | - public function get_invoice_merge_tags( $invoice ) { |
|
123 | - |
|
124 | - // Abort if it does not exist. |
|
125 | - if ( ! $invoice->get_id() ) { |
|
126 | - return array(); |
|
127 | - } |
|
128 | - |
|
129 | - $merge_tags = array( |
|
130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
148 | - ); |
|
149 | - |
|
150 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Helper function to send an email. |
|
155 | - * |
|
156 | - * @param WPInv_Invoice $invoice |
|
157 | - * @param GetPaid_Notification_Email $email |
|
158 | - * @param string $type |
|
159 | - * @param string|array $recipients |
|
160 | - * @param array $extra_args Extra template args. |
|
161 | - */ |
|
162 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
163 | - |
|
164 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
165 | - |
|
166 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
167 | - $merge_tags = $email->get_merge_tags(); |
|
168 | - |
|
169 | - $result = $mailer->send( |
|
170 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
171 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
172 | - $email->get_content( $merge_tags, $extra_args ), |
|
173 | - $email->get_attachments() |
|
174 | - ); |
|
175 | - |
|
176 | - // Maybe send a copy to the admin. |
|
177 | - if ( $email->include_admin_bcc() ) { |
|
178 | - $mailer->send( |
|
179 | - wpinv_get_admin_email(), |
|
180 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
181 | - $email->get_content( $merge_tags ), |
|
182 | - $email->get_attachments() |
|
183 | - ); |
|
184 | - } |
|
185 | - |
|
186 | - if ( ! $result ) { |
|
187 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
188 | - } |
|
189 | - |
|
190 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
191 | - |
|
192 | - return $result; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Also send emails to any cc users. |
|
197 | - * |
|
198 | - * @param array $recipients |
|
199 | - * @param GetPaid_Notification_Email $email |
|
200 | - */ |
|
201 | - public function filter_email_recipients( $recipients, $email ) { |
|
202 | - |
|
203 | - if ( ! $email->is_admin_email() ) { |
|
204 | - $cc = $email->object->get_email_cc(); |
|
205 | - |
|
206 | - if ( ! empty( $cc ) ) { |
|
207 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
208 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
209 | - } |
|
210 | - |
|
211 | - } |
|
212 | - |
|
213 | - return $recipients; |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Sends a new invoice notification. |
|
219 | - * |
|
220 | - * @param WPInv_Invoice $invoice |
|
221 | - */ |
|
222 | - public function new_invoice( $invoice ) { |
|
223 | - |
|
224 | - // Only send this email for invoices created via the admin page. |
|
225 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
226 | - return; |
|
227 | - } |
|
228 | - |
|
229 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
230 | - $recipient = wpinv_get_admin_email(); |
|
231 | - |
|
232 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
233 | - |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Sends a cancelled invoice notification. |
|
238 | - * |
|
239 | - * @param WPInv_Invoice $invoice |
|
240 | - */ |
|
241 | - public function cancelled_invoice( $invoice ) { |
|
15 | + /** |
|
16 | + * The array of invoice email actions. |
|
17 | + * |
|
18 | + * @param array |
|
19 | + */ |
|
20 | + public $invoice_actions; |
|
21 | + |
|
22 | + /** |
|
23 | + * Class constructor |
|
24 | + * |
|
25 | + */ |
|
26 | + public function __construct() { |
|
27 | + |
|
28 | + $this->invoice_actions = apply_filters( |
|
29 | + 'getpaid_notification_email_invoice_triggers', |
|
30 | + array( |
|
31 | + 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
32 | + 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
33 | + 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
34 | + 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
35 | + 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
36 | + 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
37 | + 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
38 | + 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
39 | + 'getpaid_new_customer_note' => 'user_note', |
|
40 | + 'getpaid_daily_maintenance' => 'overdue', |
|
41 | + ) |
|
42 | + ); |
|
43 | + |
|
44 | + $this->init_hooks(); |
|
45 | + |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Registers email hooks. |
|
50 | + */ |
|
51 | + public function init_hooks() { |
|
52 | + |
|
53 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
54 | + add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
55 | + |
|
56 | + foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
57 | + $this->init_email_type_hook( $hook, $email_type ); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Registers an email hook for an invoice action. |
|
63 | + * |
|
64 | + * @param string $hook |
|
65 | + * @param string|array $email_type |
|
66 | + */ |
|
67 | + public function init_email_type_hook( $hook, $email_type ) { |
|
68 | + |
|
69 | + $email_type = wpinv_parse_list( $email_type ); |
|
70 | + |
|
71 | + foreach ( $email_type as $type ) { |
|
72 | + |
|
73 | + $email = new GetPaid_Notification_Email( $type ); |
|
74 | + |
|
75 | + // Abort if it is not active. |
|
76 | + if ( ! $email->is_active() ) { |
|
77 | + continue; |
|
78 | + } |
|
79 | + |
|
80 | + if ( method_exists( $this, $type ) ) { |
|
81 | + add_action( $hook, array( $this, $type ), 100, 2 ); |
|
82 | + continue; |
|
83 | + } |
|
84 | + |
|
85 | + do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
86 | + } |
|
87 | + |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Filters invoice merge tags. |
|
92 | + * |
|
93 | + * @param array $merge_tags |
|
94 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
95 | + */ |
|
96 | + public function invoice_merge_tags( $merge_tags, $object ) { |
|
97 | + |
|
98 | + if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
99 | + return array_merge( |
|
100 | + $merge_tags, |
|
101 | + $this->get_invoice_merge_tags( $object ) |
|
102 | + ); |
|
103 | + } |
|
104 | + |
|
105 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
106 | + return array_merge( |
|
107 | + $merge_tags, |
|
108 | + $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
109 | + ); |
|
110 | + } |
|
111 | + |
|
112 | + return $merge_tags; |
|
113 | + |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Generates invoice merge tags. |
|
118 | + * |
|
119 | + * @param WPInv_Invoice $invoice |
|
120 | + * @return array |
|
121 | + */ |
|
122 | + public function get_invoice_merge_tags( $invoice ) { |
|
123 | + |
|
124 | + // Abort if it does not exist. |
|
125 | + if ( ! $invoice->get_id() ) { |
|
126 | + return array(); |
|
127 | + } |
|
128 | + |
|
129 | + $merge_tags = array( |
|
130 | + '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
131 | + '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
132 | + '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
133 | + '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
134 | + '{email}' => sanitize_email( $invoice->get_email() ), |
|
135 | + '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
136 | + '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
137 | + '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
138 | + '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
139 | + '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
140 | + '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
141 | + '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
142 | + '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
143 | + '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
144 | + '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
145 | + '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
146 | + '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
147 | + '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
148 | + ); |
|
149 | + |
|
150 | + return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Helper function to send an email. |
|
155 | + * |
|
156 | + * @param WPInv_Invoice $invoice |
|
157 | + * @param GetPaid_Notification_Email $email |
|
158 | + * @param string $type |
|
159 | + * @param string|array $recipients |
|
160 | + * @param array $extra_args Extra template args. |
|
161 | + */ |
|
162 | + public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
163 | + |
|
164 | + do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
165 | + |
|
166 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
167 | + $merge_tags = $email->get_merge_tags(); |
|
168 | + |
|
169 | + $result = $mailer->send( |
|
170 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
171 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
172 | + $email->get_content( $merge_tags, $extra_args ), |
|
173 | + $email->get_attachments() |
|
174 | + ); |
|
175 | + |
|
176 | + // Maybe send a copy to the admin. |
|
177 | + if ( $email->include_admin_bcc() ) { |
|
178 | + $mailer->send( |
|
179 | + wpinv_get_admin_email(), |
|
180 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
181 | + $email->get_content( $merge_tags ), |
|
182 | + $email->get_attachments() |
|
183 | + ); |
|
184 | + } |
|
185 | + |
|
186 | + if ( ! $result ) { |
|
187 | + $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
188 | + } |
|
189 | + |
|
190 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
191 | + |
|
192 | + return $result; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Also send emails to any cc users. |
|
197 | + * |
|
198 | + * @param array $recipients |
|
199 | + * @param GetPaid_Notification_Email $email |
|
200 | + */ |
|
201 | + public function filter_email_recipients( $recipients, $email ) { |
|
202 | + |
|
203 | + if ( ! $email->is_admin_email() ) { |
|
204 | + $cc = $email->object->get_email_cc(); |
|
205 | + |
|
206 | + if ( ! empty( $cc ) ) { |
|
207 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
208 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
209 | + } |
|
210 | + |
|
211 | + } |
|
212 | + |
|
213 | + return $recipients; |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Sends a new invoice notification. |
|
219 | + * |
|
220 | + * @param WPInv_Invoice $invoice |
|
221 | + */ |
|
222 | + public function new_invoice( $invoice ) { |
|
223 | + |
|
224 | + // Only send this email for invoices created via the admin page. |
|
225 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
226 | + return; |
|
227 | + } |
|
228 | + |
|
229 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
230 | + $recipient = wpinv_get_admin_email(); |
|
231 | + |
|
232 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
233 | + |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Sends a cancelled invoice notification. |
|
238 | + * |
|
239 | + * @param WPInv_Invoice $invoice |
|
240 | + */ |
|
241 | + public function cancelled_invoice( $invoice ) { |
|
242 | 242 | |
243 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
244 | - $recipient = wpinv_get_admin_email(); |
|
243 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
244 | + $recipient = wpinv_get_admin_email(); |
|
245 | 245 | |
246 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
246 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
247 | 247 | |
248 | - } |
|
248 | + } |
|
249 | 249 | |
250 | - /** |
|
251 | - * Sends a failed invoice notification. |
|
252 | - * |
|
253 | - * @param WPInv_Invoice $invoice |
|
254 | - */ |
|
255 | - public function failed_invoice( $invoice ) { |
|
250 | + /** |
|
251 | + * Sends a failed invoice notification. |
|
252 | + * |
|
253 | + * @param WPInv_Invoice $invoice |
|
254 | + */ |
|
255 | + public function failed_invoice( $invoice ) { |
|
256 | 256 | |
257 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
258 | - $recipient = wpinv_get_admin_email(); |
|
257 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
258 | + $recipient = wpinv_get_admin_email(); |
|
259 | 259 | |
260 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
260 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
261 | 261 | |
262 | - } |
|
262 | + } |
|
263 | 263 | |
264 | - /** |
|
265 | - * Sends a notification whenever an invoice is put on hold. |
|
266 | - * |
|
267 | - * @param WPInv_Invoice $invoice |
|
268 | - */ |
|
269 | - public function onhold_invoice( $invoice ) { |
|
264 | + /** |
|
265 | + * Sends a notification whenever an invoice is put on hold. |
|
266 | + * |
|
267 | + * @param WPInv_Invoice $invoice |
|
268 | + */ |
|
269 | + public function onhold_invoice( $invoice ) { |
|
270 | 270 | |
271 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
272 | - $recipient = $invoice->get_email(); |
|
271 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
272 | + $recipient = $invoice->get_email(); |
|
273 | 273 | |
274 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
274 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
275 | 275 | |
276 | - } |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
280 | - * |
|
281 | - * @param WPInv_Invoice $invoice |
|
282 | - */ |
|
283 | - public function processing_invoice( $invoice ) { |
|
278 | + /** |
|
279 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
280 | + * |
|
281 | + * @param WPInv_Invoice $invoice |
|
282 | + */ |
|
283 | + public function processing_invoice( $invoice ) { |
|
284 | 284 | |
285 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
286 | - $recipient = $invoice->get_email(); |
|
285 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
286 | + $recipient = $invoice->get_email(); |
|
287 | 287 | |
288 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
288 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
289 | 289 | |
290 | - } |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Sends a notification whenever an invoice is paid. |
|
294 | - * |
|
295 | - * @param WPInv_Invoice $invoice |
|
296 | - */ |
|
297 | - public function completed_invoice( $invoice ) { |
|
292 | + /** |
|
293 | + * Sends a notification whenever an invoice is paid. |
|
294 | + * |
|
295 | + * @param WPInv_Invoice $invoice |
|
296 | + */ |
|
297 | + public function completed_invoice( $invoice ) { |
|
298 | 298 | |
299 | - // (Maybe) abort if it is a renewal invoice. |
|
300 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
301 | - return; |
|
302 | - } |
|
299 | + // (Maybe) abort if it is a renewal invoice. |
|
300 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
301 | + return; |
|
302 | + } |
|
303 | 303 | |
304 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
305 | - $recipient = $invoice->get_email(); |
|
304 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
305 | + $recipient = $invoice->get_email(); |
|
306 | 306 | |
307 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
307 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
308 | 308 | |
309 | - } |
|
309 | + } |
|
310 | 310 | |
311 | - /** |
|
312 | - * Sends a notification whenever an invoice is refunded. |
|
313 | - * |
|
314 | - * @param WPInv_Invoice $invoice |
|
315 | - */ |
|
316 | - public function refunded_invoice( $invoice ) { |
|
311 | + /** |
|
312 | + * Sends a notification whenever an invoice is refunded. |
|
313 | + * |
|
314 | + * @param WPInv_Invoice $invoice |
|
315 | + */ |
|
316 | + public function refunded_invoice( $invoice ) { |
|
317 | 317 | |
318 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
319 | - $recipient = $invoice->get_email(); |
|
318 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
319 | + $recipient = $invoice->get_email(); |
|
320 | 320 | |
321 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
321 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
322 | 322 | |
323 | - } |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Notifies a user about new invoices |
|
327 | - * |
|
328 | - * @param WPInv_Invoice $invoice |
|
329 | - */ |
|
330 | - public function user_invoice( $invoice ) { |
|
325 | + /** |
|
326 | + * Notifies a user about new invoices |
|
327 | + * |
|
328 | + * @param WPInv_Invoice $invoice |
|
329 | + */ |
|
330 | + public function user_invoice( $invoice ) { |
|
331 | 331 | |
332 | - // Only send this email for invoices created via the admin page. |
|
333 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
334 | - return; |
|
335 | - } |
|
332 | + // Only send this email for invoices created via the admin page. |
|
333 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
334 | + return; |
|
335 | + } |
|
336 | 336 | |
337 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
338 | - $recipient = $invoice->get_email(); |
|
337 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
338 | + $recipient = $invoice->get_email(); |
|
339 | 339 | |
340 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
340 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
341 | 341 | |
342 | - } |
|
342 | + } |
|
343 | 343 | |
344 | - /** |
|
345 | - * Checks if an invoice is a payment form invoice. |
|
346 | - * |
|
347 | - * @param int $invoice |
|
348 | - * @return bool |
|
349 | - */ |
|
350 | - public function is_payment_form_invoice( $invoice ) { |
|
351 | - return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
352 | - } |
|
344 | + /** |
|
345 | + * Checks if an invoice is a payment form invoice. |
|
346 | + * |
|
347 | + * @param int $invoice |
|
348 | + * @return bool |
|
349 | + */ |
|
350 | + public function is_payment_form_invoice( $invoice ) { |
|
351 | + return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * Notifies admin about new invoice notes |
|
356 | - * |
|
357 | - * @param WPInv_Invoice $invoice |
|
358 | - * @param string $note |
|
359 | - */ |
|
360 | - public function user_note( $invoice, $note ) { |
|
354 | + /** |
|
355 | + * Notifies admin about new invoice notes |
|
356 | + * |
|
357 | + * @param WPInv_Invoice $invoice |
|
358 | + * @param string $note |
|
359 | + */ |
|
360 | + public function user_note( $invoice, $note ) { |
|
361 | 361 | |
362 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
363 | - $recipient = $invoice->get_email(); |
|
364 | - |
|
365 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
366 | - |
|
367 | - } |
|
362 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
363 | + $recipient = $invoice->get_email(); |
|
364 | + |
|
365 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
366 | + |
|
367 | + } |
|
368 | 368 | |
369 | - /** |
|
370 | - * (Force) Sends overdue notices. |
|
371 | - * |
|
372 | - * @param WPInv_Invoice $invoice |
|
373 | - */ |
|
374 | - public function force_send_overdue_notice( $invoice ) { |
|
375 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
376 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Sends overdue notices. |
|
381 | - * |
|
382 | - * @TODO: Create an invoices query class. |
|
383 | - */ |
|
384 | - public function overdue() { |
|
385 | - global $wpdb; |
|
386 | - |
|
387 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
388 | - |
|
389 | - // Fetch reminder days. |
|
390 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
391 | - |
|
392 | - // Abort if non is set. |
|
393 | - if ( empty( $reminder_days ) ) { |
|
394 | - return; |
|
395 | - } |
|
396 | - |
|
397 | - // Retrieve date query. |
|
398 | - $date_query = $this->get_date_query( $reminder_days ); |
|
399 | - |
|
400 | - // Invoices table. |
|
401 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
402 | - |
|
403 | - // Fetch invoices. |
|
404 | - $invoices = $wpdb->get_col( |
|
405 | - "SELECT posts.ID FROM $wpdb->posts as posts |
|
369 | + /** |
|
370 | + * (Force) Sends overdue notices. |
|
371 | + * |
|
372 | + * @param WPInv_Invoice $invoice |
|
373 | + */ |
|
374 | + public function force_send_overdue_notice( $invoice ) { |
|
375 | + $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
376 | + return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Sends overdue notices. |
|
381 | + * |
|
382 | + * @TODO: Create an invoices query class. |
|
383 | + */ |
|
384 | + public function overdue() { |
|
385 | + global $wpdb; |
|
386 | + |
|
387 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
388 | + |
|
389 | + // Fetch reminder days. |
|
390 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
391 | + |
|
392 | + // Abort if non is set. |
|
393 | + if ( empty( $reminder_days ) ) { |
|
394 | + return; |
|
395 | + } |
|
396 | + |
|
397 | + // Retrieve date query. |
|
398 | + $date_query = $this->get_date_query( $reminder_days ); |
|
399 | + |
|
400 | + // Invoices table. |
|
401 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
402 | + |
|
403 | + // Fetch invoices. |
|
404 | + $invoices = $wpdb->get_col( |
|
405 | + "SELECT posts.ID FROM $wpdb->posts as posts |
|
406 | 406 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
407 | 407 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
408 | 408 | |
409 | - foreach ( $invoices as $invoice ) { |
|
409 | + foreach ( $invoices as $invoice ) { |
|
410 | 410 | |
411 | - // Only send this email for invoices created via the admin page. |
|
412 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
413 | - $invoice = new WPInv_Invoice( $invoice ); |
|
414 | - $email->object = $invoice; |
|
411 | + // Only send this email for invoices created via the admin page. |
|
412 | + if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
413 | + $invoice = new WPInv_Invoice( $invoice ); |
|
414 | + $email->object = $invoice; |
|
415 | 415 | |
416 | - if ( $invoice->needs_payment() ) { |
|
417 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
418 | - } |
|
416 | + if ( $invoice->needs_payment() ) { |
|
417 | + $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
418 | + } |
|
419 | 419 | |
420 | - } |
|
420 | + } |
|
421 | 421 | |
422 | - } |
|
422 | + } |
|
423 | 423 | |
424 | - } |
|
424 | + } |
|
425 | 425 | |
426 | - /** |
|
427 | - * Calculates the date query for an invoices query |
|
428 | - * |
|
429 | - * @param array $reminder_days |
|
430 | - * @return string |
|
431 | - */ |
|
432 | - public function get_date_query( $reminder_days ) { |
|
426 | + /** |
|
427 | + * Calculates the date query for an invoices query |
|
428 | + * |
|
429 | + * @param array $reminder_days |
|
430 | + * @return string |
|
431 | + */ |
|
432 | + public function get_date_query( $reminder_days ) { |
|
433 | 433 | |
434 | - $date_query = array( |
|
435 | - 'relation' => 'OR' |
|
436 | - ); |
|
434 | + $date_query = array( |
|
435 | + 'relation' => 'OR' |
|
436 | + ); |
|
437 | 437 | |
438 | - foreach ( $reminder_days as $days ) { |
|
439 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
438 | + foreach ( $reminder_days as $days ) { |
|
439 | + $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
440 | 440 | |
441 | - $date_query[] = array( |
|
442 | - 'year' => $date['year'], |
|
443 | - 'month' => $date['month'], |
|
444 | - 'day' => $date['day'], |
|
445 | - ); |
|
441 | + $date_query[] = array( |
|
442 | + 'year' => $date['year'], |
|
443 | + 'month' => $date['month'], |
|
444 | + 'day' => $date['day'], |
|
445 | + ); |
|
446 | 446 | |
447 | - } |
|
447 | + } |
|
448 | 448 | |
449 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
449 | + $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
450 | 450 | |
451 | - return $date_query->get_sql(); |
|
451 | + return $date_query->get_sql(); |
|
452 | 452 | |
453 | - } |
|
453 | + } |
|
454 | 454 | |
455 | 455 | } |
@@ -13,646 +13,646 @@ discard block |
||
13 | 13 | class GetPaid_Post_Types_Admin { |
14 | 14 | |
15 | 15 | /** |
16 | - * Hook in methods. |
|
17 | - */ |
|
18 | - public static function init() { |
|
19 | - |
|
20 | - // Init metaboxes. |
|
21 | - GetPaid_Metaboxes::init(); |
|
22 | - |
|
23 | - // Filter the post updated messages. |
|
24 | - add_filter( 'post_updated_messages', 'GetPaid_Post_Types_Admin::post_updated_messages' ); |
|
25 | - |
|
26 | - // Filter post actions. |
|
27 | - add_filter( 'post_row_actions', 'GetPaid_Post_Types_Admin::post_row_actions', 10, 2 ); |
|
28 | - add_filter( 'post_row_actions', 'GetPaid_Post_Types_Admin::filter_invoice_row_actions', 90, 2 ); |
|
29 | - |
|
30 | - // Invoice table columns. |
|
31 | - add_filter( 'manage_wpi_invoice_posts_columns', array( __CLASS__, 'invoice_columns' ), 100 ); |
|
32 | - add_action( 'manage_wpi_invoice_posts_custom_column', array( __CLASS__, 'display_invoice_columns' ), 10, 2 ); |
|
33 | - |
|
34 | - // Items table columns. |
|
35 | - add_filter( 'manage_wpi_item_posts_columns', array( __CLASS__, 'item_columns' ), 100 ); |
|
36 | - add_filter( 'manage_edit-wpi_item_sortable_columns', array( __CLASS__, 'sortable_item_columns' ), 20 ); |
|
37 | - add_action( 'manage_wpi_item_posts_custom_column', array( __CLASS__, 'display_item_columns' ), 10, 2 ); |
|
38 | - add_action( 'restrict_manage_posts', array( __CLASS__, 'add_item_filters' ), 100 ); |
|
39 | - add_action( 'parse_query', array( __CLASS__, 'filter_item_query' ), 100 ); |
|
40 | - add_action( 'request', array( __CLASS__, 'reorder_items' ), 100 ); |
|
41 | - |
|
42 | - // Payment forms columns. |
|
43 | - add_filter( 'manage_wpi_payment_form_posts_columns', array( __CLASS__, 'payment_form_columns' ), 100 ); |
|
44 | - add_action( 'manage_wpi_payment_form_posts_custom_column', array( __CLASS__, 'display_payment_form_columns' ), 10, 2 ); |
|
45 | - add_filter( 'display_post_states', array( __CLASS__, 'filter_payment_form_state' ), 10, 2 ); |
|
46 | - |
|
47 | - // Discount table columns. |
|
48 | - add_filter( 'manage_wpi_discount_posts_columns', array( __CLASS__, 'discount_columns' ), 100 ); |
|
49 | - add_filter( 'bulk_actions-edit-wpi_discount', '__return_empty_array', 100 ); |
|
50 | - |
|
51 | - // Deleting posts. |
|
52 | - add_action( 'delete_post', array( __CLASS__, 'delete_post' ) ); |
|
53 | - add_filter( 'display_post_states', array( __CLASS__, 'filter_discount_state' ), 10, 2 ); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Post updated messages. |
|
58 | - */ |
|
59 | - public static function post_updated_messages( $messages ) { |
|
60 | - global $post; |
|
61 | - |
|
62 | - $messages['wpi_discount'] = array( |
|
63 | - 0 => '', |
|
64 | - 1 => __( 'Discount updated.', 'invoicing' ), |
|
65 | - 2 => __( 'Custom field updated.', 'invoicing' ), |
|
66 | - 3 => __( 'Custom field deleted.', 'invoicing' ), |
|
67 | - 4 => __( 'Discount updated.', 'invoicing' ), |
|
68 | - 5 => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Discount restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
|
69 | - 6 => __( 'Discount updated.', 'invoicing' ), |
|
70 | - 7 => __( 'Discount saved.', 'invoicing' ), |
|
71 | - 8 => __( 'Discount submitted.', 'invoicing' ), |
|
72 | - 9 => wp_sprintf( __( 'Discount scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ), |
|
73 | - 10 => __( 'Discount draft updated.', 'invoicing' ), |
|
74 | - ); |
|
75 | - |
|
76 | - $messages['wpi_payment_form'] = array( |
|
77 | - 0 => '', |
|
78 | - 1 => __( 'Payment Form updated.', 'invoicing' ), |
|
79 | - 2 => __( 'Custom field updated.', 'invoicing' ), |
|
80 | - 3 => __( 'Custom field deleted.', 'invoicing' ), |
|
81 | - 4 => __( 'Payment Form updated.', 'invoicing' ), |
|
82 | - 5 => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Payment Form restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
|
83 | - 6 => __( 'Payment Form updated.', 'invoicing' ), |
|
84 | - 7 => __( 'Payment Form saved.', 'invoicing' ), |
|
85 | - 8 => __( 'Payment Form submitted.', 'invoicing' ), |
|
86 | - 9 => wp_sprintf( __( 'Payment Form scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ), |
|
87 | - 10 => __( 'Payment Form draft updated.', 'invoicing' ), |
|
88 | - ); |
|
89 | - |
|
90 | - return $messages; |
|
91 | - |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Post row actions. |
|
96 | - */ |
|
97 | - public static function post_row_actions( $actions, $post ) { |
|
98 | - |
|
99 | - $post = get_post( $post ); |
|
100 | - |
|
101 | - // We do not want to edit the default payment form. |
|
102 | - if ( 'wpi_payment_form' == $post->post_type && $post->ID == wpinv_get_default_payment_form() ) { |
|
103 | - unset( $actions['trash'] ); |
|
104 | - unset( $actions['inline hide-if-no-js'] ); |
|
105 | - } |
|
106 | - |
|
107 | - return $actions; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
16 | + * Hook in methods. |
|
17 | + */ |
|
18 | + public static function init() { |
|
19 | + |
|
20 | + // Init metaboxes. |
|
21 | + GetPaid_Metaboxes::init(); |
|
22 | + |
|
23 | + // Filter the post updated messages. |
|
24 | + add_filter( 'post_updated_messages', 'GetPaid_Post_Types_Admin::post_updated_messages' ); |
|
25 | + |
|
26 | + // Filter post actions. |
|
27 | + add_filter( 'post_row_actions', 'GetPaid_Post_Types_Admin::post_row_actions', 10, 2 ); |
|
28 | + add_filter( 'post_row_actions', 'GetPaid_Post_Types_Admin::filter_invoice_row_actions', 90, 2 ); |
|
29 | + |
|
30 | + // Invoice table columns. |
|
31 | + add_filter( 'manage_wpi_invoice_posts_columns', array( __CLASS__, 'invoice_columns' ), 100 ); |
|
32 | + add_action( 'manage_wpi_invoice_posts_custom_column', array( __CLASS__, 'display_invoice_columns' ), 10, 2 ); |
|
33 | + |
|
34 | + // Items table columns. |
|
35 | + add_filter( 'manage_wpi_item_posts_columns', array( __CLASS__, 'item_columns' ), 100 ); |
|
36 | + add_filter( 'manage_edit-wpi_item_sortable_columns', array( __CLASS__, 'sortable_item_columns' ), 20 ); |
|
37 | + add_action( 'manage_wpi_item_posts_custom_column', array( __CLASS__, 'display_item_columns' ), 10, 2 ); |
|
38 | + add_action( 'restrict_manage_posts', array( __CLASS__, 'add_item_filters' ), 100 ); |
|
39 | + add_action( 'parse_query', array( __CLASS__, 'filter_item_query' ), 100 ); |
|
40 | + add_action( 'request', array( __CLASS__, 'reorder_items' ), 100 ); |
|
41 | + |
|
42 | + // Payment forms columns. |
|
43 | + add_filter( 'manage_wpi_payment_form_posts_columns', array( __CLASS__, 'payment_form_columns' ), 100 ); |
|
44 | + add_action( 'manage_wpi_payment_form_posts_custom_column', array( __CLASS__, 'display_payment_form_columns' ), 10, 2 ); |
|
45 | + add_filter( 'display_post_states', array( __CLASS__, 'filter_payment_form_state' ), 10, 2 ); |
|
46 | + |
|
47 | + // Discount table columns. |
|
48 | + add_filter( 'manage_wpi_discount_posts_columns', array( __CLASS__, 'discount_columns' ), 100 ); |
|
49 | + add_filter( 'bulk_actions-edit-wpi_discount', '__return_empty_array', 100 ); |
|
50 | + |
|
51 | + // Deleting posts. |
|
52 | + add_action( 'delete_post', array( __CLASS__, 'delete_post' ) ); |
|
53 | + add_filter( 'display_post_states', array( __CLASS__, 'filter_discount_state' ), 10, 2 ); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Post updated messages. |
|
58 | + */ |
|
59 | + public static function post_updated_messages( $messages ) { |
|
60 | + global $post; |
|
61 | + |
|
62 | + $messages['wpi_discount'] = array( |
|
63 | + 0 => '', |
|
64 | + 1 => __( 'Discount updated.', 'invoicing' ), |
|
65 | + 2 => __( 'Custom field updated.', 'invoicing' ), |
|
66 | + 3 => __( 'Custom field deleted.', 'invoicing' ), |
|
67 | + 4 => __( 'Discount updated.', 'invoicing' ), |
|
68 | + 5 => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Discount restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
|
69 | + 6 => __( 'Discount updated.', 'invoicing' ), |
|
70 | + 7 => __( 'Discount saved.', 'invoicing' ), |
|
71 | + 8 => __( 'Discount submitted.', 'invoicing' ), |
|
72 | + 9 => wp_sprintf( __( 'Discount scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ), |
|
73 | + 10 => __( 'Discount draft updated.', 'invoicing' ), |
|
74 | + ); |
|
75 | + |
|
76 | + $messages['wpi_payment_form'] = array( |
|
77 | + 0 => '', |
|
78 | + 1 => __( 'Payment Form updated.', 'invoicing' ), |
|
79 | + 2 => __( 'Custom field updated.', 'invoicing' ), |
|
80 | + 3 => __( 'Custom field deleted.', 'invoicing' ), |
|
81 | + 4 => __( 'Payment Form updated.', 'invoicing' ), |
|
82 | + 5 => isset( $_GET['revision'] ) ? wp_sprintf( __( 'Payment Form restored to revision from %s', 'invoicing' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, |
|
83 | + 6 => __( 'Payment Form updated.', 'invoicing' ), |
|
84 | + 7 => __( 'Payment Form saved.', 'invoicing' ), |
|
85 | + 8 => __( 'Payment Form submitted.', 'invoicing' ), |
|
86 | + 9 => wp_sprintf( __( 'Payment Form scheduled for: <strong>%1$s</strong>.', 'invoicing' ), date_i18n( __( 'M j, Y @ G:i', 'invoicing' ), strtotime( $post->post_date ) ) ), |
|
87 | + 10 => __( 'Payment Form draft updated.', 'invoicing' ), |
|
88 | + ); |
|
89 | + |
|
90 | + return $messages; |
|
91 | + |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Post row actions. |
|
96 | + */ |
|
97 | + public static function post_row_actions( $actions, $post ) { |
|
98 | + |
|
99 | + $post = get_post( $post ); |
|
100 | + |
|
101 | + // We do not want to edit the default payment form. |
|
102 | + if ( 'wpi_payment_form' == $post->post_type && $post->ID == wpinv_get_default_payment_form() ) { |
|
103 | + unset( $actions['trash'] ); |
|
104 | + unset( $actions['inline hide-if-no-js'] ); |
|
105 | + } |
|
106 | + |
|
107 | + return $actions; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | 111 | * Remove bulk edit option from admin side quote listing |
112 | 112 | * |
113 | 113 | * @since 1.0.0 |
114 | 114 | * @param array $actions post actions |
115 | - * @param WP_Post $post |
|
115 | + * @param WP_Post $post |
|
116 | 116 | * @return array $actions actions without edit option |
117 | 117 | */ |
118 | 118 | public static function filter_invoice_row_actions( $actions, $post ) { |
119 | 119 | |
120 | 120 | if ( getpaid_is_invoice_post_type( $post->post_type ) ) { |
121 | 121 | |
122 | - $actions = array(); |
|
123 | - $invoice = new WPInv_Invoice( $post ); |
|
124 | - |
|
125 | - $actions['edit'] = sprintf( |
|
126 | - '<a href="%1$s">%2$s</a>', |
|
127 | - esc_url( get_edit_post_link( $invoice->get_id() ) ), |
|
128 | - esc_html( __( 'Edit', 'invoicing' ) ) |
|
129 | - ); |
|
130 | - |
|
131 | - if ( ! $invoice->is_draft() ) { |
|
132 | - |
|
133 | - $actions['view'] = sprintf( |
|
134 | - '<a href="%1$s">%2$s</a>', |
|
135 | - esc_url( $invoice->get_view_url() ), |
|
136 | - sprintf( |
|
137 | - esc_html( __( 'View %s', 'invoicing' ) ), |
|
138 | - getpaid_get_post_type_label( $invoice->get_post_type(), false ) |
|
139 | - ) |
|
140 | - ); |
|
141 | - |
|
142 | - $actions['send'] = sprintf( |
|
143 | - '<a href="%1$s">%2$s</a>', |
|
144 | - esc_url( |
|
145 | - wp_nonce_url( |
|
146 | - add_query_arg( |
|
147 | - array( |
|
148 | - 'getpaid-admin-action' => 'send_invoice', |
|
149 | - 'invoice_id' => $invoice->get_id() |
|
150 | - ) |
|
151 | - ), |
|
152 | - 'getpaid-nonce', |
|
153 | - 'getpaid-nonce' |
|
154 | - ) |
|
155 | - ), |
|
156 | - esc_html( __( 'Send to Customer', 'invoicing' ) ) |
|
157 | - ); |
|
158 | - |
|
159 | - } |
|
122 | + $actions = array(); |
|
123 | + $invoice = new WPInv_Invoice( $post ); |
|
124 | + |
|
125 | + $actions['edit'] = sprintf( |
|
126 | + '<a href="%1$s">%2$s</a>', |
|
127 | + esc_url( get_edit_post_link( $invoice->get_id() ) ), |
|
128 | + esc_html( __( 'Edit', 'invoicing' ) ) |
|
129 | + ); |
|
130 | + |
|
131 | + if ( ! $invoice->is_draft() ) { |
|
132 | + |
|
133 | + $actions['view'] = sprintf( |
|
134 | + '<a href="%1$s">%2$s</a>', |
|
135 | + esc_url( $invoice->get_view_url() ), |
|
136 | + sprintf( |
|
137 | + esc_html( __( 'View %s', 'invoicing' ) ), |
|
138 | + getpaid_get_post_type_label( $invoice->get_post_type(), false ) |
|
139 | + ) |
|
140 | + ); |
|
141 | + |
|
142 | + $actions['send'] = sprintf( |
|
143 | + '<a href="%1$s">%2$s</a>', |
|
144 | + esc_url( |
|
145 | + wp_nonce_url( |
|
146 | + add_query_arg( |
|
147 | + array( |
|
148 | + 'getpaid-admin-action' => 'send_invoice', |
|
149 | + 'invoice_id' => $invoice->get_id() |
|
150 | + ) |
|
151 | + ), |
|
152 | + 'getpaid-nonce', |
|
153 | + 'getpaid-nonce' |
|
154 | + ) |
|
155 | + ), |
|
156 | + esc_html( __( 'Send to Customer', 'invoicing' ) ) |
|
157 | + ); |
|
158 | + |
|
159 | + } |
|
160 | 160 | |
161 | 161 | } |
162 | 162 | |
163 | 163 | return $actions; |
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Returns an array of invoice table columns. |
|
168 | - */ |
|
169 | - public static function invoice_columns( $columns ) { |
|
170 | - |
|
171 | - $columns = array( |
|
172 | - 'cb' => $columns['cb'], |
|
173 | - 'number' => __( 'Invoice', 'invoicing' ), |
|
174 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
175 | - 'invoice_date' => __( 'Created', 'invoicing' ), |
|
176 | - 'payment_date' => __( 'Completed', 'invoicing' ), |
|
177 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
178 | - 'recurring' => __( 'Recurring', 'invoicing' ), |
|
179 | - 'status' => __( 'Status', 'invoicing' ), |
|
180 | - ); |
|
181 | - |
|
182 | - return apply_filters( 'wpi_invoice_table_columns', $columns ); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Displays invoice table columns. |
|
187 | - */ |
|
188 | - public static function display_invoice_columns( $column_name, $post_id ) { |
|
189 | - |
|
190 | - $invoice = new WPInv_Invoice( $post_id ); |
|
191 | - |
|
192 | - switch ( $column_name ) { |
|
193 | - |
|
194 | - case 'invoice_date' : |
|
195 | - $date_time = esc_attr( $invoice->get_created_date() ); |
|
196 | - $date = getpaid_format_date_value( $date_time, "—", true ); |
|
197 | - echo "<span title='$date_time'>$date</span>"; |
|
198 | - break; |
|
199 | - |
|
200 | - case 'payment_date' : |
|
201 | - |
|
202 | - if ( $invoice->is_paid() ) { |
|
203 | - $date_time = esc_attr( $invoice->get_completed_date() ); |
|
204 | - $date = getpaid_format_date_value( $date_time, "—", true ); |
|
205 | - echo "<span title='$date_time'>$date</span>"; |
|
206 | - } else { |
|
207 | - echo "—"; |
|
208 | - } |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Returns an array of invoice table columns. |
|
168 | + */ |
|
169 | + public static function invoice_columns( $columns ) { |
|
170 | + |
|
171 | + $columns = array( |
|
172 | + 'cb' => $columns['cb'], |
|
173 | + 'number' => __( 'Invoice', 'invoicing' ), |
|
174 | + 'customer' => __( 'Customer', 'invoicing' ), |
|
175 | + 'invoice_date' => __( 'Created', 'invoicing' ), |
|
176 | + 'payment_date' => __( 'Completed', 'invoicing' ), |
|
177 | + 'amount' => __( 'Amount', 'invoicing' ), |
|
178 | + 'recurring' => __( 'Recurring', 'invoicing' ), |
|
179 | + 'status' => __( 'Status', 'invoicing' ), |
|
180 | + ); |
|
181 | + |
|
182 | + return apply_filters( 'wpi_invoice_table_columns', $columns ); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Displays invoice table columns. |
|
187 | + */ |
|
188 | + public static function display_invoice_columns( $column_name, $post_id ) { |
|
189 | + |
|
190 | + $invoice = new WPInv_Invoice( $post_id ); |
|
191 | + |
|
192 | + switch ( $column_name ) { |
|
193 | + |
|
194 | + case 'invoice_date' : |
|
195 | + $date_time = esc_attr( $invoice->get_created_date() ); |
|
196 | + $date = getpaid_format_date_value( $date_time, "—", true ); |
|
197 | + echo "<span title='$date_time'>$date</span>"; |
|
198 | + break; |
|
199 | + |
|
200 | + case 'payment_date' : |
|
201 | + |
|
202 | + if ( $invoice->is_paid() ) { |
|
203 | + $date_time = esc_attr( $invoice->get_completed_date() ); |
|
204 | + $date = getpaid_format_date_value( $date_time, "—", true ); |
|
205 | + echo "<span title='$date_time'>$date</span>"; |
|
206 | + } else { |
|
207 | + echo "—"; |
|
208 | + } |
|
209 | 209 | |
210 | - break; |
|
210 | + break; |
|
211 | 211 | |
212 | - case 'amount' : |
|
212 | + case 'amount' : |
|
213 | 213 | |
214 | - $amount = $invoice->get_total(); |
|
215 | - $formated_amount = wpinv_price( $amount, $invoice->get_currency() ); |
|
214 | + $amount = $invoice->get_total(); |
|
215 | + $formated_amount = wpinv_price( $amount, $invoice->get_currency() ); |
|
216 | 216 | |
217 | - if ( $invoice->is_refunded() ) { |
|
218 | - $refunded_amount = wpinv_price( 0, $invoice->get_currency() ); |
|
219 | - echo "<del>$formated_amount</del> <ins>$refunded_amount</ins>"; |
|
220 | - } else { |
|
217 | + if ( $invoice->is_refunded() ) { |
|
218 | + $refunded_amount = wpinv_price( 0, $invoice->get_currency() ); |
|
219 | + echo "<del>$formated_amount</del> <ins>$refunded_amount</ins>"; |
|
220 | + } else { |
|
221 | 221 | |
222 | - $discount = $invoice->get_total_discount(); |
|
222 | + $discount = $invoice->get_total_discount(); |
|
223 | 223 | |
224 | - if ( ! empty( $discount ) ) { |
|
225 | - $new_amount = wpinv_price( $amount + $discount, $invoice->get_currency() ); |
|
226 | - echo "<del>$new_amount</del> <ins>$formated_amount</ins>"; |
|
227 | - } else { |
|
228 | - echo $formated_amount; |
|
229 | - } |
|
224 | + if ( ! empty( $discount ) ) { |
|
225 | + $new_amount = wpinv_price( $amount + $discount, $invoice->get_currency() ); |
|
226 | + echo "<del>$new_amount</del> <ins>$formated_amount</ins>"; |
|
227 | + } else { |
|
228 | + echo $formated_amount; |
|
229 | + } |
|
230 | 230 | |
231 | - } |
|
231 | + } |
|
232 | 232 | |
233 | - break; |
|
233 | + break; |
|
234 | 234 | |
235 | - case 'status' : |
|
236 | - $status = sanitize_text_field( $invoice->get_status() ); |
|
237 | - $status_label = sanitize_text_field( $invoice->get_status_nicename() ); |
|
235 | + case 'status' : |
|
236 | + $status = sanitize_text_field( $invoice->get_status() ); |
|
237 | + $status_label = sanitize_text_field( $invoice->get_status_nicename() ); |
|
238 | 238 | |
239 | - // If it is paid, show the gateway title. |
|
240 | - if ( $invoice->is_paid() ) { |
|
241 | - $gateway = sanitize_text_field( $invoice->get_gateway_title() ); |
|
242 | - $gateway = wp_sprintf( esc_attr__( 'Paid via %s', 'invoicing' ), $gateway ); |
|
239 | + // If it is paid, show the gateway title. |
|
240 | + if ( $invoice->is_paid() ) { |
|
241 | + $gateway = sanitize_text_field( $invoice->get_gateway_title() ); |
|
242 | + $gateway = wp_sprintf( esc_attr__( 'Paid via %s', 'invoicing' ), $gateway ); |
|
243 | 243 | |
244 | - echo "<mark class='wpi-help-tip getpaid-invoice-status $status' title='$gateway'><span>$status_label</span></mark>"; |
|
245 | - } else { |
|
246 | - echo "<mark class='getpaid-invoice-status $status'><span>$status_label</span></mark>"; |
|
247 | - } |
|
244 | + echo "<mark class='wpi-help-tip getpaid-invoice-status $status' title='$gateway'><span>$status_label</span></mark>"; |
|
245 | + } else { |
|
246 | + echo "<mark class='getpaid-invoice-status $status'><span>$status_label</span></mark>"; |
|
247 | + } |
|
248 | 248 | |
249 | - // If it is not paid, display the overdue and view status. |
|
250 | - if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
249 | + // If it is not paid, display the overdue and view status. |
|
250 | + if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
251 | 251 | |
252 | - // Invoice view status. |
|
253 | - if ( wpinv_is_invoice_viewed( $invoice->get_id() ) ) { |
|
254 | - echo ' <i class="fa fa-eye wpi-help-tip" title="'. esc_attr__( 'Viewed by Customer', 'invoicing' ).'"></i>'; |
|
255 | - } else { |
|
256 | - echo ' <i class="fa fa-eye-slash wpi-help-tip" title="'. esc_attr__( 'Not Viewed by Customer', 'invoicing' ).'"></i>'; |
|
257 | - } |
|
252 | + // Invoice view status. |
|
253 | + if ( wpinv_is_invoice_viewed( $invoice->get_id() ) ) { |
|
254 | + echo ' <i class="fa fa-eye wpi-help-tip" title="'. esc_attr__( 'Viewed by Customer', 'invoicing' ).'"></i>'; |
|
255 | + } else { |
|
256 | + echo ' <i class="fa fa-eye-slash wpi-help-tip" title="'. esc_attr__( 'Not Viewed by Customer', 'invoicing' ).'"></i>'; |
|
257 | + } |
|
258 | 258 | |
259 | - // Display the overview status. |
|
260 | - if ( wpinv_get_option( 'overdue_active' ) ) { |
|
261 | - $due_date = $invoice->get_due_date(); |
|
262 | - $fomatted = getpaid_format_date( $due_date ); |
|
259 | + // Display the overview status. |
|
260 | + if ( wpinv_get_option( 'overdue_active' ) ) { |
|
261 | + $due_date = $invoice->get_due_date(); |
|
262 | + $fomatted = getpaid_format_date( $due_date ); |
|
263 | 263 | |
264 | - if ( ! empty( $fomatted ) ) { |
|
265 | - $date = wp_sprintf( __( 'Due %s', 'invoicing' ), $fomatted ); |
|
266 | - echo "<p class='description' style='color: #888;' title='$due_date'>$fomatted</p>"; |
|
267 | - } |
|
268 | - } |
|
264 | + if ( ! empty( $fomatted ) ) { |
|
265 | + $date = wp_sprintf( __( 'Due %s', 'invoicing' ), $fomatted ); |
|
266 | + echo "<p class='description' style='color: #888;' title='$due_date'>$fomatted</p>"; |
|
267 | + } |
|
268 | + } |
|
269 | 269 | |
270 | - } |
|
270 | + } |
|
271 | 271 | |
272 | - break; |
|
272 | + break; |
|
273 | 273 | |
274 | - case 'recurring': |
|
274 | + case 'recurring': |
|
275 | 275 | |
276 | - if ( $invoice->is_recurring() ) { |
|
277 | - echo '<i class="fa fa-check" style="color:#43850a;"></i>'; |
|
278 | - } else { |
|
279 | - echo '<i class="fa fa-times" style="color:#616161;"></i>'; |
|
280 | - } |
|
281 | - break; |
|
276 | + if ( $invoice->is_recurring() ) { |
|
277 | + echo '<i class="fa fa-check" style="color:#43850a;"></i>'; |
|
278 | + } else { |
|
279 | + echo '<i class="fa fa-times" style="color:#616161;"></i>'; |
|
280 | + } |
|
281 | + break; |
|
282 | 282 | |
283 | - case 'number' : |
|
283 | + case 'number' : |
|
284 | 284 | |
285 | - $edit_link = esc_url( get_edit_post_link( $invoice->get_id() ) ); |
|
286 | - $invoice_number = sanitize_text_field( $invoice->get_number() ); |
|
287 | - $invoice_details = esc_attr__( 'View Invoice Details', 'invoicing' ); |
|
285 | + $edit_link = esc_url( get_edit_post_link( $invoice->get_id() ) ); |
|
286 | + $invoice_number = sanitize_text_field( $invoice->get_number() ); |
|
287 | + $invoice_details = esc_attr__( 'View Invoice Details', 'invoicing' ); |
|
288 | 288 | |
289 | - echo "<a href='$edit_link' title='$invoice_details'><strong>$invoice_number</strong></a>"; |
|
289 | + echo "<a href='$edit_link' title='$invoice_details'><strong>$invoice_number</strong></a>"; |
|
290 | 290 | |
291 | - break; |
|
291 | + break; |
|
292 | 292 | |
293 | - case 'customer' : |
|
293 | + case 'customer' : |
|
294 | 294 | |
295 | - $customer_name = $invoice->get_user_full_name(); |
|
295 | + $customer_name = $invoice->get_user_full_name(); |
|
296 | 296 | |
297 | - if ( empty( $customer_name ) ) { |
|
298 | - $customer_name = $invoice->get_email(); |
|
299 | - } |
|
297 | + if ( empty( $customer_name ) ) { |
|
298 | + $customer_name = $invoice->get_email(); |
|
299 | + } |
|
300 | 300 | |
301 | - if ( ! empty( $customer_name ) ) { |
|
302 | - $customer_details = esc_attr__( 'View Customer Details', 'invoicing' ); |
|
303 | - $view_link = esc_url( add_query_arg( 'user_id', $invoice->get_user_id(), admin_url( 'user-edit.php' ) ) ); |
|
304 | - echo "<a href='$view_link' title='$customer_details'><span>$customer_name</span></a>"; |
|
305 | - } else { |
|
306 | - echo '<div>—</div>'; |
|
307 | - } |
|
301 | + if ( ! empty( $customer_name ) ) { |
|
302 | + $customer_details = esc_attr__( 'View Customer Details', 'invoicing' ); |
|
303 | + $view_link = esc_url( add_query_arg( 'user_id', $invoice->get_user_id(), admin_url( 'user-edit.php' ) ) ); |
|
304 | + echo "<a href='$view_link' title='$customer_details'><span>$customer_name</span></a>"; |
|
305 | + } else { |
|
306 | + echo '<div>—</div>'; |
|
307 | + } |
|
308 | 308 | |
309 | - break; |
|
309 | + break; |
|
310 | 310 | |
311 | - } |
|
311 | + } |
|
312 | 312 | |
313 | - } |
|
313 | + } |
|
314 | 314 | |
315 | - /** |
|
316 | - * Returns an array of payment forms table columns. |
|
317 | - */ |
|
318 | - public static function payment_form_columns( $columns ) { |
|
315 | + /** |
|
316 | + * Returns an array of payment forms table columns. |
|
317 | + */ |
|
318 | + public static function payment_form_columns( $columns ) { |
|
319 | 319 | |
320 | - $columns = array( |
|
321 | - 'cb' => $columns['cb'], |
|
322 | - 'title' => __( 'Name', 'invoicing' ), |
|
323 | - 'shortcode' => __( 'Shortcode', 'invoicing' ), |
|
324 | - 'earnings' => __( 'Revenue', 'invoicing' ), |
|
325 | - 'refunds' => __( 'Refunded', 'invoicing' ), |
|
326 | - 'items' => __( 'Items', 'invoicing' ), |
|
327 | - 'date' => __( 'Date', 'invoicing' ), |
|
328 | - ); |
|
320 | + $columns = array( |
|
321 | + 'cb' => $columns['cb'], |
|
322 | + 'title' => __( 'Name', 'invoicing' ), |
|
323 | + 'shortcode' => __( 'Shortcode', 'invoicing' ), |
|
324 | + 'earnings' => __( 'Revenue', 'invoicing' ), |
|
325 | + 'refunds' => __( 'Refunded', 'invoicing' ), |
|
326 | + 'items' => __( 'Items', 'invoicing' ), |
|
327 | + 'date' => __( 'Date', 'invoicing' ), |
|
328 | + ); |
|
329 | 329 | |
330 | - return apply_filters( 'wpi_payment_form_table_columns', $columns ); |
|
330 | + return apply_filters( 'wpi_payment_form_table_columns', $columns ); |
|
331 | 331 | |
332 | - } |
|
332 | + } |
|
333 | 333 | |
334 | - /** |
|
335 | - * Displays payment form table columns. |
|
336 | - */ |
|
337 | - public static function display_payment_form_columns( $column_name, $post_id ) { |
|
334 | + /** |
|
335 | + * Displays payment form table columns. |
|
336 | + */ |
|
337 | + public static function display_payment_form_columns( $column_name, $post_id ) { |
|
338 | 338 | |
339 | - // Retrieve the payment form. |
|
340 | - $form = new GetPaid_Payment_Form( $post_id ); |
|
339 | + // Retrieve the payment form. |
|
340 | + $form = new GetPaid_Payment_Form( $post_id ); |
|
341 | 341 | |
342 | - switch ( $column_name ) { |
|
342 | + switch ( $column_name ) { |
|
343 | 343 | |
344 | - case 'earnings' : |
|
345 | - echo wpinv_price( $form->get_earned() ); |
|
346 | - break; |
|
344 | + case 'earnings' : |
|
345 | + echo wpinv_price( $form->get_earned() ); |
|
346 | + break; |
|
347 | 347 | |
348 | - case 'refunds' : |
|
349 | - echo wpinv_price( $form->get_refunded() ); |
|
350 | - break; |
|
348 | + case 'refunds' : |
|
349 | + echo wpinv_price( $form->get_refunded() ); |
|
350 | + break; |
|
351 | 351 | |
352 | - case 'refunds' : |
|
353 | - echo wpinv_price( $form->get_refunded() ); |
|
354 | - break; |
|
352 | + case 'refunds' : |
|
353 | + echo wpinv_price( $form->get_refunded() ); |
|
354 | + break; |
|
355 | 355 | |
356 | - case 'shortcode' : |
|
356 | + case 'shortcode' : |
|
357 | 357 | |
358 | - if ( $form->is_default() ) { |
|
359 | - echo '—'; |
|
360 | - } else { |
|
361 | - echo '<input onClick="this.select()" type="text" value="[getpaid form=' . esc_attr( $form->get_id() ) . ']" style="width: 100%;" readonly/>'; |
|
362 | - } |
|
358 | + if ( $form->is_default() ) { |
|
359 | + echo '—'; |
|
360 | + } else { |
|
361 | + echo '<input onClick="this.select()" type="text" value="[getpaid form=' . esc_attr( $form->get_id() ) . ']" style="width: 100%;" readonly/>'; |
|
362 | + } |
|
363 | 363 | |
364 | - break; |
|
364 | + break; |
|
365 | 365 | |
366 | - case 'items' : |
|
366 | + case 'items' : |
|
367 | 367 | |
368 | - $items = $form->get_items(); |
|
368 | + $items = $form->get_items(); |
|
369 | 369 | |
370 | - if ( $form->is_default() || empty( $items ) ) { |
|
371 | - echo '—'; |
|
372 | - return; |
|
373 | - } |
|
370 | + if ( $form->is_default() || empty( $items ) ) { |
|
371 | + echo '—'; |
|
372 | + return; |
|
373 | + } |
|
374 | 374 | |
375 | - $_items = array(); |
|
375 | + $_items = array(); |
|
376 | 376 | |
377 | - foreach ( $items as $item ) { |
|
378 | - $url = $item->get_edit_url(); |
|
377 | + foreach ( $items as $item ) { |
|
378 | + $url = $item->get_edit_url(); |
|
379 | 379 | |
380 | - if ( empty( $url ) ) { |
|
381 | - $_items[] = sanitize_text_field( $item->get_name() ); |
|
382 | - } else { |
|
383 | - $_items[] = sprintf( |
|
384 | - '<a href="%s">%s</a>', |
|
385 | - esc_url( $url ), |
|
386 | - sanitize_text_field( $item->get_name() ) |
|
387 | - ); |
|
388 | - } |
|
380 | + if ( empty( $url ) ) { |
|
381 | + $_items[] = sanitize_text_field( $item->get_name() ); |
|
382 | + } else { |
|
383 | + $_items[] = sprintf( |
|
384 | + '<a href="%s">%s</a>', |
|
385 | + esc_url( $url ), |
|
386 | + sanitize_text_field( $item->get_name() ) |
|
387 | + ); |
|
388 | + } |
|
389 | 389 | |
390 | - } |
|
390 | + } |
|
391 | 391 | |
392 | - echo implode( '<br>', $_items ); |
|
392 | + echo implode( '<br>', $_items ); |
|
393 | 393 | |
394 | - break; |
|
394 | + break; |
|
395 | 395 | |
396 | - } |
|
396 | + } |
|
397 | 397 | |
398 | - } |
|
398 | + } |
|
399 | 399 | |
400 | - /** |
|
401 | - * Filters post states. |
|
402 | - */ |
|
403 | - public static function filter_payment_form_state( $post_states, $post ) { |
|
400 | + /** |
|
401 | + * Filters post states. |
|
402 | + */ |
|
403 | + public static function filter_payment_form_state( $post_states, $post ) { |
|
404 | 404 | |
405 | - if ( 'wpi_payment_form' == $post->post_type && wpinv_get_default_payment_form() == $post->ID ) { |
|
406 | - $post_states[ 'default_form' ] = __( 'Default Payment Form', 'invoicing' ); |
|
407 | - } |
|
405 | + if ( 'wpi_payment_form' == $post->post_type && wpinv_get_default_payment_form() == $post->ID ) { |
|
406 | + $post_states[ 'default_form' ] = __( 'Default Payment Form', 'invoicing' ); |
|
407 | + } |
|
408 | 408 | |
409 | - return $post_states; |
|
410 | - |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Returns an array of coupon table columns. |
|
415 | - */ |
|
416 | - public static function discount_columns( $columns ) { |
|
417 | - |
|
418 | - $columns = array( |
|
419 | - 'cb' => $columns['cb'], |
|
420 | - 'title' => __( 'Name', 'invoicing' ), |
|
421 | - 'code' => __( 'Code', 'invoicing' ), |
|
422 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
423 | - 'usage' => __( 'Usage / Limit', 'invoicing' ), |
|
424 | - 'start_date' => __( 'Start Date', 'invoicing' ), |
|
425 | - 'expiry_date' => __( 'Expiry Date', 'invoicing' ), |
|
426 | - ); |
|
427 | - |
|
428 | - return apply_filters( 'wpi_discount_table_columns', $columns ); |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Filters post states. |
|
433 | - */ |
|
434 | - public static function filter_discount_state( $post_states, $post ) { |
|
435 | - |
|
436 | - if ( 'wpi_discount' == $post->post_type ) { |
|
437 | - |
|
438 | - $discount = new WPInv_Discount( $post ); |
|
439 | - |
|
440 | - $status = $discount->is_expired() ? 'expired' : $discount->get_status(); |
|
441 | - |
|
442 | - if ( $status != 'publish' ) { |
|
443 | - return array( |
|
444 | - 'discount_status' => wpinv_discount_status( $status ), |
|
445 | - ); |
|
446 | - } |
|
447 | - |
|
448 | - return array(); |
|
449 | - |
|
450 | - } |
|
451 | - |
|
452 | - return $post_states; |
|
453 | - |
|
454 | - } |
|
455 | - |
|
456 | - /** |
|
457 | - * Returns an array of items table columns. |
|
458 | - */ |
|
459 | - public static function item_columns( $columns ) { |
|
460 | - |
|
461 | - $columns = array( |
|
462 | - 'cb' => $columns['cb'], |
|
463 | - 'title' => __( 'Name', 'invoicing' ), |
|
464 | - 'price' => __( 'Price', 'invoicing' ), |
|
465 | - 'vat_rule' => __( 'VAT rule', 'invoicing' ), |
|
466 | - 'vat_class' => __( 'VAT class', 'invoicing' ), |
|
467 | - 'type' => __( 'Type', 'invoicing' ), |
|
468 | - 'shortcode' => __( 'Shortcode', 'invoicing' ), |
|
469 | - ); |
|
470 | - |
|
471 | - if ( ! wpinv_use_taxes() ) { |
|
472 | - unset( $columns['vat_rule'] ); |
|
473 | - unset( $columns['vat_class'] ); |
|
474 | - } |
|
475 | - |
|
476 | - return apply_filters( 'wpi_item_table_columns', $columns ); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Returns an array of sortable items table columns. |
|
481 | - */ |
|
482 | - public static function sortable_item_columns( $columns ) { |
|
483 | - |
|
484 | - return array_merge( |
|
485 | - $columns, |
|
486 | - array( |
|
487 | - 'price' => 'price', |
|
488 | - 'vat_rule' => 'vat_rule', |
|
489 | - 'vat_class' => 'vat_class', |
|
490 | - 'type' => 'type', |
|
491 | - ) |
|
492 | - ); |
|
493 | - |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Displays items table columns. |
|
498 | - */ |
|
499 | - public static function display_item_columns( $column_name, $post_id ) { |
|
409 | + return $post_states; |
|
410 | + |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Returns an array of coupon table columns. |
|
415 | + */ |
|
416 | + public static function discount_columns( $columns ) { |
|
417 | + |
|
418 | + $columns = array( |
|
419 | + 'cb' => $columns['cb'], |
|
420 | + 'title' => __( 'Name', 'invoicing' ), |
|
421 | + 'code' => __( 'Code', 'invoicing' ), |
|
422 | + 'amount' => __( 'Amount', 'invoicing' ), |
|
423 | + 'usage' => __( 'Usage / Limit', 'invoicing' ), |
|
424 | + 'start_date' => __( 'Start Date', 'invoicing' ), |
|
425 | + 'expiry_date' => __( 'Expiry Date', 'invoicing' ), |
|
426 | + ); |
|
427 | + |
|
428 | + return apply_filters( 'wpi_discount_table_columns', $columns ); |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Filters post states. |
|
433 | + */ |
|
434 | + public static function filter_discount_state( $post_states, $post ) { |
|
435 | + |
|
436 | + if ( 'wpi_discount' == $post->post_type ) { |
|
437 | + |
|
438 | + $discount = new WPInv_Discount( $post ); |
|
439 | + |
|
440 | + $status = $discount->is_expired() ? 'expired' : $discount->get_status(); |
|
441 | + |
|
442 | + if ( $status != 'publish' ) { |
|
443 | + return array( |
|
444 | + 'discount_status' => wpinv_discount_status( $status ), |
|
445 | + ); |
|
446 | + } |
|
447 | + |
|
448 | + return array(); |
|
449 | + |
|
450 | + } |
|
451 | + |
|
452 | + return $post_states; |
|
453 | + |
|
454 | + } |
|
455 | + |
|
456 | + /** |
|
457 | + * Returns an array of items table columns. |
|
458 | + */ |
|
459 | + public static function item_columns( $columns ) { |
|
460 | + |
|
461 | + $columns = array( |
|
462 | + 'cb' => $columns['cb'], |
|
463 | + 'title' => __( 'Name', 'invoicing' ), |
|
464 | + 'price' => __( 'Price', 'invoicing' ), |
|
465 | + 'vat_rule' => __( 'VAT rule', 'invoicing' ), |
|
466 | + 'vat_class' => __( 'VAT class', 'invoicing' ), |
|
467 | + 'type' => __( 'Type', 'invoicing' ), |
|
468 | + 'shortcode' => __( 'Shortcode', 'invoicing' ), |
|
469 | + ); |
|
470 | + |
|
471 | + if ( ! wpinv_use_taxes() ) { |
|
472 | + unset( $columns['vat_rule'] ); |
|
473 | + unset( $columns['vat_class'] ); |
|
474 | + } |
|
475 | + |
|
476 | + return apply_filters( 'wpi_item_table_columns', $columns ); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Returns an array of sortable items table columns. |
|
481 | + */ |
|
482 | + public static function sortable_item_columns( $columns ) { |
|
483 | + |
|
484 | + return array_merge( |
|
485 | + $columns, |
|
486 | + array( |
|
487 | + 'price' => 'price', |
|
488 | + 'vat_rule' => 'vat_rule', |
|
489 | + 'vat_class' => 'vat_class', |
|
490 | + 'type' => 'type', |
|
491 | + ) |
|
492 | + ); |
|
493 | + |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Displays items table columns. |
|
498 | + */ |
|
499 | + public static function display_item_columns( $column_name, $post_id ) { |
|
500 | 500 | |
501 | - $item = new WPInv_Item( $post_id ); |
|
501 | + $item = new WPInv_Item( $post_id ); |
|
502 | 502 | |
503 | - switch ( $column_name ) { |
|
503 | + switch ( $column_name ) { |
|
504 | 504 | |
505 | - case 'price' : |
|
505 | + case 'price' : |
|
506 | 506 | |
507 | - if ( ! $item->is_recurring() ) { |
|
508 | - echo $item->get_the_price(); |
|
509 | - break; |
|
510 | - } |
|
507 | + if ( ! $item->is_recurring() ) { |
|
508 | + echo $item->get_the_price(); |
|
509 | + break; |
|
510 | + } |
|
511 | 511 | |
512 | - $price = wp_sprintf( |
|
513 | - __( '%s / %s', 'invoicing' ), |
|
514 | - $item->get_the_price(), |
|
515 | - getpaid_get_subscription_period_label( $item->get_recurring_period(), $item->get_recurring_interval(), '' ) |
|
516 | - ); |
|
512 | + $price = wp_sprintf( |
|
513 | + __( '%s / %s', 'invoicing' ), |
|
514 | + $item->get_the_price(), |
|
515 | + getpaid_get_subscription_period_label( $item->get_recurring_period(), $item->get_recurring_interval(), '' ) |
|
516 | + ); |
|
517 | 517 | |
518 | - if ( $item->get_the_price() == $item->get_the_initial_price() ) { |
|
519 | - echo $price; |
|
520 | - break; |
|
521 | - } |
|
518 | + if ( $item->get_the_price() == $item->get_the_initial_price() ) { |
|
519 | + echo $price; |
|
520 | + break; |
|
521 | + } |
|
522 | 522 | |
523 | - echo $item->get_the_initial_price(); |
|
523 | + echo $item->get_the_initial_price(); |
|
524 | 524 | |
525 | - echo '<span class="meta">' . wp_sprintf( __( 'then %s', 'invoicing' ), $price ) .'</span>'; |
|
526 | - break; |
|
525 | + echo '<span class="meta">' . wp_sprintf( __( 'then %s', 'invoicing' ), $price ) .'</span>'; |
|
526 | + break; |
|
527 | 527 | |
528 | - case 'vat_rule' : |
|
529 | - echo getpaid_get_tax_rule_label( $item->get_vat_rule() ); |
|
530 | - break; |
|
528 | + case 'vat_rule' : |
|
529 | + echo getpaid_get_tax_rule_label( $item->get_vat_rule() ); |
|
530 | + break; |
|
531 | 531 | |
532 | - case 'vat_class' : |
|
533 | - echo getpaid_get_tax_class_label( $item->get_vat_class() ); |
|
534 | - break; |
|
532 | + case 'vat_class' : |
|
533 | + echo getpaid_get_tax_class_label( $item->get_vat_class() ); |
|
534 | + break; |
|
535 | 535 | |
536 | - case 'shortcode' : |
|
537 | - echo '<input onClick="this.select()" type="text" value="[getpaid item=' . esc_attr( $item->get_id() ) . ' button=\'Buy Now\']" style="width: 100%;" readonly/>'; |
|
538 | - break; |
|
536 | + case 'shortcode' : |
|
537 | + echo '<input onClick="this.select()" type="text" value="[getpaid item=' . esc_attr( $item->get_id() ) . ' button=\'Buy Now\']" style="width: 100%;" readonly/>'; |
|
538 | + break; |
|
539 | 539 | |
540 | - case 'type' : |
|
541 | - echo wpinv_item_type( $item->get_id() ) . '<span class="meta">' . $item->get_custom_singular_name() . '</span>'; |
|
542 | - break; |
|
540 | + case 'type' : |
|
541 | + echo wpinv_item_type( $item->get_id() ) . '<span class="meta">' . $item->get_custom_singular_name() . '</span>'; |
|
542 | + break; |
|
543 | 543 | |
544 | - } |
|
544 | + } |
|
545 | 545 | |
546 | - } |
|
546 | + } |
|
547 | 547 | |
548 | - /** |
|
549 | - * Lets users filter items using taxes. |
|
550 | - */ |
|
551 | - public static function add_item_filters( $post_type ) { |
|
548 | + /** |
|
549 | + * Lets users filter items using taxes. |
|
550 | + */ |
|
551 | + public static function add_item_filters( $post_type ) { |
|
552 | 552 | |
553 | - // Abort if we're not dealing with items. |
|
554 | - if ( $post_type != 'wpi_item' ) { |
|
555 | - return; |
|
556 | - } |
|
553 | + // Abort if we're not dealing with items. |
|
554 | + if ( $post_type != 'wpi_item' ) { |
|
555 | + return; |
|
556 | + } |
|
557 | 557 | |
558 | - // Filter by vat rules. |
|
559 | - if ( wpinv_use_taxes() ) { |
|
558 | + // Filter by vat rules. |
|
559 | + if ( wpinv_use_taxes() ) { |
|
560 | 560 | |
561 | - // Sanitize selected vat rule. |
|
562 | - $vat_rule = ''; |
|
563 | - $vat_rules = getpaid_get_tax_rules(); |
|
564 | - if ( isset( $_GET['vat_rule'] ) ) { |
|
565 | - $vat_rule = $_GET['vat_rule']; |
|
566 | - } |
|
567 | - |
|
568 | - // Filter by VAT rule. |
|
569 | - echo wpinv_html_select( |
|
570 | - array( |
|
571 | - 'options' => array_merge( |
|
572 | - array( |
|
573 | - '' => __( 'All VAT rules', 'invoicing' ) |
|
574 | - ), |
|
575 | - $vat_rules |
|
576 | - ), |
|
577 | - 'name' => 'vat_rule', |
|
578 | - 'id' => 'vat_rule', |
|
579 | - 'selected' => in_array( $vat_rule, array_keys( $vat_rules ) ) ? $vat_rule : '', |
|
580 | - 'show_option_all' => false, |
|
581 | - 'show_option_none' => false, |
|
582 | - ) |
|
583 | - ); |
|
584 | - |
|
585 | - // Filter by VAT class. |
|
561 | + // Sanitize selected vat rule. |
|
562 | + $vat_rule = ''; |
|
563 | + $vat_rules = getpaid_get_tax_rules(); |
|
564 | + if ( isset( $_GET['vat_rule'] ) ) { |
|
565 | + $vat_rule = $_GET['vat_rule']; |
|
566 | + } |
|
567 | + |
|
568 | + // Filter by VAT rule. |
|
569 | + echo wpinv_html_select( |
|
570 | + array( |
|
571 | + 'options' => array_merge( |
|
572 | + array( |
|
573 | + '' => __( 'All VAT rules', 'invoicing' ) |
|
574 | + ), |
|
575 | + $vat_rules |
|
576 | + ), |
|
577 | + 'name' => 'vat_rule', |
|
578 | + 'id' => 'vat_rule', |
|
579 | + 'selected' => in_array( $vat_rule, array_keys( $vat_rules ) ) ? $vat_rule : '', |
|
580 | + 'show_option_all' => false, |
|
581 | + 'show_option_none' => false, |
|
582 | + ) |
|
583 | + ); |
|
584 | + |
|
585 | + // Filter by VAT class. |
|
586 | 586 | |
587 | - // Sanitize selected vat rule. |
|
588 | - $vat_class = ''; |
|
589 | - $vat_classes = getpaid_get_tax_classes(); |
|
590 | - if ( isset( $_GET['vat_class'] ) ) { |
|
591 | - $vat_class = $_GET['vat_class']; |
|
592 | - } |
|
593 | - |
|
594 | - echo wpinv_html_select( |
|
595 | - array( |
|
596 | - 'options' => array_merge( |
|
597 | - array( |
|
598 | - '' => __( 'All VAT classes', 'invoicing' ) |
|
599 | - ), |
|
600 | - $vat_classes |
|
601 | - ), |
|
602 | - 'name' => 'vat_class', |
|
603 | - 'id' => 'vat_class', |
|
604 | - 'selected' => in_array( $vat_class, array_keys( $vat_classes ) ) ? $vat_class : '', |
|
605 | - 'show_option_all' => false, |
|
606 | - 'show_option_none' => false, |
|
607 | - ) |
|
608 | - ); |
|
609 | - |
|
610 | - } |
|
611 | - |
|
612 | - // Filter by item type. |
|
613 | - $type = ''; |
|
614 | - if ( isset( $_GET['type'] ) ) { |
|
615 | - $type = $_GET['type']; |
|
616 | - } |
|
617 | - |
|
618 | - echo wpinv_html_select( |
|
619 | - array( |
|
620 | - 'options' => array_merge( |
|
621 | - array( |
|
622 | - '' => __( 'All item types', 'invoicing' ) |
|
623 | - ), |
|
624 | - wpinv_get_item_types() |
|
625 | - ), |
|
626 | - 'name' => 'type', |
|
627 | - 'id' => 'type', |
|
628 | - 'selected' => in_array( $type, wpinv_item_types() ) ? $type : '', |
|
629 | - 'show_option_all' => false, |
|
630 | - 'show_option_none' => false, |
|
631 | - ) |
|
632 | - ); |
|
633 | - |
|
634 | - } |
|
635 | - |
|
636 | - /** |
|
637 | - * Filters the item query. |
|
638 | - */ |
|
639 | - public static function filter_item_query( $query ) { |
|
640 | - |
|
641 | - // modify the query only if it admin and main query. |
|
642 | - if ( ! ( is_admin() && $query->is_main_query() ) ){ |
|
643 | - return $query; |
|
644 | - } |
|
645 | - |
|
646 | - // we want to modify the query for our items. |
|
647 | - if ( empty( $query->query['post_type'] ) || 'wpi_item' != $query->query['post_type'] ){ |
|
648 | - return $query; |
|
649 | - } |
|
650 | - |
|
651 | - if ( empty( $query->query_vars['meta_query'] ) ) { |
|
652 | - $query->query_vars['meta_query'] = array(); |
|
653 | - } |
|
654 | - |
|
655 | - // Filter vat rule type |
|
587 | + // Sanitize selected vat rule. |
|
588 | + $vat_class = ''; |
|
589 | + $vat_classes = getpaid_get_tax_classes(); |
|
590 | + if ( isset( $_GET['vat_class'] ) ) { |
|
591 | + $vat_class = $_GET['vat_class']; |
|
592 | + } |
|
593 | + |
|
594 | + echo wpinv_html_select( |
|
595 | + array( |
|
596 | + 'options' => array_merge( |
|
597 | + array( |
|
598 | + '' => __( 'All VAT classes', 'invoicing' ) |
|
599 | + ), |
|
600 | + $vat_classes |
|
601 | + ), |
|
602 | + 'name' => 'vat_class', |
|
603 | + 'id' => 'vat_class', |
|
604 | + 'selected' => in_array( $vat_class, array_keys( $vat_classes ) ) ? $vat_class : '', |
|
605 | + 'show_option_all' => false, |
|
606 | + 'show_option_none' => false, |
|
607 | + ) |
|
608 | + ); |
|
609 | + |
|
610 | + } |
|
611 | + |
|
612 | + // Filter by item type. |
|
613 | + $type = ''; |
|
614 | + if ( isset( $_GET['type'] ) ) { |
|
615 | + $type = $_GET['type']; |
|
616 | + } |
|
617 | + |
|
618 | + echo wpinv_html_select( |
|
619 | + array( |
|
620 | + 'options' => array_merge( |
|
621 | + array( |
|
622 | + '' => __( 'All item types', 'invoicing' ) |
|
623 | + ), |
|
624 | + wpinv_get_item_types() |
|
625 | + ), |
|
626 | + 'name' => 'type', |
|
627 | + 'id' => 'type', |
|
628 | + 'selected' => in_array( $type, wpinv_item_types() ) ? $type : '', |
|
629 | + 'show_option_all' => false, |
|
630 | + 'show_option_none' => false, |
|
631 | + ) |
|
632 | + ); |
|
633 | + |
|
634 | + } |
|
635 | + |
|
636 | + /** |
|
637 | + * Filters the item query. |
|
638 | + */ |
|
639 | + public static function filter_item_query( $query ) { |
|
640 | + |
|
641 | + // modify the query only if it admin and main query. |
|
642 | + if ( ! ( is_admin() && $query->is_main_query() ) ){ |
|
643 | + return $query; |
|
644 | + } |
|
645 | + |
|
646 | + // we want to modify the query for our items. |
|
647 | + if ( empty( $query->query['post_type'] ) || 'wpi_item' != $query->query['post_type'] ){ |
|
648 | + return $query; |
|
649 | + } |
|
650 | + |
|
651 | + if ( empty( $query->query_vars['meta_query'] ) ) { |
|
652 | + $query->query_vars['meta_query'] = array(); |
|
653 | + } |
|
654 | + |
|
655 | + // Filter vat rule type |
|
656 | 656 | if ( ! empty( $_GET['vat_rule'] ) ) { |
657 | 657 | $query->query_vars['meta_query'][] = array( |
658 | 658 | 'key' => '_wpinv_vat_rule', |
@@ -677,94 +677,94 @@ discard block |
||
677 | 677 | 'value' => sanitize_text_field( $_GET['type'] ), |
678 | 678 | 'compare' => '=' |
679 | 679 | ); |
680 | - } |
|
681 | - |
|
682 | - } |
|
683 | - |
|
684 | - /** |
|
685 | - * Reorders items. |
|
686 | - */ |
|
687 | - public static function reorder_items( $vars ) { |
|
688 | - global $typenow; |
|
689 | - |
|
690 | - if ( 'wpi_item' !== $typenow || empty( $vars['orderby'] ) ) { |
|
691 | - return $vars; |
|
692 | - } |
|
693 | - |
|
694 | - // By item type. |
|
695 | - if ( 'type' == $vars['orderby'] ) { |
|
696 | - return array_merge( |
|
697 | - $vars, |
|
698 | - array( |
|
699 | - 'meta_key' => '_wpinv_type', |
|
700 | - 'orderby' => 'meta_value' |
|
701 | - ) |
|
702 | - ); |
|
703 | - } |
|
704 | - |
|
705 | - // By vat class. |
|
706 | - if ( 'vat_class' == $vars['orderby'] ) { |
|
707 | - return array_merge( |
|
708 | - $vars, |
|
709 | - array( |
|
710 | - 'meta_key' => '_wpinv_vat_class', |
|
711 | - 'orderby' => 'meta_value' |
|
712 | - ) |
|
713 | - ); |
|
714 | - } |
|
715 | - |
|
716 | - // By vat rule. |
|
717 | - if ( 'vat_rule' == $vars['orderby'] ) { |
|
718 | - return array_merge( |
|
719 | - $vars, |
|
720 | - array( |
|
721 | - 'meta_key' => '_wpinv_vat_rule', |
|
722 | - 'orderby' => 'meta_value' |
|
723 | - ) |
|
724 | - ); |
|
725 | - } |
|
726 | - |
|
727 | - // By price. |
|
728 | - if ( 'price' == $vars['orderby'] ) { |
|
729 | - return array_merge( |
|
730 | - $vars, |
|
731 | - array( |
|
732 | - 'meta_key' => '_wpinv_price', |
|
733 | - 'orderby' => 'meta_value_num' |
|
734 | - ) |
|
735 | - ); |
|
736 | - } |
|
737 | - |
|
738 | - return $vars; |
|
739 | - |
|
740 | - } |
|
741 | - |
|
742 | - /** |
|
743 | - * Fired when deleting a post. |
|
744 | - */ |
|
745 | - public static function delete_post( $post_id ) { |
|
746 | - |
|
747 | - switch ( get_post_type( $post_id ) ) { |
|
748 | - |
|
749 | - case 'wpi_item' : |
|
750 | - do_action( "getpaid_before_delete_item", new WPInv_Item( $post_id ) ); |
|
751 | - break; |
|
752 | - |
|
753 | - case 'wpi_payment_form' : |
|
754 | - do_action( "getpaid_before_delete_payment_form", new GetPaid_Payment_Form( $post_id ) ); |
|
755 | - break; |
|
756 | - |
|
757 | - case 'wpi_discount' : |
|
758 | - do_action( "getpaid_before_delete_discount", new WPInv_Discount( $post_id ) ); |
|
759 | - break; |
|
760 | - |
|
761 | - case 'wpi_invoice' : |
|
762 | - $invoice = new WPInv_Invoice( $post_id ); |
|
763 | - do_action( "getpaid_before_delete_invoice", $invoice ); |
|
764 | - $invoice->get_data_store()->delete_items( $invoice ); |
|
765 | - $invoice->get_data_store()->delete_special_fields( $invoice ); |
|
766 | - break; |
|
767 | - } |
|
768 | - } |
|
680 | + } |
|
681 | + |
|
682 | + } |
|
683 | + |
|
684 | + /** |
|
685 | + * Reorders items. |
|
686 | + */ |
|
687 | + public static function reorder_items( $vars ) { |
|
688 | + global $typenow; |
|
689 | + |
|
690 | + if ( 'wpi_item' !== $typenow || empty( $vars['orderby'] ) ) { |
|
691 | + return $vars; |
|
692 | + } |
|
693 | + |
|
694 | + // By item type. |
|
695 | + if ( 'type' == $vars['orderby'] ) { |
|
696 | + return array_merge( |
|
697 | + $vars, |
|
698 | + array( |
|
699 | + 'meta_key' => '_wpinv_type', |
|
700 | + 'orderby' => 'meta_value' |
|
701 | + ) |
|
702 | + ); |
|
703 | + } |
|
704 | + |
|
705 | + // By vat class. |
|
706 | + if ( 'vat_class' == $vars['orderby'] ) { |
|
707 | + return array_merge( |
|
708 | + $vars, |
|
709 | + array( |
|
710 | + 'meta_key' => '_wpinv_vat_class', |
|
711 | + 'orderby' => 'meta_value' |
|
712 | + ) |
|
713 | + ); |
|
714 | + } |
|
715 | + |
|
716 | + // By vat rule. |
|
717 | + if ( 'vat_rule' == $vars['orderby'] ) { |
|
718 | + return array_merge( |
|
719 | + $vars, |
|
720 | + array( |
|
721 | + 'meta_key' => '_wpinv_vat_rule', |
|
722 | + 'orderby' => 'meta_value' |
|
723 | + ) |
|
724 | + ); |
|
725 | + } |
|
726 | + |
|
727 | + // By price. |
|
728 | + if ( 'price' == $vars['orderby'] ) { |
|
729 | + return array_merge( |
|
730 | + $vars, |
|
731 | + array( |
|
732 | + 'meta_key' => '_wpinv_price', |
|
733 | + 'orderby' => 'meta_value_num' |
|
734 | + ) |
|
735 | + ); |
|
736 | + } |
|
737 | + |
|
738 | + return $vars; |
|
739 | + |
|
740 | + } |
|
741 | + |
|
742 | + /** |
|
743 | + * Fired when deleting a post. |
|
744 | + */ |
|
745 | + public static function delete_post( $post_id ) { |
|
746 | + |
|
747 | + switch ( get_post_type( $post_id ) ) { |
|
748 | + |
|
749 | + case 'wpi_item' : |
|
750 | + do_action( "getpaid_before_delete_item", new WPInv_Item( $post_id ) ); |
|
751 | + break; |
|
752 | + |
|
753 | + case 'wpi_payment_form' : |
|
754 | + do_action( "getpaid_before_delete_payment_form", new GetPaid_Payment_Form( $post_id ) ); |
|
755 | + break; |
|
756 | + |
|
757 | + case 'wpi_discount' : |
|
758 | + do_action( "getpaid_before_delete_discount", new WPInv_Discount( $post_id ) ); |
|
759 | + break; |
|
760 | + |
|
761 | + case 'wpi_invoice' : |
|
762 | + $invoice = new WPInv_Invoice( $post_id ); |
|
763 | + do_action( "getpaid_before_delete_invoice", $invoice ); |
|
764 | + $invoice->get_data_store()->delete_items( $invoice ); |
|
765 | + $invoice->get_data_store()->delete_special_fields( $invoice ); |
|
766 | + break; |
|
767 | + } |
|
768 | + } |
|
769 | 769 | |
770 | 770 | } |
@@ -9,31 +9,31 @@ |
||
9 | 9 | defined( 'ABSPATH' ) || exit; |
10 | 10 | |
11 | 11 | return array( |
12 | - 'AT', |
|
13 | - 'BE', |
|
14 | - 'BG', |
|
15 | - 'HR', |
|
16 | - 'CY', |
|
17 | - 'CZ', |
|
18 | - 'DK', |
|
19 | - 'EE', |
|
20 | - 'FI', |
|
21 | - 'FR', |
|
22 | - 'DE', |
|
23 | - 'GR', |
|
24 | - 'HU', |
|
25 | - 'IE', |
|
26 | - 'IT', |
|
27 | - 'LV', |
|
28 | - 'LT', |
|
29 | - 'LU', |
|
30 | - 'MT', |
|
31 | - 'NL', |
|
32 | - 'PL', |
|
33 | - 'PT', |
|
34 | - 'RO', |
|
35 | - 'SK', |
|
36 | - 'SI', |
|
37 | - 'ES', |
|
38 | - 'SE' |
|
12 | + 'AT', |
|
13 | + 'BE', |
|
14 | + 'BG', |
|
15 | + 'HR', |
|
16 | + 'CY', |
|
17 | + 'CZ', |
|
18 | + 'DK', |
|
19 | + 'EE', |
|
20 | + 'FI', |
|
21 | + 'FR', |
|
22 | + 'DE', |
|
23 | + 'GR', |
|
24 | + 'HU', |
|
25 | + 'IE', |
|
26 | + 'IT', |
|
27 | + 'LV', |
|
28 | + 'LT', |
|
29 | + 'LU', |
|
30 | + 'MT', |
|
31 | + 'NL', |
|
32 | + 'PL', |
|
33 | + 'PT', |
|
34 | + 'RO', |
|
35 | + 'SK', |
|
36 | + 'SI', |
|
37 | + 'ES', |
|
38 | + 'SE' |
|
39 | 39 | ); |
@@ -34,11 +34,11 @@ discard block |
||
34 | 34 | */ |
35 | 35 | function wpinv_get_capability( $capalibilty = 'manage_invoicing' ) { |
36 | 36 | |
37 | - if ( current_user_can( 'manage_options' ) ) { |
|
38 | - return 'manage_options'; |
|
39 | - }; |
|
37 | + if ( current_user_can( 'manage_options' ) ) { |
|
38 | + return 'manage_options'; |
|
39 | + }; |
|
40 | 40 | |
41 | - return $capalibilty; |
|
41 | + return $capalibilty; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -62,10 +62,10 @@ discard block |
||
62 | 62 | // Prepare user values. |
63 | 63 | $prefix = preg_replace( '/\s+/', '', $prefix ); |
64 | 64 | $prefix = empty( $prefix ) ? $email : $prefix; |
65 | - $args = array( |
|
66 | - 'user_login' => wpinv_generate_user_name( $prefix ), |
|
67 | - 'user_pass' => wp_generate_password(), |
|
68 | - 'user_email' => $email, |
|
65 | + $args = array( |
|
66 | + 'user_login' => wpinv_generate_user_name( $prefix ), |
|
67 | + 'user_pass' => wp_generate_password(), |
|
68 | + 'user_email' => $email, |
|
69 | 69 | 'role' => 'subscriber', |
70 | 70 | ); |
71 | 71 | |
@@ -82,16 +82,16 @@ discard block |
||
82 | 82 | function wpinv_generate_user_name( $prefix = '' ) { |
83 | 83 | |
84 | 84 | // If prefix is an email, retrieve the part before the email. |
85 | - $prefix = strtok( $prefix, '@' ); |
|
85 | + $prefix = strtok( $prefix, '@' ); |
|
86 | 86 | $prefix = trim( $prefix, '.' ); |
87 | 87 | |
88 | - // Sanitize the username. |
|
89 | - $prefix = sanitize_user( $prefix, true ); |
|
88 | + // Sanitize the username. |
|
89 | + $prefix = sanitize_user( $prefix, true ); |
|
90 | 90 | |
91 | - $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); |
|
92 | - if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) { |
|
93 | - $prefix = 'gtp_' . zeroise( wp_rand( 0, 9999 ), 4 ); |
|
94 | - } |
|
91 | + $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); |
|
92 | + if ( empty( $prefix ) || in_array( strtolower( $prefix ), array_map( 'strtolower', $illegal_logins ), true ) ) { |
|
93 | + $prefix = 'gtp_' . zeroise( wp_rand( 0, 9999 ), 4 ); |
|
94 | + } |
|
95 | 95 | |
96 | 96 | $username = $prefix; |
97 | 97 | $postfix = 2; |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | // Load WP_List_Table if not loaded |
11 | 11 | if ( ! class_exists( 'WP_List_Table' ) ) { |
12 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
12 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | /** |
@@ -21,354 +21,354 @@ discard block |
||
21 | 21 | */ |
22 | 22 | class WPInv_Customers_Table extends WP_List_Table { |
23 | 23 | |
24 | - /** |
|
25 | - * @var int Number of items per page |
|
26 | - * @since 1.0.19 |
|
27 | - */ |
|
28 | - public $per_page = 10; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var int Number of items |
|
32 | - * @since 1.0.19 |
|
33 | - */ |
|
34 | - public $total = 0; |
|
35 | - |
|
36 | - /** |
|
37 | - * Get things started |
|
38 | - * |
|
39 | - * @since 1.0.19 |
|
40 | - * @see WP_List_Table::__construct() |
|
41 | - */ |
|
42 | - public function __construct() { |
|
43 | - |
|
44 | - // Set parent defaults |
|
45 | - parent::__construct( array( |
|
46 | - 'singular' => 'id', |
|
47 | - 'plural' => 'ids', |
|
48 | - 'ajax' => false, |
|
49 | - ) ); |
|
50 | - |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Gets the name of the primary column. |
|
55 | - * |
|
56 | - * @since 1.0.19 |
|
57 | - * @access protected |
|
58 | - * |
|
59 | - * @return string Name of the primary column. |
|
60 | - */ |
|
61 | - protected function get_primary_column_name() { |
|
62 | - return 'name'; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * This function renders most of the columns in the list table. |
|
67 | - * |
|
68 | - * @since 1.0.19 |
|
69 | - * |
|
70 | - * @param WP_User $item |
|
71 | - * @param string $column_name The name of the column |
|
72 | - * |
|
73 | - * @return string Column Name |
|
74 | - */ |
|
75 | - public function column_default( $item, $column_name ) { |
|
76 | - $value = sanitize_text_field( get_user_meta( $item->ID, '_wpinv_' . $column_name, true ) ); |
|
77 | - return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $item ); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Displays the country column. |
|
82 | - * |
|
83 | - * @since 1.0.19 |
|
84 | - * |
|
85 | - * @param WP_User $user |
|
86 | - * |
|
87 | - * @return string Column Name |
|
88 | - */ |
|
89 | - public function column_country( $user ) { |
|
90 | - $country = wpinv_sanitize_country( $user->_wpinv_country ); |
|
91 | - if ( $country ) { |
|
92 | - $country = wpinv_country_name( $country ); |
|
93 | - } |
|
94 | - return sanitize_text_field( $country ); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Displays the state column. |
|
99 | - * |
|
100 | - * @since 1.0.19 |
|
101 | - * |
|
102 | - * @param WP_User $user |
|
103 | - * |
|
104 | - * @return string Column Name |
|
105 | - */ |
|
106 | - public function column_state( $user ) { |
|
107 | - $country = wpinv_sanitize_country( $user->_wpinv_country ); |
|
108 | - $state = $user->_wpinv_state; |
|
109 | - if ( $state ) { |
|
110 | - $state = wpinv_state_name( $state, $country ); |
|
111 | - } |
|
112 | - |
|
113 | - return sanitize_text_field( $state ); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Displays the signup column. |
|
118 | - * |
|
119 | - * @since 1.0.19 |
|
120 | - * |
|
121 | - * @param WP_User $user |
|
122 | - * |
|
123 | - * @return string Column Name |
|
124 | - */ |
|
125 | - public function column_signup( $user ) { |
|
126 | - return getpaid_format_date_value( $user->user_registered ); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Displays the total spent column. |
|
131 | - * |
|
132 | - * @since 1.0.19 |
|
133 | - * |
|
134 | - * @param WP_User $user |
|
135 | - * |
|
136 | - * @return string Column Name |
|
137 | - */ |
|
138 | - public function column_total( $user ) { |
|
139 | - |
|
140 | - $args = array( |
|
141 | - 'data' => array( |
|
142 | - |
|
143 | - 'total' => array( |
|
144 | - 'type' => 'invoice_data', |
|
145 | - 'function' => 'SUM', |
|
146 | - 'name' => 'total_sales', |
|
147 | - ) |
|
148 | - |
|
149 | - ), |
|
150 | - 'where' => array( |
|
151 | - |
|
152 | - 'author' => array( |
|
153 | - 'type' => 'post_data', |
|
154 | - 'value' => absint( $user->ID ), |
|
155 | - 'key' => 'posts.post_author', |
|
156 | - 'operator' => '=', |
|
157 | - ), |
|
158 | - |
|
159 | - ), |
|
160 | - 'query_type' => 'get_var', |
|
161 | - 'invoice_status' => array( 'wpi-renewal', 'wpi-processing', 'publish' ), |
|
162 | - ); |
|
163 | - |
|
164 | - return wpinv_price( (float) GetPaid_Reports_Helper::get_invoice_report_data( $args ) ); |
|
165 | - |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Displays the total spent column. |
|
170 | - * |
|
171 | - * @since 1.0.19 |
|
172 | - * |
|
173 | - * @param WP_User $user |
|
174 | - * |
|
175 | - * @return string Column Name |
|
176 | - */ |
|
177 | - public function column_invoices( $user ) { |
|
178 | - |
|
179 | - $args = array( |
|
180 | - 'data' => array( |
|
181 | - |
|
182 | - 'ID' => array( |
|
183 | - 'type' => 'post_data', |
|
184 | - 'function' => 'COUNT', |
|
185 | - 'name' => 'count', |
|
186 | - 'distinct' => true, |
|
187 | - ), |
|
188 | - |
|
189 | - ), |
|
190 | - 'where' => array( |
|
191 | - |
|
192 | - 'author' => array( |
|
193 | - 'type' => 'post_data', |
|
194 | - 'value' => absint( $user->ID ), |
|
195 | - 'key' => 'posts.post_author', |
|
196 | - 'operator' => '=', |
|
197 | - ), |
|
198 | - |
|
199 | - ), |
|
200 | - 'query_type' => 'get_var', |
|
201 | - 'invoice_status' => array_keys( wpinv_get_invoice_statuses() ), |
|
202 | - ); |
|
203 | - |
|
204 | - return absint( GetPaid_Reports_Helper::get_invoice_report_data( $args ) ); |
|
205 | - |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Generates content for a single row of the table |
|
210 | - * @since 1.0.19 |
|
211 | - * |
|
212 | - * @param int $item The user id. |
|
213 | - */ |
|
214 | - public function single_row( $item ) { |
|
215 | - $item = get_user_by( 'id', $item ); |
|
216 | - |
|
217 | - if ( empty( $item ) ) { |
|
218 | - return; |
|
219 | - } |
|
220 | - |
|
221 | - echo '<tr>'; |
|
222 | - $this->single_row_columns( $item ); |
|
223 | - echo '</tr>'; |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Displays the customers name |
|
228 | - * |
|
229 | - * @param WP_User $customer customer. |
|
230 | - * @return string |
|
231 | - */ |
|
232 | - public function column_name( $customer ) { |
|
233 | - |
|
234 | - // Customer view URL. |
|
235 | - $view_url = esc_url( add_query_arg( 'user_id', $customer->ID, admin_url( 'user-edit.php' ) ) ); |
|
236 | - $row_actions = $this->row_actions( |
|
237 | - array( |
|
238 | - 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
239 | - ) |
|
240 | - ); |
|
241 | - |
|
242 | - // Get user's address. |
|
243 | - $address = wpinv_get_user_address( $customer->ID ); |
|
244 | - |
|
245 | - // Customer email address. |
|
246 | - $email = sanitize_email( $customer->user_email ); |
|
247 | - |
|
248 | - // Customer's avatar. |
|
249 | - $avatar = esc_url( get_avatar_url( $email ) ); |
|
250 | - $avatar = "<img src='$avatar' height='32' width='32'/>"; |
|
251 | - |
|
252 | - // Customer's name. |
|
253 | - $name = sanitize_text_field( "{$address['first_name']} {$address['last_name']}" ); |
|
254 | - |
|
255 | - if ( ! empty( $name ) ) { |
|
256 | - $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
257 | - } |
|
258 | - |
|
259 | - $email = "<div class='row-title'><a href='$view_url'>$email</a></div>"; |
|
260 | - |
|
261 | - return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
262 | - |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Retrieve the table columns |
|
267 | - * |
|
268 | - * @since 1.0.19 |
|
269 | - * @return array $columns Array of all the list table columns |
|
270 | - */ |
|
271 | - public function get_columns() { |
|
272 | - |
|
273 | - $columns = array( |
|
274 | - 'name' => __( 'Name', 'invoicing' ), |
|
275 | - 'country' => __( 'Country', 'invoicing' ), |
|
276 | - 'state' => __( 'State', 'invoicing' ), |
|
277 | - 'city' => __( 'City', 'invoicing' ), |
|
278 | - 'zip' => __( 'ZIP', 'invoicing' ), |
|
279 | - 'address' => __( 'Address', 'invoicing' ), |
|
280 | - 'phone' => __( 'Phone', 'invoicing' ), |
|
281 | - 'company' => __( 'Company', 'invoicing' ), |
|
282 | - 'invoices' => __( 'Invoices', 'invoicing' ), |
|
283 | - 'total' => __( 'Total Spend', 'invoicing' ), |
|
284 | - 'signup' => __( 'Date created', 'invoicing' ), |
|
285 | - ); |
|
286 | - return apply_filters( 'wpinv_customers_table_columns', $columns ); |
|
287 | - |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Retrieve the current page number |
|
292 | - * |
|
293 | - * @since 1.0.19 |
|
294 | - * @return int Current page number |
|
295 | - */ |
|
296 | - public function get_paged() { |
|
297 | - return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Returns bulk actions. |
|
302 | - * |
|
303 | - * @since 1.0.19 |
|
304 | - * @return void |
|
305 | - */ |
|
306 | - public function bulk_actions( $which = '' ) { |
|
307 | - return array(); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Prepares the display query |
|
312 | - */ |
|
313 | - public function prepare_query() { |
|
314 | - global $wpdb; |
|
315 | - |
|
316 | - $post_types = ''; |
|
317 | - |
|
318 | - foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) { |
|
319 | - $post_types .= $wpdb->prepare( "post_type=%s OR ", $post_type ); |
|
320 | - } |
|
321 | - |
|
322 | - $post_types = rtrim( $post_types, ' OR' ); |
|
323 | - |
|
324 | - // Maybe search. |
|
325 | - if ( ! empty( $_POST['s'] ) ) { |
|
326 | - $users = get_users( |
|
327 | - array( |
|
328 | - 'search' => sanitize_text_field( urldecode( $_POST['s'] ) ), |
|
329 | - 'search_columns' => array( 'user_login', 'user_email', 'display_name' ), |
|
330 | - 'fields' => 'ID', |
|
331 | - ) |
|
332 | - ); |
|
333 | - |
|
334 | - $users = implode( ', ', $users ); |
|
335 | - $post_types = "($post_types) AND ( post_author IN ( $users ) )"; |
|
336 | - } |
|
337 | - |
|
338 | - // Users with invoices. |
|
339 | - $customers = $wpdb->get_col( |
|
340 | - $wpdb->prepare( |
|
341 | - "SELECT DISTINCT( post_author ) FROM $wpdb->posts WHERE $post_types LIMIT %d,%d", |
|
342 | - $this->get_paged() * 10 - 10, |
|
343 | - $this->per_page |
|
344 | - ) |
|
345 | - ); |
|
346 | - |
|
347 | - $this->items = $customers; |
|
348 | - $this->total = (int) $wpdb->get_var( "SELECT COUNT( DISTINCT( post_author ) ) FROM $wpdb->posts WHERE $post_types" ); |
|
349 | - |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Setup the final data for the table |
|
354 | - * |
|
355 | - * @since 1.0.19 |
|
356 | - * @return void |
|
357 | - */ |
|
358 | - public function prepare_items() { |
|
359 | - $columns = $this->get_columns(); |
|
360 | - $hidden = array(); // No hidden columns |
|
361 | - $sortable = $this->get_sortable_columns(); |
|
362 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
363 | - $this->prepare_query(); |
|
364 | - |
|
365 | - $this->set_pagination_args( |
|
366 | - array( |
|
367 | - 'total_items' => $this->total, |
|
368 | - 'per_page' => $this->per_page, |
|
369 | - 'total_pages' => ceil( $this->total / $this->per_page ) |
|
370 | - ) |
|
371 | - ); |
|
372 | - |
|
373 | - } |
|
24 | + /** |
|
25 | + * @var int Number of items per page |
|
26 | + * @since 1.0.19 |
|
27 | + */ |
|
28 | + public $per_page = 10; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var int Number of items |
|
32 | + * @since 1.0.19 |
|
33 | + */ |
|
34 | + public $total = 0; |
|
35 | + |
|
36 | + /** |
|
37 | + * Get things started |
|
38 | + * |
|
39 | + * @since 1.0.19 |
|
40 | + * @see WP_List_Table::__construct() |
|
41 | + */ |
|
42 | + public function __construct() { |
|
43 | + |
|
44 | + // Set parent defaults |
|
45 | + parent::__construct( array( |
|
46 | + 'singular' => 'id', |
|
47 | + 'plural' => 'ids', |
|
48 | + 'ajax' => false, |
|
49 | + ) ); |
|
50 | + |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Gets the name of the primary column. |
|
55 | + * |
|
56 | + * @since 1.0.19 |
|
57 | + * @access protected |
|
58 | + * |
|
59 | + * @return string Name of the primary column. |
|
60 | + */ |
|
61 | + protected function get_primary_column_name() { |
|
62 | + return 'name'; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * This function renders most of the columns in the list table. |
|
67 | + * |
|
68 | + * @since 1.0.19 |
|
69 | + * |
|
70 | + * @param WP_User $item |
|
71 | + * @param string $column_name The name of the column |
|
72 | + * |
|
73 | + * @return string Column Name |
|
74 | + */ |
|
75 | + public function column_default( $item, $column_name ) { |
|
76 | + $value = sanitize_text_field( get_user_meta( $item->ID, '_wpinv_' . $column_name, true ) ); |
|
77 | + return apply_filters( 'wpinv_customers_table_column' . $column_name, $value, $item ); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Displays the country column. |
|
82 | + * |
|
83 | + * @since 1.0.19 |
|
84 | + * |
|
85 | + * @param WP_User $user |
|
86 | + * |
|
87 | + * @return string Column Name |
|
88 | + */ |
|
89 | + public function column_country( $user ) { |
|
90 | + $country = wpinv_sanitize_country( $user->_wpinv_country ); |
|
91 | + if ( $country ) { |
|
92 | + $country = wpinv_country_name( $country ); |
|
93 | + } |
|
94 | + return sanitize_text_field( $country ); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Displays the state column. |
|
99 | + * |
|
100 | + * @since 1.0.19 |
|
101 | + * |
|
102 | + * @param WP_User $user |
|
103 | + * |
|
104 | + * @return string Column Name |
|
105 | + */ |
|
106 | + public function column_state( $user ) { |
|
107 | + $country = wpinv_sanitize_country( $user->_wpinv_country ); |
|
108 | + $state = $user->_wpinv_state; |
|
109 | + if ( $state ) { |
|
110 | + $state = wpinv_state_name( $state, $country ); |
|
111 | + } |
|
112 | + |
|
113 | + return sanitize_text_field( $state ); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Displays the signup column. |
|
118 | + * |
|
119 | + * @since 1.0.19 |
|
120 | + * |
|
121 | + * @param WP_User $user |
|
122 | + * |
|
123 | + * @return string Column Name |
|
124 | + */ |
|
125 | + public function column_signup( $user ) { |
|
126 | + return getpaid_format_date_value( $user->user_registered ); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Displays the total spent column. |
|
131 | + * |
|
132 | + * @since 1.0.19 |
|
133 | + * |
|
134 | + * @param WP_User $user |
|
135 | + * |
|
136 | + * @return string Column Name |
|
137 | + */ |
|
138 | + public function column_total( $user ) { |
|
139 | + |
|
140 | + $args = array( |
|
141 | + 'data' => array( |
|
142 | + |
|
143 | + 'total' => array( |
|
144 | + 'type' => 'invoice_data', |
|
145 | + 'function' => 'SUM', |
|
146 | + 'name' => 'total_sales', |
|
147 | + ) |
|
148 | + |
|
149 | + ), |
|
150 | + 'where' => array( |
|
151 | + |
|
152 | + 'author' => array( |
|
153 | + 'type' => 'post_data', |
|
154 | + 'value' => absint( $user->ID ), |
|
155 | + 'key' => 'posts.post_author', |
|
156 | + 'operator' => '=', |
|
157 | + ), |
|
158 | + |
|
159 | + ), |
|
160 | + 'query_type' => 'get_var', |
|
161 | + 'invoice_status' => array( 'wpi-renewal', 'wpi-processing', 'publish' ), |
|
162 | + ); |
|
163 | + |
|
164 | + return wpinv_price( (float) GetPaid_Reports_Helper::get_invoice_report_data( $args ) ); |
|
165 | + |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Displays the total spent column. |
|
170 | + * |
|
171 | + * @since 1.0.19 |
|
172 | + * |
|
173 | + * @param WP_User $user |
|
174 | + * |
|
175 | + * @return string Column Name |
|
176 | + */ |
|
177 | + public function column_invoices( $user ) { |
|
178 | + |
|
179 | + $args = array( |
|
180 | + 'data' => array( |
|
181 | + |
|
182 | + 'ID' => array( |
|
183 | + 'type' => 'post_data', |
|
184 | + 'function' => 'COUNT', |
|
185 | + 'name' => 'count', |
|
186 | + 'distinct' => true, |
|
187 | + ), |
|
188 | + |
|
189 | + ), |
|
190 | + 'where' => array( |
|
191 | + |
|
192 | + 'author' => array( |
|
193 | + 'type' => 'post_data', |
|
194 | + 'value' => absint( $user->ID ), |
|
195 | + 'key' => 'posts.post_author', |
|
196 | + 'operator' => '=', |
|
197 | + ), |
|
198 | + |
|
199 | + ), |
|
200 | + 'query_type' => 'get_var', |
|
201 | + 'invoice_status' => array_keys( wpinv_get_invoice_statuses() ), |
|
202 | + ); |
|
203 | + |
|
204 | + return absint( GetPaid_Reports_Helper::get_invoice_report_data( $args ) ); |
|
205 | + |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Generates content for a single row of the table |
|
210 | + * @since 1.0.19 |
|
211 | + * |
|
212 | + * @param int $item The user id. |
|
213 | + */ |
|
214 | + public function single_row( $item ) { |
|
215 | + $item = get_user_by( 'id', $item ); |
|
216 | + |
|
217 | + if ( empty( $item ) ) { |
|
218 | + return; |
|
219 | + } |
|
220 | + |
|
221 | + echo '<tr>'; |
|
222 | + $this->single_row_columns( $item ); |
|
223 | + echo '</tr>'; |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Displays the customers name |
|
228 | + * |
|
229 | + * @param WP_User $customer customer. |
|
230 | + * @return string |
|
231 | + */ |
|
232 | + public function column_name( $customer ) { |
|
233 | + |
|
234 | + // Customer view URL. |
|
235 | + $view_url = esc_url( add_query_arg( 'user_id', $customer->ID, admin_url( 'user-edit.php' ) ) ); |
|
236 | + $row_actions = $this->row_actions( |
|
237 | + array( |
|
238 | + 'view' => '<a href="' . $view_url . '#getpaid-fieldset-billing">' . __( 'Edit Details', 'invoicing' ) . '</a>', |
|
239 | + ) |
|
240 | + ); |
|
241 | + |
|
242 | + // Get user's address. |
|
243 | + $address = wpinv_get_user_address( $customer->ID ); |
|
244 | + |
|
245 | + // Customer email address. |
|
246 | + $email = sanitize_email( $customer->user_email ); |
|
247 | + |
|
248 | + // Customer's avatar. |
|
249 | + $avatar = esc_url( get_avatar_url( $email ) ); |
|
250 | + $avatar = "<img src='$avatar' height='32' width='32'/>"; |
|
251 | + |
|
252 | + // Customer's name. |
|
253 | + $name = sanitize_text_field( "{$address['first_name']} {$address['last_name']}" ); |
|
254 | + |
|
255 | + if ( ! empty( $name ) ) { |
|
256 | + $name = "<div style='overflow: hidden;height: 18px;'>$name</div>"; |
|
257 | + } |
|
258 | + |
|
259 | + $email = "<div class='row-title'><a href='$view_url'>$email</a></div>"; |
|
260 | + |
|
261 | + return "<div style='display: flex;'><div>$avatar</div><div style='margin-left: 10px;'>$name<strong>$email</strong>$row_actions</div></div>"; |
|
262 | + |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Retrieve the table columns |
|
267 | + * |
|
268 | + * @since 1.0.19 |
|
269 | + * @return array $columns Array of all the list table columns |
|
270 | + */ |
|
271 | + public function get_columns() { |
|
272 | + |
|
273 | + $columns = array( |
|
274 | + 'name' => __( 'Name', 'invoicing' ), |
|
275 | + 'country' => __( 'Country', 'invoicing' ), |
|
276 | + 'state' => __( 'State', 'invoicing' ), |
|
277 | + 'city' => __( 'City', 'invoicing' ), |
|
278 | + 'zip' => __( 'ZIP', 'invoicing' ), |
|
279 | + 'address' => __( 'Address', 'invoicing' ), |
|
280 | + 'phone' => __( 'Phone', 'invoicing' ), |
|
281 | + 'company' => __( 'Company', 'invoicing' ), |
|
282 | + 'invoices' => __( 'Invoices', 'invoicing' ), |
|
283 | + 'total' => __( 'Total Spend', 'invoicing' ), |
|
284 | + 'signup' => __( 'Date created', 'invoicing' ), |
|
285 | + ); |
|
286 | + return apply_filters( 'wpinv_customers_table_columns', $columns ); |
|
287 | + |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Retrieve the current page number |
|
292 | + * |
|
293 | + * @since 1.0.19 |
|
294 | + * @return int Current page number |
|
295 | + */ |
|
296 | + public function get_paged() { |
|
297 | + return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Returns bulk actions. |
|
302 | + * |
|
303 | + * @since 1.0.19 |
|
304 | + * @return void |
|
305 | + */ |
|
306 | + public function bulk_actions( $which = '' ) { |
|
307 | + return array(); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Prepares the display query |
|
312 | + */ |
|
313 | + public function prepare_query() { |
|
314 | + global $wpdb; |
|
315 | + |
|
316 | + $post_types = ''; |
|
317 | + |
|
318 | + foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) { |
|
319 | + $post_types .= $wpdb->prepare( "post_type=%s OR ", $post_type ); |
|
320 | + } |
|
321 | + |
|
322 | + $post_types = rtrim( $post_types, ' OR' ); |
|
323 | + |
|
324 | + // Maybe search. |
|
325 | + if ( ! empty( $_POST['s'] ) ) { |
|
326 | + $users = get_users( |
|
327 | + array( |
|
328 | + 'search' => sanitize_text_field( urldecode( $_POST['s'] ) ), |
|
329 | + 'search_columns' => array( 'user_login', 'user_email', 'display_name' ), |
|
330 | + 'fields' => 'ID', |
|
331 | + ) |
|
332 | + ); |
|
333 | + |
|
334 | + $users = implode( ', ', $users ); |
|
335 | + $post_types = "($post_types) AND ( post_author IN ( $users ) )"; |
|
336 | + } |
|
337 | + |
|
338 | + // Users with invoices. |
|
339 | + $customers = $wpdb->get_col( |
|
340 | + $wpdb->prepare( |
|
341 | + "SELECT DISTINCT( post_author ) FROM $wpdb->posts WHERE $post_types LIMIT %d,%d", |
|
342 | + $this->get_paged() * 10 - 10, |
|
343 | + $this->per_page |
|
344 | + ) |
|
345 | + ); |
|
346 | + |
|
347 | + $this->items = $customers; |
|
348 | + $this->total = (int) $wpdb->get_var( "SELECT COUNT( DISTINCT( post_author ) ) FROM $wpdb->posts WHERE $post_types" ); |
|
349 | + |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Setup the final data for the table |
|
354 | + * |
|
355 | + * @since 1.0.19 |
|
356 | + * @return void |
|
357 | + */ |
|
358 | + public function prepare_items() { |
|
359 | + $columns = $this->get_columns(); |
|
360 | + $hidden = array(); // No hidden columns |
|
361 | + $sortable = $this->get_sortable_columns(); |
|
362 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
363 | + $this->prepare_query(); |
|
364 | + |
|
365 | + $this->set_pagination_args( |
|
366 | + array( |
|
367 | + 'total_items' => $this->total, |
|
368 | + 'per_page' => $this->per_page, |
|
369 | + 'total_pages' => ceil( $this->total / $this->per_page ) |
|
370 | + ) |
|
371 | + ); |
|
372 | + |
|
373 | + } |
|
374 | 374 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,187 +10,187 @@ discard block |
||
10 | 10 | class GetPaid_Payment_Form_Submission { |
11 | 11 | |
12 | 12 | /** |
13 | - * Submission ID |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - public $id = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * The raw submission data. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $data = null; |
|
25 | - |
|
26 | - /** |
|
27 | - * Submission totals |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $totals = array( |
|
32 | - |
|
33 | - 'subtotal' => array( |
|
34 | - 'initial' => 0, |
|
35 | - 'recurring' => 0, |
|
36 | - ), |
|
37 | - |
|
38 | - 'discount' => array( |
|
39 | - 'initial' => 0, |
|
40 | - 'recurring' => 0, |
|
41 | - ), |
|
42 | - |
|
43 | - 'fees' => array( |
|
44 | - 'initial' => 0, |
|
45 | - 'recurring' => 0, |
|
46 | - ), |
|
47 | - |
|
48 | - 'taxes' => array( |
|
49 | - 'initial' => 0, |
|
50 | - 'recurring' => 0, |
|
51 | - ), |
|
52 | - |
|
53 | - ); |
|
54 | - |
|
55 | - /** |
|
56 | - * Sets the associated payment form. |
|
57 | - * |
|
58 | - * @var GetPaid_Payment_Form |
|
59 | - */ |
|
13 | + * Submission ID |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + public $id = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * The raw submission data. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $data = null; |
|
25 | + |
|
26 | + /** |
|
27 | + * Submission totals |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $totals = array( |
|
32 | + |
|
33 | + 'subtotal' => array( |
|
34 | + 'initial' => 0, |
|
35 | + 'recurring' => 0, |
|
36 | + ), |
|
37 | + |
|
38 | + 'discount' => array( |
|
39 | + 'initial' => 0, |
|
40 | + 'recurring' => 0, |
|
41 | + ), |
|
42 | + |
|
43 | + 'fees' => array( |
|
44 | + 'initial' => 0, |
|
45 | + 'recurring' => 0, |
|
46 | + ), |
|
47 | + |
|
48 | + 'taxes' => array( |
|
49 | + 'initial' => 0, |
|
50 | + 'recurring' => 0, |
|
51 | + ), |
|
52 | + |
|
53 | + ); |
|
54 | + |
|
55 | + /** |
|
56 | + * Sets the associated payment form. |
|
57 | + * |
|
58 | + * @var GetPaid_Payment_Form |
|
59 | + */ |
|
60 | 60 | protected $payment_form = null; |
61 | 61 | |
62 | 62 | /** |
63 | - * The country for the submission. |
|
64 | - * |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - public $country = null; |
|
68 | - |
|
69 | - /** |
|
70 | - * The state for the submission. |
|
71 | - * |
|
72 | - * @since 1.0.19 |
|
73 | - * @var string |
|
74 | - */ |
|
75 | - public $state = null; |
|
76 | - |
|
77 | - /** |
|
78 | - * The invoice associated with the submission. |
|
79 | - * |
|
80 | - * @var WPInv_Invoice |
|
81 | - */ |
|
82 | - protected $invoice = null; |
|
83 | - |
|
84 | - /** |
|
85 | - * The recurring item for the submission. |
|
86 | - * |
|
87 | - * @var int |
|
88 | - */ |
|
89 | - public $has_recurring = 0; |
|
90 | - |
|
91 | - /** |
|
92 | - * An array of fees for the submission. |
|
93 | - * |
|
94 | - * @var array |
|
95 | - */ |
|
96 | - protected $fees = array(); |
|
97 | - |
|
98 | - /** |
|
99 | - * An array of discounts for the submission. |
|
100 | - * |
|
101 | - * @var array |
|
102 | - */ |
|
103 | - protected $discounts = array(); |
|
104 | - |
|
105 | - /** |
|
106 | - * An array of taxes for the submission. |
|
107 | - * |
|
108 | - * @var array |
|
109 | - */ |
|
110 | - protected $taxes = array(); |
|
111 | - |
|
112 | - /** |
|
113 | - * An array of items for the submission. |
|
114 | - * |
|
115 | - * @var GetPaid_Form_Item[] |
|
116 | - */ |
|
117 | - protected $items = array(); |
|
118 | - |
|
119 | - /** |
|
120 | - * The last error. |
|
121 | - * |
|
122 | - * @var string |
|
123 | - */ |
|
124 | - public $last_error = null; |
|
125 | - |
|
126 | - /** |
|
127 | - * Class constructor. |
|
128 | - * |
|
129 | - */ |
|
130 | - public function __construct() { |
|
131 | - |
|
132 | - // Set the state and country to the default state and country. |
|
133 | - $this->country = wpinv_default_billing_country(); |
|
134 | - $this->state = wpinv_get_default_state(); |
|
135 | - |
|
136 | - // Do we have an actual submission? |
|
137 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
138 | - $this->load_data( $_POST ); |
|
139 | - } |
|
140 | - |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Loads submission data. |
|
145 | - * |
|
146 | - * @param array $data |
|
147 | - */ |
|
148 | - public function load_data( $data ) { |
|
149 | - |
|
150 | - // Remove slashes from the submitted data... |
|
151 | - $data = wp_unslash( $data ); |
|
152 | - |
|
153 | - // Allow plugins to filter the data. |
|
154 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
155 | - |
|
156 | - // Cache it... |
|
157 | - $this->data = $data; |
|
158 | - |
|
159 | - // Then generate a unique id from the data. |
|
160 | - $this->id = md5( wp_json_encode( $data ) ); |
|
161 | - |
|
162 | - // Finally, process the submission. |
|
163 | - try { |
|
164 | - |
|
165 | - // Each process is passed an instance of the class (with reference) |
|
166 | - // and should throw an Exception whenever it encounters one. |
|
167 | - $processors = apply_filters( |
|
168 | - 'getpaid_payment_form_submission_processors', |
|
169 | - array( |
|
170 | - array( $this, 'process_payment_form' ), |
|
171 | - array( $this, 'process_invoice' ), |
|
172 | - array( $this, 'process_fees' ), |
|
173 | - array( $this, 'process_items' ), |
|
174 | - array( $this, 'process_discount' ), |
|
175 | - array( $this, 'process_taxes' ), |
|
176 | - ), |
|
177 | - $this |
|
178 | - ); |
|
179 | - |
|
180 | - foreach ( $processors as $processor ) { |
|
181 | - call_user_func_array( $processor, array( &$this ) ); |
|
182 | - } |
|
183 | - |
|
184 | - } catch ( Exception $e ) { |
|
185 | - $this->last_error = $e->getMessage(); |
|
186 | - } |
|
187 | - |
|
188 | - // Fired when we are done processing a submission. |
|
189 | - do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
190 | - |
|
191 | - } |
|
192 | - |
|
193 | - /* |
|
63 | + * The country for the submission. |
|
64 | + * |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + public $country = null; |
|
68 | + |
|
69 | + /** |
|
70 | + * The state for the submission. |
|
71 | + * |
|
72 | + * @since 1.0.19 |
|
73 | + * @var string |
|
74 | + */ |
|
75 | + public $state = null; |
|
76 | + |
|
77 | + /** |
|
78 | + * The invoice associated with the submission. |
|
79 | + * |
|
80 | + * @var WPInv_Invoice |
|
81 | + */ |
|
82 | + protected $invoice = null; |
|
83 | + |
|
84 | + /** |
|
85 | + * The recurring item for the submission. |
|
86 | + * |
|
87 | + * @var int |
|
88 | + */ |
|
89 | + public $has_recurring = 0; |
|
90 | + |
|
91 | + /** |
|
92 | + * An array of fees for the submission. |
|
93 | + * |
|
94 | + * @var array |
|
95 | + */ |
|
96 | + protected $fees = array(); |
|
97 | + |
|
98 | + /** |
|
99 | + * An array of discounts for the submission. |
|
100 | + * |
|
101 | + * @var array |
|
102 | + */ |
|
103 | + protected $discounts = array(); |
|
104 | + |
|
105 | + /** |
|
106 | + * An array of taxes for the submission. |
|
107 | + * |
|
108 | + * @var array |
|
109 | + */ |
|
110 | + protected $taxes = array(); |
|
111 | + |
|
112 | + /** |
|
113 | + * An array of items for the submission. |
|
114 | + * |
|
115 | + * @var GetPaid_Form_Item[] |
|
116 | + */ |
|
117 | + protected $items = array(); |
|
118 | + |
|
119 | + /** |
|
120 | + * The last error. |
|
121 | + * |
|
122 | + * @var string |
|
123 | + */ |
|
124 | + public $last_error = null; |
|
125 | + |
|
126 | + /** |
|
127 | + * Class constructor. |
|
128 | + * |
|
129 | + */ |
|
130 | + public function __construct() { |
|
131 | + |
|
132 | + // Set the state and country to the default state and country. |
|
133 | + $this->country = wpinv_default_billing_country(); |
|
134 | + $this->state = wpinv_get_default_state(); |
|
135 | + |
|
136 | + // Do we have an actual submission? |
|
137 | + if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
138 | + $this->load_data( $_POST ); |
|
139 | + } |
|
140 | + |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Loads submission data. |
|
145 | + * |
|
146 | + * @param array $data |
|
147 | + */ |
|
148 | + public function load_data( $data ) { |
|
149 | + |
|
150 | + // Remove slashes from the submitted data... |
|
151 | + $data = wp_unslash( $data ); |
|
152 | + |
|
153 | + // Allow plugins to filter the data. |
|
154 | + $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
155 | + |
|
156 | + // Cache it... |
|
157 | + $this->data = $data; |
|
158 | + |
|
159 | + // Then generate a unique id from the data. |
|
160 | + $this->id = md5( wp_json_encode( $data ) ); |
|
161 | + |
|
162 | + // Finally, process the submission. |
|
163 | + try { |
|
164 | + |
|
165 | + // Each process is passed an instance of the class (with reference) |
|
166 | + // and should throw an Exception whenever it encounters one. |
|
167 | + $processors = apply_filters( |
|
168 | + 'getpaid_payment_form_submission_processors', |
|
169 | + array( |
|
170 | + array( $this, 'process_payment_form' ), |
|
171 | + array( $this, 'process_invoice' ), |
|
172 | + array( $this, 'process_fees' ), |
|
173 | + array( $this, 'process_items' ), |
|
174 | + array( $this, 'process_discount' ), |
|
175 | + array( $this, 'process_taxes' ), |
|
176 | + ), |
|
177 | + $this |
|
178 | + ); |
|
179 | + |
|
180 | + foreach ( $processors as $processor ) { |
|
181 | + call_user_func_array( $processor, array( &$this ) ); |
|
182 | + } |
|
183 | + |
|
184 | + } catch ( Exception $e ) { |
|
185 | + $this->last_error = $e->getMessage(); |
|
186 | + } |
|
187 | + |
|
188 | + // Fired when we are done processing a submission. |
|
189 | + do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
190 | + |
|
191 | + } |
|
192 | + |
|
193 | + /* |
|
194 | 194 | |-------------------------------------------------------------------------- |
195 | 195 | | Payment Forms. |
196 | 196 | |-------------------------------------------------------------------------- |
@@ -199,39 +199,39 @@ discard block |
||
199 | 199 | | submission has an active payment form etc. |
200 | 200 | */ |
201 | 201 | |
202 | - /** |
|
203 | - * Prepares the submission's payment form. |
|
204 | - * |
|
205 | - * @since 1.0.19 |
|
206 | - */ |
|
207 | - public function process_payment_form() { |
|
202 | + /** |
|
203 | + * Prepares the submission's payment form. |
|
204 | + * |
|
205 | + * @since 1.0.19 |
|
206 | + */ |
|
207 | + public function process_payment_form() { |
|
208 | 208 | |
209 | - // Every submission needs an active payment form. |
|
210 | - if ( empty( $this->data['form_id'] ) ) { |
|
211 | - throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
212 | - } |
|
209 | + // Every submission needs an active payment form. |
|
210 | + if ( empty( $this->data['form_id'] ) ) { |
|
211 | + throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
212 | + } |
|
213 | 213 | |
214 | - // Fetch the payment form. |
|
215 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
214 | + // Fetch the payment form. |
|
215 | + $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
216 | 216 | |
217 | - if ( ! $this->payment_form->is_active() ) { |
|
218 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
219 | - } |
|
217 | + if ( ! $this->payment_form->is_active() ) { |
|
218 | + throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
219 | + } |
|
220 | 220 | |
221 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
222 | - } |
|
221 | + do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
222 | + } |
|
223 | 223 | |
224 | 224 | /** |
225 | - * Returns the payment form. |
|
226 | - * |
|
227 | - * @since 1.0.19 |
|
228 | - * @return GetPaid_Payment_Form |
|
229 | - */ |
|
230 | - public function get_payment_form() { |
|
231 | - return $this->payment_form; |
|
232 | - } |
|
225 | + * Returns the payment form. |
|
226 | + * |
|
227 | + * @since 1.0.19 |
|
228 | + * @return GetPaid_Payment_Form |
|
229 | + */ |
|
230 | + public function get_payment_form() { |
|
231 | + return $this->payment_form; |
|
232 | + } |
|
233 | 233 | |
234 | - /* |
|
234 | + /* |
|
235 | 235 | |-------------------------------------------------------------------------- |
236 | 236 | | Invoices. |
237 | 237 | |-------------------------------------------------------------------------- |
@@ -240,61 +240,61 @@ discard block |
||
240 | 240 | | might be for an existing invoice. |
241 | 241 | */ |
242 | 242 | |
243 | - /** |
|
244 | - * Prepares the submission's invoice. |
|
245 | - * |
|
246 | - * @since 1.0.19 |
|
247 | - */ |
|
248 | - public function process_invoice() { |
|
243 | + /** |
|
244 | + * Prepares the submission's invoice. |
|
245 | + * |
|
246 | + * @since 1.0.19 |
|
247 | + */ |
|
248 | + public function process_invoice() { |
|
249 | 249 | |
250 | - // Abort if there is no invoice. |
|
251 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
252 | - return; |
|
253 | - } |
|
250 | + // Abort if there is no invoice. |
|
251 | + if ( empty( $this->data['invoice_id'] ) ) { |
|
252 | + return; |
|
253 | + } |
|
254 | 254 | |
255 | - // If the submission is for an existing invoice, ensure that it exists |
|
256 | - // and that it is not paid for. |
|
257 | - $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
255 | + // If the submission is for an existing invoice, ensure that it exists |
|
256 | + // and that it is not paid for. |
|
257 | + $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
258 | 258 | |
259 | 259 | if ( empty( $invoice ) ) { |
260 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
261 | - } |
|
262 | - |
|
263 | - if ( $invoice->is_paid() ) { |
|
264 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
265 | - } |
|
266 | - |
|
267 | - $this->payment_form->set_items( $invoice->get_items() ); |
|
268 | - $this->payment_form->invoice = $invoice; |
|
269 | - |
|
270 | - $this->country = $invoice->get_country(); |
|
271 | - $this->state = $invoice->get_state(); |
|
272 | - $this->invoice = $invoice; |
|
273 | - |
|
274 | - do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Returns the associated invoice. |
|
279 | - * |
|
280 | - * @since 1.0.19 |
|
281 | - * @return WPInv_Invoice |
|
282 | - */ |
|
283 | - public function get_invoice() { |
|
284 | - return $this->invoice; |
|
285 | - } |
|
286 | - |
|
287 | - /** |
|
288 | - * Checks whether there is an invoice associated with this submission. |
|
289 | - * |
|
290 | - * @since 1.0.19 |
|
291 | - * @return bool |
|
292 | - */ |
|
293 | - public function has_invoice() { |
|
294 | - return ! empty( $this->invoice ); |
|
295 | - } |
|
296 | - |
|
297 | - /* |
|
260 | + throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
261 | + } |
|
262 | + |
|
263 | + if ( $invoice->is_paid() ) { |
|
264 | + throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
265 | + } |
|
266 | + |
|
267 | + $this->payment_form->set_items( $invoice->get_items() ); |
|
268 | + $this->payment_form->invoice = $invoice; |
|
269 | + |
|
270 | + $this->country = $invoice->get_country(); |
|
271 | + $this->state = $invoice->get_state(); |
|
272 | + $this->invoice = $invoice; |
|
273 | + |
|
274 | + do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Returns the associated invoice. |
|
279 | + * |
|
280 | + * @since 1.0.19 |
|
281 | + * @return WPInv_Invoice |
|
282 | + */ |
|
283 | + public function get_invoice() { |
|
284 | + return $this->invoice; |
|
285 | + } |
|
286 | + |
|
287 | + /** |
|
288 | + * Checks whether there is an invoice associated with this submission. |
|
289 | + * |
|
290 | + * @since 1.0.19 |
|
291 | + * @return bool |
|
292 | + */ |
|
293 | + public function has_invoice() { |
|
294 | + return ! empty( $this->invoice ); |
|
295 | + } |
|
296 | + |
|
297 | + /* |
|
298 | 298 | |-------------------------------------------------------------------------- |
299 | 299 | | Items. |
300 | 300 | |-------------------------------------------------------------------------- |
@@ -303,115 +303,115 @@ discard block |
||
303 | 303 | | recurring item. But can have an unlimited number of non-recurring items. |
304 | 304 | */ |
305 | 305 | |
306 | - /** |
|
307 | - * Prepares the submission's items. |
|
308 | - * |
|
309 | - * @since 1.0.19 |
|
310 | - */ |
|
311 | - public function process_items() { |
|
312 | - |
|
313 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
314 | - |
|
315 | - foreach ( $processor->items as $item ) { |
|
316 | - $this->add_item( $item ); |
|
317 | - } |
|
318 | - |
|
319 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Adds an item to the submission. |
|
324 | - * |
|
325 | - * @since 1.0.19 |
|
326 | - * @param GetPaid_Form_Item $item |
|
327 | - */ |
|
328 | - public function add_item( $item ) { |
|
329 | - |
|
330 | - // Make sure that it is available for purchase. |
|
331 | - if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
332 | - return; |
|
333 | - } |
|
334 | - |
|
335 | - // Each submission can only contain one recurring item. |
|
336 | - if ( $item->is_recurring() ) { |
|
337 | - |
|
338 | - if ( $this->has_recurring != 0 ) { |
|
339 | - throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) ); |
|
340 | - } |
|
341 | - |
|
342 | - $this->has_recurring = $item->get_id(); |
|
343 | - |
|
344 | - } |
|
345 | - |
|
346 | - // Update the items and totals. |
|
347 | - $this->items[ $item->get_id() ] = $item; |
|
348 | - $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
349 | - $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
350 | - |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Removes a specific item. |
|
355 | - * |
|
356 | - * You should not call this method after the discounts and taxes |
|
357 | - * have been calculated. |
|
358 | - * |
|
359 | - * @since 1.0.19 |
|
360 | - */ |
|
361 | - public function remove_item( $item_id ) { |
|
362 | - |
|
363 | - if ( isset( $this->items[ $item_id ] ) ) { |
|
364 | - $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
365 | - $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
366 | - |
|
367 | - if ( $this->items[ $item_id ]->is_recurring() ) { |
|
368 | - $this->has_recurring = 0; |
|
369 | - } |
|
370 | - |
|
371 | - unset( $this->items[ $item_id ] ); |
|
372 | - } |
|
373 | - |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Returns the subtotal. |
|
378 | - * |
|
379 | - * @since 1.0.19 |
|
380 | - */ |
|
381 | - public function get_subtotal() { |
|
382 | - |
|
383 | - if ( wpinv_prices_include_tax() ) { |
|
384 | - return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
385 | - } |
|
386 | - |
|
387 | - return $this->totals['subtotal']['initial']; |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Returns the recurring subtotal. |
|
392 | - * |
|
393 | - * @since 1.0.19 |
|
394 | - */ |
|
395 | - public function get_recurring_subtotal() { |
|
396 | - |
|
397 | - if ( wpinv_prices_include_tax() ) { |
|
398 | - return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
399 | - } |
|
400 | - |
|
401 | - return $this->totals['subtotal']['recurring']; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Returns all items. |
|
406 | - * |
|
407 | - * @since 1.0.19 |
|
408 | - * @return GetPaid_Form_Item[] |
|
409 | - */ |
|
410 | - public function get_items() { |
|
411 | - return $this->items; |
|
412 | - } |
|
413 | - |
|
414 | - /* |
|
306 | + /** |
|
307 | + * Prepares the submission's items. |
|
308 | + * |
|
309 | + * @since 1.0.19 |
|
310 | + */ |
|
311 | + public function process_items() { |
|
312 | + |
|
313 | + $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
314 | + |
|
315 | + foreach ( $processor->items as $item ) { |
|
316 | + $this->add_item( $item ); |
|
317 | + } |
|
318 | + |
|
319 | + do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Adds an item to the submission. |
|
324 | + * |
|
325 | + * @since 1.0.19 |
|
326 | + * @param GetPaid_Form_Item $item |
|
327 | + */ |
|
328 | + public function add_item( $item ) { |
|
329 | + |
|
330 | + // Make sure that it is available for purchase. |
|
331 | + if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
332 | + return; |
|
333 | + } |
|
334 | + |
|
335 | + // Each submission can only contain one recurring item. |
|
336 | + if ( $item->is_recurring() ) { |
|
337 | + |
|
338 | + if ( $this->has_recurring != 0 ) { |
|
339 | + throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) ); |
|
340 | + } |
|
341 | + |
|
342 | + $this->has_recurring = $item->get_id(); |
|
343 | + |
|
344 | + } |
|
345 | + |
|
346 | + // Update the items and totals. |
|
347 | + $this->items[ $item->get_id() ] = $item; |
|
348 | + $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
349 | + $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
350 | + |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Removes a specific item. |
|
355 | + * |
|
356 | + * You should not call this method after the discounts and taxes |
|
357 | + * have been calculated. |
|
358 | + * |
|
359 | + * @since 1.0.19 |
|
360 | + */ |
|
361 | + public function remove_item( $item_id ) { |
|
362 | + |
|
363 | + if ( isset( $this->items[ $item_id ] ) ) { |
|
364 | + $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
365 | + $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
366 | + |
|
367 | + if ( $this->items[ $item_id ]->is_recurring() ) { |
|
368 | + $this->has_recurring = 0; |
|
369 | + } |
|
370 | + |
|
371 | + unset( $this->items[ $item_id ] ); |
|
372 | + } |
|
373 | + |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Returns the subtotal. |
|
378 | + * |
|
379 | + * @since 1.0.19 |
|
380 | + */ |
|
381 | + public function get_subtotal() { |
|
382 | + |
|
383 | + if ( wpinv_prices_include_tax() ) { |
|
384 | + return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
385 | + } |
|
386 | + |
|
387 | + return $this->totals['subtotal']['initial']; |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Returns the recurring subtotal. |
|
392 | + * |
|
393 | + * @since 1.0.19 |
|
394 | + */ |
|
395 | + public function get_recurring_subtotal() { |
|
396 | + |
|
397 | + if ( wpinv_prices_include_tax() ) { |
|
398 | + return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
399 | + } |
|
400 | + |
|
401 | + return $this->totals['subtotal']['recurring']; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Returns all items. |
|
406 | + * |
|
407 | + * @since 1.0.19 |
|
408 | + * @return GetPaid_Form_Item[] |
|
409 | + */ |
|
410 | + public function get_items() { |
|
411 | + return $this->items; |
|
412 | + } |
|
413 | + |
|
414 | + /* |
|
415 | 415 | |-------------------------------------------------------------------------- |
416 | 416 | | Taxes |
417 | 417 | |-------------------------------------------------------------------------- |
@@ -420,128 +420,128 @@ discard block |
||
420 | 420 | | or only one-time. |
421 | 421 | */ |
422 | 422 | |
423 | - /** |
|
424 | - * Prepares the submission's taxes. |
|
425 | - * |
|
426 | - * @since 1.0.19 |
|
427 | - */ |
|
428 | - public function process_taxes() { |
|
429 | - |
|
430 | - // Abort if we're not using taxes. |
|
431 | - if ( ! $this->use_taxes() ) { |
|
432 | - return; |
|
433 | - } |
|
434 | - |
|
435 | - // If a custom country && state has been passed in, use it to calculate taxes. |
|
436 | - $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
437 | - if ( ! empty( $country ) ) { |
|
438 | - $this->country = $country; |
|
439 | - } |
|
440 | - |
|
441 | - $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
442 | - if ( ! empty( $state ) ) { |
|
443 | - $this->state = $state; |
|
444 | - } |
|
445 | - |
|
446 | - // Confirm if the provided country and the ip country are similar. |
|
447 | - $address_confirmed = $this->get_field( 'confirm-address' ); |
|
448 | - if ( wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
449 | - throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
450 | - } |
|
451 | - |
|
452 | - // Abort if the country is not taxable. |
|
453 | - if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
454 | - return; |
|
455 | - } |
|
456 | - |
|
457 | - $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
458 | - |
|
459 | - foreach ( $processor->taxes as $tax ) { |
|
460 | - $this->add_tax( $tax ); |
|
461 | - } |
|
462 | - |
|
463 | - do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Adds a tax to the submission. |
|
468 | - * |
|
469 | - * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
470 | - * @since 1.0.19 |
|
471 | - */ |
|
472 | - public function add_tax( $tax ) { |
|
473 | - |
|
474 | - if ( wpinv_round_tax_per_tax_rate() ) { |
|
475 | - $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
476 | - $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
477 | - } |
|
478 | - |
|
479 | - $this->taxes[ $tax['name'] ] = $tax; |
|
480 | - $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
481 | - $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
482 | - |
|
483 | - } |
|
484 | - |
|
485 | - /** |
|
486 | - * Removes a specific tax. |
|
487 | - * |
|
488 | - * @since 1.0.19 |
|
489 | - */ |
|
490 | - public function remove_tax( $tax_name ) { |
|
491 | - |
|
492 | - if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
493 | - $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
494 | - $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
495 | - unset( $this->taxes[ $tax_name ] ); |
|
496 | - } |
|
497 | - |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * Whether or not we'll use taxes for the submission. |
|
502 | - * |
|
503 | - * @since 1.0.19 |
|
504 | - */ |
|
505 | - public function use_taxes() { |
|
506 | - |
|
507 | - $use_taxes = wpinv_use_taxes(); |
|
508 | - |
|
509 | - if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
510 | - $use_taxes = false; |
|
511 | - } |
|
512 | - |
|
513 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
514 | - |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Returns the tax. |
|
519 | - * |
|
520 | - * @since 1.0.19 |
|
521 | - */ |
|
522 | - public function get_tax() { |
|
523 | - return $this->totals['taxes']['initial']; |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Returns the recurring tax. |
|
528 | - * |
|
529 | - * @since 1.0.19 |
|
530 | - */ |
|
531 | - public function get_recurring_tax() { |
|
532 | - return $this->totals['taxes']['recurring']; |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * Returns all taxes. |
|
537 | - * |
|
538 | - * @since 1.0.19 |
|
539 | - */ |
|
540 | - public function get_taxes() { |
|
541 | - return $this->taxes; |
|
542 | - } |
|
543 | - |
|
544 | - /* |
|
423 | + /** |
|
424 | + * Prepares the submission's taxes. |
|
425 | + * |
|
426 | + * @since 1.0.19 |
|
427 | + */ |
|
428 | + public function process_taxes() { |
|
429 | + |
|
430 | + // Abort if we're not using taxes. |
|
431 | + if ( ! $this->use_taxes() ) { |
|
432 | + return; |
|
433 | + } |
|
434 | + |
|
435 | + // If a custom country && state has been passed in, use it to calculate taxes. |
|
436 | + $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
437 | + if ( ! empty( $country ) ) { |
|
438 | + $this->country = $country; |
|
439 | + } |
|
440 | + |
|
441 | + $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
442 | + if ( ! empty( $state ) ) { |
|
443 | + $this->state = $state; |
|
444 | + } |
|
445 | + |
|
446 | + // Confirm if the provided country and the ip country are similar. |
|
447 | + $address_confirmed = $this->get_field( 'confirm-address' ); |
|
448 | + if ( wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
449 | + throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
450 | + } |
|
451 | + |
|
452 | + // Abort if the country is not taxable. |
|
453 | + if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
454 | + return; |
|
455 | + } |
|
456 | + |
|
457 | + $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
458 | + |
|
459 | + foreach ( $processor->taxes as $tax ) { |
|
460 | + $this->add_tax( $tax ); |
|
461 | + } |
|
462 | + |
|
463 | + do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Adds a tax to the submission. |
|
468 | + * |
|
469 | + * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
470 | + * @since 1.0.19 |
|
471 | + */ |
|
472 | + public function add_tax( $tax ) { |
|
473 | + |
|
474 | + if ( wpinv_round_tax_per_tax_rate() ) { |
|
475 | + $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
476 | + $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
477 | + } |
|
478 | + |
|
479 | + $this->taxes[ $tax['name'] ] = $tax; |
|
480 | + $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
481 | + $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
482 | + |
|
483 | + } |
|
484 | + |
|
485 | + /** |
|
486 | + * Removes a specific tax. |
|
487 | + * |
|
488 | + * @since 1.0.19 |
|
489 | + */ |
|
490 | + public function remove_tax( $tax_name ) { |
|
491 | + |
|
492 | + if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
493 | + $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
494 | + $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
495 | + unset( $this->taxes[ $tax_name ] ); |
|
496 | + } |
|
497 | + |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * Whether or not we'll use taxes for the submission. |
|
502 | + * |
|
503 | + * @since 1.0.19 |
|
504 | + */ |
|
505 | + public function use_taxes() { |
|
506 | + |
|
507 | + $use_taxes = wpinv_use_taxes(); |
|
508 | + |
|
509 | + if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
510 | + $use_taxes = false; |
|
511 | + } |
|
512 | + |
|
513 | + return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
514 | + |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Returns the tax. |
|
519 | + * |
|
520 | + * @since 1.0.19 |
|
521 | + */ |
|
522 | + public function get_tax() { |
|
523 | + return $this->totals['taxes']['initial']; |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Returns the recurring tax. |
|
528 | + * |
|
529 | + * @since 1.0.19 |
|
530 | + */ |
|
531 | + public function get_recurring_tax() { |
|
532 | + return $this->totals['taxes']['recurring']; |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * Returns all taxes. |
|
537 | + * |
|
538 | + * @since 1.0.19 |
|
539 | + */ |
|
540 | + public function get_taxes() { |
|
541 | + return $this->taxes; |
|
542 | + } |
|
543 | + |
|
544 | + /* |
|
545 | 545 | |-------------------------------------------------------------------------- |
546 | 546 | | Discounts |
547 | 547 | |-------------------------------------------------------------------------- |
@@ -550,99 +550,99 @@ discard block |
||
550 | 550 | | or only one-time. They also do not have to come from a discount code. |
551 | 551 | */ |
552 | 552 | |
553 | - /** |
|
554 | - * Prepares the submission's discount. |
|
555 | - * |
|
556 | - * @since 1.0.19 |
|
557 | - */ |
|
558 | - public function process_discount() { |
|
559 | - |
|
560 | - $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
561 | - $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
562 | - $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
563 | - |
|
564 | - foreach ( $processor->discounts as $discount ) { |
|
565 | - $this->add_discount( $discount ); |
|
566 | - } |
|
567 | - |
|
568 | - do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Adds a discount to the submission. |
|
573 | - * |
|
574 | - * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
575 | - * @since 1.0.19 |
|
576 | - */ |
|
577 | - public function add_discount( $discount ) { |
|
578 | - $this->discounts[ $discount['name'] ] = $discount; |
|
579 | - $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
580 | - $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * Removes a discount from the submission. |
|
585 | - * |
|
586 | - * @since 1.0.19 |
|
587 | - */ |
|
588 | - public function remove_discount( $name ) { |
|
589 | - |
|
590 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
591 | - $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
592 | - $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
593 | - unset( $this->discounts[ $name ] ); |
|
594 | - } |
|
595 | - |
|
596 | - } |
|
597 | - |
|
598 | - /** |
|
599 | - * Checks whether there is a discount code associated with this submission. |
|
600 | - * |
|
601 | - * @since 1.0.19 |
|
602 | - * @return bool |
|
603 | - */ |
|
604 | - public function has_discount_code() { |
|
605 | - return ! empty( $this->discounts['discount_code'] ); |
|
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * Returns the discount code. |
|
610 | - * |
|
611 | - * @since 1.0.19 |
|
612 | - * @return string |
|
613 | - */ |
|
614 | - public function get_discount_code() { |
|
615 | - return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * Returns the discount. |
|
620 | - * |
|
621 | - * @since 1.0.19 |
|
622 | - */ |
|
623 | - public function get_discount() { |
|
624 | - return $this->totals['discount']['initial']; |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * Returns the recurring discount. |
|
629 | - * |
|
630 | - * @since 1.0.19 |
|
631 | - */ |
|
632 | - public function get_recurring_discount() { |
|
633 | - return $this->totals['discount']['recurring']; |
|
634 | - } |
|
635 | - |
|
636 | - /** |
|
637 | - * Returns all discounts. |
|
638 | - * |
|
639 | - * @since 1.0.19 |
|
640 | - */ |
|
641 | - public function get_discounts() { |
|
642 | - return $this->discounts; |
|
643 | - } |
|
644 | - |
|
645 | - /* |
|
553 | + /** |
|
554 | + * Prepares the submission's discount. |
|
555 | + * |
|
556 | + * @since 1.0.19 |
|
557 | + */ |
|
558 | + public function process_discount() { |
|
559 | + |
|
560 | + $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
561 | + $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
562 | + $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
563 | + |
|
564 | + foreach ( $processor->discounts as $discount ) { |
|
565 | + $this->add_discount( $discount ); |
|
566 | + } |
|
567 | + |
|
568 | + do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Adds a discount to the submission. |
|
573 | + * |
|
574 | + * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
575 | + * @since 1.0.19 |
|
576 | + */ |
|
577 | + public function add_discount( $discount ) { |
|
578 | + $this->discounts[ $discount['name'] ] = $discount; |
|
579 | + $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
580 | + $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * Removes a discount from the submission. |
|
585 | + * |
|
586 | + * @since 1.0.19 |
|
587 | + */ |
|
588 | + public function remove_discount( $name ) { |
|
589 | + |
|
590 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
591 | + $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
592 | + $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
593 | + unset( $this->discounts[ $name ] ); |
|
594 | + } |
|
595 | + |
|
596 | + } |
|
597 | + |
|
598 | + /** |
|
599 | + * Checks whether there is a discount code associated with this submission. |
|
600 | + * |
|
601 | + * @since 1.0.19 |
|
602 | + * @return bool |
|
603 | + */ |
|
604 | + public function has_discount_code() { |
|
605 | + return ! empty( $this->discounts['discount_code'] ); |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * Returns the discount code. |
|
610 | + * |
|
611 | + * @since 1.0.19 |
|
612 | + * @return string |
|
613 | + */ |
|
614 | + public function get_discount_code() { |
|
615 | + return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * Returns the discount. |
|
620 | + * |
|
621 | + * @since 1.0.19 |
|
622 | + */ |
|
623 | + public function get_discount() { |
|
624 | + return $this->totals['discount']['initial']; |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * Returns the recurring discount. |
|
629 | + * |
|
630 | + * @since 1.0.19 |
|
631 | + */ |
|
632 | + public function get_recurring_discount() { |
|
633 | + return $this->totals['discount']['recurring']; |
|
634 | + } |
|
635 | + |
|
636 | + /** |
|
637 | + * Returns all discounts. |
|
638 | + * |
|
639 | + * @since 1.0.19 |
|
640 | + */ |
|
641 | + public function get_discounts() { |
|
642 | + return $this->discounts; |
|
643 | + } |
|
644 | + |
|
645 | + /* |
|
646 | 646 | |-------------------------------------------------------------------------- |
647 | 647 | | Fees |
648 | 648 | |-------------------------------------------------------------------------- |
@@ -652,89 +652,89 @@ discard block |
||
652 | 652 | | fees. |
653 | 653 | */ |
654 | 654 | |
655 | - /** |
|
656 | - * Prepares the submission's fees. |
|
657 | - * |
|
658 | - * @since 1.0.19 |
|
659 | - */ |
|
660 | - public function process_fees() { |
|
661 | - |
|
662 | - $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
663 | - |
|
664 | - foreach ( $fees_processor->fees as $fee ) { |
|
665 | - $this->add_fee( $fee ); |
|
666 | - } |
|
667 | - |
|
668 | - do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
669 | - } |
|
670 | - |
|
671 | - /** |
|
672 | - * Adds a fee to the submission. |
|
673 | - * |
|
674 | - * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
675 | - * @since 1.0.19 |
|
676 | - */ |
|
677 | - public function add_fee( $fee ) { |
|
678 | - |
|
679 | - $this->fees[ $fee['name'] ] = $fee; |
|
680 | - $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
681 | - $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
682 | - |
|
683 | - } |
|
684 | - |
|
685 | - /** |
|
686 | - * Removes a fee from the submission. |
|
687 | - * |
|
688 | - * @since 1.0.19 |
|
689 | - */ |
|
690 | - public function remove_fee( $name ) { |
|
691 | - |
|
692 | - if ( isset( $this->fees[ $name ] ) ) { |
|
693 | - $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
694 | - $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
695 | - unset( $this->fees[ $name ] ); |
|
696 | - } |
|
697 | - |
|
698 | - } |
|
699 | - |
|
700 | - /** |
|
701 | - * Returns the fees. |
|
702 | - * |
|
703 | - * @since 1.0.19 |
|
704 | - */ |
|
705 | - public function get_fee() { |
|
706 | - return $this->totals['fees']['initial']; |
|
707 | - } |
|
708 | - |
|
709 | - /** |
|
710 | - * Returns the recurring fees. |
|
711 | - * |
|
712 | - * @since 1.0.19 |
|
713 | - */ |
|
714 | - public function get_recurring_fee() { |
|
715 | - return $this->totals['fees']['recurring']; |
|
716 | - } |
|
717 | - |
|
718 | - /** |
|
719 | - * Returns all fees. |
|
720 | - * |
|
721 | - * @since 1.0.19 |
|
722 | - */ |
|
723 | - public function get_fees() { |
|
724 | - return $this->fees; |
|
725 | - } |
|
726 | - |
|
727 | - /** |
|
728 | - * Checks if there are any fees for the form. |
|
729 | - * |
|
730 | - * @return bool |
|
731 | - * @since 1.0.19 |
|
732 | - */ |
|
733 | - public function has_fees() { |
|
734 | - return count( $this->fees ) !== 0; |
|
735 | - } |
|
736 | - |
|
737 | - /* |
|
655 | + /** |
|
656 | + * Prepares the submission's fees. |
|
657 | + * |
|
658 | + * @since 1.0.19 |
|
659 | + */ |
|
660 | + public function process_fees() { |
|
661 | + |
|
662 | + $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
663 | + |
|
664 | + foreach ( $fees_processor->fees as $fee ) { |
|
665 | + $this->add_fee( $fee ); |
|
666 | + } |
|
667 | + |
|
668 | + do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
669 | + } |
|
670 | + |
|
671 | + /** |
|
672 | + * Adds a fee to the submission. |
|
673 | + * |
|
674 | + * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
675 | + * @since 1.0.19 |
|
676 | + */ |
|
677 | + public function add_fee( $fee ) { |
|
678 | + |
|
679 | + $this->fees[ $fee['name'] ] = $fee; |
|
680 | + $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
681 | + $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
682 | + |
|
683 | + } |
|
684 | + |
|
685 | + /** |
|
686 | + * Removes a fee from the submission. |
|
687 | + * |
|
688 | + * @since 1.0.19 |
|
689 | + */ |
|
690 | + public function remove_fee( $name ) { |
|
691 | + |
|
692 | + if ( isset( $this->fees[ $name ] ) ) { |
|
693 | + $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
694 | + $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
695 | + unset( $this->fees[ $name ] ); |
|
696 | + } |
|
697 | + |
|
698 | + } |
|
699 | + |
|
700 | + /** |
|
701 | + * Returns the fees. |
|
702 | + * |
|
703 | + * @since 1.0.19 |
|
704 | + */ |
|
705 | + public function get_fee() { |
|
706 | + return $this->totals['fees']['initial']; |
|
707 | + } |
|
708 | + |
|
709 | + /** |
|
710 | + * Returns the recurring fees. |
|
711 | + * |
|
712 | + * @since 1.0.19 |
|
713 | + */ |
|
714 | + public function get_recurring_fee() { |
|
715 | + return $this->totals['fees']['recurring']; |
|
716 | + } |
|
717 | + |
|
718 | + /** |
|
719 | + * Returns all fees. |
|
720 | + * |
|
721 | + * @since 1.0.19 |
|
722 | + */ |
|
723 | + public function get_fees() { |
|
724 | + return $this->fees; |
|
725 | + } |
|
726 | + |
|
727 | + /** |
|
728 | + * Checks if there are any fees for the form. |
|
729 | + * |
|
730 | + * @return bool |
|
731 | + * @since 1.0.19 |
|
732 | + */ |
|
733 | + public function has_fees() { |
|
734 | + return count( $this->fees ) !== 0; |
|
735 | + } |
|
736 | + |
|
737 | + /* |
|
738 | 738 | |-------------------------------------------------------------------------- |
739 | 739 | | MISC |
740 | 740 | |-------------------------------------------------------------------------- |
@@ -742,119 +742,119 @@ discard block |
||
742 | 742 | | Extra submission functions. |
743 | 743 | */ |
744 | 744 | |
745 | - /** |
|
746 | - * Checks if this is the initial fetch. |
|
747 | - * |
|
748 | - * @return bool |
|
749 | - * @since 1.0.19 |
|
750 | - */ |
|
751 | - public function is_initial_fetch() { |
|
752 | - return empty( $this->data['initial_state'] ); |
|
753 | - } |
|
754 | - |
|
755 | - /** |
|
756 | - * Returns the total amount to collect for this submission. |
|
757 | - * |
|
758 | - * @since 1.0.19 |
|
759 | - */ |
|
760 | - public function get_total() { |
|
761 | - $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount(); |
|
762 | - return max( $total, 0 ); |
|
763 | - } |
|
764 | - |
|
765 | - /** |
|
766 | - * Returns the recurring total amount to collect for this submission. |
|
767 | - * |
|
768 | - * @since 1.0.19 |
|
769 | - */ |
|
770 | - public function get_recurring_total() { |
|
771 | - $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount(); |
|
772 | - return max( $total, 0 ); |
|
773 | - } |
|
774 | - |
|
775 | - /** |
|
776 | - * Whether payment details should be collected for this submission. |
|
777 | - * |
|
778 | - * @since 1.0.19 |
|
779 | - */ |
|
780 | - public function should_collect_payment_details() { |
|
781 | - $initial = $this->get_total(); |
|
782 | - $recurring = $this->get_recurring_total(); |
|
783 | - |
|
784 | - if ( $this->has_recurring == 0 ) { |
|
785 | - $recurring = 0; |
|
786 | - } |
|
787 | - |
|
788 | - $collect = $initial > 0 || $recurring > 0; |
|
789 | - return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
790 | - } |
|
791 | - |
|
792 | - /** |
|
793 | - * Returns the billing email of the user. |
|
794 | - * |
|
795 | - * @since 1.0.19 |
|
796 | - */ |
|
797 | - public function get_billing_email() { |
|
798 | - return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
799 | - } |
|
800 | - |
|
801 | - /** |
|
802 | - * Checks if the submitter has a billing email. |
|
803 | - * |
|
804 | - * @since 1.0.19 |
|
805 | - */ |
|
806 | - public function has_billing_email() { |
|
807 | - $billing_email = $this->get_billing_email(); |
|
808 | - return ! empty( $billing_email ) && is_email( $billing_email ); |
|
809 | - } |
|
810 | - |
|
811 | - /** |
|
812 | - * Returns the appropriate currency for the submission. |
|
813 | - * |
|
814 | - * @since 1.0.19 |
|
815 | - * @return string |
|
816 | - */ |
|
817 | - public function get_currency() { |
|
818 | - return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
819 | - } |
|
820 | - |
|
821 | - /** |
|
822 | - * Returns the raw submission data. |
|
823 | - * |
|
824 | - * @since 1.0.19 |
|
825 | - * @return array |
|
826 | - */ |
|
827 | - public function get_data() { |
|
828 | - return $this->data; |
|
829 | - } |
|
830 | - |
|
831 | - /** |
|
832 | - * Returns a field from the submission data |
|
833 | - * |
|
834 | - * @param string $field |
|
835 | - * @since 1.0.19 |
|
836 | - * @return mixed|null |
|
837 | - */ |
|
838 | - public function get_field( $field, $sub_array_key = null ) { |
|
839 | - return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
840 | - } |
|
841 | - |
|
842 | - /** |
|
843 | - * Checks if a required field is set. |
|
844 | - * |
|
845 | - * @since 1.0.19 |
|
846 | - */ |
|
847 | - public function is_required_field_set( $field ) { |
|
848 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
849 | - } |
|
850 | - |
|
851 | - /** |
|
852 | - * Formats an amount |
|
853 | - * |
|
854 | - * @since 1.0.19 |
|
855 | - */ |
|
856 | - public function format_amount( $amount ) { |
|
857 | - return wpinv_price( $amount, $this->get_currency() ); |
|
858 | - } |
|
745 | + /** |
|
746 | + * Checks if this is the initial fetch. |
|
747 | + * |
|
748 | + * @return bool |
|
749 | + * @since 1.0.19 |
|
750 | + */ |
|
751 | + public function is_initial_fetch() { |
|
752 | + return empty( $this->data['initial_state'] ); |
|
753 | + } |
|
754 | + |
|
755 | + /** |
|
756 | + * Returns the total amount to collect for this submission. |
|
757 | + * |
|
758 | + * @since 1.0.19 |
|
759 | + */ |
|
760 | + public function get_total() { |
|
761 | + $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount(); |
|
762 | + return max( $total, 0 ); |
|
763 | + } |
|
764 | + |
|
765 | + /** |
|
766 | + * Returns the recurring total amount to collect for this submission. |
|
767 | + * |
|
768 | + * @since 1.0.19 |
|
769 | + */ |
|
770 | + public function get_recurring_total() { |
|
771 | + $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount(); |
|
772 | + return max( $total, 0 ); |
|
773 | + } |
|
774 | + |
|
775 | + /** |
|
776 | + * Whether payment details should be collected for this submission. |
|
777 | + * |
|
778 | + * @since 1.0.19 |
|
779 | + */ |
|
780 | + public function should_collect_payment_details() { |
|
781 | + $initial = $this->get_total(); |
|
782 | + $recurring = $this->get_recurring_total(); |
|
783 | + |
|
784 | + if ( $this->has_recurring == 0 ) { |
|
785 | + $recurring = 0; |
|
786 | + } |
|
787 | + |
|
788 | + $collect = $initial > 0 || $recurring > 0; |
|
789 | + return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
790 | + } |
|
791 | + |
|
792 | + /** |
|
793 | + * Returns the billing email of the user. |
|
794 | + * |
|
795 | + * @since 1.0.19 |
|
796 | + */ |
|
797 | + public function get_billing_email() { |
|
798 | + return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
799 | + } |
|
800 | + |
|
801 | + /** |
|
802 | + * Checks if the submitter has a billing email. |
|
803 | + * |
|
804 | + * @since 1.0.19 |
|
805 | + */ |
|
806 | + public function has_billing_email() { |
|
807 | + $billing_email = $this->get_billing_email(); |
|
808 | + return ! empty( $billing_email ) && is_email( $billing_email ); |
|
809 | + } |
|
810 | + |
|
811 | + /** |
|
812 | + * Returns the appropriate currency for the submission. |
|
813 | + * |
|
814 | + * @since 1.0.19 |
|
815 | + * @return string |
|
816 | + */ |
|
817 | + public function get_currency() { |
|
818 | + return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
819 | + } |
|
820 | + |
|
821 | + /** |
|
822 | + * Returns the raw submission data. |
|
823 | + * |
|
824 | + * @since 1.0.19 |
|
825 | + * @return array |
|
826 | + */ |
|
827 | + public function get_data() { |
|
828 | + return $this->data; |
|
829 | + } |
|
830 | + |
|
831 | + /** |
|
832 | + * Returns a field from the submission data |
|
833 | + * |
|
834 | + * @param string $field |
|
835 | + * @since 1.0.19 |
|
836 | + * @return mixed|null |
|
837 | + */ |
|
838 | + public function get_field( $field, $sub_array_key = null ) { |
|
839 | + return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
840 | + } |
|
841 | + |
|
842 | + /** |
|
843 | + * Checks if a required field is set. |
|
844 | + * |
|
845 | + * @since 1.0.19 |
|
846 | + */ |
|
847 | + public function is_required_field_set( $field ) { |
|
848 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
849 | + } |
|
850 | + |
|
851 | + /** |
|
852 | + * Formats an amount |
|
853 | + * |
|
854 | + * @since 1.0.19 |
|
855 | + */ |
|
856 | + public function format_amount( $amount ) { |
|
857 | + return wpinv_price( $amount, $this->get_currency() ); |
|
858 | + } |
|
859 | 859 | |
860 | 860 | } |