__( 'Security check failed.', 'easy-invoice' ) ], 403 ); } // The check reveals whether a given business is VAT-registered, and it // consumes a shared public service on the site's behalf. Both are // reasons not to leave it open to any logged-in user. if ( ! easy_invoice_user_can( 'ei_view_invoices' ) ) { wp_send_json_error( [ 'message' => __( 'You do not have permission to do that.', 'easy-invoice' ) ], 403 ); } $vat = isset( $_POST['vat'] ) ? sanitize_text_field( wp_unslash( $_POST['vat'] ) ) : ''; $country = isset( $_POST['country'] ) ? sanitize_text_field( wp_unslash( $_POST['country'] ) ) : ''; $result = ViesValidator::check( $vat, $country ); if ( is_wp_error( $result ) ) { // Deliberately a success response carrying an "unknown" state. This // is not an error in the request — it is the register declining to // answer, and the difference matters at the other end. wp_send_json_success( [ 'state' => 'unknown', 'message' => $result->get_error_message(), ] ); } if ( empty( $result['valid'] ) ) { wp_send_json_success( [ 'state' => 'invalid', 'message' => __( 'The EU VAT register does not recognise this number. Check it with your customer before treating this as a reverse-charge supply.', 'easy-invoice' ), ] ); } $message = __( 'Registered in the EU VAT register.', 'easy-invoice' ); if ( '' !== $result['name'] ) { $message = sprintf( /* translators: %s: registered trader name. */ __( 'Registered: %s', 'easy-invoice' ), $result['name'] ); } wp_send_json_success( [ 'state' => 'valid', 'message' => $message, ] ); } /** * Add the control beside the VAT field. * * Injected from the footer rather than added to the field registration * because that layer describes data, not behaviour, and every field type it * knows about renders the same way. This attaches to whatever the form * produced. * * @return void */ public static function printScript(): void { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || false === strpos( (string) $screen->id, 'easy-invoice' ) ) { return; } if ( ! easy_invoice_user_can( 'ei_view_invoices' ) ) { return; } $nonce = wp_create_nonce( self::ACTION ); ?>