AccountClaimMiddleware.php
3 years ago
ArchiveModelMiddleware.php
3 years ago
BrandColorMiddleware.php
3 years ago
CheckoutRedirectMiddleware.php
3 years ago
ComponentAssetsMiddleware.php
3 years ago
CustomerDashboardRedirectMiddleware.php
3 years ago
EditModelMiddleware.php
3 years ago
LoginLinkMiddleware.php
3 years ago
LoginMiddleware.php
3 years ago
NonceMiddleware.php
3 years ago
OrderRedirectMiddleware.php
3 years ago
PathRedirectMiddleware.php
3 years ago
PaymentFailureRedirectMiddleware.php
3 years ago
PurchaseRedirectMiddleware.php
3 years ago
SubscriptionRedirectMiddleware.php
3 years ago
WebhooksMiddleware.php
2 years ago
PathRedirectMiddleware.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Middleware; |
| 4 | |
| 5 | use Closure; |
| 6 | use SureCartCore\Requests\RequestInterface; |
| 7 | use SureCartCore\Responses\RedirectResponse; |
| 8 | |
| 9 | /** |
| 10 | * Middleware for handling model archiving. |
| 11 | */ |
| 12 | class PathRedirectMiddleware { |
| 13 | /** |
| 14 | * Enqueue component assets. |
| 15 | * |
| 16 | * @param RequestInterface $request Request. |
| 17 | * @param Closure $next Next. |
| 18 | * @return function |
| 19 | */ |
| 20 | public function handle( RequestInterface $request, Closure $next ) { |
| 21 | $path = $request->query( 'path' ); |
| 22 | $customer_link_id = $request->query( 'customer_link_id' ); |
| 23 | |
| 24 | // need a path and a customer link id. |
| 25 | if ( empty( $path ) || empty( $customer_link_id ) ) { |
| 26 | return $next( $request ); |
| 27 | } |
| 28 | |
| 29 | $path = $request->query( 'path' ); |
| 30 | return ( new RedirectResponse( $request ) )->to( |
| 31 | esc_url_raw( $this->buildUrl( untrailingslashit( get_home_url() ) . $path, $request ) ) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Build the url. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function buildUrl( $url, RequestInterface $request ) { |
| 41 | if ( empty( $url ) ) { |
| 42 | return $url; |
| 43 | } |
| 44 | |
| 45 | // add checkout id. |
| 46 | $id = $request->query( 'checkout_id' ); |
| 47 | if ( $id ) { |
| 48 | $url = add_query_arg( |
| 49 | [ |
| 50 | 'checkout_id' => $id, |
| 51 | ], |
| 52 | $url |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | $promotion_code = $request->query( 'promotion_code' ); |
| 57 | if ( $promotion_code ) { |
| 58 | $url = add_query_arg( |
| 59 | [ |
| 60 | 'coupon' => $promotion_code, |
| 61 | ], |
| 62 | $url |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | return $url; |
| 67 | } |
| 68 | } |
| 69 |