Skip to content

Instantly share code, notes, and snippets.

View mcaskill's full-sized avatar
🥃

Chauncey McAskill mcaskill

🥃
View GitHub Profile
@mcaskill
mcaskill / WP-NF-Fix-reCAPTCHA_V2.md
Last active June 7, 2023 17:05
WP / NF: Fix and improve the handling of reCAPTCHA V2 in Ninja Forms. See README below.

README: Fix eager execution/challenge of reCAPTCHA V2

When a reCAPTCHA field is configured as "invisible", Ninja Forms will execute grecaptcha.execute() when the form is rendered and in intervals of 110 seconds, regardless of the form's state. If reCAPTCHA determines a challenge is necessary, its appearance on page load is unexpected and confusing, and its potential recurrence is very annoying for users.

This patch replaces the interval callback with a more complex solution that involves interrupting the form submit with a temporary validation error (that displays "Processing…" error). When reCAPTCHA is done (checks and challenges completed), the temporary validation error is removed and the form is submitted to the server for final validation.

If you use Ninja Forms Multi-Part, you will have to remove its reCAPTCHA controller which is no longer needed with this patch.

Apply the following patches:

@mcaskill
mcaskill / WP-NF-Custom_Asynchronous_Validation.md
Last active June 5, 2023 12:56
WP / NF: Support for custom, complex, remote field validation for Ninja Forms. See README below.

README: Custom Ninja Forms Field Validation

The accompanying PHP and JS files contain stripped-down controllers that provide support for custom validation logic along with support for asynchronous validation to handle slow/complex or remote/confirmation routines.

For the client-side, this is accomplished by adding a temporary custom_validation_validating error to interrupt any attempt to submit the form or move to another part of a multi-part form. This temporary error displays a customizable "Validating…" error message. When the custom validation is complete, if the field is invalid the temporary error is replaced with a corresponding error message.

If the user attempted to submit the form or move to the next part of form, and asynchronous validation is in progress, it will re-attempt the user's action when the asynchronous validation is complete.

Ideally, Ninja Forms would implement replace its synchronous queue of callbacks with a promise-based solution to allow for controllers to take their time

@mcaskill
mcaskill / acf-preview-changes-when-revisions-disabled.php
Created May 24, 2023 21:48
WP / ACF: Ensure ACF fields can be previewed when post revisions are disabled.
<?php
/**
* Ensure ACF fields can be previewed when post revisions are disabled
* (`WP_POST_REVISIONS`).
*
* @listens action:wp_revisions_to_keep
* Filters the number of revisions to save for the given post.
*/
add_filter( 'wp_revisions_to_keep', function ( int $num, WP_Post $post ) : int {
@mcaskill
mcaskill / acf-conditional-logic-elements.js
Created January 29, 2023 22:42
WordPress / ACF: Conditional logic for non-field elements
(function($, acf, undefined) {
/**#@+
* Adds support for conditional logic for non-field elements.
*
* Currently conditional logic can only be applied to elements
* within the `.acf-label` container.
*
* Copied from acf-input.js at v6.0.7:
*
@mcaskill
mcaskill / varexport.php
Last active December 22, 2022 05:22 — forked from stemar/varexport.php
PHP var_export() with short array syntax (square brackets) indented 2 spaces.
<?php
/**
* PHP var_export() with short array syntax (square brackets) indented 2 spaces.
*
* @todo Fix edge case where if a string value has `=>\n[` it will get converted to `=> [`.
*
* @link https://www.php.net/manual/en/function.var-export.php
*
* @param mixed $expression
* @param bool $return
@mcaskill
mcaskill / sphp.sh
Last active September 22, 2022 02:33 — forked from rhukster/sphp.sh
Easy Brew PHP version switching
#!/bin/bash
# Creator: Phil Cook
# Modified: Andy Miller (2020-11-27)
# Modified: Chauncey McAskill (2022-09-21)
osx_major_version=$(sw_vers -productVersion | cut -d. -f1)
osx_minor_version=$(sw_vers -productVersion | cut -d. -f2)
osx_patch_version=$(sw_vers -productVersion | cut -d. -f3)
osx_patch_version=${osx_patch_version:-0}
osx_version=$((${osx_major_version} * 10000 + ${osx_minor_version} * 100 + ${osx_patch_version}))
@mcaskill
mcaskill / composer.sh
Last active July 28, 2021 13:59
Better safe than sorry. A shell function wrapper for Composer to check for local changes and create back-ups before executing operations that may overwrite data.
#
# Alias of `composer` command that, if Composer's installed.json is present, checks
# for local changes in dependencies and backs-up the composer.json, composer.lock,
# and vendor directory before executing install, reinstall, or update commands.
#
# Useful if you often need to modify the code of your dependencies and they are
# installed from source.
#
# Version: 1.1.0
# License: MIT
@mcaskill
mcaskill / check-multiple-inputs.js
Last active September 19, 2020 16:43
JS : Toggle a range of checkboxes using the shift-key
(function () {
const table = document.getElementById('my-table');
let lastInput = null;
table.addEventListener('click', function (event) {
const currInput = event.target;
if (currInput.type !== 'checkbox') {
return;
}
@mcaskill
mcaskill / persist-checked-inputs.js
Created September 19, 2020 15:06
JS : Persist checked inputs in web storage
(function () {
const table = document.getElementById('my-table');
const store = window.localStorage;
table.querySelectorAll('[type="checkbox"]').forEach(function (input) {
input.checked = !!store.getItem(`${input.name}_${input.value}`);
});
table.addEventListener('change', function (event) {
let input = event.target;
@mcaskill
mcaskill / wp-ajax.php
Last active May 6, 2024 16:20 — forked from yllus/admin-ajax.php
WordPress : A drop-in replacement for admin-ajax.php that is optimized for visitor-only AJAX requests.
<?php
/**
* WordPress Ajax Process Execution
*
* This file is a near-identical copy of {@see wp-admin/admin-ajax.php} from WordPress v6.3.0.
*
* Differences:
* 1. Constant `WP_ADMIN` is set to FALSE, by default.
* 2. WordPress action 'admin_init' is replaced with custom action 'ajax_init'.
* 4. Custom actions hooked on 'admin_init' are not executed.