-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact-form.php
More file actions
365 lines (308 loc) · 10.6 KB
/
contact-form.php
File metadata and controls
365 lines (308 loc) · 10.6 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/*
Plugin Name: Contact Form
Plugin URI: https://www.littlebizzy.com/plugins/contact-form
Description: Intuitive WordPress contact form
Version: 1.2.4
Requires PHP: 7.0
Tested up to: 6.9
Author: LittleBizzy
Author URI: https://www.littlebizzy.com
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Update URI: false
GitHub Plugin URI: littlebizzy/contact-form
Primary Branch: master
Text Domain: contact-form
*/
// prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// override wordpress.org with git updater
add_filter( 'gu_override_dot_org', function( $overrides ) {
$overrides[] = 'contact-form/contact-form.php';
return $overrides;
}, 999 );
// display contact form shortcode
add_shortcode( 'contact_form', 'contact_form_display' );
function contact_form_display( $atts = array() ) {
// parse shortcode attributes
$args = shortcode_atts(
array(
'show_url' => 'true',
),
$atts,
'contact_form'
);
// determine whether to show optional url field
$show_url = ( 'true' === $args['show_url'] );
// require user to be logged in before rendering form
if ( ! is_user_logged_in() ) {
return '<p>' . esc_html__( 'You must be logged in to contact us.', 'contact-form' ) . '</p>';
}
// enqueue js when shortcode is used
wp_enqueue_script(
'contact-form',
plugin_dir_url( __FILE__ ) . 'contact-form.js',
array( 'jquery' ),
filemtime( plugin_dir_path( __FILE__ ) . 'contact-form.js' ),
true
);
// pass ajax url and nonce to js
wp_localize_script(
'contact-form',
'contactForm',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'contact_form_nonce' ),
)
);
// retrieve current user
$user = wp_get_current_user();
// prepare user display name and email
$user_id = (int) $user->ID;
$first_name = get_user_meta( $user_id, 'first_name', true );
$last_name = get_user_meta( $user_id, 'last_name', true );
$full_name = trim( $first_name . ' ' . $last_name );
$name_value = $user->display_name;
if ( ! empty( $full_name ) ) {
$name_value = $full_name;
}
$email = $user->user_email;
// get billing phone from woocommerce profile
$billing_phone = '';
if ( class_exists( 'WooCommerce' ) ) {
$billing_phone = get_user_meta( $user_id, 'billing_phone', true );
}
$phone_value = __( 'Not Available', 'contact-form' );
if ( ! empty( $billing_phone ) ) {
$phone_value = $billing_phone;
}
// fetch recent woocommerce orders
$orders = array();
if ( class_exists( 'WooCommerce' ) ) {
$orders = wc_get_orders( array(
'customer_id' => $user_id,
'status' => array(
'wc-pending',
'wc-processing',
'wc-on-hold',
'wc-completed',
'wc-cancelled',
'wc-refunded',
'wc-failed',
),
'type' => 'shop_order',
'orderby' => 'date',
'order' => 'DESC',
'limit' => 15,
'return' => 'objects',
) );
}
// fetch recent subscriptions if subscriptions plugin is active
$subscriptions = array();
if ( class_exists( 'WC_Subscriptions' ) && function_exists( 'wcs_get_subscriptions_for_user' ) ) {
$subscriptions = wcs_get_subscriptions_for_user( $user_id, array(
'post_status' => array(
'wc-pending',
'wc-active',
'wc-on-hold',
'wc-pending-cancel',
'wc-cancelled',
'wc-expired',
'wc-switched',
),
'orderby' => 'date',
'order' => 'DESC',
'limit' => 15,
'return' => 'subscriptions',
) );
}
ob_start(); ?>
<form id="contact-form" method="post">
<p>
<label for="contact-name"><?php esc_html_e( 'Name', 'contact-form' ); ?></label>
<input type="text" id="contact-name" readonly value="<?php echo esc_attr( $name_value ); ?>" style="background-color: #f5f5f5;">
</p>
<p>
<label for="contact-email"><?php esc_html_e( 'Email', 'contact-form' ); ?></label>
<input type="email" id="contact-email" readonly value="<?php echo esc_attr( $email ); ?>" style="background-color: #f5f5f5;">
</p>
<p>
<label for="contact-phone"><?php esc_html_e( 'Phone', 'contact-form' ); ?></label>
<input type="text" id="contact-phone" readonly value="<?php echo esc_attr( $phone_value ); ?>" style="background-color: #f5f5f5;">
</p>
<?php if ( true === $show_url ) : ?>
<p>
<label for="contact-url"><?php esc_html_e( 'URL', 'contact-form' ); ?></label>
<input type="url" id="contact-url" name="contact_url">
</p>
<?php endif; ?>
<?php if ( ! empty( $orders ) || ! empty( $subscriptions ) ) : ?>
<p>
<label for="contact-reference"><?php esc_html_e( 'Order or Subscription', 'contact-form' ); ?></label>
<select id="contact-reference" name="contact_reference">
<option value=""><?php esc_html_e( 'Select Order or Subscription', 'contact-form' ); ?></option>
<?php foreach ( $orders as $order ) : ?>
<?php
// collect product names for display
$product_names = array();
foreach ( $order->get_items() as $item ) {
$product_names[] = $item->get_name();
}
$product_list = implode( ', ', $product_names );
// safely retrieve formatted order date
$date = $order->get_date_created();
$order_date = '';
if ( $date ) {
$order_date = $date->date_i18n( get_option( 'date_format' ) );
}
?>
<option value="<?php echo esc_attr( 'order_' . $order->get_id() ); ?>">
<?php
printf(
esc_html__( 'Order #%1$s – %2$s – %3$s', 'contact-form' ),
esc_html( $order->get_id() ),
esc_html( $order_date ),
esc_html( $product_list )
);
?>
</option>
<?php endforeach; ?>
<?php foreach ( $subscriptions as $subscription ) : ?>
<?php
// collect subscription product names
$product_names = array();
foreach ( $subscription->get_items() as $item ) {
$product_names[] = $item->get_name();
}
$product_list = implode( ', ', $product_names );
// safely retrieve formatted subscription date
$date = $subscription->get_date_created();
$subscription_date = '';
if ( $date ) {
$subscription_date = $date->date_i18n( get_option( 'date_format' ) );
}
?>
<option value="<?php echo esc_attr( 'subscription_' . $subscription->get_id() ); ?>">
<?php
printf(
esc_html__( 'Subscription #%1$s – %2$s – %3$s', 'contact-form' ),
esc_html( $subscription->get_id() ),
esc_html( $subscription_date ),
esc_html( $product_list )
);
?>
</option>
<?php endforeach; ?>
</select>
</p>
<?php endif; ?>
<p>
<label for="contact-subject"><?php esc_html_e( 'Subject', 'contact-form' ); ?></label>
<input type="text" id="contact-subject" name="contact_subject" required>
</p>
<p>
<label for="contact-message"><?php esc_html_e( 'Message', 'contact-form' ); ?></label>
<textarea id="contact-message" name="contact_message" rows="10" cols="40" required></textarea>
</p>
<input type="hidden" name="action" value="contact_form_submit">
<?php wp_nonce_field( 'contact_form_nonce', 'nonce' ); ?>
<p>
<input type="submit" value="<?php esc_attr_e( 'Send Message', 'contact-form' ); ?>">
</p>
<div id="contact-form-response"></div>
</form>
<?php
return ob_get_clean();
}
// handle ajax submission for contact form
add_action( 'wp_ajax_contact_form_submit', 'contact_form_submit' );
function contact_form_submit() {
// verify nonce
check_ajax_referer( 'contact_form_nonce', 'nonce' );
// ensure user is logged in
$user = wp_get_current_user();
if ( ! $user->exists() ) {
wp_send_json_error( apply_filters( 'contact_form_error_not_logged_in', __( 'Not logged in', 'contact-form' ) ) );
}
// collect user data
$user_id = $user->ID;
$first_name = get_user_meta( $user_id, 'first_name', true );
$last_name = get_user_meta( $user_id, 'last_name', true );
$full_name = trim( $first_name . ' ' . $last_name );
$name_value = ! empty( $full_name ) ? $full_name : $user->display_name;
$email = $user->user_email;
$email = sanitize_email( $email );
$email = str_replace( array( "\r", "\n" ), '', $email );
if ( ! is_email( $email ) ) {
wp_send_json_error( apply_filters( 'contact_form_error_validation', __( 'Invalid email address', 'contact-form' ) ) );
}
// get billing phone if woocommerce active
$billing_phone = class_exists( 'WooCommerce' ) ? get_user_meta( $user_id, 'billing_phone', true ) : '';
$phone_value = ! empty( $billing_phone ) ? $billing_phone : __( 'Not Available', 'contact-form' );
// sanitize user inputs
$subject = sanitize_text_field( wp_unslash( $_POST['contact_subject'] ?? '' ) );
$url = esc_url_raw( wp_unslash( $_POST['contact_url'] ?? '' ) );
$message = sanitize_textarea_field( wp_unslash( $_POST['contact_message'] ?? '' ) );
$reference = sanitize_text_field( wp_unslash( $_POST['contact_reference'] ?? '' ) );
// validate reference format
if ( ! empty( $reference ) && ! preg_match( '/^(order|subscription)_[0-9]+$/', $reference ) ) {
$reference = '';
}
// check required fields
if ( empty( $subject ) || empty( $message ) ) {
wp_send_json_error( apply_filters( 'contact_form_error_validation', __( 'Subject and message are required', 'contact-form' ) ) );
}
// prevent header injection
$subject = str_replace( array( "\r", "\n" ), '', $subject );
// build domain-aligned noreply address
$site_domain = wp_parse_url( home_url(), PHP_URL_HOST );
// fallback to admin email if domain parsing fails
if ( ! empty( $site_domain ) ) {
$from_email = 'noreply@' . $site_domain;
} else {
$from_email = get_option( 'admin_email' );
}
// use rfc-compliant line endings for email body
$eol = "\r\n";
// build email body
$email_body = "Name: {$name_value}{$eol}";
$email_body .= "Email: {$email}{$eol}";
$email_body .= "Phone: {$phone_value}{$eol}";
if ( ! empty( $url ) ) {
$email_body .= "URL: {$url}{$eol}";
}
$email_body .= "Reference: {$reference}{$eol}";
$email_body .= "Subject: {$subject}{$eol}";
$email_body .= "Message: {$message}{$eol}";
// build email headers
$headers = array(
'From: ' . $name_value . ' via ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>',
'Reply-To: ' . $email,
'Content-Type: text/plain; charset=UTF-8',
);
// send email to site admin
$sent = wp_mail(
get_option( 'admin_email' ),
sprintf( __( 'Contact Form: %s', 'contact-form' ), $subject ),
$email_body,
$headers
);
// return response
if ( $sent ) {
do_action( 'contact_form_sent', array(
'name' => $name_value,
'email' => $email,
'phone' => $phone_value,
'subject' => $subject,
'message' => $message,
'reference' => $reference,
'url' => $url,
) );
wp_send_json_success( apply_filters( 'contact_form_success_message', __( 'Message sent successfully.', 'contact-form' ) ) );
}
wp_send_json_error( apply_filters( 'contact_form_error_send_failed', __( 'Failed to send message.', 'contact-form' ) ) );
}
// Ref: ChatGPT