Jetpack_Infinite_Scroll_Extras

Jetpack-specific elements of Infinite Scroll.

Defined (1)

The class is defined in the following location(s).

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