/
sfhiv_service_hours.php
392 lines (364 loc) · 11.6 KB
/
sfhiv_service_hours.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
<?php
add_action('init','sfhiv_add_service_hours_type');
function sfhiv_add_service_hours_type(){
register_post_type( 'sfhiv_service_hour',
array(
'labels' => array(
'name' => __( 'Service Times' ),
'singular_name' => __( 'Service Time' )
),
'public' => false,
'hierarchical' => false,
'taxonomies' => array('sfhiv_service_category','sfhiv_population_category'),
'supports' => array('title','editor','excerpt'),
'register_meta_box_cb' => 'sfhiv_add_service_hours_meta_boxes',
)
);
p2p_register_connection_type( array(
'name' => 'service_time',
'from' => 'sfhiv_service',
'to' => 'sfhiv_service_hour',
'admin_box' => false,
'admin_column' => 'to',
) );
}
add_filter( 'the_posts', 'sfhiv_service_load_service_hours', 10, 2);
function sfhiv_service_load_service_hours($posts,$query){
if ( is_admin() || $query->query_vars['post_type'] != 'sfhiv_service' ) return $posts;
p2p_type( 'service_time' )->each_connected( $query, array('post_type'=>'sfhiv_service_hour'), 'times' );
return $posts;
}
add_filter( 'the_posts', 'sfhiv_service_hours_load_related_locations', 10, 2);
function sfhiv_service_hours_load_related_locations($posts, $query){
if ( is_admin() || $query->query_vars['post_type'] != 'sfhiv_service_hour' ) return $posts;
p2p_type( 'related_location' )->each_connected( $query, array(), 'locations' );
return $posts;
}
add_filter( 'the_posts', 'sfhiv_service_hours_load_days', 10, 2);
function sfhiv_service_hours_load_days($posts, $query){
if ( is_admin() || $query->query_vars['post_type'] != 'sfhiv_service_hour' ) return $posts;
foreach($posts as $post){
$days = wp_get_object_terms( $post->ID, "sfhiv_day_of_week_taxonomy", array(
"fields" => "slugs",
));
$post->days = $days;
}
return $posts;
}
function sfhiv_service_hour_get_service($service_hour){
$services = new WP_Query( array(
'post_type' => 'sfhiv_service',
'connected_type' => 'service_time',
'connected_items' => $service_hour->ID,
));
if($services->post_count < 1) return false;
return $services->posts[0];
}
function sfhiv_service_hours_sort_by_location($times){
$locations = array();
foreach($times as $time){
foreach($time->locations as $tiloc){
$found = false;
foreach($locations as $loc){
if($tiloc->ID == $loc->ID){
$loc->times[] = $time;
$found = true;
}
}
if(!$found){
$tiloc->times = array($time);
$locations[] = $tiloc;
}
}
$time->locations = array();
}
return $locations;
}
function sfhiv_service_hours_sort_by_start_end($hours){
$times = array();
foreach($hours as $time){
$found = false;
foreach($times as $t){
if($t['start'] == $time->start && $t['end'] == $time->end){
$found = true;
$t['times'][] = $time;
}
}
if(!$found){
$t = array(
"start" => $time->start,
"end" => $time->end,
"times" => array($time),
);
$times[] = $t;
}
}
return $times;
}
function sfhiv_service_hours_sort_by_day($hours){
$days = array();
foreach($hours as $time){
foreach($time->days as $day){
$found = false;
foreach($days as $day_obj){
if($day_obj['day'] == $day){
$day_obj['times'][] = $time;
$found = true;
}
}
if(!$found){
$d = array(
"day" => $day,
"times" => array($time),
);
$days[] = $d;
}
}
}
// sort each day by taxonomy day order
// sort each days hours by start time
return $days;
}
add_filter( 'the_posts', 'sfhiv_service_hours_load_time', 10, 2);
function sfhiv_service_hours_load_time($posts, $query){
if ( is_admin() || $query->query_vars['post_type'] != 'sfhiv_service_hour' ) return $posts;
foreach($posts as $post){
$post->start = sfhiv_service_get_start_time($post);
$post->end = sfhiv_service_get_end_time($post);
}
return $posts;
}
add_action( 'pre_get_posts', 'sfhiv_service_hour_sort_order', 5 );
function sfhiv_service_hour_sort_order( $query ) {
if ( is_admin() || !isset($query->query_vars['post_type']) || $query->query_vars['post_type'] != 'sfhiv_service_hour' ) return;
$query->query_vars['orderby'] = 'menu_order title date';
$query->query_vars['order'] = 'ASC';
}
function sfhiv_add_service_hours_meta_boxes(){
add_meta_box( 'service_hours_time', 'Service Time', 'sfhiv_services_hours_op_meta', 'sfhiv_service_hour' );
sfhiv_location_add_choose_location_meta_box('sfhiv_service_hour');
}
function sfhiv_services_hours_op_meta($post){
sfhiv_draw_services_hours_op_meta($post,'hours');
sfhiv_draw_service_hours_time_meta($post,'hours');
}
function sfhiv_draw_services_hours_op_meta($days=array(),$form_name){
$day_terms = get_terms( "sfhiv_day_of_week_taxonomy", array(
"hide_empty" => false,
));
include 'templates/service_hours.php';
}
function sfhiv_draw_service_hours_time_meta($start,$end,$form_name){
$start = date('g:i a',$start);
$end = date('g:i a',$end);
include 'templates/service_hours_time.php';
}
function sfhiv_service_hours_string_to_time($string){
return strtotime($string)-strtotime('12AM');
}
function sfhiv_add_services_meta_boxes(){
wp_enqueue_script('sfhiv_service_hour_js', plugins_url('assets/js/admin-service_hour.js',__FILE__),array('jquery'));
wp_enqueue_style('sfhiv_service_hour_css', plugins_url('assets/css/admin-service_hour.css',__FILE__));
wp_enqueue_script('sfhiv_location_js', plugins_url('assets/js/admin-location.js',__FILE__),array('jquery'));
wp_enqueue_style('sfhiv_location_css', plugins_url('assets/css/admin-location.css',__FILE__));
add_meta_box( 'service_time', 'Time', 'sfhiv_service_time_box', 'sfhiv_service' );
}
function sfhiv_service_time_box($post){
$service_hours = new WP_Query( array(
'connected_type' => 'service_time',
'connected_items' => $post->ID,
));
$collected_hours = array();
foreach($service_hours->posts as $hour){
$match = false;
$start = sfhiv_service_get_start_time($hour);
$end = sfhiv_service_get_end_time($hour);
$location = sfhiv_service_get_location_id($hour);
foreach($collected_hours as $index => $time){
if($time['start'] == $start && $time['end'] == $end && $location == $time['location']){
$match = true;
$collected_hours[$index]['days'] = array_merge($collected_hours[$index]['days'],sfhiv_service_get_service_days($hour));
}
}
if(!$match){
array_push($collected_hours,array(
'days' => sfhiv_service_get_service_days($hour),
'start' => $start,
'end' => $end,
'location' => $location,
));
}
}
foreach($collected_hours as $time){
sfhiv_service_draw_service_hour_form($time);
}
echo '<a href="#" id="new_sfhiv_service_hour" class="button" >New Time</a>';
}
function sfhiv_service_get_start_time($post){
return get_post_meta($post->ID, 'sfhiv_service_start',true);
}
function sfhiv_service_get_end_time($post){
return get_post_meta($post->ID, 'sfhiv_service_end',true);
}
function sfhiv_service_get_service_days($post){
return wp_get_object_terms( $post->ID, "sfhiv_day_of_week_taxonomy", array(
"fields" => "slugs",
));
}
function sfhiv_service_hour_get_parent($post){
$parents = new WP_Query( array(
'connected_type' => 'service_time',
'connected_items' => $post->ID,
));
if($parents->post_count > 0){
return $parents->posts[0];
}
return false;
}
function sfhiv_service_get_location_id($post){
$location = sfhiv_location_get_related_location($post->ID);
if($location){
return $location->ID;
}
return 0;
}
function sfhiv_service_draw_service_hour_form($data=array()){
echo '<div class="service_hour">';
sfhiv_draw_services_hours_op_meta($data['days'],'hours[position]');
sfhiv_draw_service_hours_time_meta($data['start'], $data['end'],'hours[position]');
sfhiv_location_location_list_draw(array($data['location']),array(
'field_name' => 'hours[position][sfhiv_location]',
));
echo '</div>';
}
add_action('wp_ajax_sfhiv_service_hour_form', 'sfhiv_service_hour_ajax_hour_form');
function sfhiv_service_hour_ajax_hour_form() {
sfhiv_service_draw_service_hour_form();
die();
}
add_action( 'save_post', 'sfhiv_service_hour_time_save', 200, 2 );
function sfhiv_service_hour_time_save($post_ID,$post){
if(get_post_type($post_ID) != 'sfhiv_service') return;
$service_hours = new WP_Query( array(
'connected_type' => 'service_time',
'connected_items' => $post_ID,
));
foreach($service_hours->posts as $hour){
wp_delete_post( $hour->ID, true );
}
foreach($_POST['hours'] as $key=>$post_data){
if(is_array($post_data['days'])){
foreach($post_data['days'] as $day){
sfhiv_create_or_update_service_hours(false,array_merge($post_data,array(
"day_of_week" => $day,
)),$post_ID);
}
}
}
sfhiv_service_hours_set_order();
}
function sfhiv_create_or_update_service_hours($post_ID=false,$post_data,$parent_ID=false){
if(!$post_ID){
if( isset($post_data['day_of_week'])
&& (isset($post_data['start']) && $post_data['start']!='')
&& (isset($post_data['end']) && $post_data['end']!='')){
$post_ID = wp_insert_post(array(
'post_type' => 'sfhiv_service_hour',
'post_title' => "will",
'post_content' => "get",
'post_excerpt' => "overwritten",
),true);
}
}
if(!$post_ID) return false;
if($parent_ID){
$parents = new WP_Query( array(
'connected_type' => 'service_time',
'connected_items' => $post_ID,
));
$found = false;
foreach($parents as $parent){
if($parent->ID == $parent_ID) $found = true;
}
if(!$found){
p2p_create_connection( 'service_time', array(
'from' => $parent_ID,
'to' => $post_ID,
));
}
$tax_terms = array('sfhiv_service_category','sfhiv_population_category');
foreach($tax_terms as $tax){
$parent_terms = wp_get_object_terms($parent_ID,$tax,array('fields'=>'slugs'));
wp_set_object_terms($post_ID,$parent_terms,$tax);
}
$parent = get_post($parent_ID);
$parent_status = get_post_status($parent_ID);
wp_update_post(array(
'ID' => $post_ID,
'post_status' => $parent_status,
'post_title' => $parent->post_title,
'post_content' => $parent->post_content,
'post_excerpt' => $parent->post_excerpt,
));
$parent_custom = get_post_custom($parent_ID);
foreach($parent_custom as $custom_key => $custom_value){
delete_post_meta($post_ID,$custom_key);
foreach($custom_value as $meta_value){
add_post_meta($post_ID,$custom_key,$meta_value);
}
}
}
// update time
sfhiv_service_time_of_day_save_post_update($post_ID);
if(isset($post_data['start'])){
$seconds = sfhiv_service_hours_string_to_time($post_data['start']);
update_post_meta($post_ID, 'sfhiv_service_start', $seconds);
}
if(isset($post_data['end'])){
$seconds = sfhiv_service_hours_string_to_time($post_data['end']);
update_post_meta($post_ID, 'sfhiv_service_end', $seconds);
}
if(isset($post_data['sfhiv_location'])){
sfhiv_location_relation_save($post_ID,$post_data['sfhiv_location']);
}
if(isset($post_data['day_of_week'])){
wp_set_object_terms( $post_ID, $post_data['day_of_week'], 'sfhiv_day_of_week_taxonomy', false );
if($parent_ID){
wp_set_object_terms( $parent_ID, $post_data['day_of_week'], 'sfhiv_day_of_week_taxonomy', true );
}
}
}
function sfhiv_service_hours_set_order(){
$query = new WP_Query(array(
'post_type' => 'sfhiv_service_hour',
'nopaging' => true,
));
usort($query->posts,function($a,$b){
$day_terms = get_terms( "sfhiv_day_of_week_taxonomy", array(
"hide_empty" => false,
));
foreach($day_terms as $term){
if(has_term($term,'sfhiv_day_of_week_taxonomy',$a) && has_term($term,'sfhiv_day_of_week_taxonomy',$b)){
if(sfhiv_service_get_start_time($a) > sfhiv_service_get_start_time($b)){
return 1;
}else{
return -1;
}
}
if(has_term($term,'sfhiv_day_of_week_taxonomy',$a)){
return -1;
}
if(has_term($term,'sfhiv_day_of_week_taxonomy',$b)){
return 1;
}
}
return 0;
});
foreach($query->posts as $index => $post){
wp_update_post(array(
'ID' => $post->ID,
'menu_order' => $index,
));
}
}
?>