FormHandlerService.php
10 months ago
NotificationManagementService.php
1 month ago
ProductPageIntegration.php
10 months ago
SignupResult.php
10 months ago
SignupService.php
1 month ago
FormHandlerService.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Frontend; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Config; |
| 8 | |
| 9 | /** |
| 10 | * Class for handling the form submission. |
| 11 | */ |
| 12 | class FormHandlerService { |
| 13 | |
| 14 | /** |
| 15 | * The signup service. |
| 16 | * |
| 17 | * @var SignupService |
| 18 | */ |
| 19 | private SignupService $signup_service; |
| 20 | |
| 21 | /** |
| 22 | * The logger. |
| 23 | * |
| 24 | * @var LoggerInterface |
| 25 | */ |
| 26 | private $logger; |
| 27 | |
| 28 | /** |
| 29 | * Initialize the service. |
| 30 | * |
| 31 | * @internal |
| 32 | * |
| 33 | * @param SignupService $signup_service The signup service. |
| 34 | */ |
| 35 | final public function init( SignupService $signup_service ) { |
| 36 | $this->signup_service = $signup_service; |
| 37 | $this->logger = \wc_get_logger(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructor. |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | add_action( 'template_redirect', array( $this, 'handle_signup' ) ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Handle the form submit event. |
| 49 | */ |
| 50 | public function handle_signup() { |
| 51 | |
| 52 | // Sanity checks. |
| 53 | if ( ! Config::allows_signups() ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if ( ! isset( $_POST['wc_bis_register'] ) ) { // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | try { |
| 62 | |
| 63 | if ( self::requires_nonce_check() ) { |
| 64 | if ( ! isset( $_POST['wc_bis_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['wc_bis_nonce'] ), 'wc_bis_signup' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 65 | wc_add_notice( $this->signup_service->get_error_message( SignupService::ERROR_INVALID_REQUEST ), 'error' ); |
| 66 | return; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | $data = $this->signup_service->parse( $_POST ); |
| 71 | if ( \is_wp_error( $data ) ) { |
| 72 | wc_add_notice( $this->signup_service->get_error_message( $data->get_error_code() ), 'error' ); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $result = $this->signup_service->signup( |
| 77 | $data['product_id'], |
| 78 | $data['user_id'], |
| 79 | $data['user_email'], |
| 80 | $data['posted_attributes'] ?? array() |
| 81 | ); |
| 82 | |
| 83 | if ( \is_wp_error( $result ) ) { |
| 84 | wc_add_notice( $this->signup_service->get_error_message( $result->get_error_code() ), 'error' ); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | wc_add_notice( $this->signup_service->get_signup_user_message( $result->get_code(), $result->get_notification() ), 'success' ); |
| 89 | } catch ( \Throwable $e ) { |
| 90 | wc_add_notice( $this->signup_service->get_error_message( SignupService::ERROR_FAILED ), 'error' ); |
| 91 | $this->logger->error( $e->getMessage(), array( 'source' => 'stock-notifications-signup-errors' ) ); |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Whether the form requires a nonce check. |
| 98 | * |
| 99 | * Note: Nonce checks may be disabled for guest signups to support HTML caching. |
| 100 | * |
| 101 | * @return bool True if the form requires a nonce check, false otherwise. |
| 102 | */ |
| 103 | public static function requires_nonce_check(): bool { |
| 104 | |
| 105 | $requires_account = ProductPageIntegration::is_personalization_enabled() && ( Config::requires_account() || \is_user_logged_in() ); |
| 106 | |
| 107 | /** |
| 108 | * Filter to require nonce check. |
| 109 | * |
| 110 | * @since 10.2.0 |
| 111 | * |
| 112 | * @param bool $requires_nonce_check Whether to require nonce check. |
| 113 | * @return bool |
| 114 | */ |
| 115 | return (bool) apply_filters( 'woocommerce_customer_stock_notifications_requires_nonce_check', $requires_account ); |
| 116 | } |
| 117 | } |
| 118 |