$context Context. * @return mixed */ public static function render_step_metadata( $value, array $context ) { if ( is_string( $value ) ) { return self::render( $value, $context ); } if ( is_array( $value ) ) { $out = array(); foreach ( $value as $key => $item ) { $out[ $key ] = self::render_step_metadata( $item, $context ); } return $out; } return $value; } /** * Replace {{variable}} placeholders. * * @param string $string Template. * @param array $context Context. * @return string */ public static function render( string $string, array $context ): string { if ( '' === $string || false === strpos( $string, '{{' ) ) { return $string; } if ( ! preg_match_all( '/\{\{([a-zA-Z0-9_]+)\}\}/', $string, $matches ) ) { return $string; } $order = self::order_from_context( $context ); $variables = null !== $order ? Variables::fromOrderInstance( $order ) : null; foreach ( $matches[0] as $i => $placeholder ) { $key = $matches[1][ $i ]; $value = $placeholder; if ( null !== $variables ) { $resolved = $variables->get( $placeholder ); if ( is_scalar( $resolved ) && (string) $resolved !== $placeholder ) { $value = (string) $resolved; } } if ( $value === $placeholder ) { $order_snap = isset( $context['order'] ) && is_array( $context['order'] ) ? $context['order'] : array(); if ( isset( $order_snap[ $key ] ) && is_scalar( $order_snap[ $key ] ) ) { $value = (string) $order_snap[ $key ]; } elseif ( isset( $context[ $key ] ) && is_scalar( $context[ $key ] ) ) { $value = (string) $context[ $key ]; } } $string = str_replace( $placeholder, $value, $string ); } return $string; } /** * @param array $context Context. * @return \WC_Order|AbandonedOrder|null */ private static function order_from_context( array $context ) { $order_id = isset( $context['order_id'] ) ? absint( $context['order_id'] ) : 0; if ( $order_id > 0 && function_exists( 'wc_get_order' ) ) { $order = wc_get_order( $order_id ); if ( $order instanceof \WC_Order ) { return $order; } } $abandoned_cart_id = isset( $context['abandoned_cart_id'] ) ? absint( $context['abandoned_cart_id'] ) : 0; if ( $abandoned_cart_id > 0 ) { $order = new AbandonedOrder( $abandoned_cart_id ); if ( ! empty( $order->get_cart_data() ) ) { return $order; } } return null; } }