-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmfo-base.php
1783 lines (1389 loc) · 60.5 KB
/
mfo-base.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
/*
Plugin Name: Maker Faire Online - CFM & More
Plugin URI: http://www.github.com/digitalman2112/mfo-wordpress-plugin
Description: Helper plugin for the Maker Faire Online system based using the Toolset plugins & more
Version: 3.24.4
Author: Ian Cole (Maker Faire Orlando)
Author URI: http://www.github.com/digitalman2112
GitHub Plugin URI: makerfaireorlando/mfo-wordpress-plugin
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
//include settings code
include( plugin_dir_path( __FILE__ ) . 'mfo-settings.php');
include( plugin_dir_path( __FILE__ ) . 'mfo-cleanup.php');
//disables the admin bar on the frontend
add_filter('show_admin_bar', '__return_false');
add_action( 'wp_enqueue_scripts', 'add_custom_scripts' );
// add a link to the WP Toolbar
function custom_toolbar_link($wp_admin_bar) {
$args = array(
'id' => 'mfooptions',
'title' => 'MFO Options',
'href' => '/wp-admin/options-general.php?page=mfo-options-page&tab=main_options',
'meta' => array(
'class' => 'mfooptions',
'title' => 'MFO Options'
)
);
$wp_admin_bar->add_node($args);
// Add the child links
$args = array(
'id' => 'mfomainoptions',
'title' => 'Main Options',
'href' => '/wp-admin/options-general.php?page=mfo-options-page&tab=main_options',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfomainoptions',
'title' => 'Main Options'
)
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'mfodisplayoptions',
'title' => 'Display Options',
'href' => '/wp-admin/options-general.php?page=mfo-options-page&tab=display_options',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfodisplayoptions',
'title' => 'Display Options'
)
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'mfofeatureoptions',
'title' => 'Feature Options',
'href' => '/wp-admin/options-general.php?page=mfo-options-page&tab=feature_options',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfomainsettings',
'title' => 'Feature Options'
)
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'mfomoduleoptions',
'title' => 'Module Options',
'href' => '/wp-admin/options-general.php?page=mfo-options-page&tab=module_options',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfomodulesettings',
'title' => 'Module Options'
)
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'mfodebugoptions',
'title' => 'Debug Options',
'href' => '/wp-admin/options-general.php?page=mfo-options-page&tab=debug_options',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfodebugsettings',
'title' => 'Debug Options'
)
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'mfoproducerdash',
'title' => 'Producer Dashboard',
'href' => '/producer-dashboard',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfoproducersdash',
'title' => 'Producer Dashboard'
)
);
$wp_admin_bar->add_node($args);
$args = array(
'id' => 'mfomakerdash',
'title' => 'Maker Dashboard',
'href' => '/maker-dashboard',
'parent' => 'mfooptions',
'meta' => array(
'class' => 'mfomakerdash',
'title' => 'Maker Dashboard'
)
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 999);
mfo_load_modules();
function mfo_load_modules () {
$options = get_option('mfo_options_modules');
if ( $options['mfo_module_eventbrite_enabled_boolean'] ) {
mfo_log (4, "load modules", "loading mfo-eventbrite-webhook module");
include( plugin_dir_path( __FILE__ ) . 'mfo-eventbrite-webhook.php');
mfo_eventbrite_init();
}
if ( $options['mfo_module_sensei_enabled_boolean'] ) {
mfo_log (4, "load modules", "loading mfo-sensei-helpers module");
include( plugin_dir_path( __FILE__ ) . 'mfo-sensei-helpers.php');
//mfo_sensei_compatibility();
}
if ( $options['mfo_module_woocommerce_enabled_boolean'] ) {
mfo_log (4, "load modules", "loading mfo-woocommerce-helpers module");
include( plugin_dir_path( __FILE__ ) . 'mfo-woocommerce-helpers.php');
}
if ( $options['mfo_slack_enabled_boolean'] ) {
include( plugin_dir_path( __FILE__ ) . 'mfo-slack.php');
mfo_log (4, "load modules", "loading mfo-slack module");
}
}
add_filter('single_template', 'mfo_single_template');
function mfo_single_template($single) {
global $wp_query, $post;
/* Checks for single template by post type */
if ($post->post_type == "maker"){
if(file_exists( dirname( __FILE__ ) . '/templates/single-maker.php'))
return dirname( __FILE__ ) . '/templates/single-maker.php';
}
elseif ($post->post_type == "exhibit"){
if(file_exists( dirname( __FILE__ ) . '/templates/single-exhibit.php'))
return dirname( __FILE__ ) . '/templates/single-exhibit.php';
}
return $single;
}
add_filter( 'page_template', 'mfo_page_template' );
function mfo_page_template( $page_template )
{
/* YOU still have to have a wordpress page, this will just auto-apply the template!" */
$pagename = get_query_var('pagename');
//mfo_log(4, "mfo_page_template", "pre: page_template=" . $page_template);
mfo_log(4, "mfo_page_template", "pre: pagename=" . $pagename);
$templates = array (
array('csv-exhibit-checkin', 'csv-exhibit-checkin.php'),
array('csv-helper-checkin', 'csv-helper-checkin.php'),
array('csv-maker-media-export', 'csv-maker-media-export.php'),
array('producer-exhibit-search-csv-results', 'csv-results.php'),
array('producer-maker-search-csv-results', 'csv-results.php'),
array('csv-social', 'csv-social.php'),
array('duplicate-exhibit', 'duplicate-exhibit.php'),
array('maker-dashboard', 'maker-dashboard.php'),
array('exhibit-table-signs', 'exhibit-table-signs.php'),
array('exhibit-space-planning-sheets', 'exhibit-space-planning-sheets.php'),
array('events-json', 'json-eventlist.php'),
array('makers-json', 'json-makerlist.php'),
array('makers-json2', 'json-makerlist2.php'),
array('jekyll-build', 'jekyll-build.php'),
array('slack-find-exhibit', 'json-slack-maker.php'),
array('select-load-in-date-time','load-in.php'),
array('producer-loadin-report', 'loadin-report.php'),
array('makers', 'exhibits-isotope.php'),
array('much-makers', 'stat.php')
);
foreach ($templates as $t) {
if ($t[0] == $pagename) {
$page_template = dirname( __FILE__ ) . '/templates/' . $t[1];
break;
}
}
//deprecated?
//mfo_load_page_template('event-talks', 'json-eventlist.php');
//mfo_load_page_template('fix-events', 'fix-event-locations.php');
mfo_log(4, "mfo_page_template", "post: page_template=" . $page_template);
return $page_template;
}
// custom exhibit permalinks with year
// NOTE THIS REQUIRES EXHIBIT CUSTOM POST TYPE MOD
// "Use a custom URL format" -> exhibit/%year%
// modify "AllTypes Row" content template to use [wpv-post-type show="single"]
// still have 404 on fireball :(
add_filter('post_link', 'year_permalink', 10, 3);
add_filter('post_type_link', 'year_permalink', 10, 3);
function year_permalink($permalink, $post, $leavename) {
if (strpos($permalink, '%year%') === FALSE) return $permalink;
mfo_log(4, "year_permalink", $permalink . "; " . $post->ID);
$appr_year = get_post_meta($post->ID, 'wpcf-approval-year', TRUE);
return str_replace('%year%', $appr_year, $permalink);
}
//add query string variables
function add_query_vars_filter($vars) {
$vars[] = "li-exhibit";
$vars[] = "dup-exhibit";
$vars[] = "post_ids";
$vars[] = "cred-edit-form";
$vars[] = "id";
//exhibits page
$vars[] = "category";
//vars for slash commands
$vars[] = "token";
$vars[] = "text";
$vars[] = "channel_name";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter');
//add_filter( 'query_vars', 'add_query_vars_filter');
add_shortcode('mfo-loadin-short', 'mfo_loadin_short');
function mfo_loadin_short($atts) {
$exhibit = $atts["id"];
//get the loadin slot for the exhibit
$exhibit_slot_title = get_post_meta( $exhibit, 'wpcf-exhibit-loadin-slot', true );
//since we store the slot title, get the slot object from that title
$exhibit_slot = get_page_by_title($exhibit_slot_title, OBJECT, 'loadin-slot');
if ($exhibit_slot) {
$exhibit_slot_time = get_post_meta( $exhibit_slot->ID, 'wpcf-loadin-start-time', true );
$exhibit_slot_dateunix = get_post_meta( $exhibit_slot->ID, 'wpcf-loadin-date', true );
$exhibit_slot_date = date("D, M j, Y", $exhibit_slot_dateunix);
$exhibit_loc = get_post_meta( $exhibit_slot->ID, '_wpcf_belongs_loadin-location_id', true );
$exhibit_slot_loc = get_the_title($exhibit_loc);
$txt = $exhibit_slot_loc.' - '.$exhibit_slot_date.' at '.$exhibit_slot_time;
}
return $txt;
}
//workaround for wordpress 4.2.3 shortcode in html attribute issue
add_shortcode('mfo-exhibit-category-buttons', 'mfo_exhibit_category_buttons');
function mfo_exhibit_category_buttons($atts) {
$class = $atts["class"];
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'exhibit-category'
);
$categories = get_categories($args);
foreach($categories as $category) {
$output = $output.'<button id="'.$category->slug.'" class="'. $class. '" data-filter=".'.$category->slug.'" data-text="'.$category->name.'">'.$category->name.'</button>';
}
return $output;
//return "<script>window.location.replace('/edit-helpers/?post_ids=".$id."');</script>";
//return "<button class='myButton' data-filter='.".$id."' data-text='Arduino'>Arduino</button>";
}
/**
* Unlimited Search Posts
* From: http://jamescollings.co.uk/blog/wordpress-search-results-page-modifications/
*/
function jc_limit_search_posts() {
if ( is_search())
set_query_var('posts_per_page', -1);
}
add_filter('pre_get_posts', 'jc_limit_search_posts');
/*
//Don't paginate wordpress archives search results
//from https://wp-types.com/forums/topic/remove-pagination-from-archive-view/
function no_nopaging($query) {
if (is_post_type_archive()) {
$query->set('nopaging', 1);
}
}
add_action('parse_query', 'no_nopaging');
*/
/**
* Join posts and postmeta tables
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
* from: http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/
*/
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join' );
/**
* Modify the search query with posts_where
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
*/
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
}
return $where;
}
add_filter( 'posts_where', 'cf_search_where' );
/**
* Prevent duplicates
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
*/
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
/*
function my_wpv_post_feature_image ($info) {
//this isnt going to do what I wanted...
}
add_filter('wpv-post-featured-image', 'my_wpv_post_feature_image');
*/
add_shortcode('mfo-redirect-exhibit-helpers', 'mfo_redirect_exhibit_helpers_shortcode');
function mfo_redirect_exhibit_helpers_shortcode($atts) {
$id = $atts["id"];
return "<script>window.location.replace('/edit-helpers/?post_ids=".$id."');</script>";
}
add_shortcode('mfo-redirect-home', 'mfo_redirect_home_shortcode');
function mfo_redirect_home_shortcode() {
$html = "<script>window.location.replace('" . get_site_url() . "');</script>";
//return "<script>window.location.replace('http://www.makerfaireorlando.com');</script>";
return $html;
}
add_shortcode('wpv-post-today', 'today_shortcode');
function today_shortcode() {
return date("m/d/Y");
}
add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );
//from - https://wp-types.com/forums/topic/how-can-my-view-access-a-url-parameter-in-a-query-string/
function wpv_post_param_shortcode( $atts ) {
if ( !empty( $atts['var'] ) ) {
$var = (array)$_GET[$atts['var']];
return esc_html( implode( ', ', $var ) );
}
}
add_filter( 'no_texturize_shortcodes', 'shortcodes_to_exempt_from_wptexturize' );
function shortcodes_to_exempt_from_wptexturize( $shortcodes ) {
$shortcodes[] = 'trim';
return $shortcodes;
}
function taxonomy_level($atts) {
$a = shortcode_atts( array(
'id' => 0,
'type' => 'category',
), $atts );
if ($a['id']==0) return -1;
$ancestors = get_ancestors($a['id'], 'exhibit-location');
return sizeof($ancestors);
}
add_shortcode('taxonomy-level', 'taxonomy_level');
function current_user_can_edit_post() {
//todo: test for $atts existing to prevent warnings
//$id = $atts["id"];
//if (!$id) {
$id=get_the_ID();
//}
$cap = false;
$cap = current_user_can('edit_post',$id);
//echo $id;
//echo " - ";
//echo get_current_user_id();
//echo " - ";
//echo $cap ? 'true' : 'false';
//echo '<br>';
return $cap;
}
add_shortcode('current-user-can-edit-post', 'current_user_can_edit_post');
function current_user_can_edit_others_posts() {
return current_user_can('edit_others_posts');
}
add_shortcode('current-user-can-edit-others-posts', 'current_user_can_edit_others_posts');
function cred_post_parent_title() {
return do_shortcode("[cred-post-parent get='title']");
}
add_shortcode('cred-post-parent-title', 'cred_post_parent_title');
//from https://wp-types.com/forums/topic/testing-user-role-to-conditionally-display-content/
function get_wp_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
add_shortcode('wpv-post-get-wp-user-role', 'get_wp_user_role');
/**
*
*These are mostly for the CSV hacking. TODO: Convert the .csv export to proper php pages
* so that these aren't needed :)
*
**/
function mfo_hide( $atts, $content = null ) {
return;
}
add_shortcode( 'mfo-hide', 'mfo_hide' );
function decode_shortcode( $atts, $content = null ) {
$decoded = html_entity_decode(do_shortcode($content));
return $decoded;
}
add_shortcode( 'decode', 'decode_shortcode' );
function striptags_shortcode( $atts, $content = null ) {
$stripped = strip_tags(do_shortcode($content));
return $stripped;
}
add_shortcode( 'striptags', 'striptags_shortcode' );
function stripcrlf_shortcode( $atts, $content = null ) {
$stripped = str_replace (array("\r\n", "\n", "\r"), ' ', do_shortcode($content));
return $stripped;
}
add_shortcode( 'stripcrlf', 'stripcrlf_shortcode' );
function trim_shortcode( $atts, $content = null ) {
$trimmed = trim(html_entity_decode(do_shortcode($content)));
return $trimmed;
}
add_shortcode( 'trim', 'trim_shortcode' );
function crlf_shortcode( $atts, $content = null ) {
return "\r\n";
}
add_shortcode( 'crlf', 'crlf_shortcode' );
//allows the custom parameter for the csv export template
function custom_rewrite_tag() {
//https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
add_rewrite_tag('%csv-filename%', '([^&]+)');
add_rewrite_tag('%csv-year%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
// adding shortcode to get parent id for wp-types
function parent_id($atts) {
global $wpdb;
$current = $atts["id"];
// Get current posts parent type
$parentType = $wpdb->get_var("SELECT `meta_key` FROM `wp_postmeta` WHERE `post_id` ={$current} AND `meta_key` LIKE '%belongs%'");
// Get current posts parentID if project
$parentID = $wpdb->get_var("SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` ={$current} AND `meta_key` = '{$parentType}'" );
return trim($parentID);
} //end function parent_id
add_shortcode("parentid", "parent_id");
//this is a hack and can likely be removed.
//check the exhibit-helpers view
function helper_approved_quantity($atts) {
global $exhibit;
// $scid = do_shortcode("[wpv-post-id id='$exhibit']");
$current = $atts["id"];
$haq = get_post_meta($current, "wpcf-helper-approved-quantity", true);
return trim($exhibit->ID);
}
add_shortcode("helper-approved-quantity", "helper_approved_quantity");
/*
* Logging Functionality - enabled with setting
* Writes to mfo-debug.log in mfo plugin directory
* Watch the log file size, can get big!
*
*/
function mfo_log($lvl, $header, $msg){
$options = get_option('mfo_options_debug');
$enabled = $options['mfo_log_enabled_boolean'];
$log_level = $options['mfo_log_level_number'];
$tz = 'America/New_York';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
$datetxt = $dt->format('Y-m-d H:i:s');
if ($enabled && ($lvl <= $log_level)) {
$logfile = plugin_dir_path(__FILE__) . 'mfo-debug.log';
file_put_contents($logfile, $datetxt. " | L" . $lvl . " | " . str_pad($header, 25) . " | " .$msg."\n",FILE_APPEND);
}
}
function mfo_warning_email ($subject, $body) {
$options = get_option('mfo_options_debug');
mfo_log (1, "WARNING EMAIL", $subject . " | " . $body);
wp_mail( $options['mfo_warning_email_string'], $subject, $body);
if (function_exists('mfo_post_to_slack')) {
mfo_post_to_slack($subject, 'mfo-wp-debug', 'tacocat', ':taco:');
}
}
function mfo_send_notification_email ($subject, $body) {
$options = get_option('mfo_options_main');
mfo_log (1, "mfo_notification_email", $subject . " | " . $body);
wp_mail( $options['mfo_notification_email_string'], $subject, $body);
$attach = array(array( "text" => $body));
if (function_exists('mfo_post_to_slack')) {
mfo_post_to_slack($subject, 'system-notifications', 'makey', ':makey:', $body);
}
}
//Add all user types to author dropdown for switching maker to another user
//from: http://wordpress.stackexchange.com/questions/50827/select-subscriber-as-author-of-post-in-admin-panel
add_filter('wp_dropdown_users', 'MySwitchUser');
function MySwitchUser($output)
{
//without this line, any admin edits will default the item to ADMIN
$post = get_post();
//global $post is available here, hence you can check for the post type here
//$users = get_users('role=subscriber');
$users = get_users(); //add all users
$output = "<select id=\"post_author_override\" name=\"post_author_override\" class=\"\">";
//Leave the admin in the list
$output .= "<option value=\"1\">Admin</option>";
foreach($users as $user)
{
$sel = ($post->post_author == $user->ID)?"selected='selected'":'';
//$output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login.'</option>';
$output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login." - ".$user->display_name.'</option>';
}
$output .= "</select>";
return $output;
}
//registration functions from: http://pastebin.com/pw4rDhTP
// Register the column - Registered
function registerdate($columns) {
$columns['registerdate'] = __('Registered', 'registerdate');
return $columns;
}
add_filter('manage_users_columns', 'registerdate');
// Display the column content
function registerdate_columns( $value, $column_name, $user_id ) {
if ( 'registerdate' != $column_name )
return $value;
$user = get_userdata( $user_id );
$registerdate = $user->user_registered;
$registerdate = date("Y-m-d", strtotime($registerdate));
return $registerdate;
}
add_action('manage_users_custom_column', 'registerdate_columns', 10, 3);
function registerdate_column_sortable($columns) {
$custom = array(
// meta column id => sortby value used in query
'registerdate' => 'registered',
);
return wp_parse_args($custom, $columns);
}
add_filter( 'manage_users_sortable_columns', 'registerdate_column_sortable' );
function registerdate_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'registerdate' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => 'registerdate',
'orderby' => 'meta_value'
) );
}
return $vars;
}
add_filter( 'request', 'registerdate_column_orderby' );
//Add Maker counts to USER admin panel
function users_makers_column( $cols ) {
$cols['user_makers'] = 'Makers';
return $cols;
}
add_filter( 'manage_users_columns', 'users_makers_column' );
//Add Exhibit counts to USER admin panel
function users_exhibits_column( $cols ) {
$cols['user_exhibits'] = 'Exhibits';
return $cols;
}
add_filter( 'manage_users_columns', 'users_exhibits_column' );
/*
//make column sortable
//I don't think this works since the underlying content is computed
//versus a field with that name
//I'd need to add stats fields to the user object and update like
// maker stats...don't know that it is worth the effort yet.
function users_sortable_columns($columns) {
$columns['user_makers'] = 'user_makers';
$columns['user_exhibits'] = 'user_exhibits';
return $columns;
}
add_filter('manage_users_sortable_columns', 'users_sortable_columns');
*/
//Custom USER admin panel columns
function user_custom_column_value( $value, $column_name, $id ) {
if ( ('user_makers' != $column_name) AND ('user_exhibits' != $column_name) )
return $value;
$count = 0;
if( $column_name == 'user_makers' ) {
global $wpdb;
$count = (int) $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(ID) FROM $wpdb->posts WHERE
post_type = 'maker' AND post_status = 'publish' AND post_author = %d",
$id
) );
} else if( $column_name == 'user_exhibits' ) {
global $wpdb;
$count = (int) $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(ID) FROM $wpdb->posts WHERE
post_type = 'exhibit' AND post_status = 'publish' AND post_author = %d",
$id
) );
}
// echo ($id.":".$column_name.":".$count."<br>");
return intval($count);
}
add_filter( 'manage_users_custom_column', 'user_custom_column_value', 10, 3 );
//Todo: Generic count exhibits, etc shortcode / function with parameters
function save_post_fee_payment($post_id) {
//$rsvp = get_post_meta($post_id, 'wpcf-orientation-session-rsvp', true);
$parent = wpcf_pr_post_get_belongs($post_id, 'exhibit');
$status = get_post_status($post_id);
mfo_log(2, "save_post_fee_payment", $post_id."; ".$parent."; ".$status);
if ($status == 'publish') {
update_post_meta($parent , 'wpcf-payment-status' , 3); //3=paid
mfo_log(2, "save_post_fee_payment", "updating parent parent-status ".$post_id."; ".$parent."; ".$status);
}
elseif ($status == 'auto-draft') {
//do nothing for now
//don't need to be alerted about these
}
else {
mfo_warning_email( "ERROR: Fee-Payment status not = publish or auto-draft: ".$post_id.": ".$parent.": ".$status, "May need to edit the exhibit payment status on this exhibit" );
}
}
add_action ('save_post_fee-payment', 'save_post_fee_payment');
function count_orientation_rsvp_shortcode( $atts ) {
$count = -1;
$id = $atts["id"];
if ( $id < 1 or $id >6) {
return "";
}
$childargs = array(
'post_type' => 'maker',
'numberposts' => -1,
'post_status' => 'publish',
'meta_query' => array(
//'relation' => 'and',
array('key' => 'wpcf-orientation-session-rsvp', 'value' => $id),
//array('key' => 'wpcf-has-approved-children', 'value' => '1')
));
$children = get_posts($childargs);
$count = count($children);
return $count;
}
add_shortcode( 'count-orientation-rsvp', 'count_orientation_rsvp_shortcode' );
function count_exhibits_shortcode( $atts, $content = null ) {
$count = -1;
$childargs = array(
'post_type' => 'exhibit',
'numberposts' => -1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'wpcf-approval-year', 'value' => mfo_event_year()))
);
$children = get_posts($childargs);
$count = count($children);
return $count;
}
add_shortcode( 'count-exhibits', 'count_exhibits_shortcode' );
function count_exhibits_pending_shortcode( $atts, $content = null ) {
$childargs = array(
'post_type' => 'exhibit',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array('key' => 'wpcf-approval-year', 'value' => mfo_event_year()) ,
array('key' => 'wpcf-approval-status', 'value' => '2'),
)
);
$children = new WP_Query($childargs);
return $children->post_count;
}
add_shortcode( 'count-exhibits-pending', 'count_exhibits_pending_shortcode' );
function count_exhibits_approved_shortcode( $atts, $content = null ) {
$childargs = array(
'post_type' => 'exhibit',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array('key' => 'wpcf-approval-year', 'value' => mfo_event_year()) ,
array('key' => 'wpcf-approval-status', 'value' => '1'),
)
);
$children = new WP_Query($childargs);
return $children->post_count;
}
add_shortcode( 'count-exhibits-approved', 'count_exhibits_approved_shortcode' );
function count_exhibits_rejected_shortcode( $atts, $content = null ) {
$childargs = array(
'post_type' => 'exhibit',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array('key' => 'wpcf-approval-year', 'value' => mfo_event_year()) ,
array('key' => 'wpcf-approval-status', 'value' => '3'),
)
);
$children = new WP_Query($childargs);
return $children->post_count; }
add_shortcode( 'count-exhibits-rejected', 'count_exhibits_rejected_shortcode' );
function count_exhibits_withdrawn_shortcode( $atts, $content = null ) {
$childargs = array(
'post_type' => 'exhibit',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array('key' => 'wpcf-approval-year', 'value' => mfo_event_year()) ,
array('key' => 'wpcf-approval-status', 'value' => '4'),
)
);
$children = new WP_Query($childargs);
return $children->post_count; }
add_shortcode( 'count-exhibits-withdrawn', 'count_exhibits_withdrawn_shortcode' );
function update_user_stats ($author_id) {
$debug="";
$debug = $debug.current_time('mysql')."\r\n";
$author_name = get_the_author_meta('display_name', $author_id);
$debug = $debug."User: ".$author_id.": ".$author_name."\r\n";
$childargs = array(
'author' => $author_id,
'post_type' => 'maker',
'numberposts' => -1,
'post_status' => 'publish',
);
$children = get_posts($childargs);
//update number-of-children
$number = count($children);
update_user_meta($author_id, 'wpcf-number-of-makers', $number);
//update has-children
if ($number > 0) {
update_user_meta($author_id, 'wpcf-has-makers', 1);
} else {
update_user_meta($author_id, 'wpcf-has-makers', 0);
}
//update has-approved-children and number-approved-children
$ham = 0; //start false
$nam = 0;
foreach ($children as $child) {
$debug = $debug." + ".$child->ID;
$debug = $debug.": ".$child->post_title;
$appr_status = get_post_meta($child->ID, 'wpcf-has-approved-children', TRUE);
$debug = $debug.", appr_status=".print_r($appr_status, TRUE);
$post_status = get_post_status($child->ID);
$debug = $debug.", post_status=".print_r($post_status, TRUE).";\r\n";
//$debug = $debug.types_render_field("approval-status", array());
if ($appr_status==1) {
$ham = 1;
$nam = $nam+1;
}
}
update_user_meta($author_id, 'wpcf-has-approved-makers', $ham);
update_user_meta($author_id, 'wpcf-number-approved-makers', $nam);
update_user_meta($author_id, 'wpcf-user-stats-debug', $debug);
}
function update_maker_stats_child_id ($post_id) {
$post_type = get_post_type($post_id);
if ($post_type == 'exhibit') {
$title = get_post_field( 'post_title', $post_id);
$parent = wpcf_pr_post_get_belongs($post_id, 'maker');
//mfo_warning_email( "update_maker_stats_child_id ".$title, $post_id.":".$parent);
if ($parent) {
update_maker_stats($parent);
}
}
}
add_action ('untrashed_post', 'update_maker_stats_child_id', 10, 1);
add_action ('trashed_post', 'update_maker_stats_child_id', 10, 1);
function update_maker_stats ($post_id) {
/*
SPECIAL NOTE:
- these will not fire on the admin screens if there is a child post type table showing
- go to the CPT for the PARENT and turn off the "management fields" for the child type
- so that they don't show.
*/
$debug = ""; //ensure it is declared
$post_type = get_post_type($post_id);
$title = get_post_field( 'post_title', $post_id);
mfo_log(2, "update_maker_stats", $post_id.":".$post_type.":".$title);
if ($post_type == 'maker') {
flush_json_from_cache();
//wp_mail( "[email protected]", "update_maker_stats for ".$title, $title);
$debug = $debug.current_time('mysql')."\r\n";
$debug = $debug."Maker: ".$post_id.": ".$title."\r\n";
$author_id = get_post_field( 'post_author', $post_id );