Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created November 2, 2017 00:20
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 nfreear/0e4cf1b05811f7c890dfbf3573bfc4ef to your computer and use it in GitHub Desktop.
Save nfreear/0e4cf1b05811f7c890dfbf3573bfc4ef to your computer and use it in GitHub Desktop.
Static pages plugin for WordPress | © 2017 Nick Freear.
<?php namespace Nfreear\WP_Plugins;
/**
* Plugin Name: WP Static Pages
* Plugin URI: https://gist.github.com/nfreear
* Description: For some URI paths, display a static text or HTML file.
* Author: Nick Freear
* Author URI: https://github.com/nfreear
* Version: 1.0-alpha
*
* @copyright © 2017 Nick Freear.
* @author Nick Freear, 01-November-2017.
*
* @link https://github.com/IET-OU/wp-iet-generic-plugins
* @link http://headstar.com/eab/issues/2017/oct2017.html
*/
class WP_Static_Pages_Plugin {
const URI_REGEX = '/^\/(?P<path>20\d{2}\/[a-z]{3})(?P<ext>\.html|\.txt)/';
// const URI_REGEX = '/^\/(?P<year>20\d{2})\/(?P<month>[a-z]{3})(?P<y2>20\d{2})?(?P<ext>\.html|\.txt)/';
const PATH = '/path/to/static/{path}{ext}';
public function __construct() {
// add_filter( 'the_content', [ &$this, 'filter_the_content' ] );
add_action( 'init', [ &$this, 'init' ] );
}
public function init() {
$request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI' );
if ( ! preg_match( self::option( 'wpst_uri_regex', self::URI_REGEX ), $request_uri, $matches )) {
return;
}
$file_ext = $matches[ 'ext' ];
$subs = [];
foreach ( $matches as $key => $value ) {
$subs [ '{' . $key . '}' ] = $value;
}
// var_dump( $subs );
// $file_path = sprintf( self::option( 'y', self::PATH ), $matches[ 'path' ] );
$file_path = strtr( self::option( 'wpst_path', self::PATH ), $subs );
$file = file_get_contents( $file_path );
if ( $file ) {
if ( $file_ext === '.txt' ) {
header( 'Content-Type: text/plain; charset=utf-8' );
}
echo $file;
exit;
}
}
protected static function option( $key, $default ) {
$KEY = strtoupper( $key );
return defined( $KEY ) ? constant( $KEY ) : $default;
}
}
$wp_static_pages = new WP_Static_Pages_Plugin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment