featured-posts.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @package Apostrophe 2
  4. *
  5. * Featured post functionality
  6. */
  7. function apostrophe_2_get_featured_posts() {
  8. return apply_filters( 'apostrophe_2_get_featured_posts', array() );
  9. }
  10. /**
  11. * Looks up featured posts via a filter or uses ones provided by Jetpack.
  12. *
  13. * @return WP_Query
  14. */
  15. function apostrophe_2_has_featured_posts( $minimum = 1 ) {
  16. if ( is_paged() )
  17. return false;
  18. $minimum = absint( $minimum );
  19. $featured_posts = apply_filters( 'apostrophe_2_get_featured_posts', array() );
  20. if ( ! is_array( $featured_posts ) )
  21. return false;
  22. if ( $minimum > count( $featured_posts ) )
  23. return false;
  24. return true;
  25. }
  26. /**
  27. * Returns true if the given post is featured.
  28. *
  29. * @return bool Whether the given post is featured or not.
  30. */
  31. function apostrophe_2_is_featured( $post_id = null ) {
  32. $post = get_post( $post_id );
  33. $featured = false;
  34. $term_id = apostrophe_2_get_jetpack_featured_content_term_id();
  35. if ( ! $term_id ) {
  36. return $featured;
  37. }
  38. $post_tags = wp_get_object_terms( $post->ID, 'post_tag' );
  39. if ( in_array( $term_id, wp_list_pluck( $post_tags, 'term_id' ) ) ) {
  40. $featured = true;
  41. }
  42. return $featured;
  43. }
  44. /*
  45. * Gets the featured content by ID.
  46. */
  47. function apostrophe_2_get_jetpack_featured_content_term_id() {
  48. if ( ! method_exists( 'Featured_Content', 'get_setting' ) ) {
  49. return 0;
  50. }
  51. $term = get_term_by( 'name', Featured_Content::get_setting( 'tag-name' ), 'post_tag' );
  52. if ( ! $term ) {
  53. return 0;
  54. }
  55. return $term->term_id;
  56. }