Geen omschrijving

infinite-scroll.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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
  51. * @action jetpack_modules_loaded
  52. * @return null
  53. */
  54. public function action_jetpack_modules_loaded() {
  55. Jetpack::enable_module_configurable( __FILE__ );
  56. }
  57. /**
  58. * Register Google Analytics setting
  59. *
  60. * @uses add_settings_field, __, register_setting
  61. * @action admin_init
  62. * @return null
  63. */
  64. public function action_admin_init() {
  65. 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' );
  66. register_setting( 'reading', $this->option_name_google_analytics, array( $this, 'sanitize_boolean_value' ) );
  67. }
  68. /**
  69. * Render Google Analytics option
  70. *
  71. * @uses checked, get_option, __
  72. * @return html
  73. */
  74. public function setting_google_analytics() {
  75. 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>';
  76. 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>';
  77. }
  78. /**
  79. * Sanitize value as a boolean
  80. *
  81. * @param mixed $value
  82. * @return bool
  83. */
  84. public function sanitize_boolean_value( $value ) {
  85. return (bool) $value;
  86. }
  87. /**
  88. * Load theme's infinite scroll annotation file, if present in the IS plugin.
  89. * The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS.
  90. *
  91. * 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.
  92. *
  93. * @uses is_admin, wp_get_theme, apply_filters
  94. * @action setup_theme
  95. * @return null
  96. */
  97. function action_after_setup_theme() {
  98. $theme = wp_get_theme();
  99. if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) )
  100. return;
  101. /** This filter is already documented in modules/infinite-scroll/infinity.php */
  102. $customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] );
  103. if ( is_readable( $customization_file ) ) {
  104. require_once( $customization_file );
  105. }
  106. elseif ( ! empty( $theme['Template'] ) ) {
  107. $customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php";
  108. if ( is_readable( $customization_file ) )
  109. require_once( $customization_file );
  110. }
  111. }
  112. /**
  113. * Modify Infinite Scroll configuration information
  114. *
  115. * @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack_Options::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION
  116. * @filter infinite_scroll_js_settings
  117. * @return array
  118. */
  119. public function filter_infinite_scroll_js_settings( $settings ) {
  120. // Provide WP Stats info for tracking Infinite Scroll loads
  121. // Abort if Stats module isn't active
  122. if ( in_array( 'stats', Jetpack::get_active_modules() ) ) {
  123. // Abort if user is logged in but logged-in users shouldn't be tracked.
  124. if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
  125. $stats_options = stats_get_options();
  126. $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;
  127. if ( ! $track_loggedin_users )
  128. return $settings;
  129. }
  130. // We made it this far, so gather the data needed to track IS views
  131. $settings['stats'] = 'blog=' . Jetpack_Options::get_option( 'id' ) . '&host=' . wp_parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION;
  132. // Pagetype parameter
  133. $settings['stats'] .= '&x_pagetype=infinite';
  134. if ( 'click' == $settings['type'] )
  135. $settings['stats'] .= '-click';
  136. $settings['stats'] .= '-jetpack';
  137. }
  138. // Check if Google Analytics tracking is requested
  139. $settings['google_analytics'] = (bool) Jetpack_Options::get_option_and_ensure_autoload( $this->option_name_google_analytics, 0 );
  140. return $settings;
  141. }
  142. /**
  143. * 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.
  144. *
  145. * @global $videopress
  146. * @uses do_action()
  147. * @uses apply_filters()
  148. * @uses wp_enqueue_style()
  149. * @uses wp_enqueue_script()
  150. * @action wp_enqueue_scripts
  151. * @return null
  152. */
  153. public function action_wp_enqueue_scripts() {
  154. // Do not load scripts and styles on singular pages and static pages
  155. $load_scripts_and_styles = ! ( is_singular() || is_page() );
  156. if (
  157. /**
  158. * Allow plugins to enqueue all Infinite Scroll scripts and styles on singular pages as well.
  159. *
  160. * @module infinite-scroll
  161. *
  162. * @since 3.1.0
  163. *
  164. * @param bool $load_scripts_and_styles Should scripts and styles be loaded on singular pahes and static pages. Default to false.
  165. */
  166. ! apply_filters( 'jetpack_infinite_scroll_load_scripts_and_styles', $load_scripts_and_styles )
  167. ) {
  168. return;
  169. }
  170. // VideoPress stand-alone plugin
  171. global $videopress;
  172. if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) {
  173. $videopress->enqueue_scripts();
  174. }
  175. // VideoPress Jetpack module
  176. if ( Jetpack::is_module_active( 'videopress' ) ) {
  177. wp_enqueue_script( 'videopress' );
  178. }
  179. // Fire the post_gallery action early so Carousel scripts are present.
  180. if ( Jetpack::is_module_active( 'carousel' ) ) {
  181. /** This filter is already documented in core/wp-includes/media.php */
  182. do_action( 'post_gallery', '', '', 0 );
  183. }
  184. // Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled
  185. if ( Jetpack::is_module_active( 'tiled-gallery' ) ) {
  186. Jetpack_Tiled_Gallery::default_scripts_and_styles();
  187. }
  188. }
  189. }
  190. Jetpack_Infinite_Scroll_Extras::instance();
  191. /**
  192. * Load main IS file
  193. */
  194. require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" );
  195. /**
  196. * 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();
  197. */
  198. remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 );