1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Reject a B2BKing vendor application while keeping the account as a customer.
*/
add_action( 'b2bking_registration_approval_after_group', 'b2bking_custom_rejection_reason_field' );
function b2bking_custom_rejection_reason_field( $user_id ) {
if ( 'no' !== get_user_meta( $user_id, 'b2bking_account_approved', true ) ) {
return;
}
?>
<div id="b2bking-custom-rejection-reason-wrap" style="display:none; margin:15px 0; width:100%;">
<label for="b2bking-custom-rejection-reason" style="display:block; font-weight:600; margin-bottom:6px;">
<?php esc_html_e( 'Vendor application rejection reason', 'b2bking' ); ?>
</label>
<textarea id="b2bking-custom-rejection-reason" rows="5" style="width:100%;" placeholder="<?php esc_attr_e( 'Enter the reason that will be emailed to the applicant.', 'b2bking' ); ?>"></textarea>
<p class="description">
<?php esc_html_e( 'The account will remain active as a regular customer without B2B or vendor access.', 'b2bking' ); ?>
</p>
</div>
<?php
}
add_action( 'admin_footer-user-edit.php', 'b2bking_custom_rejection_reason_script' );
function b2bking_custom_rejection_reason_script() {
?>
<script>
(function () {
function initializeB2BKingRejection() {
const groupSelect = document.querySelector('.b2bking_user_registration_user_data_container_element_select_group');
const reasonWrap = document.getElementById('b2bking-custom-rejection-reason-wrap');
const reasonField = document.getElementById('b2bking-custom-rejection-reason');
const approveButton = document.querySelector('.b2bking_user_registration_user_data_container_element_approval_button_approve');
if (!groupSelect || !reasonWrap || !reasonField || !approveButton || !window.jQuery || !window.b2bking) {
return;
}
const customerOption = groupSelect.querySelector('option[value="b2c"]');
if (!customerOption) {
return;
}
customerOption.textContent = 'Reject vendor application (keep as customer)';
const buttonTextNode = Array.from(approveButton.childNodes).find(function (node) {
return node.nodeType === window.Node.TEXT_NODE && node.textContent.trim();
});
const originalButtonLabel = buttonTextNode ? buttonTextNode.textContent.trim() : 'Approve user';
function updateRejectionInterface() {
const rejectingApplication = groupSelect.value === 'b2c';
reasonWrap.style.display = rejectingApplication ? 'block' : 'none';
if (buttonTextNode) {
buttonTextNode.textContent = rejectingApplication ? ' Reject vendor application' : ' ' + originalButtonLabel;
}
}
groupSelect.addEventListener('change', updateRejectionInterface);
updateRejectionInterface();
approveButton.addEventListener('click', function (event) {
if (groupSelect.value !== 'b2c') {
return;
}
event.preventDefault();
event.stopImmediatePropagation();
const reason = reasonField.value.trim();
if (!reason) {
window.alert('Please enter a rejection reason first.');
reasonField.focus();
return;
}
if (!window.confirm('Reject this vendor application and keep the account as a regular customer?')) {
return;
}
window.jQuery.post(window.ajaxurl, {
action: 'b2bkingapproveuser',
security: window.b2bking.security,
chosen_group: 'b2c',
user: document.getElementById('b2bking_user_registration_data_id').value,
b2bking_rejection_reason: reason
}, function (response) {
if (String(response).trim() === 'success') {
window.location.reload();
return;
}
window.alert('The application could not be updated. Please reload the page and try again.');
}).fail(function () {
window.alert('The application could not be updated. Please reload the page and try again.');
});
}, true);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeB2BKingRejection);
} else {
initializeB2BKingRejection();
}
}());
</script>
<?php
}
add_action( 'b2bking_approved_user_as_b2c', 'b2bking_custom_send_rejection_reason', 10, 2 );
function b2bking_custom_send_rejection_reason( $user_id, $email_address ) {
if ( empty( $_POST['b2bking_rejection_reason'] ) ) {
return;
}
$reason = sanitize_textarea_field( wp_unslash( $_POST['b2bking_rejection_reason'] ) );
$user = get_userdata( $user_id );
if ( ! $user || ! is_email( $email_address ) || '' === $reason ) {
return;
}
update_user_meta( $user_id, 'b2bking_vendor_rejection_reason', $reason );
$name = $user->first_name ? $user->first_name : $user->display_name;
$site_name = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
$subject = sprintf( '[%s] Vendor application update', $site_name );
$message = '<p>Hi ' . esc_html( $name ) . ',</p>';
$message .= '<p>Thank you for your vendor application. We are unable to approve it at this time.</p>';
$message .= '<p><strong>Reason:</strong><br>' . nl2br( esc_html( $reason ) ) . '</p>';
$message .= '<p>Your account remains active as a regular customer, but B2B and vendor access have not been enabled.</p>';
if ( function_exists( 'WC' ) && WC()->mailer() ) {
$message = WC()->mailer()->wrap_message( 'Vendor application update', $message );
}
wp_mail(
$email_address,
$subject,
$message,
array( 'Content-Type: text/html; charset=UTF-8' )
);
}