-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc-post-types.php
1813 lines (1555 loc) · 62.3 KB
/
wc-post-types.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: WordCamp.org Post Types
* Plugin Description: Sessions, Speakers, Sponsors and much more.
*/
require 'inc/back-compat.php';
require_once 'inc/favorite-schedule-shortcode.php';
require_once 'inc/privacy.php';
require_once 'inc/deprecated.php';
// Bitwise mask for the sessions CPT, to add endpoints to the session pages. This should be a unique power of 2
// greater than the core-defined ep_masks, but could potentially conflict with another plugin.
// See https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/.
define( 'EP_SESSIONS', 16384 );
class WordCamp_Post_Types_Plugin {
protected $wcpt_permalinks;
const SESSION_DEFAULT_DURATION = 50 * MINUTE_IN_SECONDS;
/**
* Fired when plugin file is loaded.
*/
public function __construct() {
$this->wcpt_permalinks = array();
add_action( 'init', array( $this, 'register_post_types' ) );
add_action( 'init', array( $this, 'register_taxonomies' ) );
add_action( 'init', array( $this, 'init' ) );
add_action( 'after_theme_setup', array( $this, 'add_image_sizes' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_print_styles', array( $this, 'admin_css' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
add_action( 'save_post', array( $this, 'save_post_session' ), 10, 2 );
add_action( 'save_post', array( $this, 'save_post_sponsor' ), 10, 2);
add_filter( 'updated_post_meta', array( $this, 'update_wcorg_user_id' ), 10, 4 );
add_filter( 'added_post_meta', array( $this, 'update_wcorg_user_id' ), 10, 4 );
add_filter( 'deleted_post_meta', array( $this, 'delete_wcorg_user_id' ), 10, 3 );
add_filter( 'manage_wcb_speaker_posts_columns', array( $this, 'manage_post_types_columns' ) );
add_filter( 'manage_wcb_session_posts_columns', array( $this, 'manage_post_types_columns' ) );
add_filter( 'manage_wcb_sponsor_posts_columns', array( $this, 'manage_post_types_columns' ) );
add_filter( 'manage_wcb_organizer_posts_columns', array( $this, 'manage_post_types_columns' ) );
add_filter( 'manage_edit-wcb_session_sortable_columns', array( $this, 'manage_sortable_columns' ) );
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
add_action( 'manage_posts_custom_column', array( $this, 'manage_post_types_columns_output' ), 10, 2 );
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_shortcode( 'schedule', array( $this, 'shortcode_schedule' ) );
add_filter( 'body_class', array( $this, 'session_category_slugs_to_body_tag' ) );
add_filter( 'the_content', array( $this, 'add_avatar_to_speaker_posts' ) );
add_filter( 'the_content', array( $this, 'add_speaker_info_to_session_posts' ) );
add_filter( 'the_content', array( $this, 'add_slides_info_to_session_posts' ) );
add_filter( 'the_content', array( $this, 'add_video_info_to_session_posts' ) );
add_filter( 'the_content', array( $this, 'add_session_categories_to_session_posts' ) );
add_filter( 'the_content', array( $this, 'add_session_info_to_speaker_posts' ) );
add_filter( 'dashboard_glance_items', array( $this, 'glance_items' ) );
add_filter( 'option_default_comment_status', array( $this, 'default_comment_ping_status' ) );
add_filter( 'option_default_ping_status', array( $this, 'default_comment_ping_status' ) );
add_action( 'init', array( $this, 'rest_init' ), 9 );
}
/**
* Run and setup hooks for wc_post_types.
*/
public function init() {
do_action( 'wcpt_back_compat_init' );
if ( is_user_logged_in() ) {
register_setting(
'wcb_sponsor_options',
'wcb_sponsor_level_order',
array(
'sanitize_callback' => array( $this, 'validate_sponsor_options' ),
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'integer',
),
),
),
)
);
}
}
/**
* Runs during admin_init.
*/
public function admin_init() {
add_action( 'pre_get_posts', array( $this, 'admin_pre_get_posts' ) );
}
/**
* Runs during init, because rest_api_init is too late.
*/
public function rest_init() {
require_once 'inc/rest-api.php';
}
/**
* Runs during admin_menu
*/
public function admin_menu() {
$page = add_submenu_page( 'edit.php?post_type=wcb_sponsor', __( 'Order Sponsor Levels', 'wordcamporg' ), __( 'Order Sponsor Levels', 'wordcamporg' ), 'edit_posts', 'sponsor_levels', array( $this, 'render_order_sponsor_levels' ) );
add_action( "admin_print_scripts-$page", array( $this, 'enqueue_order_sponsor_levels_scripts' ) );
}
/**
* Add custom image sizes
*/
public function add_image_sizes() {
add_image_size( 'wcb-sponsor-logo-horizontal-2x', 600, 220, false );
}
/**
* Enqueues scripts and styles for the render_order_sponsors_level admin page.
*/
public function enqueue_order_sponsor_levels_scripts() {
wp_enqueue_script( 'wcb-sponsor-order', plugins_url( '/js/order-sponsor-levels.js', __FILE__ ), array( 'jquery-ui-sortable' ), '20110212', true );
wp_enqueue_style( 'wcb-sponsor-order', plugins_url( '/css/order-sponsor-levels.css', __FILE__ ), array(), '20110212' );
}
/**
* Renders the Order Sponsor Levels admin page.
*/
public function render_order_sponsor_levels() {
$levels = $this->get_sponsor_levels();
?>
<div class="wrap">
<h1><?php esc_html_e( 'Order Sponsor Levels', 'wordcamporg' ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( 'wcb_sponsor_options' ); ?>
<div class="description sponsor-order-instructions">
<?php esc_html_e( 'Change the order of sponsor levels are displayed in the sponsors page template.', 'wordcamporg' ); ?>
</div>
<ul class="sponsor-order">
<?php foreach ( $levels as $term ) : ?>
<li class="level">
<input type="hidden" class="level-id" name="wcb_sponsor_level_order[]" value="<?php echo esc_attr( $term->term_id ); ?>" />
<?php echo esc_html( $term->name ); ?>
</li>
<?php endforeach; ?>
</ul>
<p class="submit">
<input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Options', 'wordcamporg' ); ?>" />
</p>
</form>
</div>
<?php
}
/**
* Runs when settings are updated for the sponsor level order page.
*/
public function validate_sponsor_options( $input ) {
if ( ! is_array( $input ) ) {
$input = null;
} else {
foreach ( $input as $key => $value ) {
$input[ $key ] = (int) $input[ $key ];
}
$input = array_values( $input );
}
return $input;
}
/**
* Returns the sponsor level terms in set order.
*/
public function get_sponsor_levels() {
$option = get_option( 'wcb_sponsor_level_order' );
$term_objects = get_terms( 'wcb_sponsor_level', array( 'get' => 'all' ) );
$terms = array();
$ordered_terms = array();
foreach ( $term_objects as $term ) {
$terms[ $term->term_id ] = $term;
}
if ( empty( $option ) ) {
$option = array();
}
foreach ( $option as $term_id ) {
if ( isset( $terms[ $term_id ] ) ) {
$ordered_terms[] = $terms[ $term_id ];
unset( $terms[ $term_id ] );
}
}
return array_merge( $ordered_terms, array_values( $terms ) );
}
/**
* Runs during pre_get_posts in admin.
*
* @param WP_Query $query
*/
public function admin_pre_get_posts( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
$current_screen = get_current_screen();
// Order by session time.
if ( 'edit-wcb_session' === $current_screen->id ) {
if ( $query->get( 'orderby' ) === '_wcpt_session_time' ) {
$query->set( 'meta_key', '_wcpt_session_time' );
$query->set( 'orderby', 'meta_value_num' );
} else if ( $query->get( 'orderby' ) === '' ) {
$query->set( 'meta_key', '_wcpt_session_time' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'asc' );
}
}
}
/**
* Enqueues Scripts required for displaying settings.
*/
public function admin_enqueue_scripts() {
global $post_type;
// Register.
wp_register_script(
'wcb-spon', // Avoid "sponsor" since that's a trigger word for ad blockers.
plugins_url( 'js/wcb-spon.js', __FILE__ ),
array( 'jquery', 'backbone', 'media-views' ),
1,
true
);
wp_localize_script(
'wcb-spon',
'wcbSponsors',
array(
'l10n' => array(
'modalTitle' => __( 'Sponsor Agreement', 'wordcamporg' ),
),
'modal' => array(
'allowedTypes' => array( 'image', 'application/pdf' ),
),
)
);
$this->register_script( 'wcb-session-meta', 'build/sessions.js' );
$this->register_script( 'wcb-speaker-meta', 'build/speakers.js' );
$this->register_script( 'wcb-organizer-meta', 'build/organizers.js' );
// Enqueues scripts and styles for session admin page.
if ( 'wcb_session' == $post_type ) {
wp_enqueue_script( 'wcb-session-meta' );
$session_time = false;
$most_recent_sessions = get_posts( array(
'post_type' => 'wcb_session',
'orderby' => 'modified',
'post_status' => array( 'draft', 'pending', 'publish' ),
'numberposts' => '1',
) );
if ( ! empty( $most_recent_sessions ) ) {
$session_time = absint( $most_recent_sessions[0]->_wcpt_session_time );
}
// if ( ! $session_time ) {
// $wordcamp_start_date = get_wordcamp_post()->meta['Start Date (YYYY-mm-dd)'][0];
// $session_time = ( isset( $wordcamp_start_date ) ) ? $wordcamp_start_date : 0;
// }
$settings = array(
'duration' => self::SESSION_DEFAULT_DURATION,
'time' => $session_time,
);
wp_add_inline_script(
'wcb-session-meta',
sprintf(
'var WCPT_Session_Defaults = JSON.parse( decodeURIComponent( \'%s\' ) );',
rawurlencode( wp_json_encode( $settings ) )
),
'before'
);
}
// Enqueues scripts and styles for sponsors editor page.
if ( 'wcb_sponsor' === $post_type ) {
wp_enqueue_script( 'wcb-spon' );
}
// Enqueues scripts and styles for speakers editor pages.
if ( 'wcb_speaker' === $post_type ) {
wp_enqueue_script( 'wcb-speaker-meta' );
}
// Enqueues scripts and styles for organizers editor pages.
if ( 'wcb_organizer' === $post_type ) {
wp_enqueue_script( 'wcb-organizer-meta' );
}
}
/**
* Register a script generated by `wp-scripts`
*/
public function register_script( $handle, $path ) {
$full_path = __DIR__ . '/' . $path;
$deps_path = __DIR__ . '/' . str_replace( '.js', '.asset.php', $path );
$script_info = file_exists( $deps_path )
? require( $deps_path )
: array(
'dependencies' => array(),
// 'version' => filemtime( $full_path ),
);
wp_register_script(
$handle,
plugins_url( $path, __FILE__ ),
$script_info['dependencies'],
$script_info['version'],
true
);
}
/**
* Enqueue scripts.
*/
public function wp_enqueue_scripts() {
wp_enqueue_style( 'wcb_shortcodes', plugins_url( 'css/shortcodes.css', __FILE__ ), array(), 3 );
}
/**
* Runs during admin_print_styles, does some CSS things.
*
* @todo add an icon for wcb_organizer
* @uses get_current_screen()
* @uses wp_enqueue_style()
*/
public function admin_css() {
$screen = get_current_screen();
switch ( $screen->id ) {
case 'edit-wcb_organizer':
case 'edit-wcb_speaker':
case 'edit-wcb_sponsor':
case 'edit-wcb_session':
case 'wcb_sponsor':
case 'dashboard':
wp_enqueue_style(
'wcpt-admin',
plugins_url( '/css/admin.css', __FILE__ ),
array(),
filemtime( __DIR__ . '/css/admin.css' )
);
break;
case 'wcb_session':
case 'wcb_organizer':
case 'wcb_speaker':
wp_enqueue_style(
'wcpt-editor',
plugins_url( '/css/editor.css', __FILE__ ),
array(),
filemtime( __DIR__ . '/css/editor.css' )
);
break;
default:
}
}
/**
* The [schedule] shortcode callback (experimental)
*
* @todo implement date arg
* @todo implement anchor for session_link
* @todo maybe simplify $attr['custom']
* @todo cleanup
*/
public function shortcode_schedule( $attr, $content ) {
enqueue_favorite_sessions_dependencies();
$attr = preprocess_schedule_attributes( $attr );
$tracks = get_schedule_tracks( $attr['tracks'] );
$tracks_explicitly_specified = 'all' !== $attr['tracks'];
$sessions = get_schedule_sessions( $attr['date'], $tracks_explicitly_specified, $tracks );
$columns = get_schedule_columns( $tracks, $sessions, $tracks_explicitly_specified );
$class_names = 'wcpt-schedule';
// Twenty Twenty has a very narrow content width, use wide width when displaying more than 2 tracks.
if ( 'twentytwenty' === get_stylesheet() && ( count( $tracks ) > 2 ) ) {
$class_names .= ' alignwide';
}
$html = sprintf( '<table class="%s" border="0">', $class_names );
$html .= '<thead>';
$html .= '<tr>';
// Table headings.
$html .= '<th class="wcpt-col-time">' . esc_html__( 'Time', 'wordcamporg' ) . '</th>';
foreach ( $columns as $term_id ) {
$track = get_term( $term_id, 'wcb_track' );
$html .= sprintf(
'<th class="wcpt-col-track"> <span class="wcpt-track-name">%s</span> <span class="wcpt-track-description">%s</span> </th>',
isset( $track->term_id ) ? esc_html( $track->name ) : '',
isset( $track->term_id ) ? esc_html( $track->description ) : ''
);
}
$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';
$time_format = get_option( 'time_format', 'g:i a' );
foreach ( $sessions as $time => $entry ) {
$skip_next = 0;
$colspan = 0;
$columns_html = '';
foreach ( $columns as $key => $term_id ) {
// Allow the below to skip some items if needed.
if ( $skip_next > 0 ) {
$skip_next--;
continue;
}
// For empty items print empty cells.
if ( empty( $entry[ $term_id ] ) ) {
$columns_html .= '<td class="wcpt-session-empty"></td>';
continue;
}
// For custom labels print label and continue.
if ( is_string( $entry[ $term_id ] ) ) {
$columns_html .= sprintf( '<td colspan="%d" class="wcpt-session-custom">%s</td>', count( $columns ), esc_html( $entry[ $term_id ] ) );
break;
}
// Gather relevant data about the session.
$colspan = 1;
$classes = array();
$session = get_post( $entry[ $term_id ] );
$session_title = apply_filters( 'the_title', $session->post_title );
$session_tracks = get_the_terms( $session->ID, 'wcb_track' );
$session_categories = get_the_terms( $session->ID, 'wcb_session_category' );
$session_track_titles = is_array( $session_tracks ) ? implode( ', ', wp_list_pluck( $session_tracks, 'name' ) ) : '';
$session_type = get_post_meta( $session->ID, '_wcpt_session_type', true );
if ( ! in_array( $session_type, array( 'session', 'custom' ), true ) ) {
$session_type = 'session';
}
// Fetch speakers associated with this session.
$speakers = array();
$speakers_ids = array_map( 'absint', (array) get_post_meta( $session->ID, '_wcpt_speaker_id' ) );
if ( ! empty( $speakers_ids ) ) {
$speakers = get_posts( array(
'post_type' => 'wcb_speaker',
'posts_per_page' => -1,
'post__in' => $speakers_ids,
) );
}
// Add CSS classes to help with custom styles.
foreach ( $speakers as $speaker ) {
$classes[] = 'wcb-speaker-' . $speaker->post_name;
}
if ( is_array( $session_tracks ) ) {
foreach ( $session_tracks as $session_track ) {
$classes[] = 'wcb-track-' . $session_track->slug;
}
}
if ( is_array( $session_categories ) ) {
foreach ( $session_categories as $session_category ) {
$classes[] = 'wcb-session-category-' . $session_category->slug;
}
}
$classes[] = 'wcpt-session-type-' . $session_type;
$classes[] = 'wcb-session-' . $session->post_name;
$content = '<div class="wcb-session-cell-content">';
// Determine the session title.
if ( 'permalink' === $attr['session_link'] && 'session' === $session_type ) {
$session_title_html = sprintf( '<a class="wcpt-session-title" href="%s">%s</a>', esc_url( get_permalink( $session->ID ) ), $session_title );
} elseif ( 'anchor' === $attr['session_link'] && 'session' === $session_type ) {
$session_title_html = sprintf( '<a class="wcpt-session-title" href="%s">%s</a>', esc_url( $this->get_wcpt_anchor_permalink( $session->ID ) ), $session_title );
} else {
$session_title_html = sprintf( '<span class="wcpt-session-title">%s</span>', $session_title );
}
$content .= $session_title_html;
$speakers_names = array();
foreach ( $speakers as $speaker ) {
$speaker_name = apply_filters( 'the_title', $speaker->post_title );
if ( 'anchor' === $attr['speaker_link'] ) {
// speakers/#wcorg-speaker-slug.
$speaker_permalink = $this->get_wcpt_anchor_permalink( $speaker->ID );
} elseif ( 'wporg' === $attr['speaker_link'] ) {
// profiles.wordpress.org/user.
$speaker_permalink = $this->get_speaker_wporg_permalink( $speaker->ID );
} elseif ( 'permalink' === $attr['speaker_link'] ) {
// year.city.wordcamp.org/speakers/slug.
$speaker_permalink = get_permalink( $speaker->ID );
}
if ( ! empty( $speaker_permalink ) ) {
$speaker_name = sprintf( '<a href="%s">%s</a>', esc_url( $speaker_permalink ), esc_html( $speaker_name ) );
}
$speakers_names[] = $speaker_name;
}
// Add speakers names to the output string.
if ( count( $speakers_names ) ) {
$content .= sprintf( ' <span class="wcpt-session-speakers">%s</span>', implode( ', ', $speakers_names ) );
}
// End of cell-content.
$content .= '</div>';
// Favourite session star-icon.
if ( 'session' === $session_type ) {
$content .= '<div class="wcb-session-favourite-icon">';
$content .= '<a href="#" role="button" class="fav-session-button" aria-pressed="false"><span class="screen-reader-text">';
$content .= sprintf( esc_html__( 'Favorite session: %s', 'wordcamporg' ), $session_title );
$content .= '</span><span class="dashicons dashicons-star-filled"></span></a></div>';
}
$columns_clone = $columns;
// If the next element in the table is the same as the current one, use colspan.
if ( key( array_slice( $columns, -1, 1, true ) ) !== $key ) {
foreach ( $columns_clone as $clonekey => $clonevalue ) {
if ( $clonekey === $key ) {
continue;
}
if ( ! empty( $entry[ $clonevalue ] ) && $entry[ $clonevalue ] === $session->ID ) {
$colspan++;
$skip_next++;
} else {
break;
}
}
}
$columns_html .= sprintf( '<td colspan="%d" class="%s" data-track-title="%s" data-session-id="%s">%s</td>', $colspan, esc_attr( implode( ' ', $classes ) ), $session_track_titles, esc_attr( $session->ID ), $content );
}
$global_session = count( $columns ) === $colspan ? ' global-session' : '';
$global_session_slug = $global_session ? ' ' . sanitize_html_class( sanitize_title_with_dashes( $session->post_title ) ) : '';
$html .= sprintf( '<tr class="%s">', sanitize_html_class( 'wcpt-time-' . wp_date( $time_format, $time ) ) . $global_session . $global_session_slug );
$html .= sprintf( '<td class="wcpt-time">%s</td>', str_replace( ' ', ' ', esc_html( wp_date( $time_format, $time ) ) ) );
$html .= $columns_html;
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
$html .= fav_session_share_form();
return $html;
}
/**
* Returns a speaker's WordPress.org profile url (if username set)
*
* @param int $speaker_id int The speaker's post id.
*
* @return NULL|string
*/
public function get_speaker_wporg_permalink( $speaker_id ) {
$post = get_post( $speaker_id );
if ( 'wcb_speaker' !== $post->post_type || 'publish' !== $post->post_status ) {
return null;
}
$wporg_user_id = get_post_meta( $speaker_id, '_wcpt_user_id', true );
if ( ! $wporg_user_id ) {
return null;
}
$user = get_user_by( 'id', $wporg_user_id );
if ( ! $user ) {
return null;
}
$permalink = sprintf( 'http://profiles.wordpress.org/%s', strtolower( $user->user_nicename ) );
return esc_url_raw( $permalink );
}
/**
* Returns an anchor permalink for a Speaker or Session.
*
* Any page with the Speakers or Sessions block will contain IDs that can be used as anchors. If the current
* page contains the corresponding block, we'll assume the user wants to link there. Otherwise, we'll attempt
* to find another page that contains the block.
*
* Note: if `content_blocks` skip feature flag is set, this site still uses the shortcodes, and we search for
* shortcode content instead.
*
* @param int $target_id The speaker/session's post ID.
*
* @return string
*/
public function get_wcpt_anchor_permalink( $target_id ) {
global $post;
if ( ! $post ) {
return '';
}
$anchor_target = get_post( $target_id );
if ( 'publish' !== $anchor_target->post_status ) {
return '';
}
switch ( $anchor_target->post_type ) {
case 'wcb_speaker':
$current_post_has_target = wcorg_skip_feature( 'content_blocks' ) ?
has_shortcode( $post->post_content, 'speakers' ) :
has_block( 'wordcamp/speakers', $post->post_content );
$permalink = $current_post_has_target ? get_permalink( $post->id ) : $this->get_wcpt_permalink( 'speakers' );
$anchor_id = $anchor_target->post_name;
break;
case 'wcb_session':
$current_post_has_target = wcorg_skip_feature( 'content_blocks' ) ?
has_shortcode( $post->post_content, 'sessions' ) :
has_block( 'wordcamp/sessions', $post->post_content );
$permalink = $current_post_has_target ? get_permalink( $post->id ) : $this->get_wcpt_permalink( 'sessions' );
$anchor_id = $anchor_target->ID;
break;
default:
$permalink = false;
$anchor_id = false;
break;
}
if ( ! $permalink ) {
return '';
}
return sprintf(
'%s#wcorg-%s-%s',
$permalink,
str_replace( 'wcb_', '', $anchor_target->post_type ),
sanitize_html_class( $anchor_id )
);
}
/**
* Returns the page permalink for speakers, sessions, or organizers.
*
* Fetches for a page with the Speakers, Sessions, or Organizers block and returns the permalink of the oldest
* page on the site (which should be the generated site content).
*
* Note: if `content_blocks` skip feature flag is set, this site still uses the shortcodes, and we search for
* shortcode content instead.
*
* @param string $type
*
* @return false | string
*/
public function get_wcpt_permalink( $type ) {
if ( ! in_array( $type, array( 'speakers', 'sessions', 'organizers' ), true ) ) {
return false;
}
/*
* The [schedule] shortcode can call this for each session and speaker, so cache the result to avoid
* dozens of SQL queries.
*/
if ( isset( $this->wcpt_permalinks[ $type ] ) ) {
return $this->wcpt_permalinks[ $type ];
}
$this->wcpt_permalinks[ $type ] = false;
$wcpt_post = get_posts( array(
'post_type' => array( 'page' ),
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'asc',
's' => wcorg_skip_feature( 'content_blocks' ) ? "[{$type}" : "<!-- wp:wordcamp/{$type}",
'posts_per_page' => 1,
) );
if ( ! empty( $wcpt_post ) ) {
$this->wcpt_permalinks[ $type ] = get_permalink( $wcpt_post[0] );
}
return $this->wcpt_permalinks[ $type ];
}
/**
* Determine if the current loop is just a single page, or a loop of posts within a page
*
* For example, this helps to target a single wcb_speaker post vs a page containing the [speakers] shortcode,
* which loops through wcb_speaker posts. Using functions like is_single() don't work, because they reference
* the main query instead of the $speakers query.
*
* @param string $post_type
*
* @return bool
*/
protected function is_single_cpt_post( $post_type ) {
global $wp_query;
return isset( $wp_query->query[ $post_type ] ) && $post_type === $wp_query->query['post_type'];
}
/**
* Add the speaker's avatar to their post
*
* We don't enable it for sites that were created before it was committed, because it may need custom CSS
* to look good with their custom design, but we allow older sites to opt-in.
*
* @param string $content
*
* @return string
*/
public function add_avatar_to_speaker_posts( $content ) {
global $post;
$enabled_site_ids = apply_filters( 'wcpt_speaker_post_avatar_enabled_site_ids', array( 364 ) ); // 2014.sf
if ( ! $this->is_single_cpt_post( 'wcb_speaker') ) {
return $content;
}
$site_id = get_current_blog_id();
if ( $site_id <= apply_filters( 'wcpt_speaker_post_avatar_min_site_id', 463 ) && ! in_array( $site_id, $enabled_site_ids, true ) ) {
return $content;
}
$avatar = get_avatar( get_post_meta( $post->ID, '_wcb_speaker_email', true ) );
return '<div class="speaker-avatar">' . $avatar . '</div>' . $content;
}
/**
* Add speaker information to Session posts
*
* We don't enable it for sites that were created before it was committed, because some will have already
* crafted the bio to include this content, so duplicating it would look wrong, but we still allow older
* sites to opt-in.
*
* @param string $content
*
* @return string
*/
public function add_speaker_info_to_session_posts( $content ) {
global $post;
$enabled_site_ids = apply_filters( 'wcpt_session_post_speaker_info_enabled_site_ids', array( 364 ) ); // 2014.sf
if ( ! $this->is_single_cpt_post( 'wcb_session') ) {
return $content;
}
$site_id = get_current_blog_id();
if ( $site_id <= apply_filters( 'wcpt_session_post_speaker_info_min_site_id', 463 ) && ! in_array( $site_id, $enabled_site_ids, true ) ) {
return $content;
}
$speaker_ids = (array) get_post_meta( $post->ID, '_wcpt_speaker_id' );
if ( empty( $speaker_ids ) ) {
return $content;
}
$speaker_args = array(
'post_type' => 'wcb_speaker',
'posts_per_page' => -1,
'post__in' => $speaker_ids,
'orderby' => 'title',
'order' => 'asc',
);
$speakers = new WP_Query( $speaker_args );
if ( ! $speakers->have_posts() ) {
return $content;
}
$speakers_html = sprintf(
'<h2 class="session-speakers">%s</h2>',
_n(
'Speaker',
'Speakers',
$speakers->post_count,
'wordcamporg'
)
);
$speakers_html .= '<ul id="session-speaker-names">';
while ( $speakers->have_posts() ) {
$speakers->the_post();
$speakers_html .= sprintf( '<li><a href="%s">%s</a></li>', get_the_permalink(), get_the_title() );
}
$speakers_html .= '</ul>';
wp_reset_postdata();
return $content . $speakers_html;
}
/**
* Add Slides link to Session posts
*
* We don't enable it for sites that were created before it was committed, because some will have already
* crafted the session to include this content, so duplicating it would look wrong, but we still allow older
* sites to opt-in.
*
* @param string $content
*
* @return string
*/
public function add_slides_info_to_session_posts( $content ) {
global $post;
$enabled_site_ids = apply_filters(
'wcpt_session_post_slides_info_enabled_site_ids',
array(
206, // testing.wordcamp.org.
648, // 2016.asheville.
651, // 2016.kansascity.
623, // 2016.tampa.
)
);
if ( ! $this->is_single_cpt_post( 'wcb_session' ) ) {
return $content;
}
$site_id = get_current_blog_id();
if ( $site_id <= apply_filters( 'wcpt_session_post_slides_info_min_site_id', 699 ) && ! in_array( $site_id, $enabled_site_ids, true ) ) {
return $content;
}
$session_slides = get_post_meta( $post->ID, '_wcpt_session_slides', true );
if ( empty( $session_slides ) ) {
return $content;
}
$session_slides_html = '<div class="session-video">';
$session_slides_html .= sprintf( __( '<a href="%s" target="_blank">View Session Slides</a>', 'wordcamporg' ), esc_url( $session_slides ) );
$session_slides_html .= '</div>';
return $content . $session_slides_html;
}
/**
* Add Video link to Session posts
*
* We don't enable it for sites that were created before it was committed, because some will have already
* crafted the session to include this content, so duplicating it would look wrong, but we still allow older
* sites to opt-in.
*
* @param string $content
*
* @return string
*/
public function add_video_info_to_session_posts( $content ) {
global $post;
$enabled_site_ids = apply_filters(
'wcpt_session_post_video_info_enabled_site_ids',
array(
206, // testing.wordcamp.org .
648, // 2016.asheville .
623, // 2016.tampa .
)
);
if ( ! $this->is_single_cpt_post( 'wcb_session' ) ) {
return $content;
}
$site_id = get_current_blog_id();
if ( $site_id <= apply_filters( 'wcpt_session_post_video_info_min_site_id', 699 ) && ! in_array( $site_id, $enabled_site_ids, true ) ) {
return $content;
}
$session_video = get_post_meta( $post->ID, '_wcpt_session_video', true );
if ( empty( $session_video ) ) {
return $content;
}
$session_video_html = '<div class="session-video">';
$session_video_html .= sprintf( __( '<a href="%s" target="_blank">View Session Video</a>', 'wordcamporg' ), esc_url( $session_video ) );
$session_video_html .= '</div>';
return $content . $session_video_html;
}
/**
* Append a session's categories to its post content.
*
* @param string $content
*
* @return string
*/
public function add_session_categories_to_session_posts( $content ) {
global $post;
if ( ! $this->is_single_cpt_post( 'wcb_session' ) ) {
return $content;
}
$session_categories_html = '';
$session_categories_list = get_the_term_list( $post->ID, 'wcb_session_category', '', _x( ', ', 'Used between list items, there is a space after the comma.', 'wordcamporg' ) );
if ( $session_categories_list ) {
$session_categories_html = sprintf(
'<span class="session-categories-links"><span class="screen-reader-text">%1$s</span> %2$s</span>',
esc_html_x( 'Categories', 'Used before session category names.', 'wordcamporg' ),
wp_kses_post( $session_categories_list )
);
}
return $content . $session_categories_html;
}
/**
* Add the sessions's category slugs to the body tag.
*
* @param array $body_classes
*
* @return array
*/
public function session_category_slugs_to_body_tag( $body_classes ) {
if ( 'wcb_session' === get_post_type() ) {
$session_categories = get_the_terms( get_post(), 'wcb_session_category' );
if ( is_array( $session_categories ) ) {
foreach ( $session_categories as $session_category ) {
$body_classes[] = 'wcb_session_category-' . $session_category->slug;
}
}
}
return $body_classes;
}
/**
* Add session information to Speaker posts
*
* We don't enable it for sites that were created before it was committed, because some will have already
* crafted the bio to include this content, so duplicating it would look wrong, but we still allow older
* sites to opt-in.
*
* @param string $content
*
* @return string
*/
public function add_session_info_to_speaker_posts( $content ) {
global $post;
$enabled_site_ids = apply_filters( 'wcpt_speaker_post_session_info_enabled_site_ids', array( 364 ) ); // 2014.sf
if ( ! $this->is_single_cpt_post( 'wcb_speaker') ) {
return $content;
}
$site_id = get_current_blog_id();
if ( $site_id <= apply_filters( 'wcpt_speaker_post_session_info_min_site_id', 463 ) && ! in_array( $site_id, $enabled_site_ids, true ) ) {
return $content;
}
$session_args = array(
'post_type' => 'wcb_session',