PluginProbe
Cooked – Recipe Management / 1.16.1
Cooked – Recipe Management v1.16.1
1.16.1 1.16.0 1.15.0 trunk 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.12.0 1.13.0 1.14.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.15.1 1.7.15.3 1.7.15.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 All 37 releases
cooked / includes / class.cooked-measurements.php

class.cooked-measurements.php in Cooked – Recipe Management 1.16.1, at includes/class.cooked-measurements.php

766 lines 31.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cooked Measurement Functions
4 *
5 * @package Cooked
6 * @subpackage Measurement Functions
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 use NXP\MathExecutor;
14
15 /**
16 * Cooked_Measurements Class
17 *
18 * This class handles the Cooked Measurements.
19 *
20 * @since 1.0.0
21 */
22 class Cooked_Measurements {
23
24 public static function get() {
25
26 // Use the "cooked_measurements" filter to add your own measurements.
27 $measurements = apply_filters('cooked_measurements', [
28 'g' => [
29 'singular_abbr' => _x( 'g', 'Grams Abbreviation (Singular)', 'cooked' ),
30 'plural_abbr' => _x( 'g', 'Grams Abbreviation (Plural)', 'cooked' ),
31 'singular' => __( 'gram', 'cooked' ),
32 'plural' => __( 'grams', 'cooked' ),
33 'system' => 'metric',
34 'variations' => [ 'g', 'g.', 'gram', 'grams' ],
35 ],
36 'kg' => [
37 'singular_abbr' => _x('kg', 'Kilograms Abbreviation (Singular)', 'cooked'),
38 'plural_abbr' => _x('kg', 'Kilograms Abbreviation (Plural)', 'cooked'),
39 'singular' => __('kilogram', 'cooked'),
40 'plural' => __('kilograms', 'cooked'),
41 'system' => 'metric',
42 'variations' => ['kg', 'kg.', 'kilogram', 'kilograms'],
43 ],
44 'mg' => [
45 'singular_abbr' => __('mg', 'cooked'),
46 'plural_abbr' => __('mg', 'cooked'),
47 'singular' => __('milligram', 'cooked'),
48 'plural' => __('milligrams', 'cooked'),
49 'system' => 'metric',
50 'variations' => ['mg', 'mg.', 'milligram', 'milligrams'],
51 ],
52 'oz' => [
53 'singular_abbr' => __('oz', 'cooked'),
54 'plural_abbr' => __('oz', 'cooked'),
55 'singular' => __('ounce', 'cooked'),
56 'plural' => __('ounces', 'cooked'),
57 'system' => 'imperial',
58 'variations' => ['oz', 'oz.', 'ounce', 'ounces'],
59 ],
60 'floz' => [
61 'singular_abbr' => __('fl oz', 'cooked'),
62 'plural_abbr' => __('fl oz', 'cooked'),
63 'singular' => __('fluid ounce', 'cooked'),
64 'plural' => __('fluid ounces', 'cooked'),
65 'system' => 'imperial',
66 'variations' => ['fl oz', 'fl oz.', 'fl. oz.', 'fluid ounce', 'fluid ounces'],
67 ],
68 'cup' => [
69 'singular_abbr' => __('cup', 'cooked'),
70 'plural_abbr' => __('cups', 'cooked'),
71 'singular' => __('cup', 'cooked'),
72 'plural' => __('cups', 'cooked'),
73 'system' => 'imperial',
74 'variations' => ['c', 'c.', 'cup', 'cups'],
75 ],
76 'tsp' => [
77 'singular_abbr' => __('tsp', 'cooked'),
78 'plural_abbr' => __('tsp', 'cooked'),
79 'singular' => __('teaspoon', 'cooked'),
80 'plural' => __('teaspoons', 'cooked'),
81 'system' => 'imperial',
82 'variations' => ['t', 'tsp.', 'tsp', 'teaspoon', 'teaspoons'],
83 ],
84 'tbsp' => [
85 'singular_abbr' => __('tbsp', 'cooked'),
86 'plural_abbr' => __('tbsp', 'cooked'),
87 'singular' => __('tablespoon', 'cooked'),
88 'plural' => __('tablespoons', 'cooked'),
89 'system' => 'imperial',
90 'variations' => ['T', 'tbl.', 'tbl', 'tbs.', 'tbs', 'tbsp.', 'tbsp', 'tablespoon', 'tablespoons'],
91 ],
92 'dl' => [
93 'singular_abbr' => __('dl', 'cooked'),
94 'plural_abbr' => __('dl', 'cooked'),
95 'singular' => __('deciliter', 'cooked'),
96 'plural' => __('deciliters', 'cooked'),
97 'system' => 'metric',
98 'variations' => ['dl', 'dl.', 'deciliter', 'deciliters'],
99 ],
100 'ml' => [
101 'singular_abbr' => __('ml', 'cooked'),
102 'plural_abbr' => __('ml', 'cooked'),
103 'singular' => __('milliliter', 'cooked'),
104 'plural' => __('milliliters', 'cooked'),
105 'system' => 'metric',
106 'variations' => ['ml', 'ml.', 'mL', 'mL.', 'cc', 'milliliter', 'milliliters', 'millilitre', 'millilitres'],
107 ],
108 'l' => [
109 'singular_abbr' => __('l', 'cooked'),
110 'plural_abbr' => __('l', 'cooked'),
111 'singular' => __('liter', 'cooked'),
112 'plural' => __('liters', 'cooked'),
113 'system' => 'metric',
114 'variations' => ['l', 'l.', 'L', 'L.', 'liter', 'liters', 'litre', 'litres'],
115 ],
116 'stick' => [
117 'singular_abbr' => __('stick', 'cooked'),
118 'plural_abbr' => __('sticks', 'cooked'),
119 'singular' => __('stick', 'cooked'),
120 'plural' => __('sticks', 'cooked'),
121 'variations' => ['stick', 'sticks'],
122 ],
123 'lb' => [
124 'singular_abbr' => __('lb', 'cooked'),
125 'plural_abbr' => __('lbs', 'cooked'),
126 'singular' => __('pound', 'cooked'),
127 'plural' => __('pounds', 'cooked'),
128 'system' => 'imperial',
129 'variations' => ['lb', 'lbs', 'lb.', 'lbs.', 'pound', 'pounds'],
130 ],
131 'dash' => [
132 'singular_abbr' => __('dash', 'cooked'),
133 'plural_abbr' => __('dashes', 'cooked'),
134 'singular' => __('dash', 'cooked'),
135 'plural' => __('dashes', 'cooked'),
136 'variations' => ['dash', 'dashes'],
137 ],
138 'drop' => [
139 'singular_abbr' => __('drop', 'cooked'),
140 'plural_abbr' => __('drops', 'cooked'),
141 'singular' => __('drop', 'cooked'),
142 'plural' => __('drops', 'cooked'),
143 'variations' => ['drop', 'drops'],
144 ],
145 'gal' => [
146 'singular_abbr' => __('gal', 'cooked'),
147 'plural_abbr' => __('gals', 'cooked'),
148 'singular' => __('gallon', 'cooked'),
149 'plural' => __('gallons', 'cooked'),
150 'system' => 'imperial',
151 'variations' => ['G', 'G.', 'gal', 'gal.', 'gallon', 'gallons'],
152 ],
153 'pinch' => [
154 'singular_abbr' => __('pinch', 'cooked'),
155 'plural_abbr' => __('pinches', 'cooked'),
156 'singular' => __('pinch', 'cooked'),
157 'plural' => __('pinches', 'cooked'),
158 'variations' => ['pinch', 'pinches'],
159 ],
160 'pt' => [
161 'singular_abbr' => __('pt', 'cooked'),
162 'plural_abbr' => __('pt', 'cooked'),
163 'singular' => __('pint', 'cooked'),
164 'plural' => __('pints', 'cooked'),
165 'system' => 'imperial',
166 'variations' => ['p', 'p.', 'pt', 'pt.', 'pts', 'pts.', 'fl pt', 'fl. pt.', 'pint', 'pints'],
167 ],
168 'qt' => [
169 'singular_abbr' => __('qt', 'cooked'),
170 'plural_abbr' => __('qts', 'cooked'),
171 'singular' => __('quart', 'cooked'),
172 'plural' => __('quarts', 'cooked'),
173 'system' => 'imperial',
174 'variations' => ['q', 'q.', 'qt', 'qt.', 'qts', 'qts.', 'fl qt', 'fl. qt.', 'quart', 'quarts'],
175 ],
176 'drizzle' => [
177 'singular_abbr' => __('drizzle', 'cooked'),
178 'plural_abbr' => __('drizzle', 'cooked'),
179 'singular' => __('Drizzle', 'cooked'),
180 'plural' => __('Drizzle', 'cooked'),
181 'variations' => ['drizzle'],
182 ],
183 'clove' => [
184 'singular_abbr' => __('clove', 'cooked'),
185 'plural_abbr' => __('cloves', 'cooked'),
186 'singular' => __('clove', 'cooked'),
187 'plural' => __('cloves', 'cooked'),
188 'variations' => [ 'clove', 'cloves' ],
189 ],
190 'jar' => [
191 'singular_abbr' => __('jar', 'cooked'),
192 'plural_abbr' => __('jars', 'cooked'),
193 'singular' => __('jar', 'cooked'),
194 'plural' => __('jars', 'cooked'),
195 'variations' => [ 'jar', 'jars' ],
196 ],
197 'can' => [
198 'singular_abbr' => __('can', 'cooked'),
199 'plural_abbr' => __('cans', 'cooked'),
200 'singular' => __('can', 'cooked'),
201 'plural' => __('cans', 'cooked'),
202 'variations' => [ 'can', 'cans' ],
203 ],
204 ]);
205
206 return $measurements;
207 }
208
209 public static function nutrition_facts() {
210 global $_cooked_settings;
211
212 // Use the "cooked_nutrition_facts" filter to add your own nutrition facts.
213 // Reference: https://www.fda.gov/food/nutrition-facts-label/daily-value-nutrition-and-supplement-facts-labels
214 $unit_g = __( 'g', 'cooked' );
215 $unit_mg = __( 'mg', 'cooked' );
216 $unit_mcg = __( 'mcg', 'cooked' );
217
218 $nutrition_facts = apply_filters('cooked_nutrition_facts', [
219 'top' => [
220 'servings' => [
221 'name' => __('Servings', 'cooked'),
222 'type' => 'number'
223 ],
224 'serving_size' => [
225 'name' => __('Serving size', 'cooked'),
226 'type' => 'text'
227 ],
228 ],
229
230 'mid' => [
231 'calories' => [
232 'name' => __('Calories', 'cooked'),
233 'type' => 'number'
234 ]
235 ],
236
237 'main' => [
238 'fat' => [
239 'name' => __('Total Fat', 'cooked'),
240 'type' => 'number',
241 'measurement' => $unit_g,
242 'pdv' => apply_filters('cooked_pdv_fat', 78),
243 'subs' => [
244 'sat_fat' => [
245 'name' => __('Saturated Fat', 'cooked'),
246 'type' => 'number',
247 'measurement' => $unit_g,
248 'pdv' => apply_filters('cooked_pdv_satfat', 20)
249 ],
250 'trans_fat' => [
251 'name' => __('Trans Fat', 'cooked'),
252 'nutrition_info_name' => __('<i>Trans</i> Fat', 'cooked'),
253 'type' => 'number',
254 'measurement' => $unit_g
255 ],
256 'monounsaturated_fat' => [
257 'name' => __('Monounsaturated Fat', 'cooked'),
258 'type' => 'number',
259 'measurement' => $unit_g
260 ],
261 'polyunsaturated_fat' => [
262 'name' => __('Polyunsaturated Fat', 'cooked'),
263 'type' => 'number',
264 'measurement' => $unit_g
265 ]
266 ]
267 ],
268 'cholesterol' => [
269 'name' => __('Cholesterol', 'cooked'),
270 'type' => 'number',
271 'measurement' => $unit_mg,
272 'pdv' => apply_filters('cooked_pdv_cholesterol', 300)
273 ],
274 'sodium' => [
275 'name' => __('Sodium', 'cooked'),
276 'type' => 'number',
277 'measurement' => $unit_mg,
278 'pdv' => apply_filters('cooked_pdv_sodium', 2300)
279 ],
280 'carbs' => [
281 'name' => (isset($_cooked_settings['carb_format']) && $_cooked_settings['carb_format'] == 'total' ? __('Total Carbohydrate', 'cooked') : __('Net Carbohydrate', 'cooked')),
282 'type' => 'number',
283 'measurement' => $unit_g,
284 'pdv' => apply_filters('cooked_pdv_carbs', 275),
285 'subs' => [
286 'fiber' => [
287 'name' => __('Dietary Fiber', 'cooked'),
288 'type' => 'number',
289 'measurement' => $unit_g,
290 'pdv' => apply_filters('cooked_pdv_fiber', 28)
291 ],
292 'sugars' => [
293 'name' => __('Total Sugars', 'cooked'),
294 'type' => 'number',
295 'measurement' => $unit_g,
296 //'pdv' => apply_filters('cooked_pdv_sugars', 28)
297 ],
298 'added_sugars' => [
299 'name' => __('Added Sugars', 'cooked'),
300 'type' => 'number',
301 'measurement' => $unit_g,
302 'pdv' => apply_filters('cooked_pdv_added_sugars', 50)
303 ]
304 ]
305 ],
306 'protein' => [
307 'name' => __('Protein', 'cooked'),
308 'type' => 'number',
309 'measurement' => $unit_g,
310 //'pdv' => apply_filters('cooked_pdv_protein', 50)
311 ]
312 ],
313
314 'bottom' => [
315 'vitamin_a' => [
316 'name' => __('Vitamin A', 'cooked'),
317 'type' => 'number',
318 'measurement' => $unit_mcg,
319 'pdv' => apply_filters('cooked_pdv_vitamin_a', 900)
320 ],
321 'vitamin_c' => [
322 'name' => __('Vitamin C', 'cooked'),
323 'type' => 'number',
324 'measurement' => $unit_mg,
325 'pdv' => apply_filters('cooked_pdv_vitamin_c', 90)
326 ],
327 'calcium' => [
328 'name' => __('Calcium', 'cooked'),
329 'type' => 'number',
330 'measurement' => $unit_mg,
331 'pdv' => apply_filters('cooked_pdv_calcium', 1300)
332 ],
333 'iron' => [
334 'name' => __('Iron', 'cooked'),
335 'type' => 'number',
336 'measurement' => $unit_mg,
337 'pdv' => apply_filters('cooked_pdv_iron', 18)
338 ],
339 'potassium' => [
340 'name' => __('Potassium', 'cooked'),
341 'type' => 'number',
342 'measurement' => $unit_mg,
343 'pdv' => apply_filters('cooked_pdv_potassium', 4700)
344 ],
345 'vitamin_d' => [
346 'name' => __('Vitamin D', 'cooked'),
347 'type' => 'number',
348 'measurement' => $unit_mcg,
349 'pdv' => apply_filters('cooked_pdv_vitamin_d', 20)
350 ],
351 'vitamin_e' => [
352 'name' => __('Vitamin E', 'cooked'),
353 'type' => 'number',
354 'measurement' => $unit_mg,
355 'pdv' => apply_filters('cooked_pdv_vitamin_e', 15)
356 ],
357 'vitamin_k' => [
358 'name' => __('Vitamin K', 'cooked'),
359 'type' => 'number',
360 'measurement' => $unit_mcg,
361 'pdv' => apply_filters('cooked_pdv_vitamin_k', 120)
362 ],
363 'thiamin' => [
364 'name' => __('Thiamin', 'cooked'),
365 'type' => 'number',
366 'measurement' => $unit_mg,
367 'pdv' => apply_filters('cooked_pdv_thiamin', 1.2)
368 ],
369 'riboflavin' => [
370 'name' => __('Riboflavin', 'cooked'),
371 'type' => 'number',
372 'measurement' => $unit_mg,
373 'pdv' => apply_filters('cooked_pdv_riboflavin', 1.3)
374 ],
375 'niacin' => [
376 'name' => __('Niacin', 'cooked'),
377 'type' => 'number',
378 'measurement' => $unit_mg,
379 'pdv' => apply_filters('cooked_pdv_niacin', 16)
380 ],
381 'vitamin_b6' => [
382 'name' => __('Vitamin B6', 'cooked'),
383 'type' => 'number',
384 'measurement' => $unit_mg,
385 'pdv' => apply_filters('cooked_pdv_vitamin_b6', 1.7)
386 ],
387 'folate' => [
388 'name' => __('Folate', 'cooked'),
389 'type' => 'number',
390 'measurement' => $unit_mcg,
391 'pdv' => apply_filters('cooked_pdv_folate', 400)
392 ],
393 'vitamin_b12' => [
394 'name' => __('Vitamin B12', 'cooked'),
395 'type' => 'number',
396 'measurement' => $unit_mg,
397 'pdv' => apply_filters('cooked_pdv_vitamin_b12', 2.4)
398 ],
399 'biotin' => [
400 'name' => __('Biotin', 'cooked'),
401 'type' => 'number',
402 'measurement' => $unit_mcg,
403 'pdv' => apply_filters('cooked_pdv_biotin', 30)
404 ],
405 'pantothenic_acid' => [
406 'name' => __('Pantothenic Acid', 'cooked'),
407 'type' => 'number',
408 'measurement' => $unit_mg,
409 'pdv' => apply_filters('cooked_pdv_pantothenic_acid', 5)
410 ],
411 'phosphorus' => [
412 'name' => __('Phosphorus', 'cooked'),
413 'type' => 'number',
414 'measurement' => $unit_mg,
415 'pdv' => apply_filters('cooked_pdv_phosphorus', 1250)
416 ],
417 'iodine' => [
418 'name' => __('Iodine', 'cooked'),
419 'type' => 'number',
420 'measurement' => $unit_mcg,
421 'pdv' => apply_filters('cooked_pdv_iodine', 150)
422 ],
423 'magnesium' => [
424 'name' => __('Magnesium', 'cooked'),
425 'type' => 'number',
426 'measurement' => $unit_mg,
427 'pdv' => apply_filters('cooked_pdv_magnesium', 420)
428 ],
429 'zinc' => [
430 'name' => __('Zinc', 'cooked'),
431 'type' => 'number',
432 'measurement' => $unit_mg,
433 'pdv' => apply_filters('cooked_pdv_zinc', 11)
434 ],
435 'selenium' => [
436 'name' => __('Selenium', 'cooked'),
437 'type' => 'number',
438 'measurement' => $unit_mcg,
439 'pdv' => apply_filters('cooked_pdv_selenium', 55)
440 ],
441 'copper' => [
442 'name' => __('Copper', 'cooked'),
443 'type' => 'number',
444 'measurement' => $unit_mg,
445 'pdv' => apply_filters('cooked_pdv_copper', 0.9)
446 ],
447 'manganese' => [
448 'name' => __('Manganese', 'cooked'),
449 'type' => 'number',
450 'measurement' => $unit_mg,
451 'pdv' => apply_filters('cooked_pdv_manganese', 2.3)
452 ],
453 'chromium' => [
454 'name' => __('Chromium', 'cooked'),
455 'type' => 'number',
456 'measurement' => $unit_mcg,
457 'pdv' => apply_filters('cooked_pdv_chromium', 35)
458 ],
459 'molybdenum' => [
460 'name' => __('Molybdenum', 'cooked'),
461 'type' => 'number',
462 'measurement' => $unit_mcg,
463 'pdv' => apply_filters('cooked_pdv_molybdenum', 45)
464 ],
465 'chloride' => [
466 'name' => __('Chloride', 'cooked'),
467 'type' => 'number',
468 'measurement' => $unit_mg,
469 'pdv' => apply_filters('cooked_pdv_chloride', 2300)
470 ]
471 ]
472 ]);
473
474 return $nutrition_facts;
475 }
476
477 public static function singular_plural( $singular_text, $plural_text, $count ) {
478 if ($count <= 1 ):
479 return $singular_text;
480 else:
481 return $plural_text;
482 endif;
483 }
484
485 public function cleanup_amount( $amount ) {
486 $fractions = self::get_fraction_array();
487
488 foreach( $fractions['fractions_a'] as $key => $f ):
489 $amount = str_replace( $f, $fractions['fractions_b'][$key], $amount);
490 endforeach;
491
492 $amount_parts = explode( ' ', $amount );
493 if ( isset($amount_parts[1]) ):
494 $amount_parts[0] = preg_replace("/[^0-9\/\.\,]/","",$amount_parts[0]);
495 $amount_parts[1] = preg_replace("/[^0-9\/\.\,]/","",$amount_parts[1]);
496 $amount = $amount_parts[0] . ' ' . $amount_parts[1];
497 else:
498 $amount = preg_replace("/[^0-9\/\.\,]/","",$amount);
499 endif;
500
501 $amount = self::locale_formatted( $amount );
502
503 return $amount;
504 }
505
506 public function locale_formatted( $amount ) {
507 global $wp_locale;
508
509 if ( isset( $wp_locale ) ) {
510 $amount = str_replace( $wp_locale->number_format['decimal_point'], '.', str_replace( $wp_locale->number_format['thousands_sep'], '', $amount) );
511 } else {
512 $amount = str_replace( ',', '', $amount);
513 }
514
515 return $amount;
516 }
517
518 public function format_amount( $float_amount = 0, $format = 'fraction' ) {
519 if ( $format == 'decimal' ):
520 $amount = $float_amount ? number_format_i18n( floatval( $float_amount ), 2 ) : 0;
521 else:
522 $float_parts = explode( '.', $float_amount );
523 if ( isset($float_parts[1]) ):
524 $float_parts[0] = preg_replace("/[^0-9\/\.]/","",$float_parts[0]);
525 $float_parts[1] = preg_replace("/[^0-9\/\.]/","",$float_parts[1]);
526 $decimal_part = $float_amount - $float_parts[0];
527 if ( $decimal_part > 0.075 ):
528 $fraction = self::float2rat($decimal_part);
529 $fraction = self::fraction_cleaner($fraction);
530 $allowed_fractions = self::get_fraction_array();
531 foreach( $allowed_fractions['fractions_b'] as $key => $f ):
532 $fraction = str_replace( $f, $allowed_fractions['fractions_c'][$key], $fraction);
533 endforeach;
534 $amount = ( $float_parts[0] ? $float_parts[0] . ' ' . $fraction : $fraction );
535 else:
536 $amount = preg_replace("/[^0-9\/\.]/","",$float_parts[0]);
537 endif;
538 else:
539 $amount = preg_replace("/[^0-9\/\.]/","",$float_parts[0]);
540 endif;
541 endif;
542
543 return $amount;
544 }
545
546 public function math($expression = false) {
547 if ($expression) {
548 $expression = preg_replace( '/[^0-9\+\-\*\/\(\)\.\,]/', '', esc_html($expression) );
549
550 $invalid = '/' == substr($expression, -1) ? true : false; // More checks can be done here if needed.
551
552 if (!$invalid) {
553 try {
554 $mathExecutor = new MathExecutor();
555 return $mathExecutor->execute($expression);
556 } catch (Exception $e) {
557 return 0;
558 }
559 } else {
560 return 0;
561 }
562 } else {
563 return 0;
564 }
565 }
566
567 public function calculate($amount, $type = 'decimal') {
568 if ($type === 'decimal') {
569 $amount_parts = explode(' ', $amount);
570 $total_parts = count($amount_parts);
571
572 if ( $total_parts === 1 ) {
573 $amount = self::math( $amount );
574 $amount = floatval( $amount );
575 } elseif ( $total_parts === 2 ) {
576 $full_part = floatval( $amount_parts[0] );
577 $fractional_part = floatval( self::math( $amount_parts[1] ) );
578 $amount = $full_part + $fractional_part;
579 $amount = floatval( $amount );
580 } else {
581 $amount = floatval( $amount );
582 }
583 } else {
584 $amount_parts = explode('.', $amount);
585 $total_parts = count($amount_parts);
586
587 if ($total_parts === 2) {
588 $full_part = intval($amount_parts[0]);
589 $fractional_part = self::float2rat($amount - $full_part);
590 if ($full_part === 0 && $fractional_part) {
591 $amount = $fractional_part;
592 } elseif ($fractional_part) {
593 $amount = ($fractional_part == 1) ? $full_part + $fractional_part : "$full_part $fractional_part";
594 } else {
595 $amount = $full_part;
596 }
597 } else {
598 if ($total_parts !== 1) {
599 $amount = self::float2rat($amount);
600 }
601 }
602
603 }
604
605 return $amount;
606 }
607
608 public function fraction_cleaner($fraction) {
609 $fraction_parts = explode('/', $fraction);
610 $decimal = $fraction_parts[0] / $fraction_parts[1];
611
612 if ($decimal < 1):
613 $decimal_array = [0.125, 0.166, 0.200, 0.250, 0.333, 0.500, 0.666, 0.750, 0.875];
614 $closest_decimal = self::get_closest_decimal( $decimal, $decimal_array );
615
616 switch ($closest_decimal):
617 case 0.125:
618 return '1/8';
619 case 0.166:
620 return '1/6';
621 case 0.200:
622 return '1/5';
623 case 0.250:
624 return '1/4';
625 case 0.333:
626 return '1/3';
627 case 0.500:
628 return '1/2';
629 case 0.666:
630 return '2/3';
631 case 0.750:
632 return '3/4';
633 case 0.875:
634 return '7/8';
635 endswitch;
636 else:
637 return self::calculate($decimal, 'fraction');
638 endif;
639 }
640
641 public function get_closest_decimal($search, $arr) {
642 $closest = null;
643 foreach ($arr as $item) {
644 if ($closest === null || abs($search - $closest) > abs($item - $search)) {
645 $closest = $item;
646 }
647 }
648 return $closest;
649 }
650
651 public function float2rat($n, $tolerance = 1.e-6) {
652 $h1=1; $h2=0;
653 $k1=0; $k2=1;
654 $b = 1/$n;
655 do {
656 $b = 1/$b;
657 $a = floor($b);
658 $aux = $h1; $h1 = $a*$h1+$h2; $h2 = $aux;
659 $aux = $k1; $k1 = $a*$k1+$k2; $k2 = $aux;
660 $b = $b-$a;
661 } while (abs($n-$h1/$k1) > $n*$tolerance);
662
663 if ( $h1/$k1 < 0.125 ):
664 return "1/8";
665 else:
666 return "$h1/$k1";
667 endif;
668 }
669
670 public function get_fraction_array() {
671 $fractions_a = [
672 ['�
673 �', '&#8539;', '&#215B;', '&frac18;'],
674 ['�
675 �', '&#8537;', '&#x2159;', '&frac16;'],
676 ['�
677 �', '&#8533;', '&#x2155;', '&frac15;'],
678 ['¼', '&#188;', '&#xBC;', '&frac14;'],
679 ['�
680 �', '&#8531;', '&#2153;', '&frac13;'],
681 ['½', '&#189;', '&#xBD;', '&frac12;'],
682 ['�
683 �', '&#8532;', '&#2154;', '&frac23;'],
684 ['�
685 �', '&#8541;', '&#x215D;', '&frac58;'],
686 ['¾', '&#190;', '&#xBE;', '&frac34;'],
687 ['�
688 �', '&#8542;', '&#215E;', '&frac78;']
689 ];
690
691 $fractions_b = [
692 '1/8',
693 '1/6',
694 '1/5',
695 '1/4',
696 '1/3',
697 '1/2',
698 '2/3',
699 '5/8',
700 '3/4',
701 '7/8',
702 ];
703
704 $fractions_c = [
705 '&#8539;',
706 '&#8537;',
707 '&#8533;',
708 '&#188;',
709 '&#8531;',
710 '&#189;',
711 '&#8532;',
712 '&#8541;',
713 '&#190;',
714 '&#8542;'
715 ];
716
717 $fraction_array = ['fractions_a' => $fractions_a, 'fractions_b' => $fractions_b, 'fractions_c' => $fractions_c];
718 return $fraction_array;
719 }
720
721 public static function time_format( $minutes, $format = 'default' ) {
722 ob_start();
723
724 if ( $minutes < 60 ):
725 if ( $format === 'iso' ):
726 return 'PT0H'.intval( $minutes ).'M';
727 else:
728 /* translators: singular and plural number of minutes (shorthand) */
729 echo esc_html( self::singular_plural( sprintf( esc_html__( '%d min','cooked' ), number_format_i18n($minutes) ), sprintf( esc_html__( '%d mins','cooked' ), number_format_i18n($minutes) ), $minutes ) );
730 endif;
731 elseif ( $minutes < 1440 ):
732 $hours = floor( $minutes / 60 );
733 $minutes_left = $minutes - ( $hours * 60 );
734 if ( $format === 'iso' ):
735 return 'PT'.intval( $hours ).'H'.( $minutes_left ? intval( $minutes_left ) : 0 ).'M';
736 else:
737 /* translators: singular and plural number of hours (shorthand) */
738 echo esc_html( self::singular_plural( sprintf( esc_html__( '%d hr','cooked' ), number_format_i18n($hours) ), sprintf( esc_html__( '%d hrs','cooked' ), number_format_i18n($hours) ), $hours ) );
739 /* translators: singular and plural number of minutes (shorthand) */
740 echo ( $minutes_left ? '&nbsp;' . esc_html( self::singular_plural( sprintf( esc_html__( '%d min','cooked' ), number_format_i18n($minutes_left) ), sprintf( esc_html__( '%d mins','cooked' ), number_format_i18n($minutes_left) ), $minutes_left ) ) : '' );
741 endif;
742 else:
743 $days = floor( $minutes / 24 / 60 );
744 $hours_left = 0;
745 $minutes_left = $minutes - ( $days * 24 * 60 );
746 if ( $minutes_left > 60 ):
747 $hours_left = floor( $minutes_left / 60 );
748 $minutes_left = $minutes_left - ( $hours_left * 60 );
749 endif;
750 if ( $format === 'iso' ):
751 return 'P'.intval( $days ).'DT'.( $hours_left ? intval( $hours_left ) : 0 ).'H'.( $minutes_left ? intval( $minutes_left ) : 0 ).'M';
752 else:
753 /* translators: singular and plural number of days */
754 echo esc_html( self::singular_plural( sprintf( esc_html__( '%d day','cooked' ), number_format_i18n($days) ), sprintf( esc_html__( '%d days','cooked' ), number_format_i18n($days) ), $days ) );
755 /* translators: singular and plural number of hours (shorthand) */
756 echo ( $hours_left ? '&nbsp;' . esc_html( self::singular_plural( sprintf( esc_html__( '%d hr','cooked' ), number_format_i18n($hours_left) ), sprintf( esc_html__( '%d hrs','cooked' ), number_format_i18n($hours_left) ), $hours_left ) ) : '' );
757 /* translators: singular and plural number of minutes (shorthand) */
758 echo ( $minutes_left ? '&nbsp;' . esc_html( self::singular_plural( sprintf( esc_html__( '%d min','cooked' ), number_format_i18n($minutes_left) ), sprintf( esc_html__( '%d mins','cooked' ), number_format_i18n($minutes_left) ), $minutes_left ) ) : '' );
759 endif;
760 endif;
761
762 return ob_get_clean();
763 }
764
765 }
766