Completed
Push — update/configure-url-to-jetpac... ( b7ff27...e36779 )
by
unknown
06:43
created

infinite_scroll_configuration_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

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