-
Notifications
You must be signed in to change notification settings - Fork 0
/
leadgen.php
409 lines (284 loc) · 12.1 KB
/
leadgen.php
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?php
/**
* Plugin Name: Lead Gen Form
* Description: Collect customer data and build customer profiles inside of your WordPress Dashboard.
* Version: 1.0
* Author: Leighton Sapir
* Author URI: http://iamleigh.com/
* License: MIT
*/
if ( ! defined( 'ABSPATH' ) ) {
die( "Now you see me, now you don't... puff!!!" );
}
// Return plugin URL
if ( ! function_exists( 'leadgen_plugin_url' ) ) {
function leadgen_plugin_url() {
return trailingslashit( plugin_dir_url( __FILE__ ) );
}
}
// Return plugin path
if ( ! function_exists( 'leadgen_plugin_dir' ) ) {
function leadgen_plugin_dir() {
return trailingslashit( plugin_dir_path( __FILE__ ) );
}
}
// CUSTOMER CPT
function leadgen_customers() {
$labels = array(
'name' => 'Customers',
'singular_name' => 'Customer',
'add_new' => 'Add New',
'add_new_item' => 'Add New Customer',
'edit_item' => 'Edit Customer',
'new_item' => 'New Customer',
'view_item' => 'View Customer',
'view_items' => 'View Customers',
'search_items' => 'Search Customers',
'not_found' => 'No customers found',
'not_found_in_trash' => 'No customers found in trash',
);
$supports = array(
'title',
'editor',
);
$args = array(
'labels' => $labels,
'public' => true,
'menu_icon' => null,
'rewrite' => array( 'slug' => 'customer' ),
'capability_type' => 'post',
'has_archive' => false,
'menu_position' => '15',
'supports' => $supports,
);
register_post_type( 'customers', $args );
}
add_action( 'init', 'leadgen_customers' );
// CUSTOMER META BOX
function leadgen_customer_info() {
add_meta_box(
'leadgen_customer_info_meta',
'Customer Info',
'leadgen_customer_info_show',
'customers',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'leadgen_customer_info' );
function leadgen_customer_info_show( $customer_info ) {
$phone = esc_html( get_post_meta( $customer_info->ID, 'phone', true ) );
$email = esc_html( get_post_meta( $customer_info->ID, 'email', true ) );
$budget = esc_html( get_post_meta( $customer_info->ID, 'budget', true ) );
$message = esc_html( get_post_meta( $customer_info->ID, 'message', true ) ); ?>
<table>
<tr>
<td>Phone:</td>
<td><input type="number" size="80" name="leadgen_customer_phone" value="<?php echo $phone; ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="email" size="80" name="leadgen_customer_email" value="<?php echo $email; ?>" /></td>
</tr>
<tr>
<td>Desired Budget:</td>
<td><input type="number" size="80" name="leadgen_customer_budget" value="<?php echo $budget; ?>" /></td>
</tr>
</table>
<?php
}
function leadgen_customer_info_add( $customer_id, $customer_info ) {
if ( $customer_info->post_type === 'customers' ) {
if ( isset( $_POST['leadgen_customer_phone'] ) && $_POST['leadgen_customer_phone'] != '' ) {
update_post_meta( $customer_id, 'phone', $_POST['leadgen_customer_phone'] );
}
if ( isset( $_POST['leadgen_customer_email'] ) && $_POST['leadgen_customer_email'] != '' ) {
update_post_meta( $customer_id, 'email', $_POST['leadgen_customer_email'] );
}
if ( isset( $_POST['leadgen_customer_budget'] ) && $_POST['leadgen_customer_budget'] != '' ) {
update_post_meta( $customer_id, 'budget', $_POST['leadgen_customer_budget'] );
}
}
}
add_action( 'save_post', 'leadgen_customer_info_add', 10, 2 );
// SHORTCODE
function leadgen_form( $atts ) {
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
$shortcode_atts = shortcode_atts( array(
'styles' => true,
'title' => 'Contact Us',
'name' => 'Name',
'name_max' => '20',
'phone' => 'Phone',
'phone_max' => '10',
'email' => 'Email',
'email_max' => '',
'budget' => 'Desired Budget',
'budget_min' => '100',
'budget_max' => '1000',
'message' => 'Message',
'message_cols' => '100',
'message_rows' => '6',
'submit' => 'Submit',
'ajax' => true,
), $atts );
extract( $shortcode_atts );
if ( $ajax ) {
wp_enqueue_script( 'leadgen', leadgen_plugin_url() . 'assets/js/leadgen.js', array( 'jquery' ) );
wp_localize_script( 'leadgen', 'leadgen_data', array( 'shortcode_atts' => $shortcode_atts, 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
if ( 'POST' === $_SERVER['REQUEST_METHOD'] && ! empty( $_POST['action'] ) && $_POST['action'] === "leadgen_new_customer" ) {
$response = save_new_customer_form();
if ( ! $response['success'] ) {
$errors = $response['errors'];
}
}
if ( $styles === true ) {
$class = ' class="leadgen-ui leadgen-form"';
} else {
$class = '';
} ?>
<div id="leadgen-form">
<form id="leadgen_new_customer"<?php echo $class; ?> name="leadgen_new_customer" method="post" action="">
<?php if (
( isset( $_POST['title'] ) && !empty( $_POST['title'] ) ) ||
( isset( $_POST['leadgen_customer_phone'] ) && !empty( $_POST['leadgen_customer_phone'] ) ) ||
( isset( $_POST['leadgen_customer_email'] ) && !empty( ['leadgen_customer_email'] ) ) ||
( isset( $_POST['leadgen_customer_budget'] ) && !empty( ['leadgen_customer_budget'] ) )
) { ?>
<label class="leadgen-label--info"><?php echo $errors['form']; ?></label>
<?php } ?>
<?php if ( $ajax === true ) { ?>
<div class="leadgen-notice"></div>
<?php } ?>
<h1 class="leadgen-title"><?php echo $title; ?></h1>
<div id="leadgen-customer-name" class="leadgen-field">
<label for="lgf-title" class="leadgen-label"><?php echo $name; ?></label>
<input type="text" id="lgf-title" class="leadgen-input<?php if ( isset( $errors['title'] ) && !empty( $errors['title'] ) ) { echo ' ' . $errors['class']; } ?>" value="" tabindex="1" name="title" maxlength="<?php echo $name_max; ?>"/>
<?php if ( isset( $errors['title'] ) && !empty( $errors['title'] ) ) { ?>
<label id="lgf-title" class="leadgen-label--error"><?php echo $errors['title']; ?></label>
<?php } ?>
</div>
<div id="leadgen-customer-phone" class="leadgen-field">
<label for="lgf-phone" class="leadgen-label"><?php echo $phone; ?></label>
<input type="tel" id="lgf-phone" class="leadgen-input<?php if ( isset( $errors['leadgen_customer_phone'] ) && !empty( $errors['leadgen_customer_phone'] ) ) { echo ' ' . $errors['class']; } ?>" value="" tabindex="1" name="leadgen_customer_phone" maxlength="<?php echo $phone_max; ?>" />
<?php if ( isset( $errors['leadgen_customer_phone'] ) && !empty( $errors['leadgen_customer_phone'] ) ) { ?>
<label id="lgf-phone" class="leadgen-label--error"><?php echo $errors['leadgen_customer_phone']; ?></label>
<?php } ?>
</div>
<div id="leadgen-customer-email" class="leadgen-field">
<label for="lgf-email" class="leadgen-label"><?php echo $email; ?></label>
<input type="email" id="lgf-email" class="leadgen-input<?php if ( isset( $errors['leadgen_customer_email'] ) && !empty( $errors['leadgen_customer_email'] ) ) { echo ' ' . $errors['class']; } ?>" value="" tabindex="1" name="leadgen_customer_email" maxlength="<?php echo $email_max; ?>" />
<?php if ( isset( $errors['leadgen_customer_email'] ) && !empty( $errors['leadgen_customer_email'] ) ) { ?>
<label id="lgf-email" class="leadgen-label--error"><?php echo $errors['leadgen_customer_email']; ?></label>
<?php } ?>
</div>
<div id="leadgen-customer-budget" class="leadgen-field">
<label for="lgf-budget" class="leadgen-label"><?php echo $budget; ?></label>
<input type="number" id="lgf-budget" class="leadgen-input<?php if ( isset( $errors['leadgen_customer_budget'] ) && !empty( $errors['leadgen_customer_budget'] ) ) { echo ' ' . $errors['class']; } ?>" value="" tabindex="1" name="leadgen_customer_budget" min="<?php echo $budget_min; ?>" max="<?php echo $budget_max; ?>" />
<?php if ( isset( $errors['leadgen_customer_budget'] ) && !empty( $errors['leadgen_customer_budget'] ) ) { ?>
<label id="lgf-budget" class="leadgen-label--error"><?php echo $errors['leadgen_customer_budget']; ?></label>
<?php } ?>
</div>
<div class="leadgen-field">
<label for="lgf-description" class="leadgen-label"><?php echo $message; ?></label>
<textarea id="lgf-description" class="leadgen-textarea" tabindex="3" name="description" cols="<?php echo $message_cols; ?>" rows="<?php echo $message_rows; ?>"></textarea>
</div>
<div class="leadgen-field--button">
<button id="submit" class="leadgen-button" name="submit"><?php echo $submit; ?></button>
</div>
<?php foreach ( $shortcode_atts as $key => $shortcode_att ) { ?>
<input type="hidden" name="shortcode_atts[<?php echo $key; ?>]" value="<?php echo $shortcode_att; ?>"/>
<?php } ?>
<input type="hidden" name="action" value="leadgen_new_customer" />
<?php wp_nonce_field( 'leadgen-new-client', 'leadgen_nonce' ); ?>
</form>
</div>
<?php
}
add_shortcode( "leadgen", "leadgen_form" );
function leadgen_load_styles() {
wp_enqueue_style( 'leadgen', leadgen_plugin_url() . 'assets/css/leadgen.css' );
}
add_action( 'wp_enqueue_scripts', 'leadgen_load_styles' );
function leadgen_handle_form_submit() {
$post_data = $_POST['form_data'];
$response = save_new_customer_form();
if ( ! $response['success'] ) {
wp_send_json_error( $response['errors'] );
}
wp_send_json_success( $post_data['data'] );
}
function save_new_customer_form() {
$shortcode_atts = $_POST['shortcode_atts'];
extract( $shortcode_atts );
$errors = array();
if ( ! isset( $_POST['leadgen_nonce'] ) || ! wp_verify_nonce( $_POST['leadgen_nonce'], 'leadgen-new-client' ) ) {
return array(
'success' => false,
'errors' => array(
'leadgen_nonce' => 'Sorry, your nonce did not verify.',
),
);
}
$customer_name = $customer_phone = $customer_email = $customer_budget = $customer_message = '';
if (
( empty( $_POST['title'] ) && $_POST['title'] === '' ) ||
( empty( $_POST['leadgen_customer_phone'] ) && $_POST['leadgen_customer_phone'] === '' ) ||
( empty( $_POST['leadgen_customer_email'] ) && $_POST['leadgen_customer_email'] === '' ) ||
( empty( $_POST['leadgen_customer_budget'] ) && $_POST['leadgen_customer_budget'] === '' )
) {
$errors['form'] = 'Something went wrong. Please, verify and try again.';
$errors['class'] = 'leadgen-has_error';
}
if ( isset( $_POST['title'] ) && $_POST['title'] !== '' ) {
$customer_name = $_POST['title'];
} else {
$errors['title'] = '"' . $name . '" is required.';
}
if ( isset( $_POST['leadgen_customer_phone'] ) && $_POST['leadgen_customer_phone'] !== '' ) {
$customer_phone = $_POST['leadgen_customer_phone'];
} else {
$errors['leadgen_customer_phone'] = '"' . $phone . '" is required.';
}
if ( isset( $_POST['leadgen_customer_email'] ) && $_POST['leadgen_customer_email'] !== '' ) {
$customer_email = $_POST['leadgen_customer_email'];
} else {
$errors['leadgen_customer_email'] = '"' . $email . '" is required.';
}
if ( isset( $_POST['leadgen_customer_budget'] ) && $_POST['leadgen_customer_budget'] !== '' ) {
$customer_budget = $_POST['leadgen_customer_budget'];
} else {
$errors['leadgen_customer_budget'] = '"' . $budget . '" is required.';
}
if ( isset( $_POST['description'] ) ) {
$customer_message = $_POST['description'];
}
if ( ! empty( $customer_name ) && ! empty( $customer_phone ) && ! empty( $customer_email ) && ! empty( $customer_budget ) ) {
// add the content of the form to $post as an array
$new_client = array(
'post_title' => $customer_name,
'post_content' => $customer_message,
'leadgen_customer_phone' => $customer_phone,
'leadgen_customer_email' => $customer_email,
'leadgen_customer_budget' => $customer_budget,
'post_status' => 'publish',
'post_type' => 'customers',
);
// save the new post
$pid = wp_insert_post( $new_client );
// return as sucess
return array(
'success' => true,
'data' => $pid,
);
}
// return a response
return array(
'success' => false,
'errors' => $errors,
);
}
add_action( 'wp_ajax_leadgen_new_customer', 'leadgen_handle_form_submit' );
add_action( 'wp_ajax_nopriv_leadgen_new_customer', 'leadgen_handle_form_submit' );