settings = $settings ?? new Settings(); $this->gateway = $gateway ?? new Timetics_Public_Gateway(); parent::__construct( $name, $description ); } /** * Status line while the tool runs; each tool names its own. * * @return string */ public function status_label(): string { return ''; } /** * Take the card left by the last run. * * @return array|null */ public function take_card(): ?array { $card = $this->card; $this->card = null; return $card; } /** * Leave a card for the chat screen. * * @param string $card Card name. * @param array $data Card data. * @return void */ protected function set_card( string $card, array $data ): void { $this->card = [ 'card' => $card, 'data' => $data, ]; } /** * Error result when Timetics is not loaded, else null. * * @return array|null */ protected function inactive(): ?array { if ( $this->gateway->is_active() ) { return null; } return [ 'ok' => false, 'error' => 'Timetics is not active on this website.', ]; } /** * "13:30" in the site's time format, e.g. "1:30 PM". * * @param string $hm 24h time. * @return string */ protected function time_label( string $hm ): string { return ( new \DateTimeImmutable( '2000-01-01 ' . $hm ) )->format( (string) get_option( 'time_format', 'g:i a' ) ); } /** * "2030-01-15" as weekday plus the site's date format, e.g. "Tue, 15 Jan 2030". * * @param string $ymd Date. * @return string */ protected function date_label( string $ymd ): string { return ( new \DateTimeImmutable( $ymd ) )->format( 'D, ' . (string) get_option( 'date_format', 'j M Y' ) ); } /** * A timezone a visitor understands: "Europe/London" as "London time", * offsets and UTC as Booktics shows them ("UTC+6"). * * @param string $timezone Timezone name. * @return string */ public static function timezone_label( string $timezone ): string { if ( false === strpos( $timezone, '/' ) ) { return Visitor_Timezone::format( $timezone ); } $city = str_replace( '_', ' ', substr( (string) strrchr( $timezone, '/' ), 1 ) ); /* translators: %s: city of a timezone, e.g. London */ return sprintf( __( '%s time', 'aisentic' ), $city ); } /** * A Timetics panel setting (falls back to the panel default). * * @param string $key Field key. * @param mixed $fallback Fallback. * @return mixed */ protected function setting( string $key, $fallback = null ) { return ( new Public_Agent_Settings( $this->settings ) )->integration( 'timetics', $key, $fallback ); } /** * Today in the visitor's timezone, "Y-m-d". * * @return string */ protected function visitor_today(): string { return $this->visitor_now()->format( 'Y-m-d' ); } /** * Now in the visitor's timezone. * * @return \DateTimeImmutable */ protected function visitor_now(): \DateTimeImmutable { return $this->gateway->now()->setTimezone( new \DateTimeZone( $this->gateway->visitor_timezone() ) ); } }