forked from WordPress/phpdoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-functions.php
More file actions
1743 lines (1532 loc) · 53.1 KB
/
api-functions.php
File metadata and controls
1743 lines (1532 loc) · 53.1 KB
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
use Aivec\Plugins\DocParser\Formatting;
use Aivec\Plugins\DocParser\Registrations;
use Aivec\Plugins\DocParser\Views\ImporterPage\ImporterPage;
/**
* Returns the slug for the source type taxonomy
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string
*/
function avcpdp_get_source_type_taxonomy_slug() {
return Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG;
}
/**
* Get an array of all parsed post types.
*
* @param string $labels If set to 'labels' post types with their labels are returned.
* @return array
*/
function avcpdp_get_parsed_post_types($labels = '') {
$post_types = [
'wp-parser-class' => __('Classes', 'wp-parser'),
'wp-parser-function' => __('Functions', 'wp-parser'),
'wp-parser-hook' => __('Hooks', 'wp-parser'),
'wp-parser-method' => __('Methods', 'wp-parser'),
];
if ('labels' !== $labels) {
return array_keys($post_types);
}
return $post_types;
}
/**
* Checks if given post type is one of the parsed post types.
*
* If no post type is provided, this function will use the post type of
* the current post, or the queried post type if archive or search
*
* If archive or search, `false` will be returned if any of the queried
* post types are not a `wp-parser-*` post type
*
* @param null|string $post_type Optional. The post type. Default null.
* @return bool
*/
function avcpdp_is_parsed_post_type($post_type = null) {
if (!empty($post_type)) {
return in_array($post_type, avcpdp_get_parsed_post_types(), true);
}
if (is_single()) {
return in_array(get_post_type(), avcpdp_get_parsed_post_types(), true);
}
$ptypes = get_query_var('post_type');
$ptypes = !empty($ptypes) ? $ptypes : '';
if (is_array($ptypes)) {
foreach ($ptypes as $ptype) {
if (!in_array($ptype, avcpdp_get_parsed_post_types(), true)) {
return false;
}
}
} else {
if (!in_array($ptypes, avcpdp_get_parsed_post_types(), true)) {
return false;
}
}
return true;
}
/**
* Gets the current parsed post type
*
* This function will use the post type of the current post, or the
* queried post type if archive or search
*
* If archive or search, `null` will be returned if any of the queried
* post types are not a `wp-parser-*` post type
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string|string[]|null
*/
function avcpdp_get_parsed_post_type() {
if (is_single()) {
$sptype = get_post_type();
if (!in_array($sptype, avcpdp_get_parsed_post_types(), true)) {
return null;
}
return $sptype;
}
$ptypes = get_query_var('post_type');
$ptypes = !empty($ptypes) ? $ptypes : null;
if ($ptypes === null) {
return null;
}
if (is_array($ptypes)) {
foreach ($ptypes as $ptype) {
if (!in_array($ptype, avcpdp_get_parsed_post_types(), true)) {
return null;
}
}
} else {
if (!in_array($ptypes, avcpdp_get_parsed_post_types(), true)) {
return null;
}
}
return $ptypes;
}
/**
* Get the specific type of hook.
*
* @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post.
* @return string Either 'action', 'filter', or an empty string if not a hook post type.
*/
function avcpdp_get_hook_type($post = null) {
$hook = '';
if ('wp-parser-hook' === get_post_type($post)) {
$hook = get_post_meta(get_post_field('ID', $post), '_wp-parser_hook_type', true);
}
return $hook;
}
/**
* Returns the array of post types that have source code.
*
* @return array
*/
function avcpdp_get_post_types_with_source_code() {
return ['wp-parser-class', 'wp-parser-method', 'wp-parser-function'];
}
/**
* Does the post type have source code?
*
* @param null|string $post_type Optional. The post type name. If null, assumes current post type. Default null.
* @return bool
*/
function avcpdp_post_type_has_source_code($post_type = null) {
$post_type = $post_type ? $post_type : get_post_type();
return in_array($post_type, avcpdp_get_post_types_with_source_code());
}
/**
* Returns the SVG logo term meta for the current reference
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return array {
* The SVG logo term meta data
*
* @type string $file Image path
* @type string $url Image URL
* @type string $type File extension
* }
*/
function avcpdp_get_reference_logo() {
$stterms = avcpdp_get_source_type_terms();
if (empty($stterms)) {
return null;
}
$svglogo = get_term_meta($stterms['name']->term_id, 'item_image', true);
if (empty($svglogo)) {
return null;
}
return $svglogo;
}
/**
* Returns search page link
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $s
* @param array $args
* @param array $post_types
* @return string
*/
function avcpdp_get_search_link($s = '', $args = [], $post_types = []) {
$args = avcpdp_get_search_args($s, $args, $post_types);
foreach ($args as $argk => $argsv) {
if (is_string($argsv)) {
$args[$argk] = rawurlencode($argsv);
}
}
return add_query_arg($args, home_url('/'));
}
/**
* Returns search arguments
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $s
* @param array $args
* @param array $post_types
* @return (string|array)[]
*/
function avcpdp_get_search_args($s = '', $args = [], $post_types = []) {
$sttstring = '';
$stterms = avcpdp_get_source_type_terms();
if (!empty($stterms)) {
$sttstring = $stterms['type']->slug . ',' . $stterms['name']->slug;
}
return array_merge(
[
's' => $s,
'post_type' => $post_types,
'avcpdp_search' => 1,
'taxonomy' => Registrations::SOURCE_TYPE_TAX_SLUG,
Registrations::SOURCE_TYPE_TAX_SLUG => $sttstring,
],
$args
);
}
/**
* Returns `true` if the current query is the main query and a search query
* for at least one of the 4 `wp-parser-*` reference post types
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return bool
*/
function avcpdp_is_reference_search() {
// only process main query
if (!is_main_query()) {
return false;
}
// check if search query
if (!is_search()) {
return false;
}
return avcpdp_is_parsed_post_type();
}
/**
* Returns source type "type" and "name" terms for the current page
*
* If the current page is an archive or search page and at least one
* of the queried post types is a `wp-parser-*` post type, this function
* will attempt to extract the source type terms from the taxonomy query.
*
* If the current page is a single page, source type terms associated with
* the post ID will be returned.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return array {
* A key-value map of source type terms. Empty array if the source type terms could
* not be determined
*
* @type \WP_Term $type The type of source (plugin, theme, or composer-package)
* @type \WP_Term $name The unique name for the source (eg: my-plugin)
* }
*/
function avcpdp_get_source_type_terms() {
if (is_archive() || is_search()) {
return avcpdp_get_reference_archive_source_type_terms();
}
return avcpdp_get_post_source_type_terms();
}
/**
* Returns key-value list of all source types and their corresponding child terms
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return (WP_Term[])[]
*/
function avcpdp_get_all_source_type_terms() {
$pterm = avcpdp_get_source_type_plugin_term();
$tterm = avcpdp_get_source_type_theme_term();
$cterm = avcpdp_get_source_type_composer_package_term();
$taxslug = avcpdp_get_source_type_taxonomy_slug();
$pterms = [];
if (!empty($pterm)) {
$pterms = get_terms([
'taxonomy' => $taxslug,
'parent' => $pterm->term_id,
]);
$pterms = $pterms instanceof WP_Error ? [] : $pterms;
}
$tterms = [];
if (!empty($tterm)) {
$tterms = get_terms([
'taxonomy' => $taxslug,
'parent' => $tterm->term_id,
]);
$tterms = $tterms instanceof WP_Error ? [] : $tterms;
}
$cterms = [];
if (!empty($cterm)) {
$cterms = get_terms([
'taxonomy' => $taxslug,
'parent' => $cterm->term_id,
]);
$cterms = $cterms instanceof WP_Error ? [] : $cterms;
}
return [
'plugin' => $pterms,
'theme' => $tterms,
'composer-package' => $cterms,
];
}
/**
* Returns source type "type" and "name" terms for the current post
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param int|null $post_id
* @return array
*/
function avcpdp_get_post_source_type_terms($post_id = null) {
if ($post_id === null) {
$post_id = get_the_ID();
}
if (empty($post_id)) {
return [];
}
$terms = wp_get_post_terms($post_id, Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
if (empty($terms)) {
return [];
}
$res = [];
foreach ($terms as $term) {
if ($term->parent === 0) {
$res['type'] = $term;
} else {
$res['name'] = $term;
}
}
if (empty($res['type']) || empty($res['name'])) {
return [];
}
return $res;
}
/**
* Returns the source type terms for the `wp-parser-*` post type currently being queried
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return WP_Term[]
*/
function avcpdp_get_reference_archive_source_type_terms() {
if (!is_archive() && !is_search()) {
// not a code reference archive, cannot get source type terms
return [];
}
// only get source type terms from `wp-parser-*` post types
/*
$ptypes = get_query_var('post_type');
$ptypes = !empty($ptypes) ? $ptypes : '';
if (is_array($ptypes)) {
foreach ($ptypes as $ptype) {
if (!in_array($ptype, avcpdp_get_parsed_post_types(), true)) {
return false;
}
}
} else {
if (!in_array($ptypes, avcpdp_get_parsed_post_types(), true)) {
return false;
}
} */
$stype = get_query_var(\Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
if (empty($stype)) {
// source type not queried for, cannot determine URL
return [];
}
$stypepieces = explode(',', $stype);
if (!avcpdp_source_type_term_slugs_are_valid($stypepieces)) {
// the combination of source type terms are not valid
return [];
}
$terms = avcpdp_get_source_type_terms_from_slug_pair($stypepieces);
return $terms;
}
/**
* Returns the base URL for the `wp-parser-*` post type of the current post/query
*
* This function will return an empty string if the current main query is not related
* to a reference post type or if the current page is a singular page but the post
* type is not a reference post type
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string Example: `/reference/plugin/my-plugin/functions`
*/
function avcpdp_get_reference_base_url() {
$baseurl = avcpdp_get_reference_type_base_url();
if (empty($baseurl)) {
return '';
}
$ptype = avcpdp_get_parsed_post_type();
if (!is_string($ptype)) {
return '';
}
$parsertype = avcpdp_get_reference_post_type_url_slug($ptype);
$baseurl .= "/{$parsertype}";
return $baseurl;
}
/**
* Returns URL portion for a `wp-parser-*` post type
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $ptype
* @return string
*/
function avcpdp_get_reference_post_type_url_slug($ptype) {
if (!avcpdp_is_parsed_post_type($ptype)) {
return '';
}
return \Aivec\Plugins\DocParser\Registrations::WP_PARSER_PT_MAP[$ptype]['urlpiece'];
}
/**
* Returns the base URL for the source type terms of the current post/query
*
* This function will return an empty string if the current main query does not
* contain a source type taxonomy search or if the current page is a singular page
* but the post does not have source type terms applied to it.
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return string Example: `/reference/plugin/my-plugin`
*/
function avcpdp_get_reference_type_base_url() {
$stterms = avcpdp_get_source_type_terms();
if (empty($stterms)) {
return '';
}
$type = $stterms['type']->slug;
$name = $stterms['name']->slug;
$baseurl = home_url("/reference/{$type}/{$name}");
return $baseurl;
}
/**
* Returns the base URL for the current reference single post.
*
* The URL is constructed from the source type terms assigned to the post. The
* `wp-parser-*` post type URL piece is **not appended**.
*
* Example: `/reference/plugin/my-plugin`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param int|null $pid Optional. Post ID. Defaults to current post
* @return string Returns an empty string if the URL cannot be determined
*/
function avcpdp_get_reference_single_base_url($pid = null) {
if (!is_single($pid)) {
return '';
}
if (empty($pid)) {
$pid = get_the_ID();
}
if (!avcpdp_source_type_terms_are_valid_for_post($pid)) {
return '';
}
$stterms = avcpdp_get_post_source_type_terms($pid);
$type = $stterms['type']->slug;
$name = $stterms['name']->slug;
$baseurl = home_url("/reference/{$type}/{$name}");
return $baseurl;
}
/**
* Returns hierarchical descending array of reference landing page posts tied to the current source types terms
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return array
*/
function avcpdp_get_reference_landing_page_posts_from_source_type_terms() {
$stterms = avcpdp_get_source_type_terms();
if (empty($stterms)) {
return [];
}
$trail = [];
$stypelanding = get_posts([
'order' => 'ASC',
'orderby' => 'parent',
'post_status' => ['publish', 'private'],
'post_type' => Aivec\Plugins\DocParser\Registrations::CODE_REFERENCE_POST_TYPE,
'tax_query' => [
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'term_id',
'terms' => $stterms['type']->term_id,
'include_children' => false,
],
],
]);
if (!empty($stypelanding)) {
if ($stypelanding[0]->post_status === 'publish') {
$trail[] = $stypelanding[0];
}
$sourcelanding = get_posts([
'post_parent' => $stypelanding[0]->ID,
'post_status' => 'publish',
'post_type' => Aivec\Plugins\DocParser\Registrations::CODE_REFERENCE_POST_TYPE,
'tax_query' => [
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'term_id',
'terms' => $stterms['name']->term_id,
'include_children' => false,
],
],
]);
if (!empty($sourcelanding)) {
$trail[] = $sourcelanding[0];
}
}
return $trail;
}
/**
* Checks whether the source type terms are valid for a post
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param int|null $post_id
* @return bool
*/
function avcpdp_source_type_terms_are_valid_for_post($post_id = null) {
if ($post_id === null) {
$post_id = get_the_ID();
}
if (empty($post_id)) {
return false;
}
$terms = wp_get_post_terms($post_id, Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
return avcpdp_source_type_terms_are_valid($terms);
}
/**
* Returns an array of source type terms given an array of source type slugs
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $termslugs
* @return WP_Term[]
*/
function avcpdp_get_source_type_terms_from_slug_pair($termslugs) {
$terms = [];
foreach ($termslugs as $termslug) {
$term = get_term_by('slug', $termslug, Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
if (empty($term)) {
return false;
}
if ($term->parent === 0) {
$terms['type'] = $term;
} else {
$terms['name'] = $term;
}
}
return $terms;
}
/**
* Checks whether an array of source type term slugs is a valid combination
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $termslugs
* @return bool
*/
function avcpdp_source_type_term_slugs_are_valid($termslugs) {
if (empty($termslugs)) {
return false;
}
if (count($termslugs) > 2) {
// a post can only have one source type and one source type name
return false;
}
$terms = avcpdp_get_source_type_terms_from_slug_pair($termslugs);
return avcpdp_source_type_terms_are_valid($terms);
}
/**
* Checks whether an array of source types is a valid combination
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $terms
* @return bool
*/
function avcpdp_source_type_terms_are_valid($terms) {
if (empty($terms)) {
return false;
}
if (count($terms) > 2) {
// a post can only have one source type and one source type name
return false;
}
$res = [];
foreach ($terms as $term) {
if ($term->parent === 0) {
$res['type'] = $term;
} else {
$res['name'] = $term;
}
}
if (empty($res['type']) || empty($res['name'])) {
// source type and source type name are required
return false;
}
if ($res['name']->parent !== $res['type']->term_id) {
// source type name must be a child of source type
return false;
}
return true;
}
/**
* Returns source type taxonomy "plugin" term
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return WP_Term|false
*/
function avcpdp_get_source_type_plugin_term() {
return get_term_by('slug', 'plugin', Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
}
/**
* Returns source type taxonomy "theme" term
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return WP_Term|false
*/
function avcpdp_get_source_type_theme_term() {
return get_term_by('slug', 'theme', Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
}
/**
* Returns source type taxonomy "composer-package" term
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return WP_Term|false
*/
function avcpdp_get_source_type_composer_package_term() {
return get_term_by('slug', 'composer-package', Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG);
}
/**
* Returns list of child terms for the source type taxonomy "plugin" term
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @return array
*/
function avcpdp_get_source_type_plugin_terms() {
$term = avcpdp_get_source_type_plugin_term();
if (empty($term)) {
return [];
}
$pterms = get_terms([
'parent' => $term->term_id,
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
]);
if (empty($pterms) || $pterms instanceof WP_Error) {
return [];
}
return $pterms;
}
/**
* Returns list of reference post type posts for a given role slug
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param WP_Term[] $stterms
* @param string $role
* @param array|string $post_type
* @param int $posts_per_page
* @return WP_Query
*/
function avcpdp_get_reference_post_list_by_role($stterms, $role, $post_type, $posts_per_page = 5) {
if (empty($post_type)) {
$post_type = avcpdp_get_parsed_post_types();
}
$q = new WP_Query([
'fields' => 'ids',
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::ROLE_TAX_SLUG,
'field' => 'slug',
'terms' => $role,
'include_children' => true,
],
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'slug',
'terms' => [$stterms['type']->slug, $stterms['name']->slug],
'include_children' => false,
'operator' => 'AND',
],
],
]);
return $q;
}
/**
* Returns list of tags that have at least one association with a `wp-parser-*` post
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $stterms Source type terms
* @param string $fields
* @param bool $hide_empty
* @return WP_Term[]
*/
function avcpdp_get_associated_tags($stterms, $fields = 'all', $hide_empty = true) {
$terms = get_tags([
'hide_empty' => $hide_empty,
'fields' => $fields,
]);
if ($terms instanceof WP_Error) {
return [];
}
$tagswithposts = [];
foreach ($terms as $tag) {
$q = new WP_Query([
'fields' => 'ids',
'post_type' => avcpdp_get_parsed_post_types(),
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag->slug,
'include_children' => true,
],
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'slug',
'terms' => [$stterms['type']->slug, $stterms['name']->slug],
'include_children' => false,
'operator' => 'AND',
],
],
]);
if ($q->post_count > 0) {
$tagswithposts[] = $tag;
}
}
return $tagswithposts;
}
/**
* Returns list of role terms for a source
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param array $stterms Source type terms
* @param array|string $post_type
* @param string $fields
* @param bool $hide_empty
* @return WP_Term[]
*/
function avcpdp_get_role_terms($stterms, $post_type, $fields = 'all', $hide_empty = true) {
$terms = get_terms([
'taxonomy' => Aivec\Plugins\DocParser\Registrations::ROLE_TAX_SLUG,
'hide_empty' => $hide_empty,
'fields' => $fields,
]);
if ($terms instanceof WP_Error) {
return [];
}
if (empty($post_type)) {
$post_type = avcpdp_get_parsed_post_types();
}
$roleswithposts = [];
foreach ($terms as $role) {
$q = new WP_Query([
'fields' => 'ids',
'post_type' => $post_type,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::ROLE_TAX_SLUG,
'field' => 'slug',
'terms' => $role->slug,
'include_children' => true,
],
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'slug',
'terms' => [$stterms['type']->slug, $stterms['name']->slug],
'include_children' => false,
'operator' => 'AND',
],
],
]);
if ($q->post_count > 0) {
$roleswithposts[] = $role;
}
}
return $roleswithposts;
}
/**
* Returns list of hook reference post IDs
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param WP_Term[] $stterms
* @param string $hook_type
* @param int $posts_per_page
* @return WP_Query
*/
function avcpdp_get_hook_reference_posts($stterms, $hook_type = 'all', $posts_per_page = 20) {
return avcpdp_get_reference_posts(
'wp-parser-hook',
$stterms,
$posts_per_page,
['hook_type' => $hook_type] // custom param. Handled in \Aivec\Plugins\DocParser\Queries::preGetPosts()
);
}
/**
* Returns list of function reference post IDs
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param WP_Term[] $stterms
* @param int $posts_per_page
* @return WP_Query
*/
function avcpdp_get_function_reference_posts($stterms, $posts_per_page = 20) {
return avcpdp_get_reference_posts('wp-parser-function', $stterms, $posts_per_page);
}
/**
* Returns list of method reference post IDs
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param WP_Term[] $stterms
* @param int $posts_per_page
* @return WP_Query
*/
function avcpdp_get_method_reference_posts($stterms, $posts_per_page = 20) {
return avcpdp_get_reference_posts('wp-parser-method', $stterms, $posts_per_page);
}
/**
* Returns list of class reference post IDs
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param WP_Term[] $stterms
* @param int $posts_per_page
* @return WP_Query
*/
function avcpdp_get_class_reference_posts($stterms, $posts_per_page = 20) {
return avcpdp_get_reference_posts('wp-parser-class', $stterms, $posts_per_page);
}
/**
* Returns list of function reference post IDs
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string|string[] $ptype
* @param WP_Term[] $stterms
* @param int $posts_per_page
* @param array $qparams
* @return WP_Query
*/
function avcpdp_get_reference_posts($ptype, $stterms, $posts_per_page = 20, $qparams = []) {
$params = [
'fields' => 'ids',
'post_type' => $ptype,
'posts_per_page' => $posts_per_page,
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'slug',
'terms' => [$stterms['type']->slug, $stterms['name']->slug],
'include_children' => false,
'operator' => 'AND',
],
],
];
$q = new WP_Query(array_merge($params, $qparams));
return $q;
}
/**
* Returns all wp-parser-* posts associated with a given source type and name pair
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $source_type
* @param string $source_name
* @return WP_Query
*/
function avcpdp_get_all_parser_posts_for_source($source_type, $source_name) {
$q = new WP_Query([
'fields' => 'ids',
'post_status' => ['any'],
'post_type' => avcpdp_get_parsed_post_types(),
'posts_per_page' => -1,
'tax_query' => [
[
'taxonomy' => Aivec\Plugins\DocParser\Registrations::SOURCE_TYPE_TAX_SLUG,
'field' => 'slug',
'terms' => [$source_type, $source_name],
'include_children' => false,
'operator' => 'AND',
],
],
]);
return $q;
}
/**
* Returns list of reference post type posts that have at least one role assigned to them
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param int|null $post_id
* @return WP_Term[]
*/
function avcpdp_get_reference_post_roles($post_id = null) {
if (empty($post_id)) {
$post_id = get_the_ID();
}
if (empty($post_id)) {
return [];
}
$terms = wp_get_post_terms($post_id, Aivec\Plugins\DocParser\Registrations::ROLE_TAX_SLUG);
if ($terms instanceof WP_Error) {
return [];
}
return $terms;
}
/**
* Returns a role term given a role slug
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param string $slug
* @return WP_Term|false
*/
function avcpdp_get_role_term_by_slug($slug) {
return get_term_by('slug', $slug, Aivec\Plugins\DocParser\Registrations::ROLE_TAX_SLUG);
}
/**
* Returns version number of the imported source.
*
* The the post ID must have a source type term associated with it for this
* function to return a value other than `null`
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @param null|int $post_id