-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorder.php
More file actions
362 lines (304 loc) · 14.5 KB
/
corder.php
File metadata and controls
362 lines (304 loc) · 14.5 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
<?php
/*
Plugin Name: C Order
Plugin URI: http://taskodelic.com/corder
Description: Plugin for simple client orders management.
Version: 0.1
Author: pedectrian
Author URI: http://taskodelic.om
Text Domain: corder
Requires at least: 3.0
Tested up to: 3.8
*/
class Corder
{
const VERSION = '0.1';
protected $wpdb;
/** @var string $prefix */
public $prefix = 'corder_client';
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
// Add custom post type
add_action( 'init', array( $this, 'create_order_post_type') );
// Add custom post type list fields
add_filter( 'manage_edit-corder_order_columns', array( $this, 'set_custom_edit_corder_order_columns' ));
add_action( 'manage_corder_order_posts_custom_column', array( $this, 'custom_corder_order_column' ), 10, 2 );
// Add custom post type edit meta box and set order title (Order #id)
add_action( 'save_post', array( $this, 'save_corder_order_meta' ));
add_filter( 'wp_insert_post_data', array( $this, 'set_corder_order_title' ));
// Remove all actions for custom post type except edit
add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ));
}
/**
* Adds custom post type 'corder_order'
*/
public function create_order_post_type() {
register_post_type( 'corder_order',
array(
'labels' => array(
'name' => __( 'Orders' ),
'singular_name' => __( 'Order' )
),
'public' => true,
'publicly_queryable' => false,
'query_var' => false,
'has_archive' => true,
'supports' => array(
'revisions',
),
'menu_position' => 2,
'register_meta_box_cb' => array( $this, 'corder_add_post_type_metabox' )
)
);
}
/**
* Set corder_order post type list columns
* @param $columns
* @return mixed
*/
public function set_custom_edit_corder_order_columns( $columns ) {
unset( $columns['author'] );
$newColumns = array(
'name' => __( 'Client Name', 'corder' ),
'phone' => __( 'Client Phone', 'corder' ),
'email' => __( 'Client Email', 'corder' ),
'full_address' => __( 'Client Full Address', 'corder' ),
'delivery_type' => __( 'Client Delivery Type', 'corder' ),
'comment' => __( 'Comment', 'corder' ),
'amount' => __( 'Amount', 'corder' ),
// 'price' => __( 'Price', 'corder' ),
);
return array_merge($columns, $newColumns);
}
/**
* Get data for corder_order post type list fields
* @param $column
* @param $post_id
*/
public function custom_corder_order_column( $column, $post_id ) {
$term = get_post_meta( $post_id , '_' . $this->prefix . '_' . $column, true );
// Change delivery ID to delivery name
if( $column == 'delivery_type' ) {
switch( $term ) {
case 1:
$term = 'Самовывоз';
break;
case 2:
$term = 'Курьер';
break;
}
}
// Echo string or not found message
if ( $term && is_string( $term ) ) {
echo $term;
} else {
echo "-";
}
}
/**
* Saves corder_order post type meta fields
* @param $post_id
* @return bool
*/
public function save_corder_order_meta( $post_id ) {
// Form nonce validation
if( !wp_verify_nonce( $_POST['corder_noncename'], plugin_basename(__FILE__) ) ) {
return $post_id;
}
// is the user allowed to edit the post or page?
if( ! current_user_can( 'edit_post', $post_id )){
return $post_id;
}
$prefix = $this->prefix . '_';
// Collect user data array
$client = array(
'_'.$prefix.'name' => isset($_POST[$prefix.'name']) ? $_POST[$prefix.'name'] : '',
'_'.$prefix.'phone' => isset($_POST[$prefix.'phone']) ? $_POST[$prefix.'phone'] : '',
'_'.$prefix.'town' => isset($_POST[$prefix.'town']) ? $_POST[$prefix.'town'] : '',
'_'.$prefix.'full_address' => isset($_POST[$prefix.'full_address']) ? $_POST[$prefix.'full_address'] : '',
'_'.$prefix.'delivery_type' => isset($_POST[$prefix.'delivery_type']) ? $_POST[$prefix.'delivery_type'] : '',
'_'.$prefix.'price' => isset($_POST[$prefix.'price']) ? $_POST[$prefix.'price'] : '',
'_'.$prefix.'comment' => isset($_POST[$prefix.'comment']) ? $_POST[$prefix.'comment'] : '',
'_'.$prefix.'amount' => isset($_POST[$prefix.'amount']) ? $_POST[$prefix.'amount'] : 1,
);
// add values as custom fields
foreach( $client as $key => $value ) { // cycle through the $quote_post_meta array
// if( $post->post_type == 'revision' ) return; // don't store custom data twice
$value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
update_post_meta( $post_id, $key, $value);
}
return true;
}
/**
* Sets corder_order title on save action
*/
function set_corder_order_title( $post ){
if(!isset($_GET['post_type']) || $_GET['post_type'] != 'corder_order' || $post['post_status'] != 'auto-draft') {
return $post;
}
$order_id = get_option( 'order_id', 0 ) + 1;
$post['post_title'] = "Order #{$order_id}";
update_option( 'order_id', $order_id );
return $post;
}
/**
* Removes actions for corder_order post type
* @param $actions
* @return mixed
*/
public function remove_row_actions( $actions )
{
global $current_screen;
if( $current_screen->post_type != 'corder_order' )
return $actions;
unset( $actions['view'] );
unset( $actions['trash'] );
unset( $actions['inline hide-if-no-js'] );
return $actions;
}
/**
* Adds the corder_order meta box
*/
function corder_add_post_type_metabox() {
add_meta_box( 'corder_metabox', 'Order', array( $this, 'corder_metabox' ), 'corder_order', 'normal' );
}
/**
* Meta box for corder_order edit page
*/
public function corder_metabox() {
global $post;
// Noncename needed to verify where the data originated
wp_nonce_field(plugin_basename(__FILE__), 'corder_noncename');
// Get the data if its already been entered
$prefix = 'corder_client';
$corder_client_name = get_post_meta($post->ID, '_'.$prefix.'_name', true);
$corder_client_phone = get_post_meta($post->ID, '_'.$prefix.'_phone', true);
$corder_client_email = get_post_meta($post->ID, '_'.$prefix.'_email', true);
$corder_client_town = get_post_meta($post->ID, '_'.$prefix.'_town', true);
$corder_client_fulladdress = get_post_meta($post->ID, '_'.$prefix.'_full_address', true);
$corder_client_delivery_type = get_post_meta($post->ID, '_'.$prefix.'_delivery_type', true);
$corder_client_amount = get_post_meta($post->ID, '_'.$prefix.'_amount', true);
$corder_client_comment = get_post_meta($post->ID, '_'.$prefix.'_comment', true);
// $corder_client_price = get_post_meta($post->ID, '_'.$prefix.'_price', true);
// Echo out the field
?>
<div class="width_full p_box">
<p>
<label>Client Name<br>
<input type="text" name="corder_client_name" class="widefat" value="<?php echo esc_attr($corder_client_name); ?>">
</label>
</p>
<p>
<label>Client Phone<br>
<input type="phone" name="corder_client_phone" class="widefat" value="<?php echo esc_attr($corder_client_phone); ?>">
</label>
</p>
<p>
<label>Client Email<br>
<input type="phone" name="corder_client_email" class="widefat" value="<?php echo esc_attr($corder_client_email); ?>">
</label>
</p>
<p>
<label>Client Town<br>
<textarea name="corder_client_town" class="widefat"><?php echo esc_attr($corder_client_town); ?></textarea>
</label>
</p>
<p>
<label>Client Full Address<br>
<textarea name="corder_client_full_address" class="widefat"><?php echo esc_attr($corder_client_fulladdress); ?></textarea>
</label>
</p>
<p>
<label>Client Delivery Type<br></label>
<label><input type="radio" name="corder_client_delivery_type" value="1" <?php checked($corder_client_delivery_type, 1); ?> /> Самовывоз<br/></label>
<label><input type="radio" name="corder_client_delivery_type" value="2" <?php checked($corder_client_delivery_type, 2); ?> /> Курьерская доставка</label>
</p>
<p>
<label>Amount<br>
<input type="text" name="corder_client_amount" class="widefat" value="<?php echo esc_attr($corder_client_amount); ?>">
</label>
</p>
<p>
<label>Comment<br>
<textarea name="corder_client_comment" class="widefat"><?php echo esc_attr($corder_client_comment); ?></textarea>
</label>
</p>
<!-- <p>-->
<!-- <label>Price<br>-->
<!-- <input type="text" name="corder_client_price" class="widefat" value="--><?php //echo esc_attr($corder_client_price); ?><!--">-->
<!-- </label>-->
<!-- </p>-->
</div>
<?php
}
public function getOrderForm() {
$processed = '';
if( !isset($_POST['processed']) && isset($_POST['corder_noncename']) && wp_verify_nonce( $_POST['corder_noncename'], plugin_basename(__FILE__) ) ) {
$post = array(
'post_status' => 'publish',
'post_type' => 'corder_order',
);
$order_id = get_option( 'order_id', 0 ) + 1;
$post['post_title'] = "Order #{$order_id}";
$post_id = wp_insert_post( $post );
$prefix = $this->prefix . '_';
// Collect user data array
$client = array(
'_'.$prefix.'name' => isset($_POST[$prefix.'name']) ? $_POST[$prefix.'name'] : '',
'_'.$prefix.'phone' => isset($_POST[$prefix.'phone']) ? $_POST[$prefix.'phone'] : '',
'_'.$prefix.'email' => isset($_POST[$prefix.'email']) ? $_POST[$prefix.'email'] : '',
'_'.$prefix.'town' => isset($_POST[$prefix.'town']) ? $_POST[$prefix.'town'] : '',
'_'.$prefix.'full_address' => isset($_POST[$prefix.'full_address']) ? $_POST[$prefix.'full_address'] : '',
'_'.$prefix.'delivery_type' => isset($_POST[$prefix.'delivery_type']) ? $_POST[$prefix.'delivery_type'] : '',
//'_'.$prefix.'price' => 3500,
'_'.$prefix.'comment' => isset($_POST[$prefix.'comment']) ? $_POST[$prefix.'comment'] : '',
'_'.$prefix.'amount' => 1,
);
// add values as custom fields
foreach( $client as $key => $value ) { // cycle through the $quote_post_meta array
// if( $post->post_type == 'revision' ) return; // don't store custom data twice
$value = implode(',', (array)$value); // if $value is an array, make it a CSV (unlikely)
update_post_meta( $post_id, $key, $value);
}
$processed = "<input type='hidden' name='processed' value='1'>";
update_option( 'order_id', $order_id );
wp_mail( 'info@hair-remover-shop.ru', 'Новый заказ №' . ($order_id), 'На сайте создан новый заказ' );
}
?>
<form method="POST" class="client-order-form">
<?php echo $processed ?>
<?php if($processed != '') : ?>
<script>
alert('Спасибо! Ваш заказ был принят. Мы свяжемся с вами в ближайшее время.');
</script>
<?php endif; ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="<?php echo get_template_directory_uri(); ?>/js/jquery.h5validate.js"></script>
<script>
$(document).ready(function () {
$('.client-order-form').h5Validate();
});
</script>
<?php wp_nonce_field(plugin_basename(__FILE__), 'corder_noncename'); ?>
<label class="f-label">ФИО</label>
<input class="f-text" title="Your name is required." required="required" type="text" name="corder_client_name" placeholder="Иванова Мария Сергеевна">
<label class="f-label">Полный адрес (с индексом)</label>
<input class="f-text" required="required" type="text" name="corder_client_full_address" placeholder="142456, Москва, ул. Мира, д. 3, кв. 11">
<label class="f-label">Email</label>
<input class="f-text" required="required" type="email" name="corder_client_email" placeholder="ivanova.maria@example.ru">
<label class="f-label">Телефон</label>
<input class="f-text" required="required" type="text" name="corder_client_phone" placeholder="89123456789">
<label class="f-label">Комментарий</label>
<input class="f-text" type="text" name="corder_client_comment" placeholder="">
<p>
<label class="f-label">Тип доставки<br></label>
<label><input type="radio" name="corder_client_delivery_type" value="1" checked /> Самовывоз<br/></label>
<label><input type="radio" name="corder_client_delivery_type" value="2" /> Курьерская доставка</label>
</p>
<div class="align-center"><button type="submit" class="btn" >Заказать</button></div>
</form>
<?php
}
}
$corder = new Corder();