$context Whatever that view needs.
*/
public static function render( string $view, array $context ): string {
$custom = apply_filters( 'rahkar_wp_track_view', '', $view, $context );
if ( is_string( $custom ) && '' !== trim( $custom ) ) {
// Theme-authored markup, same trust level as a template override.
return $custom;
}
switch ( $view ) {
case 'empty':
return self::empty();
case 'list':
return self::list( (array) ( $context['orders'] ?? [] ), (array) ( $context['terminal'] ?? [] ) );
case 'detail':
$order = $context['order'] ?? null;
return $order instanceof WC_Order ? self::detail( $order ) : '';
}
return '';
}
/**
* Nothing found for this number.
*/
private static function empty(): string {
$out = '
'
. esc_html__( 'سفارشی برای این شماره پیدا نشد.', 'rahkar-toolkit' )
. '
';
$contact = (string) Settings::get( 'order_tracking.support_contact', '' );
if ( '' !== $contact ) {
$out .= '' . esc_html(
sprintf(
/* translators: %s: support phone number */
__( 'در صورت نیاز به راهنمایی، با پشتیبانی تماس بگیرید: %s', 'rahkar-toolkit' ),
$contact
)
) . '
';
}
return $out;
}
/**
* The chooser, when a number has several orders.
*
* @param list $orders Orders to list.
* @param list $terminal Statuses that mean "finished".
*/
private static function list( array $orders, array $terminal ): string {
$out = '';
foreach ( $orders as $order ) {
if ( ! $order instanceof WC_Order ) {
continue;
}
$done = in_array( $order->get_status(), $terminal, true );
$out .= sprintf(
''
. '%2$s '
. '%4$s ',
(int) $order->get_id(),
esc_html( '#' . $order->get_order_number() ),
$done ? '.6' : '1',
esc_html( wc_get_order_status_name( $order->get_status() ) )
);
}
return $out . ' ';
}
/**
* One order.
*
* @param WC_Order $order Order to show.
*/
private static function detail( WC_Order $order ): string {
$rows = [
__( 'شماره سفارش', 'rahkar-toolkit' ) => '#' . $order->get_order_number(),
__( 'وضعیت', 'rahkar-toolkit' ) => wc_get_order_status_name( $order->get_status() ),
__( 'مبلغ', 'rahkar-toolkit' ) => wp_strip_all_tags( $order->get_formatted_order_total() ),
];
$tracking = (string) $order->get_meta( FulfilmentFields::TRACKING_META );
$trackingHtml = '';
if ( '' !== $tracking ) {
$urlPattern = (string) Settings::get( 'order_tracking.tracking_url', '' );
// A link only when a shop configured one. `{code}` is the customer-
// facing tracking code, url-encoded since it lands in a query string.
$trackingHtml = '' !== $urlPattern
? sprintf(
'%2$s ',
esc_url( str_replace( '{code}', rawurlencode( $tracking ), $urlPattern ) ),
esc_html( $tracking )
)
: esc_html( $tracking );
}
$out = '';
foreach ( $rows as $label => $value ) {
$out .= sprintf(
'%1$s %2$s ',
esc_html( (string) $label ),
esc_html( (string) $value )
);
}
if ( '' !== $trackingHtml ) {
$out .= sprintf(
'%1$s %2$s ',
esc_html__( 'کد رهگیری پستی', 'rahkar-toolkit' ),
wp_kses_post( $trackingHtml )
);
}
return $out . ' ';
}
}