0 && function_exists( 'is_page' ) && is_page( $id ); } /** * Statuses after which nothing more will happen to an order. * * @return list */ public static function terminalStatuses(): array { return apply_filters( 'rahkar_track_terminal_statuses', array( 'completed', 'cancelled', 'refunded', 'failed' ) ); } /** * Has this order left the building? * * @param WC_Order $order Order to check. */ public static function isShipped( WC_Order $order ): bool { if ( '1' === (string) $order->get_meta( '_rahkar_shipped', true ) ) { return true; } return function_exists( 'rahkar_order_tracking_number' ) && '' !== (string) rahkar_order_tracking_number( $order ); } /** * The phone number recorded on an order. * * @param WC_Order $order Order to read. */ public static function orderPhone( WC_Order $order ): string { $p = (string) $order->get_meta( '_rahkar_otp_verified_phone', true ); if ( '' === $p ) { $p = (string) $order->get_billing_phone(); } return $p; } /** * Mask a phone number for display. * * Shown back to whoever asked so they know WHICH number the code went to, * without the page itself disclosing a customer's number to someone who only * guessed an order code. * * @param string $phone Canonical number. */ public static function maskPhone( $phone ): string { $national = '0' . $phone; // 11-digit national form. $len = strlen( $national ); if ( $len < 4 ) { return ''; } $out = substr( $national, 0, 2 ) . str_repeat( '•', $len - 4 ) . substr( $national, -2 ); return function_exists( 'rahkar_fa_digits' ) ? rahkar_fa_digits( $out ) : $out; } /** * Resolve an order from whatever the customer typed. * * @param string $code Order number, or an order key. * * @return WC_Order|null */ public static function resolveOrder( $code ) { $code = trim( (string) $code ); if ( '' === $code ) { return null; } if ( ctype_digit( $code ) ) { $order = wc_get_order( (int) $code ); if ( $order instanceof WC_Order ) { // Default WC: order number == id → done, no extra query. Custom // numbering: prefer a real number match if one exists. if ( (string) $order->get_order_number() === $code ) { return $order; } $by_number = self::findByNumber( $code ); return $by_number ? $by_number : $order; } } return self::findByNumber( $code ); } /** * Find an order by its displayed number. * * @param string $number Order number as the customer sees it. * * @return WC_Order|null */ private static function findByNumber( $number ) { $pre = apply_filters( 'rahkar_track_find_order_by_number', null, $number ); if ( $pre instanceof WC_Order ) { return $pre; } $orders = wc_get_orders( array( 'limit' => 1, 'return' => 'objects', 'meta_query' => array( array( 'key' => '_order_number', 'value' => (string) $number ) ), // phpcs:ignore WordPress.DB.SlowDBQuery ) ); return ( is_array( $orders ) && ! empty( $orders ) && $orders[0] instanceof WC_Order ) ? $orders[0] : null; } /** * Every order placed with this phone number. * * @param string $phone Canonical number. * * @return list */ public static function queryOrders( $phone ): array { $base = array( 'limit' => 10, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects' ); $queries = apply_filters( 'rahkar_track_order_queries', array( array_merge( $base, array( 'billing_phone' => '0' . $phone ) ), array_merge( $base, array( 'meta_query' => array( array( 'key' => '_rahkar_otp_verified_phone', 'value' => $phone ) ), // phpcs:ignore WordPress.DB.SlowDBQuery ) ), ), $phone ); $map = array(); foreach ( $queries as $args ) { $res = wc_get_orders( $args ); if ( ! is_array( $res ) ) { continue; } foreach ( $res as $o ) { if ( $o instanceof WC_Order ) { $map[ $o->get_id() ] = $o; } } } $orders = array_values( $map ); usort( $orders, function ( $a, $b ) { $ta = $a->get_date_created() ? $a->get_date_created()->getTimestamp() : 0; $tb = $b->get_date_created() ? $b->get_date_created()->getTimestamp() : 0; return $tb <=> $ta; } ); return array_slice( $orders, 0, 10 ); } }