summaryrefslogtreecommitdiff
blob: b4f092602ce2e7e2ffce0c473fc3fe2a2554d633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
 * Module Name: Infinite Scroll
 * Module Description: Add support for infinite scroll to your theme.
 * Sort Order: 26
 * First Introduced: 2.0
 * Requires Connection: No
 * Auto Activate: No
 * Module Tags: Appearance
 */

/**
 * Jetpack-specific elements of Infinite Scroll
 */
class Jetpack_Infinite_Scroll_Extras {
	/**
	 * Class variables
	 */
	// Oh look, a singleton
	private static $__instance = null;

	// Option names
	private $option_name_google_analytics = 'infinite_scroll_google_analytics';

	/**
	 * Singleton implementation
	 *
	 * @return object
	 */
	public static function instance() {
		if ( ! is_a( self::$__instance, 'Jetpack_Infinite_Scroll_Extras' ) )
			self::$__instance = new Jetpack_Infinite_Scroll_Extras;

		return self::$__instance;
	}

	/**
	 * Register actions and filters
	 *
	 * @uses add_action, add_filter
	 * @return null
	 */
	private function __construct() {
		add_action( 'jetpack_modules_loaded', array( $this, 'action_jetpack_modules_loaded' ) );

		add_action( 'admin_init', array( $this, 'action_admin_init' ), 11 );

		add_action( 'after_setup_theme', array( $this, 'action_after_setup_theme' ), 5 );

		add_filter( 'infinite_scroll_js_settings', array( $this, 'filter_infinite_scroll_js_settings' ) );

		add_action( 'wp_enqueue_scripts', array( $this, 'action_wp_enqueue_scripts' ) );
	}

	/**
	 * Enable "Configure" button on module card
	 *
	 * @uses Jetpack::enable_module_configurable, Jetpack::module_configuration_load
	 * @action jetpack_modules_loaded
	 * @return null
	 */
	public function action_jetpack_modules_loaded() {
		Jetpack::enable_module_configurable( __FILE__ );
		Jetpack::module_configuration_load( __FILE__, array( $this, 'module_configuration_load' ) );
	}

	/**
	 * Redirect configure button to Settings > Reading
	 *
	 * @uses wp_safe_redirect, admin_url
	 * @return null
	 */
	public function module_configuration_load() {
		wp_safe_redirect( admin_url( 'options-reading.php#infinite-scroll-options' ) );
		exit;
	}

	/**
	 * Register Google Analytics setting
	 *
	 * @uses add_settings_field, __, register_setting
	 * @action admin_init
	 * @return null
	 */
	public function action_admin_init() {
		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' );
		register_setting( 'reading', $this->option_name_google_analytics, array( $this, 'sanitize_boolean_value' ) );
	}

	/**
	 * Render Google Analytics option
	 *
	 * @uses checked, get_option, __
	 * @return html
	 */
	public function setting_google_analytics() {
		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>';
	}

	/**
	 * Sanitize value as a boolean
	 *
	 * @param mixed $value
	 * @return bool
	 */
	public function sanitize_boolean_value( $value ) {
		return (bool) $value;
	}

	/**
	 * Load theme's infinite scroll annotation file, if present in the IS plugin.
	 * The `setup_theme` action is used because the annotation files should be using `after_setup_theme` to register support for IS.
	 *
	 * 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.
	 *
	 * @uses is_admin, wp_get_theme, get_theme, get_current_theme, apply_filters
	 * @action setup_theme
	 * @return null
	 */
	function action_after_setup_theme() {
		$theme = function_exists( 'wp_get_theme' ) ? wp_get_theme() : get_theme( get_current_theme() );

		if ( ! is_a( $theme, 'WP_Theme' ) && ! is_array( $theme ) )
			return;

		$customization_file = apply_filters( 'infinite_scroll_customization_file', dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Stylesheet']}.php", $theme['Stylesheet'] );

		if ( is_readable( $customization_file ) ) {
			require_once( $customization_file );
		}
		elseif ( ! empty( $theme['Template'] ) ) {
			$customization_file = dirname( __FILE__ ) . "/infinite-scroll/themes/{$theme['Template']}.php";

			if ( is_readable( $customization_file ) )
				require_once( $customization_file );
		}
	}

	/**
	 * Modify Infinite Scroll configuration information
	 *
	 * @uses Jetpack::get_active_modules, is_user_logged_in, stats_get_options, Jetpack_Options::get_option, get_option, JETPACK__API_VERSION, JETPACK__VERSION
	 * @filter infinite_scroll_js_settings
	 * @return array
	 */
	public function filter_infinite_scroll_js_settings( $settings ) {
		// Provide WP Stats info for tracking Infinite Scroll loads
		// Abort if Stats module isn't active
		if ( in_array( 'stats', Jetpack::get_active_modules() ) ) {
			// Abort if user is logged in but logged-in users shouldn't be tracked.
			if ( is_user_logged_in() && function_exists( 'stats_get_options' ) ) {
				$stats_options = stats_get_options();
				$track_loggedin_users = isset( $stats_options['reg_users'] ) ? (bool) $stats_options['reg_users'] : false;

				if ( ! $track_loggedin_users )
					return $settings;
			}

			// We made it this far, so gather the data needed to track IS views
			$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;

			// Pagetype parameter
			$settings['stats'] .= '&x_pagetype=infinite';
			if ( 'click' == $settings['type'] )
				$settings['stats'] .= '-click';

			$settings['stats'] .= '-jetpack';
		}

		// Check if Google Analytics tracking is requested
		$settings['google_analytics'] = (bool) get_option( $this->option_name_google_analytics );

		return $settings;
	}

	/**
	 * 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.
	 *
	 * @global $videopress
	 * @uses do_action()
	 * @uses apply_filters()
	 * @uses wp_enqueue_style()
	 * @uses wp_enqueue_script()
	 * @action wp_enqueue_scripts
	 * @return null
	 */
	public function action_wp_enqueue_scripts() {
		// Do not load scripts and styles on singular pages and static pages
		$load_scripts_and_styles = ! ( is_singular() || is_page() );
		if ( ! apply_filters( 'jetpack_infinite_scroll_load_scripts_and_styles', $load_scripts_and_styles ) )
			return;

		// VideoPress stand-alone plugin
		global $videopress;
		if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) {
			$videopress->enqueue_scripts();
		}

		// VideoPress Jetpack module
		if ( Jetpack::is_module_active( 'videopress' ) ) {
			Jetpack_VideoPress_Shortcode::enqueue_scripts();
		}

		// Fire the post_gallery action early so Carousel scripts are present.
		if ( Jetpack::is_module_active( 'carousel' ) ) {
			do_action( 'post_gallery', '', '' );
		}

		// Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled
		if ( Jetpack::is_module_active( 'tiled-gallery' ) ) {
			Jetpack_Tiled_Gallery::default_scripts_and_styles();
		}

		// Core's Audio and Video Shortcodes
		if ( 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ) ) {
			wp_enqueue_style( 'wp-mediaelement' );
			wp_enqueue_script( 'wp-mediaelement' );
		}

		if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
			wp_enqueue_style( 'wp-mediaelement' );
			wp_enqueue_script( 'wp-mediaelement' );
		}
	}
}
Jetpack_Infinite_Scroll_Extras::instance();

/**
 * Load main IS file
 */
require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" );

/**
 * 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();
 */
remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 );