Skip to content

Instantly share code, notes, and snippets.

Avatar

K Adam White kadamwhite

View GitHub Profile
@kadamwhite
kadamwhite / efficient-api-resource-fetch-demo.js
Last active February 19, 2023 05:56
Example of how to compose multiple API requests to efficiently fetch related resources.
View efficient-api-resource-fetch-demo.js
/**
* WP Post object. Only properties needed by the code are included.
*
* @typedef {object} WPPost
* @property {number} id ID of post.
* @property {number} author Post author ID.
* @property {number[]} categories IDs of associated categories.
* @property {number[]} tags IDs of associated tags.
* @property {number} featured_media ID of featured image.
*/
@kadamwhite
kadamwhite / README.md
Created September 23, 2022 00:13
Cool bug (potential WP core bug?) where meta with multiple rows registered as "single" prevents REST update.
View README.md

Reproduction Steps

  1. Clone these files into a plugin directory
  2. Activate plugin (will populate several postmeta table rows about most recent posts)
  3. Open that post in the editor
  4. See the textbox in the Publish Status panel with label "Edit me:"
  5. Put any string into that input
  6. Try to save
  7. Encounter error Updating failed. Could not update the meta value of example_post_meta in database.
View backtrace-to-logs.php
<?php
/**
* Format a backtrace in a way that makes sense to me visually.
* Command-click on filenames in terminal output to jump to them.
*
* @param array $backtrace
* @return string
*/
function backtrace_to_log( array $backtrace ) : string {
$table = [ [ ' file', 'function' ], [ '---------', '--------' ] ];
@kadamwhite
kadamwhite / trait-csv-report.php
Created February 11, 2022 00:41
A PHP trait to write data from WP-CLI to a CSV, in a memory-friendly and won't-lose-data-if-CLI-errors way.
View trait-csv-report.php
<?php
/**
* Simplify progressive generation of a CSV output file. Permits a command to
* write each row to a CSV as records are considered, avoiding the need to
* maintain the entire dataset in memory.
*/
/* phpcs:disable HM.Files.ClassFileName.MismatchedName */
// (HM's standards want this to be class-*, which is misleading.)
@kadamwhite
kadamwhite / ffmpeg-cheat-sheet.md
Last active November 5, 2022 13:38
ffmpeg command "cheat sheet"
View ffmpeg-cheat-sheet.md

ffmpeg cheat-sheet

Video

Compressing x265

ffmpeg -i INPUT_FILE.mkv -c copy -pix_fmt yuv420p10le -c:v libx265 -preset slow -crf 27 -profile:v main10 OUTPUT_FILE.mkv
@kadamwhite
kadamwhite / ExcludeMediaQueriesPlugin.js
Created April 22, 2021 15:07
A Webpack plugin to remove media queries matching certain patterns from a generated CSS file.
View ExcludeMediaQueriesPlugin.js
/**
* This file defines the project-level Webpack production configuration. It
* combines configs from all relevant plugins and themes into one single
* array (a "multi-configuration" Webpack setup) and runs those builds in
* a single pass.
*/
const { basename, extname } = require( 'path' );
const postcss = require( 'postcss' );
const filtermq = require( 'postcss-filter-mq' );
@kadamwhite
kadamwhite / phpcs-changed-files.sh
Created March 4, 2021 15:16
Bash script to run PHPCS against only files which have changed since branching off a specific base branch.
View phpcs-changed-files.sh
#/usr/bin/env bash
# Usage:
#
# Run this script (via an npm run alias) with no arguments to compute the
# issues in files changed since you branched from "development."
#
# npm run lint:php:changed
#
# Pass a specific branch name as the first argument to determine which files
@kadamwhite
kadamwhite / geo.js
Created February 16, 2021 17:25
Script to create an SVG visual representation of a page's DOM nesting
View geo.js
const equalPoint = ( x1, y1, x2, y2 ) => ( x1 === x2 && y1 === y2 );
const sum = nums => nums.reduce( ( sum, num ) => sum + num, 0 );
// See https://stackoverflow.com/questions/9043805/test-if-two-lines-intersect-javascript-function
const linesIntersect = ( x1, y1, x2, y2, x3, y3, x4, y4 ) => {
if (
equalPoint( x1, y1, x3, y3 ) ||
equalPoint( x1, y1, x4, y4 ) ||
equalPoint( x2, y2, x3, y3 ) ||
@kadamwhite
kadamwhite / summarize-altis-traefik-logs
Created February 10, 2021 19:34
Use like `docker logs docker_proxy_1 --follow | summarize-altis-traefik-logs
View summarize-altis-traefik-logs
#!/usr/bin/env node
const { match } = require('assert');
const { parse } = require('path');
const readline = require('readline');
const { getContainers, matchContainerByURL } = require( '/home/kadam/bin/get-docker-ips' );
// Figure out if we asked for a certain number of events, or else show the 20 most recent.
const [ , , flag, val ] = process.argv;
let eventCount = 20;
@kadamwhite
kadamwhite / get-docker-ips.js
Last active February 10, 2021 18:00
Node script to print out all running docker containers by IP
View get-docker-ips.js
#!/usr/bin/env node
const child_process = require( 'child_process' );
/**
* Execute a command as a spawned process.
*
* @param {String} command A bash command string, excluding arguments.
* @param {String[]} args An array of argument strings for the provided command.
*/