forked from argoproject/Argo
-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathrelated-content.php
830 lines (709 loc) · 23.1 KB
/
related-content.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
<?php
/**
* Show related tags and subcategories for each main category
* Used on category.php to display a list of related terms
*
* @since 0.5.5
* @return String HTML '' if there are no related topics or a UL if there are related topics
*/
function largo_get_related_topics_for_category( $obj ) {
$MAX_RELATED_TOPICS = 5;
if ( !isset( $obj->post_type ) ) {
$obj->post_type = 0;
}
if ( $obj->post_type ) {
if ( $obj->post_type == 'nav_menu_item' ) {
$cat_id = $obj->object_id;
}
} else {
$cat_id = $obj->cat_ID;
}
// spit out the subcategories
$outarray = array();
$cats = _subcategories_for_category( $cat_id );
foreach ( $cats as $c ) {
$outarray[] = sprintf( '<li><a href="%s">%s</a></li>',
get_category_link( $c->term_id ), $c->name
);
}
if ( count( $cats ) < $MAX_RELATED_TOPICS ) {
$tags = _tags_associated_with_category( $cat_id,
$MAX_RELATED_TOPICS - count( $cats ) );
foreach ( $tags as $t ) {
$outarray[] = sprintf( '<li><a href="%s">%s</a></li>',
get_tag_link( $t->term_id ), $t->name
);
}
}
$out = '';
// Generate the <ul>
if ( count( $outarray ) > 0 ) {
$out = "<ul>";
$title_ul = apply_filters( 'largo_related_topics_title_ul', __( 'Related Topics:' , 'largo' ) );
$out .= '<li><strong>' . $title_ul . '</strong></li>';
$out .= join( '', $outarray );
$out .= "</ul>";
}
return $out;
}
function _tags_associated_with_category( $cat_id, $max = 5 ) {
$query = new WP_Query( array(
'posts_per_page' => 100,
'cat' => $cat_id,
'update_post_meta_cache' => false,
'no_found_rows' => true,
) );
// Get a list of the tags used in posts in this category.
$tags = array();
$tag_objs = array();
foreach ( $query->posts as $post ) {
$ptags = get_the_tags( $post->ID );
if ( $ptags ) {
foreach ( $ptags as $tag ) {
if ( isset( $tags[$tag->term_id] ) ) {
$tags[ $tag->term_id ]++;
} else {
$tags[ $tag->term_id ] = 0;
}
$tag_objs[ $tag->term_id ] = $tag;
}
}
}
// Sort the most popular and get the $max results, or all results
// if max is -1
arsort( $tags, SORT_NUMERIC );
if ( $max == -1 ) {
$tag_keys = array_keys( $tags );
}
else {
$temporary_array_keys = array_keys( $tags );
$tag_keys = array_splice( $temporary_array_keys, 0, $max );
}
// Create an array of the selected tag objects
$return_tags = array();
foreach ( $tag_keys as $tk ) {
array_push( $return_tags, $tag_objs[ $tk ] );
}
return $return_tags;
}
function _subcategories_for_category( $cat_id ) {
// XXX: could also use get_term_children(). not sure which is better.
$cats = get_categories( array(
'child_of' => $cat_id,
) );
return $cats;
}
/**
* Provides topics (categories and tags) related to the current post in The
* Loop.
*
* @param int $max The maximum number of topics to return.
* @return array of term objects.
* @since 1.0
*/
function largo_get_post_related_topics( $max = 5 ) {
$cats = get_the_category();
$tags = get_the_tags();
$topics = array();
if ( $cats ) {
foreach ( $cats as $cat ) {
if ( $cat->name == 'Uncategorized' ) {
continue;
}
$posts = largo_get_recent_posts_for_term( $cat, 3, 2 );
if ( $posts ) {
$topics[] = $cat;
}
}
}
if ( $tags ) {
foreach ( $tags as $tag ) {
$posts = largo_get_recent_posts_for_term( $tag, 3, 2 );
if ( $posts ) {
$topics[] = $tag;
}
}
}
$topics = apply_filters( 'largo_get_post_related_topics', $topics, $max );
return array_slice( $topics, 0, $max );
}
/**
* Provides the recent posts for a term object (category, post_tag, etc).
* @uses global $post
* @param object $term A term object.
* @param int $max Maximum number of posts to return.
* @param int $min Minimum number of posts. If not met, returns false.
* @param Array $post__not_in Array of integer post IDs to be excluded from the query
* @return array|false of post objects.
* @since 1.0
*/
function largo_get_recent_posts_for_term( $term, $max = 5, $min = 1, $post__not_in = array() ) {
global $post;
$query_args = array(
'posts_per_page' => $max,
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'post__not_in' => $post__not_in,
);
// Exclude the current post if we're inside The Loop
// On the homepage, this excludes the most-recently-published post
if ( $post->ID ) {
$query_args[ 'post__not_in' ] = array_merge( array( $post->ID ), $query_args[ 'post__not_in' ] );
}
if ( $term->taxonomy == 'post_tag' ) {
// have to use tag__in because tag_id doesn't seem to work.
$query_args[ 'tag__in' ] = array( $term->term_id );
}
elseif ( $term->taxonomy == 'category' ) {
$query_args[ 'cat' ] = $term->term_id;
}
elseif ( $term->taxonomy == 'series' ) {
$query_args[ 'series' ] = $term->slug;
}
//if this is a fake term, just grab post ids
if ( $term->term_id == -90 && $post ) {
$post_ids = preg_split( '#\s*,\s*#', get_post_meta( $post->ID, 'largo_custom_related_posts', true ) );
$query_args[ 'post__in' ] = $post_ids;
$query_args[ 'orderby' ] = 'post__in';
$query_args['posts_per_page'] = count( $post_ids );
}
$query_args = apply_filters( 'largo_get_recent_posts_for_term_query_args', $query_args, $term, $max, $min, $post );
$query = new WP_Query( $query_args );
if ( count( $query->posts ) < $min ) {
return false;
}
return $query->posts;
}
/**
* Determine if a post has either categories or tags
*
* @return bool true is a post has categories or tags
* @since 1.0
*/
function largo_has_categories_or_tags() {
if ( get_the_tags() ) {
return true;
}
$cats = get_the_category();
if ( $cats ) {
foreach ( $cats as $cat ) {
if ( $cat->name != 'Uncategorized' ) {
return true;
}
}
}
return false;
}
/**
* Return (or echo) a list of categories and tags
*
* @param $max int number of categories and tags to return
* @param $echo bool echo the output or return it (default: echo)
* @param $link bool return the tags and category links or just the terms themselves
* @param $use_icon bool include the tag icon or not (used on single.php)
* @param $separator string to use as a separator between list items
* @param $item_wrapper string html tag to use as a wrapper for elements in the output
* @param $exclude array of term ids to exclude
* @return array of category and tag links
* @since 1.0
* @todo consider prioritizing tags by popularity?
*/
if ( ! function_exists( 'largo_categories_and_tags' ) ) {
function largo_categories_and_tags( $max = 5, $echo = true, $link = true, $use_icon = false, $separator = ', ', $item_wrapper = 'span', $exclude = array(), $rss = false ) {
$cats = get_the_category();
$tags = get_the_tags();
$icon = '';
$output = array();
// if $use_icon is true, include the markup for the tag icon
if ( $use_icon === true )
$icon = '<i class="icon-white icon-tag"></i>';
elseif ( $use_icon )
$icon = '<i class="icon-white icon-' . esc_attr( $use_icon ) . '"></i>';
if ( $cats ) {
foreach ( $cats as $cat ) {
// skip uncategorized and any others in the array of terms to exclude
if ( $cat->name == 'Uncategorized' || in_array( $cat->term_id, $exclude ) )
continue;
if ( $link ) {
$output[] = sprintf(
// translators: %1$s is an HTML tag. %2$S is a URL. %3$s is the plural name for posts. %4$s is a post category. %5$s is HTML for an icon. %6$s is a CSS class.
__( '<%1$s class="post-category-link %6$s"><a href="%2$s" title="Read %3$s in the %4$s category">%5$s%4$s</a></%1$s>', 'largo' ),
esc_attr( $item_wrapper ),
esc_attr( $rss ? get_category_feed_link( $cat->term_id ) : get_category_link( $cat->term_id ) ),
esc_html( of_get_option( 'posts_term_plural' ) ),
esc_html( $cat->name ),
$icon,
esc_attr( sprintf(
'%1$s-%2$s',
$cat->taxonomy,
$cat->slug
) )
);
} else {
$output[] = $cat->name;
}
}
}
if ( $tags ) {
foreach ( $tags as $tag ) {
if ( in_array( $tag->term_id, $exclude ) )
continue;
if ( $link ) {
$output[] = sprintf(
// translators: %1$s is an HTML tag. %2$S is a URL. %3$s is the plural name for posts. %4$s is a post tag. %5$s is HTML for an icon. %6$s is a CSS class.
__( '<%1$s class="post-tag-link %6$s"><a href="%2$s" title="Read %3$s tagged with: %4$s">%5$s%4$s</a></%1$s>', 'largo' ),
esc_attr( $item_wrapper ),
esc_attr( $rss ? get_tag_feed_link( $tag->term_id ) : get_tag_link( $tag->term_id ) ),
esc_html( of_get_option( 'posts_term_plural' ) ),
esc_html( $tag->name ),
$icon,
esc_attr( sprintf(
'%1$s-%2$s',
$tag->taxonomy,
$tag->slug
) )
);
} else {
$output[] = $tag->name;
}
}
}
if ( $echo )
echo implode( $separator, array_slice( $output, 0, $max ) );
return $output;
}
}
/**
* Returns (and optionally echoes) the 'top term' for a post, falling back to a category if one wasn't specified
*
* @param array|string $options Settings for post id, echo, link, use icon, wrapper and exclude
*/
function largo_top_term( $options = array() ) {
global $wpdb;
$defaults = array(
'post' => get_the_ID(),
'echo' => TRUE,
'link' => TRUE,
'use_icon' => FALSE,
'wrapper' => 'span', // an HTML tag ID
'exclude' => array(), //only for compatibility with largo_categories_and_tags
);
$args = wp_parse_args( $options, $defaults );
/*
* Try to get a term ID
* Or continue using 'none' if that is the case
*/
$term_id = get_post_meta( $args['post'], 'top_term', TRUE );
// Try to get the taxonomy for the term ID, but if it's 'none' for the "None" option, don't bother doing this.
if ( !empty( $term_id ) && $term_id !== 'none' ) {
//get the taxonomy slug
$taxonomy = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $term_id) );
}
// if no top_term specified, or if the top term is not in a taxonomy and the top term is not 'none',
if ( empty( $term_id ) || ( empty( $taxonomy ) && $term_id !== 'none' ) ) {
// Get the categories the post is in and try to use the first one as a term id
$term_id = get_the_category( $args['post'] );
if ( is_array( $term_id ) && count( $term_id ) ) {
$term_id = $term_id[0]->term_id;
}
// The post isn't in a category? Try post-types if that's enabled.
if ( empty( $term_id ) && taxonomy_exists( 'post-type' ) ) {
$term_id = get_the_terms( $args['post'], 'post-type' );
if ( is_array( $term_id ) && count( $term_id ) ) {
$term_id = $term_id[0]->term_id;
}
}
}
/*
* Using the term ID, get the term and then generate some text
*/
if ( $term_id && $term_id !== 'none' && !empty( $taxonomy ) ) {
$icon = ( $args['use_icon'] ) ? '<i class="icon-white icon-tag"></i>' : '' ; //this will probably change to a callback largo_term_icon() someday
$link = ( $args['link'] ) ? array( '<a href="%2$s" title="Read %3$s in the %4$s category">','</a>' ) : array( '', '' ) ;
// get the term object
$term = get_term( $term_id, $taxonomy );
if ( is_wp_error( $term ) ) return;
$output = sprintf(
'<%1$s class="post-category-link _top_term_output %6$s">'.$link[0].'%5$s%4$s'.$link[1].'</%1$s>',
$args['wrapper'],
get_term_link( $term ),
of_get_option( 'posts_term_plural' ),
$term->name,
$icon,
sprintf(
'%1$s-%2$s',
$term->taxonomy,
$term->slug
)
);
}
/*
* No output?
* generate a link to the post's category or tags
*/
if (
empty( $output )
&& 'none' !== $term_id
&& (int) $args['post'] === get_the_ID()
) {
// Can't pass a post ID to largo_categories_and_tags, so this may return the wrong links.
$lcat_output = largo_categories_and_tags( 1, false, $args['link'], $args['use_icon'], '', $args['wrapper'], $args['exclude'] );
if ( is_string( $lcat_output ) ) {
$output = $lcat_output;
} elseif ( is_array( $lcat_output ) ) {
if ( empty( $lcat_output ) ) {
$output = '';
} else {
foreach ( $lcat_output as $o ) {
if ( empty( $o ) ) {
continue;
}
if ( is_string( $o ) ) {
$output = $o;
break;
}
}
}
} else {
$output = '';
}
}
/*
* for https://github.com/INN/Largo/issues/1082, support not outputting anything
* @since 0.5.5
*/
if ( $term_id == 'none' ) {
$output = '';
}
if ( $args['echo'] ) echo $output;
return $output;
}
/**
* Add the post's top term to the post's post_class array
*
* @link https://github.com/INN/Largo/issues/1119
* @since 0.5.5
* @filter post_class
* @param array $classes An array of classes on the post
* @return array
*/
function largo_post_class_top_term( $classes ) {
global $post;
if ( is_a( $post, 'WP_Post' ) ) {
$top_term = get_post_meta( $post->ID, 'top_term', TRUE );
$term = get_term_by( 'id', $top_term, 'post_tag' );
// Don't output the class .top-term-- if there isn't a top term saved
if ( !empty( $term ) ) {
$classes[] = 'top-term-' . $term->taxonomy . '-' . $term->slug;
}
}
return $classes;
}
add_filter( 'post_class', 'largo_post_class_top_term' );
/**
*
*/
function largo_filter_get_post_related_topics( $topics, $max ) {
$post = get_post();
if ( $post ) {
$posts = preg_split( '#\s*,\s*#', get_post_meta( $post->ID, 'largo_custom_related_posts', true ) );
if ( !empty( $posts[0] ) ) {
// Add a fake term with the ID of -90
$top_posts = new stdClass();
$top_posts->term_id = -90;
$top_posts->name = __( 'Top Posts', 'largo' );
array_unshift( $topics, $top_posts );
}
}
return $topics;
}
add_filter( 'largo_get_post_related_topics', 'largo_filter_get_post_related_topics', 10, 2 );
/**
*
*/
function largo_filter_get_recent_posts_for_term_query_args( $query_args, $term, $max, $min, $post ) {
if ( $term->term_id == -90 ) {
$posts = preg_split( '#\s*,\s*#', get_post_meta( $post->ID, 'largo_custom_related_posts', true ) );
$query_args = array(
'posts_per_page' => $max,
'orderby' => 'post__in',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'post__in' => $posts,
);
}
return $query_args;
}
add_filter( 'largo_get_recent_posts_for_term_query_args', 'largo_filter_get_recent_posts_for_term_query_args', 10, 5 );
/**
* The Largo Related class.
* Used to dig through posts to find IDs related to the current post
*/
class Largo_Related {
var $number;
var $post_id;
var $post_ids = array();
var $post;
/**
* Constructor.
* Sets up essential parameters for retrieving related posts
*
* @access public
*
* @param integer $number optional The number of post IDs to fetch. Defaults to 1
* @param integer $post_id optional The ID of the post to get related posts about. If not provided, defaults to global $post
* @return null
*/
function __construct( $number = 1, $post_id = '' ) {
if ( ! empty( $number ) ) {
$this->number = $number;
}
if ( ! empty( $post_id ) ) {
$this->post_id = $post_id;
} else {
$this->post_id = get_the_ID();
}
$this->post = get_post( $this->post_id );
}
/**
* Array sorter for organizing terms by # of posts they have
*
* @param object $a First WP term object
* @param object $b Second WP term object
* @return integer
*/
function popularity_sort( $a, $b ) {
if ( $a->count == $b->count ) return 0;
return ( $a->count < $b->count ) ? -1 : 1;
}
/**
* Performs cleanup of IDs list prior to returning it. Also applies a filter.
*
* @access protected
*
* @return array The final array of related post IDs
*/
protected function cleanup_ids() {
//make things unique just to be safe
$ids = array_unique( $this->post_ids );
//truncate to desired length
$ids = array_slice( $ids, 0, $this->number );
//run filters
return apply_filters( 'largo_related_posts', $ids );
}
/**
* Fetches posts contained within the series(es) this post resides in. Feeds them into $this->post_ids array
*
* @access protected
* @see largo_series_custom_order
*/
protected function get_series_posts() {
//try to get posts by series, if this post is in a series
$series = get_the_terms( $this->post_id, 'series' );
if ( is_array( $series ) ) {
//loop thru all the series this post belongs to
foreach ( $series as $term ) {
//start to build our query of posts in this series
// get the posts in this series, ordered by rank or (if missing?) date
$args = array(
'post_type' => 'post',
'posts_per_page' => $this->number,
'taxonomy' => 'series',
'term' => $term->slug,
'orderby' => 'date',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $this->post_id ),
'date_query' => array(
'after' => $this->post->post_date,
),
);
// see if there's a post that has the sort order info for this series
$cftl_query = new WP_Query( array(
'post_type' => 'cftl-tax-landing',
'tax_query' => array (
'series' => $term->slug,
),
'posts_per_page' => 1
));
if ( $cftl_query->have_posts() ) {
$cftl_query->next_post();
$has_order = get_post_meta( $cftl_query->post->ID, 'post_order', TRUE );
if ( !empty( $has_order ) ) {
switch ( $has_order ) {
case 'ASC':
$args['order'] = 'ASC';
break;
// 'series_custom' and 'featured' are custom ones, caught with largo_series_custom_order in inc/wp-taxonomy-landing/functions/cftl-series-order.php
case 'custom':
$args['orderby'] = 'series_custom';
break;
case 'featured, DESC':
case 'featured, ASC':
$args['orderby'] = $has_order;
break;
}
}
}
// build the query with the sort defined
$series_query = new WP_Query( $args );
// If not enough posts were added from after this post, look before this post
if ( count( $series_query->posts ) < $this->number ) {
// Store the returned posts from the after query
$this->add_from_query( $series_query );
// Change it to look backwards
$args['date_query'] = array(
'before' => $this->post->post_date,
);
// rerun the query
$series_query = new WP_Query( $args );
}
// Store the posts
if ( $series_query->have_posts() ) {
$this->add_from_query( $series_query );
if ( $this->have_enough_posts() ) {
break;
}
}
}
}
}
/**
* Fetches posts contained within the categories and tags this post has. Feeds them into $this->post_ids array
*
* @access protected
*/
protected function get_term_posts() {
//we've gone back and forth through all the post's series, now let's try traditional taxonomies
$taxonomies = array();
foreach ( array( 'category', 'post_tag' ) as $_taxonomy ) {
$_terms = get_object_term_cache( $this->post_id, $_taxonomy );
if ( false === $_terms ) {
$_terms = wp_get_object_terms( $this->post_id, $_taxonomy );
wp_cache_add( $this->post_id, $taxonomies, $_taxonomy . '_relationships' );
}
if ( is_array( $_terms ) ) {
$taxonomies = array_merge( $taxonomies, $_terms );
}
}
//loop thru taxonomies, much like series, and get posts
if ( is_array( $taxonomies ) ) {
//sort by popularity
usort( $taxonomies, array( __CLASS__, 'popularity_sort' ) );
foreach ( $taxonomies as $term ) {
$args = array(
'post_type' => 'post',
'posts_per_page' => $this->number,
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $this->post_id ),
'date_query' => array(
'after' => $this->post->post_date,
),
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'terms' => $term->slug,
'field' => 'slug',
)
)
);
// run the query
$term_query = new WP_Query( $args );
// If not enough posts were added from after this post, look before this post
if ( count( $term_query->posts ) < $this->number ) {
// Store the returned posts from the after query
$this->add_from_query( $term_query );
// Change it to look backwards
$args['date_query'] = array(
'before' => $this->post->post_date,
);
// rerun the query
$term_query = new WP_Query( $args );
}
// Store the returned posts
if ( $term_query->have_posts() ) {
$this->add_from_query( $term_query );
if ( $this->have_enough_posts() ) {
break;
}
}
} // foreach
}
}
/**
* Fetches recent posts. Used as a fallback when other methods have failed to fill post_ids to requested length
*
* @access protected
*/
protected function get_recent_posts() {
$args = array(
'post_type' => 'post',
'posts_per_page' => $this->number,
'post__not_in' => array( $this->post_id ),
);
$posts_query = new WP_Query( $args );
if ( $posts_query->have_posts() ) {
$this->add_from_query( $posts_query );
}
}
/**
* Loops through series, terms and recent to fill array of related post IDs. Primary means of using this class.
*
* @access public
*
* @return array An array of post ids related to the given post
*/
public function ids() {
//see if this post has manually set related posts
$post_ids = get_post_meta( $this->post_id, 'largo_custom_related_posts', true );
if ( ! empty( $post_ids ) ) {
$this->post_ids = explode( ",", str_replace( ' ', '', $post_ids ) );
if ( $this->have_enough_posts() ) {
return $this->cleanup_ids();
}
}
$this->get_series_posts();
//are we done yet?
if ( $this->have_enough_posts() ) return $this->cleanup_ids();
$this->get_term_posts();
//are we done yet?
if ( $this->have_enough_posts() ) return $this->cleanup_ids();
$this->get_recent_posts();
return $this->cleanup_ids();
}
/**
* Takes a WP_Query result and adds the IDs to $this->post_ids
*
* @access protected
*
* @param object a WP_Query object
* @param boolean optional whether the query post order has been reversed yet. If not, this will loop through in both directions.
*/
protected function add_from_query( $q, $reversed = FALSE ) {
// don't pick up anything until we're past our own post
$found_ours = FALSE;
while ( $q->have_posts() ) {
$q->the_post();
// add this post if it's new
if (
! in_array( $q->post->ID, $this->post_ids ) // only add it if it wasn't already there
&& $q->post->ID != $this->post_id // do not add the id of the current post, because we do not want to ever return that
) {
$this->post_ids[] = (int) trim( $q->post->ID );
// stop if we have enough
if ( $this->have_enough_posts() ) return;
}
}
}
/**
* Counts to see if enough posts have been found
*/
protected function have_enough_posts() {
if ( count( $this->post_ids ) >= $this->number )
return true;
return false;
}
}