-
Notifications
You must be signed in to change notification settings - Fork 50
/
wp-event-manager-functions.php
2207 lines (1994 loc) · 63.4 KB
/
wp-event-manager-functions.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if(!function_exists('get_event_listings')) :
/**
* Queries event listings with certain criteria and returns them.
*
* @access public
* @return WP_Query
*/
function get_event_listings($args = array()) {
global $wpdb, $event_manager_keyword;
$args = wp_parse_args($args, array(
'search_location' => '',
'search_keywords' => '',
'search_datetimes' => array(),
'search_categories' => array(),
'search_event_types' => array(),
'search_ticket_prices' => array(),
'offset' => 0,
'posts_per_page' => 15,
'orderby' => 'date',
'order' => 'DESC',
'featured' => null,
'cancelled' => null,
'event_online' => null,
'fields' => 'all',
'lang' => '',
'post_status' => array(),
));
/**
* Perform actions that need to be done prior to the start of the event listings query.
* @param array $args Arguments used to retrieve event listings.
* @since 1.5
*/
do_action('get_event_listings_init', $args);
if(get_option('event_manager_hide_expired')) {
$post_status = 'publish';
} else {
$post_status = array('publish', 'expired');
}
$query_args = array(
'post_type' => 'event_listing',
'post_status' => $post_status,
'ignore_sticky_posts' => 1,
'offset' => absint($args['offset']),
'posts_per_page' => intval($args['posts_per_page']),
'orderby' => $args['orderby'],
'order' => $args['order'],
'tax_query' => array(),
'meta_query' => array(),
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'cache_results' => false,
'fields' => $args['fields']
);
if($args['posts_per_page'] < 0) {
$query_args['no_found_rows'] = true;
}
if(!empty($args['search_location'])) {
$location_meta_keys = array('geolocation_formatted_address', '_event_location', 'geolocation_state_long');
$location_search = array('relation' => 'OR');
foreach($location_meta_keys as $meta_key) {
$location_search[] = array(
'key' => $meta_key,
'value' => $args['search_location'],
'compare' => 'like',
'type' => 'char',
);
$location_search[] = array(
'key' => $meta_key,
'value' => trim(preg_replace("/[^a-zA-Z,\s]/", "", $args['search_location']), ','),
'compare' => 'like',
'type' => 'char',
);
}
$query_args['meta_query'][] = $location_search;
}
if(!is_null($args['featured'])) {
$query_args['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => '_event_featured',
'value' => '1',
'compare' => $args['featured'] ? '=' : '!='
),
array(
'key' => '_featured',
'value' => '1',
'compare' => $args['featured'] ? '=' : '!='
)
);
$query_args['meta_query']['relation'] = 'AND';
}
if(!is_null($args['cancelled']) || 1 === absint(get_option('event_manager_hide_cancelled_events'))) {
$query_args['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => '_event_cancelled',
'value' => '1',
'compare' => $args['cancelled'] ? '=' : '!='
),
array(
'key' => '_cancelled',
'value' => '1',
'compare' => $args['cancelled'] ? '=' : '!='
)
);
}
if(isset($args['event_online']) && !empty($args['event_online'])) {
if($args['event_online'] == 'true')
$event_online = 'yes';
elseif($args['event_online'] == 'false')
$event_online = 'no';
$query_args['meta_query'][] = array(
'key' => '_event_online',
'value' => $event_online,
'compare' => $args['event_online'] ? '=' : '!='
);
}
if(!empty($args['search_datetimes'][0])) {
$date_search = array();
$dates = json_decode($args['search_datetimes'][0], true);
// Get date and time setting defined in admin panel Event listing -> Settings -> Date & Time formatting
$datepicker_date_format = WP_Event_Manager_Date_Time::get_datepicker_format();
// Covert datepicker format into php date() function date format
$php_date_format = WP_Event_Manager_Date_Time::get_view_date_format_from_datepicker_date_format($datepicker_date_format);
if (!empty($dates)) {
$dates['start'] = WP_Event_Manager_Date_Time::date_parse_from_format($php_date_format, $dates['start']);
$dates['end'] = WP_Event_Manager_Date_Time::date_parse_from_format($php_date_format, $dates['end']);
$date_search['relation'] = 'AND';
$date_search[] = array(
'key' => '_event_start_date',
'value' => $dates['end'],
'compare' => '<=',
'type' => 'date'
);
$date_search[] = array(
'key' => '_event_start_date',
'value' => $dates['start'],
'compare' => '>=',
'type' => 'date'
);
apply_filters('event_manager_get_listings_date_filter_args', $date_search, $dates);
if(!empty( $date_search))
$query_args['meta_query'][] = $date_search;
}
}
if(!empty($args['search_categories'][0])) {
$field = is_numeric($args['search_categories'][0]) ? 'term_id' : 'slug';
$operator = 'all' === get_option('event_manager_category_filter_type', 'all') && sizeof($args['search_categories']) > 1 ? 'AND' : 'IN';
$query_args['tax_query'][] = array(
'taxonomy' => 'event_listing_category',
'field' => $field,
'terms' => array_values($args['search_categories']),
'include_children' => 'AND' !== $operator,
'operator' => $operator
);
}
if(!empty($args['search_event_types'][0])) {
$field = is_numeric($args['search_event_types'][0]) ? 'term_id' : 'slug';
$operator = 'all' === get_option('event_manager_event_type_filter_type', 'all') && sizeof($args['search_event_types']) > 1 ? 'AND' : 'IN';
$query_args['tax_query'][] = array(
'taxonomy' => 'event_listing_type',
'field' => $field,
'terms' => array_values($args['search_event_types']),
'include_children' => $operator !== 'AND' ,
'operator' => $operator
);
}
if(!empty($args['search_tags'][0])) {
$field = is_numeric($args['search_tags'][0]) ? 'term_id' : 'slug';
$operator = 'all' === get_option('event_manager_event_type_filter_type', 'all') && sizeof($args['search_tags']) > 1 ? 'AND' : 'IN';
$query_args['tax_query'][] = array(
'taxonomy' => 'event_listing_tag',
'field' => $field,
'terms' => array_values($args['search_tags']),
'include_children' => $operator !== 'AND' ,
'operator' => $operator
);
}
// Must match with event_ticket_options options value at wp-event-manager-form-submit-event.php
if(!empty($args['search_ticket_prices'][0])) {
$ticket_price_value='';
if($args['search_ticket_prices'][0]==='paid' || $args['search_ticket_prices'][0]==='ticket_price_paid') {
$ticket_price_value='paid';
} elseif($args['search_ticket_prices'][0]==='free' || $args['search_ticket_prices'][0]==='ticket_price_free') {
$ticket_price_value='free';
}
$ticket_search[] = array(
'key' => '_event_ticket_options',
'value' => $ticket_price_value,
'compare' => 'LIKE',
);
$query_args['meta_query'][] = $ticket_search;
}
if('featured' === $args['orderby']) {
$query_args['meta_query'] = array(
'relation' => 'AND',
'featured_clause' => array(
'key' => '_featured',
'compare' => 'EXISTS',
),
'event_start_date_clause' => array(
'key' => '_event_start_date',
'compare' => 'EXISTS',
),
'event_start_time_clause' => array(
'key' => '_event_start_time',
'compare' => 'EXISTS',
),
);
$query_args['orderby'] = array(
'featured_clause' => 'desc',
'event_start_date_clause' => $args['order'],
'event_start_time_clause' => $args['order'],
);
}
if('rand_featured' === $args['orderby']) {
$query_args['orderby'] = array(
'menu_order' => 'ASC',
'rand' => 'ASC',
);
}
// If orderby meta key _event_start_date
if('event_start_date' === $args['orderby']) {
$query_args['orderby'] ='meta_value';
$query_args['meta_key'] ='_event_start_date';
$query_args['meta_type'] ='DATETIME';
}
// If orderby event_start_date and time both
if('event_start_date_time' === $args['orderby']) {
$query_args['meta_query'] = array(
'relation' => 'AND',
'event_start_date_clause' => array(
'key' => '_event_start_date',
'compare' => 'EXISTS',
),
'event_start_time_clause' => array(
'key' => '_event_start_time',
'compare' => 'EXISTS',
),
);
$query_args['orderby'] = array(
'event_start_date_clause' => $args['order'],
'event_start_time_clause' => $args['order'],
);
}
$event_manager_keyword = esc_attr($args['search_keywords']);
if(!empty($event_manager_keyword) && strlen($event_manager_keyword) >= apply_filters('event_manager_get_listings_keyword_length_threshold', 2)) {
$query_args['s'] = $event_manager_keyword;
add_filter('posts_search', 'get_event_listings_keyword_search');
}
$query_args = apply_filters('event_manager_get_listings', $query_args, $args);
if(empty($query_args['meta_query'])) {
unset($query_args['meta_query']);
}
if(empty($query_args['tax_query'])) {
unset($query_args['tax_query']);
} else {
$query_args['meta_query']['tax_query'] = array($query_args['tax_query']);
$query_args['meta_query']['relation'] = 'AND';
// $query_args['tax_query']['relation'] = 'AND';
}
// Polylang LANG arg
if(function_exists('pll_current_language') && !empty($args['lang'])) {
$query_args['lang'] = $args['lang'];
}
// Filter args
$query_args = apply_filters('get_event_listings_query_args', $query_args, $args);
do_action('before_get_event_listings', $query_args, $args);
// Cache results.
if(apply_filters('get_event_listings_cache_results', true)) {
$to_hash = wp_json_encode($query_args) . apply_filters('wpml_current_language', '');
$query_args_hash = 'em_' . md5($to_hash . EVENT_MANAGER_VERSION) . WP_Event_Manager_Cache_Helper::get_transient_version('get_event_listings');
$result = false;
$cached_query_results = true;
$cached_query_posts = get_transient($query_args_hash);
if(is_string($cached_query_posts)) {
$cached_query_posts = json_decode($cached_query_posts, false);
if($cached_query_posts
&& is_object($cached_query_posts)
&& isset($cached_query_posts->max_num_pages)
&& isset($cached_query_posts->found_posts)
&& isset($cached_query_posts->posts)
&& is_array($cached_query_posts->posts)
) {
$posts = array_map('get_post', $cached_query_posts->posts);
$result = new WP_Query();
$result->parse_query($query_args);
$result->posts = $posts;
$result->found_posts = intval($cached_query_posts->found_posts);
$result->max_num_pages = intval($cached_query_posts->max_num_pages);
$result->post_count = count($posts);
}
}
if(false === $result) {
$result = new WP_Query($query_args);
$cached_query_results = false;
$cacheable_result = array();
$cacheable_result['posts'] = array_values($result->posts);
$cacheable_result['found_posts'] = $result->found_posts;
$cacheable_result['max_num_pages'] = $result->max_num_pages;
set_transient($query_args_hash, wp_json_encode($cacheable_result), DAY_IN_SECONDS);
}
if($cached_query_results) {
// Random order is cached so shuffle them.
if('rand_featured' === $args['orderby']) {
usort($result->posts, '_wpem_shuffle_featured_post_results_helper');
} elseif('rand' === $args['orderby']) {
shuffle($result->posts);
}
}
} else {
$result = new WP_Query($query_args);
}
$result = apply_filters('get_event_listings_result_args',$result,$query_args);
do_action('after_get_event_listings', $query_args, $args);
remove_filter('posts_search', 'get_event_listings_keyword_search');
return $result;
}
endif;
if(!function_exists('_wpem_shuffle_featured_post_results_helper')) :
/**
* Helper function to maintain featured status when shuffling results.
*
* @param WP_Post $a
* @param WP_Post $b
*
* @return bool
*/
function _wpem_shuffle_featured_post_results_helper($a, $b) {
if(-1 === $a->menu_order || -1 === $b->menu_order) {
// Left is featured.
if(0 === $b->menu_order) {
return -1;
}
// Right is featured.
if(0 === $a->menu_order) {
return 1;
}
}
return rand(-1, 1);
}
endif;
if(!function_exists('get_event_listings_keyword_search')) :
/**
* Join and where query for keywords
*
* @param array $search
* @return array
*/
function get_event_listings_keyword_search($search) {
global $wpdb, $event_manager_keyword;
// Searchable Meta Keys: set to empty to search all meta keys
$searchable_meta_keys = array(
'_event_location',
'_organizer_name',
'_event_tags',
'_event_address',
'_event_pincode',
'_event_location',
'_registration',
'_event_start_date',
'_event_start_time',
'_organizer_contact_person_name',
'_organizer_email',
'_organizer_website',
'_organizer_video',
'_organizer_youtube',
'_organizer_google_plus',
'_organizer_facebook',
'_organizer_linkedin',
'_organizer_twitter',
'_organizer_xing',
'_organizer_pinterest',
'_organizer_instagram',
);
$searchable_meta_keys = apply_filters('event_listing_searchable_meta_keys', $searchable_meta_keys);
$conditions = array();
// Search Post Meta
if(apply_filters('event_listing_search_post_meta', true)) {
// Only selected meta keys
if($searchable_meta_keys) {
$conditions[] = "{$wpdb->posts}.ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key IN ('" . implode("','", array_map('esc_sql', $searchable_meta_keys)) . "') AND meta_value LIKE '%" . esc_sql($event_manager_keyword) . "%')";
} else {
// No meta keys defined, search all post meta value
$conditions[] = "{$wpdb->posts}.ID IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%" . esc_sql($event_manager_keyword) . "%')";
}
}
// Search taxonomy
$conditions[] = "{$wpdb->posts}.ID IN (SELECT object_id FROM {$wpdb->term_relationships} AS tr LEFT JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id WHERE t.name LIKE '%" . esc_sql($event_manager_keyword) . "%')";
/**
* Filters the conditions to use when querying event listings. Resulting array is joined with OR statements.
*
* @since 1.5
*
* @param array $conditions Conditions to join by OR when querying event listings.
* @param string $event_manager_keyword Search query.
*/
$conditions = apply_filters('event_listing_search_conditions', $conditions, $event_manager_keyword);
if(empty($conditions)) {
return $search;
}
$conditions_str = implode(' OR ', $conditions);
if(!empty($search)) {
$search = preg_replace('/^ AND /', '', $search);
$search = " AND ({$search} OR ({$conditions_str}))";
} else {
$search = " AND ({$conditions_str})";
}
return $search;
}
endif;
if(!function_exists('get_event_listing_post_statuses')) :
/**
* Get post statuses used for events.
*
* @access public
* @return array
*/
function get_event_listing_post_statuses() {
return apply_filters('event_listing_post_statuses', array(
'draft' => _x('Draft', 'post status', 'wp-event-manager'),
'expired' => _x('Expired', 'post status', 'wp-event-manager'),
'preview' => _x('Preview', 'post status', 'wp-event-manager'),
'pending' => _x('Pending approval', 'post status', 'wp-event-manager'),
'pending_payment' => _x('Pending payment', 'post status', 'wp-event-manager'),
'publish' => _x('Active', 'post status', 'wp-event-manager'),
));
}
endif;
if(!function_exists('get_featured_event_ids')) :
/**
* Gets the ids of featured events.
*
* @access public
* @return array
*/
function get_featured_event_ids() {
return get_posts(array(
'posts_per_page' => -1,
'suppress_filters' => false,
'post_type' => 'event_listing',
'post_status' => 'publish',
'meta_key' => '_featured',
'meta_value' => '1',
'fields' => 'ids'
));
}
endif;
if(!function_exists('get_event_listing_types')) :
/**
* Get event listing types.
*
* @access public
* @return array
*/
function get_event_listing_types($fields = 'all') {
if(!get_option('event_manager_enable_event_types')){
return array();
}else{
$args = array(
'fields' => $fields,
'hide_empty' => false,
'order' => 'ASC',
'orderby' => 'name'
);
$args = apply_filters('get_event_listing_types_args', $args);
// Prevent users from filtering the taxonomy
$args['taxonomy'] = 'event_listing_type';
return get_terms($args);
}
}
endif;
if(!function_exists('get_event_listing_categories')) :
/**
* Get event categories.
*
* @access public
* @return array
*/
function get_event_listing_categories() {
if(!get_option('event_manager_enable_categories')) {
return array();
}
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
);
/**
* Change the category query arguments.
*
* @since 2.5
*
* @param array $args
*/
$args = apply_filters('get_event_listing_category_args', $args);
// Prevent users from filtering the taxonomy.
$args['taxonomy'] = 'event_listing_category';
return get_terms($args);
}
endif;
if(!function_exists('event_manager_get_filtered_links')) :
/**
* Shows links after filtering events.
*/
function event_manager_get_filtered_links($args = array()) {
$search_datetimes= array();
$search_categories = array();
$search_event_types= array();
$search_ticket_prices= array();
// Convert to slugs
if($args['search_categories']) {
foreach($args['search_categories'] as $category) {
if(is_numeric($category)) {
$category_object = get_term_by('id', $category, 'event_listing_category');
if(!is_wp_error($category_object)) {
$search_categories [] = $category_object->slug;
}
} else {
$search_categories [] = $category;
}
}
}
// Convert to slugs
if($args['search_event_types']) {
foreach($args['search_event_types'] as $type) {
if(is_numeric($type)) {
$type_object = get_term_by('id', $type, 'event_listing_type');
if(!is_wp_error($type_object)) {
$search_event_types[] = $type_object->slug;
}
} else {
$search_event_types[] = $type;
}
}
}
// Datetimes
// Add just key like datetime_any, datetime_today..
if($args['search_datetimes']) {
foreach($args['search_datetimes'] as $datetime) {
$search_datetimes[]=$datetime;
}
}
// Ticket price
// Add just key like ticket_price_any, ticket_price_paid..
if($args['search_ticket_prices']) {
foreach($args['search_ticket_prices'] as $ticket_price) {
$search_ticket_prices[]=$ticket_price;
}
}
$links = apply_filters('event_manager_event_filters_showing_events_links', array(
'reset' => array(
'name' => __('Reset', 'wp-event-manager'),
'url' => '#'
),
'rss_link' => array(
'name' => __('RSS', 'wp-event-manager'),
'url' => get_event_listing_rss_link(apply_filters('event_manager_get_listings_custom_filter_rss_args', array(
'search_keywords' => $args['search_keywords'],
'search_location' => $args['search_location'],
'search_datetimes' => implode(',', $search_datetimes),
'search_categories' => implode(',', $search_categories),
'search_event_types' => implode(',', $search_event_types),
'search_ticket_prices' => implode(',', $search_ticket_prices)
)))
)
), $args);
if(!$args['search_keywords'] && !$args['search_location'] && !$args['search_datetimes'] && !$args['search_categories'] && !$args['search_event_types'] && !$args['search_ticket_prices'] && !apply_filters('event_manager_get_listings_custom_filter', false)) {
unset($links['reset']);
}
$return = '';
$i = 1;
foreach($links as $key => $link) {
if($i > 1)
$return .= ' <a href="#">|</a> ';
$return .= '<a href="' . esc_url($link['url']) . '" class="' . esc_attr($key) . '">' . $link['name'] . '</a>';
$i++;
}
return $return;
}
endif;
if(!function_exists('get_event_listing_rss_link')) :
/**
* Get the Event Listing RSS link.
*
* @return string
*/
function get_event_listing_rss_link($args = array()) {
$rss_link = add_query_arg(urlencode_deep(array_merge(array('feed' => 'event_feed'), $args)), home_url());
return $rss_link;
}
endif;
if(!function_exists('wp_event_manager_notify_new_user')) :
/**
* Handle account creation.
*
* @param int $user_id
* @param string $password
*/
function wp_event_manager_notify_new_user($user_id, $password) {
global $wp_version;
if(version_compare($wp_version, '4.3.1', '<')) {
wp_new_user_notification($user_id, $password);
} else {
$notify = 'admin';
if(empty($password)) {
$notify = 'both';
}
wp_new_user_notification($user_id, null, $notify);
}
}
endif;
if(!function_exists('wp_event_manager_create_account')) :
/**
* Handle account creation.
*
* @param array $args containing username, email, role
* @param string $deprecated role string
* @return WP_error | bool was an account created?
*/
function wp_event_manager_create_account($args, $deprecated = '') {
global $current_user;
global $wp_version;
// Soft Deprecated in 1.0
if(!is_array($args)) {
$args = array(
'username' => '',
'password' => false,
'email' => $args,
'role' => $deprecated,
);
} else {
$defaults = array(
'username' => '',
'email' => '',
'password' => false,
'role' => get_option('default_role')
);
$args = wp_parse_args($args, $defaults);
extract($args);
}
$username = sanitize_user($args['username'], true);
$email = apply_filters('user_registration_email', sanitize_email($args['email']));
if(empty($email)) {
return new WP_Error('validation-error', __('Invalid email address.', 'wp-event-manager'));
}
if(empty($username)) {
$username = sanitize_user(current(explode('@', $email)));
}
if(!is_email($email)) {
return new WP_Error('validation-error', __('Your email address isn’t correct.', 'wp-event-manager'));
}
if(email_exists($email)) {
return new WP_Error('validation-error', __('This email is already registered, please choose another one.', 'wp-event-manager'));
}
// Ensure username is unique
$append = 1;
$o_username = $username;
while (username_exists($username)) {
$username = $o_username . $append;
$append ++;
}
// Final error checking
$reg_errors = new WP_Error();
$reg_errors = apply_filters('event_manager_registration_errors', $reg_errors, $username, $email);
do_action('event_manager_register_post', $username, $email, $reg_errors);
if($reg_errors->get_error_code()) {
return $reg_errors;
}
// Create account
$new_user = array(
'user_login' => $username,
'user_pass' => $password,
'user_email' => $email,
'role' => $role
);
// User is forced to set up account with email sent to them. This password will remain a secret.
if(empty($new_user['user_pass'])) {
$new_user['user_pass'] = wp_generate_password();
}
$user_id = wp_insert_user(apply_filters('event_manager_create_account_data', $new_user));
if(is_wp_error($user_id)) {
return $user_id;
}
// Notify
/**
* Send notification to new users.
*
* @since 1.8
*
* @param int $user_id
* @param string|bool $password
* @param array $new_user {
* Information about the new user.
*
* @type string $user_login Username for the user.
* @type string $user_pass Password for the user (may be blank).
* @type string $user_email Email for the new user account.
* @type string $role New user's role.
* }
*/
do_action('event_manager_notify_new_user', $user_id, $password, $new_user);
// Login
if(!is_user_logged_in()){
wp_set_auth_cookie($user_id, true, is_ssl());
$current_user = get_user_by('id', $user_id);
}
return true;
}
endif;
/**
* True if an the user can post a event. If accounts are required, and reg is enabled, users can post (they signup at the same time).
*
* @return bool
*/
function event_manager_user_can_post_event() {
$can_post = true;
if(!is_user_logged_in()) {
if(event_manager_user_requires_account() && !event_manager_enable_registration()) {
$can_post = false;
}
}
return apply_filters('event_manager_user_can_post_event', $can_post);
}
/**
* True if an the user can edit a event.
*
* @return bool
*/
function event_manager_user_can_edit_event($event_id) {
$can_edit = true;
if(!is_user_logged_in() || !$event_id) {
$can_edit = false;
} else {
$event = get_post($event_id);
if(!$event || (absint($event->post_author) !== get_current_user_id() && !current_user_can('edit_post', $event_id))) {
$can_edit = false;
}
}
return apply_filters('event_manager_user_can_edit_event', $can_edit, $event_id);
}
/**
* Checks if the visitor is currently on a WPEM page, event listing, or taxonomy.
*
* @return bool
* @since 2.5
*/
function is_wpem() {
/**
* Filter the result of is_wpem().
*/
return apply_filters('is_wpem', (is_wpem_page() || has_wpem_shortcode() || is_wpem_event_listing() || is_wpem_taxonomy()));
}
/**
* Checks if the visitor is currently on a WPEM page.
*
* @return bool
* @since 2.5
*/
function is_wpem_page() {
$is_wpem_page = is_post_type_archive('event_listing');
if(!$is_wpem_page) {
$wpem_page_ids = array_filter(
array(
get_option('event_manager_submit_event_form_page_id', false),
get_option('event_manager_event_dashboard_page_id', false),
get_option('event_manager_events_page_id', false),
)
);
/**
* Filters a list of all page IDs related to WPEM.
*/
$wpem_page_ids = array_unique(apply_filters('event_manager_page_ids', $wpem_page_ids));
$is_wpem_page = is_page($wpem_page_ids);
}
/**
* Filter the result of is_wpem_page().
*/
return apply_filters('is_wpem_page', $is_wpem_page);
}
/**
* Checks if the provided content or the current single page or post has a WPEM shortcode.
*
* @param string|null $content Content to check. If not provided, it uses the current post content.
* @param string|array|null $tag Check specifically for one or more shortcodes. If not provided, checks for any WPJM shortcode.
*
* @return bool
*/
function has_wpem_shortcode($content = null, $tag = null) {
global $post;
$has_wpem_shortcode = false;
if(null === $content && is_singular() && is_a($post, 'WP_Post')) {
$content = $post->post_content;
}
if(!empty($content)) {
$wpem_shortcodes = array('submit_event_form', 'event_dashboard', 'events', 'event', 'event_summary', 'event_apply');
/**
* Filters a list of all shortcodes associated with WPEM.
*
* @since 2.5
*
* @param string[] $wpem_shortcodes
*/
$wpem_shortcodes = array_unique(apply_filters('event_manager_shortcodes', $wpem_shortcodes));
if(null !== $tag) {
if(!is_array($tag)) {
$tag = array($tag);
}
$wpem_shortcodes = array_intersect($wpem_shortcodes, $tag);
}
foreach($wpem_shortcodes as $shortcode) {
if(has_shortcode($content, $shortcode)) {
$has_wpem_shortcode = true;
break;
}
}
}
/**
* Filter the result of has_wpem_shortcode().
*
* @since 2.5
*
* @param bool $has_wpem_shortcode
*/
return apply_filters('has_wpem_shortcode', $has_wpem_shortcode);
}
/**
* Checks if the current page is a event listing.
*
* @since 2.5
*
* @return bool
*/
function is_wpem_event_listing() {
return is_singular(array('event_listing'));
}
/**
* Checks if the visitor is on a page for a WPEM taxonomy.
*
* @since 2.5
*
* @return bool
*/
function is_wpem_taxonomy() {
return is_tax(get_object_taxonomies('event_listing'));
}
/**
* True if only one type allowed per event.
*
* @return bool
*/
function event_manager_multiselect_event_type() {
if(!class_exists('WP_Event_Manager_Form_Submit_Event')) {
include_once(EVENT_MANAGER_PLUGIN_DIR . '/forms/wp-event-manager-form-abstract.php');
include_once(EVENT_MANAGER_PLUGIN_DIR . '/forms/wp-event-manager-form-submit-event.php');
}
$form_submit_event_instance = call_user_func(array('WP_Event_Manager_Form_Submit_Event', 'instance'));
$event_fields = $form_submit_event_instance->merge_with_custom_fields();
if(isset($event_fields['event']['event_type']['type']) && $event_fields['event']['event_type']['type'] === 'term-multiselect') {
return apply_filters('event_manager_multiselect_event_type', true);
} else {
return apply_filters('event_manager_multiselect_event_type', false);
}
}
/**
* True if only one category allowed per event.
*
* @return bool
*/
function event_manager_multiselect_event_category() {
if(!class_exists('WP_Event_Manager_Form_Submit_Event')) {
include_once(EVENT_MANAGER_PLUGIN_DIR . '/forms/wp-event-manager-form-abstract.php');
include_once(EVENT_MANAGER_PLUGIN_DIR . '/forms/wp-event-manager-form-submit-event.php');
}
$form_submit_event_instance = call_user_func(array('WP_Event_Manager_Form_Submit_Event', 'instance'));
$event_fields = $form_submit_event_instance->merge_with_custom_fields();
if(isset($event_fields['event']['event_category']['type']) && $event_fields['event']['event_category']['type'] === 'term-multiselect') {
return apply_filters('event_manager_multiselect_event_category', true);
} else {
return apply_filters('event_manager_multiselect_event_category', false);
}
}
/**
* True if registration is enabled.
*
* @return bool
*/
function event_manager_enable_registration() {
return apply_filters('event_manager_enable_registration', get_option('event_manager_enable_registration') == 1 ? true : false);
}
/**
* True if usernames are generated from email addresses.
*
* @return bool
*/
function event_manager_generate_username_from_email() {
return apply_filters('event_manager_generate_username_from_email', get_option('event_manager_generate_username_from_email') == 1 ? true : false);
}
/**
* True if an account is required to post a event.
*
* @return bool
*/