3, 'title' => __('Rooster One Store Locator', 'ai-eshop-optimizer'), 'description' => __('Grid or list of all your store locations — address, phone, opening hours and Google Maps / review links.', 'ai-eshop-optimizer'), 'category' => 'widgets', 'icon' => 'location-alt', 'keywords' => array('store', 'locator', 'locations', 'shops'), 'supports' => array('align' => array('wide', 'full'), 'spacing' => array('margin' => true, 'padding' => true)), 'attributes' => array( 'layout' => array('type' => 'string', 'default' => 'grid'), 'columns' => array('type' => 'number', 'default' => 2), 'showPhoto' => array('type' => 'boolean', 'default' => true), 'showHours' => array('type' => 'boolean', 'default' => true), 'showMaps' => array('type' => 'boolean', 'default' => true), 'showRateUs' => array('type' => 'boolean', 'default' => true), 'showReviews' => array('type' => 'boolean', 'default' => true), 'reviewsCount' => array('type' => 'number', 'default' => 3), 'reviewsMinRating' => array('type' => 'number', 'default' => 0), 'reviewsSlider' => array('type' => 'boolean', 'default' => true), 'onlyHq' => array('type' => 'boolean', 'default' => false), 'heading' => array('type' => 'string', 'default' => ''), ), 'uses_context' => array('postId', 'postType'), 'render_callback' => array(__CLASS__, 'render'), )); } public static function shortcode($atts) { $atts = shortcode_atts(array( 'layout' => 'grid', 'columns' => 2, 'show_hours' => 1, 'show_maps' => 1, 'show_rate_us' => 1, 'only_hq' => 0, 'heading' => '', ), $atts, 'aieo_store_locator'); return self::render(array( 'layout' => (string) $atts['layout'], 'columns' => (int) $atts['columns'], 'showHours' => (bool) (int) $atts['show_hours'], 'showMaps' => (bool) (int) $atts['show_maps'], 'showRateUs' => (bool) (int) $atts['show_rate_us'], 'onlyHq' => (bool) (int) $atts['only_hq'], 'heading' => (string) $atts['heading'], )); } public static function render($attrs, $content = '', $block = null) { if (!class_exists('AIEO_Google_Reviews_Locations') || !class_exists('AIEO_Google_Reviews_Helpers')) { return ''; } $attrs = wp_parse_args(is_array($attrs) ? $attrs : array(), array( 'layout' => 'grid', 'columns' => 2, 'showPhoto' => true, 'showHours' => true, 'showMaps' => true, 'showRateUs' => true, 'showReviews' => true, 'reviewsCount' => 3, 'reviewsMinRating' => 0, 'reviewsSlider' => true, 'onlyHq' => false, 'heading' => '', )); if ('others' === $attrs['layout']) { return class_exists('AIEO_Store_Page_Blocks') ? AIEO_Store_Page_Blocks::others($block) : ''; } $locs = AIEO_Google_Reviews_Locations::list_in_language(); if (!empty($attrs['onlyHq'])) { $locs = array_values(array_filter($locs, static function ($l) { return !empty($l['is_hq']); })); } if (empty($locs)) { return ''; } AIEO_Google_Reviews_Helpers::enqueue_locations_assets(); if ('shops' === $attrs['layout']) { return self::render_shops($locs, $attrs, $block); } $cols = max(1, min(4, (int) $attrs['columns'])); $layout = in_array($attrs['layout'], array('list', 'grid'), true) ? $attrs['layout'] : 'grid'; $wrap = get_block_wrapper_attributes(array('class' => 'aieo-store-locator aieo-store-locator--' . $layout, 'style' => '--aieo-loc-cols:' . $cols . ';')); static $css_done = false; $css = ''; if (!$css_done) { $css_done = true; $css = ''; } $out = $css . '
'; if ('' !== (string) $attrs['heading']) { $out .= '

' . esc_html($attrs['heading']) . '

'; } $out .= '
'; foreach ($locs as $loc) { $name = (string) ($loc['label'] ?? ''); if ('' === $name) { $name = (string) ($loc['google_name'] ?? ''); } if ('' === $name) { continue; } $page = class_exists('AIEO_SEO_Location_Page') ? (string) AIEO_SEO_Location_Page::url_for($loc) : ''; $name_html = '' !== $page ? '' . esc_html($name) . '' : esc_html($name); $out .= '
'; if (!empty($attrs['showPhoto'])) { $out .= self::photo_for_location($loc, $page); } $out .= '

' . $name_html . '

'; $out .= AIEO_Google_Reviews_Helpers::render_nap_html($loc); if (!empty($attrs['showHours'])) { $out .= AIEO_Google_Reviews_Helpers::render_hours_html($loc); } $out .= AIEO_Google_Reviews_Helpers::render_action_links_html($loc, !empty($attrs['showMaps']), !empty($attrs['showRateUs'])); if (!empty($attrs['showReviews']) && class_exists('AIEO_Google_Reviews_Block')) { $reviews = AIEO_Google_Reviews_Block::render(array( 'mode' => 'single', 'locationSlug' => (string) ($loc['slug'] ?? ''), 'showOverall' => false, 'showRateUs' => false, 'maxCount' => max(0, (int) $attrs['reviewsCount']), 'minRating' => max(0, (int) $attrs['reviewsMinRating']), 'layout' => 'list', )); if ('' !== trim((string) $reviews)) { $out .= '
' . $reviews . '
'; } } $out .= '
'; } $out .= '
'; return $out; } public static function store_post_for_location($loc) { static $found = array(); $key = (string) ($loc['id'] ?? '') . '|' . (string) ($loc['slug'] ?? ''); if (isset($found[$key])) { return $found[$key]; } $metas = array(); if (!empty($loc['id'])) { $metas[] = array('key' => '_aieo_grv_location_id', 'value' => (string) (int) $loc['id']); } if (!empty($loc['slug'])) { $metas[] = array('key' => '_aieo_grv_location_slug', 'value' => (string) $loc['slug']); } if (!$metas) { return $found[$key] = 0; } $ids = get_posts(array( 'post_type' => array('aieo_store', 'store_location'), 'post_status' => 'publish', 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => true, 'meta_query' => array_merge(array('relation' => 'OR'), $metas), )); $id = $ids ? (int) $ids[0] : 0; if ($id && has_filter('wpml_object_id')) { $shown = (int) apply_filters('wpml_object_id', $id, (string) get_post_type($id), true); $id = $shown > 0 ? $shown : $id; } return $found[$key] = $id; } private static function photo_for_location($loc, $link = '') { $pid = self::store_post_for_location($loc); if (!$pid || !has_post_thumbnail($pid)) { return ''; } $img = get_the_post_thumbnail($pid, 'large', array('loading' => 'lazy', 'class' => 'aieo-store-card__photo-img')); $href = '' !== (string) $link ? (string) $link : get_permalink($pid); return '' . $img . ''; } private static function render_shops(array $locs, array $attrs, $block = null) { self::enqueue_shops_style(); $cols = max(1, min(3, (int) $attrs['columns'])); $attrs_html = array( 'class' => 'aieo-shops-wrap', 'data-rooster-ui' => 'rooster-one/store_pages', 'data-aieo-shops' => (string) wp_json_encode(AIEO_Store_Hours::config()), ); $wrap = ($block instanceof WP_Block) ? get_block_wrapper_attributes($attrs_html) : self::attrs_html($attrs_html); $out = '
'; if ('' !== (string) $attrs['heading']) { $out .= '

' . esc_html($attrs['heading']) . '

'; } $cards = array_values(array_filter(array_map(static function ($loc) use ($attrs) { return self::shop_card($loc, $attrs); }, $locs), 'strlen')); $cols = max(1, min($cols, count($cards))); $out .= '
' . implode('', $cards); AIEO_Store_Hours::need(); return $out . '
'; } private static function shop_card(array $loc, array $attrs) { $name = (string) ($loc['label'] ?? ''); if ('' === $name) { $name = (string) ($loc['google_name'] ?? ''); } if ('' === $name) { return ''; } $pid = self::store_post_for_location($loc); $page = class_exists('AIEO_SEO_Location_Page') ? (string) AIEO_SEO_Location_Page::url_for($loc) : ''; if ('' === $page && $pid) { $page = (string) get_permalink($pid); } $tz = AIEO_Store_Hours::timezone($loc, $pid); $zone = AIEO_Store_Hours::zone_attrs($tz); $now = AIEO_Store_Hours::now(null, $tz); $iv = AIEO_Store_Hours::intervals($loc); $state = AIEO_Store_Hours::state($iv, $now); $out = '
'; if (!empty($attrs['showPhoto']) && $pid && has_post_thumbnail($pid)) { $inside = (int) get_post_meta($pid, AIEO_Store_CPT::META_INSIDE_PHOTO, true); $inside = ($inside && wp_attachment_is_image($inside)) ? $inside : 0; $front = get_the_post_thumbnail($pid, 'large', array('loading' => 'lazy', 'class' => 'aieo-shop__front')); $out .= '
'; $out .= '' !== $page ? '' : $front; if ($inside) { $alt = trim((string) get_post_meta($inside, '_wp_attachment_image_alt', true)); $alt = '' !== $alt ? $alt : sprintf(__('Inside %s', 'ai-eshop-optimizer'), $name); $out .= wp_get_attachment_image($inside, 'large', false, array('class' => 'aieo-shop__inside', 'loading' => 'lazy', 'alt' => $alt)); $out .= ''; } $out .= '
'; } $out .= '
'; $out .= '

' . ('' !== $page ? '' . esc_html($name) . '' : esc_html($name)) . '

'; if (null !== $state) { $out .= '

' . esc_html(AIEO_Store_Hours::state_words($state, $now)) . '

'; } $out .= '
'; $struct = array_filter(array( (string) ($loc['street'] ?? ''), trim(AIEO_Store_Contact::postcode($loc['postcode'] ?? '') . ' ' . (string) ($loc['locality'] ?? '')), (string) ($loc['region'] ?? ''), ), 'strlen'); $addr = !empty($struct) ? implode(', ', $struct) : (string) ($loc['address'] ?? ''); $maps = AIEO_Google_Reviews_Helpers::maps_url($loc); $phone = trim((string) ($loc['phone'] ?? '')); if ('' !== $addr || '' !== $phone) { $out .= '
'; if ('' !== $addr) { $out .= '

' . ('' !== $maps ? '' . esc_html($addr) . '' : esc_html($addr)) . '

'; } if ('' !== $phone) { $out .= AIEO_Store_Contact::phone($loc, 'aieo-shop__phone', $state, $now); } $out .= '
'; } $acts = ''; if ('' !== $page) { $acts .= '' . esc_html__('See the shop', 'ai-eshop-optimizer') . ''; } if (!empty($attrs['showMaps']) && '' !== $maps) { $acts .= '' . esc_html__('Get directions', 'ai-eshop-optimizer') . self::icon('arrow-up-right') . ''; } if ('' !== $acts) { $out .= '
' . $acts . '
'; } if (!empty($attrs['showHours'])) { $rows = AIEO_Store_Hours::rows($iv); if ($rows) { $today = intdiv(($now % AIEO_Store_Hours::WEEK + AIEO_Store_Hours::WEEK) % AIEO_Store_Hours::WEEK, 1440); $words = AIEO_Store_Hours::words(); $table = ''; foreach ($rows as $r) { $on = in_array($today, $r['days'], true); $spans = implode(', ', array_map(static function ($t) { return '' . esc_html($t) . ''; }, explode(', ', (string) $r['hours']))); $table .= ''; } $out .= self::row('clock', __('Hours', 'ai-eshop-optimizer'), $table . '
' . esc_html__('Opening hours', 'ai-eshop-optimizer') . '
' . esc_html($r['label']) . ' (' . esc_html($words['today']) . ')' . $spans . '
'); } } if ($pid) { foreach (array( array(AIEO_Store_CPT::META_IN_SHOP, 'shopping-bag', __('In the shop', 'ai-eshop-optimizer')), array(AIEO_Store_CPT::META_GETTING_HERE, 'map-pin', __('Getting here', 'ai-eshop-optimizer')), ) as $field) { $text = trim((string) get_post_meta($pid, $field[0], true)); if ('' !== $text) { $out .= self::row($field[1], $field[2], '
' . wpautop(esc_html($text)) . '
'); } } } $out .= '
' . self::reviews_row($loc, $attrs); return $out . '
'; } private static function reviews_row(array $loc, array $attrs) { $body = ''; $rating = isset($loc['baseline_rating']) && '' !== (string) $loc['baseline_rating'] ? (float) $loc['baseline_rating'] : 0.0; $count = isset($loc['baseline_review_count']) ? (int) $loc['baseline_review_count'] : 0; $line = ''; if ($rating > 0 && $count > 0) { $line = sprintf(esc_html(_n('%1$s from %2$s Google review', '%1$s from %2$s Google reviews', $count, 'ai-eshop-optimizer')), '' . esc_html(number_format_i18n($rating, 1)) . '', esc_html(number_format_i18n($count))); } $rate = !empty($attrs['showRateUs']) ? AIEO_Google_Reviews_Helpers::review_url($loc) : ''; if ('' !== $rate) { $line .= ('' !== $line ? ' · ' : '') . '' . esc_html__('Rate us on Google', 'ai-eshop-optimizer') . ''; } if ('' !== $line) { $body .= '

' . $line . '

'; } if (!empty($attrs['showReviews']) && class_exists('AIEO_Google_Reviews_Block')) { $reviews = AIEO_Google_Reviews_Block::render(array( 'mode' => 'single', 'locationSlug' => (string) ($loc['slug'] ?? ''), 'showOverall' => false, 'showRateUs' => false, 'maxCount' => !empty($attrs['reviewsSlider']) ? 50 : max(0, (int) $attrs['reviewsCount']), 'minRating' => max(0, (int) $attrs['reviewsMinRating']), 'layout' => 'list', 'presentation' => !empty($attrs['reviewsSlider']) ? 'slider' : 'static', )); if ('' !== trim((string) $reviews) && false === strpos((string) $reviews, 'aieo-grv__empty')) { $body .= '
' . $reviews . '
'; } } return '' !== $body ? self::row('star', __('Reviews', 'ai-eshop-optimizer'), $body) : ''; } private static function row($icon, $label, $content) { return '

' . self::icon($icon) . '' . esc_html($label) . '

' . $content . '
'; } private static function icon($slug) { if (function_exists('rooster_icon') && function_exists('rooster_has_icon') && rooster_has_icon($slug)) { return (string) rooster_icon($slug, array('class' => 'aieo-shop__icon')); } return ''; } private static function attrs_html(array $attrs) { $out = array(); foreach ($attrs as $k => $v) { $out[] = esc_attr($k) . '="' . esc_attr($v) . '"'; } return implode(' ', $out); } private static function enqueue_shops_style() { if (!wp_style_is('aieo-store-shops', 'registered')) { wp_register_style('aieo-store-shops', false, array(), null); wp_add_inline_style('aieo-store-shops', self::shops_css()); add_filter('rooster/used_css/safelist', array(__CLASS__, 'used_css_safelist')); } wp_enqueue_style('aieo-store-shops'); } public static function used_css_safelist($list) { $list = (array) $list; $list[] = '/^(is-closed|is-today|is-inside|aieo-shop__phone-off|aieo-shop__phone-note)$/'; return $list; } public static function shops_css() { $muted = 'var(--wp--preset--color--muted,color-mix(in srgb,CanvasText 62%,Canvas))'; $line = 'var(--wp--preset--color--line,color-mix(in srgb,CanvasText 18%,Canvas))'; $gap = 'var(--wp--preset--spacing--3,1rem)'; return '.aieo-shops{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:var(--wp--preset--spacing--6,3.5rem) var(--wp--preset--spacing--4,1.25rem)}' . '.aieo-shops--cols-1{grid-template-columns:minmax(0,1fr)}' . '.aieo-shops--cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}' . '@media(max-width:781px){.aieo-shops,.aieo-shops--cols-3{grid-template-columns:minmax(0,1fr)}}' . '.aieo-shop,.aieo-shop__body{display:flex;flex-direction:column;min-width:0}' . '@media(min-width:782px){' . '.aieo-shops--solo .aieo-shop:has(>.aieo-shop__img){display:grid;grid-template-columns:minmax(0,1.1fr) minmax(0,1fr);column-gap:var(--wp--preset--spacing--6,3.5rem);align-items:stretch}' . '.aieo-shops--solo .aieo-shop>.aieo-shop__img{margin:0;aspect-ratio:auto;min-height:22rem}' . '.aieo-shops--solo .aieo-shop>.aieo-shop__row{grid-column:1/-1;margin-top:var(--wp--preset--spacing--5,2rem)}' . '}' . '.aieo-shop__img{position:relative;margin:0 0 ' . $gap . ';aspect-ratio:3/2;overflow:hidden;border-radius:var(--wp--custom--rooster--shape--media,0)}' . '.aieo-shop__img a{display:block;height:100%}' . '.aieo-shop__img img{display:block;width:100%;height:100%;object-fit:cover}' . '.aieo-shop__img .aieo-shop__inside{position:absolute;inset:0;opacity:0;transition:opacity .3s ease}' . '.aieo-shop__img.is-inside .aieo-shop__inside{opacity:1}' . '@media(prefers-reduced-motion:reduce){.aieo-shop__img .aieo-shop__inside{transition:none}}' . '.aieo-shop__look{position:absolute;inset-inline-end:var(--wp--preset--spacing--2,.75rem);inset-block-end:var(--wp--preset--spacing--2,.75rem);display:inline-flex;align-items:center;gap:.5rem;min-height:44px;padding:.5rem 1rem;border:0;border-radius:var(--wp--custom--rooster--shape--control,0);background:var(--wp--preset--color--bg,Canvas);color:var(--wp--preset--color--fg,CanvasText);font:inherit;font-size:var(--wp--preset--font-size--sm,.875rem);cursor:pointer}' . '.aieo-shop__look[aria-pressed="true"] .aieo-shop__look-off,.aieo-shop__look:not([aria-pressed="true"]) .aieo-shop__look-on{display:none}' . '.aieo-shop__head{display:flex;flex-wrap:wrap;align-items:baseline;align-items:last baseline;gap:.25rem 1rem}' . '.aieo-shop__name{flex:1 1 16rem;min-width:0;margin:0;font-size:var(--wp--preset--font-size--xx-large,clamp(2rem,1.4rem + 2.2vw,3.25rem));line-height:1.05}' . '.aieo-shop__name a{color:inherit;text-decoration:none}' . '.aieo-shop__state{flex:0 0 auto;display:inline-flex;align-items:center;gap:.5rem;margin:0 0 0 auto;color:' . $muted . ';font-size:var(--wp--preset--font-size--sm,.9375rem)}' . '.aieo-shop__state::before{content:"";flex:none;width:.5rem;height:.5rem;border-radius:50%;background:currentColor}' . '.aieo-shop__state.is-open{color:var(--wp--preset--color--success-ink,var(--wp--preset--color--success,CanvasText))}' . '@media(max-width:600px){.aieo-shop__state{margin-inline-start:0}}' . '.aieo-shop__nap{margin:var(--wp--preset--spacing--2,.75rem) 0 0;font-style:normal;line-height:1.6}' . '.aieo-shop__nap p{margin:0}' . '.aieo-shop__addr a{color:inherit;text-decoration:none}' . '.aieo-shop__addr a:hover{text-decoration:underline}' . '.aieo-shop__phone a{color:inherit}' . '.aieo-shop__phone-note{color:' . $muted . '}' . '.aieo-shop__acts{display:flex;flex-wrap:wrap;gap:.75rem;margin:var(--wp--preset--spacing--4,1.5rem) 0}' . '.aieo-shop__btn{display:inline-flex;align-items:center;justify-content:center;gap:.5rem;text-decoration:none}' . '@media(max-width:781px){.aieo-shop__acts .aieo-shop__btn{flex:1 1 100%}}' . '.aieo-shop__icon{flex:none;width:20px;height:20px}' . '.aieo-shop__row{display:grid;grid-template-columns:minmax(8rem,11rem) minmax(0,1fr);align-items:start;gap:' . $gap . ' var(--wp--preset--spacing--4,1.5rem);padding:var(--wp--preset--spacing--3,1.25rem) 0;border-top:1px solid ' . $line . '}' . '@media(max-width:600px){.aieo-shop__row{grid-template-columns:minmax(0,1fr);gap:.5rem}}' . '.aieo-shop__label{display:flex;align-items:center;gap:.625rem;margin:0;font-size:var(--wp--preset--font-size--md,1rem);font-weight:600;line-height:1.4}' . '.aieo-shop__value>:first-child{margin-top:0}' . '.aieo-shop__value>:last-child{margin-bottom:0}' . '.aieo-shop__hours{width:100%;border-collapse:collapse}' . '.aieo-shop__hours caption{caption-side:top}' . '.aieo-shop__hours th{padding:.125rem 1rem .125rem 0;font-weight:400;text-align:start;vertical-align:top}' . '.aieo-shop__hours td{padding:.125rem 0;text-align:end;vertical-align:top}' . '.aieo-shop__hours td>span{white-space:nowrap}' . '.aieo-shop__hours tr.is-today th,.aieo-shop__hours tr.is-today td{font-weight:700}' . '.aieo-shop__today{font-weight:400;color:' . $muted . '}' . '.aieo-shop__text p{margin:0 0 .5rem}' . '.aieo-shop__rating{margin:0 0 .5rem}' . '.aieo-shop__reviews{margin-top:.5rem}' . '.aieo-stores-head{display:grid;grid-template-columns:minmax(0,1.6fr) minmax(0,1fr);align-items:start;gap:var(--wp--preset--spacing--3,1rem) var(--wp--preset--spacing--6,3rem)}' . '.aieo-stores-head>*{margin:0}' . '@media(max-width:781px){.aieo-stores-head{grid-template-columns:minmax(0,1fr)}}'; } }