*/ private array $rendered = array(); /** * Rows for a surface. * * @param \WC_Order $order Order. * @param string $surface `admin`, `email`, `email_plain`, `account` or `confirmation`. * @return array */ public function rows( \WC_Order $order, string $surface ): array { $snapshot = $order->get_meta( OrderWriter::META_SNAPSHOT, true ); $rows = array(); if ( is_array( $snapshot ) && isset( $snapshot['items'] ) && is_array( $snapshot['items'] ) ) { foreach ( $snapshot['items'] as $item ) { if ( ! is_array( $item ) ) { continue; } $value = (string) ( $item['display_value'] ?? '' ); if ( '' === $value ) { continue; } $rows[] = array( 'label' => (string) ( $item['label'] ?? '' ), 'value' => $value, ); } } /** * Filters the rows shown on an order surface. * * @since 1.0.0 * * @param array $rows Rows { label, value }. * @param \WC_Order $order Order. * @param string $surface `admin`, `email`, `email_plain`, `account` or `confirmation`. */ return (array) apply_filters( 'extrova_order_display_rows', $rows, $order, $surface ); } /** * Admin order screen (`woocommerce_admin_order_data_after_shipping_address`). * * @param mixed $order Order. */ public function admin( $order ): void { if ( $order instanceof \WC_Order ) { $this->template( 'admin/order-extras.php', $order, 'admin' ); } } /** * Emails (`woocommerce_email_order_meta`, priority 15). * * @param mixed $order Order. * @param mixed $sent_to_admin Sent to admin. * @param mixed $plain_text Plain text. */ public function email( $order, $sent_to_admin = false, $plain_text = false ): void { if ( ! $order instanceof \WC_Order ) { return; } if ( $plain_text ) { $this->template( 'emails/plain/order-extras.php', $order, 'email_plain' ); } else { $this->template( 'emails/order-extras.php', $order, 'email' ); } } /** * My account > view order and classic thank-you (`woocommerce_order_details_after_order_table`). * * @param mixed $order Order. */ public function account( $order ): void { if ( $order instanceof \WC_Order && ! isset( $this->rendered[ $order->get_id() ] ) ) { $this->rendered[ $order->get_id() ] = true; $this->template( 'order/order-extras.php', $order, 'account' ); } } /** * Markup for the order confirmation block. Empty when already rendered or not allowed. * * @param \WC_Order $order Order. */ public function confirmation_html( \WC_Order $order ): string { if ( isset( $this->rendered[ $order->get_id() ] ) ) { return ''; } $this->rendered[ $order->get_id() ] = true; ob_start(); $this->template( 'order/order-extras.php', $order, 'confirmation' ); return (string) ob_get_clean(); } /** * Same ownership rules as the WooCommerce order confirmation blocks: valid order key and * either the logged-in owner, or a guest order within the email verification grace period. * * @param \WC_Order $order Order. * @param string $key Order key from the URL. */ public function can_view_confirmation( \WC_Order $order, string $key ): bool { if ( '' === $key || ! $order->key_is_valid( $key ) ) { return false; } $customer_id = (int) $order->get_customer_id(); if ( $customer_id > 0 ) { return get_current_user_id() === $customer_id; } $created = $order->get_date_created(); $grace = (int) apply_filters( 'woocommerce_order_email_verification_grace_period', 10 * MINUTE_IN_SECONDS, $order ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WooCommerce filter. return $created && ( time() - $created->getTimestamp() ) <= $grace; } /** * Loads a (theme-overridable) template. * * @param string $template Template path. * @param \WC_Order $order Order. * @param string $surface Surface. */ private function template( string $template, \WC_Order $order, string $surface ): void { $rows = $this->rows( $order, $surface ); if ( array() === $rows ) { return; } wc_get_template( $template, array( 'rows' => $rows, 'order' => $order, ), 'extrova/', EXTROVA_PATH . 'templates/' ); } }