PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.7.5
Jetpack – WP Security, Backup, Speed, & Growth v2.7.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / infinite-scroll.php

infinite-scroll.php in Jetpack – WP Security, Backup, Speed, & Growth 2.7.5, at modules/infinite-scroll.php

231 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * 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.
178 *
179 * @global $videopress
180 * @uses do_action()
181 * @uses apply_filters()
182 * @uses wp_enqueue_style()
183 * @uses wp_enqueue_script()
184 * @action wp_enqueue_scripts
185 * @return null
186 */
187 public function action_wp_enqueue_scripts() {
188 // VideoPress stand-alone plugin
189 global $videopress;
190 if ( ! empty( $videopress ) && The_Neverending_Home_Page::archive_supports_infinity() && is_a( $videopress, 'VideoPress' ) && method_exists( $videopress, 'enqueue_scripts' ) ) {
191 $videopress->enqueue_scripts();
192 }
193
194 // VideoPress Jetpack module
195 if ( Jetpack::is_module_active( 'videopress' ) ) {
196 Jetpack_VideoPress_Shortcode::enqueue_scripts();
197 }
198
199 // Fire the post_gallery action early so Carousel scripts are present.
200 if ( Jetpack::is_module_active( 'carousel' ) ) {
201 do_action( 'post_gallery', '', '' );
202 }
203
204 // Always enqueue Tiled Gallery scripts when both IS and Tiled Galleries are enabled
205 if ( Jetpack::is_module_active( 'tiled-gallery' ) ) {
206 Jetpack_Tiled_Gallery::default_scripts_and_styles();
207 }
208
209 // Core's Audio and Video Shortcodes
210 if ( 'mediaelement' === apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ) ) {
211 wp_enqueue_style( 'wp-mediaelement' );
212 wp_enqueue_script( 'wp-mediaelement' );
213 }
214
215 if ( 'mediaelement' === apply_filters( 'wp_video_shortcode_library', 'mediaelement' ) ) {
216 wp_enqueue_style( 'wp-mediaelement' );
217 wp_enqueue_script( 'wp-mediaelement' );
218 }
219 }
220 }
221 Jetpack_Infinite_Scroll_Extras::instance();
222
223 /**
224 * Load main IS file
225 */
226 require_once( dirname( __FILE__ ) . "/infinite-scroll/infinity.php" );
227
228 /**
229 * 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();
230 */
231 remove_action( 'after_setup_theme', 'the_neverending_home_page_theme_support', 5 );