Skip to content

Instantly share code, notes, and snippets.

@JudeRosario
Created June 11, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JudeRosario/e0c449683474b25829811216287771b7 to your computer and use it in GitHub Desktop.
Save JudeRosario/e0c449683474b25829811216287771b7 to your computer and use it in GitHub Desktop.
class PRELOAD_FULLPAGE_CACHE
{
function __construct() {
add_action( 'wp_insert_post', array( $this, 'preload_desktop' ), 900, 3 ); // let's fetch the post very late
add_action( 'wp_insert_post', array( $this, 'preload_mobile' ), 990, 3 ); // let's fetch mobile version even later
add_action( 'wp_insert_post', array( $this, 'preload_amp' ), 999, 3 ); // let's fetch AMP version at last; only works on posts
}
// verison to fetch: desktop
// user-agent: Chrome 62 on a macOS Sierra 10.12.6
function preload_desktop( $post_ID, $post, $update ) {
$desktop_url = get_permalink( $post_ID );
$desktop_url_args = array(
'httpversion' => '1.1',
'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
);
wp_remote_get( $desktop_url, $desktop_url_args );
}
// version to fetch: mobile
// user-agent: iPhone 6 on iOS 9
function preload_mobile( $post_ID, $post, $update ) {
$mobile_url = get_permalink( $post_ID );
$mobile_url_args = array(
'httpversion' => '1.1',
'user-agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
);
wp_remote_get( $mobile_url, $mobile_url_args );
}
// version to fetch: amp
// user-agent: Chrome 62 on a macOS Sierra 10.12.6
function preload_amp( $post_ID, $post, $update ) {
$amp_url = get_permalink( $post_ID ) . 'amp/';
$amp_url_args = array(
'httpversion' => '1.1',
'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
);
wp_remote_get( $amp_url, $amp_url_args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment