Skip to content

Instantly share code, notes, and snippets.

View mcaskill's full-sized avatar
🥃

Chauncey McAskill mcaskill

🥃
View GitHub Profile
@mcaskill
mcaskill / Function.Date-Format-Conversion.php
Last active February 6, 2024 11:22
PHP : Translate date/time format between `date()` and `strftime()`
<?php
/**
* Convert date/time format between `date()` and `strftime()`
*
* Timezone conversion is done for Unix. Windows users must exchange %z and %Z.
*
* Unsupported date formats : S, n, t, L, B, G, u, e, I, P, Z, c, r
* Unsupported strftime formats : %U, %W, %C, %g, %r, %R, %T, %X, %c, %D, %F, %x
*
@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 / Function.HTML-Build-Attributes.php
Last active January 8, 2024 04:14
PHP / WordPress : Generate a string of HTML attributes
<?php
if (!function_exists('html_build_attributes')) {
/**
* Generate a string of HTML attributes
*
* @param array $attr Associative array of attribute names and values.
* @param callable|null $callback Callback function to escape values for HTML attributes.
* Defaults to `htmlspecialchars()`.
* @return string Returns a string of HTML attributes.
@mcaskill
mcaskill / Function.Array-Group-By.php
Last active January 3, 2024 10:15
PHP : Groups an array by a given key
<?php
if (!function_exists('array_group_by')) {
/**
* Groups an array by a given key.
*
* Groups an array into arrays by a given key, or set of keys, shared between all array members.
*
* Based on {@author Jake Zatecky}'s {@link https://github.com/jakezatecky/array_group_by array_group_by()} function.
* This variant allows $key to be closures.
@mcaskill
mcaskill / wp-ajax.php
Last active December 4, 2023 11:21 — 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.
@mcaskill
mcaskill / Function.Array-Chunk-By.php
Last active October 25, 2023 17:38
PHP : Splits an array into chunks using a callback function.
<?php
if (!function_exists('array_chunk_by')) {
/**
* Splits an array into chunks using a callback function.
*
* Chunks an array into arrays by iteratively applying the $callback function
* to the elements of the $array.
*
* @see https://rlaanemets.com/post/show/group-array-by-adjacent-elements-in-javascript
@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-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