Skip to content

Instantly share code, notes, and snippets.

@efreed
Last active June 20, 2016 18:01
Show Gist options
  • Save efreed/6c52f859bdcaa5893147 to your computer and use it in GitHub Desktop.
Save efreed/6c52f859bdcaa5893147 to your computer and use it in GitHub Desktop.
Wordpress plugin to read zip'd website into a shortcode
<?php
/**
* Plugin Name: DoxyGen Integration
* Plugin URI: http://www.fivetalent.com/
* Description: Enables shortcodes to embed a doxygen site into a page
* Version: 1.0
* Author: Eric Freed / Five Talent
*/
// Insert HTML content into post
add_shortcode('doxygen_site', 'doxygen_site');
function doxygen_site($params, $content){
if (isset($params['output']) && $params['output'] == 'blank') {
return '';
}
$out = '';
$z = new ZipArchive();
$bottomstyle = <<<HTML
<style>
.nav-head { padding:0; }
#titlearea {
height: 170px;
padding-top: 20px;
}
#nav-tree {
background-color: inherit;
}
#nav-tree a { color: white; }
#nav-tree a:hover { background-color: #E21D1A; }
#nav-tree, #nav-tree .selected {
background-image: none;
}
#nav-tree .selected {
background-color: #E21D1A;
}
</style>
HTML;
$topstyle = <<<HTML
<style>
#content { width:100% }
#header { width: 250px; padding: 10px 5px; }
#menu-main-navigation {
max-height: 210px;
overflow: hidden;
}
.contents { margin-left:262px; }
@media screen and (max-width: 767px) {
#side-nav, #nav-tree, #MSearchBox {
display: none;
}
.contents {
margin-left:0;
}
}
</style>
HTML;
$script = <<<HTML
<script type="text/javascript">
// Move the location of the nav bar
var navplaceholder = jQuery("#side-nav").html();
jQuery("#nav-tree").remove();
jQuery("#header").append(navplaceholder);
//
function initResizable() {}
var resizeHeight = 0;
jQuery(function() {
// Remove all but the first 4 items in mobile menu
jQuery("#header select option:gt(4)").remove();
// Remove all but the first 4 items in left nav menu
jQuery("#menu-main-navigation li:gt(4)").remove();
// Copy nav items into the mobile menu
jQuery.each(NAVTREE[0][2],function(){
var item = '<option value="' + this[1] + '">' + this[0] + '</option>';
if (jQuery.isArray(this[2])) {
jQuery.each(this[2], function(){
item += '<option value="' + this[1] + '"> -- ' + this[0] + '</option>';
});
}
jQuery("#header select").append(item);
});
});
</script>
HTML;
if ($z->open(ABSPATH . $params['file']) === TRUE) {
$path = explode('/', substr($_SERVER['REQUEST_URI'], 1)); //remove intial "/" and build into an array
array_shift($path); // drop off the 'documentation/' prefix
array_shift($path); // drop off the page name part
$file = implode('/', $path);
if (empty($file)) {
$file = 'index.html';
}
$html = $z->getFromName($file);
$z->close();
//$pos = strpos($html, '<script type="text/javascript">');
//if ($pos !== false) {
// $html = substr_replace($html, $head, $pos, 0); //insert our head code
//}
$html = str_replace('<!--topcssflag-->', $topstyle, $html);
$html = str_replace('<!--jsflag-->', $script, $html);
$html = str_replace('<!--bottomcssflag-->', $bottomstyle, $html);
$html = str_replace('resize.js','',$html);
$out .= $html;
} else {
$out .= ABSPATH . $params['file'] . ' Could not load. ';
$out .= (file_exists(ABSPATH . $file))?'It does exist':'It does not exist';
}
return $out;
}
// To make possible the rewrite rule with extra elements after a page name, need to disable redirect_canonical
add_filter('redirect_canonical', 'doxygen_canonical_exclude');
add_filter('wpseo_canonical', 'doxygen_canonical_exclude'); // should be needed if Yoast is installed
function doxygen_canonical_exclude($canonical) {
if (strpos($_SERVER['REQUEST_URI'],'/documentation')===0 &&
strpos($_SERVER['REQUEST_URI'],'/search/')===false && // Search ajax support
substr_count($_SERVER['REQUEST_URI'],'/')>2 // Let /documentation/page redirect to the trailing /
) {
$canonical = false;
}
return $canonical;
}
// Define rewrite rules
add_action('init','doxygen_readfile_init');
function doxygen_readfile_init() {
add_rewrite_rule('documentation/([^/]+)/(search/.+|.+[^l])$', 'wp-admin/admin-ajax.php?action=doxygen_readfile', 'top');
add_rewrite_rule('(documentation/[^/]+)/.+l$', 'index.php?pagename=$matches[1]', 'top');
}
//register_activation_hook(__FILE__, 'doxygen_flush_rewrite');
//register_deactivation_hook(__FILE__, 'doxygen_flush_rewrite');
function doxygen_flush_rewrite() {
flush_rewrite_rules(true);
}
// Enable reading of asset files
add_action('wp_ajax_doxygen_readfile', 'doxygen_readfile'); // for logged-in admins
add_action('wp_ajax_nopriv_doxygen_readfile', 'doxygen_readfile'); // for non-admins
function doxygen_readfile() {
global $wpdb;
$path = explode('/', substr($_SERVER['REQUEST_URI'], 1)); //remove intial "/" and build into an array
array_shift($path); // drop off the 'documentation/' prefix
$page = $wpdb->get_results(
$wpdb->prepare("
SELECT p.post_content
FROM {$wpdb->posts} parent JOIN {$wpdb->posts} p ON p.post_parent = parent.ID
WHERE parent.post_name = 'documentation'
AND p.post_type = 'page' AND p.post_status = 'publish'
AND p.post_date < NOW() AND p.post_name = %s
", array_shift($path))
, ARRAY_A );
if (count($page) == 0) {
http_response_code(404);
echo 'invalid page name';
exit;
}
if (!preg_match('/\[doxygen_site file="\/([^"]+)"/i', $page[0]['post_content'], $doxygen_shortcodes)) {
http_response_code(404);
echo 'invalid shortcode';
exit;
}
$z = new ZipArchive();
if ($z->open(ABSPATH . $doxygen_shortcodes[1]) === TRUE) {
$dotparts = explode('.', $_SERVER['REQUEST_URI']);
switch($dotparts[count($dotparts)-1]) {
case 'js':
header('Content-type: text/javascript');
break;
case 'css':
header('Content-type: text/css');
break;
case 'png':
header('Content-type: image/png');
}
$file = implode('/', $path); // put the rest of the path back into a string
$file = explode('?', $file)[0]; // remove any get parameters
echo $z->getFromName($file); // if the file's not found, it will just return empty
$z->close();
exit;
}
http_response_code(404);
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment