forked from mgsisk/webcomic
/
webcomic.php
executable file
·1730 lines (1520 loc) · 72.4 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://github.com/mgsisk/webcomic
Description: Comic publishing power for the web.
Version: 4.3.2
Author: Michael Sisk
Author URI: http://mgsisk.com
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl.html
Copyright 2008 - 2015 Michael Sisk (me@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 3 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, see http://www.gnu.org/licenses.
*/
/**
* Initialize the plugin.
*
* @package Webcomic
*/
class Webcomic {
/**
* Internal version number.
* @var string
*/
protected static $version = "4.3.2";
/**
* Absolute path to the Webcomic directory.
* @var string
*/
protected static $dir = "";
/**
* URL to the Webcomic directory.
* @var string
*/
protected static $url = "";
/**
* Whether to enable debug mode.
* @var boolean
*/
protected static $debug = false;
/**
* 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::log_ipn()
* @uses Webcomic::wp_head()
* @uses Webcomic::twitter_oauth()
* @uses Webcomic::save_transcript()
* @uses Webcomic::dynamic_defaults()
* @uses Webcomic::webcomic_redirect()
* @uses Webcomic::setup_theme()
* @uses Webcomic::the_post()
* @uses Webcomic::pre_get_posts()
* @uses Webcomic::buffer_alert()
* @uses Webcomic::wp_enqueue_scripts()
* @uses Webcomic::tweet_webcomic()
* @uses Webcomic::request()
* @uses Webcomic::get_term()
* @uses Webcomic::the_posts()
* @uses Webcomic::get_terms()
* @uses Webcomic::body_class()
* @uses Webcomic::post_class()
* @uses Webcomic::get_the_terms()
* @uses Webcomic::post_type_link()
* @uses Webcomic::pre_option_template()
* @uses Webcomic::template_include()
* @uses Webcomic::the_content_feed()
* @uses Webcomic::wp_get_object_terms()
* @uses Webcomic::extra_theme_headers()
* @uses Webcomic::pre_option_stylesheet()
* @uses Webcomic::wp_get_attachment_image_attributes()
* @uses Webcomic::loop_end()
* @uses Webcomic::loop_start()
* @uses Webcomic::the_excerpt()
* @uses Webcomic::the_content()
* @uses Webcomic::integrate_sort_asc()
* @uses WebcomicWidget
* @uses WebcomicShortcode
*/
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( 'wp_head', array( $this, 'wp_head' ), 1 );
add_action( 'init', array( $this, 'twitter_oauth' ) );
add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
add_action( 'init', array( $this, 'save_transcript' ) );
add_action( 'init', array( $this, 'dynamic_defaults' ) );
add_action( 'init', array( $this, 'webcomic_redirect' ) );
add_action( 'setup_theme', array( $this, 'setup_theme' ) );
add_action( 'the_post', array( $this, 'the_post' ), 10, 1 );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 );
add_action( 'webcomic_buffer_alert', array( $this, 'buffer_alert' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) );
add_action( 'transition_post_status', array( $this, 'tweet_webcomic' ), 10, 3 );
add_filter( 'request', array( $this, 'request' ), 10, 1 );
add_filter( 'get_term', array( $this, 'get_term' ), 10, 2 );
add_filter( 'the_posts', array( $this, 'the_posts' ), 10, 2 );
add_filter( 'get_terms', array( $this, 'get_terms' ), 10, 3 );
add_filter( 'body_class', array( $this, 'body_class' ), 10, 2 );
add_filter( 'post_class', array( $this, 'post_class' ), 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( 'pre_option_template', array( $this, 'pre_option_template' ) );
add_filter( 'template_include', array( $this, 'template_include' ), 10, 1 );
add_filter( 'the_content_feed', array( $this, 'the_content_feed' ), 10, 1 );
add_filter( 'pre_option_stylesheet', array( $this, 'pre_option_stylesheet' ) );
add_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms' ), 10, 4 );
add_filter( 'extra_theme_headers', array( $this, 'extra_theme_headers' ), 10, 1 );
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'wp_get_attachment_image_attributes' ), 10, 2 );
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, 'integrate_sort_asc' ), 10, 1 );
}
require_once self::$dir . '-/php/tags.php';
require_once self::$dir . '-/php/widgets.php'; new WebcomicWidget;
require_once self::$dir . '-/php/shortcodes.php'; new WebcomicShortcode;
}
}
/**
* Initialize major plugin features.
*
* This hook performs a number of important functions, including
* loading the plugin text domain, defining the 'prints',
* 'transcripts', and 'crossover' rewrite endpoints, and
* registering custom image sizes, post types, and taxonomies.
*
* @uses Webcomic::$dir
* @uses Webcomic::$url
* @uses Webcomic::$config
* @hook init
*/
public function init() {
load_plugin_textdomain( 'webcomic', '', self::$dir . '-/i18n/' );
define( 'EP_WEBCOMIC', 1099511627776 ); // 2^40
define( 'EP_WEBCOMIC_TERM', 2199023255552 ); // 2^41
add_rewrite_endpoint( 'prints', EP_WEBCOMIC );
add_rewrite_endpoint( 'transcripts', EP_WEBCOMIC );
add_rewrite_endpoint( 'crossover', EP_WEBCOMIC_TERM );
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' => 'dashicons-images-alt2',
'supports' => $v[ 'supports' ],
'has_archive' => $v[ 'slugs' ][ 'archive' ],
'rewrite' => array(
'slug' => $v[ 'slugs' ][ 'webcomic' ],
'ep_mask' => EP_WEBCOMIC | EP_PERMALINK,
'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,
'show_admin_column' => true,
'rewrite' => array(
'slug' => $v[ 'slugs' ][ 'storyline' ],
'with_front' => false,
'hierarchical' => true,
'ep_mask' => EP_WEBCOMIC_TERM
)
) );
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' )
),
'show_admin_column' => true,
'rewrite' => array(
'slug' => $v[ 'slugs' ][ 'character' ],
'with_front' => false,
'ep_mask' => EP_WEBCOMIC_TERM
)
) );
}
foreach ( self::$config[ 'collections' ] as $k => $v ) {
foreach ( $v[ 'taxonomies' ] as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $k );
}
}
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' => 'dashicons-testimonial'
) );
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,
'show_admin_column' => true
) );
register_post_type( 'webcomic_commerce', array(
'public' => false,
'supports' => false
) );
}
/**
* Handle PayPal IPN's.
*
* Logs instant payment notifications and updates the original print
* availability of webcomics as necessary.
*
* @todo developer.paypal.com/webapps/developer/docs/api
*
* @uses Webcomic::$config
* @action webcomic_ipn Triggered prior to processing a Paypal IPN request.
* @hook init
*/
public function log_ipn() {
global $wpdb;
if ( isset( $_GET[ 'webcomic_commerce_ipn' ] ) and !empty( $_POST ) ) {
do_action( 'webcomic_ipn' );
$excerpt = $content = $header = $error = '';
$request = 'cmd=' . urlencode( '_notify-validate' );
foreach ( $_POST as $k => $v ) {
$value = urlencode( stripslashes( $v ) );
$request .= "&{$k}={$value}";
}
$remote = wp_remote_post( self::$debug ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', array(
'body' => $request,
'headers' => array(
'Host' => self::$debug ? 'www.sandbox.paypal.com' : 'www.paypal.com'
),
'sslverify' => true
) );
if ( 200 === wp_remote_retrieve_response_code( $remote ) ) {
$date = date( 'Y-m-d H:i:s', strtotime( $_POST[ 'payment_date' ] ) );
$response = wp_remote_retrieve_body( $remote );
if ( 0 === strcmp( $response, 'VERIFIED' ) ) {
if ( 'Completed' !== $_POST[ 'payment_status' ] ) {
$error = true;
$excerpt = __( 'Incomplete transaction', 'webcomic' );
} elseif ( $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM {$wpdb->posts} WHERE post_title = %s", $_POST[ 'txn_id' ] ) ) ) {
$error = true;
$excerpt = __( 'Transaction already completed', 'webcomic' );
} elseif ( 'cart' === $_POST[ 'txn_type' ] ) {
$i = 1;
$excerpt = __( 'Shopping Cart', 'webcomic' );
$content = '<dl>';
while ( isset( $_POST[ "item_number{$i}" ] ) ) {
$item = explode( '-', $_POST[ "item_number{$i}" ] );
$commerce = get_post_meta( $item[ 0 ], 'webcomic_commerce', true );
if ( empty( self::$config[ 'collections' ][ $item[ 1 ] ] ) ) {
$content .= '<dt class="error">' . $_POST[ "item_number{$i}" ] . '</dt>';
$content .= '<dd class="error">' . sprintf( __( 'Invalid collection %s', 'webcomic' ), $item[ 1 ] ) . '</dd>';
} elseif ( self::$config[ 'collections' ][ $item[ 1 ] ][ 'commerce' ][ 'business' ] !== $_POST[ 'receiver_email' ] ) {
$content .= '<dt class="error">' . $_POST[ "item_number{$i}" ] . '</dt>';
$content .= '<dd>' . sprintf( __( 'Incorrect business email %s', 'webcomic' ), $_POST[ 'receiver_email' ] ) . '</dd>';
} elseif ( $_POST[ "quantity{$i}" ] and number_format( $commerce[ 'total' ][ $item[ 2 ] ], 2 ) !== number_format( $_POST[ "mc_gross_{$i}" ] / $_POST[ "quantity{$i}" ], 2 ) ) {
$content .= '<dt class="error">' . $_POST[ "item_number{$i}" ] . '</dt>';
$content .= '<dd>' . sprintf( __( 'Incorrect price %s', 'webcomic' ), number_format( $_POST[ "mc_gross_{$i}" ] / $_POST[ "quantity{$i}" ], 2 ) ) . '</dd>';
} elseif ( self::$config[ 'collections' ][ $item[ 1 ] ][ 'commerce' ][ 'currency' ] !== $_POST[ 'mc_currency' ] ) {
$content .= '<dt class="error">' . $_POST[ "item_number{$i}" ] . '</dt>';
$content .= '<dd>' . sprintf( __( 'Incorrect currency %s', 'webcomic' ), $_POST[ 'mc_currency' ] ) . '</dd>';
} else {
if ( 'original' === $item[ 2 ] ) {
update_post_meta( $item[ 0 ], 'webcomic_original', false );
}
$content .= '<dt>' . $_POST[ "item_number{$i}" ] . '</dt>';
$content .= '<dd>' . __( 'Sale Get!', 'webcomic' ) . '</dd>';
}
$i++;
}
$content .= '</dl>';
} elseif ( 'donation' === $_GET[ 'webcomic_commerce_ipn' ] ) {
if ( empty( self::$config[ 'collections' ][ $_POST[ 'item_number' ] ] ) ) {
$error = true;
$excerpt = sprintf( __( 'Invalid collection %s', 'webcomic' ), $_POST[ 'item_number' ] );
} elseif ( self::$config[ 'collections' ][ $_POST[ 'item_number' ] ][ 'commerce' ][ 'business' ] !== $_POST[ 'receiver_email' ] ) {
$error = true;
$excerpt = sprintf( __( 'Incorrect business email %s', 'webcomic' ), $_POST[ 'receiver_email' ] );
} elseif ( self::$config[ 'collections' ][ $_POST[ 'item_number' ] ][ 'commerce' ][ 'donation' ] and ( number_format( self::$config[ 'collections' ][ $_POST[ 'item_number' ] ][ 'commerce' ][ 'donation' ], 2 ) !== number_format( $_POST[ 'mc_gross' ], 2 ) ) ) {
$error = true;
$excerpt = sprintf( __( 'Incorrect price %s', 'webcomic' ), number_format( $_POST[ 'mc_gross' ], 2 ) );
} elseif ( self::$config[ 'collections' ][ $_POST[ 'item_number' ] ][ 'commerce' ][ 'currency' ] !== $_POST[ 'mc_currency' ] ) {
$error = true;
$excerpt = sprintf( __( 'Incorrect currency %s', 'webcomic' ), $_POST[ 'mc_currency' ] );
} else {
$excerpt = __( 'Donation Get!', 'webcomic' );
}
} else {
$item = explode( '-', $_POST[ 'item_number' ] );
$commerce = get_post_meta( $item[ 0 ], 'webcomic_commerce', true );
if ( empty( self::$config[ 'collections' ][ $item[ 1 ] ] ) ) {
$error = true;
$excerpt = sprintf( __( 'Invalid collection %s', 'webcomic' ), $item[ 1 ] );
} elseif ( self::$config[ 'collections' ][ $item[ 1 ] ][ 'commerce' ][ 'business' ] !== $_POST[ 'receiver_email' ] ) {
$error = true;
$excerpt = sprintf( __( 'Incorrect business email %s', 'webcomic' ), $_POST[ 'receiver_email' ] );
} elseif ( $_POST[ 'quantity' ] and number_format( $commerce[ 'total' ][ $item[ 2 ] ], 2 ) !== number_format( $_POST[ 'mc_gross' ] / $_POST[ 'quantity' ], 2 ) ) {
$error = true;
$excerpt = sprintf( __( 'Incorrect price %s', 'webcomic' ), number_format( $_POST[ 'mc_gross' ] / $_POST[ 'quantity' ], 2 ) );
} elseif ( self::$config[ 'collections' ][ $item[ 1 ] ][ 'commerce' ][ 'currency' ] !== $_POST[ 'mc_currency' ] ) {
$error = true;
$excerpt = sprintf( __( 'Incorrect currency %s', 'webcomic' ), $_POST[ 'mc_currency' ] );
} else {
if ( 'original' === $item[ 2 ] ) {
update_post_meta( $item[ 0 ], 'webcomic_original', false );
}
$excerpt = __( 'Sale Get!', 'webcomic' );
}
}
} elseif ( 0 === strcmp( $response, 'INVALID' ) ) {
$error = true;
$excerpt = __( 'Invalid response', 'webcomic' );
}
} else {
$error = true;
$excerpt = sprintf( __( 'HTTP Error %s', 'webcomic' ), wp_remote_retrieve_response_code( $remote ) );
}
if ( !$content and !empty( $_POST[ 'item_number' ] ) ) {
$content = $_POST[ 'item_number' ];
}
if ( $id = wp_insert_post( array(
'post_name' => sanitize_title( $_POST[ 'txn_id' ] ),
'post_type' => 'webcomic_commerce',
'post_date' => $date,
'post_title' => $_POST[ 'txn_id' ],
'post_author' => 1,
'post_status' => 'puglish',
'post_content' => $content,
'post_excerpt' => $excerpt,
'post_date_gmt' => get_gmt_from_date( $date )
) ) and !is_wp_error( $id ) and $error ) {
update_post_meta( $id, 'webcomic_commerce_error', true );
}
}
}
/**
* Add Generator, Open Graph, and Twitter Card metadata.
*
* 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 wp_head() {
global $wp_query, $post;
$object = $wp_query->get_queried_object();
$output = array();
if ( self::$collection and !is_404() ) {
$output[ 'generator' ] = 'Webcomic ' . self::$version;
$output[ 'og:type' ] = empty( $object->post_type ) ? 'website' : 'article';
$output[ 'twitter:card' ] = is_singular() ? 'photo' : 'summary';
$output[ 'og:site_name' ] = get_bloginfo( 'name' );
$output[ 'og:description' ] = get_bloginfo( 'description' );
if ( is_singular() ) {
setup_postdata( $post );
$output[ 'og:url' ] = get_permalink();
$output[ 'og:title' ] = esc_attr( get_the_title() );
$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 ( $description = esc_attr( strip_tags( get_the_excerpt() ) ) ) {
$output[ 'og:description' ] = $description;
}
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 );
}
}
}
} elseif ( is_tax() ) {
$output[ 'og:url' ] = get_term_link( ( integer ) $object->term_id, $object->taxonomy );
$output[ 'og:title' ] = esc_attr( single_term_title( '', false ) );
if ( $description = esc_attr( strip_tags( term_description( $object->term_id, $object->taxonomy ) ) ) ) {
$output[ 'og:description' ] = $description;
}
} else {
$output[ 'og:url' ] = get_post_type_archive_link( $object->name );
$output[ 'og:title' ] = esc_attr( self::$config[ 'collections' ][ $object->name ][ 'name' ] );
if ( $description = esc_attr( strip_tags( self::$config[ 'collections' ][ $object->name ][ 'description' ] ) ) ) {
$output[ 'og:description' ] = $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, 'full' );
$output[ 'og:image' ][] = array(
$attributes[ 0 ],
':width' => $attributes[ 1 ],
':height' => $attributes[ 2 ]
);
}
} elseif ( is_tax() and isset( self::$config[ 'terms' ][ $object->term_id ][ 'image' ] ) ) {
$attributes = wp_get_attachment_image_src( self::$config[ 'terms' ][ $object->term_id ][ 'image' ], 'full' );
$output[ 'og:image' ] = $attributes[ 0 ];
$output[ 'og:image:width' ] = $attributes[ 1 ];
$output[ 'og:image:height' ] = $attributes[ 2 ];
} elseif ( self::$config[ 'collections' ][ self::$collection ][ 'image' ] ) {
$attributes = wp_get_attachment_image_src( self::$config[ 'collections' ][ self::$collection ][ 'image' ], 'full' );
$output[ 'og:image' ] = $attributes[ 0 ];
$output[ 'og:image:width' ] = $attributes[ 1 ];
$output[ 'og:image:height' ] = $attributes[ 2 ];
}
}
if ( $output = apply_filters( 'webcomic_opengraph', $output, $object, self::$collection ) ) {
foreach ( ( array ) $output as $k => $v ) {
if ( is_array( $v ) ) {
foreach( $v as $x ) {
if ( is_array( $x ) ) {
foreach ( $x as $a => $b ) {
echo '<meta ', 0 === strpos( $k, 'twitter' ) ? 'name' : 'property', '="', $k, is_string( $a ) ? $a : '', '" content="', $b, '">', "\n";
}
} else {
echo '<meta ', 0 === strpos( $k, 'twitter' ) ? 'name' : 'property', '="', $k, '" content="', $x, '">', "\n";
}
}
} else {
echo '<meta ', ( 0 === strpos( $k, 'twitter' ) or 'generator' === $k ) ? 'name' : 'property', '="', $k, '" content="', $v, '">', "\n";
}
}
}
}
/**
* Handle Twitter OAuth authentication.
*
* @uses Webcomic::$config
* @hook init
*/
public function twitter_oauth() {
if ( isset( $_GET[ 'webcomic_twitter_oauth' ] ) ) {
if ( !class_exists( 'tmhOAuth' ) ) {
require_once self::$dir . '-/lib/twitter.php';
}
$admin_url = add_query_arg( array( 'post_type' => $_GET[ 'webcomic_collection' ], 'page' => "{$_GET[ 'webcomic_collection' ]}-options" ), admin_url( 'edit.php' ) );
if ( isset( $_GET[ 'denied' ] ) ) {
wp_die( sprintf( __( 'Authorization denied. <a href="%1$s">Return to %2$s Settings</a>', 'webcomic' ), $admin_url, self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'name' ] ), __( 'Twitter Authorization Denied | Webcomic', 'webcomic' ), array( 'response' => 200 ) );
} else {
$oauth = new tmhOAuth( array(
'consumer_key' => self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'consumer_key' ],
'consumer_secret' => self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'consumer_secret' ],
'user_token' => self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'request_token' ],
'user_secret' => self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'request_secret' ]
) );
$code = $oauth->request( 'POST', str_replace( '.json', '', $oauth->url( 'oauth/access_token', '' ) ), array(
'oauth_verifier' => $_GET[ 'oauth_verifier' ]
) );
if ( 200 === intval( $code ) ) {
$response = $oauth->extract_params( $oauth->response[ 'response' ] );
self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'oauth_token' ] = $response[ 'oauth_token' ];
self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'oauth_secret' ] = $response[ 'oauth_token_secret' ];
self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'request_token' ] = self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'twitter' ][ 'request_secret' ] = '';
update_option( 'webcomic_options', self::$config );
$oauth->config[ 'user_token' ] = $response[ 'oauth_token' ];
$oauth->config[ 'user_secret' ] = $response[ 'oauth_token_secret' ];
$code = $oauth->request( 'GET', $oauth->url( '1.1/account/verify_credentials' ) );
if ( 200 === intval( $code ) ) {
$response = json_decode( $oauth->response[ 'response' ] );
wp_die( sprintf( __( 'Newly published %1$s webcomics will be tweeted to <a href="%2$s">%3$s</a>. <a href="%4$s">Return to %1s Settings</a>', 'webcomic' ),
self::$config[ 'collections' ][ $_GET[ 'webcomic_collection' ] ][ 'name' ],
"http://twitter.com/{$response->screen_name}",
$response->screen_name,
$admin_url
), __( 'Twitter Authorization Complete | Webcomic', 'webcomic' ), array( 'response' => 200 ) );
} else {
wp_die( sprintf( __( 'Your credentials could not be verified. Please ensure that your <b>consumer key</b> and <b>consumer secret</b> were entered correctly and <a href="%s">try again.</a>', 'webcomic' ), $admin_url ), __( 'Twitter Authorization Failed | Webcomic', 'webcomic' ), array( 'response' => 200 ) );
}
} else {
wp_die( sprintf( __( 'Your credentials could not be verified. Please ensure that your <b>consumer key</b> and <b>consumer secret</b> were entered correctly and <a href="%s">try again.</a>', 'webcomic' ), $admin_url ), __( 'Twitter Authorization Failed | Webcomic', 'webcomic' ), array( 'response' => 200 ) );
}
}
}
}
/**
* Handle infinite scroll.
*
* @hook wp_loaded
*/
public function wp_loaded() {
if (isset($_POST['webcomic-infinite'])) {
$collections = get_webcomic_collections();
$webcomics = new WP_Query(array(
'posts_per_page' => 2,
'post_type' => in_array($_POST['collection'], $collections) ? $_POST['collection'] : $collections,
'offset' => (int) $_POST['offset'],
'order' => in_array($_POST['order'], array('ASC', 'DESC')) ? $_POST['order'] : 'DESC'
));
if ($webcomics->have_posts()) {
$webcomics->the_post();
if ( !locate_template( array( 'webcomic/infinite.php' ), true ) ) {
require self::$dir . '-/php/integrate/infinite.php';
}
}
if (2 > $webcomics->post_count) {
if ( !locate_template( array( 'webcomic/infinite-end.php' ), true ) ) {
require self::$dir . '-/php/integrate/infinite-end.php';
}
}
exit;
}
}
/**
* Handle transcript submissions.
*
* @uses Webcomic::$config
* @action webcomic_transcript_submit Triggered prior to processing a user-submitted transcript.
* @action webcomic_transcript_submitted Triggered after processing a user-submitted transcript.
* @hook init
*/
public function save_transcript() {
if ( isset( $_POST[ 'webcomic_user_transcript' ] ) and wp_verify_nonce( $_POST[ 'webcomic_user_transcript' ], 'webcomic_user_transcript' ) ) {
do_action( 'webcomic_transcript_submit' );
$anonymous = false;
if ( !$the_post = get_post( $_POST[ 'webcomic_transcript_post' ] ) or empty( self::$config[ 'collections' ][ $the_post->post_type ] ) ) {
wp_die( __( 'Transcripts can only be submitted for webcomics.', 'webcomic' ), __( 'Error | Webcomic', 'webcomic' ) );
} elseif ( !get_post_meta( $_POST[ 'webcomic_transcript_post' ], 'webcomic_transcripts', true ) ) {
wp_die( __( 'This webcomic cannot be transcribed.', 'webcomic' ), __( 'Transcribing Not Allowed | Webcomic', 'webcomic' ) );
} else {
$user = wp_get_current_user();
if ( $user->ID ) {
$_POST[ 'webcomic_transcript_url' ] = $user->user_url;
$_POST[ 'webcomic_transcript_email' ] = $user->user_email;
$_POST[ 'webcomic_transcript_author' ] = $user->display_name;
} elseif ( empty( $_POST[ 'webcomic_transcript_author' ] ) ) {
$_POST[ 'webcomic_transcript_author' ] = $anonymous = __( 'an anonymous user', 'webcomic' );
}
if ( 'register' === self::$config[ 'collections' ][ $the_post->post_type ][ 'transcripts' ][ 'permission' ] and empty( $user->ID ) ) {
wp_die( sprintf( __( 'You must be <a href="%s">logged in</a> to transcribe this webcomic.', 'webcomic' ), wp_login_url( get_permalink( $the_post->ID ) ) ), __( 'Unauthorized Transcriber | Webcomic', 'webcomic' ), 401 );
} elseif ( 'identify' === self::$config[ 'collections' ][ $the_post->post_type ][ 'transcripts' ][ 'permission' ] and ( empty( $_POST[ 'webcomic_transcript_author' ] ) or empty( $_POST[ 'webcomic_transcript_email' ] ) or !filter_var( $_POST[ 'webcomic_transcript_email' ], FILTER_VALIDATE_EMAIL ) ) ) {
wp_die( __( 'You must provide a name and valid email address to transcribe this webcomic.', 'webcomic' ), __( 'Unauthorized Transcriber | Webcomic', 'webcomic' ), 401 );
} elseif ( empty( $_POST[ 'webcomic_transcript_content' ] ) ) {
wp_die( __( 'Please write a transcript.', 'webcomic' ), __( 'Error | Webcomic', 'webcomic' ) );
}
if ( empty( $_POST[ 'webcomic_transcript_update' ] ) ) {
$author = empty( $user->ID ) ? 1 : $user->ID;
} elseif ( $update_post = get_post( $_POST[ 'webcomic_transcript_update' ] ) ) {
$author = ( int ) $update_post->post_author;
} else {
$author = 0;
}
$date = current_time( 'mysql' );
$title = sprintf( __( '%s Transcript', 'webcomic' ), get_the_title( $_POST[ 'webcomic_transcript_post' ] ) );
if ( $new_post = wp_insert_post( array(
'ID' => empty( $_POST[ 'webcomic_transcript_update' ] ) ? 0 : $_POST[ 'webcomic_transcript_update' ],
'post_name' => sanitize_title( $title ),
'post_type' => 'webcomic_transcript',
'post_date' => $date,
'post_title' => $title,
'post_author' => $author,
'post_parent' => $_POST[ 'webcomic_transcript_post' ],
'post_status' => 'draft',
'post_content' => $_POST[ 'webcomic_transcript_content' ],
'post_date_gmt' => get_gmt_from_date( $date )
) ) and !is_wp_error( $new_post ) ) {
if ( !$anonymous and ( int ) $user->ID !== $author ) {
add_post_meta( $new_post, 'webcomic_author', array(
'name' => $_POST[ 'webcomic_transcript_author' ],
'email' => $_POST[ 'webcomic_transcript_email' ],
'url' => $_POST[ 'webcomic_transcript_url' ],
'ip' => preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER[ 'REMOTE_ADDR' ] ),
'time' => ( integer ) current_time( 'timestamp' )
) );
}
if ( $_POST[ 'webcomic_transcript_language' ] ) {
wp_set_post_terms( $new_post, $_POST[ 'webcomic_transcript_language' ], 'webcomic_language' );
}
if ( self::$config[ 'collections' ][ $the_post->post_type ][ 'transcripts' ][ 'notify' ][ 'hook' ] ) {
wp_mail(
self::$config[ 'collections' ][ $the_post->post_type ][ 'transcripts' ][ 'notify' ][ 'email' ],
sprintf( __( '[%1$s] %2$s Transcript Submitted', 'webcomic' ), get_bloginfo( 'name' ), $the_post->post_title ),
sprintf( __( 'This is an automated notification that %1$s%2$s has <a href="%3$s">%4$s</a> for %5$s.', 'webcomic' ),
$_POST[ 'webcomic_transcript_author' ],
$_POST[ 'webcomic_transcript_email' ] ? " <{$_POST[ 'webcomic_transcript_email' ]}>" : '',
esc_url( admin_url( "post.php?post={$new_post}&action=edit" ) ),
$_POST[ 'webcomic_transcript_update' ] ? __( 'improved a transcript', 'webcomic' ) : __( 'submitted a transcript', 'webcomic' ),
'<a href="' . esc_url( admin_url( "edit.php?post_type={$the_post->post_type}" ) ) . '">' . self::$config[ 'collections' ][ $the_post->post_type ][ 'name' ] . '</a> - <a href="' . esc_url( admin_url( "post.php?post={$the_post->ID}&action=edit" ) ) . '">' . $the_post->post_title . '</a>'
),
'content-type: text/html'
);
}
$_POST[ 'webcomic_transcript_submission' ] = true;
} else {
$_POST[ 'webcomic_transcript_submission' ] = false;
}
}
do_action( 'webcomic_transcript_submitted' );
}
}
/**
* Get default dynamic webcomic container URL's.
*
* @hook init
*/
public function dynamic_defaults() {
if ( isset( $_GET[ 'webcomic_dynamic_defaults' ] ) and 'xmlhttprequest' === strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) ) {
$output = array();
foreach ( $_GET[ 'webcomic_dynamic_defaults' ] as $default ) {
$output[] = array(
'url' => get_permalink( $default[ 'parent' ] ),
'container' => $default[ 'container' ]
);
}
echo json_encode( $output );
die;
}
}
/**
* Handle parameterized webcomic URL's.
*
* @uses WebcomicTag::get_relative_webcomic_term_link()
* @uses WebcomicTag::get_relative_webcomic_link()
* @hook init
*/
public function webcomic_redirect() {
$link = '';
if ( isset( $_GET[ 'first_webcomic' ] ) or isset( $_GET[ 'last_webcomic' ] ) or isset( $_GET[ 'random_webcomic' ] ) ) {
if ( isset( $_GET[ 'first_webcomic' ] ) ) {
$relative = 'first';
} elseif ( isset( $_GET[ 'last_webcomic' ] ) ) {
$relative = 'last';
} else {
$relative = 'random';
}
$in_same_term = empty( $_GET[ 'in_same_term' ] ) ? false : maybe_unserialize( stripslashes( urldecode( $_GET[ 'in_same_term' ] ) ) );
$excluded_terms = empty( $_GET[ 'excluded_terms' ] ) ? false : maybe_unserialize( stripslashes( urldecode( $_GET[ 'excluded_terms' ] ) ) );
$taxonomy = empty( $_GET[ 'taxonomy' ] ) ? false : maybe_unserialize( stripslashes( urldecode( $_GET[ 'taxonomy' ] ) ) );
$collection = empty( $_GET[ "{$relative}_webcomic" ] ) ? array_rand( self::$config[ 'collections' ] ) : $_GET[ "{$relative}_webcomic" ];
$link = WebcomicTag::get_relative_webcomic_link( $relative, $in_same_term, $excluded_terms, $taxonomy, $collection );
} elseif ( isset( $_GET[ 'first_webcomic_term' ] ) or isset( $_GET[ 'last_webcomic_term' ] ) or isset( $_GET[ 'random_webcomic_term' ] ) ) {
$target = empty( $_GET[ 'target' ] ) ? 'archive' : $_GET[ 'target' ];
if ( isset( $_GET[ 'first_webcomic_term' ] ) ) {
$relative = 'first';
} elseif ( isset( $_GET[ 'last_webcomic_term' ] ) ) {
$relative = 'last';
} else {
$relative = 'random';
}
if ( empty( $_GET[ "{$relative}_webcomic_term" ] ) ) {
$taxonomy = array_rand( self::$config[ 'collections' ] ) . '_' . array_rand( array( 'storyline' => true, 'character' => true ) );
} else {
$taxonomy = $_GET[ "{$relative}_webcomic_term" ];
}
$args = empty( $_GET[ 'args' ] ) ? false : maybe_unserialize( stripslashes( urldecode( $_GET[ 'args' ] ) ) );
$link = WebcomicTag::get_relative_webcomic_term_link( $target, $relative, $taxonomy, $args );
}
if ( $link ) {
wp_redirect( $link );
die;
}
}
/**
* 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. Unfortunately, this means checking even before any
* conditional tags are available.
*
* @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() ) {
$tokens = array(
'%year%' => '(?P<year>\d{4})',
'%monthnum%' => '(?P<monthnum>0[1-9]|1[012])',
'%day%' => '(?P<day>0[1-9]|[12][0-9]|3[01])',
'%hour%' => '(?P<hour>0[0-9]|1[0-9]|2[0-3])',
'%minute%' => '(?P<minute>0[1-9]|[1-5][0-9])',
'%second%' => '(?P<second>0[1-9]|[1-5][0-9])',
'%post_id%' => '(?P<post_id>\d+)',
'%author%' => '(?P<author>[\w-]+)'
);
foreach ( self::$config[ 'collections' ] as $k => $v ) {
$tokens[ "%{$k}_storyline%" ] = "(?P<{$k}_storyline>([\w-]+/?)+)";
$permalinks[ "{$k}_archive" ] = $v[ 'slugs' ][ 'archive' ];
$permalinks[ "{$k}_webcomic" ] = str_replace( array_keys( $tokens ), $tokens, $v[ 'slugs' ][ 'webcomic' ] );
$permalinks[ "{$k}_storyline" ] = $v[ 'slugs' ][ 'storyline' ];
$permalinks[ "{$k}_character" ] = $v[ 'slugs' ][ 'character' ];
}
}
if (
(
preg_match( '/webcomic\d+(_(storyline|character))?/', implode( ' ', 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( '{/(' . implode( '|', $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
) {
if ( 2 < count( $match ) and $permalinks ) {
foreach ( $permalinks as $k => $v ) {
if ( false !== strpos( $k, '_webcomic' ) and preg_match( '{' . $v . '}', $match[ 1 ] ) ) {
self::$collection = str_replace( '_webcomic', '', $k );
break;
}
}
} else {
$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 ];
}
}
$active_theme = new WP_Theme( get_stylesheet_directory(), '' );
self::$integrate = !$active_theme->get( 'Webcomic' );
}
/**
* 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 ( !current_user_can( 'edit_post', $post->ID ) ) {
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 ];
} elseif ( ! $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 ];
}
}
}
}
/**
* Include webcomic posts on standard archive pages.
*
* Also handles the crossover ep_mask for term crossover archives.
*
* @param object $query The query object for the current page.
* @return object
* @uses Webcomic::$config
* @hook pre_get_posts
*/
public function pre_get_posts( $query ) {
if ( $query->is_main_query() and is_tax() and !empty( $query->tax_query->queries[ 0 ][ 'taxonomy' ] ) and preg_match( '/^webcomic\d+_(storyline|character)$/', $query->tax_query->queries[ 0 ][ 'taxonomy' ] ) ) {
if ( isset( $query->query_vars[ 'crossover' ] ) ) {
$parts = explode( '/', $query->get( 'crossover' ) );
if ( empty( $parts ) or 'page' === $parts[ 0 ] ) {
$collections = array_keys( self::$config[ 'collections' ] );
$key = array_search( str_replace( array( '_storyline', '_character' ), '', $query->tax_query->queries[ 0 ][ 'taxonomy' ] ), $collections );
unset( $collections[ $key ] );
$query->set( 'post_type', $collections );
if ( isset( $parts[ 1 ] ) ) {
$query->set( 'paged', $parts[ 1 ] );
}
} else {
$collection = '';
foreach ( self::$config[ 'collections' ] as $k => $v ) {
if ( false === strpos( $query->tax_query->queries[ 0 ][ 'taxonomy' ], $k ) and $parts[ 0 ] === $v[ 'slugs' ][ 'name' ] ) {
$collection = $k;
break;
}
}
if ( $collection ) {
$query->set( 'post_type', array( $collection ) );
if ( isset( $parts[ 2 ] ) ) {
$query->set( 'paged', $parts[ 2 ] );
}
} else {
$query->set( 'post_type', $parts[ 0 ] );
}
}
} elseif ( !$query->get( 'post_type' ) ) {
$query->set( 'post_type', array( str_replace( array( '_storyline', '_character' ), '', $query->tax_query->queries[ 0 ][ 'taxonomy' ] ) ) );
}
}
if ( $query->is_main_query() and !$query->get( 'post_type' ) and is_archive() ) {
$query->set( 'post_type', array_merge( get_post_types( array( 'public' => true ) ), array_keys( self::$config[ 'collections' ] ) ) );
}
return $query;
}