jetpack.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Jetpack Compatibility File.
  4. *
  5. * @link https://jetpack.com/
  6. *
  7. * @package Rebalance
  8. */
  9. /**
  10. * Jetpack Setup
  11. */
  12. function rebalance_jetpack_setup() {
  13. // Add support for Jetpack Portfolio Custom Post Type.
  14. add_theme_support( 'jetpack-portfolio', array(
  15. 'title' => true,
  16. 'content' => true,
  17. 'featured-image' => true,
  18. ) );
  19. // Custom logo fallback
  20. // - Use Jetpack logo if theme does not support custom logo
  21. if ( ! current_theme_supports( 'custom-logo' ) ) {
  22. // Add support for Jetpack site logo
  23. add_image_size( 'rebalance_site_logo', 0, 80 );
  24. $args = array(
  25. 'header-text' => array(
  26. 'site-title',
  27. 'site-description',
  28. ),
  29. 'size' => 'rebalance_site_logo',
  30. );
  31. add_theme_support( 'site-logo', $args );
  32. }
  33. // Featured content
  34. add_theme_support( 'featured-content', array(
  35. 'filter' => 'rebalance_get_featured_projects',
  36. 'max_posts' => 1,
  37. 'post_types' => 'jetpack-portfolio',
  38. ) );
  39. // Add theme support for Infinite Scroll.
  40. // See: https://jetpack.com/support/infinite-scroll/
  41. add_theme_support( 'infinite-scroll', array(
  42. 'footer_widgets' => array( 'sidebar-1' ),
  43. 'wrapper' => false,
  44. 'container' => 'infinite-wrap',
  45. 'render' => 'rebalance_infinite_scroll_render',
  46. 'footer' => 'page',
  47. ) );
  48. // Add theme support for Responsive Videos.
  49. // See: https://jetpack.com/support/responsive-videos/
  50. add_theme_support( 'jetpack-responsive-videos' );
  51. // Add theme support for Content Options.
  52. // See: https://jetpack.com/support/content-options/
  53. add_theme_support( 'jetpack-content-options', array(
  54. 'blog-display' => 'excerpt',
  55. 'masonry' => '#infinite-wrap',
  56. 'author-bio' => true,
  57. 'post-details' => array(
  58. 'stylesheet' => 'rebalance-style',
  59. 'date' => '.entry-tags-date',
  60. 'categories' => '.entry-categories',
  61. 'tags' => '.entry-tags',
  62. 'author' => '.entry-meta .author',
  63. ),
  64. 'featured-images' => array(
  65. 'archive' => true,
  66. 'post' => true,
  67. 'portfolio' => true,
  68. 'fallback' => true,
  69. ),
  70. ) );
  71. } // end function rebalance_jetpack_setup
  72. add_action( 'after_setup_theme', 'rebalance_jetpack_setup' );
  73. /**
  74. * Custom render function for Infinite Scroll.
  75. */
  76. function rebalance_infinite_scroll_render() {
  77. while ( have_posts() ) {
  78. the_post();
  79. get_template_part( 'template-parts/content', 'card' );
  80. }
  81. }
  82. /**
  83. * Enable Infinite Scroll for Projects (jetpack-portfolio)
  84. *
  85. * @param array $args
  86. * @filter infinite_scroll_query_args
  87. * @return array
  88. */
  89. function rebalance_jetpack_infinite_scroll_query_args( $args ) {
  90. if ( is_page_template( 'template-projects.php' ) ) {
  91. $args['post_type'] = array( 'post', 'page', 'jetpack-portfolio' );
  92. $args['ignore_sticky_posts'] = false;
  93. $args['paged'] = get_query_var('paged') ? get_query_var('paged') : 1;
  94. }
  95. return $args;
  96. }
  97. add_filter( 'infinite_scroll_query_args', 'rebalance_jetpack_infinite_scroll_query_args' );
  98. /**
  99. * Getter function for Featured Content
  100. *
  101. * @return (string) The value of the filter defined in add_theme_support( 'featured-content' )
  102. */
  103. function rebalance_get_featured_projects() {
  104. return apply_filters( 'rebalance_get_featured_projects', array() );
  105. }
  106. /**
  107. * Helper function to check for Featured Content
  108. *
  109. * @param (integer)
  110. * @return (boolean) true/false
  111. */
  112. function rebalance_has_featured_projects( $minimum = 1 ) {
  113. if ( is_paged() ) {
  114. return false;
  115. }
  116. $minimum = absint( $minimum );
  117. $featured_posts = apply_filters( 'rebalance_get_featured_projects', array() );
  118. if ( ! is_array( $featured_posts ) ) {
  119. return false;
  120. }
  121. if ( $minimum > count( $featured_posts ) ) {
  122. return false;
  123. }
  124. return true;
  125. }
  126. function rebalance_get_featured_project_ids() {
  127. // Get array of cached results if they exist.
  128. $featured_ids = get_transient( 'featured_content_ids' );
  129. if ( false === $featured_ids ) {
  130. $settings = get_theme_mods();
  131. $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
  132. if ( $term ) {
  133. // Query for featured posts.
  134. $featured_ids = get_posts( array(
  135. 'fields' => 'ids',
  136. 'numberposts' => $max_posts,
  137. 'post_type' => 'jetpack-portfolio',
  138. 'suppress_filters' => false,
  139. 'tax_query' => array(
  140. array(
  141. 'field' => 'term_id',
  142. 'taxonomy' => 'post_tag',
  143. 'terms' => $term->term_id,
  144. ),
  145. ),
  146. )
  147. );
  148. }
  149. set_transient( 'featured_content_ids', $featured_ids );
  150. }
  151. // Ensure correct format before return.
  152. return array_map( 'absint', $featured_ids );
  153. }
  154. /**
  155. * Portfolio Title
  156. */
  157. function rebalance_portfolio_title( $before = '', $after = '' ) {
  158. $jetpack_portfolio_title = get_option( 'jetpack_portfolio_title' );
  159. $title = '';
  160. if ( is_post_type_archive( 'jetpack-portfolio' ) ) {
  161. if ( isset( $jetpack_portfolio_title ) && '' != $jetpack_portfolio_title ) {
  162. $title = esc_html( $jetpack_portfolio_title );
  163. } else {
  164. $title = post_type_archive_title( '', false );
  165. }
  166. } elseif ( is_tax( 'jetpack-portfolio-type' ) || is_tax( 'jetpack-portfolio-tag' ) ) {
  167. $title = single_term_title( '', false );
  168. }
  169. echo $before . $title . $after;
  170. }
  171. /**
  172. * Portfolio Content
  173. */
  174. function rebalance_portfolio_content( $before = '', $after = '' ) {
  175. $jetpack_portfolio_content = get_option( 'jetpack_portfolio_content' );
  176. if ( is_tax() && get_the_archive_description() ) {
  177. echo $before . get_the_archive_description() . $after;
  178. } else if ( isset( $jetpack_portfolio_content ) && '' != $jetpack_portfolio_content ) {
  179. $content = convert_chars( convert_smilies( wptexturize( wp_kses_post( $jetpack_portfolio_content ) ) ) );
  180. echo $before . $content . $after;
  181. }
  182. }
  183. /**
  184. * Portfolio Featured Image
  185. */
  186. function rebalance_portfolio_thumbnail( $before = '', $after = '' ) {
  187. $jetpack_portfolio_featured_image = get_option( 'jetpack_portfolio_featured_image' );
  188. if ( isset( $jetpack_portfolio_featured_image ) && '' != $jetpack_portfolio_featured_image ) {
  189. $featured_image = wp_get_attachment_image( (int) $jetpack_portfolio_featured_image, 'full-width' );
  190. echo $before . $featured_image . $after;
  191. }
  192. }
  193. /**
  194. * Filter Infinite Scroll text handle
  195. */
  196. function rebalance_portfolio_infinite_scroll_navigation( $js_settings ) {
  197. if ( is_post_type_archive( 'jetpack-portfolio' ) || is_tax( 'jetpack-portfolio-type' ) || is_tax( 'jetpack-portfolio-tag' ) ) {
  198. $js_settings[ 'text' ] = esc_js( esc_html__( 'Older projects', 'rebalance' ) );
  199. }
  200. return $js_settings;
  201. }
  202. add_filter( 'infinite_scroll_js_settings', 'rebalance_portfolio_infinite_scroll_navigation' );
  203. /**
  204. * Return Author Bio
  205. * If Jetpack is not available, fall back to rebalance_author_meta()
  206. */
  207. function rebalance_author_bio() {
  208. if ( ! function_exists( 'jetpack_author_bio' ) ) {
  209. rebalance_author_meta();
  210. } else {
  211. jetpack_author_bio();
  212. }
  213. }
  214. /**
  215. * Author Author Bio Avatar Size
  216. */
  217. function rebalance_author_bio_avatar_size() {
  218. return 111;
  219. }
  220. add_filter( 'jetpack_author_bio_avatar_size', 'rebalance_author_bio_avatar_size' );
  221. /**
  222. * Custom function to check for a post thumbnail
  223. * If Jetpack is not available, fall back to has_post_thumbnail()
  224. */
  225. function rebalance_has_post_thumbnail( $post = null ) {
  226. if ( function_exists( 'jetpack_has_featured_image' ) ) {
  227. return jetpack_has_featured_image( $post );
  228. } else {
  229. return has_post_thumbnail( $post );
  230. }
  231. }
  232. /**
  233. * Disable Lazy Loading on non-singular views
  234. *
  235. * @filter lazyload_is_enabled
  236. * @return bool
  237. */
  238. add_action( 'wp', 'rebalance_remove_lazy_load_hooks' );
  239. function rebalance_remove_lazy_load_hooks() {
  240. if ( is_singular() || ! class_exists( 'Jetpack_Lazy_Images' ) ) {
  241. return;
  242. }
  243. $instance = Jetpack_Lazy_Images::instance();
  244. add_action( 'wp_head', array( $instance, 'remove_filters' ), 10000 );
  245. remove_action( 'wp_enqueue_scripts', array( $instance, 'enqueue_assets' ) );
  246. }
  247. /**
  248. * Disable Lazy Loading on paged archives and homepage. Fixes overlapping content areas.
  249. *
  250. * @filter lazyload_is_enabled
  251. * @return bool
  252. */
  253. add_filter( 'lazyload_is_enabled', 'rebalance_lazyload_exclude', 15 );
  254. function rebalance_lazyload_exclude() {
  255. if ( is_paged() || is_home() ) {
  256. return false;
  257. } else {
  258. return true;
  259. }
  260. }