$b[0]; } ); return $out; } private static function minutes( $hhmm ) { $d = preg_replace( '/\D/', '', (string) $hhmm ); if ( strlen( (string) $d ) < 4 ) { return null; } return (int) substr( $d, 0, 2 ) * 60 + (int) substr( $d, 2, 2 ); } public static function now( $now = null, $tz = null ) { $tz = $tz instanceof DateTimeZone ? $tz : wp_timezone(); $n = $now instanceof DateTimeImmutable ? $now->setTimezone( $tz ) : new DateTimeImmutable( 'now', $tz ); return (int) $n->format( 'w' ) * 1440 + (int) $n->format( 'G' ) * 60 + (int) $n->format( 'i' ); } public static function state( array $iv, $now ) { if ( array() === $iv ) { return null; } if ( 1 === count( $iv ) && 0 === $iv[0][0] && self::WEEK === $iv[0][1] ) { return array( 'open' => true, 'always' => true, 'minute' => 0, 'days' => 0 ); } $now = ( (int) $now % self::WEEK + self::WEEK ) % self::WEEK; foreach ( $iv as $x ) { foreach ( array( 0, self::WEEK ) as $w ) { if ( $now + $w >= $x[0] && $now + $w < $x[1] ) { return self::change( true, $now, $x[1] - ( $now + $w ) ); } } } $next = null; foreach ( $iv as $x ) { $delta = ( ( $x[0] - $now ) % self::WEEK + self::WEEK ) % self::WEEK; if ( null === $next || $delta < $next ) { $next = $delta; } } return self::change( false, $now, (int) $next ); } private static function change( $open, $now, $delta ) { $at = $now % 1440 + $delta; return array( 'open' => $open, 'always' => false, 'minute' => $at % 1440, 'days' => intdiv( $at, 1440 ), ); } public static function state_words( $state, $now ) { if ( null === $state ) { return ''; } $w = self::words(); if ( $state['always'] ) { return $w['always']; } return sprintf( $state['open'] ? $w['open'] : $w['closed'], self::when( $state, $now ) ); } public static function when( array $state, $now ) { $w = self::words(); $time = self::hm( $state['minute'] ); if ( 0 === $state['days'] ) { return sprintf( $w['atToday'], $time ); } if ( 1 === $state['days'] ) { return sprintf( $w['atTomorrow'], $time ); } $day = ( intdiv( ( (int) $now % self::WEEK + self::WEEK ) % self::WEEK, 1440 ) + $state['days'] ) % 7; return sprintf( $w['atDay'], $w['days'][ $day ], $time ); } public static function hm( $minute ) { $minute = ( (int) $minute % 1440 + 1440 ) % 1440; return sprintf( '%02d:%02d', intdiv( $minute, 60 ), $minute % 60 ); } public static function rows( array $iv ) { if ( array() === $iv ) { return array(); } $order = self::week_order(); $by_day = self::day_hours( $iv, $order ); $groups = array(); foreach ( $order as $d ) { $groups[ $by_day[ $d ] ][] = $d; } $rows = array(); foreach ( $groups as $hours => $days ) { $rows[] = array( 'days' => $days, 'label' => self::days_label( $days, $order ), 'hours' => (string) $hours ); } return $rows; } public static function runs( array $iv ) { if ( array() === $iv ) { return array(); } $order = self::week_order(); $by_day = self::day_hours( $iv, $order ); $runs = array(); foreach ( $order as $d ) { $last = count( $runs ) - 1; if ( $last >= 0 && $runs[ $last ]['hours'] === $by_day[ $d ] ) { $runs[ $last ]['days'][] = $d; } else { $runs[] = array( 'days' => array( $d ), 'hours' => $by_day[ $d ] ); } } foreach ( $runs as $i => $run ) { $runs[ $i ] = array( 'days' => $run['days'], 'label' => self::days_label( $run['days'], $order ), 'hours' => (string) $run['hours'] ); } return $runs; } private static function week_order() { $first = (int) get_option( 'start_of_week', 1 ); $order = array(); for ( $i = 0; $i < 7; $i++ ) { $order[] = ( $first + $i ) % 7; } return $order; } private static function day_hours( array $iv, array $order ) { $w = self::words(); $always = 1 === count( $iv ) && 0 === $iv[0][0] && self::WEEK === $iv[0][1]; $by_day = array(); foreach ( $order as $d ) { $spans = array(); foreach ( $iv as $x ) { if ( intdiv( $x[0], 1440 ) === $d ) { $spans[] = sprintf( $w['span'], self::hm( $x[0] ), self::hm( $x[1] ) ); } } $by_day[ $d ] = $always ? $w['allDay'] : ( array() === $spans ? $w['closedDay'] : implode( ', ', $spans ) ); } return $by_day; } private static function days_label( array $days, array $order ) { $w = self::words(); $pos = array_flip( $order ); $idx = array_map( static function ( $d ) use ( $pos ) { return $pos[ $d ]; }, $days ); sort( $idx ); $runs = array(); foreach ( $idx as $i ) { $last = count( $runs ) - 1; if ( $last >= 0 && $runs[ $last ][1] === $i - 1 ) { $runs[ $last ][1] = $i; } else { $runs[] = array( $i, $i ); } } $parts = array(); foreach ( $runs as $r ) { $a = $w['days'][ $order[ $r[0] ] ]; $b = $w['days'][ $order[ $r[1] ] ]; if ( $r[0] === $r[1] ) { $parts[] = $a; } elseif ( $r[1] === $r[0] + 1 ) { $parts[] = $a; $parts[] = $b; } else { $parts[] = sprintf( $w['range'], $a, $b ); } } return implode( ', ', $parts ); } public static function words() { static $w = null; if ( null !== $w ) { return $w; } $days = array(); $wpl = isset( $GLOBALS['wp_locale'] ) ? $GLOBALS['wp_locale'] : null; for ( $d = 0; $d < 7; $d++ ) { $days[] = ( $wpl && method_exists( $wpl, 'get_weekday' ) ) ? (string) $wpl->get_weekday( $d ) : gmdate( 'l', strtotime( 'Sunday +' . $d . ' days' ) ); } $w = array( 'open' => __( 'Open, closes %s', 'ai-eshop-optimizer' ), 'closed' => __( 'Closed, opens %s', 'ai-eshop-optimizer' ), 'always' => __( 'Open 24 hours', 'ai-eshop-optimizer' ), 'atToday' => __( 'at %s', 'ai-eshop-optimizer' ), 'atTomorrow' => __( 'tomorrow at %s', 'ai-eshop-optimizer' ), 'atDay' => __( 'on %1$s at %2$s', 'ai-eshop-optimizer' ), 'phoneOff' => __( 'opens %s', 'ai-eshop-optimizer' ), 'span' => __( '%1$s to %2$s', 'ai-eshop-optimizer' ), 'range' => __( '%1$s to %2$s', 'ai-eshop-optimizer' ), 'closedDay' => __( 'Closed', 'ai-eshop-optimizer' ), 'allDay' => __( 'Open 24 hours', 'ai-eshop-optimizer' ), 'today' => __( 'today', 'ai-eshop-optimizer' ), 'days' => $days, ); return $w; } public static function timezone( array $loc, $pid = 0 ) { $name = $pid && class_exists( 'AIEO_Store_CPT' ) ? (string) get_post_meta( (int) $pid, AIEO_Store_CPT::META_TIMEZONE, true ) : ''; if ( '' === $name ) { $country = strtoupper( trim( (string) ( $loc['country'] ?? '' ) ) ); $zones = 2 === strlen( $country ) ? (array) @DateTimeZone::listIdentifiers( DateTimeZone::PER_COUNTRY, $country ) : array(); $name = 1 === count( $zones ) ? (string) $zones[0] : ''; } if ( '' !== $name && in_array( $name, timezone_identifiers_list(), true ) ) { return new DateTimeZone( $name ); } return wp_timezone(); } public static function zone_attrs( DateTimeZone $tz ) { return array( 'tz' => $tz->getName(), 'offset' => (int) round( $tz->getOffset( new DateTimeImmutable( 'now', $tz ) ) / 60 ) ); } public static function config() { $tz = wp_timezone(); return array( 'tz' => wp_timezone_string(), 'offset' => (int) round( $tz->getOffset( new DateTimeImmutable( 'now', $tz ) ) / 60 ), 'words' => self::words(), ); } public static function script_tag() { if ( self::$printed || ! defined( 'AIEO_DIR' ) ) { return ''; } $file = AIEO_DIR . 'includes/dmm/google-reviews/store-hours.js'; $min = AIEO_DIR . 'includes/dmm/google-reviews/store-hours.min.js'; if ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) && is_readable( $min ) && ( ! is_readable( $file ) || filemtime( $min ) >= filemtime( $file ) ) ) { $file = $min; } $js = is_readable( $file ) ? trim( (string) file_get_contents( $file ) ) : ''; if ( '' === $js ) { return ''; } self::$printed = true; return wp_get_inline_script_tag( str_ireplace( ' self::SCRIPT_ID ) ); } public static function rocket_keep( $list ) { if ( is_array( $list ) ) { $list[] = self::SCRIPT_ID; } return $list; } public static function reset() { self::$printed = false; self::$need = false; } } }