-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
executable file
·1437 lines (1230 loc) · 42.4 KB
/
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
/**
* READ ME, PLEASE:
*
* Various functions for this theme MUST execute in a specific order and
* depend on WordPress hooks to be executed with specific priority values.
* These functions hook into 'init' with priority values set.
* This order should NOT be modified unless you want bad things to happen.
*
* 1) Register Taxonomies
* 2) Register CPT's
* 3) Determine the current requested post's relevant version
* 4) Require version-specific functions/config.php and shortcodes.php files
* 5) Register Config::$styles, Config::$scripts (MUST occur after version-
* specific functions/config.php is loaded so that extra styles, scripts can
* be appended per version)
**/
require_once( 'functions/base.php' ); # Base theme functions
require_once( 'custom-taxonomies.php' ); # Where taxonomies are defined
require_once( 'custom-post-types.php' ); # Where post types are defined
require_once( 'functions/config.php' ); # Where site-level configuration settings are defined
require_once( 'functions/acf-functions.php' ); # Where related stories ACF fields and functions are defined
require_once( 'functions/related-stories.php' ); # Where related stories ACF fields and functions are defined
// Plugin extras/overrides
if ( class_exists( 'UCF_Events_Common' ) ) {
include_once 'includes/ucf-events-functions.php';
}
/****************************************************************************
*
* START version configuration, backward compatibility functions here
*
****************************************************************************/
/**
* Function that determines which version should be considered active, based
* on the passed in post object or what post data is being loaded.
**/
function get_relevant_version( $the_post=null ) {
if ( !$the_post ) {
$request_uri = untrailingslashit( strtok( $_SERVER['REQUEST_URI'], '?') );
if ( $request_uri === get_site_url( get_current_blog_id(), '', 'relative' ) ) {
// If the home page has been requested, always return the current issue
$the_post = get_current_issue();
} else if ( is_admin() ) {
if ( wp_doing_ajax() ) {
// If this is an admin-ajax request, sniff $_POST data
// for a post ID. There are a lot of different possible
// actions that could be happening here, but if this is a POST
// admin-ajax request and there is a `post_id` somewhere in
// $_POST, assume we should care about it:
// https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/ajax-actions.php
// https://github.com/WordPress/WordPress/blob/master/wp-admin/admin-ajax.php
if ( isset( $_POST['post_id'] ) ) {
$the_post = get_post( $_POST['post_id'] );
} else {
$the_post = null;
}
} else {
// If we're on an admin screen, grab the post ID from the URL
// if we're on a post/page edit screen, and determine the version
// from that. Otherwise, just assume the latest version
$request_uri_params_str = wp_parse_url( $request_uri, PHP_URL_QUERY );
parse_str( $request_uri_params_str, $request_uri_params );
if (
array_key_exists( 'post', $request_uri_params )
&& strpos( $request_uri, '/post.php' ) !== false
) {
$uri_post_id = $request_uri_params['post'];
$the_post = get_post( $uri_post_id );
} else {
$the_post = null;
}
}
} else {
global $post;
// If global $post hasn't been set yet, try fishing the requested url for
// post information
if ( !$post ) {
$url_query_array = array();
$url_query = parse_url( $request_uri, PHP_URL_QUERY );
parse_str( $url_query, $url_query_array );
// Get the relative path of the url, accounting for potential
// subdirectory WordPress installs
$url_path = str_replace( get_site_url( get_current_blog_id(), '', 'relative' ), '', $request_uri );
if ( substr( $url_path, 0, 1 ) === '?' ) {
$url_path = '/' . $url_path; // If path only contains a query str, make sure it is prepended with /
}
// Check if url structure follows WP's default permalink pattern
// (<root url>/?params). Post ID is stored in 'p' param.
// Should catch draft post previews here.
if ( $url_path === '/?' . $url_query && isset( $url_query_array['p'] ) ) {
$the_post = get_post( $url_query_array['p'] );
}
else {
// URL follows custom permalink structure. Try using
// get_page_by_path() per each post type we want to enforce
// version-specific assets on.
// Make sure $url_path has no query params before passing to get_page_by_path()
$url_path = explode( '/?', $url_path );
$url_path = $url_path[0];
// Remove '/story' and '/issue' url prefixes (get_page_by_path() will fail if
// URLs have these prefixes)
$url_path = preg_replace( '/^\/story\/|\/issue\/|\/photo\_essay\//', '/', $url_path );
$post_issue = get_page_by_path( $url_path , OBJECT, array( 'issue' ) );
$post_story = get_page_by_path( $url_path , OBJECT, array( 'story' ) );
$post_photo_essay = get_page_by_path( $url_path , OBJECT, array( 'photo_essay' ) );
if ( $post_issue ) {
$the_post = $post_issue;
}
else if ( $post_story ) {
$the_post = $post_story;
}
else if ( $post_photo_essay ) {
$the_post = $post_photo_essay;
}
else {
// The requested content isn't a story, issue or photo essay.
// Set $the_post to null here so that get_relevant_issue()
// will return a fallback value (the latest issue).
$the_post = null;
}
}
}
else {
$the_post = $post;
}
}
}
$relevant_issue = get_relevant_issue( $the_post );
$relevant_version = get_post_meta( $relevant_issue->ID, 'issue_version', true );
if ( empty( $relevant_version ) ) {
$relevant_version = LATEST_VERSION;
}
return intval( $relevant_version );
}
/**
* Returns a relative path to a file by version.
**/
function get_version_file_path( $filename, $version=null ) {
if ( !$version ) {
$version = get_relevant_version();
}
return VERSIONS_PATH . 'v' . $version . '/' . $filename;
}
/**
* Based on the current relevant version, require necessary files.
**/
function setup_version_files() {
define( 'THE_POST_VERSION', get_relevant_version() ); // The version for the story/issue loaded in a given request, or the latest version if a story or issue is not available
define( 'THE_POST_VERSION_DIR', get_stylesheet_directory() . '/' . VERSIONS_PATH . 'v' . THE_POST_VERSION );
define( 'THE_POST_VERSION_URL', get_stylesheet_directory_uri() . '/' . VERSIONS_PATH . 'v' . THE_POST_VERSION );
require_once( get_version_file_path( 'functions/config.php' ) ); # Where version-level configuration settings are defined
require_once( get_version_file_path( 'shortcodes.php' ) ); # Per version shortcodes
}
add_action( 'init', 'setup_version_files', 3 );
/**
* Loads version-specific Front Page, Page and CPT templates instead of templates
* from the theme's root directory.
*
* Note: Posts should always use templates from the root directory
* (they are not modified per-version).
**/
function by_version_template( $template ) {
global $post;
if ( is_front_page() ) {
$new_template = locate_template( array( get_version_file_path( 'front-page.php' ) ) );
} elseif ( $post->post_type === 'page' ) {
$new_template = locate_template( array( get_version_file_path( 'page.php' ) ) );
}
if ( in_array( $post->post_type, array( 'story', 'issue', 'photo_essay' ) ) ) {
$new_template = locate_template( array( get_version_file_path( 'single-' . $post->post_type . '.php' ) ) );
}
if ( !empty( $new_template ) ) {
return $new_template;
}
return $template;
}
add_filter( 'template_include', 'by_version_template', 99 );
/**
* Loads version-specific header template. Should be used in place of
* get_header() for this theme.
*
* Note: this (and get_version_footer()) cannot hook into get_header/get_footer
* without requiring that a header.php/footer.php file exists in the theme
* root, so we opt to use a separate function instead to avoid excessive file
* includes.
**/
function get_version_header( $template_name='' ) {
if ( $template_name ) {
$template_name = '-' . $template_name;
}
$new_template = locate_template( array( get_version_file_path( 'header' . $template_name . '.php' ) ) );
if ( !empty( $new_template ) ) {
return load_template( $new_template );
}
}
/**
* Loads version-specific footer template. Should be used in place of
* get_footer() for this theme.
**/
function get_version_footer( $template_name='' ) {
if ( $template_name ) {
$template_name = '-' . $template_name;
}
$new_template = locate_template( array( get_version_file_path( 'footer' . $template_name . '.php' ) ) );
if ( !empty( $new_template ) ) {
return load_template( $new_template );
}
}
/**
* Check/create hidden theme options which store flags that tell us if methods
* for maintaining backward compatibility have already been performed.
* Functions with theme option flags below should only run once, assuming they
* ran successfully the first time.
**/
function check_backward_compatibility() {
// Attempts to set versions on Issue posts, based on Issue slugs set
// in V1_ISSUES and V2_ISSUES.
if ( get_option( 'theme_bc_versions_set' ) == false ) {
$success = set_initial_issue_versions();
if ( $success == true ) {
add_option( 'theme_bc_versions_set', true );
}
}
// Set a 'story_template' meta field value for stories with an issue slug
// in the V1_ISSUES constant so that they are 'custom' if they have no
// value.
if ( get_option( 'theme_bc_v1_templates_set' ) == false ) {
$success = set_templates_for_v1();
if ( $success == true ) {
add_option( 'theme_bc_v1_templates_set', true );
}
}
}
add_action( 'init', 'check_backward_compatibility' );
/**
* Set version value on Issues in V1_ISSUES and V2_ISSUES.
* Returns true on success, false on failure.
**/
function set_initial_issue_versions() {
$issues = get_posts( array(
'numberposts' => -1,
'post_type' => 'issue',
'orderby' => 'post_date',
'order' => 'DESC',
) );
if ( !$issues || !is_array( $issues ) ) {
return false;
}
foreach ( $issues as $issue ) {
$success = null;
if ( in_array( $issue->post_name, unserialize( V1_ISSUES ) ) ) {
$success = update_post_meta( $issue->ID, 'issue_version', 1 );
if ( $success !== true ) {
return false;
}
// Trigger a post update to make sure VDP ban is run
wp_update_post( $issue );
}
else if ( in_array( $issue->post_name, unserialize( V2_ISSUES ) ) ) {
$success = update_post_meta( $issue->ID, 'issue_version', 2 );
if ( $success !== true ) {
return false;
}
// Trigger a post update to make sure VDP ban is run
wp_update_post( $issue );
}
}
return true;
}
/**
* Set a 'story_template' meta field value for stories with an issue
* slug in the V1_ISSUES constant so that they are 'custom'
* if they have no value. Also updates v1 'issue_template' values.
**/
function set_templates_for_v1() {
$issue_slugs = unserialize( V1_ISSUES );
$stories = get_posts( array(
'numberposts' => -1,
'post_type' => 'story',
'tax_query' => array(
array(
'taxonomy' => 'issues',
'field' => 'slug',
'terms' => $issue_slugs
)
)
) );
$issues = array();
foreach ( $issue_slugs as $slug ) {
$issue = get_posts( array(
'numberposts' => 1,
'post_type' => 'issue',
'name' => $slug
) );
if ( $issue[0] ) {
$issue = $issue[0];
$issues[] = $issue->ID;
}
}
if ( !$stories || !is_array( $stories ) || !$issues || !is_array( $issues ) ) {
return false;
}
foreach ( $stories as $story ) {
if ( get_post_meta( $story->ID, 'story_template', true ) !== 'custom' ) {
$success = update_post_meta( $story->ID, 'story_template', 'custom' );
if ( $success !== true ) {
return false;
}
// Trigger a post update to make sure VDP ban is run
wp_update_post( $story );
}
}
foreach ( $issues as $issue_id ) {
if ( get_post_meta( $issue_id, 'issue_template', true ) !== 'custom' ) {
$success = update_post_meta( $issue_id, 'issue_template', 'custom' );
if ( $success !== true ) {
return false;
}
// Trigger a post update to make sure VDP ban is run
wp_update_post( get_post( $issue_id ) );
}
}
return true;
}
/****************************************************************************
*
* START site-level functions here
*
****************************************************************************/
/*
* Returns current issue post based on the Current Issue Cover
* value in Theme Options
*/
function no_current_issue_notice() {
echo '<div class="error"><p><strong>ERROR:</strong> Could not find current issue cover. <strong>The home page and other portions of the website <u>will not display correctly</u> until this is fixed.</strong> Make sure the "Current Issue Cover" Theme Options value is set to a <strong>published</strong> Issue post.</p></div>';
}
function get_current_issue() {
$posts = get_posts( array(
'post_type' => 'issue',
'name' => get_theme_option( 'current_issue_cover' )
) );
if( count( $posts ) == 0 ) {
// Couldn't find an issue with name saved in current_issue_cover
// theme option
add_action( 'admin_notices', 'no_current_issue_notice' );
return false;
} else {
return $posts[0];
}
}
/*
* Retrieve a list of the current issue's stories
*/
function get_current_issue_stories($exclude=array(), $limit=-1) {
$current_issue = get_current_issue();
if($current_issue === False) {
return False;
} else {
return get_issue_stories($current_issue, array('exclude'=>$exclude, 'numberposts'=>$limit));
}
}
/*
* Returns a relevant issue post object depending on the page being viewed.
* i.e., if the $post obj passed is the front page or subpage, get the current issue;
* otherwise, get the current story issue
*/
function get_relevant_issue( $post ) {
$issue = null;
if ( $post && $post->post_type == 'story' && !is_404() && !is_search() ) {
$issue = get_story_issue( $post );
}
else if ( $post && $post->post_type == 'issue' && !is_404() && !is_search() ) {
$issue = $post;
}
else if ( $post && $post->post_type == 'photo_essay' && !is_404() && !is_search() ) {
$issue = get_story_issue( $post );
}
// Fallback, or if get_story_issue() returned false
if ( !$issue ) {
$issue = get_current_issue();
}
return $issue;
}
/*
* Returns featured image URL of a specified post ID
*/
function get_featured_image_url($id, $size=null) {
$size = !$size ? 'single-post-thumbnail' : $size;
$url = '';
if(has_post_thumbnail($id)
&& ($thumb_id = get_post_thumbnail_id($id)) !== False
&& ($image = wp_get_attachment_image_src($thumb_id, $size)) !== False) {
return $image[0];
}
return $url;
}
/*
* Returns a theme option value or NULL if it doesn't exist
*/
function get_theme_option($key) {
global $theme_options;
// Added switch case in v6.0.0 for backward compatibility
// with UCF Social plugin:
switch ( $key ) {
case 'fb_url':
return get_option( 'ucf_social_facebook_url' );
case 'twitter_url':
return get_option( 'ucf_social_twitter_url' );
case 'instagram_url':
return get_option( 'ucf_social_instagram_url' );
case 'youtube_url':
return get_option( 'ucf_social_youtube_url' );
case 'flickr_url':
case 'share_url':
return null;
default:
return isset( $theme_options[$key] ) ? $theme_options[$key] : null;
}
}
/*
* Modify the permalinks for the Issue post type to the following form:
* http://ucf.edu/pegasus/<issue slug>/
*/
function modify_issue_permalinks($url, $post) {
if($post->post_type == 'issue') {
if ($post->post_status == 'publish') {
return get_bloginfo('url').'/'.$post->post_name.'/';
}
else {
// Handle drafts/previews appropriately
return get_bloginfo('url').'/?p='.$post->ID.'&post_type=issue';
}
}
return $url;
}
add_filter('post_type_link', 'modify_issue_permalinks', 10, 2);
/*
* Modify the permalinks for the Story post type to the following form:
* http://ucf.edu/pegasus/<story slug>/
*/
function modify_story_permalinks($url, $post) {
if($post->post_type == 'story') {
if ($post->post_status == 'publish') {
return get_bloginfo('url').'/'.$post->post_name.'/';
}
else {
// Handle drafts/previews appropriately
return get_bloginfo('url').'/?p='.$post->ID.'&post_type=story';
}
}
return $url;
}
add_filter('post_type_link', 'modify_story_permalinks', 10, 2);
/*
* Add a rewrite rule to handle the new Issue post type permalink structure
*/
function cpt_slug_init() {
$issue_slugs = array_map( function( $i ) {
return preg_quote($i->post_name);
},
get_posts( array(
'post_type' => 'issue',
'numberposts' => -1
)));
$story_slugs = array_map( function( $i ) {
return preg_quote($i->post_name);
},
get_posts( array(
'post_type' => 'story',
'numberposts' => -1
)));
add_rewrite_rule('^('.implode('|', $issue_slugs).')$', 'index.php?issue=$matches[1]', 'top');
add_rewrite_rule('^('.implode('|', $story_slugs).')$', 'index.php?story=$matches[1]', 'top');
flush_rewrite_rules(false);
}
add_action('init', 'cpt_slug_init');
/*
* Add featured images to RSS feeds as an <enclosure> node.
*/
function add_post_thumbnail_node() {
global $post;
if(has_post_thumbnail($post->ID)) {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$url = get_featured_image_url($post->ID);
echo('<enclosure url="'.$url.'" length="'.filesize(get_attached_file($thumbnail_id)).'" type="'.get_post_mime_type($thumbnail_id).'" />');
}
}
add_action('rss2_item', 'add_post_thumbnail_node');
/*
* Remove <content:encoded> node if it is enabled,
* Update limit on the number of posts displayed at once in a feed,
* and replace <description> node with the story's story_subtitle
* meta field content.
*/
update_option('rss_use_excerpt', 1);
update_option('posts_per_rss', 50);
function story_excerpt() {
global $post;
if ($post->post_type == 'story') {
if (get_post_meta($post->ID, 'story_description', TRUE)) {
return get_post_meta($post->ID, 'story_description', TRUE);
}
else {
return get_post_meta($post->ID, 'story_subtitle', TRUE);
}
}
else { return the_excerpt(); }
}
add_filter('the_excerpt_rss', 'story_excerpt');
/**
* Get the URL of the feed for an issue.
* Accepts either an issue post object or an issue taxonomy term object.
**/
function get_issue_feed_url($object) {
$slug = null;
$url = null;
if ($object->post_type == 'issue') {
$slug = $object->post_name;
}
else if ($object->taxonomy == 'issues') {
$slug = $object->slug;
}
if ($slug !== null) {
$url = home_url('/issues/'.$slug.'/feed/?post_type=story');
}
return $url;
}
/*
* Enqueue Issue or Story post type specific scripts
*/
function enqueue_issue_story_scripts() {
global $post;
if ( !is_404() && !is_search() ) {
// 1. add issue cover script(s)
if( $post->post_type == 'issue' || is_home() ) {
// issue-wide
$dev_issue_directory = get_post_meta( $post->ID, 'issue_dev_issue_asset_directory', TRUE );
$issue_javascript_url = Issue::get_issue_javascript_url( $post );
if ( !empty( $issue_javascript_url ) ) {
Config::add_script( $issue_javascript_url );
}
elseif ( DEV_MODE == 1 && !empty( $dev_issue_directory ) ) {
$dev_issue_javascript_url = THEME_DEV_URL.'/'.$dev_issue_directory.$post->post_name.'.js';
if ( curl_exists( $dev_issue_javascript_url ) ) {
Config::add_script( $dev_issue_javascript_url );
}
}
// issue cover specific
$dev_issue_home_directory = get_post_meta( $post->ID, 'issue_dev_home_asset_directory', TRUE );
$home_javascript_url = Issue::get_home_javascript_url( $post );
if ( !empty( $home_javascript_url ) ) {
Config::add_script( $home_javascript_url );
}
elseif ( DEV_MODE == 1 && !empty( $dev_issue_home_directory ) ) {
$dev_home_javascript_url = THEME_DEV_URL.'/'.$dev_issue_home_directory.'issue-cover.js';
if ( curl_exists( $dev_home_javascript_url ) ) {
Config::add_script( $dev_home_javascript_url );
}
}
// 2. add story script(s)
} else if( $post->post_type == 'story' ) {
$story_issue = get_story_issue( $post );
// issue-wide
$issue_javascript_url = null;
$dev_issue_directory = null;
if ( $story_issue ) {
$issue_javascript_url = Issue::get_issue_javascript_url( $story_issue );
$dev_issue_directory = get_post_meta( $story_issue->ID, 'issue_dev_issue_asset_directory', TRUE );
}
if( $story_issue !== False && !empty( $issue_javascript_url ) ) {
Config::add_script( $issue_javascript_url );
}
elseif (
$story_issue !== False &&
DEV_MODE == 1 &&
!empty( $dev_issue_directory ) )
{
$dev_issue_javascript_url = THEME_DEV_URL.'/'.$dev_issue_directory.$story_issue->post_name.'.js';
if ( curl_exists( $dev_issue_javascript_url ) ) {
Config::add_script( $dev_issue_javascript_url );
}
}
// story specific
$javascript_url = Story::get_javascript_url( $post );
$dev_story_directory = get_post_meta( $post->ID, 'story_dev_directory', TRUE );
if ( !empty( $javascript_url ) ) {
Config::add_script( $javascript_url );
}
elseif (
DEV_MODE == 1 &&
!empty( $dev_story_directory ) )
{
$dev_story_javascript_url = THEME_DEV_URL.'/'.$dev_story_directory.$post->post_name.'.js';
if ( curl_exists( $dev_story_javascript_url ) ) {
Config::add_script( $dev_story_javascript_url );
}
}
}
}
}
add_action('wp_enqueue_scripts', 'enqueue_issue_story_scripts', 10);
/*
* Get the issue post associated with a story
*/
function get_story_issue( $story ) {
$issue_terms = wp_get_object_terms( $story->ID, 'issues' );
// Make sure to grab issues that may not be published, but aren't trash
$issue_posts = get_posts( array(
'post_type' => 'issue',
'numberposts' => -1,
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private'
)
) );
if ( $issue_terms ) {
foreach( $issue_terms as $term ) {
$post_slug = $term->slug;
if ( $issue_posts ) {
foreach( $issue_posts as $issue ) {
if( $post_slug == $issue->post_name ) {
return $issue;
}
}
}
}
}
return false;
}
/*
* Get the stories associated with an issue
*/
function get_issue_stories( $issue, $options=array() ) {
if ( $issue ) {
$issue_term_slug = $issue->post_name;
$default_options = array(
'post_type' => 'story',
'exclude' => array(),
'numberposts' => -1,
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'issues',
'field' => 'slug',
'terms' => $issue_term_slug
)
),
);
$options = array_merge($default_options, $options);
return get_posts($options);
}
return null;
}
/*
* Check to see if some arbitary file exists (does not return a 404/500)
* http://stackoverflow.com/questions/14699941/php-curl-check-for-file-existence-before-downloading
*
* @return bool
*/
function curl_exists($url) {
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.1 404 Not Found' || $file_headers[0] == 'HTTP/1.1 500 Internal Server Error') {
return false;
}
return true;
}
/*
* Get home page/story stylesheet markup for the header
*
* @return string
* @author Jo Greybill
*/
function output_header_markup($post) {
$output = '';
// Add font library stylesheets for fonts that are available via the
// WYSIWYG editor (excluding Cloud.Typography fonts, which are already included)
if ( $post->post_type == 'page' || ( ( $post->post_type == 'story' || $post->post_type == 'issue' || $post->post_type == 'photo_essay' || is_home() ) && !uses_custom_template( $post ) ) ) {
$fonts = unserialize( TEMPLATE_FONT_URLS );
if ( $fonts ) {
foreach ( $fonts as $name => $url ) {
// Our font handles should all use the naming convention
// `font-[slug]`. Try to create a stylesheet handle on the
// fly and compare with it, since `wp_style_is()` can't
// compare via asset URL:
$approximated_handle = 'font-' . sanitize_title( $name );
// Try to avoid loading fonts twice:
if ( ! wp_style_is( $approximated_handle, 'enqueued' ) ) {
$url = cache_bust_url( $url );
$output .= '<link rel="stylesheet" href="'.$url.'" type="text/css" media="all" />';
}
}
}
$output .= '<style type="text/css">';
$output .= get_all_font_classes();
$output .= '</style>';
}
// Page stylesheet
if ( $post->post_type == 'page' ) {
$page_stylesheet_url = Page::get_stylesheet_url( $post );
if ( !empty( $page_stylesheet_url ) ) {
$page_stylesheet_url = cache_bust_url( $page_stylesheet_url );
$output .= '<link rel="stylesheet" href="'.$page_stylesheet_url.'" type="text/css" media="all" />';
}
}
if (!is_search() && !is_404()) {
// 1. Set necessary html, body element width+height for stories, issues
$output .= '<style type="text/css">';
$output .= '
html, body {
height: 100%;
width: 100%;
}
@media (max-width: 767px) {
html, body {
width: auto;
}
}
';
$output .= '</style>';
// 2. Story font declarations (default and custom templates)
if ($post->post_type == 'story') {
// Custom stories
if (uses_custom_template($post)) {
$story_fonts = get_post_meta($post->ID, 'story_fonts', TRUE);
if (!empty($story_fonts)) {
$fonts = explode(',', $story_fonts);
$available_fonts = unserialize(CUSTOM_AVAILABLE_FONTS);
foreach ($fonts as $font) {
trim($font);
if (array_key_exists($font, $available_fonts)) {
$font_url = cache_bust_url( $available_fonts[$font] );
$output .= '<link rel="stylesheet" href="'.$font_url.'" type="text/css" media="all" />';
}
}
}
// Default template stories
} else {
$font = get_default_template_font_styles( $post );
$output .= '<style type="text/css">';
if ( function_exists( 'get_default_template_font_css' ) ) {
$output .= get_default_template_font_css( $font ); // override per version
}
$output .= '</style>';
}
}
// 3. Custom issue page-specific stylesheet
if ( (is_home() || $post->post_type == 'issue') && (uses_custom_template($post)) ) {
$home_stylesheet_url = cache_bust_url( Issue::get_home_stylesheet_url($post) );
$dev_issue_home_directory = get_post_meta($post->ID, 'issue_dev_home_asset_directory', TRUE);
if (!empty($home_stylesheet_url)) {
$output .= '<link rel="stylesheet" href="'.$home_stylesheet_url.'" type="text/css" media="all" />';
}
elseif ( DEV_MODE == 1 && !empty($dev_issue_home_directory) ) {
$dev_home_stylesheet_url = cache_bust_url( THEME_DEV_URL.'/'.$dev_issue_home_directory.'issue-cover.css' );
if (curl_exists($dev_home_stylesheet_url)) {
$output .= '<link rel="stylesheet" href="'.$dev_home_stylesheet_url.'" type="text/css" media="all" />';
}
}
}
// 4. Custom story stylesheet
if( $post->post_type == 'story' && uses_custom_template($post) ) {
$story_stylesheet_url = cache_bust_url( Story::get_stylesheet_url($post) );
$dev_issue_directory = get_post_meta($post->ID, 'story_dev_directory', TRUE);
if ( !empty($story_stylesheet_url) ) {
$output .= '<link rel="stylesheet" href="'.$story_stylesheet_url.'" type="text/css" media="all" />';
}
elseif ( (DEV_MODE == 1) && !empty($dev_issue_directory) ) {
$dev_story_stylesheet_url = cache_bust_url( THEME_DEV_URL.'/'.$dev_issue_directory.$post->post_name.'.css' );
if (curl_exists($dev_story_stylesheet_url)) {
$output .= '<link rel="stylesheet" href="'.$dev_story_stylesheet_url.'" type="text/css" media="all" />';
}
}
}
}
return $output;
}
/*
* HTTPS-friendly wp_get_attachment_url
*/
function protocol_relative_attachment_url($url) {
if (is_ssl()) {
$url = str_replace('http://', 'https://', $url);
}
return $url;
}
add_filter('wp_get_attachment_url', 'protocol_relative_attachment_url');
/**
* Prevent WordPress from wrapping images with captions with a
* [caption] shortcode in versions 5-.
**/
function pegasus_disable_captions() {
$version = get_relevant_version();
if ( $version <= 5 ) {
return true;
}
return false;
}
add_filter( 'disable_captions', 'pegasus_disable_captions' );
/**
* Whether or not the current story or issue requires a custom template.
**/
function uses_custom_template($post) {
$meta_field = $post->post_type.'_template';
$template = get_post_meta($post->ID, $meta_field, TRUE);
if ( $template && !empty($template) && $template == 'custom') {
return true;
}
return false;
}
/**
* Displays an issue cover or story contents. Accounts for whether or
* not Developer Mode is on/off and what story/issue template is set.
*
* Note: files pulled via file_get_contents() here should be requested over http.
*
* Markup priority: Uploaded HTML File -> WYSIWYG editor content -> dev directory content
**/
function display_markup_or_template($post) {
if ($post->post_type == 'issue') {
$dev_directory = get_post_meta($post->ID, 'issue_dev_home_asset_directory', TRUE);
$dev_directory_html_url = str_replace('https', 'http', THEME_DEV_URL.'/'.$dev_directory.'issue-cover.html');
}
else {
$dev_directory = get_post_meta($post->ID, $post->post_type.'_dev_directory', TRUE);
$dev_directory_html_url = str_replace('https', 'http', THEME_DEV_URL.'/'.$dev_directory.$post->post_name.'.html');
}
$post_template = get_post_meta($post->ID, $post->post_type.'_template', TRUE);
$uploaded_html = get_post_meta($post->ID, $post->post_type.'_html', TRUE);
$uploaded_html_url = wp_get_attachment_url($uploaded_html);
if ($uploaded_html_url) {
$uploaded_html_url = str_replace('https', 'http', $uploaded_html_url);
}
// If developer mode is on and this story/issue is custom,
// try to use dev directory contents:
if (
DEV_MODE == 1 &&
empty($post->post_content) &&
$dev_directory !== False &&
uses_custom_template($post)
) {
add_filter('the_content', 'kill_empty_p_tags', 999);
// Uploaded HTML file should always take priority over dev directory contents
if (!empty($uploaded_html) && !empty($uploaded_html_url)) {
print apply_filters( 'the_content', wp_remote_retrieve_body( wp_remote_get( $uploaded_html_url ) ) );
}
else {
if (curl_exists($dev_directory_html_url)) {
$content = wp_remote_retrieve_body( wp_remote_get( $dev_directory_html_url ) );
print apply_filters('the_content', $content);
}
}
}
else {
// Check the set post template. Note that if this value is set to 'default'
// it is saved in the database as an empty value.
if (!empty($post_template)) {
switch ($post_template) {
case 'custom':
// Kill automatic <p> tag insertion if this isn't an old story.
// Don't want to accidentally screw up an old story that worked