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