Files
smilia/wp-content/plugins/smilia-core/includes/class-schema.php
T

947 lines
28 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Smilia\Core;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Schema {
private const DAYS = array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' );
private const LANGUAGE_NAMES = array(
'pl' => 'Polski',
'en' => 'English',
'ru' => 'Русский',
);
private const SOURCES = array(
'google' => 'Google',
'booksy' => 'Booksy',
'znanylekarz' => 'ZnanyLekarz',
);
private const NEW_PATIENTS = array(
'pl' => 'Nowi pacjenci',
'en' => 'New patients',
'ru' => 'Новые пациенты',
);
private static ?array $price_span = null;
private static ?array $price_groups = null;
public static function graph( WP_Post $post ): array {
$home = home_url( '/' );
$url = (string) wp_get_canonical_url( $post );
$trail = Markup::trail( $post );
$reviews = self::reviews( $post, $home );
$catalog = self::price_catalog( $post, $url );
$service = self::service( $post, $home, $url );
$procedure = self::procedure( $post, $url );
$person = self::doctor( $post, $home, $url );
$nodes = array(
self::page( $post, $home, $url, array(
'service' => $service,
'procedure' => $procedure,
'person' => $person,
'catalog' => $catalog,
'board' => $reviews['board'],
) ),
);
foreach ( array( $person, $service, $procedure ) as $extra ) {
if ( $extra ) {
$nodes[] = $extra;
}
}
$nodes[] = self::clinic( $home, $reviews['nodes'], $catalog ? array( '@id' => $catalog['@id'] ) : array() );
$faq = self::faq( $post, $url );
if ( $faq ) {
$nodes[] = $faq;
}
$others = self::other_doctors( $post, $url );
if ( $others ) {
$nodes[] = $others;
}
if ( count( $trail ) > 1 ) {
$nodes[] = self::breadcrumb( $url, $trail );
}
$nodes[] = self::website( $home );
return array(
'@context' => 'https://schema.org',
'@graph' => $nodes,
);
}
private static function website( string $home ): array {
$node = array(
'@type' => 'WebSite',
'@id' => $home . '#website',
'url' => $home,
'name' => get_bloginfo( 'name' ),
'inLanguage' => self::locales(),
);
$description = get_bloginfo( 'description' );
if ( '' !== $description ) {
$node['description'] = $description;
}
return $node;
}
private static function page( WP_Post $post, string $home, string $url, array $parts ): array {
$page = array(
'@type' => 'WebPage',
'@id' => $url . '#webpage',
'url' => $url,
'name' => wp_get_document_title(),
'inLanguage' => get_bloginfo( 'language' ),
'datePublished' => get_post_time( DATE_W3C, true, $post ),
'dateModified' => get_post_modified_time( DATE_W3C, true, $post ),
);
$description = Seo::description( $post );
if ( '' !== $description ) {
$page['description'] = $description;
}
$image = self::post_image( $post );
if ( $image ) {
$page['primaryImageOfPage'] = $image;
$page['image'] = $image;
}
$translations = self::translations( $post );
if ( $translations ) {
$page['workTranslation'] = $translations;
}
if ( $parts['person'] ) {
$page['@type'] = 'ProfilePage';
$page['mainEntity'] = array( '@id' => $parts['person']['@id'] );
} elseif ( $parts['service'] ) {
$page['@type'] = 'MedicalWebPage';
$page['specialty'] = array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' );
$page['mainEntity'] = array( '@id' => $parts['service']['@id'] );
if ( $parts['procedure'] ) {
$page['about'] = array( '@id' => $parts['procedure']['@id'] );
}
} elseif ( has_block( 'smilia/contacts-hero', $post ) ) {
$page['@type'] = 'ContactPage';
$page['mainEntity'] = array( '@id' => $home . '#clinic' );
} elseif ( $parts['catalog'] ) {
$page['@type'] = 'CollectionPage';
$page['mainEntity'] = $parts['catalog'];
} elseif ( $parts['board'] ) {
$page['@type'] = 'CollectionPage';
$page['mainEntity'] = $parts['board'];
} elseif ( has_block( 'smilia/prepare', $post ) ) {
$page['@type'] = 'MedicalWebPage';
$page['audience'] = array(
'@type' => 'PeopleAudience',
'audienceType' => self::NEW_PATIENTS[ self::language() ] ?? self::NEW_PATIENTS['en'],
);
} elseif ( (int) $post->ID === self::front_id() ) {
$page['mainEntity'] = array( '@id' => $home . '#clinic' );
}
return $page;
}
private static function language(): string {
return function_exists( 'pll_current_language' ) ? (string) pll_current_language() : '';
}
private static function front_id(): int {
$front = (int) get_option( 'page_on_front' );
if ( $front && function_exists( 'pll_get_post' ) ) {
$front = (int) pll_get_post( $front ) ?: $front;
}
return $front;
}
private static function breadcrumb( string $url, array $trail ): array {
return array(
'@type' => 'BreadcrumbList',
'@id' => $url . '#breadcrumb',
'itemListElement' => array_map(
static fn( array $crumb, int $index ): array => array(
'@type' => 'ListItem',
'position' => $index + 1,
'name' => $crumb['title'],
'item' => $crumb['url'],
),
$trail,
array_keys( $trail )
),
);
}
private static function clinic( string $home, array $reviews, array $catalog ): array {
$clinic = array(
'@type' => 'Dentist',
'@id' => $home . '#clinic',
'name' => get_bloginfo( 'name' ),
'url' => $home,
'logo' => self::logo(),
);
$description = get_bloginfo( 'description' );
if ( '' !== $description ) {
$clinic['description'] = $description;
}
$images = self::clinic_images();
if ( $images ) {
$clinic['image'] = 1 === count( $images ) ? $images[0] : $images;
}
$phone = Settings::get( 'phone_main' );
if ( '' !== $phone ) {
$clinic['telephone'] = $phone;
}
$email = Settings::get( 'email' );
if ( '' !== $email ) {
$clinic['email'] = $email;
}
$address = self::address( Settings::get( 'address' ) );
if ( $address ) {
$clinic['address'] = $address;
$clinic['hasMap'] = add_query_arg(
array(
'api' => 1,
'query' => rawurlencode( Settings::get( 'address' ) ),
),
'https://www.google.com/maps/search/'
);
if ( isset( $address['addressLocality'] ) ) {
$clinic['areaServed'] = array(
'@type' => 'City',
'name' => $address['addressLocality'],
);
}
}
$geo = self::geo();
if ( $geo ) {
$clinic['geo'] = $geo;
}
$points = array();
if ( '' !== $phone ) {
$points[] = array(
'@type' => 'ContactPoint',
'contactType' => 'customer service',
'telephone' => $phone,
'availableLanguage' => self::languages(),
);
}
$whatsapp = Settings::get( 'whatsapp' );
if ( '' !== $whatsapp ) {
$points[] = array(
'@type' => 'ContactPoint',
'contactType' => 'customer service',
'telephone' => $whatsapp,
'url' => 'https://wa.me/' . preg_replace( '/\D/', '', $whatsapp ),
'availableLanguage' => self::languages(),
);
}
if ( $points ) {
$clinic['contactPoint'] = $points;
}
$hours = self::opening_hours();
if ( $hours ) {
$clinic['openingHoursSpecification'] = $hours;
}
$profiles = array_values( Settings::profiles() );
if ( $profiles ) {
$clinic['sameAs'] = $profiles;
}
$span = self::price_span();
if ( $span ) {
$clinic['priceRange'] = $span['min'] . '-' . $span['max'] . ' PLN';
$clinic['currenciesAccepted'] = 'PLN';
}
if ( $reviews ) {
$clinic['review'] = $reviews;
}
$clinic['medicalSpecialty'] = array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' );
$clinic['isAcceptingNewPatients'] = true;
$clinic['knowsLanguage'] = self::languages();
$services = array();
foreach ( Content::services() as $service ) {
$services[] = array(
'@type' => 'Service',
'@id' => $service['url'] . '#service',
'name' => $service['title'],
'url' => $service['url'],
);
}
if ( $catalog ) {
$clinic['hasOfferCatalog'] = $catalog;
} elseif ( $services ) {
$clinic['hasOfferCatalog'] = array(
'@type' => 'OfferCatalog',
'name' => get_bloginfo( 'name' ),
'itemListElement' => $services,
);
}
$employees = array();
foreach ( Content::doctors() as $doctor ) {
$employees[] = array(
'@type' => self::person_types( (int) $doctor['id'] ),
'@id' => $doctor['url'] . '#person',
'name' => $doctor['name'],
'url' => $doctor['url'],
'jobTitle' => $doctor['position'],
);
}
if ( $employees ) {
$clinic['employee'] = $employees;
}
return $clinic;
}
private static function logo(): array|string {
$icon = (int) get_option( 'site_icon' );
$src = $icon > 0 ? wp_get_attachment_image_src( $icon, 'full' ) : false;
if ( ! $src ) {
return get_theme_file_uri( 'assets/img/smilia-mark.svg' );
}
return array(
'@type' => 'ImageObject',
'url' => $src[0],
'width' => (int) $src[1],
'height' => (int) $src[2],
);
}
private static function clinic_images(): array {
$front = self::front_id();
if ( $front <= 0 ) {
return array();
}
$ids = array( (int) get_post_thumbnail_id( $front ) );
$queue = parse_blocks( (string) get_post_field( 'post_content', $front ) );
while ( $queue ) {
$block = array_shift( $queue );
if ( 'smilia/office-photo' === $block['blockName'] ) {
$ids[] = (int) ( $block['attrs']['imageId'] ?? 0 );
}
$queue = array_merge( $queue, $block['innerBlocks'] );
}
$images = array();
foreach ( array_unique( array_filter( $ids ) ) as $id ) {
$image = self::attachment( $id, Seo::IMAGE_SIZE );
if ( $image ) {
$images[] = $image;
}
}
return $images;
}
private static function geo(): array {
if ( ! preg_match( '/^\s*(-?\d{1,2}(?:\.\d+)?)\s*,\s*(-?\d{1,3}(?:\.\d+)?)\s*$/', Settings::raw( 'geo' ), $match ) ) {
return array();
}
return array(
'@type' => 'GeoCoordinates',
'latitude' => (float) $match[1],
'longitude' => (float) $match[2],
);
}
private static function post_image( WP_Post $post ): ?array {
$id = (int) get_post_thumbnail_id( $post );
return $id > 0 ? self::attachment( $id, Seo::IMAGE_SIZE ) : null;
}
private static function attachment( int $id, string $size ): ?array {
$src = wp_get_attachment_image_src( $id, $size );
if ( ! $src ) {
return null;
}
$node = array(
'@type' => 'ImageObject',
'url' => $src[0],
'contentUrl' => $src[0],
'width' => (int) $src[1],
'height' => (int) $src[2],
);
$alt = trim( (string) get_post_meta( $id, '_wp_attachment_image_alt', true ) );
if ( '' !== $alt ) {
$node['caption'] = $alt;
}
return $node;
}
private static function address( string $address ): array {
if ( '' === $address ) {
return array();
}
$result = array(
'@type' => 'PostalAddress',
'streetAddress' => $address,
'addressCountry' => 'PL',
);
if ( preg_match( '/^(.+),\s*(\d{2}-\d{3})\s+(.+)$/u', $address, $parts ) ) {
$result['streetAddress'] = trim( $parts[1] );
$result['postalCode'] = $parts[2];
$result['addressLocality'] = trim( $parts[3] );
}
return $result;
}
private static function opening_hours(): array {
$rows = Settings::hours();
if ( count( $rows ) !== count( self::DAYS ) ) {
return array();
}
$result = array();
foreach ( $rows as $index => $row ) {
if ( ! preg_match( '/(\d{1,2}:\d{2})\s*[–-]\s*(\d{1,2}:\d{2})/u', $row['time'], $time ) ) {
continue;
}
$result[] = array(
'@type' => 'OpeningHoursSpecification',
'dayOfWeek' => 'https://schema.org/' . self::DAYS[ $index ],
'opens' => str_pad( $time[1], 5, '0', STR_PAD_LEFT ),
'closes' => str_pad( $time[2], 5, '0', STR_PAD_LEFT ),
);
}
return $result;
}
private static function locales(): array {
if ( ! function_exists( 'pll_languages_list' ) ) {
return array( get_bloginfo( 'language' ) );
}
return array_values( array_map( static fn( string $locale ): string => str_replace( '_', '-', $locale ), (array) pll_languages_list( array( 'fields' => 'locale' ) ) ) );
}
private static function languages(): array {
$slugs = function_exists( 'pll_languages_list' ) ? (array) pll_languages_list( array( 'fields' => 'slug' ) ) : array();
$list = array();
foreach ( $slugs as $slug ) {
$list[] = array(
'@type' => 'Language',
'name' => self::LANGUAGE_NAMES[ $slug ] ?? $slug,
'alternateName' => $slug,
);
}
return $list;
}
private static function translations( WP_Post $post ): array {
if ( ! function_exists( 'pll_languages_list' ) || ! function_exists( 'pll_get_post' ) ) {
return array();
}
$current = function_exists( 'pll_get_post_language' ) ? (string) pll_get_post_language( $post->ID ) : '';
$result = array();
foreach ( (array) pll_languages_list( array( 'fields' => 'slug' ) ) as $slug ) {
if ( $slug === $current ) {
continue;
}
$other = (int) pll_get_post( $post->ID, $slug );
if ( $other <= 0 ) {
continue;
}
$link = get_permalink( $other );
$result[] = array(
'@type' => 'WebPage',
'@id' => $link . '#webpage',
'url' => $link,
'inLanguage' => str_replace( '_', '-', (string) pll_get_post_language( $other, 'locale' ) ),
);
}
return $result;
}
private static function block_attributes( WP_Post $post, string $name ): array {
foreach ( parse_blocks( $post->post_content ) as $block ) {
if ( $name === $block['blockName'] ) {
return (array) $block['attrs'];
}
}
return array();
}
private static function lines( string $value ): array {
return array_values( array_filter( array_map( 'trim', (array) preg_split( '/\R/u', wp_strip_all_tags( $value ) ) ) ) );
}
private static function service( WP_Post $post, string $home, string $url ): array {
if ( Post_Types::SERVICE !== $post->post_type ) {
return array();
}
$attributes = self::block_attributes( $post, 'smilia/service-page' );
$offers = array();
$amounts = array();
foreach ( (array) ( $attributes['methods'] ?? array() ) as $method ) {
$price = (string) preg_replace( '/[\s\x{a0}]+/u', '', (string) ( $method['price'] ?? '' ) );
if ( ! preg_match( '/\d+(?:[.,]\d+)?/', $price, $found ) ) {
continue;
}
$amount = (float) str_replace( ',', '.', $found[0] );
$amounts[] = $amount;
$offers[] = array(
'@type' => 'Offer',
'name' => wp_strip_all_tags( (string) ( $method['title'] ?? '' ) ),
'priceSpecification' => array(
'@type' => 'PriceSpecification',
'minPrice' => $amount,
'priceCurrency' => 'PLN',
),
);
}
if ( ! $offers ) {
$link = get_permalink( $post );
foreach ( self::price_groups() as $group ) {
if ( $link !== $group['url'] ) {
continue;
}
foreach ( $group['items'] as $item ) {
if ( null === $item['amount'] ) {
continue;
}
$amounts[] = $item['amount'];
$offers[] = array(
'@type' => 'Offer',
'name' => $item['name'],
'priceSpecification' => array(
'@type' => 'PriceSpecification',
'minPrice' => $item['amount'],
'priceCurrency' => 'PLN',
),
);
}
}
}
$title = get_the_title( $post );
$node = array(
'@type' => 'Service',
'@id' => $url . '#service',
'name' => $title,
'serviceType' => $title,
'url' => $url,
'provider' => array( '@id' => $home . '#clinic' ),
'audience' => array(
'@type' => 'PeopleAudience',
'audienceType' => self::audience_label(),
),
'availableChannel' => array(
'@type' => 'ServiceChannel',
'serviceUrl' => $url,
'availableLanguage' => self::languages(),
),
);
$phone = Settings::get( 'phone_main' );
if ( '' !== $phone ) {
$node['availableChannel']['servicePhone'] = array(
'@type' => 'ContactPoint',
'telephone' => $phone,
'contactType' => 'customer service',
);
}
$city = self::address( Settings::get( 'address' ) )['addressLocality'] ?? '';
if ( '' !== $city ) {
$node['areaServed'] = array(
'@type' => 'City',
'name' => $city,
);
}
$description = Seo::description( $post );
if ( '' !== $description ) {
$node['description'] = $description;
}
$image = self::post_image( $post );
if ( $image ) {
$node['image'] = $image;
}
if ( 1 === count( $offers ) ) {
$node['offers'] = $offers[0];
} elseif ( $offers ) {
$node['hasOfferCatalog'] = array(
'@type' => 'OfferCatalog',
'name' => $title,
'itemListElement' => $offers,
);
$node['offers'] = array(
'@type' => 'AggregateOffer',
'lowPrice' => min( $amounts ),
'highPrice' => max( $amounts ),
'priceCurrency' => 'PLN',
'offerCount' => count( $offers ),
);
}
return $node;
}
private static function audience_label(): string {
return array(
'pl' => 'Dzieci',
'en' => 'Children',
'ru' => 'Дети',
)[ self::language() ] ?? 'Children';
}
private static function procedure( WP_Post $post, string $url ): array {
if ( Post_Types::SERVICE !== $post->post_type ) {
return array();
}
$attributes = self::block_attributes( $post, 'smilia/service-page' );
$steps = array();
foreach ( (array) ( $attributes['steps'] ?? array() ) as $index => $step ) {
if ( ! is_array( $step ) ) {
continue;
}
$line = trim( wp_strip_all_tags( (string) ( $step['title'] ?? '' ) ) . '. ' . wp_strip_all_tags( (string) ( $step['text'] ?? '' ) ) );
if ( '' !== trim( $line, '. ' ) ) {
$steps[] = ( $index + 1 ) . '. ' . $line;
}
}
$intro = trim( wp_strip_all_tags( (string) ( $attributes['intro'] ?? '' ) ) );
if ( ! $steps && '' === $intro ) {
return array();
}
$node = array(
'@type' => 'MedicalProcedure',
'@id' => $url . '#procedure',
'name' => get_the_title( $post ),
'url' => $url,
'relevantSpecialty' => array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' ),
);
if ( '' !== $intro ) {
$node['description'] = $intro;
}
if ( $steps ) {
$node['howPerformed'] = implode( ' ', $steps );
}
return $node;
}
private static function doctor( WP_Post $post, string $home, string $url ): array {
if ( Post_Types::DOCTOR !== $post->post_type ) {
return array();
}
$name = get_the_title( $post );
$position = (string) get_post_meta( $post->ID, Meta_Fields::POSITION, true );
$node = array(
'@type' => self::person_types( $post->ID ),
'@id' => $url . '#person',
'name' => $name,
'url' => $url,
'jobTitle' => $position,
'worksFor' => array( '@id' => $home . '#clinic' ),
);
$parts = preg_split( '/\s+/u', trim( $name ), 2 );
if ( 2 === count( $parts ) ) {
$node['givenName'] = $parts[0];
$node['familyName'] = $parts[1];
}
$description = Seo::description( $post );
if ( '' !== $description ) {
$node['description'] = $description;
}
$thumb = (int) get_post_thumbnail_id( $post );
$photo = $thumb > 0 ? self::attachment( $thumb, 'full' ) : null;
if ( $photo ) {
$node['image'] = $photo;
}
if ( is_array( $node['@type'] ) ) {
$node['practicesAt'] = array( '@id' => $home . '#clinic' );
$node['medicalSpecialty'] = array( 'https://schema.org/Dentistry', 'https://schema.org/Pediatric' );
$node['isAcceptingNewPatients'] = true;
}
$education = self::lines( (string) get_post_meta( $post->ID, Meta_Fields::EDUCATION, true ) );
if ( $education ) {
$node['alumniOf'] = array_map(
static fn( string $school ): array => array(
'@type' => 'CollegeOrUniversity',
'name' => $school,
),
$education
);
}
$credentials = self::lines( (string) get_post_meta( $post->ID, Meta_Fields::CREDENTIALS, true ) );
if ( $credentials ) {
$node['hasCredential'] = array_map(
static fn( string $credential ): array => array(
'@type' => 'EducationalOccupationalCredential',
'name' => $credential,
),
$credentials
);
}
$topics = self::lines( (string) get_post_meta( $post->ID, Meta_Fields::TOPICS, true ) );
if ( $topics ) {
$node['knowsAbout'] = $topics;
}
$city = self::address( Settings::get( 'address' ) )['addressLocality'] ?? '';
$experience = trim( wp_strip_all_tags( (string) ( self::block_attributes( $post, 'smilia/doctor-page' )['experience'] ?? '' ) ) );
if ( '' !== $position ) {
$occupation = array(
'@type' => 'Occupation',
'name' => $position,
);
if ( '' !== $experience ) {
$occupation['description'] = $experience;
}
if ( '' !== $city ) {
$occupation['occupationLocation'] = array(
'@type' => 'City',
'name' => $city,
);
}
$node['hasOccupation'] = $occupation;
}
return $node;
}
private static function person_types( int $post_id ): array|string {
return 'person' === (string) get_post_meta( $post_id, Meta_Fields::ROLE, true ) ? 'Person' : array( 'Person', 'IndividualPhysician' );
}
private static function other_doctors( WP_Post $post, string $url ): array {
if ( Post_Types::DOCTOR !== $post->post_type ) {
return array();
}
$items = array();
foreach ( Content::doctors() as $doctor ) {
if ( (int) $doctor['id'] === (int) $post->ID ) {
continue;
}
$items[] = array(
'@type' => 'ListItem',
'position' => count( $items ) + 1,
'name' => $doctor['name'],
'url' => $doctor['url'],
);
}
if ( ! $items ) {
return array();
}
$title = trim( wp_strip_all_tags( (string) ( self::block_attributes( $post, 'smilia/doctor-page' )['othersTitle'] ?? '' ) ) );
$list = array(
'@type' => 'ItemList',
'@id' => $url . '#doctors',
'itemListElement' => $items,
);
if ( '' !== $title ) {
$list['name'] = $title;
}
return $list;
}
private static function faq( WP_Post $post, string $url ): array {
$questions = array();
$title = '';
foreach ( parse_blocks( $post->post_content ) as $block ) {
if ( 'smilia/faq' !== $block['blockName'] ) {
continue;
}
if ( '' === $title ) {
$type = \WP_Block_Type_Registry::get_instance()->get_registered( 'smilia/faq' );
$attrs = $type ? $type->prepare_attributes_for_render( (array) $block['attrs'] ) : (array) $block['attrs'];
$title = trim( preg_replace( '/\s+/u', ' ', wp_strip_all_tags( str_replace( '<br>', ' ', (string) ( $attrs['title'] ?? '' ) ) ) ) );
}
$questions = array_merge( $questions, self::questions( $block ) );
}
if ( ! $questions ) {
return array();
}
$node = array(
'@type' => 'FAQPage',
'@id' => $url . '#faq',
'inLanguage' => get_bloginfo( 'language' ),
'mainEntity' => $questions,
);
if ( '' !== $title ) {
$node['name'] = $title;
}
return $node;
}
private static function questions( array $block ): array {
return array_map(
static fn( array $item ): array => array(
'@type' => 'Question',
'name' => $item['question'],
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => trim( wp_strip_all_tags( preg_replace( '#<a(\s[^>]*)?>.*?</a>#is', '', $item['answer'] ) ) ),
),
),
Content::faq( (string) ( $block['attrs']['group'] ?? 'home' ) )
);
}
private static function reviews( WP_Post $post, string $home ): array {
$empty = array(
'board' => array(),
'nodes' => array(),
);
$board = has_block( 'smilia/reviews-board', $post );
$limit = $board ? 100 : ( has_block( 'smilia/reviews', $post ) ? 3 : 0 );
if ( $limit <= 0 ) {
return $empty;
}
$nodes = array();
foreach ( Content::reviews( $limit ) as $review ) {
$node = array(
'@type' => 'Review',
'@id' => $home . '#review-' . (int) $review['id'],
'author' => array(
'@type' => 'Person',
'name' => $review['author'],
),
'reviewBody' => $review['text'],
'datePublished' => get_post_time( DATE_W3C, true, (int) $review['id'] ),
'inLanguage' => get_bloginfo( 'language' ),
);
if ( '' !== $review['topic'] ) {
$node['name'] = $review['topic'];
}
if ( isset( self::SOURCES[ $review['source'] ] ) ) {
$publisher = array(
'@type' => 'Organization',
'name' => self::SOURCES[ $review['source'] ],
);
$link = Settings::get( 'review_' . $review['source'] );
if ( '' !== $link ) {
$publisher['url'] = $link;
}
$node['publisher'] = $publisher;
}
$nodes[] = $node;
}
if ( ! $nodes ) {
return $empty;
}
if ( ! $board ) {
return array(
'board' => array(),
'nodes' => $nodes,
);
}
return array(
'board' => array(
'@type' => 'ItemList',
'name' => get_the_title( $post ),
'itemListElement' => array_map(
static fn( array $node, int $index ): array => array(
'@type' => 'ListItem',
'position' => $index + 1,
'item' => array( '@id' => $node['@id'] ),
),
$nodes,
array_keys( $nodes )
),
),
'nodes' => $nodes,
);
}
private static function price_catalog( WP_Post $post, string $url ): array {
$attributes = self::block_attributes( $post, 'smilia/prices' );
if ( ! $attributes ) {
return array();
}
$sections = array();
foreach ( Prices::groups( (array) ( $attributes['groups'] ?? array() ) ) as $group ) {
$offers = array();
foreach ( $group['items'] as $item ) {
if ( null === $item['amount'] ) {
continue;
}
$offer = array(
'@type' => 'Offer',
'name' => $item['name'],
'priceSpecification' => array(
'@type' => 'PriceSpecification',
'minPrice' => $item['amount'],
'priceCurrency' => 'PLN',
),
);
if ( '' !== $group['url'] ) {
$offer['itemOffered'] = array(
'@type' => 'Service',
'@id' => $group['url'] . '#service',
'name' => $group['title'],
'url' => $group['url'],
);
}
$offers[] = $offer;
}
if ( ! $offers ) {
continue;
}
$section = array(
'@type' => 'OfferCatalog',
'name' => $group['title'],
'itemListElement' => $offers,
);
if ( '' !== $group['url'] ) {
$section['url'] = $group['url'];
}
$sections[] = $section;
}
if ( ! $sections ) {
return array();
}
return array(
'@type' => 'OfferCatalog',
'@id' => $url . '#catalog',
'name' => get_the_title( $post ),
'itemListElement' => $sections,
);
}
private static function price_groups(): array {
if ( null !== self::$price_groups ) {
return self::$price_groups;
}
$pages = get_posts(
array(
'post_type' => 'page',
'name' => 'prices',
'post_status' => 'publish',
'numberposts' => 1,
'lang' => self::language(),
)
);
self::$price_groups = $pages ? Prices::groups( (array) ( self::block_attributes( $pages[0], 'smilia/prices' )['groups'] ?? array() ) ) : array();
return self::$price_groups;
}
private static function price_span(): ?array {
if ( null !== self::$price_span ) {
return self::$price_span ?: null;
}
self::$price_span = array();
$pages = get_posts(
array(
'post_type' => 'page',
'name' => 'prices',
'post_status' => 'publish',
'numberposts' => 1,
'lang' => '',
)
);
if ( ! $pages ) {
return null;
}
$amounts = array();
foreach ( (array) ( self::block_attributes( $pages[0], 'smilia/prices' )['groups'] ?? array() ) as $group ) {
foreach ( Prices::groups( array( $group ) ) as $parsed ) {
foreach ( $parsed['items'] as $item ) {
if ( null !== $item['amount'] ) {
$amounts[] = $item['amount'];
}
}
}
}
if ( $amounts ) {
self::$price_span = array(
'min' => min( $amounts ),
'max' => max( $amounts ),
);
}
return self::$price_span ?: null;
}
}