<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
use Automattic\Jetpack\Assets;
Plugin Name: The Neverending Home Page.
Plugin URI: https://automattic.com/
Description: Adds infinite scrolling support to the front-end blog post view for themes, pulling the next set of posts automatically into view when the reader approaches the bottom of the page.
Author URI: https://automattic.com/
License: GNU General Public License v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Class: The_Neverending_Home_Page relies on add_theme_support, expects specific
* styling from each theme; including fixed footer.
* @phan-constructor-used-for-side-effects
class The_Neverending_Home_Page {
* Maximum allowed number of posts per page in $_REQUEST.
const MAX_ALLOWED_POSTS_PER_PAGE_ΙΝ_REQUEST = 5000;
* Register actions and filters, plus parse IS settings
* @uses add_action, add_filter, self::get_settings
public function __construct() {
add_action( 'pre_get_posts', array( $this, 'posts_per_page_query' ) );
add_action( 'admin_init', array( $this, 'settings_api_init' ) );
add_action( 'template_redirect', array( $this, 'action_template_redirect' ) );
add_action( 'customize_preview_init', array( $this, 'init_customizer_assets' ) );
add_action( 'template_redirect', array( $this, 'ajax_response' ) );
add_action( 'custom_ajax_infinite_scroll', array( $this, 'query' ) );
add_filter( 'infinite_scroll_query_args', array( $this, 'inject_query_args' ) );
add_filter( 'infinite_scroll_allowed_vars', array( $this, 'allowed_query_vars' ) );
add_action( 'the_post', array( $this, 'preserve_more_tag' ) );
add_action( 'wp_footer', array( $this, 'footer' ) );
add_filter( 'infinite_scroll_additional_scripts', array( $this, 'add_mejs_config' ) );
add_filter( 'grunion_contact_form_redirect_url', array( $this, 'filter_grunion_redirect_url' ) );
// needs to happen after parse_query so that Jetpack_AMP_Support::is_amp_request() is ready.
add_action( 'wp', array( $this, 'amp_load_hooks' ) );
// Parse IS settings from theme
* Initialize our static variables
* @var null - I don't think this is used?
public static $the_time = null;
* Don't access directly, instead use self::get_settings().
public static $settings = null;
* The enabled option name.
public static $option_name_enabled = 'infinite_scroll';
* Parse IS settings provided by theme
* @uses get_theme_support, infinite_scroll_has_footer_widgets, sanitize_title, add_action, get_option, wp_parse_args, is_active_sidebar
public static function get_settings() {
'type' => 'scroll', // scroll | click
'requested_type' => 'scroll', // store the original type for use when logic overrides it
'footer_widgets' => false, // true | false | sidebar_id | array of sidebar_ids -- last two are checked with is_active_sidebar
'container' => 'content', // container html id
'wrapper' => true, // true | false | html class -- the html class.
'render' => false, // optional function, otherwise the `content` template part will be used
'footer' => true, // boolean to enable or disable the infinite footer | string to provide an html id to derive footer width from
'footer_callback' => false, // function to be called to render the IS footer, in place of the default
'posts_per_page' => false, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- int | false to set based on IS type
'click_handle' => true, // boolean to enable or disable rendering the click handler div. If type is click and this is false, page must include its own trigger with the HTML ID `infinite-handle`.
if ( self::$settings === null ) {
$css_pattern = '#[^A-Z\d\-_]#i';
// Validate settings passed through add_theme_support()
$_settings = get_theme_support( 'infinite-scroll' );
if ( is_array( $_settings ) ) {
// Preferred implementation, where theme provides an array of options
if ( isset( $_settings[0] ) && is_array( $_settings[0] ) ) {
foreach ( $_settings[0] as $key => $value ) {
if ( in_array( $value, array( 'scroll', 'click' ), true ) ) {
$settings['requested_type'] = $value;
$settings[ $key ] = $settings['requested_type'];
if ( is_string( $value ) ) {
$settings[ $key ] = sanitize_title( $value );
} elseif ( is_array( $value ) ) {
$settings[ $key ] = array_map( 'sanitize_title', $value );
} elseif ( is_bool( $value ) ) {
$settings[ $key ] = $value;
if ( 'wrapper' === $key && is_bool( $value ) ) {
$settings[ $key ] = $value;
$value = preg_replace( $css_pattern, '', $value );
if ( ! empty( $value ) ) {
$settings[ $key ] = $value;
if ( false !== $value && is_callable( $value ) ) {
$settings[ $key ] = $value;
if ( is_bool( $value ) ) {
$settings[ $key ] = $value;
} elseif ( is_string( $value ) ) {
$value = preg_replace( $css_pattern, '', $value );
if ( ! empty( $value ) ) {
$settings[ $key ] = $value;
if ( is_callable( $value ) ) {
$settings[ $key ] = $value;
$settings[ $key ] = false;
if ( is_numeric( $value ) ) {
$settings[ $key ] = (int) $value;
if ( is_bool( $value ) ) {
$settings[ $key ] = $value;
} elseif ( is_string( $_settings[0] ) ) {
// Checks below are for backwards compatibility
// Container to append new posts to
$settings['container'] = preg_replace( $css_pattern, '', $_settings[0] );
if ( isset( $_settings[1] ) ) {
$settings['wrapper'] = (bool) $_settings[1];
// Always ensure all values are present in the final array
$settings = wp_parse_args( $settings, $defaults );
// Check if a legacy `infinite_scroll_has_footer_widgets()` function is defined and override the footer_widgets parameter's value.
// Otherwise, if a widget area ID or array of IDs was provided in the footer_widgets parameter, check if any contains any widgets.
// It is safe to use `is_active_sidebar()` before the sidebar is registered as this function doesn't check for a sidebar's existence when determining if it contains any widgets.
if ( function_exists( 'infinite_scroll_has_footer_widgets' ) ) {
// @phan-suppress-next-line PhanUndeclaredFunction -- Checked above. See also https://github.com/phan/phan/issues/1204.
$settings['footer_widgets'] = (bool) infinite_scroll_has_footer_widgets();
} elseif ( is_array( $settings['footer_widgets'] ) ) {
$sidebar_ids = $settings['footer_widgets'];
$settings['footer_widgets'] = false;
foreach ( $sidebar_ids as $sidebar_id ) {
if ( is_active_sidebar( $sidebar_id ) ) {
$settings['footer_widgets'] = true;
} elseif ( is_string( $settings['footer_widgets'] ) ) {
$settings['footer_widgets'] = (bool) is_active_sidebar( $settings['footer_widgets'] );
* Filter Infinite Scroll's `footer_widgets` parameter.
* @module infinite-scroll
* @param bool $settings['footer_widgets'] Does the current theme have Footer Widgets.
$settings['footer_widgets'] = apply_filters( 'infinite_scroll_has_footer_widgets', $settings['footer_widgets'] );
// Finally, after all of the sidebar checks and filtering, ensure that a boolean value is present, otherwise set to default of `false`.
if ( ! is_bool( $settings['footer_widgets'] ) ) {
$settings['footer_widgets'] = false;
// Ensure that IS is enabled and no footer widgets exist if the IS type isn't already "click".
if ( 'click' !== $settings['type'] ) {
// Check the setting status
$disabled = '' === get_option( self::$option_name_enabled ) ? true : false;
// Footer content or Reading option check
if ( $settings['footer_widgets'] || $disabled ) {
$settings['type'] = 'click';
// Force display of the click handler and attendant bits when the type isn't `click`
if ( 'click' !== $settings['type'] ) {
$settings['click_handle'] = true;
// Store final settings in a class static to avoid reparsing
self::$settings = $settings;
* Filter the array of Infinite Scroll settings.
* @module infinite-scroll
* @param array $settings Array of Infinite Scroll settings.
$filtered_settings = apply_filters( 'infinite_scroll_settings', self::$settings );
// Ensure all properties are still set.
return (object) wp_parse_args( $filtered_settings, $defaults );
* Number of posts per page.
* @uses self::wp_query, self::get_settings, apply_filters
public static function posts_per_page() {
$settings = self::get_settings();
$posts_per_page = $settings->posts_per_page ? $settings->posts_per_page : self::wp_query()->get( 'posts_per_page' );
$posts_per_page_core_option = get_option( 'posts_per_page' );
// If Infinite Scroll is set to click, and if the site owner changed posts_per_page, let's use that.
'click' === $settings->type
&& ( '10' !== $posts_per_page_core_option )
$posts_per_page = $posts_per_page_core_option;
// Take JS query into consideration here.
$posts_per_page_in_request = isset( $_REQUEST['query_args']['posts_per_page'] ) ? (int) $_REQUEST['query_args']['posts_per_page'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $posts_per_page_in_request > 0 &&
self::MAX_ALLOWED_POSTS_PER_PAGE_ΙΝ_REQUEST >= $posts_per_page_in_request
$posts_per_page = $posts_per_page_in_request;
* Filter the number of posts per page.
* @module infinite-scroll
* @param int $posts_per_page The number of posts to display per page.
return (int) apply_filters( 'infinite_scroll_posts_per_page', $posts_per_page );
* Retrieve the query used with Infinite Scroll
public static function wp_query() {
* Filter the Infinite Scroll query object.
* @module infinite-scroll
* @param WP_Query $wp_the_query WP Query.
return apply_filters( 'infinite_scroll_query_object', $wp_the_query );
* Has infinite scroll been triggered?
public static function got_infinity() {
* Filter the parameter used to check if Infinite Scroll has been triggered.
* @module infinite-scroll
* @param bool isset( $_GET[ 'infinity' ] ) Return true if the "infinity" parameter is set.
return apply_filters( 'infinite_scroll_got_infinity', isset( $_GET['infinity'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no changes made to the site.
* Is this guaranteed to be the last batch of posts?
public static function is_last_batch() {
* Override whether or not this is the last batch for a request
* @module infinite-scroll
* @param bool|null null Bool if value should be overridden, null to determine from query
* @param object self::wp_query() WP_Query object for current request
* @param object self::get_settings() Infinite Scroll settings
$override = apply_filters( 'infinite_scroll_is_last_batch', null, self::wp_query(), self::get_settings() ); // phpcs:ignore WordPress.WP.ClassNameCase.Incorrect -- False positive.
if ( is_bool( $override ) ) {
$entries = (int) self::wp_query()->found_posts;
$posts_per_page = self::posts_per_page();
// This is to cope with an issue in certain themes or setups where posts are returned but found_posts is 0.
return (bool) ( ! is_countable( self::wp_query()->posts ) || ( count( self::wp_query()->posts ) < $posts_per_page ) );
$paged = max( 1, (int) self::wp_query()->get( 'paged' ) );
// Are there enough posts for more than the first page?
if ( $entries <= $posts_per_page ) {
// Calculate entries left after a certain number of pages
if ( $paged && $paged > 1 ) {
$entries -= $posts_per_page * $paged;
// Are there some entries left to display?
* The more tag will be ignored by default if the blog page isn't our homepage.
* Let's force the $more global to false.
* @param array $array - the_post array.
public function preserve_more_tag( $array ) {
if ( self::got_infinity() ) {
$more = 0; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- 0 = show content up to the more tag. Add more link.
* Add a checkbox field to Settings > Reading
* for enabling infinite scroll.
* Only show if the current theme supports infinity.
* @uses current_theme_supports, add_settings_field, __, register_setting
public function settings_api_init() {
if ( ! current_theme_supports( 'infinite-scroll' ) ) {
// Add the setting field [infinite_scroll] and place it in Settings > Reading
add_settings_field( self::$option_name_enabled, '<span id="infinite-scroll-options">' . esc_html__( 'Infinite Scroll Behavior', 'jetpack' ) . '</span>', array( $this, 'infinite_setting_html' ), 'reading' );
register_setting( 'reading', self::$option_name_enabled, 'esc_attr' );
* HTML code to display a checkbox true/false option
* for the infinite_scroll setting.
public function infinite_setting_html() {
$settings = self::get_settings();
// If the blog has footer widgets, show a notice instead of the checkbox
if ( $settings->footer_widgets || 'click' === $settings->requested_type ) {
echo '<label><em>' . esc_html__( 'We’ve changed this option to a click-to-scroll version for you since you have footer widgets in Appearance → Widgets, or your theme uses click-to-scroll as the default behavior.', 'jetpack' ) . '</em></label>';
echo '<label><input name="infinite_scroll" type="checkbox" value="1" ' . checked( 1, '' !== get_option( self::$option_name_enabled ), false ) . ' /> ' . esc_html__( 'Check to load posts as you scroll. Uncheck to show clickable button to load posts', 'jetpack' ) . '</label>';
// translators: the number of posts to show on each page load.
echo '<p class="description">' . esc_html( sprintf( _n( 'Shows %s post on each load.', 'Shows %s posts on each load.', self::posts_per_page(), 'jetpack' ), number_format_i18n( self::posts_per_page() ) ) ) . '</p>';
* Does the legwork to determine whether the feature is enabled.
* @uses current_theme_supports, self::archive_supports_infinity, self::get_settings, add_filter, wp_enqueue_script, plugins_url, wp_enqueue_style, add_action
* @action template_redirect
public function action_template_redirect() {
// Check that we support infinite scroll, and are on the home page.
if ( ! current_theme_supports( 'infinite-scroll' ) || ! self::archive_supports_infinity() ) {
$id = self::get_settings()->container;
// Check that we have an id.
// AMP infinite scroll functionality will start on amp_load_hooks().
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
'the-neverending-homepage',
Assets::get_file_url_for_environment(
'_inc/build/infinite-scroll/infinity.min.js',
'modules/infinite-scroll/infinity.js'
JETPACK__VERSION . '-is5.0.1', // Added for ability to cachebust on WP.com.
// Add our default styles.
wp_register_style( 'the-neverending-homepage', plugins_url( 'infinity.css', __FILE__ ), array(), '20140422' );