| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Name: Infinite Scroll |
| 4 |
* Module Description: Automatically pull the next set of posts into view when the reader approaches the bottom of the page. |
| 5 |
* Sort Order: 14 |
| 6 |
* First Introduced: 2.0 |
| 7 |
* Requires Connection: No |
| 8 |
* Auto Activate: No |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Jetpack-specific elements of Infinite Scroll |
| 13 |
*/ |
| 14 |
class Jetpack_Infinite_Scroll_Extras { |
| 15 |
/** |
| 16 |
* Class variables |
| 17 |
*/ |
| 18 |
// Oh look, a singleton |
| 19 |
private static $__instance = null; |
| 20 |
|
| 21 |
// Option names |
| 22 |
private $option_name_google_analytics = 'infinite_scroll_google_analytics'; |
| 23 |
|
| 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 |
|
| 33 |
return self::$__instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register actions and filters |
| 38 |
* |
| 39 |
* @uses add_action, add_filter |
| 40 |
* @return null |
| 41 |
*/ |
| 42 |
private function __construct() { |
| 43 |
add_action( 'jetpack_modules_loaded', array( $this, 'action_jetpack_modules_loaded' ) ); |
| 44 |
|
| 45 |
add_action( 'admin_init', array( $this, 'action_admin_init' ), 11 ); |
| 46 |
|
| 47 |
add_action( 'after_setup_theme', array( $this, 'action_after_setup_theme' ), 5 ); |
| 48 |
|
| 49 |
add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ) ); |
| 50 |
|
| 51 |
add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ) ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Enable "Configure" button on module card |
| 56 |
* |
| 57 |
* @uses Jetpack::enable_module_configurable, Jetpack::module_configuration_load |
| 58 |
* @action jetpack_modules_loaded |
| 59 |
* @return null |
| 60 |
*/ |
| 61 |
public function action_jetpack_modules_loaded() { |
| 62 |
Jetpack::enable_module_configurable( __FILE__ ); |
| 63 |
Jetpack::module_configuration_load( __FILE__, array( $this, 'module_configuration_load' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Redirect configure button to Settings > Reading |
| 68 |
* |
| 69 |
* @uses wp_safe_redirect, admin_url |
| 70 |
* @return null |
| 71 |
*/ |
| 72 |
public function module_configuration_load() { |
| 73 |
wp_safe_redirect( admin_url( 'options-reading.php#infinite-scroll-options' ) ); |
| 74 |
exit; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Register Google Analytics setting |
| 79 |
* |
| 80 |
* @uses add_settings_field, __, register_setting |
| 81 |
* @action admin_init |
| 82 |
* @return null |
| 83 |
*/ |
| 84 |
public function action_admin_init() { |
| 85 |
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' ); |
| 86 |
register_setting( 'reading', $this->option_name_google_analytics, array( $this, 'sanitize_boolean_value' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Render Google Analytics option |
| 91 |
* |
| 92 |
* @uses checked, get_option, __ |
| 93 |
* @return html |
| 94 |
*/ |
| 95 |
public function setting_google_analytics() { |
| 96 |
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>'; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sanitize value as a boolean |
| 101 |
* |
| 102 |
* @param mixed $value |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
public function sanitize_boolean_value( $value ) { |
| 106 |
return (bool) $value; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Load theme's infinite scroll annotation file, if present in the IS plugin. |
| 111 |
* The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS. |
| 112 |
* |
| 113 |
* 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. |
| 114 |
* |
| 115 |
* @uses is_admin, wp_get_theme, get_theme, get_current_theme, apply_filters |
| 116 |
* @action setup_theme |
| 117 |
* @return null |
| 118 |
*/ |
| 119 |
function action_after_setup_theme() { |
| 120 |
$theme = function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_theme( get_current_theme() ); |
| 121 |
|
| 122 |
if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) ) |
| 123 |
return; |
| 124 |
|
| 125 |
$customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] ); |
| 126 |
|
| 127 |
if ( is_readable( $customization_file ) ) { |
| 128 |
require_once( $customization_file ); |
| 129 |
} |
| 130 |
elseif ( ! empty( $theme['Template'] ) ) { |
| 131 |
$customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php"; |
| 132 |
|
| 133 |
if ( is_readable( $customization_file ) ) |
| 134 |
require_once( $customization_file ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Modify Infinite Scroll configuration information |
| 140 |
* |
| 141 |
* @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION |
| 142 |
* @filter infinite_scroll_js_settings |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
public function filter_infinite_scroll_js_settings( $settings ) { |
| 146 |
// Provide WP Stats info for tracking Infinite Scroll loads |
| 147 |
// Abort if Stats module isn't active |
| 148 |
if ( in_array( 'stats', Jetpack::get_active_modules() ) ) { |
| 149 |
// Abort if user is logged in but logged-in users shouldn't be tracked. |
| 150 |
if ( is_user_logged_in() ) { |
| 151 |
$stats_options = stats_get_options(); |
| 152 |
$track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false; |
| 153 |
|
| 154 |
if ( ! $track_loggedin_users ) |
| 155 |
return $settings; |
| 156 |
} |
| 157 |
|
| 158 |
// We made it this far, so gather the data needed to track IS views |
| 159 |
$settings['stats'] = 'blog=' . Jetpack::get_option( 'id' ) . '&host=' . parse_url( get_option( 'home' ), PHP_URL_HOST ) . '&v=ext&j=' . JETPACK__API_VERSION . ':' . JETPACK__VERSION; |
| 160 |
|
| 161 |
// Pagetype parameter |
| 162 |
$settings['stats'] .= '&x_pagetype=infinite'; |
| 163 |
if ( 'click' == $settings['type'] ) |
| 164 |
$settings['stats'] .= '-click'; |
| 165 |
|
| 166 |
$settings['stats'] .= '-jetpack'; |
| 167 |
} |
| 168 |
|
| 169 |
// Check if Google Analytics tracking is requested |
| 170 |
$settings['google_analytics'] = (bool) get_option( $this->option_name_google_analytics ); |
| 171 |
|
| 172 |
return $settings; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Load VideoPress scripts if plugin is active. |
| 177 |
* |
| 178 |
* @global $videopress |
| 179 |
* @action wp_enqueue_scripts |
| 180 |
* @return null |
| 181 |
*/ |
| 182 |
public function action_wp_enqueue_scripts() { |
| 183 |
global $videopress; |
| 184 |
if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) |
| 185 |
$videopress->enqueue_scripts(); |
| 186 |
} |
| 187 |
} |
| 188 |
Jetpack_Infinite_Scroll_Extras::instance(); |
| 189 |
|
| 190 |
/** |
| 191 |
* Load main IS file |
| 192 |
*/ |
| 193 |
require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" ); |
| 194 |
|
| 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 ); |