Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gulp uncss
<?php
/**
* Plugin Name: Gulp Sitemap Generator
* Plugin URI: https://gladdy.uk/blog/2014/04/13/using-uncss-and-grunt-uncss-with-wordpress/
* Description: Generate a JSON list of every page on a site so it can be used with Gulp and uncss.
* Author: Liam Gladdy
* Author URI: http://gladdy.co.uk
* Version: 1.0
*/
add_action('template_redirect', 'show_sitemap');
function show_sitemap() {
if (isset($_GET['show_sitemap'])) {
// Set up an array for all the URLs
$urls = array();
// Homepage
$urls[] = get_site_url();
// Get all the pages, posts (including CPTs)
$the_query = new WP_Query(array('post_type' => 'any', 'posts_per_page' => '-1', 'post_status' => 'publish'));
while ($the_query->have_posts()) {
$the_query->the_post();
$urls[] = get_permalink();
}
// Authors
$authors = get_users();
foreach ($authors as $author) {
$urls[] = get_author_posts_url( $author->ID );
}
// Every term imaginable, even the empty ones (categories, custom taxonomies, tags, etc.)
$args = array(
'public' => true,
);
$taxonomies = get_taxonomies($args, 'names');
$args = array('hide_empty=0');
$terms = get_terms($taxonomies, $args);
foreach ($terms as $term) {
$urls[] = get_term_link( $term );
}
// Getting a list of Archive URLs seems like it should be easier...probably missing something on the Codex, but this works.
$args = array(
'type' => 'monthly',
'format' => 'custom',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 0
);
$archive_links_raw = wp_get_archives($args);
$archive_links_pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
preg_match_all($archive_links_pattern, $archive_links_raw, $cleaned_archive_links);
$cleaned_archive_links = $cleaned_archive_links[1];
foreach ($cleaned_archive_links as $cleaned_archive_link) {
$urls[] = $cleaned_archive_link;
}
// Add in a search result page for '.'
$main_url = get_site_url();
$urls[] = $main_url . '/?s=.';
// Force a search with no results
$urls[] = $main_url . '/?s=asdfasdfasdfasdf';
// Force a 404 page
$urls[] = $main_url . '/asdfasdfasdfasdf';
// Return all of the urls captured above and encode to json for UnCSS
die(json_encode($urls));
}
}
var gulp = require('gulp'),
shell = require('gulp-shell'),
uncss = require('gulp-uncss');
// Generate Sitemap JSON
gulp.task('sitemap', shell.task(['curl --silent --output sitemap.json http://dev.foo.org/\?show_sitemap']));
// Remove Unused CSS (use after running `gulp sitemap`)
gulp.task('uncss', function() {
return gulp.src('css/style.css')
.pipe(uncss({
html: JSON.parse(require('fs').readFileSync('sitemap.json', 'utf-8'))
}))
.pipe(gulp.dest('css/clean/'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment