-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathclass-kind-taxonomy.php
executable file
·1420 lines (1311 loc) · 41 KB
/
class-kind-taxonomy.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
/**
* Post Kind Taxonomy Class
*
* Registers the taxonomy and sets its behavior.
*
* @package Post Kinds
*/
final class Kind_Taxonomy {
private static $kinds = array(); // Store a Post_Kind class which is a definition of a specific kind
public static function init() {
$wp_version = get_bloginfo( 'version' );
require_once plugin_dir_path( __FILE__ ) . '/register-kinds.php';
// Add the Correct Archive Title to Kind Archives.
if ( version_compare( $wp_version, '5.5', '>' ) ) {
add_filter( 'get_the_archive_title', array( self::class, 'kind_archive_title' ), 10 );
} else {
add_filter( 'get_the_archive_title', array( self::class, 'kind_archive_title' ), 10, 3 );
}
add_filter( 'get_the_archive_title_prefix', array( self::class, 'kind_archive_prefix' ), 10 );
add_filter( 'get_the_archive_description', array( self::class, 'kind_archive_description' ), 10 );
add_filter( 'document_title_parts', array( self::class, 'document_title_parts' ), 10 );
// Add Kind Permalinks.
add_filter( 'post_link', array( self::class, 'kind_permalink' ), 10, 3 );
add_filter( 'post_type_link', array( self::class, 'kind_permalink' ), 10, 3 );
// Query Variable to Exclude Kinds from Feed
add_filter( 'query_vars', array( self::class, 'query_vars' ) );
add_action( 'pre_get_posts', array( self::class, 'kind_filter_query' ) );
add_action( 'pre_get_posts', array( self::class, 'kind_photo_filter' ) );
add_action( 'pre_get_posts', array( self::class, 'kind_alias_filter' ) );
add_action( 'pre_get_posts', array( self::class, 'kind_firehose_query' ), 99 );
// Add Dropdown
add_action( 'restrict_manage_posts', array( self::class, 'kind_dropdown' ), 10, 2 );
// Add Links to Ping to the Webmention Sender.
add_filter( 'webmention_links', array( self::class, 'webmention_links' ), 11, 2 );
// Add Links to Enclosures if Appropriate
add_filter( 'enclosure_links', array( self::class, 'enclosure_links' ), 11, 2 );
// Add Classes to Post.
add_filter( 'post_class', array( self::class, 'post_class' ) );
// Trigger Webmention on Change in Post Status.
add_filter( 'transition_post_status', array( self::class, 'transition' ), 10, 3 );
// On Post Save Set Post Format
add_action( 'save_post', array( self::class, 'post_formats' ), 99, 3 );
// Create hook triggered by change of kind
add_action( 'set_object_terms', array( self::class, 'set_object_terms' ), 10, 6 );
add_filter( 'single_post_title', array( self::class, 'single_post_title' ), 9, 2 );
add_filter( 'the_title', array( self::class, 'the_title' ), 9, 2 );
add_filter( 'get_sample_permalink', array( self::class, 'get_sample_permalink' ), 12, 5 );
add_action( 'rest_api_init', array( self::class, 'rest_kind' ) );
add_filter( 'embed_template_hierarchy', array( self::class, 'embed_template_hierarchy' ) );
add_filter( 'template_include', array( self::class, 'template_include' ) );
add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
}
/** Template Redirect
*/
public static function template_include( $template ) {
global $wp_query;
// Only use this template for archive pages
if ( is_singular() ) {
return $template;
}
// if this is not a request for 'kind_photos'.
if ( ! isset( $wp_query->query_vars['kind_photos'] ) ) {
return $template;
}
$photo_template = locate_template( 'kind-photos.php' );
if ( '' !== $photo_template ) {
return $photo_template;
}
return $template;
}
/**
* Register the Route.
*/
public static function register_routes() {
register_rest_route(
'post-kinds/1.0',
'/fields',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'read' ),
'args' => array(
'kind' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
),
'permission_callback' => function () {
return current_user_can( 'read' );
},
),
)
);
}
public static function read( $request ) {
$kind = $request->get_param( 'kind' );
return self::get_kind_info( $kind, 'all' );
}
/**
* Add our query variables to query_vars list.
*
* @access public
*
* @param array $qvars Current query_vars.
* @return array
*/
public static function query_vars( $qvars ) {
$qvars[] = 'exclude';
$qvars[] = 'exclude_terms';
$qvars[] = 'kind_photos';
$qvars[] = 'kind_firehose';
return $qvars;
}
/**
* Filter the query for our post kinds.
*
* @access public
*
* @param $query
*/
public static function kind_photo_filter( $query ) {
// check if the user is requesting an admin page
if ( is_admin() || ! $query->is_main_query() ) {
return $query;
}
$photos = get_query_var( 'kind_photos' );
// Return if not set
if ( $photos ) {
$query->set( 'posts_per_page', 30 );
$query->is_archive = true;
$query->is_comment_feed = false;
$query->is_home = false;
$query->set(
'meta_query',
array(
'relation' => 'OR',
array(
'key' => '_content_img_ids',
'compare' => 'EXISTS',
),
array(
'key' => 'mf2_photo',
'compare' => 'EXISTS',
),
)
);
}
return $query;
}
/**
* Filter the query for our post kinds.
*
* @access public
*
* @param $query
*/
public static function kind_filter_query( $query ) {
// check if the user is requesting an admin page
if ( is_admin() || ! $query->is_main_query() ) {
return $query;
}
$exclude = get_query_var( 'exclude' );
// Return if both are not set
if ( ! taxonomy_exists( $exclude ) ) {
return $query;
}
$filter = get_query_var( 'exclude_terms' );
if ( empty( $filter ) ) {
return $query;
}
$filter = explode( ',', $filter );
$operator = 'NOT IN';
$query->set(
'tax_query',
array(
array(
'taxonomy' => $exclude,
'field' => 'slug',
'terms' => $filter,
'operator' => $operator,
),
)
);
return $query;
}
/**
* Filter the main query for post kinds.
*
* @access public
*
* @param $query
*/
public static function kind_firehose_query( $query ) {
// check if the user is requesting an admin page
if ( is_admin() || ! $query->is_main_query() ) {
return $query;
}
$filter = get_query_var( 'kind_firehose' );
if ( ! empty( $filter ) ) {
$query->is_archive = true;
$query->is_comment_feed = false;
$query->is_home = false;
$query->is_front_page = false;
$query->is_single = false;
$query->is_posts_page = false;
return $query;
}
if ( $query->is_archive() ) {
return $query;
}
$firehose = get_option( 'kind_firehose' );
if ( empty( $firehose ) || ! is_array( $firehose ) ) {
return $query;
}
$query->set(
'tax_query',
array(
'relation' => 'OR',
array(
'taxonomy' => 'kind',
'field' => 'slug',
'terms' => $firehose,
'operator' => 'IN',
),
array(
'taxonomy' => 'kind',
'operator' => 'NOT EXISTS',
),
)
);
return $query;
}
/**
* Allows for some pre-defined aliases
*
* @access public
*
* @param $query
*/
public static function kind_alias_filter( $query ) {
if ( empty( get_query_var( 'kind' ) ) ) {
return $query;
}
$kind = get_query_var( 'kind' );
if ( 'food' === $kind ) {
$query->set( 'kind', 'eat,drink' );
}
if ( 'reaction' === $kind ) {
$query->set( 'kind', 'bookmark,repost,like,favorite' );
}
if ( 'media' === $kind ) {
$query->set( 'kind', 'watch,read,listen,play' );
}
return $query;
}
/**
* Register our REST API endpoint for post kinds.
*
* @access public
*/
public static function rest_kind() {
register_rest_field(
'post',
'kind',
array(
'get_callback' => array( self::class, 'get_post_kind_slug' ),
'update_callback' => array( self::class, 'set_rest_post_kind' ),
'schema' => array(
'kind' => __( 'Post Kind', 'indieweb-post-kinds' ),
'type' => 'string',
),
)
);
}
/**
* Filter template hierarchy to include our template files.
*
* @access public
*
* @param array $templates Array of template file names.
* @return mixed
*/
public static function embed_template_hierarchy( $templates ) {
$object = get_queried_object();
if ( ! empty( $object->post_type ) ) {
$post_kind = get_post_kind( $object );
if ( $post_kind ) {
array_unshift( $templates, "embed-{$object->post_type}-{$post_kind}.php" );
}
}
return $templates;
}
/**
* Generate a sample permalink for a post.
*
* @access public
*
* @param string $permalink Current permalink.
* @param int $post_id Post ID.
* @param string $title Current post title.
* @param string $name Current post name.
* @param WP_Post $post Post object.
* @return mixed
*/
public static function get_sample_permalink( $permalink, $post_id, $title, $name, $post ) {
if ( 'publish' === $post->post_status || ! empty( $post->post_title ) ) {
return $permalink;
}
// Only kick in if the permalink would be the post_id otherwise.
if ( is_numeric( $permalink[1] ) && $post_id === (int) $permalink[1] ) {
$excerpt = self::get_excerpt( $post );
$excerpt = sanitize_title( mb_strimwidth( wp_strip_all_tags( $excerpt ), 0, 40 ) ); // phpcs:ignore
if ( ! empty( $excerpt ) ) {
$permalink[1] = wp_unique_post_slug( $excerpt, $post_id, $post->post_status, $post->post_type, $post->post_parent );
}
}
return $permalink;
}
/**
* Generate a post title.
*
* @access public
*
* @param int!WP_Post $post Post ID or Post Object.
* @return string
*/
public static function generate_title( $post, $length = 40 ) {
$post = get_post( $post );
if ( ! $post instanceof WP_Post ) {
return null;
}
$kind = get_post_kind_slug( $post );
$excerpt = wp_strip_all_tags( self::get_excerpt( $post ) );
if ( ! in_array( $kind, array( 'note', 'article' ), true ) || ! $kind ) {
$kind_post = new Kind_Post( $post );
$cite = $kind_post->get_cite();
if ( Parse_This_MF2::is_microformat( $cite ) ) {
if ( array_key_exists( 'name', $cite['properties'] ) ) {
$excerpt = $cite['properties']['name'];
if ( is_array( $excerpt ) ) {
$excerpt = $excerpt[0];
}
}
}
}
return mb_strimwidth( $excerpt, 0, $length, '...' ); // phpcs:ignore
}
/**
* Filter the post title. Add a symbol at the end if generated title.
*
* @access public
*
* @param string $title Current post title
* @param int $post_id Post ID
* @return string
*/
public static function the_title( $title, $post_id ) {
if ( ! $title && is_admin() ) {
$title = self::generate_title( $post_id, 60 );
if ( '' === $title ) {
$title = '♦'; }
}
$post_kind = get_post_kind( $post_id );
if ( get_option( 'kind_title' ) && '' !== $post_kind ) {
if ( is_feed() && empty( $title ) ) {
$title = sprintf( '[%1$s] %2$s', $post_kind, self::generate_title( $post_id, 60 ) );
}
}
return $title;
}
/**
* Filter the single post title.
*
* @access public
*
* @param string $title Current post title
* @param WP_Post $post Post object
* @return string
*/
public static function single_post_title( $title, $post ) {
if ( ! is_single() ) {
return $title;
}
if ( empty( $title ) ) {
$title = self::generate_title( $post );
}
$post_kind = get_post_kind( $post );
if ( get_option( 'kind_title' ) && '' !== $post_kind && $post_kind ) {
$title = '[' . $post_kind . '] ' . $title;
}
return $title;
}
/**
* Filter the post excerpt.
*
* @access public
*
* @param WP_Post $post Post object.
* @return string
*/
public static function get_excerpt( $post ) {
if ( ! $post instanceof WP_Post ) {
return '';
}
if ( ! empty( $post->post_excerpt ) ) {
return $post->post_excerpt;
}
return $post->post_content;
}
/**
* Set our post object terms.
*
* @access public
*
* @param object $object_id Current object ID.
* @param array $terms Array of object terms.
* @param array $tt_ids Array of term taxonomy IDs.
* @param string $taxonomy Taxonomy slug
* @param bool $append If terms should be appended or overwrite.
* @param array $old_tt_ids Array of original trem taxonomy IDs.
*/
public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
if ( empty( $tt_ids ) && empty( $old_tt_ids ) ) {
return;
}
if ( 'kind' === $taxonomy ) {
$old_term = get_term_by( 'term_taxonomy_id', array_pop( $old_tt_ids ), 'kind' );
$old_term = $old_term instanceof WP_Term ? $old_term->slug : '';
$new_term = get_term_by( 'term_taxonomy_id', array_pop( $tt_ids ), 'kind' );
$new_term = $new_term instanceof WP_Term ? $new_term->slug : '';
if ( $old_term !== $new_term ) {
// Trigger a hook on a changed kind identifying old and new so actions can be performed
do_action( 'change_kind', $object_id, $old_term, $new_term );
}
}
}
/**
* To Be Run on Plugin Activation.
*/
public static function activate_kinds() {
if ( function_exists( 'iwt_plugin_notice' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( 'You have Indieweb Taxonomy activated. Post Kinds replaces this plugin. Please disable Taxonomy before activating' );
}
self::register();
self::kind_defaultterms();
flush_rewrite_rules();
}
public static function get_pagination_regex() {
global $wp_rewrite;
return $wp_rewrite->pagination_base . '/?([0-9]{1,})';
}
public static function get_feed_regex( $with_base = true ) {
global $wp_rewrite;
// Build a regex to match the feed section of URLs, something like (feed|atom|rss|rss2)/?
$feedregex2 = '';
foreach ( (array) $wp_rewrite->feeds as $feed_name ) {
$feedregex2 .= $feed_name . '|';
}
$feedregex2 = '(' . trim( $feedregex2, '|' ) . ')';
return ( $with_base ) ? $wp_rewrite->feed_base . '/' . $feedregex2 : $feedregex2;
}
public static function generate_permastruct( $elements ) {
if ( empty( $elements ) || ! is_array( $elements ) ) {
return '';
}
$elements[] = '?$';
return implode( '/', $elements );
}
/**
* Register the custom taxonomy for kinds.
*/
public static function register() {
$labels = array(
'name' => _x( 'Kinds', 'taxonomy general name', 'indieweb-post-kinds' ),
'singular_name' => _x( 'Kind', 'taxonomy singular name', 'indieweb-post-kinds' ),
'search_items' => _x( 'Search Kinds', 'search locations', 'indieweb-post-kinds' ),
'popular_items' => _x( 'Popular Kinds', 'popular kinds', 'indieweb-post-kinds' ),
'all_items' => _x( 'All Kinds', 'all taxonomy items', 'indieweb-post-kinds' ),
'parent_item' => _x( 'Parent Kind', 'taxonomy parent item', 'indieweb-post-kinds' ),
'parent_item_colon' => _x( 'Parent Kind:', 'taxonomy parent item with colon', 'indieweb-post-kinds' ),
'desc_field_description' => __( 'This will appear on archives as the description if your theme supports', 'indieweb-post-kinds' ),
'edit_item' => _x( 'Edit Kind', 'edit taxonomy item', 'indieweb-post-kinds' ),
'view_item' => _x( 'View Kind', 'view taxonomy item', 'indieweb-post-kinds' ),
'update_item' => _x( 'Update Kind', 'update taxonomy item', 'indieweb-post-kinds' ),
'add_new_item' => _x( 'Add New Kind', 'add taxonomy item', 'indieweb-post-kinds' ),
'new_item_name' => _x( 'New Kind', 'new taxonomy item', 'indieweb-post-kinds' ),
'separate_items_with_commas' => _x( 'Separate kinds with commas', 'separate kinds with commas', 'indieweb-post-kinds' ),
'add_or_remove_items' => _x( 'Add or remove kinds', 'add or remove items', 'indieweb-post-kinds' ),
'choose_from_most_used' => _x( 'Choose from the most used kinds', 'choose most used', 'indieweb-post-kinds' ),
'not found' => _x( 'No kinds found', 'no kinds found', 'indieweb-post-kinds' ),
'no_terms' => _x( 'No kinds', 'no kinds', 'indieweb-post-kinds' ),
'back_to_items' => __( 'Back to Kinds', 'indieweb-post-kinds' ),
'item_link' => __( 'Kind Link', 'indieweb-post-kinds' ),
'item_link_description' => __( 'A link to a kind', 'indieweb-post-kinds' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => WP_DEBUG,
'show_in_nav_menus' => true,
'show_in_rest' => false,
'show_tagcloud' => true,
'show_in_quick_edit' => false,
'show_admin_column' => true,
'meta_box_cb' => array( self::class, 'select_metabox' ),
'rewrite' => true,
'query_var' => true,
'default_term' => array(
'name' => __( 'Article', 'indieweb-post-kinds' ),
'slug' => 'article',
),
);
register_taxonomy( 'kind', array( 'post' ), $args );
add_permastruct( 'kind_date', 'kind/%kind%/%year%/%monthnum%/' );
$kind_rewrite_taxonomies = apply_filters( 'kind_rewrite_taxonomies', array( 'post_tag', 'category', 'series' ) );
// For each kind, support filtering by other taxonomies.
foreach ( $kind_rewrite_taxonomies as $taxonomy ) {
add_permastruct( 'kind_' . $taxonomy, 'kind/%kind%/' . $taxonomy . '/%' . $taxonomy . '%' );
}
$kind_exclude_slug = apply_filters( 'kind_exclude_slug', 'exclude' );
add_rewrite_tag( '%kind_exclude%', '([^/]*)', 'exclude=' );
add_rewrite_tag( '%kind_exclude_terms%', '([a-z,]+)', 'exclude_terms=' );
add_permastruct( 'kind_excludes', $kind_exclude_slug . '/%kind_exclude%/%kind_exclude_terms%' );
$kind_photos_slug = apply_filters( 'kind_photos_slug', 'photos' );
$kind_firehose_slug = apply_filters( 'kind_firehose_slug', 'firehose' );
$year_regex = '([0-9]{4})';
$month_regex = '(0-9]{2})';
$day_regex = '([0-9]{1,})';
$pagination_regex = self::get_pagination_regex();
$feed_regex = self::get_feed_regex();
$feed_regex2 = self::get_feed_regex( false );
$tax_regex = '([^/]*)';
$term_regex = '([^/]*)';
// Year Archives for Photos
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $year_regex, $pagination_regex ) ),
'index.php?year=$matches[1]&paged=$matches[2]&kind_photos=1',
'top'
);
add_rewrite_rule(
implode( '/', array( $kind_photos_slug, $year_regex, '?$' ) ),
'index.php?year=$matches[1]&kind_photos=1',
'top'
);
// Year and Month Archive for Photos
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $year_regex, $month_regex, $pagination_regex ) ),
'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&kind_photos=1',
'top'
);
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $year_regex, $month_regex ) ),
'index.php?year=$matches[1]&monthnum=$matches[2]&kind_photos=1',
'top'
);
// Year and Month And Day Archive for Photos
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $year_regex, $month_regex, $day_regex, $pagination_regex ) ),
'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&kind_photos=1',
'top'
);
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $year_regex, $month_regex, $day_regex ) ),
'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&kind_photos=1',
'top'
);
// Month And Day Archive for Photos
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $month_regex, $day_regex, $pagination_regex ) ),
'index.php?monthnum=$matches[1]&day=$matches[2]&paged=$matches[3]&kind_photos=1',
'top'
);
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $month_regex, $day_regex ) ),
'index.php?monthnum=$matches[1]&day=$matches[2]&kind_photos=1',
'top'
);
// Root Photos Feed.
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $feed_regex2 ) ),
'index.php?feed=$matches[1]&kind_photos=1',
'top'
);
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $feed_regex ) ),
'index.php?feed=$matches[1]&kind_photos=1',
'top'
);
// Root Photos Pagination.
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug, $pagination_regex ) ),
'index.php?kind_photos=1&paged=$matches[1]',
'top'
);
// Root Photos.
add_rewrite_rule(
self::generate_permastruct( array( $kind_photos_slug ) ),
'index.php?kind_photos=1',
'top'
);
// Root Firehose Pagination.
add_rewrite_rule(
self::generate_permastruct( array( $kind_firehose_slug, $pagination_regex ) ),
'index.php?kind_firehose=1&paged=$matches[1]',
'top'
);
// Root Firehose Feed.
add_rewrite_rule(
self::generate_permastruct( array( $kind_firehose_slug, $feed_regex ) ),
'index.php?kind_firehose=1&feed=$matches[1]',
'top'
);
add_rewrite_rule(
self::generate_permastruct( array( $kind_firehose_slug, $feed_regex2 ) ),
'index.php?kind_firehose=1&feed=$matches[1]',
'top'
);
// Root Firehose.
add_rewrite_rule(
self::generate_permastruct( array( $kind_firehose_slug ) ),
'index.php?kind_firehose=1',
'top'
);
}
/**
* Sets up Default Terms for Kind Taxonomy.
*/
public static function kind_defaultterms() {
$terms = self::get_kind_list();
foreach ( $terms as $term ) {
if ( ! get_term_by( 'slug', $term, 'kind' ) ) {
self::create_post_kind( $term );
} else {
self::update_post_kind( $term );
}
}
}
/**
* Update our post kind if the term already exists.
*
* @access private
*
* @param $term
*/
private static function update_post_kind( $term ) {
$t = get_term_by( 'slug', $term, 'kind' );
if ( ! $t ) {
return self::create_post_kind( $term );
}
$kind = self::get_post_kind_info( $term );
if ( $kind ) {
wp_update_term(
$t->term_id,
'kind',
array(
'name' => $kind->singular_name,
'description' => $kind->description,
'slug' => $term,
)
);
}
}
/**
* Create our post kind term.
*
* @access private
*
* @param $term
*/
private static function create_post_kind( $term ) {
$kind = self::get_post_kind_info( $term );
if ( $kind ) {
wp_insert_term(
$kind->slug,
'kind',
array(
'name' => $kind->singular_name,
'description' => $kind->description,
'slug' => $term,
)
);
}
}
/**
* Filter our post kind permalink.
*
* @access public
*
* @param string $permalink Permalink string to filter.
* @param int $post_id Post ID.
* @param bool $leavename Whether or not to leave the post name.
* @return mixed
*/
public static function kind_permalink( $permalink, $post_id, $leavename ) {
if ( false === strpos( $permalink, '%kind%' ) ) {
return $permalink; }
// Get post
$post = get_post( $post_id );
if ( ! $post ) {
return $permalink; }
// Get taxonomy terms
$terms = wp_get_object_terms( $post->ID, 'kind' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) && is_object( $terms[0] ) ) {
$taxonomy_slug = $terms[0]->slug;
} else {
$taxonomy_slug = 'note'; }
return str_replace( '%kind%', $taxonomy_slug, $permalink );
}
/**
* Fetch the current post kind terms from the current query.
*
* @access public
*
* @return array
*/
public static function get_terms_from_query() {
global $wp_query;
$terms = array();
if ( ! empty( $wp_query->tax_query->queried_terms ) ) {
$queried = wp_list_pluck( $wp_query->tax_query->queried_terms, 'terms' );
foreach ( $queried as $tax => $slugs ) {
foreach ( $slugs as $slug ) {
$terms[] = get_term_by( 'slug', $slug, $tax );
}
}
}
return $terms;
}
/**
* Filters the post kind archive prefix.
* Prefix Introduced in WP5.5
*
* @access public
*
* @param string $prefix Archive title prefix.
* @return string|void
*/
public static function kind_archive_prefix( $prefix ) {
if ( is_tax() || is_category() || is_tag() ) {
$terms = self::get_terms_from_query();
$tax = get_taxonomy( reset( $terms )->taxonomy );
$plural = ( count( $terms ) === 1 );
return ( $plural ? $tax->labels->singular_name : $tax->labels->name ) . ':';
}
return $prefix;
}
/**
* Filters the post kind archive title.
* Original Title and Prefix introduced in WP5.5.
*
* @access public
*
* @param string $title Current archive title value.
* @param string $original_title Title without prefix.
* @param string $prefix Archive title prefix.
* @return string|void
*/
public static function kind_archive_title( $title, $original_title = null, $prefix = null ) {
$return = array();
$wp_version = get_bloginfo( 'version' );
$prefix = apply_filters( 'get_the_archive_title_prefix', $prefix );
if ( is_tax() || is_category() || is_tag() ) {
$terms = self::get_terms_from_query();
foreach ( $terms as $term ) {
if ( 'kind' === $term->taxonomy ) {
$return[] = self::get_kind_info( $term->slug, 'name' );
} else {
$return[] = $term->name;
}
}
if ( $return ) {
$title = join( ', ', $return );
}
if ( is_year() ) {
/* translators: 1: Taxonomy. Yearly archive title. 2: Year */
$title = sprintf( __( '%1$1s - %2$2s', 'indieweb-post-kinds' ), $title, get_the_date( _x( 'Y', 'yearly archives date format', 'indieweb-post-kinds' ) ) );
} elseif ( is_month() ) {
/* translators: 1: Taxonomy. 2: Month name and year */
$title = sprintf( __( '%1$1s - %2$2s', 'indieweb-post-kinds' ), $title, get_the_date( _x( 'F Y', 'monthly archives date format', 'indieweb-post-kinds' ) ) );
} elseif ( is_day() ) {
/* translators: 1. Taxonomy . 1: Date */
$title = sprintf( __( '%1$1s - %2$2s', 'indieweb-post-kinds' ), $title, get_the_date( _x( 'F j, Y', 'daily archives date format', 'indieweb-post-kinds' ) ) );
}
} elseif ( get_query_var( 'kind_firehose' ) ) {
/* translators: 1. Title 2. Translated term for Firehose Feed */
$title = sprintf( __( '%1$s - %2$2s', 'indieweb-post-kinds' ), $title, __( 'Firehose', 'indieweb-post-kinds' ) );
}
if ( $prefix ) {
$title = sprintf(
/* translators: 1: Title prefix. 2: Title. */
_x( '%1$s %2$s', 'archive title', 'indieweb-post-kinds' ),
$prefix,
'<span>' . $title . '</span>'
);
}
return $title;
}
/**
* Filters the post kind archive description.
*
* @access public
*
* @param string $title Current archive description.
* @return string
*/
public static function kind_archive_description( $title ) {
$return = array();
if ( is_tax( 'kind' ) ) {
$terms = self::get_terms_from_query();
foreach ( $terms as $term ) {
if ( 'kind' === $term->taxonomy ) {
$return[] = self::get_kind_info( $term->slug, 'description' );
} else {
$return[] = $term->name;
}
}
if ( $return ) {
return join( '<br />', $return );
}
}
return $title;
}
/**
* Sets Post Format for Post Kind.
*
* @param int $post_id Post ID
* @param WP_Post $post Post Object
* @param boolean $update,
*/
public static function post_formats( $post_id, $post, $update ) {
$kind = get_post_kind_slug( $post_id );
if ( ! $update ) {
set_post_format( $post_id, self::get_kind_info( $kind, 'format' ) );
}
}
/**
* Fiters the `<title>` tag parts for the post kind.
*
* @access public
*
* @param array $parts Document title parts.
* @return mixed
*/
public static function document_title_parts( $parts ) {
if ( is_tax() || is_category() || is_tag() ) {
$terms = self::get_terms_from_query();
foreach ( $terms as $term ) {
if ( 'kind' === $term->taxonomy ) {
$return[] = self::get_kind_info( $term->slug, 'name' );
} else {
$return[] = $term->name;
}
}
if ( $return ) {
$parts['title'] = join( ', ', $return );
}
}
return $parts;
}
/**
* Callback for the taxonomy meta box for our post kinds taxonomy.
*
* @access public
*
* @param WP_Post $post Post object.
*/
public static function select_metabox( $post ) {
$include = get_option( 'kind_termslist' );
$include = array_merge( $include, array( 'note', 'reply', 'article' ) );
// If Simple Location is Enabled, include the check-in type
// Filter Kinds
$include = array_unique( apply_filters( 'kind_include', $include ) );
// Note cannot be removed or disabled without hacking the code
if ( ! in_array( 'note', $include, true ) ) {
$include[] = 'note';
}
if ( isset( $_GET['kind'] ) ) {
$default = get_term_by( 'slug', $_GET['kind'], 'kind' );
} elseif ( 'publish' === get_post_status( $post ) ) {
// On existing published posts without a kind fall back on article which most closely mimics the behavior of an unclassified post
$default = get_term_by( 'slug', 'article', 'kind' );
} else {
$default = get_term_by( 'slug', get_option( 'kind_default' ), 'kind' );
}
$terms = get_terms(
array(
'taxonomy' => 'kind',
'hide_empty' => 0,
)
);
$postterms = get_the_terms( $post->ID, 'kind' );
$current = ( $postterms ? array_pop( $postterms ) : false );
$current = ( $current ? $current->term_id : $default->term_id );
echo '<div id="kind-all">';
echo '<ul id="taxonomy-kind" class="list:kind category-tabs form-no-clear">';
foreach ( $terms as $term ) {
$id = 'kind-' . $term->term_id;
$slug = $term->slug;
if ( in_array( $slug, $include, true ) ) {
printf( '<li id="%1$s" class="kind-%2$s"><label class="selectit">', esc_attr( $id ), esc_attr( $slug ) );
printf( '<input type="radio" id="in-%1$s" name="tax_input[kind]" value="%2$s" %3$s />', esc_attr( $id ), esc_attr( $slug ), checked( $current, $term->term_id, false ) );
self::get_icon( $slug, true );
echo esc_html( self::get_kind_info( $slug, 'singular_name' ) );
echo '<br />';
echo '</label></li>';
}
}
echo '</ul></div>';
}