Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active May 19, 2020 18:05
Show Gist options
  • Save westonruter/26667d26ea34ef8e9f5e1053a747c73f to your computer and use it in GitHub Desktop.
Save westonruter/26667d26ea34ef8e9f5e1053a747c73f to your computer and use it in GitHub Desktop.
WordPress PWA compatibility plugin for the Lovecraft theme by Anders Norén. For use with the PWA feature plugin: https://wordpress.org/plugins/pwa/
body.offline .toggles {
display: none;
}
<?php
/**
* Offline template.
*
* @package Google\PWA_Lovecraft_Theme_Compat
*/
// Prevent showing nav menus.
add_filter( 'has_nav_menu', '__return_false' );
add_filter( 'wp_list_pages', '__return_empty_string' );
?>
<?php get_header(); ?>
<div class="wrapper section">
<div class="section-inner">
<div class="content">
<div class="post single">
<div class="post-inner">
<div class="post-header">
<h1 class="post-title"><?php _e( 'Offline', 'pwa' ); ?></h1>
</div><!-- .post-header -->
<div class="post-content">
<?php wp_service_worker_error_message_placeholder(); ?>
</div>
</div><!-- .post-inner -->
</div><!-- .post -->
</div><!-- .content -->
<?php //get_sidebar(); ?>
<div class="clear"></div>
</div><!-- .section-inner -->
</div><!-- .wrapper.section -->
<?php get_footer(); ?>
<?php
/**
* PWA Lovecraft Theme Compat plugin bootstrap.
*
* @package Google\PWA_Lovecraft_Theme_Compat
* @author Weston Ruter, Google
* @license GPL-2.0-or-later
* @copyright 2020 Google Inc.
*
* @wordpress-plugin
* Plugin Name: PWA Lovecraft Theme Compat
* Plugin URI: https://gist.github.com/westonruter/26667d26ea34ef8e9f5e1053a747c73f
* Description: Plugin to add <a href="https://wordpress.org/plugins/pwa/">PWA plugin</a> compatibility to the <a href="https://wordpress.org/themes/lovecraft/">Lovecraft</a> theme by Anders Norén.
* Version: 0.1
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace Google\PWA_Lovecraft_Theme_Compat;
add_filter( 'after_setup_theme', __NAMESPACE__ . '\add_hooks', 20 );
/**
* Add hooks.
*/
function add_hooks() {
if ( 'lovecraft' !== get_template() ) {
return;
}
add_filter( 'template_include', __NAMESPACE__ . '\filter_template_include_to_override_offline_template', 20 );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\add_offline_template_style', 11 );
add_filter( 'wp_offline_error_precache_entry', __NAMESPACE__ . '\filter_offline_error_precache_entry' );
}
/**
* Locate offline template.
*
* @return string
*/
function locate_offline_template_path() {
return __DIR__ . '/offline.php';
}
/**
* Filter the template include file to override the offline template.
*
* @param string $include Template include file.
* @return string Filtered template include file.
*/
function filter_template_include_to_override_offline_template( $include ) {
if ( function_exists( 'is_offline' ) && \is_offline() ) {
$include = __DIR__ . '/offline.php';
}
return $include;
}
/**
* Add offline template styles.
*/
function add_offline_template_style() {
if ( function_exists( 'is_offline' ) && \is_offline() ) {
wp_add_inline_style( 'lovecraft_style', file_get_contents( __DIR__ . '/offline.css' ) );
}
}
/**
* Filter the offline error precache entry to ensure revision is bumped when changes are made to the template.
*
* @param array $entry Entry.
* @return array Entry.
*/
function filter_offline_error_precache_entry( $entry ) {
$entry['revision'] .= ';' . __NAMESPACE__ . '-' . filemtime( locate_offline_template_path() );
return $entry;
}
@westonruter
Copy link
Author

westonruter commented May 15, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment