Skip to content

Commit

Permalink
Use offline page content for the offline template
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Aug 27, 2019
1 parent 94d8c59 commit 8d65cb8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
60 changes: 58 additions & 2 deletions public_html/wp-content/mu-plugins/theme-templates/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace WordCamp\Theme_Templates;
use WP_Post;
use WP_Service_Worker_Scripts;
use WordCamp\Blocks\Utilities as BlockUtilities;

defined( 'WPINC' ) || die();

Expand Down Expand Up @@ -132,7 +133,60 @@ function inject_offline_template( $template_path ) {
}

/**
* Add a cache-buster to the offline template's pre-cache entry.
* Get the offline page.
*
* This is a page created when the WordCamp site is created, so it should exist with this post meta.
*
* @return WP_Post|false
*/
function get_offline_page() {
$found_pages = get_posts( array(
'posts_per_page' => 1,
'post_type' => 'page',
'orderby' => 'date',
'order' => 'asc',
'meta_key' => 'wc_page_offline',
'meta_value' => 'yes',
'hierarchical' => 0,
) );
if ( count( $found_pages ) ) {
return $found_pages[0];
}

return false;
}

/**
* Get the offline page content, if the page exists. Otherwise, use simple defaults.
*
* @return array {
* Content for the offline template.
*
* @type string $title Title of the page, or a default title.
* @type string $content Content of the page, or the PWA error callback.
* }
*/
function get_offline_content() {
$page = get_offline_page();
if ( $page ) {
return array(
'title' => $page->post_title,
'content' => BlockUtilities\get_all_the_content( $page ),
);
}

ob_start();
wp_service_worker_error_message_placeholder();
$offline_content = ob_get_clean();

return array(
'title' => esc_html__( 'Offline', 'wordcamporg' ),
'content' => $offline_content,
);
}

/**
* Add the Offline page content as a revision parameter, to bust the cache when the page is changed.
*
* @param array|false $entry {
* Offline error precache entry.
Expand All @@ -144,8 +198,10 @@ function inject_offline_template( $template_path ) {
* @return array|false
*/
function add_offline_template_cachebuster( $entry ) {
$page = get_offline_content();

if ( $entry && isset( $entry['revision'] ) ) {
$entry['revision'] .= ';' . filemtime( __DIR__ . '/templates/offline.php' );
$entry['revision'] .= ';' . md5( $page['content'] );
}

return $entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,22 @@
namespace WordCamp\Theme_Templates;

get_header();
?>

<main>
<h1>
<?php echo esc_html( _x( 'Offline', 'Page Title', 'wordcamporg' ) ); ?>
</h1>

<p>
<?php esc_html_e( "This page couldn't be loaded because you appear to be offline. Please try again once you have a network connection.", 'wordcamporg' ); ?>
</p>

<p>
<?php esc_html_e( 'In the mean time, hopefully this information is useful:', 'wordcamporg' ); ?>
</p>

<?php
require_once dirname( __DIR__ ) . '/parts/dates.php';
require_once dirname( __DIR__ ) . '/parts/location.php';
?>

<h3>
<?php esc_html_e( 'Schedule' ); ?>
</h3>
$offline_page = get_offline_content();
?>

<?php echo do_shortcode( '[schedule]' ); ?>
<main id="main" class="site-main">
<section class="error-offline">
<header class="page-header">
<h1 class="page-title">
<?php echo esc_html( $offline_page['title'] ); ?>
</h1>
</header>

<div class="page-content">
<?php echo wp_kses_post( $offline_page['content'] ); ?>
</div>
</section>
</main>

<?php
Expand Down

0 comments on commit 8d65cb8

Please sign in to comment.