settings = $settings; $this->services = $services; $this->domains = $domains; } /** * Values every email can use. * * @since 0.1.0 * * @param array|null $customer Customer row, or null. * @return array */ public function base( ?array $customer ): array { $name = ''; if ( $customer ) { $name = trim( (string) $customer['first_name'] ); $name = '' !== $name ? $name : trim( (string) $customer['company'] ); } $portal_id = (int) $this->settings->get( 'portal_page_id' ); $portal = $portal_id > 0 ? get_permalink( $portal_id ) : ''; return array( 'customer_name' => '' !== $name ? $name : __( 'there', 'hostkhata' ), 'business_name' => (string) $this->settings->get( 'business_name' ), 'site_name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), 'site_url' => home_url( '/' ), 'portal_url' => $portal ? $portal : home_url( '/' ), 'payment_instructions' => (string) $this->settings->get( 'payment_instructions' ), 'admin_url' => admin_url( 'admin.php?page=hostkhata' ), ); } /** * Example values for every merge tag, for previews and test messages. * * @since 0.1.0 * * @return array */ public function sample(): array { return array_merge( $this->base( null ), array( 'customer_name' => __( 'Asha', 'hostkhata' ), 'order_number' => 'ORD-00042', 'order_total' => $this->money( 450000 ), 'invoice_number' => 'INV-00123', 'invoice_total' => $this->money( 450000 ), 'invoice_balance' => $this->money( 250000 ), 'payment_amount' => $this->money( 200000 ), 'due_date' => $this->date( gmdate( 'Y-m-d', time() + 7 * DAY_IN_SECONDS ) ), 'expires_on' => $this->date( gmdate( 'Y-m-d', time() + 30 * DAY_IN_SECONDS ) ), 'invoice_url' => home_url( '/' ), 'domain' => 'example.com', 'days' => '7', 'amount' => $this->money( 300000 ), ) ); } /** * Values for an order. * * @since 0.1.0 * * @param array $order Order row. * @return array */ public function order( array $order ): array { return array( 'order_number' => (string) $order['order_number'], 'order_total' => $this->settings->format_money( (int) $order['total'] ), 'admin_url' => admin_url( 'admin.php?page=hostkhata#/orders' ), ); } /** * A readable list of the hosting and domain records created for an order. * * @since 0.1.0 * * @param int $order_id Order id. * @return array */ public function order_details( int $order_id ): array { $lines = array(); foreach ( $this->services->for_order( $order_id ) as $service ) { $parts = array_filter( array( $service['domain'], $service['server_label'], $service['username'] ? __( 'username', 'hostkhata' ) . ': ' . $service['username'] : '' ) ); /* translators: %s: hosting account details. */ $lines[] = sprintf( __( 'Hosting: %s', 'hostkhata' ), implode( ' ยท ', $parts ) ); } foreach ( $this->domains->for_order( $order_id ) as $domain ) { /* translators: 1: domain name, 2: expiry date. */ $lines[] = sprintf( __( 'Domain: %1$s (expires %2$s)', 'hostkhata' ), $domain['domain_name'], $this->date( (string) $domain['expires_on'] ) ); } return array( 'details' => implode( "\n", $lines ) ); } /** * Values for an invoice. * * @since 0.1.0 * * @param array $invoice Invoice row. * @return array */ public function invoice( array $invoice ): array { $url = '' !== $invoice['public_token'] ? add_query_arg( 'hostkhata_invoice', $invoice['public_token'], home_url( '/' ) ) : $this->base( null )['portal_url']; return array( 'invoice_number' => (string) $invoice['invoice_number'], 'invoice_total' => $this->settings->format_money( (int) $invoice['total'] ), 'invoice_balance' => $this->settings->format_money( (int) $invoice['balance'] ), 'due_date' => $this->date( (string) $invoice['due_date'] ), 'invoice_url' => $url, ); } /** * A date in the site's format. * * @since 0.1.0 * * @param string $date `YYYY-MM-DD`. * @return string */ public function date( string $date ): string { return Date::is_valid_date( $date ) ? mysql2date( get_option( 'date_format' ), $date ) : $date; } /** * Formats an amount. * * @since 0.1.0 * * @param int $minor Minor units. * @return string */ public function money( int $minor ): string { return $this->settings->format_money( $minor ); } }