PluginProbe
WPSSO Core – Complete Schema Markup and Meta Tags / 22.7.0
WPSSO Core – Complete Schema Markup and Meta Tags v22.7.0
22.7.0 22.6.1 22.6.0 22.5.3 22.5.2 22.5.1 22.4.0 22.3.0 22.2.1 22.2.0 22.1.3 22.1.2 22.1.1 22.1.0 22.0.0 21.13.3 21.13.2 trunk 21.13.1
← All changes | lib/util-units.php +467 -0 22.5.222.7.0 View file →
@@ -1,0 +1,467 @@
1 +<?php
2 +/*
3 + * License: GPLv3
4 + * License URI: https://www.gnu.org/licenses/gpl.txt
5 + * Copyright 2012-2026 Jean-Sebastien Morisset (https://wpsso.com/)
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 +
10 + die( 'These aren\'t the droids you\'re looking for.' );
11 +}
12 +
13 +if ( ! defined( 'WPSSO_PLUGINDIR' ) ) {
14 +
15 + die( 'Do. Or do not. There is no try.' );
16 +}
17 +
18 +if ( ! class_exists( 'WpssoUtilUnits' ) ) {
19 +
20 + class WpssoUtilUnits {
21 +
22 + private $p; // Wpsso class object.
23 +
24 + /*
25 + * Instantiated by WpssoUtil->__construct().
26 + */
27 + public function __construct( &$plugin ) {
28 +
29 + $this->p =& $plugin;
30 +
31 + if ( $this->p->debug->enabled ) {
32 +
33 + $this->p->debug->mark();
34 + }
35 + }
36 +
37 + public static function get_convert( $value, $to, $from = '' ) {
38 +
39 + $to = 'lbs' === $to ? 'lb' : $to; // WooCommerce uses 'lbs' and WPSSO uses 'lb'.
40 + $from = 'lbs' === $from ? 'lb' : $from; // WooCommerce uses 'lbs' and WPSSO uses 'lb'.
41 +
42 + $dimension_units = self::get_dimension_units(); // Uses a local cache.
43 + $fl_volume_units = self::get_fluid_volume_units(); // Uses a local cache.
44 + $weight_units = self::get_weight_units(); // Uses a local cache.
45 +
46 + if ( isset( $dimension_units[ $to ] ) ) {
47 +
48 + if ( '' === $from || isset( $dimension_units[ $from ] ) ) {
49 +
50 + return self::get_dimension_convert( $value, $to, $from );
51 + }
52 +
53 + } elseif ( isset( $fl_volume_units[ $to ] ) ) {
54 +
55 + if ( '' === $from || isset( $fluid_value_units[ $from ] ) ) {
56 +
57 + return self::get_fluid_value_convert( $value, $to, $from );
58 + }
59 +
60 + } elseif ( isset( $weight_units[ $to ] ) ) {
61 +
62 + if ( '' === $from || isset( $weight_units[ $from ] ) ) {
63 +
64 + return self::get_weight_convert( $value, $to, $from );
65 + }
66 + }
67 +
68 + return false;
69 + }
70 +
71 + public static function get_mixed_text( $mixed_key ) {
72 +
73 + $type = self::get_mixed_type( $mixed_key );
74 +
75 + switch( $type ) {
76 +
77 + case 'dimension':
78 + case 'length':
79 + case 'width':
80 + case 'height':
81 +
82 + return self::get_dimension_text();
83 +
84 + case 'fluid_volume':
85 +
86 + return self::get_fluid_volume_text();
87 +
88 + case 'weight':
89 +
90 + return self::get_weight_text();
91 + }
92 +
93 + return '';
94 + }
95 +
96 + public static function get_mixed_label( $mixed_key ) {
97 +
98 + $type = self::get_mixed_type( $mixed_key );
99 +
100 + switch( $type ) {
101 +
102 + case 'dimension':
103 + case 'length':
104 + case 'width':
105 + case 'height':
106 +
107 + return self::get_dimension_label();
108 +
109 + case 'fluid_volume':
110 +
111 + return self::get_fluid_volume_label();
112 +
113 + case 'weight':
114 +
115 + return self::get_weight_label();
116 + }
117 +
118 + return '';
119 + }
120 +
121 + public static function get_mixed_type( $mixed_key ) {
122 +
123 + static $local_cache = array();
124 +
125 + if ( isset( $local_cache[ $mixed_key ] ) ) {
126 +
127 + return $local_cache[ $mixed_key ];
128 + }
129 +
130 + $count = null;
131 + $match_key = str_replace( ':', '_', $mixed_key ); // Fix for meta tag names.
132 + $match_key = preg_replace( '/_(value|units)$/', '_', $match_key, $limit = -1, $count );
133 +
134 + switch( $match_key ) {
135 +
136 + case 'dimension':
137 + case 'length':
138 + case 'width':
139 + case 'height':
140 + case ( false !== strpos( $match_key, '_length_' ) ? true : false ):
141 + case ( false !== strpos( $match_key, '_width_' ) ? true : false ):
142 + case ( false !== strpos( $match_key, '_height_' ) ? true : false ):
143 +
144 + return $local_cache[ $mixed_key ] = 'dimension';
145 +
146 + case 'fluid_volume':
147 + case ( false !== strpos( $match_key, '_fluid_volume_' ) ? true : false ):
148 +
149 + return $local_cache[ $mixed_key ] = 'fluid_volume';
150 +
151 + case 'weight':
152 + case ( false !== strpos( $match_key, '_weight_' ) ? true : false ):
153 +
154 + return $local_cache[ $mixed_key ] = 'weight';
155 + }
156 +
157 + return $local_cache[ $mixed_key ] = '';
158 + }
159 +
160 + /*
161 + * Dimensions.
162 + *
163 + * See https://support.google.com/merchants/answer/11018531.
164 + */
165 + public static function get_dimension_text() {
166 +
167 + $wpsso =& Wpsso::get_instance();
168 +
169 + return isset( $wpsso->options[ 'og_def_dimension_units' ] ) ? $wpsso->options[ 'og_def_dimension_units' ] : 'cm';
170 + }
171 +
172 + public static function get_dimension_label( $key = '' ) {
173 +
174 + $key = empty( $key ) ? self::get_dimension_text() : $key;
175 + $units = self::get_dimension_units(); // Returns translated labels.
176 +
177 + return isset( $units[ $key ] ) ? $units[ $key ] : '';
178 + }
179 +
180 + /*
181 + * See wc_get_dimension() from woocommerce/includes/wc-formatting-functions.php.
182 + */
183 + public static function get_dimension_units() {
184 +
185 + static $local_cache = null;
186 +
187 + if ( null === $local_cache ) {
188 +
189 + $local_cache = array(
190 + 'mm' => _x( 'mm', 'option value', 'wpsso' ), // Millimeter.
191 + 'cm' => _x( 'cm', 'option value', 'wpsso' ), // Centimeter.
192 + 'm' => _x( 'm', 'option value', 'wpsso' ), // Meter.
193 + 'in' => _x( 'in', 'option value', 'wpsso' ), // Inch.
194 + 'ft' => _x( 'ft', 'option value', 'wpsso' ), // Foot.
195 + 'yd' => _x( 'yd', 'option value', 'wpsso' ), // Yard.
196 + );
197 +
198 + $local_cache = apply_filters( 'wpsso_dimension_units', $local_cache );
199 + }
200 +
201 + return $local_cache;
202 + }
203 +
204 + public static function get_dimension_convert( $value, $to, $from = '' ) {
205 +
206 + $value = (float) $value;
207 + $from = empty( $from ) ? self::get_dimension_text() : $from;
208 +
209 + if ( $from !== $to ) { // Just in case.
210 +
211 + /*
212 + * Convert dimension to cm first.
213 + */
214 + switch ( $from ) {
215 +
216 + /*
217 + * Metric units.
218 + */
219 + case 'mm': $value *= 0.1; break; // Millimeter to Centimeter.
220 + case 'cm': $value *= 1; break; // Centimeter to Centimeter.
221 + case 'm': $value *= 100; break; // Meter to Centimeter.
222 + case 'km': $value *= 100000; break; // Kilometer to Centimeter.
223 +
224 + /*
225 + * Imperial units.
226 + */
227 + case 'in': $value *= 2.54; break; // Inch to Centimeter.
228 + case 'ft': $value *= 30.48; break; // Foot to Centimeter.
229 + case 'yd': $value *= 91.44; break; // Yard to Centimeter.
230 + case 'mi': $value *= 160934.4; break; // Mile to Centimeter.
231 + }
232 +
233 + /*
234 + * Convert dimension from cm to desired output.
235 + */
236 + switch ( $to ) {
237 +
238 + /*
239 + * Metric units.
240 + */
241 + case 'mm': $value *= 10; break; // Centimeter to Millimeter.
242 + case 'cm': $value *= 1; break; // Centimeter to Centimeter.
243 + case 'm': $value *= 0.01; break; // Centimeter to Meter.
244 + case 'km': $value *= 0.00001; break; // Centimeter to Kilometer.
245 +
246 + /*
247 + * Imperial units.
248 + */
249 + case 'in': $value *= 0.3937007874; break; // Centimeter to Inch.
250 + case 'ft': $value *= 0.03280839895; break; // Centimeter to Foot.
251 + case 'yd': $value *= 0.010936132983; break; // Centimeter to Yard.
252 + case 'mi': $value *= 0.0000062137119224; break; // Centimeter to Mile.
253 + }
254 + }
255 +
256 + return ( $value < 0 ) ? 0 : $value;
257 + }
258 +
259 + /*
260 + * Fluid volumes.
261 + */
262 + public static function get_fluid_volume_text() {
263 +
264 + $wpsso =& Wpsso::get_instance();
265 +
266 + return isset( $wpsso->options[ 'og_def_fluid_volume_units' ] ) ? $wpsso->options[ 'og_def_fluid_volume_units' ] : 'cm';
267 + }
268 +
269 + public static function get_fluid_volume_label( $key = '' ) {
270 +
271 + $key = empty( $key ) ? self::get_fluid_volume_text() : $key;
272 + $units = self::get_fluid_volume_units(); // Returns translated labels.
273 +
274 + return isset( $units[ $key ] ) ? $units[ $key ] : '';
275 + }
276 +
277 + public static function get_fluid_volume_units() {
278 +
279 + static $local_cache = null;
280 +
281 + if ( null === $local_cache ) {
282 +
283 + $local_cache = array(
284 + 'ml' => _x( 'ml', 'option value', 'wpsso' ), // Millilitre.
285 + 'cl' => _x( 'cl', 'option value', 'wpsso' ), // Centilitre.
286 + 'l' => _x( 'l', 'option value', 'wpsso' ), // Liter.
287 + 'US tsp' => _x( 'US tsp', 'option value', 'wpsso' ), // US teaspoon.
288 + 'US tbsp' => _x( 'US tbsp', 'option value', 'wpsso' ), // US tablespoon.
289 + 'US fl oz' => _x( 'US fl oz', 'option value', 'wpsso' ), // US fluid ounce.
290 + 'US cup' => _x( 'US cup', 'option value', 'wpsso' ), // US cup.
291 + 'US pt' => _x( 'US pt', 'option value', 'wpsso' ), // US pint.
292 + 'US qt' => _x( 'US qt', 'option value', 'wpsso' ), // US quart.
293 + 'US gal' => _x( 'US gal', 'option value', 'wpsso' ), // US gallon.
294 + );
295 +
296 + $local_cache = apply_filters( 'wpsso_fluid_volume_units', $local_cache );
297 + }
298 +
299 + return $local_cache;
300 + }
301 +
302 + public static function get_fluid_volume_convert( $value, $to, $from = '' ) {
303 +
304 + $value = (float) $value;
305 + $from = empty( $from ) ? self::get_fluid_volume_text() : $from;
306 +
307 + if ( $from !== $to ) { // Just in case.
308 +
309 + /*
310 + * Convert volume to ml first.
311 + */
312 + switch ( $from ) {
313 +
314 + /*
315 + * Metric units.
316 + */
317 + case 'ml': $value *= 1; break; // Millilitre to Millilitre.
318 + case 'cl': $value *= 10; break; // Centilitre to Millilitre.
319 + case 'l': $value *= 1000; break; // Liter to Millilitre.
320 + case 'kl': $value *= 1000000; break; // Kiloliter to Millilitre.
321 +
322 + /*
323 + * Imperial units.
324 + */
325 + case 'US tsp': $value *= 4.92892; break; // US teaspoon to Millilitre.
326 + case 'US tbsp': $value *= 14.7868; break; // US tablespoon to Millilitre.
327 + case 'US fl oz': $value *= 29.5735; break; // US fluid ounce to Millilitre.
328 + case 'US cup': $value *= 236.588; break; // US cup to Millilitre.
329 + case 'US pt': $value *= 473.176; break; // US pint to Millilitre.
330 + case 'US qt': $value *= 946.353; break; // US quart to Millilitre.
331 + case 'US gal': $value *= 3785.41; break; // US gallon to Millilitre.
332 + }
333 +
334 + /*
335 + * Convert volume from ml to desired output.
336 + */
337 + switch ( $to ) {
338 +
339 + /*
340 + * Metric units.
341 + */
342 + case 'ml': $value *= 1; break; // Millilitre to Millilitre.
343 + case 'cl': $value *= 0.1; break; // Millilitre to Centilitre.
344 + case 'l': $value *= 0.001; break; // Millilitre to Liter.
345 + case 'kl': $value *= 0.000001; break; // Millilitre to Kiloliter.
346 +
347 + /*
348 + * Imperial units.
349 + */
350 + case 'US tsp': $value *= 0.202884; break; // Millilitre to US teaspoon.
351 + case 'US tbsp': $value *= 0.067628; break; // Millilitre to US tablespoon.
352 + case 'US fl oz': $value *= 0.033814; break; // Millilitre to US fluid ounce.
353 + case 'US cup': $value *= 0.00422675; break; // Millilitre to US cup.
354 + case 'US pt': $value *= 0.00211338; break; // Millilitre to US pint.
355 + case 'US qt': $value *= 0.00105669; break; // Millilitre to US quart.
356 + case 'US gal': $value *= 0.000264172; break; // Millilitre to US gallon.
357 + }
358 + }
359 +
360 + return ( $value < 0 ) ? 0 : $value;
361 + }
362 +
363 + /*
364 + * Weight.
365 + *
366 + * See https://support.google.com/merchants/answer/11018531.
367 + */
368 + public static function get_weight_text() {
369 +
370 + $wpsso =& Wpsso::get_instance();
371 +
372 + $key = isset( $wpsso->options[ 'og_def_weight_units' ] ) ? $wpsso->options[ 'og_def_weight_units' ] : 'cm';
373 +
374 + $key = 'lbs' === $key ? 'lb' : $key; // WooCommerce uses 'lbs' and WPSSO uses 'lb'.
375 +
376 + return $key;
377 + }
378 +
379 + public static function get_weight_label( $key = '' ) {
380 +
381 + $key = 'lbs' === $key ? 'lb' : $key; // WooCommerce uses 'lbs' and WPSSO uses 'lb'.
382 + $key = empty( $key ) ? self::get_weight_text() : $key;
383 + $units = self::get_weight_units(); // Returns translated labels.
384 +
385 + return isset( $units[ $key ] ) ? $units[ $key ] : '';
386 + }
387 +
388 + /*
389 + * See wc_get_weight() from woocommerce/includes/wc-formatting-functions.php.
390 + */
391 + public static function get_weight_units() {
392 +
393 + static $local_cache = null;
394 +
395 + if ( null === $local_cache ) {
396 +
397 + $local_cache = array(
398 + 'mg' => _x( 'mg', 'option value', 'wpsso' ), // Milligram.
399 + 'g' => _x( 'g', 'option value', 'wpsso' ), // Gram.
400 + 'kg' => _x( 'kg', 'option value', 'wpsso' ), // Kilogram.
401 + 'oz' => _x( 'oz', 'option value', 'wpsso' ), // Ounce.
402 + 'lb' => _x( 'lbs', 'option value', 'wpsso' ), // Pound.
403 + 'st' => _x( 'st', 'option value', 'wpsso' ), // Stone.
404 + );
405 +
406 + $local_cache = apply_filters( 'wpsso_weight_units', $local_cache );
407 + }
408 +
409 + return $local_cache;
410 + }
411 +
412 + public static function get_weight_convert( $value, $to, $from = '' ) {
413 +
414 + $value = (float) $value;
415 + $to = 'lbs' === $to ? 'lb' : $to; // WooCommerce uses 'lbs' and WPSSO uses 'lb'.
416 + $from = 'lbs' === $from ? 'lb' : $from; // WooCommerce uses 'lbs' and WPSSO uses 'lb'.
417 + $from = empty( $from ) ? self::get_weight_text() : $from;
418 +
419 + if ( $from !== $to ) {
420 +
421 + /*
422 + * Convert weight to kg first.
423 + */
424 + switch ( $from ) {
425 +
426 + /*
427 + * Metric units.
428 + */
429 + case 'mg': $value *= 0.000001; break; // Milligram to Kilogram.
430 + case 'g': $value *= 0.001; break; // Gram to Kilogram.
431 + case 'kg': $value *= 1; break; // Kilogram to Kilogram.
432 + case 't': $value *= 1000; break; // Metric Ton to Kilogram.
433 +
434 + /*
435 + * Imperial units.
436 + */
437 + case 'oz': $value *= 0.02834952; break; // Ounce to Kilogram.
438 + case 'lb': $value *= 0.4535924; break; // Pound to Kilogram.
439 + case 'st': $value *= 6.350293; break; // Stone to Kilogram.
440 + }
441 +
442 + /*
443 + * Convert weight from kg to desired output.
444 + */
445 + switch ( $to ) {
446 +
447 + /*
448 + * Metric units.
449 + */
450 + case 'mg': $value *= 1000000; break; // Kilogram to Milligram.
451 + case 'g': $value *= 1000; break; // Kilogram to Gram.
452 + case 'kg': $value *= 1; break; // Kilogram to Kilogram.
453 + case 't': $value *= 0.001; break; // Kilogram to Metric Ton.
454 +
455 + /*
456 + * Imperial units.
457 + */
458 + case 'oz': $value *= 35.27396; break; // Kilogram to Ounce.
459 + case 'lb': $value *= 2.204623; break; // Kilogram to Pound.
460 + case 'st': $value *= 0.157473; break; // Kilogram to Stone.
461 + }
462 + }
463 +
464 + return ( $value < 0 ) ? 0 : $value;
465 + }
466 + }
467 +}