| 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, Jetpack::module_configuration_load |
| 61 |
* @action jetpack_modules_loaded |
| 62 |
* @return null |
| 63 |
*/ |
| 64 |
public function action_jetpack_modules_loaded() { |
| 65 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 66 |
Jetpack::module_configuration_load( __FILE__, array( $this, 'module_configuration_load' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Redirect configure button to Settings > Reading |
| 71 |
* |
| 72 |
* @uses wp_safe_redirect, admin_url |
| 73 |
* @return null |
| 74 |
*/ |
| 75 |
public function module_configuration_load() { |
| 76 |
wp_safe_redirect( admin_url( 'options-reading.php#infinite-scroll-options' ) ); |
| 77 |
exit; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Register Google Analytics setting |
| 82 |
* |
| 83 |
* @uses add_settings_field, __, register_setting |
| 84 |
* @action admin_init |
| 85 |
* @return null |
| 86 |
*/ |
| 87 |
public function action_admin_init() { |
| 88 |
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' ); |
| 89 |
register_setting( 'reading', $this->option_name_google_analytics, array( $this, 'sanitize_boolean_value' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Render Google Analytics option |
| 94 |
* |
| 95 |
* @uses checked, get_option, __ |
| 96 |
* @return html |
| 97 |
*/ |
| 98 |
public function setting_google_analytics() { |
| 99 |
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 Infinite Scroll post load as a page view in Google Analytics', 'jetpack' ) . '</label>'; |
| 100 |
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>'; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Sanitize value as a boolean |
| 105 |
* |
| 106 |
* @param mixed $value |
| 107 |
* @return bool |
| 108 |
*/ |
| 109 |
public function sanitize_boolean_value( $value ) { |
| 110 |
return (bool) $value; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Load theme's infinite scroll annotation file, if present in the IS plugin. |
| 115 |
* The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS. |
| 116 |
* |
| 117 |
* 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. |
| 118 |
* |
| 119 |
* @uses is_admin, wp_get_theme, get_theme, get_current_theme, apply_filters |
| 120 |
* @action setup_theme |
| 121 |
* @return null |
| 122 |
*/ |
| 123 |
function action_after_setup_theme() { |
| 124 |
$theme = function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_theme( get_current_theme() ); |
| 125 |
|
| 126 |
if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) ) |
| 127 |
return; |
| 128 |
|
| 129 |
/** This filter is already documented in modules/infinite-scroll/infinity.php */ |
| 130 |
$customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] ); |
| 131 |
|
| 132 |
if ( is_readable( $customization_file ) ) { |
| 133 |
require_once( $customization_file ); |
| 134 |
} |
| 135 |
elseif ( ! empty( $theme['Template'] ) ) { |
| 136 |
$customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php"; |
| 137 |
|
| 138 |
if ( is_readable( $customization_file ) ) |
| 139 |
require_once( $customization_file ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Modify Infinite Scroll configuration information |
| 145 |
* |
| 146 |
* @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack_Options::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION |
| 147 |
* @filter infinite_scroll_js_settings |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
public function filter_infinite_scroll_js_settings( $settings ) { |
| 151 |
// Provide WP Stats info for tracking Infinite Scroll loads |
| 152 |
// Abort if Stats module isn't active |
| 153 |
if ( in_array( 'stats', Jetpack::get_active_modules() ) ) { |
| 154 |
// Abort if user is logged in but logged-in users shouldn't be tracked. |
| 155 |
if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) { |
| 156 |
$stats_options = stats_get_options(); |
| 157 |
$track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false; |
| 158 |
|
| 159 |
if ( ! $track_loggedin_users ) |
| 160 |
return $settings; |
| 161 |
} |
| 162 |
|
| 163 |
// We made it this far, so gather the data needed to track IS views |
| 164 |
$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; |
| 165 |
|
| 166 |
// Pagetype parameter |
| 167 |
$settings['stats'] .= '&x_pagetype=infinite'; |
| 168 |
if ( 'click' == $settings['type'] ) |
| 169 |
$settings['stats'] .= '-click'; |
| 170 |
|
| 171 |
$settings['stats'] .= '-jetpack'; |
| 172 |
} |
| 173 |
|
| 174 |
// Check if Google Analytics tracking is requested |
| 175 |
$settings['google_analytics'] = (bool) Jetpack_Options::get_option_and_ensure_autoload( $this->option_name_google_analytics, 0 ); |
| 176 |
|
| 177 |
return $settings; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* 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. |
| 182 |
* |
| 183 |
* @global $videopress |
| 184 |
* @uses do_action() |
| 185 |
* @uses apply_filters() |
| 186 |
* @uses wp_enqueue_style() |
| 187 |
* @uses wp_enqueue_script() |
| 188 |
* @action wp_enqueue_scripts |
| 189 |
* @return null |
| 190 |
*/ |
| 191 |
public function action_wp_enqueue_scripts() { |
| 192 |
// Do not load scripts and styles on singular pages and static pages |
| 193 |
$load_scripts_and_styles = ! ( is_singular() || is_page() ); |
| 194 |
if ( |
| 195 |
/** |
| 196 |
* Allow plugins to enqueue all Infinite Scroll scripts and styles on singular pages as well. |
| 197 |
* |
| 198 |
* @module infinite-scroll |
| 199 |
* |
| 200 |
* @since 3.1.0 |
| 201 |
* |
| 202 |
* @param bool $load_scripts_and_styles Should scripts and styles be loaded on singular pahes and static pages. Default to false. |
| 203 |
*/ |
| 204 |
! apply_filters( 'jetpack_infinite_scroll_load_scripts_and_styles', $load_scripts_and_styles ) |
| 205 |
) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
// VideoPress stand-alone plugin |
| 210 |
global $videopress; |
| 211 |
if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) { |
| 212 |
$videopress->enqueue_scripts(); |
| 213 |
} |
| 214 |
|
| 215 |
// VideoPress Jetpack module |
| 216 |
if ( Jetpack::is_module_active( 'videopress' ) ) { |
| 217 |
wp_enqueue_script( 'videopress' ); |
| 218 |
} |
| 219 |
|
| 220 |
// Fire the post_gallery action early so Carousel scripts are present. |
| 221 |
if ( Jetpack::is_module_active( 'carousel' ) ) { |
| 222 |
/** This filter is already documented in core/wp-includes/media.php */ |
| 223 |
do_action( 'post_gallery', '', '' ); |
| 224 |
} |
| 225 |
|
| 226 |
// Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled |
| 227 |
if ( Jetpack::is_module_active( 'tiled-gallery' ) ) { |
| 228 |
Jetpack_Tiled_Gallery::default_scripts_and_styles(); |
| 229 |
} |
| 230 |
|
| 231 |
// Core's Audio and Video Shortcodes |
| 232 |
if ( |
| 233 |
/** This filter is already documented in core/wp-includes/media.php */ |
| 234 |
'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ) |
| 235 |
) { |
| 236 |
wp_enqueue_style( 'wp-mediaelement' ); |
| 237 |
wp_enqueue_script( 'wp-mediaelement' ); |
| 238 |
} |
| 239 |
|
| 240 |
if ( |
| 241 |
/** This filter is already documented in core/wp-includes/media.php */ |
| 242 |
'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) |
| 243 |
) { |
| 244 |
wp_enqueue_style( 'wp-mediaelement' ); |
| 245 |
wp_enqueue_script( 'wp-mediaelement' ); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
Jetpack_Infinite_Scroll_Extras::instance(); |
| 250 |
|
| 251 |
/** |
| 252 |
* Load main IS file |
| 253 |
*/ |
| 254 |
require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" ); |
| 255 |
|
| 256 |
/** |
| 257 |
* 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(); |
| 258 |
*/ |
| 259 |
remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 ); |
| 260 |
|