Skip to content

Instantly share code, notes, and snippets.

@alfasado
Last active August 29, 2015 13:57
Show Gist options
  • Save alfasado/9926625 to your computer and use it in GitHub Desktop.
Save alfasado/9926625 to your computer and use it in GitHub Desktop.
mt/plugin/RewriteArchiver/php/config.php
<?php
class RewriteArchiver extends MTPlugin {
var $registry = array(
'config_settings' => array(
'RewriteArchiverRewriteTo' => array( 'default' => '/archiver.html' ),
),
'callbacks' => array(
'post_init' => 'rewrite_archiver',
),
);
function rewrite_archiver ( $mt, &$ctx, &$args ) {
$blog = $ctx->stash( 'blog' );
$app = $ctx->stash( 'bootstrapper' );
$file = $app->stash( 'file' );
if ( file_exists( $file ) ) return;
$url = $app->stash( 'url' );
$request = $app->stash( 'request' );
$tmpl = $app->config( 'RewriteArchiverRewriteTo' );
$tmpl = preg_replace( '/^\//', '', $tmpl );
$tmpl = preg_replace( '/\//', DIRECTORY_SEPARATOR, $tmpl );
$site_url = $blog->site_url();
$root = preg_replace( '/^https{0,1}:\/\/.*?\//', '/', $site_url );
$search = preg_quote( $root, '/' );
$path = preg_replace( "/^$search/", '', $request );
$folder = preg_replace( "!(.*)/.*$!", '$1', $path );
$new_url = $site_url . $tmpl;
$new_request = $root . $tmpl;
$new_file = $blog->site_path() . DIRECTORY_SEPARATOR . $tmpl;
if (! file_exists( $new_file ) ) return;
require_once( 'MTUtil.php' );
$cat = cat_path_to_category( $folder, NULL, 'folder' );
if ( isset( $cat ) ) {
$app->stash( 'file', $new_file );
$app->stash( 'request', $new_request );
$app->stash( 'url', $new_url );
$app->stash( 'orig_url', $url );
$ctx->stash( 'category', $cat[ 0 ] );
$ctx->__stash[ 'vars' ][ 'folder_path' ] = $folder;
$cache = $app->cache_filename( $ctx->stash( 'blog_id' ), $new_file, $new_url );
$app->stash( 'cache', $cache );
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment