infinite-scroll.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Module Name: Infinite Scroll
  4. * Module Description: Automatically load new content when a visitor scrolls
  5. * Sort Order: 26
  6. * First Introduced: 2.0
  7. * Requires Connection: No
  8. * Auto Activate: No
  9. * Module Tags: Appearance
  10. * Feature: Appearance
  11. * Additional Search Queries: scroll, infinite, infinite scroll
  12. */
  13. /**
  14. * Jetpack-specific elements of Infinite Scroll
  15. */
  16. class Jetpack_Infinite_Scroll_Extras {
  17. /**
  18. * Class variables
  19. */
  20. // Oh look, a singleton
  21. private static $__instance = null;
  22. // Option names
  23. private $option_name_google_analytics = 'infinite_scroll_google_analytics';
  24. /**
  25. * Singleton implementation
  26. *
  27. * @return object
  28. */
  29. public static function instance() {
  30. if ( ! is_a( self::$__instance, 'Jetpack_Infinite_Scroll_Extras' ) )
  31. self::$__instance = new Jetpack_Infinite_Scroll_Extras;
  32. return self::$__instance;
  33. }
  34. /**
  35. * Register actions and filters
  36. *
  37. * @uses add_action, add_filter
  38. * @return null
  39. */
  40. private function __construct() {
  41. add_action( 'jetpack_modules_loaded', array( $this, 'action_jetpack_modules_loaded' ) );
  42. add_action( 'admin_init', array( $this, 'action_admin_init' ), 11 );
  43. add_action( 'after_setup_theme', array( $this, 'action_after_setup_theme' ), 5 );
  44. add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ) );
  45. add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ) );
  46. }
  47. /**
  48. * Enable "Configure" button on module card
  49. *
  50. * @uses Jetpack::enable_module_configurable, Jetpack::module_configuration_load
  51. * @action jetpack_modules_loaded
  52. * @return null
  53. */
  54. public function action_jetpack_modules_loaded() {
  55. Jetpack::enable_module_configurable( __FILE__ );
  56. Jetpack::module_configuration_load( __FILE__, array( $this, 'module_configuration_load' ) );
  57. }
  58. /**
  59. * Redirect configure button to Settings > Reading
  60. *
  61. * @uses wp_safe_redirect, admin_url
  62. * @return null
  63. */
  64. public function module_configuration_load() {
  65. wp_safe_redirect( admin_url( 'options-reading.php#infinite-scroll-options' ) );
  66. exit;
  67. }
  68. /**
  69. * Register Google Analytics setting
  70. *
  71. * @uses add_settings_field, __, register_setting
  72. * @action admin_init
  73. * @return null
  74. */
  75. public function action_admin_init() {
  76. add_settings_field( $this->option_name_google_analytics, '<span id="infinite-scroll-google-analytics">' . __( 'Use Google Analytics with Infinite Scroll', 'jetpack' ) . '</span>', array( $this, 'setting_google_analytics' ), 'reading' );
  77. register_setting( 'reading', $this->option_name_google_analytics, array( $this, 'sanitize_boolean_value' ) );
  78. }
  79. /**
  80. * Render Google Analytics option
  81. *
  82. * @uses checked, get_option, __
  83. * @return html
  84. */
  85. public function setting_google_analytics() {
  86. echo '<label><input name="infinite_scroll_google_analytics" type="checkbox" value="1" ' . checked( true, (bool) get_option( $this->option_name_google_analytics, false ), false ) . ' /> ' . esc_html__( 'Track each scroll load (7 posts by default) as a page view in Google Analytics', 'jetpack' ) . '</label>';
  87. echo '<p class="description">' . esc_html__( 'Check the box above to record each new set of posts loaded via Infinite Scroll as a page view in Google Analytics.', 'jetpack' ) . '</p>';
  88. }
  89. /**
  90. * Sanitize value as a boolean
  91. *
  92. * @param mixed $value
  93. * @return bool
  94. */
  95. public function sanitize_boolean_value( $value ) {
  96. return (bool) $value;
  97. }
  98. /**
  99. * Load theme's infinite scroll annotation file, if present in the IS plugin.
  100. * The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS.
  101. *
  102. * As released in Jetpack 2.0, a child theme's parent wasn't checked for in the plugin's bundled support, hence the convoluted way the parent is checked for now.
  103. *
  104. * @uses is_admin, wp_get_theme, apply_filters
  105. * @action setup_theme
  106. * @return null
  107. */
  108. function action_after_setup_theme() {
  109. $theme = wp_get_theme();
  110. if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) )
  111. return;
  112. /** This filter is already documented in modules/infinite-scroll/infinity.php */
  113. $customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] );
  114. if ( is_readable( $customization_file ) ) {
  115. require_once( $customization_file );
  116. }
  117. elseif ( ! empty( $theme['Template'] ) ) {
  118. $customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php";
  119. if ( is_readable( $customization_file ) )
  120. require_once( $customization_file );
  121. }
  122. }
  123. /**
  124. * Modify Infinite Scroll configuration information
  125. *
  126. * @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack_Options::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION
  127. * @filter infinite_scroll_js_settings
  128. * @return array
  129. */
  130. public function filter_infinite_scroll_js_settings( $settings ) {
  131. // Provide WP Stats info for tracking Infinite Scroll loads
  132. // Abort if Stats module isn't active
  133. if ( in_array( 'stats', Jetpack::get_active_modules() ) ) {
  134. // Abort if user is logged in but logged-in users shouldn't be tracked.
  135. if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
  136. $stats_options = stats_get_options();
  137. $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
  138. if ( ! $track_loggedin_users )
  139. return $settings;
  140. }
  141. // We made it this far, so gather the data needed to track IS views
  142. $settings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
  143. // Pagetype parameter
  144. $settings['stats'] .= '&x_pagetype=infinite';
  145. if ( 'click' == $settings['type'] )
  146. $settings['stats'] .= '-click';
  147. $settings['stats'] .= '-jetpack';
  148. }
  149. // Check if Google Analytics tracking is requested
  150. $settings['google_analytics'] = (bool) Jetpack_Options::get_option_and_ensure_autoload( $this->option_name_google_analytics, 0 );
  151. return $settings;
  152. }
  153. /**
  154. * Always load certain scripts when IS is enabled, as they can't be loaded after `document.ready` fires, meaning they can't leverage IS's script loader.
  155. *
  156. * @global $videopress
  157. * @uses do_action()
  158. * @uses apply_filters()
  159. * @uses wp_enqueue_style()
  160. * @uses wp_enqueue_script()
  161. * @action wp_enqueue_scripts
  162. * @return null
  163. */
  164. public function action_wp_enqueue_scripts() {
  165. // Do not load scripts and styles on singular pages and static pages
  166. $load_scripts_and_styles = ! ( is_singular() || is_page() );
  167. if (
  168. /**
  169. * Allow plugins to enqueue all Infinite Scroll scripts and styles on singular pages as well.
  170. *
  171. * @module infinite-scroll
  172. *
  173. * @since 3.1.0
  174. *
  175. * @param bool $load_scripts_and_styles Should scripts and styles be loaded on singular pahes and static pages. Default to false.
  176. */
  177. ! apply_filters( 'jetpack_infinite_scroll_load_scripts_and_styles', $load_scripts_and_styles )
  178. ) {
  179. return;
  180. }
  181. // VideoPress stand-alone plugin
  182. global $videopress;
  183. if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) {
  184. $videopress->enqueue_scripts();
  185. }
  186. // VideoPress Jetpack module
  187. if ( Jetpack::is_module_active( 'videopress' ) ) {
  188. wp_enqueue_script( 'videopress' );
  189. }
  190. // Fire the post_gallery action early so Carousel scripts are present.
  191. if ( Jetpack::is_module_active( 'carousel' ) ) {
  192. /** This filter is already documented in core/wp-includes/media.php */
  193. do_action( 'post_gallery', '', '', 0 );
  194. }
  195. // Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled
  196. if ( Jetpack::is_module_active( 'tiled-gallery' ) ) {
  197. Jetpack_Tiled_Gallery::default_scripts_and_styles();
  198. }
  199. // Core's Audio and Video Shortcodes
  200. if (
  201. /** This filter is already documented in core/wp-includes/media.php */
  202. 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' )
  203. ) {
  204. wp_enqueue_style( 'wp-mediaelement' );
  205. wp_enqueue_script( 'wp-mediaelement' );
  206. }
  207. if (
  208. /** This filter is already documented in core/wp-includes/media.php */
  209. 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' )
  210. ) {
  211. wp_enqueue_style( 'wp-mediaelement' );
  212. wp_enqueue_script( 'wp-mediaelement' );
  213. }
  214. }
  215. }
  216. Jetpack_Infinite_Scroll_Extras::instance();
  217. /**
  218. * Load main IS file
  219. */
  220. require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" );
  221. /**
  222. * Remove the IS annotation loading function bundled with the IS plugin in favor of the Jetpack-specific version in Jetpack_Infinite_Scroll_Extras::action_after_setup_theme();
  223. */
  224. remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 );