array( 'stroke', 'M12 2a10 10 0 1 0 0 20a10 10 0 1 0 0-20zM12 6v6l4 2' ), 'email' => array( 'stroke', 'M4 4h16a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2zM22 7l-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7' ), 'call' => array( 'stroke', 'M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z' ), 'whatsapp' => array( 'fill', 'M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413Z' ), 'viber' => array( 'fill', 'M11.4 0C9.473.028 5.333.344 3.02 2.467 1.302 4.187.696 6.7.633 9.817.57 12.933.488 18.776 6.12 20.36h.003l-.004 2.416s-.037.977.61 1.177c.777.242 1.234-.5 1.98-1.302.407-.44.972-1.084 1.397-1.58 3.85.326 6.812-.416 7.15-.525.776-.252 5.176-.816 5.892-6.657.74-6.02-.36-9.83-2.34-11.546-.596-.55-3.006-2.3-8.375-2.323 0 0-.395-.025-1.037-.017zm.058 1.693c.545-.004.88.017.88.017 4.542.02 6.717 1.388 7.222 1.846 1.675 1.435 2.53 4.868 1.906 9.897v.002c-.604 4.878-4.174 5.184-4.832 5.395-.28.09-2.882.737-6.153.524 0 0-2.436 2.94-3.197 3.704-.12.12-.26.167-.352.144-.13-.033-.166-.188-.165-.414l.02-4.018c-4.762-1.32-4.485-6.292-4.43-8.895.054-2.604.543-4.738 1.996-6.173 1.96-1.773 5.474-2.018 7.11-2.03zm.38 2.602c-.167 0-.303.135-.304.302 0 .167.133.303.3.305 1.624.01 2.946.537 4.028 1.592 1.073 1.046 1.62 2.468 1.633 4.334.002.167.14.3.307.3.166-.002.3-.138.3-.304-.014-1.984-.618-3.596-1.816-4.764-1.19-1.16-2.692-1.753-4.447-1.765zm-3.96.695c-.19-.032-.4.005-.616.117l-.01.002c-.43.247-.816.562-1.146.932-.002.004-.006.004-.008.008-.267.323-.42.638-.46.948-.008.046-.01.093-.007.14 0 .136.022.27.065.4l.013.01c.135.48.473 1.276 1.205 2.604.42.768.903 1.5 1.446 2.186.27.344.56.673.87.984l.132.132c.31.308.64.6.984.87.686.543 1.418 1.027 2.186 1.447 1.328.733 2.126 1.07 2.604 1.206l.01.014c.13.042.265.064.402.063.046.002.092 0 .138-.008.31-.036.627-.19.948-.46.004 0 .003-.002.008-.005.37-.33.683-.72.93-1.148l.003-.01c.225-.432.15-.842-.18-1.12-.004 0-.698-.58-1.037-.83-.36-.255-.73-.492-1.113-.71-.51-.285-1.032-.106-1.248.174l-.447.564c-.23.283-.657.246-.657.246-3.12-.796-3.955-3.955-3.955-3.955s-.037-.426.248-.656l.563-.448c.277-.215.456-.737.17-1.248-.217-.383-.454-.756-.71-1.115-.25-.34-.826-1.033-.83-1.035-.137-.165-.31-.265-.502-.297zm4.49.88c-.158.002-.29.124-.3.282-.01.167.115.312.282.324 1.16.085 2.017.466 2.645 1.15.63.688.93 1.524.906 2.57-.002.168.13.306.3.31.166.003.305-.13.31-.297.025-1.175-.334-2.193-1.067-2.994-.74-.81-1.777-1.253-3.05-1.346h-.024zm.463 1.63c-.16.002-.29.127-.3.287-.008.167.12.31.288.32.523.028.875.175 1.113.422.24.245.388.62.416 1.164.01.167.15.295.318.287.167-.008.295-.15.287-.317-.03-.644-.215-1.178-.58-1.557-.367-.378-.893-.574-1.52-.607h-.018z' ), 'messenger' => array( 'fill', 'M.001 11.639C.001 4.949 5.241 0 12.001 0S24 4.95 24 11.639c0 6.689-5.24 11.638-12 11.638-1.21 0-2.38-.16-3.47-.46a.96.96 0 00-.64.05l-2.39 1.05a.96.96 0 01-1.35-.85l-.07-2.14a.97.97 0 00-.32-.68A11.39 11.389 0 01.002 11.64zm8.32-2.19l-3.52 5.6c-.35.53.32 1.139.82.75l3.79-2.87c.26-.2.6-.2.87 0l2.8 2.1c.84.63 2.04.4 2.6-.48l3.52-5.6c.35-.53-.32-1.13-.82-.75l-3.79 2.87c-.25.2-.6.2-.86 0l-2.8-2.1a1.8 1.8 0 00-2.61.48z' ), ); const HEADER_STYLE = 'aieo-header-contact'; const DIALOG_STYLE = 'aieo-hours-dialog'; const DISPLAYS = array( 'inline', 'dialog' ); public static function display( $value ) { return 'dialog' === (string) $value ? 'dialog' : 'inline'; } private static $rows = null; private static $hours_logic = false; private static $dialogs = array(); private static $hours_js = null; public static function register() { add_filter( 'rooster/header/contact', array( __CLASS__, 'header_contact' ), 10, 2 ); } public static function all() { if ( null !== self::$rows ) { return self::$rows; } self::$rows = array(); if ( ! self::load() ) { return self::$rows; } global $wpdb; $table = AIEO_Google_Reviews_Locations::table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) !== $table ) { return self::$rows; } foreach ( AIEO_Google_Reviews_Locations::list_in_language() as $row ) { self::$rows[ (string) $row['slug'] ] = $row; } return self::$rows; } public static function flush() { self::$rows = null; self::$hours_logic = false; self::$dialogs = array(); } public static function choices() { $out = array(); foreach ( self::all() as $slug => $row ) { $out[ $slug ] = '' !== (string) $row['label'] ? (string) $row['label'] : $slug; } return $out; } public static function location( $slug = '' ) { $all = self::all(); $slug = sanitize_key( (string) $slug ); if ( '' !== $slug && isset( $all[ $slug ] ) ) { return $all[ $slug ]; } foreach ( $all as $row ) { if ( ! empty( $row['is_hq'] ) ) { return $row; } } return $all ? reset( $all ) : null; } public static function link( $channel, $value, $country = '' ) { $value = trim( (string) $value ); if ( '' === $value ) { return ''; } switch ( $channel ) { case 'call': $tel = (string) preg_replace( '/[^\d+]/', '', $value ); $tel = 0 === strpos( $tel, '00' ) ? '+' . substr( $tel, 2 ) : $tel; return preg_match( '/^\+?\d{5,15}$/', $tel ) ? 'tel:' . $tel : ''; case 'whatsapp': if ( preg_match( '#^(https://(wa\.me|api\.whatsapp\.com|chat\.whatsapp\.com)/|whatsapp://)#i', $value ) ) { return $value; } $digits = self::international( $value, $country ); return '' !== $digits ? 'https://wa.me/' . $digits : ''; case 'viber': if ( preg_match( '#^(viber://|https://(invite\.viber\.com|vb\.me)/)#i', $value ) ) { return $value; } $digits = self::international( $value, $country ); return '' !== $digits ? 'viber://chat?number=%2B' . $digits : ''; case 'messenger': if ( preg_match( '#^(?:https?://)?(?:www\.|m\.)?(?:m\.me|messenger\.com/t|facebook\.com)/([A-Za-z0-9._-]+)/?$#i', $value, $m ) && 'profile.php' !== strtolower( $m[1] ) ) { return 'https://m.me/' . $m[1]; } if ( preg_match( '#^https://(www\.)?messenger\.com/#i', $value ) ) { return $value; } return preg_match( '/^[A-Za-z0-9._-]{3,}$/', $value ) ? 'https://m.me/' . $value : ''; } return ''; } private static function international( $value, $country ) { if ( preg_match( '/[^\d\s+().\/-]/', $value ) ) { return ''; } $digits = (string) preg_replace( '/\D/', '', $value ); if ( 0 === strpos( ltrim( $value ), '+' ) ) { $intl = $digits; } elseif ( 0 === strpos( $digits, '00' ) ) { $intl = substr( $digits, 2 ); } else { $code = ''; if ( '' !== (string) $country && function_exists( 'WC' ) && WC()->countries && method_exists( WC()->countries, 'get_country_calling_code' ) ) { $code = WC()->countries->get_country_calling_code( strtoupper( (string) $country ) ); $code = is_array( $code ) ? (string) reset( $code ) : (string) $code; } $code = (string) preg_replace( '/\D/', '', $code ); if ( '' === $code ) { return ''; } $intl = $code . ltrim( $digits, '0' ); } return strlen( $intl ) >= 8 && strlen( $intl ) <= 15 ? $intl : ''; } public static function channels( array $loc, array $wanted = self::CHANNELS ) { $out = array(); $place = (string) ( $loc['label'] ?? '' ); $country = (string) ( $loc['country'] ?? '' ); foreach ( self::CHANNELS as $channel ) { if ( ! in_array( $channel, $wanted, true ) ) { continue; } $value = 'call' === $channel ? (string) ( $loc['phone'] ?? '' ) : (string) ( $loc[ $channel ] ?? '' ); $url = self::link( $channel, $value, $country ); if ( '' === $url ) { continue; } if ( 'call' === $channel ) { $text = trim( $value ); $name = sprintf( __( 'Call %1$s, %2$s', 'ai-eshop-optimizer' ), $place, $text ); } else { $text = array( 'whatsapp' => 'WhatsApp', 'viber' => 'Viber', 'messenger' => 'Messenger' )[ $channel ]; $name = sprintf( __( '%1$s: %2$s', 'ai-eshop-optimizer' ), $text, $place ); } $out[ $channel ] = array( 'url' => $url, 'text' => $text, 'name' => '' !== $place ? $name : $text ); } return $out; } public static function icon( $channel ) { if ( ! isset( self::ICON_PATHS[ $channel ] ) ) { return ''; } list( $paint, $d ) = self::ICON_PATHS[ $channel ]; $paint = 'stroke' === $paint ? ' fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"' : ' fill="currentColor"'; return ''; } public static function email( $loc ) { $email = is_array( $loc ) ? sanitize_email( (string) ( $loc['email'] ?? '' ) ) : ''; return is_email( $email ) ? $email : ''; } public static function parse( $list ) { $list = strtolower( trim( (string) $list ) ); if ( 'none' === $list ) { return array(); } if ( 'all' === $list ) { return self::CHANNELS; } return array_values( array_intersect( self::CHANNELS, array_map( 'trim', explode( ',', $list ) ) ) ); } public static function buttons( $loc, array $channels, $icons = true, $hours = false, $display = 'inline' ) { $display = self::display( $display ); if ( ! is_array( $loc ) || ! $channels ) { return ''; } $html = ''; foreach ( self::channels( $loc, $channels ) as $channel => $c ) { $icon = $icons ? self::icon( $channel ) : ''; $week = ( $hours && 'call' === $channel ) ? self::week( $loc ) : array(); $attrs = $week ? self::call_hours_attrs( $week, (string) ( $loc['slug'] ?? '' ), $display ) : ''; $words = '' . esc_html( $c['text'] ) . ''; if ( $week && 'inline' === $display ) { $words = '' . $words . '' . esc_html( self::hours_summary( $week ) ) . ''; } $html .= '' . $icon . $words . '' . ( $week ? self::hours_for( $loc, $display ) : '' ); } return $html; } public static function style() { if ( wp_style_is( self::STYLE, 'enqueued' ) ) { return; } if ( ! wp_style_is( self::STYLE, 'registered' ) && defined( 'AIEO_DIR' ) ) { $base = defined( 'AIEO_FILE' ) ? AIEO_FILE : AIEO_DIR . 'ai-eshop-optimizer.php'; $rel = ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && is_readable( AIEO_DIR . 'assets/css/aieo-location-contact.min.css' ) ? 'assets/css/aieo-location-contact.min.css' : 'assets/css/aieo-location-contact.css'; $ver = is_readable( AIEO_DIR . $rel ) ? (string) filemtime( AIEO_DIR . $rel ) : ( defined( 'AIEO_VERSION' ) ? AIEO_VERSION : '1' ); wp_register_style( self::STYLE, plugins_url( $rel, $base ), array(), $ver ); } wp_enqueue_style( self::STYLE ); } public static function week( array $loc ) { $periods = json_decode( (string) ( $loc['hours_json'] ?? '' ), true ); if ( ! is_array( $periods ) ) { return array(); } $out = array(); foreach ( $periods as $p ) { if ( ! is_array( $p ) || ! isset( $p['open']['day'] ) ) { continue; } $open = self::minute( (int) $p['open']['day'], (string) ( $p['open']['time'] ?? '' ) ); if ( null === $open ) { continue; } if ( ! isset( $p['close']['day'] ) ) { if ( 0 === $open ) { return array( array( 0, self::WEEK ) ); } continue; } $close = self::minute( (int) $p['close']['day'], (string) ( $p['close']['time'] ?? '' ) ); if ( null === $close ) { continue; } if ( $close <= $open ) { $close += self::WEEK; } $out[] = array( $open, $close ); } usort( $out, static function ( $a, $b ) { return $a[0] <=> $b[0]; } ); return $out; } private static function minute( $day, $hhmm ) { $hhmm = (string) preg_replace( '/\D/', '', $hhmm ); if ( 4 !== strlen( $hhmm ) || $day < 0 || $day > 6 ) { return null; } $h = (int) substr( $hhmm, 0, 2 ); $m = (int) substr( $hhmm, 2, 2 ); if ( $h > 24 || $m > 59 || ( 24 === $h && $m > 0 ) ) { return null; } return $day * 1440 + $h * 60 + $m; } public static function time_words( $minutes ) { $minutes = ( (int) $minutes ) % 1440; $format = (string) get_option( 'time_format', 'H:i' ); return function_exists( 'wp_date' ) ? (string) wp_date( '' !== $format ? $format : 'H:i', $minutes * 60, new DateTimeZone( 'UTC' ) ) : sprintf( '%d:%02d', intdiv( $minutes, 60 ), $minutes % 60 ); } public static function hours_summary( array $week ) { if ( ! $week ) { return ''; } if ( 1 === count( $week ) && 0 === $week[0][0] && self::WEEK <= $week[0][1] ) { return __( 'Open 24 hours', 'ai-eshop-optimizer' ); } $days = array_fill( 0, 7, array() ); foreach ( $week as $span ) { $day = intdiv( $span[0], 1440 ) % 7; $days[ $day ][] = self::time_words( $span[0] % 1440 ) . '–' . self::time_words( $span[1] % 1440 ); } $wpl = isset( $GLOBALS['wp_locale'] ) ? $GLOBALS['wp_locale'] : null; $abbr = static function ( $day ) use ( $wpl ) { return ( $wpl && method_exists( $wpl, 'get_weekday_abbrev' ) ) ? (string) $wpl->get_weekday_abbrev( $wpl->get_weekday( $day ) ) : (string) $day; }; $groups = array(); foreach ( array( 1, 2, 3, 4, 5, 6, 0 ) as $day ) { if ( ! $days[ $day ] ) { continue; } $words = implode( ', ', $days[ $day ] ); $last = count( $groups ) - 1; if ( $last >= 0 && $groups[ $last ]['words'] === $words && ( $groups[ $last ]['to'] + 1 ) % 7 === $day ) { $groups[ $last ]['to'] = $day; } else { $groups[] = array( 'from' => $day, 'to' => $day, 'words' => $words ); } } $parts = array(); foreach ( $groups as $g ) { $parts[] = ( $g['from'] === $g['to'] ? $abbr( $g['from'] ) : $abbr( $g['from'] ) . '–' . $abbr( $g['to'] ) ) . ' ' . $g['words']; } return implode( ', ', $parts ); } public static function call_hours_attrs( array $week, $slug = '', $display = 'inline' ) { $spans = array(); foreach ( $week as $span ) { $spans[] = array( (int) $span[0], (int) $span[1], self::time_words( $span[0] % 1440 ), self::time_words( $span[1] % 1440 ) ); } $tz = function_exists( 'wp_timezone_string' ) ? (string) wp_timezone_string() : 'UTC'; return ' data-aieo-hours="' . esc_attr( (string) wp_json_encode( $spans, JSON_UNESCAPED_UNICODE ) ) . '" data-aieo-tz="' . esc_attr( $tz ) . '"' . ( '' !== (string) $slug ? ' data-aieo-location="' . esc_attr( sanitize_key( (string) $slug ) ) . '"' : '' ) . ( 'dialog' === self::display( $display ) ? ' data-aieo-hours-display="dialog"' : '' ); } public static function open_at( array $week, $at = null ) { $at = $at instanceof DateTimeInterface ? $at : new DateTimeImmutable( 'now', wp_timezone() ); $here = ( new DateTimeImmutable( '@' . $at->getTimestamp() ) )->setTimezone( wp_timezone() ); $now = (int) $here->format( 'w' ) * 1440 + (int) $here->format( 'G' ) * 60 + (int) $here->format( 'i' ); foreach ( $week as $span ) { foreach ( array( $now, $now + self::WEEK ) as $t ) { if ( $t >= $span[0] && $t < $span[1] ) { return array( 'open' => true, 'next' => $span[1] % self::WEEK ); } } } $next = null; $gap = null; foreach ( $week as $span ) { $d = ( $span[0] - $now + self::WEEK ) % self::WEEK; if ( null === $gap || $d < $gap ) { $gap = $d; $next = $span[0]; } } return array( 'open' => false, 'next' => $next ); } public static function periods_from_days( array $days ) { $periods = array(); for ( $day = 0; $day < 7; $day++ ) { $line = trim( (string) ( $days[ $day ] ?? '' ) ); if ( '' === $line ) { continue; } foreach ( preg_split( '/\s*[,;]\s*/', $line ) as $range ) { if ( ! preg_match( '/^(\d{1,2})(?::(\d{2}))?\s*[-–—]\s*(\d{1,2})(?::(\d{2}))?$/u', $range, $m ) ) { return null; } $oh = (int) $m[1]; $om = (int) ( '' !== ( $m[2] ?? '' ) ? $m[2] : 0 ); $ch = (int) $m[3]; $cm = (int) ( '' !== ( $m[4] ?? '' ) ? $m[4] : 0 ); if ( $oh > 23 || $om > 59 || $ch > 24 || $cm > 59 || ( 24 === $ch && $cm > 0 ) ) { return null; } $open = $oh * 60 + $om; $close = $ch * 60 + $cm; $cday = $close <= $open || 1440 === $close ? ( $day + 1 ) % 7 : $day; $close = 1440 === $close ? 0 : $close; $periods[] = array( 'open' => array( 'day' => $day, 'time' => sprintf( '%02d%02d', $oh, $om ) ), 'close' => array( 'day' => $cday, 'time' => sprintf( '%02d%02d', intdiv( $close, 60 ), $close % 60 ) ), ); } } return $periods; } public static function day_lines( array $loc ) { $lines = array_fill( 0, 7, array() ); foreach ( self::week( $loc ) as $span ) { if ( 0 === $span[0] && self::WEEK <= $span[1] ) { return array_fill( 0, 7, '00:00–24:00' ); } $close = $span[1] - intdiv( $span[0], 1440 ) * 1440; $lines[ intdiv( $span[0], 1440 ) % 7 ][] = sprintf( '%02d:%02d', intdiv( $span[0] % 1440, 60 ), $span[0] % 60 ) . '–' . ( 1440 === $close ? '24:00' : sprintf( '%02d:%02d', intdiv( $close % 1440, 60 ), $close % 60 ) ); } return array_map( static function ( $l ) { return implode( ', ', $l ); }, $lines ); } public static function hours_words() { return array( 'open' => __( 'We\'re open until %s', 'ai-eshop-optimizer' ), 'today' => __( 'We open today at %s', 'ai-eshop-optimizer' ), 'tomorrow' => __( 'We open tomorrow at %s', 'ai-eshop-optimizer' ), 'on' => __( 'We open %1$s at %2$s', 'ai-eshop-optimizer' ), 'closed' => __( 'We\'re closed right now', 'ai-eshop-optimizer' ), 'days' => self::weekdays(), ); } public static function weekdays() { $wpl = isset( $GLOBALS['wp_locale'] ) ? $GLOBALS['wp_locale'] : null; $days = array(); for ( $d = 0; $d < 7; $d++ ) { $days[] = ( $wpl && method_exists( $wpl, 'get_weekday' ) ) ? (string) $wpl->get_weekday( $d ) : (string) $d; } return $days; } public static function hours_for( array $loc, $display = 'inline' ) { if ( ! self::$hours_logic ) { self::$hours_logic = true; add_action( did_action( 'wp_head' ) ? 'wp_footer' : 'wp_head', array( __CLASS__, 'print_hours_logic' ), 1 ); } $slug = sanitize_key( (string) ( $loc['slug'] ?? '' ) ); if ( 'dialog' === self::display( $display ) && '' !== $slug ) { if ( ! self::$dialogs ) { add_action( 'wp_footer', array( __CLASS__, 'print_dialogs' ), 20 ); } if ( ! isset( self::$dialogs[ $slug ] ) ) { self::$dialogs[ $slug ] = $loc; } self::dialog_style(); } return ''; } public static function print_hours_logic() { echo self::hours_logic_tag(); } public static function hours_logic_tag() { if ( null === self::$hours_js ) { $dir = defined( 'AIEO_DIR' ) ? AIEO_DIR : trailingslashit( dirname( __DIR__ ) ); $file = ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && is_readable( $dir . 'assets/js/aieo-hours.min.js' ) ? 'assets/js/aieo-hours.min.js' : 'assets/js/aieo-hours.js'; self::$hours_js = is_readable( $dir . $file ) ? trim( (string) file_get_contents( $dir . $file ) ) : ''; } $words = 'window.aieoHoursWords=' . wp_json_encode( self::hours_words(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ) . ';'; return ''; } public static function header_contact( $html, $args = array() ) { if ( '' !== (string) $html ) { return (string) $html; } $args = is_array( $args ) ? $args : array(); $loc = self::location( (string) ( $args['location'] ?? '' ) ); $call = is_array( $loc ) ? self::channels( $loc, array( 'call' ) ) : array(); if ( empty( $call['call'] ) ) { return ''; } $c = $call['call']; $label = trim( wp_strip_all_tags( (string) ( $args['label'] ?? '' ) ) ); $size = sanitize_html_class( (string) ( $args['size'] ?? '' ) ); $week = self::week( $loc ); $display = self::display( $args['hours'] ?? 'inline' ); $words = $week && 'inline' === $display && 'mobile' !== (string) ( $args['device'] ?? '' ); $name = '' !== $label ? $label . ', ' . $c['text'] : $c['name']; $out = '' . self::phone_icon() . ( '' !== $label ? '' . esc_html( $label ) . '' : '' ) . '' . esc_html( $c['text'] ) . '' . ( $words ? '' . esc_html( self::hours_summary( $week ) ) . '' : '' ) . '' . ( $week ? self::hours_for( $loc, $display ) : '' ); self::header_style(); return $out; } public static function week_lines( array $week ) { if ( ! $week ) { return array(); } if ( 1 === count( $week ) && 0 === $week[0][0] && self::WEEK <= $week[0][1] ) { return array( array( 'days' => '', 'hours' => __( 'Open 24 hours', 'ai-eshop-optimizer' ) ) ); } $days = array_fill( 0, 7, array() ); foreach ( $week as $span ) { $days[ intdiv( $span[0], 1440 ) % 7 ][] = self::time_words( $span[0] % 1440 ) . ' – ' . self::time_words( $span[1] % 1440 ); } $names = self::weekdays(); $groups = array(); foreach ( array( 1, 2, 3, 4, 5, 6, 0 ) as $day ) { $words = $days[ $day ] ? implode( ', ', $days[ $day ] ) : _x( 'Closed', 'a day without opening hours', 'ai-eshop-optimizer' ); $last = count( $groups ) - 1; if ( $last >= 0 && $groups[ $last ]['hours'] === $words ) { $groups[ $last ]['to'] = $day; } else { $groups[] = array( 'from' => $day, 'to' => $day, 'hours' => $words ); } } $out = array(); foreach ( $groups as $g ) { $out[] = array( 'days' => $g['from'] === $g['to'] ? $names[ $g['from'] ] : $names[ $g['from'] ] . ' – ' . $names[ $g['to'] ], 'hours' => $g['hours'], ); } return $out; } public static function dialog_template( array $loc ) { $week = self::week( $loc ); $slug = sanitize_key( (string) ( $loc['slug'] ?? '' ) ); if ( ! $week || '' === $slug ) { return ''; } $id = 'aieo-hours-dialog-' . $slug; $lines = ''; foreach ( self::week_lines( $week ) as $line ) { $lines .= '