Skip to content

Instantly share code, notes, and snippets.

Avatar

Joe Maller joemaller

View GitHub Profile
@joemaller
joemaller / WufooEndpoint.php
Last active Dec 1, 2020
A class for creating a WordPress rewrite rule to serve a stylesheet from a persistent top-level url
View WufooEndpoint.php
<?php
namespace IdeasOnPurpose;
/**
* This class created a nice little persistent endpoint for an
* externally referenced stylesheet. This was necessary because
* our themes are versioned with each release living in a different-
* named directory. All assets are also generated by Webpack and
* have hashed filenames.
@joemaller
joemaller / ip-address-log.sh
Last active Oct 10, 2020
Script for logging IP address changes. Checked with shfmt: `docker run --rm -it -v "$PWD":/src peterdavehello/shfmt shfmt -i 2 -w /src/ip-address-log.sh`
View ip-address-log.sh
#! /usr/bin/env bash
# Stop on errors, unset vars and broken pipes
set -o errexit
set -o nounset
set -o pipefail
LOG_FILE=${1:-}
if [ -z "$LOG_FILE" ]; then
echo "Please provide a logfile as the first argument"
@joemaller
joemaller / inotifywait
Created Aug 28, 2020
inotifywait docker command
View inotifywait
docker run -it -v $PWD:/app alpine sh -c 'apk --no-cache add inotify-tools; inotifywait -m --format "%e %f" /app'
@joemaller
joemaller / macos-onedrive-migration.sh
Created Jun 17, 2020
Rename commands for migrating a macOS fileserver to Microsoft OneDrive
View macos-onedrive-migration.sh
#! /usr/bin/env bash
# This is a small collection of helper commands for migrating a legacy macOS fileserver
# to OneDrive/Sharepoint. Filenames on cloud platforms are more restrictive than macOS, so
# a large number of files needed renaming before they could be synced to the Microsoft Cloud.
# 1. Use the rename CLI tool. It's installed by default on Debian/Ubuntu but needs to be
# installed on macOS and some other platforms. More here: http://plasmasturm.org/code/rename/
brew install rename
@joemaller
joemaller / child-filepath.md
Last active Apr 28, 2020
Get PHP child Class file paths from inherited parent class methods
View child-filepath.md

Get PHP child Class file paths from inherited parent class methods

While refactoring some code into a reusable PHP Class I hit a brief roadblock where some code expected the [__FILE__ magic constant][file] file path. Since that value always refers to the specific file it's called from, it won't work from a separate Class file because the referenced path would be the parent instead of the child.

The full filepath of a child Class can be inferred from an inherited parent Class method by combining [get_class($this)][get_class] or [get_called_class()][get_called_class] with the [ReflectionClass::getFileName][getfilename] method like this:

// ParentClass.php
class ParentClass
{
@joemaller
joemaller / postmortem.md
Created Apr 20, 2020
recap of tracking down a very tricky bug
View postmortem.md

Postmortem on a multi-layered bug

Also, a very long, mostly miserable day.

The problem

A web project was recently updated but now won't build on some computers. The updates largely involved migrating the project to our [webpack-based Docker toolset][docker-build] which is in use on other projects and is supposed to be stable. Updates were authored on Macs, then tested on Windows -- where the whole project failed to run.

"THIS IS IMPOSSIBLE"

@joemaller
joemaller / README.md
Last active Apr 2, 2020
A snippet for wrapping a selection in HTML comments which include the file name
View README.md

Wrapped template fragments

When developing websites, the dev team at Ideas On Purpose has found it very useful to wrap template fragments in named HTML comments. During development and debugging, these waypoints reveal the exact files and structure of how how the page was assembled.

Most files look something like this:

<?php
  // a bunch of logic which populates the $pages variable
?>
@joemaller
joemaller / wordpress-is_term.md
Created Mar 12, 2020
A simple is_term() conditional function for WordPress
View wordpress-is_term.md

Even though individual Tags, Categories and Taxonomies are all represented as WP_Term objects, WordPress has three separate conditional tags for sorting them out: is_tag(), is_category() and is_tax(). These are sometimes combined into a single conditional statement in WordPress core, and there are plenty of examples on GitHub where developers have created an umbrella is_term() function like this:

function is_term($term = '') {
    return is_tag($term) || is_category($term) || is_tax($term);
}

It would be nice to have that function added to WordPress core.

@joemaller
joemaller / crontab
Created Mar 11, 2020
Add this to a user's crontab on macOS to auto-remove downloads more than 14 days old
View crontab
# Removes downloads older than 14 days. Runs every 6 hours
45 */6 * * * find ~/Downloads -maxdepth 1 -mtime +14 -exec rm -rf {} \;
@joemaller
joemaller / README.md
Last active Feb 28, 2020
Fix term counts so wp-admin Taxonomy listings show the correct number of items for the selected post_type.
View README.md

Fixing post counts for WordPress taxonomies

The problem

For WordPress sites using Custom Post Types, Taxonomy admin listings show cumulative term-assignment totals for all post_types instead of counts specific to the selected post_type. Since Taxonomy listings are always attached to a single post_type, totals for shared taxonomies don't make sense.

The Goal

Fix term counts so wp-admin Taxonomy listings show the correct number of items for the selected post_type.

You can’t perform that action at this time.