Skip to content

Instantly share code, notes, and snippets.

View astockwell's full-sized avatar

Alex Stockwell astockwell

View GitHub Profile
@astockwell
astockwell / script.js
Created June 1, 2013 00:15
Simple JS timing/performance/latency testing script (via http://kellegous.com/j/2013/01/26/layout-performance/)
var Now;
if (typeof performance !== 'undefined' && performance.now) {
Now = function() {
return performance.now();
};
} else if (Date.now) {
Now = function() {
return Date.now();
};
} else {
var output = document.getElementById('output');
function assert( outcome, description ) {
var li = document.createElement('li');
li.className = outcome ? 'pass' : 'fail';
li.appendChild( document.createTextNode( description ) );
output.appendChild(li);
};
<script>
(function(d,s){
var x = d.createElement(s),
o = d.getElementsByTagName(s)[0];
x.src="script.js"; // your script here
x.async=true; // mostly optional
o.parentNode.insertBefore(s,o); // inject
})(document,'script');
</script>
@astockwell
astockwell / klas_to_flat.sh
Last active December 20, 2015 11:59
Creates a working project for flat (non-Wordpress) site development from the latest [KLAS framework](https://github.com/kylelarkin/klas). Usage is indicated at the top of the file. Don't use if you don't understand what's going on, as this file is destructive. Remove from your project after use.
#!/bin/bash
#### TO USE ####
# 1. Create a new folder for your project
# 2. Save this script in that folder (as klas_to_flat.sh)
# 3. Run `chmod +x klas_to_flat.sh` to make it executable
# 4. Run `./klas_to_flat.sh`
#### Smile ####
set -eu
@astockwell
astockwell / functions.php
Created August 2, 2013 19:12
Format Date/Time strings in PHP < version 5.3
<?php
/*
* VARIABLE FORMATS:
* $e_date = '02/10/2012' (string)
* $e_time = '7:00 pm' (string)
* $e_end = '11:00 pm' (string)
* $format_short = true (boolean)
*/
@astockwell
astockwell / functions.php
Last active December 21, 2015 00:59
Paginate a custom WP_Query on a Wordpress static page. Be sure to REFRESH PERMALINKS!!!
// add new rule to match routes at "/events/page/#"
function add_rewrite_rules($aRules) {
$aNewRules = array('events/page/(\d+)/?$' => 'index.php?pagename=events&paged=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');
@astockwell
astockwell / _README.md
Last active December 30, 2023 22:39
PHP Serialization Fix. For use after a find/replace/sed on MySQL dump, to repair String lengths in serialized PHP objects. Optimized and refactored for memory-safe production use in Python (~7s for 250MB dump, ~400K replacements). Written also in Perl (~7s for 250MB), Ruby (~8s for 250MB) and Go (~20s for 250MB) for benchmark comparison. Python …
@astockwell
astockwell / scrape_image_urls.rb
Created September 8, 2013 23:49
Scrape media portal of client site (had to use Watir due to site being built in backbone.js) to download 1000+ high-res images. 1) Scrape image file URLs from all gallery pages. 2) Run non-trivial curl/wget loop to download image files (not shown). 3) Re-organize files from step 2 based on product categories from step 1.
#!/bin/env ruby
require 'watir-webdriver'
@username = ENV["username"]
@password = ENV["password"]
@login_url = ENV["login_url"]
@product_url = "#{@login_url}/photos/"
@astockwell
astockwell / helpers.php
Last active December 26, 2015 05:59
PHP truncate text at word function
<?php
function truncate($string, $length, $tailing_char = '...') {
if (strlen($string) > $length):
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string . ' ', 0, $length)) . $tailing_char;
endif;
return $string;
}
@astockwell
astockwell / script.js
Created October 24, 2013 20:03
Enquire! "True" on line 46 activates this breakpoint for non-supporting browsers (IE!!!).
// ========================================================================== //
// Media Query JS support
// ========================================================================== //
$(function(){
mq_desktop = 620;
// Initial setup (all breakpoints)
$(document).ready(function() {
// Setup learnmore blocks
$('.learnmore').hide();