Skip to content

Instantly share code, notes, and snippets.

View balbuf's full-sized avatar
🍕
thinking about pizza

Stephen Beemsterboer balbuf

🍕
thinking about pizza
View GitHub Profile
@balbuf
balbuf / add-textdomain.php
Last active May 10, 2016 13:40
Add or update text domain of all WP i18n functions in a file
<?php
/**
* Console application, which adds textdomain argument
* to all i18n function calls
*
* Adapted from:
* http://develop.svn.wordpress.org/trunk/tools/i18n/add-textdomain.php
*
* Extended such that all text domains are replaced if found or added otherwise.
* Also, double-quoted strings are replaced by single-quoted strings.
@balbuf
balbuf / typeofProp.js
Created February 27, 2016 21:43
Safe JavaScript typeof checking for a deep property on a variable
/**
* Pass a defined variable and an arbitrary number of
* property names to get the typeof the deepest property.
* @param {mixed} value any defined JS variable
* optional, unlimited:
* @param {string|array} property name or array of args to call the previous property with and use that value
* @return {string|false} typeof that property or false if evaluating the chain would result in a reference error
*
* @example
* typeofProp(window,'location','href','toString',[],'substr','foo','bar'); // false
@balbuf
balbuf / sanitizecss.js
Created May 6, 2016 18:51
Sanitize CSS and optionally prepend each selector with another selector to isolate the application of these styles
/**
* Takes a block of CSS and uses the browser's CSS parsing mechanism to sanitize
* the content and return only valid style declarations. Optionally you can
* prepend each selector with another selector to isolate the styles even further.
* A use case would be accepting user-generated CSS.
*/
function sanitizeCSS(css, selectorPrefix) {
var style = document.createElement('style')
, rules
, output = ''
@balbuf
balbuf / wp-flatline.js
Created May 9, 2016 18:38
Bookmarklet to stop subsequent WordPress heartbeat calls in the current page load (prepend with `javascript:`)
(function($,a,f){a=$.ajax,f={};f.done=f.fail=f.always=f.abort=function(){return f};$.ajax=function(d){return d&&d.data&&d.data.action=='heartbeat'?f:a.apply($,arguments)};console.log('\uD83D\uDC94')})(jQuery);
@balbuf
balbuf / basecamp-list-indent.js
Created May 9, 2016 18:40
Bookmarklet to indent the current list item in the Basecamp WYSIWYG editor (prepend with `javascript:`)
(function($,s,l,p){(s=$('iframe.wysihtml5-sandbox')[0])&&(s=s.contentWindow.getSelection())&&(l=$(s.focusNode).closest('li'))&&(p=l.prev('li'))&&p.length&&p.append($('<ul>').append(l))})(jQuery);
@balbuf
balbuf / getXPath.js
Created June 28, 2016 19:18
Add a "getXPath" method to the prototype for HTML element nodes
window.HTMLElement.prototype.getXPath = function() {
// special cases
if (this.id) return '//*[@id="' + this.id + '"]';
if (this.tagName.toLowerCase() === 'html') return '/html';
var count = 0, index, nodes = this.parentNode.childNodes;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType !== 1 || nodes[i].tagName !== this.tagName) continue;
count++;
@balbuf
balbuf / ProtectedData.js
Created September 5, 2016 22:09
Demonstrates a method of managing protected data within objects
/**
* Protected setter/getter which uses an arbitrary object as a "key".
* The safety comes from the object "key" existing only within the protected scope of the consuming objects.
* @param {object} key object used to
* @param {bool} silent silently fails with incorrect key instead of
*/
function ProtectedData(key, silent) {
// key must be an object for it to be secure
if (Object(key) !== key) throw new Error('Invalid key type.');
// property mapping
@balbuf
balbuf / prototypeSnooper.js
Created September 5, 2016 22:29
Monitor a type of object by wrapping all of its prototype methods in a function that logs messages in the console when fired.
/**
* Provides a rudimentary means of debugging by snooping on method calls to a particular kind of object.
* All of the object's prototypal methods will be wrapped in a new function that provides information
* in the console. When the method is called, the object type, method, and params will be logged in
* the console, then the original method will be called with the proper arguments.
*
* @param {function} obj object constructor to snoop (not an instantiated object)
* @return {function} the same object for chaining
*/
function prototypeSnooper(obj) {
@balbuf
balbuf / wordpress-import-update.php
Created October 15, 2016 17:54
Force the WordPress importer to update existing posts instead of skipping them
<?php
/**
* When using the WordPress Importer, update existing
* posts instead of skipping them. Updates content according
* to the import file even if the existing post was updated
* more recently.
*
* To use, drop this file into your /mu-plugins/ folder or
* copy this code into your functions.php file.
@balbuf
balbuf / example.css
Last active February 6, 2019 19:22
SASS function to generate nth formulas based on repeating patterns
// example output CSS
.container :nth-of-type(5n + 1), .container :nth-of-type(5n + 2), .container :nth-of-type(5n + 3) {
background: orange;
}
.container :nth-of-type(5n + 4), .container :nth-of-type(5n + 5) {
background: blue;
}