Skip to content

Instantly share code, notes, and snippets.

Avatar
🥃

Chauncey McAskill mcaskill

🥃
View GitHub Profile
@mcaskill
mcaskill / check-multiple-inputs.js
Last active Sep 19, 2020
JS : Toggle a range of checkboxes using the shift-key
View check-multiple-inputs.js
(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 Sep 19, 2020
JS : Persist checked inputs in web storage
View persist-checked-inputs.js
(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 17, 2020 — forked from yllus/admin-ajax.php
WordPress : A drop-in replacement for admin-ajax.php that is optimized for visitor-only AJAX requests.
View wp-ajax.php
<?php
/**
* WordPress Ajax Process Execution
*
* This file is a near-identical copy of {@see wp-admin/admin-ajax.php} from WordPress v5.4.1.
*
* 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 / charcoal-collection-loader-model-caching.php
Created Jul 3, 2019
Charcoal : How to cache object data from a `CollectionLoader` into a `ModelLoader` cache namespace.
View charcoal-collection-loader-model-caching.php
<?php
use Pimple\Container;
use Charcoal\Model\ModelInterface;
use Charcoal\Model\CollectionInterface;
use App\Post;
$container = new Container();
$container['cache'] = function (Container $container) {
@mcaskill
mcaskill / Function.Blade-Props.php
Created Apr 28, 2019
Blade : Keep partials reusable with two presenter-style functions.
View Function.Blade-Props.php
<?php
/**
* Convert array to an object recursively, a new instances of the stdClass.
*
* @param array $props The array to convert into an object.
* @return object
*/
function props(array $props) : object
{
@mcaskill
mcaskill / wp-without-hooks.php
Last active Jan 18, 2019 — forked from westonruter/01-common-example.php
WordPress \ Hooks : Temporarily disable hooks while executing a given callback.
View wp-without-hooks.php
<?php
/**
* Call the callback with one or all handlers disabled for the given action or filter.
*
* Temporarily disables the specified hook, or all hooks, from a specified filter or action
* before calling $callback.
*
* @link https://gist.github.com/westonruter/6647252
*
@mcaskill
mcaskill / wp-add-hooks.php
Created Jan 18, 2019
WordPress \ Hooks : Hook a function or method to an array of actions or filters.
View wp-add-hooks.php
<?php
/**
* Hook a function or method to an array of actions.
*
* @param string[] $tags Array of names of filters to hook the $function callback to.
* @param callable $function_to_add The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed. Default 10.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
@mcaskill
mcaskill / Function.Array-Chunk-By.php
Last active Nov 13, 2020
PHP : Splits an array into chunks using a callback function.
View Function.Array-Chunk-By.php
<?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 / elogram.php
Created Jun 26, 2017
Elogram Quickstart
View elogram.php
<?php
use Larabros\Elogram\Client;
header('Content-Type: application/json;charset=utf-8');
/** Register the Composer autoloader */
require dirname(__DIR__) . '/vendor/autoload.php';
$clientId = '';
@mcaskill
mcaskill / String.prototype.explode.js
Created Apr 25, 2017
JS : Split a string by string
View String.prototype.explode.js
if (!String.prototype.explode) {
/**
* Split a string by string
*
* Splits a String object into an array of substrings, each of which is formed
* by splitting it on boundaries formed by the string delimiter.
*
* @link http://locutus.io/php/explode/ Created by Kevin van Zonneveld (http://kvz.io)
* @example
* 'Kevin van Zonneveld'.explode(' ') // [ 'Kevin', 'van', 'Zonneveld' ]