Skip to content

Commit

Permalink
Move is_robot function to javascript
Browse files Browse the repository at this point in the history
The Liveblog is currently reyling on an existence of `vary_cache_on_function` function when modifying the behaviour for bots.

As not all environments taking advantage of page level caching have the posibility of varying page cache on user agent, the check should happen in browser.

This commit it moving the logic to inside the js/liveblog-lazyloader.js taking advantage of the same regex and disabling the lazyloading initialisation for robots + hiding the load more button.
  • Loading branch information
david-binda committed Sep 13, 2016
1 parent e50c8da commit 5ccdbcd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
22 changes: 0 additions & 22 deletions classes/class-wpcom-liveblog-lazyloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public static function is_enabled() {
*/
self::$enabled = (bool) apply_filters( 'liveblog_enable_lazyloader', true );

// Disable lazy loading for robots
if ( self::$enabled && self::is_robot() ) {
self::$enabled = false;
}

// Disable lazy loading on archived liveblogs
if ( 'enable' != WPCOM_Liveblog::get_liveblog_state() ) {
self::$enabled = false;
Expand All @@ -51,23 +46,6 @@ public static function is_enabled() {
return self::$enabled;
}

/**
* Checks if the current user is a robot.
*
* @return bool
*/
private static function is_robot() {

// Variant determiner for caches.
if ( function_exists( 'vary_cache_on_function' ) ) {
vary_cache_on_function(
'return isset( $_SERVER[\'HTTP_USER_AGENT\'] ) && preg_match( \'/bot|crawl|slurp|spider/i\', $_SERVER[\'HTTP_USER_AGENT\'] );'
);
}

return isset( $_SERVER['HTTP_USER_AGENT'] ) && preg_match( '/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'] );
}

/**
* Returns the number of initially displayed Liveblog entries.
*
Expand Down
5 changes: 5 additions & 0 deletions js/liveblog-lazyloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
initialize: function() {
lazyloader.entrySets = [];

if ( navigator.userAgent.match(/bot|crawl|slurp|spider/i) ) {
$( '.liveblog-load-more' ).hide();
return; //no lazy loading for robots
}

lazyloader.setBusy();
lazyloader.fetchEntries();

Expand Down

0 comments on commit 5ccdbcd

Please sign in to comment.