'hours', 'viber' => 'always', 'whatsapp' => 'always', 'messenger' => 'always', 'email' => 'always' ); public static function when( array $loc, $medium ) { $set = self::stored( $loc ); $v = (string) ( $set[ $medium ] ?? '' ); return in_array( $v, self::WHEN, true ) ? $v : ( self::DEFAULTS[ $medium ] ?? 'always' ); } public static function stored( array $loc ) { $raw = json_decode( (string) ( $loc['contact_when'] ?? '' ), true ); $out = array(); foreach ( is_array( $raw ) ? $raw : array() as $medium => $when ) { if ( in_array( $medium, self::MEDIA, true ) && in_array( $when, self::WHEN, true ) ) { $out[ $medium ] = $when; } } return $out; } public static function encode( $choices ) { $out = array(); foreach ( self::MEDIA as $medium ) { $v = is_array( $choices ) ? (string) ( $choices[ $medium ] ?? '' ) : ''; if ( in_array( $v, self::WHEN, true ) && self::DEFAULTS[ $medium ] !== $v ) { $out[ $medium ] = $v; } } return $out ? (string) wp_json_encode( $out ) : null; } public static function link( array $loc, $medium ) { if ( 'email' === $medium ) { $email = sanitize_email( (string) ( $loc['email'] ?? '' ) ); return is_email( $email ) ? array( 'href' => 'mailto:' . $email, 'text' => $email ) : null; } $value = trim( (string) ( 'phone' === $medium ? ( $loc['phone'] ?? '' ) : ( $loc[ $medium ] ?? '' ) ) ); if ( '' === $value ) { return null; } if ( 'phone' === $medium ) { $tel = (string) preg_replace( '/[^0-9+]/', '', $value ); return '' !== $tel ? array( 'href' => 'tel:' . $tel, 'text' => $value ) : null; } $href = class_exists( 'AIEO_Location_Contact' ) ? (string) AIEO_Location_Contact::link( $medium, $value, (string) ( $loc['country'] ?? '' ) ) : ''; if ( '' === $href ) { return null; } $text = preg_match( '#^[a-z][a-z0-9+.-]*:#i', $value ) ? (string) preg_replace( '#^(?:https?://)?(?:www\.)?(?:wa\.me/|m\.me/|viber://chat\?number=)#i', '', $value ) : $value; $text = rawurldecode( $text ); if ( 'messenger' !== $medium ) { $intl = preg_match( '/^\d{8,15}$/', $text ) && ( false !== strpos( $href, '%2B' . $text ) || false !== strpos( $href, 'wa.me/' . $text ) ); $text = self::number_text( $intl ? '+' . $text : $text, (string) ( $loc['country'] ?? '' ) ); } return array( 'href' => $href, 'text' => $text ); } public static function number_text( $value, $country = '' ) { $value = trim( (string) $value ); if ( ! preg_match( '/^(\+|00)?(\d{6,15})$/', $value, $m ) ) { return $value; } $digits = $m[2]; if ( '' === $m[1] ) { return self::groups( $digits ); } $cc = ''; foreach ( self::calling_codes( $country ) as $code ) { if ( '' !== $code && 0 === strpos( $digits, $code ) && strlen( $digits ) - strlen( $code ) >= 6 ) { $cc = $code; break; } } return '' !== $cc ? '+' . $cc . ' ' . self::groups( substr( $digits, strlen( $cc ) ) ) : '+' . $digits; } private static function groups( $d ) { $n = strlen( $d ); if ( $n <= 4 ) { return $d; } $tail = 0 === $n % 3 ? array() : ( 1 === $n % 3 ? array( substr( $d, -4 ) ) : array( substr( $d, -8, 4 ), substr( $d, -4 ) ) ); $head = substr( $d, 0, $n - 4 * count( $tail ) ); return implode( ' ', array_merge( '' === $head ? array() : str_split( $head, 3 ), $tail ) ); } private static function calling_codes( $country ) { $wc = function_exists( 'WC' ) && WC()->countries && method_exists( WC()->countries, 'get_country_calling_code' ) ? WC()->countries : null; if ( ! $wc ) { return array(); } $digits = static function ( $code ) { $code = is_array( $code ) ? (string) reset( $code ) : (string) $code; return (string) preg_replace( '/\D/', '', $code ); }; $own = '' !== (string) $country ? $digits( $wc->get_country_calling_code( strtoupper( (string) $country ) ) ) : ''; $rest = array(); foreach ( array_keys( (array) $wc->get_countries() ) as $c ) { $rest[] = $digits( $wc->get_country_calling_code( $c ) ); } $rest = array_values( array_unique( array_filter( $rest, 'strlen' ) ) ); usort( $rest, static function ( $a, $b ) { return strlen( $b ) <=> strlen( $a ); } ); return array_values( array_unique( array_merge( '' !== $own ? array( $own ) : array(), $rest ) ) ); } public static function postcode( $value ) { return str_replace( ' ', "\u{00A0}", trim( (string) $value ) ); } public static function label( $medium ) { $labels = array( 'phone' => __( 'Phone', 'ai-eshop-optimizer' ), 'viber' => 'Viber', 'whatsapp' => 'WhatsApp', 'messenger' => 'Messenger', 'email' => __( 'Email', 'ai-eshop-optimizer' ), ); return $labels[ $medium ] ?? $medium; } public static function value( array $loc, $medium, $state, $now, $tag = 'span', $class = '' ) { $link = self::link( $loc, $medium ); if ( null === $link ) { return ''; } $when = null === $state ? 'always' : self::when( $loc, $medium ); $open = null === $state || ! empty( $state['open'] ); $attrs = ( '' !== $class ? ' class="' . esc_attr( $class ) . '"' : '' ); if ( 'always' !== $when ) { $attrs .= ' data-aieo-shop-medium="' . esc_attr( $medium ) . '" data-when="' . esc_attr( $when ) . '" data-href="' . esc_attr( $link['href'] ) . '" data-text="' . esc_attr( $link['text'] ) . '"'; } if ( 'hidden' === $when && ! $open ) { return '<' . $tag . $attrs . ' hidden>' . $tag . '>'; } if ( 'hours' === $when && ! $open ) { $words = AIEO_Store_Hours::words(); return '<' . $tag . $attrs . '>' . esc_html( $link['text'] ) . ' · ' . esc_html( sprintf( $words['phoneOff'], AIEO_Store_Hours::when( $state, $now ) ) ) . '' . $tag . '>'; } return '<' . $tag . $attrs . '>' . self::anchor( $medium, $link ) . '' . $tag . '>'; } private static function anchor( $medium, array $link ) { $external = in_array( $medium, array( 'whatsapp', 'messenger' ), true ) ? ' target="_blank" rel="noopener"' : ''; return '' . esc_html( $link['text'] ) . ''; } public static function phone( array $loc, $class, $state, $now ) { $phone = trim( (string) ( $loc['phone'] ?? '' ) ); if ( '' === $phone ) { return ''; } $when = null === $state ? 'hours' : self::when( $loc, 'phone' ); if ( 'hours' !== $when ) { return self::value( $loc, 'phone', $state, $now, 'p', $class ); } $tel = (string) preg_replace( '/[^0-9+]/', '', $phone ); $out = '
'; if ( null === $state || $state['open'] ) { $out .= '' . esc_html( $phone ) . ''; } else { $words = AIEO_Store_Hours::words(); $out .= '' . esc_html( $phone ) . ' · ' . esc_html( sprintf( $words['phoneOff'], AIEO_Store_Hours::when( $state, $now ) ) ) . ''; } return $out . '
'; } } }