/
webcomic.php
executable file
·1321 lines (1159 loc) · 54.6 KB
/
webcomic.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
/*
Text Domain: webcomic
Plugin Name: Webcomic
Plugin URI: http://webcomic.nu
Description: Comic publishing power for the web.
Version: 4
Author: Michael Sisk
Author URI: http://mgsisk.com
License: GPL2
Copyright 2008 - 2012 Michael Sisk (contact@mgsisk.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
/** Comic Publishing Power for the Web
*
* @todo Future Bulk media actions; see core.trac.wordpress.org/ticket/16031
* @todo Future Screen Options; see core.trac.wordpress.org/ticket/18323
*
* @package Webcomic
* @copyright 2008 - 2012 Michael Sisk
* @license //gnu.org/licenses/gpl-2.0.html GPL2
* @version 4
* @link http://webcomic.nu
*/
/** Initialize the plugin.
*
* @package Webcomic
*/
class Webcomic {
/** Internal version number.
* @var string
*/
protected static $version = '4';
/** Absolute path to the Webcomic directory.
* @var string
*/
protected static $dir = '';
/** URL to the Webcomic directory.
* @var string
*/
protected static $url = '';
/** Stores error notifications.
* @var array
*/
protected static $error = array();
/** Stores success notifications.
* @var array
*/
protected static $notice = array();
/** Stores the configuration.
* @var array
*/
protected static $config = array();
/** Whether to attempt integration with the active theme.
* @var boolean
*/
protected static $integrate = false;
/** Stores the collection the current page is related to.
* @var string
*/
protected static $collection = '';
/** Set class properties and register hooks.
*
* @uses Webcomic::$dir
* @uses Webcomic::$url
* @uses Webcomic::$config
* @uses Webcomic::init()
* @uses Webcomic::buffer_alert()
* @uses Webcomic::get_term()
* @uses Webcomic::get_terms()
* @uses Webcomic::get_the_terms()
* @uses Webcomic::post_type_link()
* @uses Webcomic::get_object_terms()
* @uses Webcomic::get_attachment_image_attributes()
* @uses Webcomic::head()
* @uses Webcomic::the_post()
* @uses Webcomic::setup_theme()
* @uses Webcomic::enqueue_scripts()
* @uses Webcomic::template_redirect()
* @uses Webcomic::theme()
* @uses Webcomic::request()
* @uses Webcomic::the_posts()
* @uses Webcomic::body_class()
* @uses Webcomic::the_content_feed()
* @uses Webcomic::extra_theme_headers()
* @uses Webcomic::loop_start()
* @uses Webcomic::the_content()
* @uses WebcomicShortcodes
* @uses WebcomicWidgets
*/
public function __construct() {
self::$dir = plugin_dir_path( __FILE__ );
self::$url = plugin_dir_url( __FILE__ );
self::$config = get_option( 'webcomic_options' );
if ( self::$config and version_compare( self::$config[ 'version' ], '4x', '>=' ) ) {
add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'log_ipn' ) );
add_action( 'init', array( $this, 'twitter_oauth' ) );
add_action( 'init', array( $this, 'save_transcript' ) );
add_action( 'init', array( $this, 'random_redirect' ) );
add_action( 'webcomic_buffer_alert', array( $this, 'buffer_alert' ) );
add_filter( 'get_term', array( $this, 'get_term' ), 10, 2 );
add_filter( 'get_terms', array( $this, 'get_terms' ), 10, 3 );
add_filter( 'get_the_terms', array( $this, 'get_the_terms' ), 10, 3 );
add_action( 'post_type_link', array( $this, 'post_type_link' ), 10, 4 );
add_filter( 'wp_get_object_terms', array( $this, 'get_object_terms' ), 10, 4 );
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'get_attachment_image_attributes' ), 10, 2 );
if ( !is_admin() ) {
add_action( 'wp_head', array( $this, 'head' ), 1 );
add_action( 'the_post', array( $this, 'the_post' ), 10, 1 );
add_action( 'setup_theme', array( $this, 'setup_theme' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
add_filter( 'template', array( $this, 'theme' ), 10, 1 );
add_filter( 'request', array( $this, 'request' ), 10, 1 );
add_filter( 'stylesheet', array( $this, 'theme' ), 10, 1 );
add_filter( 'the_posts', array( $this, 'the_posts' ), 10, 2 );
add_filter( 'body_class', array( $this, 'body_class' ), 10, 2 );
add_filter( 'the_content_feed', array( $this, 'the_content_feed' ), 10, 1 );
add_filter( 'extra_theme_headers', array( $this, 'extra_theme_headers' ), 10, 1 );
if ( self::$config[ 'integrate' ] ) {
add_action( 'loop_end', array( $this, 'loop_end' ), 10, 1 );
add_action( 'loop_start', array( $this, 'loop_start' ), 10, 1 );
add_action( 'the_excerpt', array( $this, 'the_excerpt' ), 10, 1 );
add_action( 'the_content', array( $this, 'the_content' ), 10, 1 );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 );
}
require_once self::$dir . '-/php/tags.php';
require_once self::$dir . '-/php/shortcodes.php'; new WebcomicShortcode;
}
require_once self::$dir . '-/php/widgets.php'; new WebcomicWidgets;
}
}
/** Initialize major plugin features.
*
* This hook performs a number of important functions, including
* loading the plugin text domain, defining the 'prints' and
* 'transcripts' rewrite endpoints, and registering custom image
* sizes, post types, and taxonomies.
*
* @uses Webcomic::$dir
* @uses Webcomic::$url
* @uses Webcomic::$config
* @uses Webcomic::log_ipn()
* @uses Webcomic::save_transcript()
* @uses Webcomic::random_redirect()
* @hook init
*/
public function init() {
load_plugin_textdomain( 'webcomic', '', self::$dir . '-/locale/' );
define( 'EP_WEBCOMIC', 524288 );
add_rewrite_endpoint( 'prints', EP_WEBCOMIC );
add_rewrite_endpoint( 'transcripts', EP_WEBCOMIC );
foreach ( self::$config[ 'sizes' ] as $k => $v ) {
add_image_size( $k, $v[ 'width' ], $v[ 'height' ], $v[ 'crop' ] );
}
foreach ( self::$config[ 'collections' ] as $k => $v ) {
register_post_type( $k, array(
'labels' => array(
'name' => $v[ 'name' ],
'singular_name' => sprintf( __( '%s Webcomic', 'webcomic' ), esc_html( $v[ 'name' ] ) ),
'add_new' => __( 'Add New', 'webcomic' ),
'add_new_item' => __( 'Add New Webcomic', 'webcomic' ),
'edit_item' => __( 'Edit Webcomic', 'webcomic' ),
'new_item' => __( 'New Webcomic', 'webcomic' ),
'all_items' => __( 'All Webcomics', 'webcomic' ),
'view_item' => __( 'View Webcomic', 'webcomic' ),
'search_items' => __( 'Search Webcomics', 'webcomic' ),
'not_found' => __( 'No webcomics found.', 'webcomic' ),
'not_found_in_trash' => __( 'No webcomics found in Trash.', 'webcomic' )
),
'description' => esc_html( $v[ 'description' ] ),
'public' => true,
'menu_icon' => self::$url . '-/img/webcomic-small.png',
'supports' => $v[ 'supports' ],
'taxonomies' => $v[ 'taxonomies' ],
'has_archive' => $v[ 'slugs' ][ 'archive' ],
'permalink_epmask' => EP_WEBCOMIC,
'rewrite' => array(
'slug' => $v[ 'slugs' ][ 'webcomic' ],
'with_front' => false
)
) );
register_taxonomy( "{$k}_storyline", $k, array(
'labels' => array(
'name' => sprintf( __( '%s Storylines', 'webcomic' ), esc_html( $v[ 'name' ] ) ),
'singular_name' => __( 'Storyline', 'webcomic' ),
'search_items' => __( 'Search Storylines', 'webcomic' ),
'popular_items' => __( 'Popular Storylines', 'webcomic' ),
'all_items' => __( 'All Storylines', 'webcomic' ),
'parent_item' => __( 'Parent Storyline', 'webcomic' ),
'parent_item_colon' => __( 'Parent Storyline:', 'webcomic' ),
'edit_item' => __( 'Edit Storyline', 'webcomic' ),
'update_item' => __( 'Update Storyline', 'webcomic' ),
'add_new_item' => __( 'Add New Storyline', 'webcomic' ),
'new_item_name' => __( 'New Storyline Name', 'webcomic' ),
'menu_name' => __( 'Storylines', 'webcomic' )
),
'hierarchical' => true,
'rewrite' => array(
'slug' => $v[ 'slugs' ][ 'storyline' ],
'with_front' => false,
'hierarchical' => true
)
) );
register_taxonomy( "{$k}_character", $k, array(
'labels' => array(
'name' => sprintf( __( '%s Characters', 'webcomic' ), esc_html( $v[ 'name' ] ) ),
'singular_name' => __( 'Character', 'webcomic' ),
'search_items' => __( 'Search Characters', 'webcomic' ),
'popular_items' => __( 'Popular Characters', 'webcomic' ),
'all_items' => __( 'All Characters', 'webcomic' ),
'edit_item' => __( 'Edit Character', 'webcomic' ),
'update_item' => __( 'Update Character', 'webcomic' ),
'add_new_item' => __( 'Add New Character', 'webcomic' ),
'new_item_name' => __( 'New Character Name', 'webcomic' ),
'separate_items_with_commas' => __( 'Separate characters with commas', 'webcomic' ),
'add_or_remove_items' => __( 'Add or remove characters', 'webcomic' ),
'choose_from_most_used' => __( 'Choose from the most used characters', 'webcomic' ),
'menu_name' => __( 'Characters', 'webcomic' )
),
'rewrite' => array(
'slug' => $v[ 'slugs' ][ 'character' ],
'with_front' => false
)
) );
}
register_post_type( 'webcomic_transcript', array(
'labels' => array(
'name' => __( 'Webcomic Transcripts', 'webcomic' ),
'singular_name' => __( 'Webcomic Transcript', 'webcomic' ),
'add_new' => __( 'Add New', 'webcomic' ),
'add_new_item' => __( 'Add New Transcript', 'webcomic' ),
'edit_item' => __( 'Edit Transcript', 'webcomic' ),
'new_item' => __( 'New Transcript', 'webcomic' ),
'all_items' => __( 'All Transcripts', 'webcomic' ),
'view_item' => __( 'View Transcript', 'webcomic' ),
'search_items' => __( 'Search Transcripts', 'webcomic' ),
'not_found' => __( 'No transcripts found.', 'webcomic' ),
'not_found_in_trash' => __( 'No transcripts found in Trash.', 'webcomic' )
),
'public' => false,
'show_ui' => true,
'exclude_from_search' => false,
'supports' => array( 'editor', 'author', 'revisions' ),
'menu_icon' => self::$url . '-/img/transcript-small.png'
) );
register_taxonomy( 'webcomic_language', 'webcomic_transcript', array(
'labels' => array(
'name' => __( 'Webcomic Transcript Languages', 'webcomic' ),
'singular_name' => __( 'Language', 'webcomic' ),
'search_items' => __( 'Search Languages', 'webcomic' ),
'popular_items' => __( 'Popular Lanuages', 'webcomic' ),
'all_items' => __( 'All Languages', 'webcomic' ),
'edit_item' => __( 'Edit Language', 'webcomic' ),
'update_item' => __( 'Update Language', 'webcomic' ),
'add_new_item' => __( 'Add New Language', 'webcomic' ),
'new_item_name' => __( 'New Language Name', 'webcomic' ),
'separate_items_with_commas' => __( 'Separate languages with commas', 'webcomic' ),
'add_or_remove_items' => __( 'Add or remove languages', 'webcomic' ),
'choose_from_most_used' => __( 'Choose from the most used languages', 'webcomic' ),
'menu_name' => __( 'Languages', 'webcomic' )
),
'rewrite' => false,
'show_in_nav_menus' => false
) );
}
/** Email buffer alert notifications.
*
* Hooks into the webcomic_buffer_alert event scheduled during
* installation. As with all WordPress scheduled events this
* requires someone actually visiting the site to trigger the event
* and send a buffer alert email.
*
* @uses Webcomic::$config
* @hook webcomic_buffer_alert
*/
public function buffer_alert() {
global $wpdb;
$now = ( integer ) current_time( 'timestamp' );
foreach ( self::$config[ 'collections' ] as $k => $v ) {
if ( $v[ 'buffer' ][ 'hook' ] and $buffer = strtotime( $wpdb->get_var( $wpdb->prepare( "SELECT post_date FROM $wpdb->posts WHERE post_type = '%s' AND post_status = 'future' ORDER BY post_date DESC", $k ) ) ) and $eta = floor( ( $buffer - $now ) / 86400 ) and $eta <= $v[ 'buffer' ][ 'days' ] ) {
wp_mail(
$v[ 'buffer' ][ 'email' ],
sprintf( _n( '[%s] %s Buffer Alert - %s Day Left', '[%s] %s Buffer Alert - %s Days Left', $eta, 'webcomic' ), get_bloginfo( 'name' ), $v[ 'name' ], $eta ),
sprintf( __( 'This is an automated reminder that the buffer for %1$s expires on %2%s.', 'webcomic' ),
sprintf( '<a href="%s">%s</a>', esc_url( admin_url( "edit.php?post_type={$k}" ) ), $v[ 'name' ] ),
date( 'j F Y', $buffer )
),
'content-type: text/html'
);
}
}
}
/** Add a webcomic_image property to term objects.
*
* @param object $term Retrieved term.
* @param string $taxonomy Taxonomy the term belongs to.
* @return object
* @uses Webcomic::$config
* @hook get_term
*/
public function get_term( $term, $taxonomy ) {
if ( preg_match( '/^webcomic\d+_(storyline|character)$/', $taxonomy ) ) {
$term->webcomic_image = empty( self::$config[ 'terms' ][ $term->term_id ][ 'image' ] ) ? 0 : self::$config[ 'terms' ][ $term->term_id ][ 'image' ];
}
return $term;
}
/** Add a webcomic_image property to term objects.
*
* @param array $terms Array of retrieved terms.
* @param array $taxonomies Array of taxonomies the terms belong to.
* @param array $args Additional arguments passed to get_terms().
* @return array
* @uses Webcomic::$config
* @hook get_terms
*/
public function get_terms( $terms, $taxonomies, $args ) {
if ( preg_match( '/webcomic\d+_(storyline|character)/', join( ' ', ( array ) $taxonomies ) ) ) {
foreach ( $terms as $k => $v ) {
if ( isset( $v->taxonomy ) and preg_match( '/^webcomic\d+_(storyline|character)$/', $v->taxonomy ) ) {
$terms[ $k ]->webcomic_image = empty( self::$config[ 'terms' ][ $v->term_id ][ 'image' ] ) ? 0 : self::$config[ 'terms' ][ $v->term_id ][ 'image' ];
}
}
}
return $terms;
}
/** Add a webcomic_image property to term objects.
*
* @param array $terms Array of retrieved terms.
* @param integer $id Object ID the terms are related to.
* @param string $taxonomy Taxonomy the terms belong to.
* @return array
* @uses Webcomic::$config
* @hook get_the_terms
*/
public function get_the_terms( $terms, $id, $taxonomy ) {
if ( preg_match( '/^webcomic\d+_(storyline|character)$/', $taxonomy ) ) {
foreach ( $terms as $k => $v ) {
$terms[ $k ]->webcomic_image = empty( self::$config[ 'terms' ][ $v->term_id ][ 'image' ] ) ? 0 : self::$config[ 'terms' ][ $v->term_id ][ 'image' ];
}
}
return $terms;
}
/** Handle custom permalink tokens.
*
* @todo Future: Use get_term_parents; see core.trac.wordpress.org/ticket/17069
*
* @param string $link Permalink to swap tokens in.
* @param object $post Post object.
* @param boolean $name Whether to keep post name.
* @param boolean $sample Whether this is a sample permalink.
* @return string
* @uses Webcomic::$config
* @hook post_type_link
*/
public function post_type_link( $link, $post, $name, $sample ) {
if ( empty( self::$config[ 'collections' ][ $post->post_type ] ) or false === strpos( $link, '%' ) ) {
return $link;
}
if ( false !== strpos( $link, "%{$post->post_type}_storyline%" ) and $storylines = get_the_terms( $post->ID, "{$post->post_type}_storyline" ) and !is_wp_error( $storylines ) ) {
$storylines = array_reverse( $storylines );
$storyline = $storylines[ 0 ]->slug;
if ( $parent = $storylines[ 0 ]->parent and $parents = get_ancestors( $storylines[ 0 ]->term_id, $storylines[ 0 ]->taxonomy ) ) {
$storyline = array();
foreach ( $parents as $parent ) {
$the_parent = get_term( $parent, $storylines[ 0 ]->taxonomy );
$storylines[] = $the_parent->slug;
}
$storyline = array_reverse( $storylines );
$storyline[] = $storylines[ 0 ]->slug;
$storyline = join( '/', $storyline );
}
} else {
$storyline = '';
}
$time = explode( ' ', date( 'Y m d H i s', strtotime( $post->post_date ) ) );
$tokens = array(
'%year%' => $time[ 0 ],
'%monthnum%' => $time[ 1 ],
'%day%' => $time[ 2 ],
'%hour%' => $time[ 3 ],
'%minute%' => $time[ 4 ],
'%second%' => $time[ 5 ],
'%post_id%' => $post->ID,
'%author%' => false !== strpos( $link, '%author%' ) ? get_userdata( $post->post_author )->user_nicename : '',
"%{$post->post_type}_storyline%" => $storyline
);
return str_replace( array_keys( $tokens ), $tokens, $link );
}
/** Add a webcomic_image property to term objects.
*
* @param array $terms Array of retrieved terms.
* @param integer $objects Object ID's the terms are related to.
* @param string $taxonomies Taxonomy the terms belong to.
* @param array $args Arguments passed to wp_get_object_terms().
* @return array
* @uses Webcomic::$config
* @hook get_the_terms
*/
public function get_object_terms( $terms, $objects, $taxonomies, $args ) {
if ( 'all' === $args[ 'fields' ] and preg_match( '/webcomic\d+_(storyline|character)/', join( ' ', ( array ) $taxonomies ) ) ) {
foreach ( $terms as $k => $v ) {
if ( isset( $v->taxonomy ) and preg_match( '/^webcomic\d+_(storyline|character)$/', $v->taxonomy ) ) {
$terms[ $k ]->webcomic_image = empty( self::$config[ 'terms' ][ $v->term_id ][ 'image' ] ) ? 0 : self::$config[ 'terms' ][ $v->term_id ][ 'image' ];
}
}
}
return $terms;
}
/** Use caption for title of media objects attached to a webcomic.
*
* @param array $attributes An array of media attributes.
* @param object $attachment The media object.
* @return array
* @uses Webcomic::$config
* @hook wp_get_attachment_image_attributes
*/
public function get_attachment_image_attributes( $attributes, $attachment ) {
if ( $attachment->post_parent and $attachment->post_excerpt and isset( self::$config[ 'collections' ][ get_post_type( $attachment->post_parent ) ] ) ) {
$attributes[ 'title' ] = esc_attr( trim( strip_tags( $attachment->post_excerpt ) ) );
}
return $attributes;
}
/** Add Open Graph metadata for Webcomic-related pages.
*
* Use of the 'property' attribute is obnoxious but intentional; see
* [ogp.me](http://ogp.me/) for details on the Open Graph protocol.
*
* @uses Webcomic::$config
* @uses Webcomic::$collection
* @uses Webcomic::get_attachments()
* @hook wp_head
* @filter array webcomic_opengraph Filters the Open Graph data Webcomic adds to all Webcomic-related pages.
*/
public function head() {
global $wp_query, $post;
if ( self::$collection ) {
$object = $wp_query->get_queried_object();
$output = array();
$output[ 'og:type' ] = empty( $object->post_type ) ? 'website' : 'article';
$output[ 'og:site_name' ] = get_bloginfo( 'name' );
if ( is_singular() ) {
setup_postdata( $post );
$output[ 'og:url' ] = get_permalink();
$output[ 'og:title' ] = esc_attr( get_the_title() );
$output[ 'og:description' ] = esc_attr( strip_tags( wp_trim_excerpt( get_the_excerpt() ) ) );
$output[ 'article:author' ] = esc_url( get_author_posts_url( $object->post_author ) );
$output[ 'article:published_time' ] = get_the_time( 'c' );
$output[ 'article:modified_time' ] = get_the_modified_time( 'c' );
$output[ 'article:section' ] = esc_attr( self::$config[ 'collections' ][ self::$collection ][ 'name' ] );
$output[ 'article:tag' ] = array();
if ( isset( self::$config[ 'collections' ][ $object->post_type ] ) ) {
foreach ( array_merge( ( array ) get_the_terms( $object->ID, "{$object->post_type}_storyline" ), ( array ) get_the_terms( $object->ID, "{$object->post_type}_character" ) ) as $term ) {
if ( isset( $term->name ) ) {
$output[ 'article:tag' ][] = esc_attr( $term->name );
}
}
}
} else if ( is_tax() ) {
$output[ 'og:url' ] = get_term_link( ( integer ) $object->term_id, $object->taxonomy );
$output[ 'og:title' ] = esc_attr( single_term_title( '', false ) );
$output[ 'og:description' ] = esc_attr( strip_tags( term_description( $object->term_id, $object->taxonomy ) ) );
} else {
$output[ 'og:url' ] = get_post_type_archive_link( $object->name );
$output[ 'og:title' ] = esc_attr( self::$config[ 'collections' ][ $object->name ][ 'name' ] );
$output[ 'og:description' ] = esc_attr( strip_tags( self::$config[ 'collections' ][ $object->name ][ 'description' ] ) );
}
if ( is_singular() and isset( self::$config[ 'collections' ][ $object->post_type ] ) and $attachments = self::get_attachments( $object->ID ) ) {
$output[ 'og:image' ] = array();
foreach ( $attachments as $attachment ) {
$attributes = wp_get_attachment_image_src( $attachment->ID );
$output[ 'og:image' ][] = $attributes[ 0 ];
}
} else if ( is_tax() and isset( self::$config[ 'terms' ][ $object->term_id ][ 'image' ] ) ) {
$attributes = wp_get_attachment_image_src( self::$config[ 'terms' ][ $object->term_id ][ 'image' ] );
$output[ 'og:image' ] = $attributes[ 0 ];
} else if ( self::$config[ 'collections' ][ self::$collection ][ 'image' ] ) {
$attributes = wp_get_attachment_image_src( self::$config[ 'collections' ][ self::$collection ][ 'image' ] );
$output[ 'og:image' ] = $attributes[ 0 ];
}
$output = apply_filters( 'webcomic_opengraph', $output, $object, self::$collection );
foreach ( ( array ) $output as $k => $v ) {
if ( is_array( $v ) ) {
foreach( $v as $b ) {
echo sprintf( '<meta property="%s" content="%s">', $k, $b ), "\n";
}
} else {
echo sprintf( '<meta property="%s" content="%s">', $k, $v ), "\n";
}
}
}
}
/** Filter titles and content for restricted webcomics.
*
* @param object $post The global post object.
* @uses WebcomicTag::verify_webcomic_age()
* @uses WebcomicTag::verify_webcomic_role()
* @hook the_post
*/
public function the_post( $post ) {
global $pages;
if ( isset( self::$config[ 'collections' ][ $post->post_type ] ) ) {
if ( !WebcomicTag::verify_webcomic_role( $post->post_type ) ) {
$post->post_title = __( 'Restricted Content', 'webcomic' );
$pages = array( is_user_logged_in() ? __( "You don't have permission to view this content.", 'webcomic' ) : sprintf( __( 'You must <a href="%s">log in</a> to view this content.', 'webcomic' ), wp_login_url( get_permalink( $post->ID ) ) ) );
$post->content = $pages[ 0 ];
$post->ID = 0;
} else if ( !$clear = WebcomicTag::verify_webcomic_age( $post->post_type ) ) {
$post->post_title = __( 'Restricted Content', 'webcomic' );
$pages = array( is_null( $clear ) ? sprintf( __( 'Please <a href="%s">verify your age</a> to view this content.', 'webcomic' ), get_permalink( $post->ID ) ) : __( "You don't have permission to view this content.", 'webcomic' ) );
$post->content = $pages[ 0 ];
$post->ID = 0;
}
}
}
/** Check to see if the current page is webcomic-related.
*
* We have to do this as early as possible to ensure that the
* correct template can be set if the collection is using a custom
* theme. It'd be better to use `wp_get_theme` for the integration
* check, but custom headers can't be retrieved this way since
* WordPress 3.4.
*
* @uses Webcomic::$config
* @uses Webcomic::$integrate
* @uses Webcomic::$collection
* @hook setup_theme
*/
public function setup_theme() {
global $wp_rewrite;
$match = $permalinks = array();
if ( $wp_rewrite->using_permalinks() ) {
foreach ( self::$config[ 'collections' ] as $k => $v ) {
$permalinks[ "{$k}_archive" ] = $v[ 'slugs' ][ 'archive' ];
$permalinks[ "{$k}_webcomic" ] = $v[ 'slugs' ][ 'webcomic' ];
$permalinks[ "{$k}_storyline" ] = $v[ 'slugs' ][ 'storyline' ];
$permalinks[ "{$k}_character" ] = $v[ 'slugs' ][ 'character' ];
}
}
if (
(
preg_match( '/webcomic\d+(_(storyline|character))?/', join( ' ', array_keys( $_GET ) ), $match )
or ( isset( $_GET[ 'post_type' ] ) and isset( self::$config[ 'collections' ][ $_GET[ 'post_type' ] ] ) and $match[ 0 ] = $_GET[ 'post_type' ] )
or ( $wp_rewrite->using_permalinks() and preg_match( sprintf( '{/(%s)/}', join( '|', $permalinks ) ), $_SERVER[ 'REQUEST_URI' ], $match ) )
or ( $id = url_to_postid( $_SERVER[ 'REQUEST_URI' ] ) and $match[ 0 ] = get_post_meta( $id, 'webcomic_collection', true ) and isset( self::$config[ 'collections' ][ $match[ 0 ] ] ) )
)
and $match
) {
$match[ 0 ] = preg_replace( '/_(storyline|character)$/', '', $match[ 0 ] );
self::$collection = empty( self::$config[ 'collections' ][ $match[ 0 ] ] ) ? preg_replace( '/_(archive|webcomic|storyline|character)$/', '', array_search( $match[ 1 ], $permalinks ) ) : $match[ 0 ];
}
$integrate = get_file_data( get_stylesheet_directory() . '/style.css', array( 'webcomic' => 'Webcomic' ) );
self::$integrate = empty( $integrate[ 'webcomic' ] );
}
/** Register and enqueue javascript.
*
* @uses Webcomic::$config
* @hook wp_enqueue_scripts
*/
public function enqueue_scripts() {
if ( self::$config[ 'shortcuts' ] ) {
wp_register_script( 'webcomic-shortcuts', self::$url . '-/js/shortcuts.js', array( 'jquery', 'jquery-hotkeys' ), false, true );
wp_enqueue_script( 'webcomic-shortcuts' );
}
if ( self::$config[ 'dynamic' ] ) {
wp_register_script( 'jquery-bbq', self::$url . '-/library/jquery.bbq.js', array( 'jquery' ), false, true );
wp_register_script( 'webcomic-dynamic', self::$url . '-/js/dynamic.js', array( 'jquery', 'jquery-bbq' ), false, true );
wp_enqueue_script( 'webcomic-dynamic' );
}
wp_register_script( 'webcomic-dropdown', self::$url . '-/js/dropdown.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'webcomic-dropdown' );
}
/** Perform various checks if the current page is Webcomic-related.
*
* If the collection is role-restricted we verify the users role.
* Restricted users are kicked to the role-restricted template. If
* the template doesn't exist we die with an error.
*
* If the collection is age-restricted we verify the user's age.
* Users of unknown age or underage users are kicked to the
* age-restricted template. If neither template exists we die with
* an error.
*
* If "prints" has been set we're most likely looking at a Webcomic
* prints page, so we load the prints template if it's available.
*
* If an Ajax request has been made for a single webcomic post we
* load the dynamic webcomic template or fall back to the automatic
* integration template if no dynamic template is available.
*
* @uses Webcomic::$collection
* @uses WebcomicTag::verify_webcomic_age()
* @uses WebcomicTag::verify_webcomic_role()
* @hook template_redirect
* @template role-restricted-{$collection}.php, role-restricted.php, restricted-{$collection}.php, restricted.php
* @template age-restricted-{$collection}.php, age-restricted.php, restricted-{$collection}.php, restricted.php
* @template prints-{$post->ID}.php, prints-{$post->post_name}.php, prints-{$collection}.php, prints.php
* @template dynamic-{$container}-{$collection}.php, dynamic-{$container}.php, dynamic-{$collection}.php, dynamic.php
*/
public function template_redirect() {
global $wp_query, $post;
if ( self::$collection ) {
$collection = self::$collection;
if ( !WebcomicTag::verify_webcomic_role( $collection ) ) {
if ( !locate_template( array( "webcomic/role-restricted-{$collection}.php", 'webcomic/role-restricted.php', "webcomic/restricted-{$collection}.php", 'webcomic/restricted.php' ), true, false ) ) {
wp_die( is_user_logged_in() ? __( "You don't have permission to view this content.", 'webcomic' ) : sprintf( __( 'Please <a href="%s">log in</a> to view this content.', 'webcomic' ), wp_login_url( $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ] ) ), __( 'Restricted Content | Webcomic', 'webcomic' ), 401 );
}
die;
}
if ( !$clear = WebcomicTag::verify_webcomic_age( $collection ) ) {
if ( !locate_template( array( "webcomic/age-restricted-{$collection}.php", 'webcomic/age-restricted.php', "webcomic/restricted-{$collection}.php", 'webcomic/restricted.php' ), true, false ) ) {
wp_die( is_null( $clear ) ? sprintf( __( 'Please verify your age to view this content:%s', 'webcomic' ), sprintf( '<form method="post"><label>%s <input type="date" name="webcomic_birthday"></label><input type="submit"></form>', __( 'Birthday', 'webcomic' ) ) ) : __( "You don't have permission to view this content.", 'webcomic' ), __( 'Restricted Content | Webcomic', 'webcomic' ), 401 );
}
die;
}
if ( $post and isset( $wp_query->query_vars[ 'prints' ] ) and locate_template( array( "webcomic/prints-{$post->ID}.php", "webcomic/prints-{$post->post_name}.php", "webcomic/prints-{$collection}.php", 'webcomic/prints.php' ), true, false ) ) {
die;
}
if ( isset( $_GET[ 'webcomic_dynamic' ] ) and 'xmlhttprequest' === strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) ) {
setup_postdata( $post );
if ( !locate_template( array( "webcomic/dynamic-{$_GET[ 'webcomic_dynamic' ]}-{$collection}.php", "webcomic/dynamic-{$_GET[ 'webcomic_dynamic' ]}.php", "webcomic/dynamic-{$collection}.php", 'webcomic/dynamic.php' ), true, false ) ) {
require self::$dir . '-/php/integrate/dynamic.php';
}
die;
}
}
}
/** Return the appropriate theme ID for custom collection themes.
*
* @param string $theme Name of the current theme.
* @return string
* @uses Webcomic::$config
* @uses Webcomic::$collection
* @hook template
* @hook stylesheet
*/
public function theme( $theme ) {
return ( self::$collection and self::$config[ 'collections' ][ self::$collection ][ 'theme' ] ) ? self::$config[ 'collections' ][ self::$collection ][ 'theme' ] : $theme;
}
/** Integrate webcomics into the main site feed.
*
* @param array $query Post request query.
* @return array
* @uses Webcomic::$collection
* @hook request
*/
public function request( $query ) {
if ( isset( $query[ 'feed' ] ) and 1 === count( $query ) ) {
$post_type = array( 'post' );
foreach ( array_keys( self::$config[ 'collections' ] ) as $k ) {
if ( self::$config[ 'collections' ][ $k ][ 'feeds' ][ 'main' ] ) {
$post_type[] = $k;
}
}
if ( 1 < count( $post_type ) ) {
$query[ 'post_type' ] = $post_type;
}
}
return $query;
}
/** Display webcomics in place of transcripts in searches.
*
* @param array $posts Posts array.
* @param object $query WP_Query object.
* @return array
* @hook the_posts
*/
public function the_posts( $posts, $query ) {
if ( is_search() and $query->is_main_query() ) {
$stati = get_post_stati( array( 'public' => true ) );
foreach ( $posts as $k => $v ) {
if ( 'webcomic_transcript' === $v->post_type ) {
if ( $v->post_parent and in_array( get_post_status( $v->post_parent ), $stati ) ) {
$posts[ $k ] = get_post( $v->post_parent );
} else {
unset( $posts[ $k ] );
}
}
}
return array_values( array_unique( $posts, SORT_REGULAR ) );
}
return $posts;
}
/** Add webcomic classes to the body tag.
*
* @param array $classes Array of body classes.
* @param mixed $class Additional classes passed to body_class().
* @return array
* @uses Webcomic::$config
* @uses Webcomic::$collection
* @hook body_class
*/
public function body_class( $classes, $class ) {
if ( self::$collection ) {
$classes[] = 'webcomic';
$classes[] = esc_attr( sprintf( 'webcomic-%s', self::$config[ 'collections' ][ self::$collection ][ 'slugs' ][ 'name' ] ) );
}
return $classes;
}
/** Add webcomic previews to feed content.
*
* @param string $content The post content.
* @return string
* @uses Webcomic::$config
* @uses Webcomic:get_attachments()
* @hook the_content_feed
* @template feed-{$collectionID}.php, feed.php
*/
public function the_content_feed( $content ) {
global $post;
if ( !empty( self::$config[ 'collections' ][ $post->post_type ][ 'feeds' ][ 'hook' ] ) ) {
$prepend = $append = '';
$feed_size = self::$config[ 'collections' ][ $post->post_type ][ 'feeds' ][ 'size' ];
$attachments = self::get_attachments( $post->ID );
if ( $template = locate_template( array( "webcomic/feed-{$post->post_type}.php", 'webcomic/feed.php' ) ) ) {
require $template;
} else {
require self::$dir . '-/php/integrate/feed.php';
}
$content = $prepend . $content . $append;
}
return $content;
}
/** Add the 'Webcomic' key for theme headers.
*
* Theme authors can specify a theme as being 'Webcomic ready' by
* adding a Webcomic key with the minimum required version of
* Webcomic, like:
*
* `Webcomic: 4`
*
* @param array $extra Extra theme header fields.
* @return array
* @hook extra_theme_headers
*/
public function extra_theme_headers( $extra ) {
$extra[] = 'Webcomic';
return $extra;
}
/** Automagically integrate basic webcomic functionality.
*
* @param object $query WP_Query object for the loop.
* @return null
* @uses Webcomic::$dir
* @uses Webcomic::$config
* @uses Webcomic::$integrate
* @hook loop_start
* @action webcomic_loop_start Triggered during integration just before Webcomic content is prepended to the start of a WordPress loop.
* @template loop_start-{$collection}.php, loop_start.php
*/
public function loop_start( $query ) {
global $post;
if ( self::$integrate and $query->is_main_query() ) {
if ( is_front_page() and $webcomic = get_posts( array(
'numberposts' => 1,
'post_type' => array_keys( self::$config[ 'collections' ] )
) ) ) {
$post = $webcomic[ 0 ];
setup_postdata( $webcomic[ 0 ] );
} else if ( is_front_page() ) {
return;
}
do_action( 'webcomic_loop_start', self::$collection );
if ( !locate_template( array( 'webcomic/loop_start-' . self::$collection . '.php', 'webcomic/loop_start.php' ), true, false ) ) {
require self::$dir . '-/php/integrate/loop_start.php';
}
}
}
/** Automagically integrate basic webcomic functionality.
*
* @param object $query WP_Query object for the loop.
* @return null
* @uses Webcomic::$dir
* @uses Webcomic::$config
* @uses Webcomic::$integrate
* @hook loop_start
* @action webcomic_loop_end Triggered during integration just before Webcomic content is appended to the end of a WordPress loop.
* @template loop_end-{$collection}.php, loop_end.php
*/
public function loop_end( $query ) {
global $post;
if ( self::$integrate and $query->is_main_query() and is_singular( array_keys( self::$config[ 'collections' ] ) ) ) {
do_action( 'webcomic_loop_end', self::$collection );
if ( !locate_template( array( 'webcomic/loop_end=' . self::$collection . '.php', 'webcomic/loop_end.php' ), true, false ) ) {
require self::$dir . '-/php/integrate/loop_end.php';
}
}
}
/** Automagically integrate basic webcomic functionality.
*
* @param string $excerpt The post excerpt.
* @return string
* @uses Webcomic::$integrate
* @hook the_content
* @template the_excerpt-{$collection}.php, the_excerpt.php
*/
public function the_excerpt( $excerpt ) {
global $wp_query, $post;
if ( self::$integrate and in_the_loop() and $wp_query->is_main_query() and !is_feed() and $collection = get_post_type( $post ) and isset( self::$config[ 'collections' ][ $collection ] ) ) {
$prepend = $append = '';
if ( $template = locate_template( array( "webcomic/the_excerpt-{$collection}.php", 'webcomic/the_excerpt.php' ) ) ) {
require $template;
} else {
require self::$dir . '-/php/integrate/the_excerpt.php';
}
}
return $excerpt;
}
/** Automagically integrate basic webcomic functionality.
*
* @param string $content The post content.
* @return string
* @uses Webcomic::$integrate
* @hook the_content
* @template the_content-{$collection}.php, the_content.php
*/
public function the_content( $content ) {
global $wp_query, $post;
if ( self::$integrate and in_the_loop() and $wp_query->is_main_query() and !is_feed() and $collection = get_post_type( $post ) and isset( self::$config[ 'collections' ][ $collection ] ) ) {
$prepend = $append = '';
if ( $template = locate_template( array( "webcomic/the_content-{$collection}.php", 'webcomic/the_content.php' ) ) ) {
require $template;
} else {
require self::$dir . '-/php/integrate/the_content.php';
}
$content = $prepend . $content . $append;
}
return $content;
}
/** Automagically integrate basic webcomic functionality.
*
* Reorders webcomics on post type and taxonomy archive pages so
* that they appear in chronological order.
*
* @param object $query WP_Query object.
* @uses WebcomicTag::is_webcomic_archive()
* @uses WebcomicTag::is_webcomic_tax()
*/
public function pre_get_posts( $query ) {
if ( self::$integrate and $query->is_main_query() and ( WebcomicTag::is_webcomic_archive() or WebcomicTag::is_webcomic_tax() ) ) {
$query->set( 'order', 'ASC' );
}
}
/** Return Webcomic attachments for a post.
*
* @param integer $id Post ID to retrieve attachments for.
* @return array
*/
protected function get_attachments( $id = 0 ) {
return get_children( array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_parent' => $id,
'post_mime_type' => 'image'
) );
}
/** Handle PayPal IPN's.
*
* Logs instant payment notifications and updates the original print
* availability of webcomics as necessary. Logs are saved to
* /webcomic/-/logs/ipn-{$blog_id}.php
*
* https://www.paypal.com/cgi-bin/webscr | https://www.sandbox.paypal.com/cgi-bin/webscr
*
* @uses Webcomic::$config
* @action webcomic_ipn Triggered prior to processing a Paypal IPN request.
*/
public function log_ipn() {