Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.77% covered (success)
96.77%
210 / 217
76.92% covered (warning)
76.92%
20 / 26
CRAP
0.00% covered (danger)
0.00%
0 / 1
Schema_Settings
96.77% covered (success)
96.77%
210 / 217
76.92% covered (warning)
76.92%
20 / 26
81
0.00% covered (danger)
0.00%
0 / 1
 get_editable
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 get_defaults
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 get_organization
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 get_site_represents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_person
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 get_local_business
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 get_breadcrumb_list
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 sanitize
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 sanitize_site_represents
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 sanitize_person
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 sanitize_url_list
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
8.02
 get_stored
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sanitize_breadcrumb_list
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 sanitize_local_business
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 woo_address_defaults
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
5
 geo
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 coordinate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 opening_hours
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 time
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 country_code
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 telephone
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 price_range
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 unicode_length
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
4.12
 text
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 email
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2/**
3 * Package-owned store for the site-level Schema.org settings.
4 *
5 * Persists the admin-configurable values WordPress has no native source for —
6 * social profiles (`sameAs`), a contact `email`, optional `name` /
7 * `description` overrides, LocalBusiness details, and the BreadcrumbList toggle.
8 * The Organization node reads the effective values via {@see self::get_organization()} and
9 * {@see self::get_local_business()}; the Settings UI round-trips them through
10 * {@see Schema_Settings_Controller}.
11 *
12 * The option is a container keyed by schema type so later types slot in without
13 * breaking the contract. Empty Organization overrides fall back to site identity
14 * at read time, so an unconfigured site still emits a valid node and later Site
15 * Title changes track.
16 *
17 * @package automattic/jetpack-seo-package
18 */
19
20namespace Automattic\Jetpack\SEO;
21
22/**
23 * Reads, sanitizes, and persists the site-level Schema settings.
24 */
25class Schema_Settings {
26
27    /**
28     * Versioned option name. The `_v1` suffix lets a future shape change ship a
29     * new option rather than migrate in place.
30     *
31     * @var string
32     */
33    const OPTION_NAME = 'jetpack_seo_schema_settings_v1';
34
35    /**
36     * Schema.org day-code order for stored opening-hours settings.
37     *
38     * @var array<int, string>
39     */
40    const OPENING_DAYS = array( 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su' );
41
42    /**
43     * The editing payload for the Settings form / REST route: the raw stored
44     * overrides (empty where unset) plus the site-identity defaults the form shows
45     * as field placeholders. Keeping the two separate lets an empty field track the
46     * Site Title instead of freezing its value.
47     *
48     * @return array{siteRepresents: string, organization: array{name: string, description: string, sameAs: array<int, string>, email: string}, localBusiness: array{enabled: bool, address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}, telephone: string, geo: array{latitude: string, longitude: string}, openingHours: array<string, array{opens: string, closes: string}>, priceRange: string}, person: array{name: string, description: string, sameAs: array<int, string>}, breadcrumbList: array{enabled: bool}, defaults: array{organization: array{name: string, description: string}, localBusiness: array{address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}}, person: array{name: string, description: string}}}
49     */
50    public static function get_editable() {
51        $defaults = self::get_defaults();
52        $stored   = self::get_stored();
53
54        return array(
55            'siteRepresents' => $stored['siteRepresents'],
56            'organization'   => $stored['organization'],
57            'localBusiness'  => $stored['localBusiness'],
58            'person'         => $stored['person'],
59            'breadcrumbList' => $stored['breadcrumbList'],
60            'defaults'       => array(
61                'organization'  => array(
62                    'name'        => $defaults['organization']['name'],
63                    'description' => $defaults['organization']['description'],
64                ),
65                'localBusiness' => array(
66                    'address' => $defaults['localBusiness']['address'],
67                ),
68                'person'        => array(
69                    'name'        => $defaults['person']['name'],
70                    'description' => $defaults['person']['description'],
71                ),
72            ),
73        );
74    }
75
76    /**
77     * Site-identity defaults for the fields WordPress has a source for: `name` /
78     * `description` from the Site Title and Tagline. Shown as placeholders and used
79     * as the fallback. WooCommerce store address settings, when WooCommerce is
80     * active, provide LocalBusiness address placeholders and read-time fallbacks.
81     * `sameAs` / `email` and the other LocalBusiness fields have no source, so they
82     * aren't defaulted.
83     *
84     * @return array{organization: array{name: string, description: string}, localBusiness: array{address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}}, person: array{name: string, description: string}}
85     */
86    public static function get_defaults() {
87        $site_name    = self::text( get_bloginfo( 'name' ) );
88        $site_tagline = self::text( get_bloginfo( 'description' ) );
89
90        return array(
91            'organization'  => array(
92                'name'        => $site_name,
93                'description' => $site_tagline,
94            ),
95            'localBusiness' => array(
96                'address' => self::woo_address_defaults(),
97            ),
98            // A personal site's title is typically the person's name, and the
99            // tagline a short bio — so the same site-identity values seed both.
100            'person'        => array(
101                'name'        => $site_name,
102                'description' => $site_tagline,
103            ),
104        );
105    }
106
107    /**
108     * The effective Organization settings the node consumes: stored overrides where
109     * present, site-identity defaults otherwise. `sameAs` / `email` are stored-only.
110     * Computed live so an unconfigured `name` / `description` tracks site identity.
111     *
112     * @return array{name: string, description: string, sameAs: array<int, string>, email: string}
113     */
114    public static function get_organization() {
115        $defaults = self::get_defaults();
116        $stored   = self::get_stored();
117
118        $organization = $stored['organization'];
119        $fallbacks    = $defaults['organization'];
120
121        return array(
122            'name'        => '' !== $organization['name'] ? $organization['name'] : $fallbacks['name'],
123            'description' => '' !== $organization['description'] ? $organization['description'] : $fallbacks['description'],
124            'sameAs'      => $organization['sameAs'],
125            'email'       => $organization['email'],
126        );
127    }
128
129    /**
130     * Return the site publisher type, defaulting to Organization for existing sites.
131     *
132     * @return string
133     */
134    public static function get_site_represents() {
135        return self::get_stored()['siteRepresents'];
136    }
137
138    /**
139     * Return Person overrides with live site-identity fallbacks for name and description.
140     *
141     * @return array{name: string, description: string, sameAs: array<int, string>}
142     */
143    public static function get_person() {
144        $defaults = self::get_defaults();
145        $stored   = self::get_stored();
146
147        $person    = $stored['person'];
148        $fallbacks = $defaults['person'];
149
150        return array(
151            'name'        => '' !== $person['name'] ? $person['name'] : $fallbacks['name'],
152            'description' => '' !== $person['description'] ? $person['description'] : $fallbacks['description'],
153            'sameAs'      => $person['sameAs'],
154        );
155    }
156
157    /**
158     * The effective LocalBusiness settings the node consumes: stored values, with
159     * each address subfield falling back to WooCommerce store-address defaults when
160     * the stored value is empty. Other LocalBusiness properties are stored-only.
161     *
162     * @return array{enabled: bool, address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}, telephone: string, geo: array{latitude: string, longitude: string}, openingHours: array<string, array{opens: string, closes: string}>, priceRange: string}
163     */
164    public static function get_local_business() {
165        $defaults = self::get_defaults();
166        $stored   = self::get_stored();
167
168        $local_business = $stored['localBusiness'];
169        $fallbacks      = $defaults['localBusiness']['address'];
170
171        foreach ( array( 'streetAddress', 'addressLocality', 'addressRegion', 'postalCode', 'addressCountry' ) as $field ) {
172            if ( '' === $local_business['address'][ $field ] ) {
173                $local_business['address'][ $field ] = $fallbacks[ $field ];
174            }
175        }
176
177        return $local_business;
178    }
179
180    /**
181     * The BreadcrumbList settings consumed by the graph builder.
182     *
183     * @return array{enabled: bool}
184     */
185    public static function get_breadcrumb_list() {
186        return self::get_stored()['breadcrumbList'];
187    }
188
189    /**
190     * Sanitize a raw submission and persist it, then return the new editing payload
191     * (so the caller can hand it straight back to the client).
192     *
193     * @param mixed $raw Raw input (expected to be the container array).
194     * @return array{siteRepresents: string, organization: array{name: string, description: string, sameAs: array<int, string>, email: string}, localBusiness: array{enabled: bool, address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}, telephone: string, geo: array{latitude: string, longitude: string}, openingHours: array<string, array{opens: string, closes: string}>, priceRange: string}, person: array{name: string, description: string, sameAs: array<int, string>}, breadcrumbList: array{enabled: bool}, defaults: array{organization: array{name: string, description: string}, localBusiness: array{address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}}, person: array{name: string, description: string}}}
195     */
196    public static function update( $raw ) {
197        $raw    = is_array( $raw ) ? $raw : array();
198        $stored = self::get_stored();
199        foreach ( array( 'siteRepresents', 'organization', 'localBusiness', 'person', 'breadcrumbList' ) as $section ) {
200            if ( ! array_key_exists( $section, $raw ) ) {
201                $raw[ $section ] = $stored[ $section ];
202            }
203        }
204
205        update_option( self::OPTION_NAME, self::sanitize( $raw ) );
206        return self::get_editable();
207    }
208
209    /**
210     * Normalize and sanitize raw input into the stored option shape: trimmed plain
211     * text for `name` / `description`, validated + deduped URLs for `sameAs`, a
212     * sanitized `email`, and normalized LocalBusiness fields. Defensive against
213     * non-array / non-string input.
214     *
215     * @param mixed $raw Raw input.
216     * @return array{siteRepresents: string, organization: array{name: string, description: string, sameAs: array<int, string>, email: string}, localBusiness: array{enabled: bool, address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}, telephone: string, geo: array{latitude: string, longitude: string}, openingHours: array<string, array{opens: string, closes: string}>, priceRange: string}, person: array{name: string, description: string, sameAs: array<int, string>}, breadcrumbList: array{enabled: bool}}
217     */
218    public static function sanitize( $raw ) {
219        $raw          = is_array( $raw ) ? $raw : array();
220        $organization = isset( $raw['organization'] ) && is_array( $raw['organization'] )
221            ? $raw['organization']
222            : array();
223
224        return array(
225            'siteRepresents' => self::sanitize_site_represents( $raw['siteRepresents'] ?? '' ),
226            'organization'   => array(
227                'name'        => self::text( $organization['name'] ?? '' ),
228                'description' => self::text( $organization['description'] ?? '' ),
229                'sameAs'      => self::sanitize_url_list( $organization['sameAs'] ?? array() ),
230                'email'       => self::email( $organization['email'] ?? '' ),
231            ),
232            'localBusiness'  => self::sanitize_local_business( $raw['localBusiness'] ?? array() ),
233            'person'         => self::sanitize_person( $raw['person'] ?? array() ),
234            'breadcrumbList' => self::sanitize_breadcrumb_list( $raw['breadcrumbList'] ?? array() ),
235        );
236    }
237
238    /**
239     * Normalize the site-entity choice. Anything other than `person` (including
240     * missing / legacy data) resolves to `organization`, the historical default.
241     *
242     * @param mixed $value Raw site-entity choice.
243     * @return string
244     */
245    private static function sanitize_site_represents( $value ) {
246        return 'person' === $value ? 'person' : 'organization';
247    }
248
249    /**
250     * Normalize and sanitize raw Person input into the stored option shape: trimmed
251     * plain text for `name` / `description`, validated + deduped URLs for `sameAs`.
252     *
253     * @param mixed $raw Raw Person input.
254     * @return array{name: string, description: string, sameAs: array<int, string>}
255     */
256    private static function sanitize_person( $raw ) {
257        $raw = is_array( $raw ) ? $raw : array();
258
259        return array(
260            'name'        => self::text( $raw['name'] ?? '' ),
261            'description' => self::text( $raw['description'] ?? '' ),
262            'sameAs'      => self::sanitize_url_list( $raw['sameAs'] ?? array() ),
263        );
264    }
265
266    /**
267     * Normalize a list of profile URLs (`sameAs`): keep only valid absolute http(s)
268     * URLs and drop duplicates. Shared by the settings store and the Organization
269     * node so what the form stores is exactly what the schema graph emits.
270     *
271     * @param mixed $value Raw value (expected to be an array of URLs).
272     * @return array<int, string>
273     */
274    public static function sanitize_url_list( $value ) {
275        if ( ! is_array( $value ) ) {
276            return array();
277        }
278
279        $urls = array();
280        foreach ( $value as $url ) {
281            if ( ! is_string( $url ) ) {
282                continue;
283            }
284
285            $url = trim( $url );
286            if ( '' === $url ) {
287                continue;
288            }
289
290            // Absolute http(s) URLs with a host only. Deliberately not
291            // wp_http_validate_url(): that resolves DNS (gethostbyname), which
292            // blocks front-end rendering and drops valid-but-unresolvable hosts.
293            // These URLs are emitted as markup, never fetched.
294            $scheme = strtolower( (string) wp_parse_url( $url, PHP_URL_SCHEME ) );
295            if ( ! in_array( $scheme, array( 'http', 'https' ), true ) || ! wp_parse_url( $url, PHP_URL_HOST ) ) {
296                continue;
297            }
298
299            $clean = esc_url_raw( $url, array( 'http', 'https' ) );
300            if ( '' !== $clean ) {
301                $urls[] = $clean;
302            }
303        }
304
305        return array_values( array_unique( $urls ) );
306    }
307
308    /**
309     * The stored settings, normalized to the full option shape (so callers can
310     * rely on every key being present even when the option is absent or partial).
311     *
312     * @return array{siteRepresents: string, organization: array{name: string, description: string, sameAs: array<int, string>, email: string}, localBusiness: array{enabled: bool, address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}, telephone: string, geo: array{latitude: string, longitude: string}, openingHours: array<string, array{opens: string, closes: string}>, priceRange: string}, person: array{name: string, description: string, sameAs: array<int, string>}, breadcrumbList: array{enabled: bool}}
313     */
314    private static function get_stored() {
315        return self::sanitize( get_option( self::OPTION_NAME, array() ) );
316    }
317
318    /**
319     * Normalize BreadcrumbList input. Missing legacy data defaults to enabled.
320     *
321     * @param mixed $raw Raw BreadcrumbList input.
322     * @return array{enabled: bool}
323     */
324    private static function sanitize_breadcrumb_list( $raw ) {
325        $raw = is_array( $raw ) ? $raw : array();
326
327        return array(
328            'enabled' => ! array_key_exists( 'enabled', $raw ) || rest_sanitize_boolean( $raw['enabled'] ),
329        );
330    }
331
332    /**
333     * Normalize and sanitize raw LocalBusiness input into the stored option shape.
334     *
335     * @param mixed $raw Raw LocalBusiness input.
336     * @return array{enabled: bool, address: array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}, telephone: string, geo: array{latitude: string, longitude: string}, openingHours: array<string, array{opens: string, closes: string}>, priceRange: string}
337     */
338    private static function sanitize_local_business( $raw ) {
339        $raw     = is_array( $raw ) ? $raw : array();
340        $address = isset( $raw['address'] ) && is_array( $raw['address'] ) ? $raw['address'] : array();
341
342        return array(
343            'enabled'      => ! empty( $raw['enabled'] ),
344            'address'      => array(
345                'streetAddress'   => self::text( $address['streetAddress'] ?? '' ),
346                'addressLocality' => self::text( $address['addressLocality'] ?? '' ),
347                'addressRegion'   => self::text( $address['addressRegion'] ?? '' ),
348                'postalCode'      => self::text( $address['postalCode'] ?? '' ),
349                'addressCountry'  => self::country_code( $address['addressCountry'] ?? '' ),
350            ),
351            'telephone'    => self::telephone( $raw['telephone'] ?? '' ),
352            'geo'          => self::geo( $raw['geo'] ?? array() ),
353            'openingHours' => self::opening_hours( $raw['openingHours'] ?? array() ),
354            'priceRange'   => self::price_range( $raw['priceRange'] ?? '' ),
355        );
356    }
357
358    /**
359     * WooCommerce store-address defaults for LocalBusiness address placeholders.
360     *
361     * @return array{streetAddress: string, addressLocality: string, addressRegion: string, postalCode: string, addressCountry: string}
362     */
363    private static function woo_address_defaults() {
364        $address = array(
365            'streetAddress'   => '',
366            'addressLocality' => '',
367            'addressRegion'   => '',
368            'postalCode'      => '',
369            'addressCountry'  => '',
370        );
371
372        if ( ! class_exists( 'WooCommerce' ) ) {
373            return $address;
374        }
375
376        $street_1 = self::text( (string) get_option( 'woocommerce_store_address', '' ) );
377        $street_2 = self::text( (string) get_option( 'woocommerce_store_address_2', '' ) );
378        if ( '' !== $street_1 ) {
379            $address['streetAddress'] = $street_1;
380        }
381        if ( '' !== $street_2 ) {
382            $address['streetAddress'] = '' !== $address['streetAddress'] ? $address['streetAddress'] . ', ' . $street_2 : $street_2;
383        }
384
385        $address['addressLocality'] = self::text( (string) get_option( 'woocommerce_store_city', '' ) );
386        $address['postalCode']      = self::text( (string) get_option( 'woocommerce_store_postcode', '' ) );
387
388        $country_region            = explode( ':', self::text( (string) get_option( 'woocommerce_default_country', '' ) ), 2 );
389        $address['addressCountry'] = self::country_code( $country_region[0] ?? '' );
390        $address['addressRegion']  = $country_region[1] ?? '';
391
392        return $address;
393    }
394
395    /**
396     * Normalize latitude/longitude settings. A half-coordinate is useless, so any
397     * invalid or missing endpoint clears both.
398     *
399     * @param mixed $raw Raw geo input.
400     * @return array{latitude: string, longitude: string}
401     */
402    private static function geo( $raw ) {
403        $empty = array(
404            'latitude'  => '',
405            'longitude' => '',
406        );
407
408        if ( ! is_array( $raw ) ) {
409            return $empty;
410        }
411
412        $latitude  = self::coordinate( $raw['latitude'] ?? null, 90 );
413        $longitude = self::coordinate( $raw['longitude'] ?? null, 180 );
414
415        if ( null === $latitude || null === $longitude ) {
416            return $empty;
417        }
418
419        return array(
420            'latitude'  => $latitude,
421            'longitude' => $longitude,
422        );
423    }
424
425    /**
426     * Validate and normalize one coordinate.
427     *
428     * @param mixed $value Raw coordinate.
429     * @param int   $max   Absolute allowed bound.
430     * @return string|null
431     */
432    private static function coordinate( $value, $max ) {
433        if ( ! is_scalar( $value ) ) {
434            return null;
435        }
436
437        $value = trim( (string) $value );
438        if ( '' === $value || ! is_numeric( $value ) ) {
439            return null;
440        }
441
442        $number = (float) $value;
443        if ( abs( $number ) > $max ) {
444            return null;
445        }
446
447        return $value;
448    }
449
450    /**
451     * Normalize opening hours to all seven schema.org day codes.
452     *
453     * @param mixed $raw Raw opening-hours input.
454     * @return array<string, array{opens: string, closes: string}>
455     */
456    private static function opening_hours( $raw ) {
457        $raw   = is_array( $raw ) ? $raw : array();
458        $hours = array();
459
460        foreach ( self::OPENING_DAYS as $day ) {
461            $entry  = isset( $raw[ $day ] ) && is_array( $raw[ $day ] ) ? $raw[ $day ] : array();
462            $opens  = self::time( $entry['opens'] ?? '' );
463            $closes = self::time( $entry['closes'] ?? '' );
464
465            if ( '' === $opens || '' === $closes ) {
466                $opens  = '';
467                $closes = '';
468            }
469
470            $hours[ $day ] = array(
471                'opens'  => $opens,
472                'closes' => $closes,
473            );
474        }
475
476        return $hours;
477    }
478
479    /**
480     * Validate an `HH:MM` 24-hour time value.
481     *
482     * @param mixed $value Raw time value.
483     * @return string
484     */
485    private static function time( $value ) {
486        if ( ! is_string( $value ) ) {
487            return '';
488        }
489
490        $value = trim( $value );
491        return preg_match( '/^([01][0-9]|2[0-3]):[0-5][0-9]$/', $value ) ? $value : '';
492    }
493
494    /**
495     * Normalize an optional ISO 3166-1 alpha-2 country code.
496     *
497     * @param mixed $value Raw country code.
498     * @return string
499     */
500    private static function country_code( $value ) {
501        $value = self::text( $value );
502        if ( '' === $value || 1 !== preg_match( '/^[A-Za-z]{2}$/D', $value ) ) {
503            return '';
504        }
505
506        return strtoupper( $value );
507    }
508
509    /**
510     * Normalize an optional telephone number using a permissive common format.
511     *
512     * @param mixed $value Raw telephone number.
513     * @return string
514     */
515    private static function telephone( $value ) {
516        $value = self::text( $value );
517        if ( '' === $value ) {
518            return '';
519        }
520
521        $has_valid_characters = 1 === preg_match( '/^\+?[0-9 ()-]+$/D', $value );
522        $has_digit            = 1 === preg_match( '/[0-9]/', $value );
523
524        return $has_valid_characters && $has_digit ? $value : '';
525    }
526
527    /**
528     * Normalize an optional LocalBusiness price range.
529     *
530     * @param mixed $value Raw price range.
531     * @return string
532     */
533    private static function price_range( $value ) {
534        $value = self::text( $value );
535        return self::unicode_length( $value ) < 100 ? $value : '';
536    }
537
538    /**
539     * Count Unicode code points, including when the mbstring extension is absent.
540     *
541     * @param string $value Value to measure.
542     * @return int
543     */
544    private static function unicode_length( $value ) {
545        if ( function_exists( 'mb_strlen' ) ) {
546            return mb_strlen( $value, 'UTF-8' );
547        }
548
549        $length = preg_match_all( '/./us', $value, $matches );
550        return false === $length ? strlen( $value ) : $length;
551    }
552
553    /**
554     * Normalize a scalar value to trimmed plain text.
555     *
556     * @param mixed $value Raw value.
557     * @return string
558     */
559    private static function text( $value ) {
560        if ( ! is_string( $value ) ) {
561            return '';
562        }
563        return trim( wp_strip_all_tags( $value ) );
564    }
565
566    /**
567     * Normalize an email value.
568     *
569     * @param mixed $value Raw value.
570     * @return string
571     */
572    private static function email( $value ) {
573        if ( ! is_string( $value ) ) {
574            return '';
575        }
576        return sanitize_email( $value );
577    }
578}