infinite-scroll.php
| 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 | /** |
| 15 | * Jetpack-specific elements of Infinite Scroll |
| 16 | */ |
| 17 | class Jetpack_Infinite_Scroll_Extras { |
| 18 | /** |
| 19 | * Class variables |
| 20 | */ |
| 21 | // Oh look, a singleton |
| 22 | private static $__instance = null; |
| 23 | |
| 24 | // Option names |
| 25 | private $option_name_google_analytics = 'infinite_scroll_google_analytics'; |
| 26 | |
| 27 | /** |
| 28 | * Singleton implementation |
| 29 | * |
| 30 | * @return object |
| 31 | */ |
| 32 | public static function instance() { |
| 33 | if ( ! is_a( self::$__instance, 'Jetpack_Infinite_Scroll_Extras' ) ) |
| 34 | self::$__instance = new Jetpack_Infinite_Scroll_Extras; |
| 35 | |
| 36 | return self::$__instance; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Register actions and filters |
| 41 | * |
| 42 | * @uses add_action, add_filter |
| 43 | * @return null |
| 44 | */ |
| 45 | private function __construct() { |
| 46 | add_action( 'jetpack_modules_loaded', array( $this, 'action_jetpack_modules_loaded' ) ); |
| 47 | |
| 48 | add_action( 'admin_init', array( $this, 'action_admin_init' ), 11 ); |
| 49 | |
| 50 | add_action( 'after_setup_theme', array( $this, 'action_after_setup_theme' ), 5 ); |
| 51 | |
| 52 | add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ) ); |
| 53 | |
| 54 | add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Enable "Configure" button on module card |
| 59 | * |
| 60 | * @uses Jetpack::enable_module_configurable |
| 61 | * @action jetpack_modules_loaded |
| 62 | * @return null |
| 63 | */ |
| 64 | public function action_jetpack_modules_loaded() { |
| 65 | Jetpack::enable_module_configurable( __FILE__ ); |
| 66 | } |
| 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 | /** |
| 81 | * Render Google Analytics option |
| 82 | * |
| 83 | * @uses checked, get_option, __ |
| 84 | * @return html |
| 85 | */ |
| 86 | public function setting_google_analytics() { |
| 87 | 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>'; |
| 88 | 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>'; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sanitize value as a boolean |
| 93 | * |
| 94 | * @param mixed $value |
| 95 | * @return bool |
| 96 | */ |
| 97 | public function sanitize_boolean_value( $value ) { |
| 98 | return (bool) $value; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Load theme's infinite scroll annotation file, if present in the IS plugin. |
| 103 | * The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS. |
| 104 | * |
| 105 | * 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. |
| 106 | * |
| 107 | * @uses is_admin, wp_get_theme, apply_filters |
| 108 | * @action setup_theme |
| 109 | * @return null |
| 110 | */ |
| 111 | function action_after_setup_theme() { |
| 112 | $theme = wp_get_theme(); |
| 113 | |
| 114 | if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) ) |
| 115 | return; |
| 116 | |
| 117 | /** This filter is already documented in modules/infinite-scroll/infinity.php */ |
| 118 | $customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] ); |
| 119 | |
| 120 | if ( is_readable( $customization_file ) ) { |
| 121 | require_once( $customization_file ); |
| 122 | } |
| 123 | elseif ( ! empty( $theme['Template'] ) ) { |
| 124 | $customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php"; |
| 125 | |
| 126 | if ( is_readable( $customization_file ) ) |
| 127 | require_once( $customization_file ); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Modify Infinite Scroll configuration information |
| 133 | * |
| 134 | * @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack_Options::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION |
| 135 | * @filter infinite_scroll_js_settings |
| 136 | * @return array |
| 137 | */ |
| 138 | public function filter_infinite_scroll_js_settings( $settings ) { |
| 139 | // Provide WP Stats info for tracking Infinite Scroll loads |
| 140 | // Abort if Stats module isn't active |
| 141 | if ( in_array( 'stats', Jetpack::get_active_modules() ) ) { |
| 142 | // Abort if user is logged in but logged-in users shouldn't be tracked. |
| 143 | if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) { |
| 144 | $stats_options = stats_get_options(); |
| 145 | $track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false; |
| 146 | |
| 147 | if ( ! $track_loggedin_users ) |
| 148 | return $settings; |
| 149 | } |
| 150 | |
| 151 | // We made it this far, so gather the data needed to track IS views |
| 152 | $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; |
| 153 | |
| 154 | // Pagetype parameter |
| 155 | $settings['stats'] .= '&x_pagetype=infinite'; |
| 156 | if ( 'click' == $settings['type'] ) |
| 157 | $settings['stats'] .= '-click'; |
| 158 | |
| 159 | $settings['stats'] .= '-jetpack'; |
| 160 | } |
| 161 | |
| 162 | // Check if Google Analytics tracking is requested |
| 163 | $settings['google_analytics'] = (bool) Jetpack_Options::get_option_and_ensure_autoload( $this->option_name_google_analytics, 0 ); |
| 164 | |
| 165 | return $settings; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * 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. |
| 170 | * |
| 171 | * @global $videopress |
| 172 | * @uses do_action() |
| 173 | * @uses apply_filters() |
| 174 | * @uses wp_enqueue_style() |
| 175 | * @uses wp_enqueue_script() |
| 176 | * @action wp_enqueue_scripts |
| 177 | * @return null |
| 178 | */ |
| 179 | public function action_wp_enqueue_scripts() { |
| 180 | // Do not load scripts and styles on singular pages and static pages |
| 181 | $load_scripts_and_styles = ! ( is_singular() || is_page() ); |
| 182 | if ( |
| 183 | /** |
| 184 | * Allow plugins to enqueue all Infinite Scroll scripts and styles on singular pages as well. |
| 185 | * |
| 186 | * @module infinite-scroll |
| 187 | * |
| 188 | * @since 3.1.0 |
| 189 | * |
| 190 | * @param bool $load_scripts_and_styles Should scripts and styles be loaded on singular pahes and static pages. Default to false. |
| 191 | */ |
| 192 | ! apply_filters( 'jetpack_infinite_scroll_load_scripts_and_styles', $load_scripts_and_styles ) |
| 193 | ) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | // VideoPress stand-alone plugin |
| 198 | global $videopress; |
| 199 | if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) { |
| 200 | $videopress->enqueue_scripts(); |
| 201 | } |
| 202 | |
| 203 | // VideoPress Jetpack module |
| 204 | if ( Jetpack::is_module_active( 'videopress' ) ) { |
| 205 | wp_enqueue_script( 'videopress' ); |
| 206 | } |
| 207 | |
| 208 | // Fire the post_gallery action early so Carousel scripts are present. |
| 209 | if ( Jetpack::is_module_active( 'carousel' ) ) { |
| 210 | /** This filter is already documented in core/wp-includes/media.php */ |
| 211 | do_action( 'post_gallery', '', '', 0 ); |
| 212 | } |
| 213 | |
| 214 | // Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled |
| 215 | if ( Jetpack::is_module_active( 'tiled-gallery' ) ) { |
| 216 | Jetpack_Tiled_Gallery::default_scripts_and_styles(); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | Jetpack_Infinite_Scroll_Extras::instance(); |
| 221 | |
| 222 | /** |
| 223 | * Load main IS file |
| 224 | */ |
| 225 | require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" ); |
| 226 | |
| 227 | /** |
| 228 | * 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(); |
| 229 | */ |
| 230 | remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 ); |
| 231 |