orderRepository = $orderRepository; $this->request = $request; $this->messageManager = $messageManager; } /** * Process actions. * * @param string $action Action. * @param Entity\Order $order Order. * * @return void */ public function checkAction( string $action, Entity\Order $order ): void { $redirectTo = $this->request->getQuery( self::PARAM_REDIRECT_TO ); if ( 1 !== wp_verify_nonce( $this->request->getQuery( Plugin::PARAM_NONCE ), self::createNonceAction( $action, $order->getNumber() ) ) ) { $this->messageManager->flash_message( __( 'Link has expired. Please try again.', 'packeta' ), MessageManager::TYPE_ERROR ); $this->redirectTo( $redirectTo, $order ); } } /** * Creates nonce action name. * * @param string $action Action. * @param string $orderNumber Order number. * * @return string */ public static function createNonceAction( string $action, string $orderNumber ): string { return $action . '_' . $orderNumber; } /** * Redirects. * * @param string $redirectTo Redirect to. * @param Entity\Order|null $order Order. * * @return void */ public function redirectTo( string $redirectTo, ?Entity\Order $order = null ): void { if ( self::REDIRECT_TO_ORDER_GRID === $redirectTo ) { $orderGridParams = []; parse_str( $this->request->getQuery( self::PARAM_ORDER_GRID_PARAMS ) ?? '', $orderGridParams ); $redirectLink = add_query_arg( [ 'post_type' => 'shop_order', ] + $orderGridParams, admin_url( 'edit.php' ) ); if ( wp_safe_redirect( $redirectLink ) ) { exit; } } if ( self::REDIRECT_TO_ORDER_DETAIL === $redirectTo && null !== $order ) { $redirectLink = add_query_arg( [ 'post_type' => 'shop_order', 'post' => $order->getNumber(), 'action' => 'edit', ], admin_url( 'post.php' ) ); if ( wp_safe_redirect( $redirectLink ) ) { exit; } } } /** * Gets order ID. * * @return int|null */ private function getOrderId(): ?int { $orderId = $this->request->getQuery( self::PARAM_ORDER_ID ); if ( is_numeric( $orderId ) ) { return (int) $orderId; } return null; } /** * Gets order. * * @return Entity\Order|null */ public function getOrder(): ?Entity\Order { $orderId = $this->getOrderId(); if ( null !== $orderId ) { return $this->orderRepository->getById( $orderId ); } return null; } }