jetpack.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Jetpack Compatibility File.
  4. *
  5. * @link https://jetpack.me/
  6. *
  7. * @package AltoFocus
  8. */
  9. /**
  10. * Jetpack setup function.
  11. *
  12. * See: https://jetpack.me/support/infinite-scroll/
  13. * See: https://jetpack.me/support/responsive-videos/
  14. */
  15. function altofocus_jetpack_setup() {
  16. // Add theme support for Infinite Scroll.
  17. add_theme_support( 'infinite-scroll', array(
  18. 'type' => 'click',
  19. 'container' => 'main',
  20. 'render' => 'altofocus_infinite_scroll_render',
  21. 'wrapper' => false,
  22. 'footer' => 'page',
  23. ) );
  24. // Custom logo fallback
  25. add_theme_support( 'custom-logo', array(
  26. 'height' => 120,
  27. 'width' => 120,
  28. 'flex-width' => true,
  29. 'header-text' => array(
  30. 'site-title',
  31. 'site-description'
  32. ),
  33. ) );
  34. // Add theme support for Responsive Videos.
  35. add_theme_support( 'jetpack-responsive-videos' );
  36. // Add theme support for Social Menus
  37. add_theme_support( 'jetpack-social-menu', 'svg' );
  38. // Featured content
  39. add_theme_support( 'featured-content', array(
  40. 'filter' => 'altofocus_get_featured_posts',
  41. 'max_posts' => 999,
  42. 'post_types' => array( 'post' ),
  43. ) );
  44. // Content Options
  45. add_theme_support( 'jetpack-content-options', array(
  46. 'author-bio' => false,
  47. 'post-details' => array(
  48. 'stylesheet' => 'altofocus-style',
  49. 'date' => '.posted-on',
  50. 'categories' => '.cat-links',
  51. 'tags' => '.tags-links',
  52. 'author' => '.byline',
  53. ),
  54. 'featured-images' => array(
  55. 'archive' => true,
  56. 'post' => true,
  57. 'post-default' => true,
  58. 'page' => false,
  59. 'page-default' => false,
  60. 'fallback' => true,
  61. ),
  62. ) );
  63. }
  64. add_action( 'after_setup_theme', 'altofocus_jetpack_setup' );
  65. /**
  66. * Getter function for Featured Content
  67. *
  68. * @return (string) The value of the filter defined in add_theme_support( 'featured-content' ).
  69. */
  70. function altofocus_get_featured_posts() {
  71. return apply_filters( 'altofocus_get_featured_posts', array() );
  72. }
  73. /**
  74. * Helper function to check for Featured Content
  75. *
  76. * @param (integer)
  77. * @return (boolean) true/false
  78. */
  79. function altofocus_has_featured_posts( $minimum = 1 ) {
  80. if ( is_paged() )
  81. return false;
  82. $minimum = absint( $minimum );
  83. $featured_posts = apply_filters( 'altofocus_get_featured_posts', array() );
  84. if ( ! is_array( $featured_posts ) )
  85. return false;
  86. if ( $minimum > count( $featured_posts ) )
  87. return false;
  88. return true;
  89. }
  90. /**
  91. * Get featured post IDs
  92. *
  93. * @return array()
  94. */
  95. function altofocus_get_featured_post_ids() {
  96. // Get array of cached results if they exist.
  97. $featured_ids = get_transient( 'featured_content_ids' );
  98. if ( false === $featured_ids ) {
  99. $featured_options = get_option( 'featured-content', FALSE );
  100. $featured_tag_name = $featured_options[ 'tag-name' ] ?? null;
  101. $term = get_term_by( 'name', $featured_tag_name, 'post_tag' );
  102. if ( $term ) {
  103. // Query for featured posts.
  104. $featured_ids = get_posts( array(
  105. 'fields' => 'ids',
  106. 'numberposts' => $max_posts,
  107. 'post_type' => array( 'post' ),
  108. 'suppress_filters' => false,
  109. 'tax_query' => array(
  110. array(
  111. 'field' => 'term_id',
  112. 'taxonomy' => 'post_tag',
  113. 'terms' => $term->term_id,
  114. ),
  115. ),
  116. )
  117. );
  118. }
  119. set_transient( 'featured_content_ids', $featured_ids );
  120. }
  121. // Ensure correct format before return.
  122. if ( ! is_array( $featured_ids ) ) {
  123. return;
  124. } else {
  125. return array_map( 'absint', $featured_ids );
  126. }
  127. }
  128. /**
  129. * Custom render function for Infinite Scroll.
  130. *
  131. * Stores newly loaded post IDs into a JS variable to pass to
  132. * Isotope when new posts are loaded and need to be appended
  133. * and repositioned
  134. *
  135. * Global JS variable: loadedPosts
  136. */
  137. function altofocus_infinite_scroll_render() {
  138. $loaded_post_IDs = array();
  139. while ( have_posts() ) {
  140. the_post();
  141. get_template_part( 'components/post/content', get_post_format() );
  142. // Put new post IDs into an array for use in isotope 'load more' calls
  143. if ( ! in_array( get_the_ID(), $loaded_post_IDs ) ) {
  144. $loaded_post_IDs[] = get_the_ID();
  145. }
  146. } ?>
  147. <input type="hidden" data="<?php echo esc_attr( json_encode( $loaded_post_IDs ) ); ?>" id="infinite-ids" />
  148. <?php
  149. }
  150. /**
  151. * Jetpack Social Menu Fallback
  152. */
  153. function altofocus_social_menu() {
  154. if ( ! function_exists( 'jetpack_social_menu' ) ) {
  155. return;
  156. } else {
  157. jetpack_social_menu();
  158. }
  159. }
  160. /**
  161. * Custom function to check for a post thumbnail;
  162. * If Jetpack is not available, fall back to has_post_thumbnail()
  163. */
  164. function altofocus_has_post_thumbnail( $post = null ) {
  165. if ( ! function_exists( 'jetpack_has_featured_image' ) ) {
  166. return has_post_thumbnail( $post );
  167. } else {
  168. return jetpack_has_featured_image( $post );
  169. }
  170. }