Skip to content

Instantly share code, notes, and snippets.

@joemaller
joemaller / validate-gf_token.php
Created October 10, 2024 17:09
Function to validate Gravity Forms gf_token save tokens for accessing in-progress forms
<?php
function validateGF_Token($token)
{
global $wpdb;
$gf_drafts_table = $wpdb->prefix . 'gf_draft_submissions';
$table_check = $wpdb->get_var("SHOW TABLES LIKE '{$gf_drafts_table}'");
if ($table_check !== $gf_drafts_table) {
return false;
}
@joemaller
joemaller / core-group-block-variations.md
Last active June 20, 2024 03:06
A solution for determining isActive in Wordpress (Gutenberg) core/group Block Variations.

Creating Block Varitions from core/groups

Recently we had a Block Variation which needed to be switched from core/columns to core/group. Should have been easy enough, but the identity of the variation, its name and icon, would never show up. No matter what, the variation's isActive method was not even being called.

The problem

It turns out that the various layouts available to the core/group Group Block are all Block Variations. Each has it's own isActive function, which simply checks the Block's layout.type attribute and sometimes it's layout.orientation.

The problem is that all of the isActive functions for each of the Block's variations are run in order until one returns true, at which point WordPress stops checking. This is documented in the very last paragraph of the Block Editor Handbook's [V

@joemaller
joemaller / call-exec.js
Created January 26, 2024 23:34
Mock child_process.exec in module in Vitest
import { exec } from "node:child_process";
export function callExec() {
return new Promise((resolve, reject) => {
exec(`ls -la`, (error, stdout, stderr) => {
if (error) {
reject(error);
return;
}
if (stdout) {

News to Posts Migration

  1. Update the iop-theme to >2.28.0
  2. Update iop-data-model to >0.4.0
  3. Delete all existing Posts in wp-admin
  4. Change all News posts' post_type to Posts in phpMyAdmin:
    UPDATE `wp_posts` SET `post_type` = 'post' WHERE `post_type` = 'news';
  5. Update permalink Custom Structure to /news/%postname%/

How to run phpcs on WordPress-Develop files without installing PHP

I've been running all my dev projects via Docker to avoid the pitfalls of installing server software on my computer. This also lets me jump between macheines very easily since everything I need is wrapped up in dockerfiles, docker-compose files or package.json scripts.

WordPress development doesn't work like that. The default development environment expects PHP to be installed and will try to run PHPCS validation checks from a local instance of PHP.

Use Composer's Docker image

The official Composer Docker image is extremely versatile. It can run arbitrary shell commands if the passed argument isn't a Composer keyword.

@joemaller
joemaller / wordpress-blocks-min-scripts.md
Last active September 29, 2023 02:41
How WordPress includes minimized script snippets for blocks in use.

WordPress v6.3.1 inlines styles for used blocks via the wp_head hook. However there is an additional wrinkle. If the SCRIPT_DEBUG constant is set and truthy, then a minimized copy of the styles will be inlined. The minimized copy is pre-generated and included with WordPress.

Code for choosing to use the min variant is in wp-includes/blocks.php:

// Check whether styles should have a ".min" suffix or not.
$suffix = SCRIPT_DEBUG ? '' : '.min';
if ( $is_core_block ) {
    $style_path = ( 'editorStyle' === $field_name ) ? "editor{$suffix}.css" : "style{$suffix}.css";
}
@joemaller
joemaller / wsl-port-proxy.json
Created March 3, 2023 20:08
script and config file for configuring WSL 2 portproxy rules
{
"ports": [3000, 5173, 8000]
}
@joemaller
joemaller / conversion.js
Created November 15, 2022 15:48
Question about preferred code conversion
// Starting with an API returned data structure like this:
const Parameters = [
{
Name: "PASS",
Type: "SecureString",
Value: "cGFzc3dvcmQ=",
Version: 3,
LastModifiedDate: "2022-11-14T15:13:08.263Z",
ARN: "arn:aws:ssm:us-east-1:678000000000:parameter/PASS",
@joemaller
joemaller / curl-format.txt
Created November 8, 2022 20:13
Template for reporting connection times from curl connections. More here: https://blog.josephscott.org/2011/10/14/timing-details-with-curl/
\n
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
———\n
time_total: %{time_total}\n
@joemaller
joemaller / harrassment-training-bot.js
Created October 6, 2022 14:56
bot for helping test online training. Kind of garbage, but maybe useful in the future.
/**
* For ADP's 2022 sexual harrassment training
*/
cr = () => {
const next = $('button.next-button:not(.disabled)')
if (next.length) {
next.click();
}