Skip to content

Instantly share code, notes, and snippets.

View adamcrampton's full-sized avatar
💪
Crushing it

Adam Crampton adamcrampton

💪
Crushing it
  • Sydney, Australia
View GitHub Profile
@adamcrampton
adamcrampton / cookieMatch.js
Created April 1, 2019 05:15
For use with GTM variable - checks for existence and value of cookie - returns null if not found
// Return either the value of the Adobe Audience Manage cookie or null
// Note the AAM cookie is set by a Cloudflare worker at the edge
function() {
return (document.cookie.match(/^(?:.*;)?\s*aam\s*=\s*([^;]+)(?:.*)?$/)||[,null])[1]
}
@adamcrampton
adamcrampton / detectRegionAndSetCookie.js
Created April 1, 2019 05:14
For use with Cloudflare Worker - detects region and sets a cookie
// For use with a Cloudflare Worker
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
});
async function fetchAndApply(request) {
// Only run if cookie not present.
let cookies = request.headers.get('Cookie') || "";
if (cookies.includes("aam=true")) {
@adamcrampton
adamcrampton / nonceMaker.js
Created March 28, 2019 01:04
Created a numbered nonce - you can also add letters to the "possible" variable if you require an alpha numeric string
function makeNonce(length) {
let nonce = "";
let possible = "0123456789";
for (let i = 0; i < length; i++) {
nonce += possible.charAt(Math.floor(Math.random() * possible.length));
}
return nonce;
}
@adamcrampton
adamcrampton / arrayShuffler.js
Created February 18, 2019 03:02
Randomize a JS object using Fisher-Yates-Durstenfeld shuffle algorithm
// Fisher-Yates-Durstenfeld shuffle
// https://stackoverflow.com/questions/3718282/javascript-shuffling-objects-inside-an-object-randomize
function shuffle(sourceArray) {
for (var i = 0; i < sourceArray.length - 1; i++) {
var j = i + Math.floor(Math.random() * (sourceArray.length - i));
var temp = sourceArray[j];
sourceArray[j] = sourceArray[i];
sourceArray[i] = temp;
}
@adamcrampton
adamcrampton / WPCountByTaxonomyTypeAndGroup.sql
Created February 6, 2019 00:24
Count all posts in WordPress database by tag or category, and output daily totals
SELECT p.post_date, COUNT(*) FROM wp_posts p
JOIN wp_term_relationships tr
ON p.ID = tr.object_id
WHERE tr.term_taxonomy_id = 15996
AND p.post_status = "publish"
AND p.post_type = "post"
AND p.post_date > "2017-02-06"
GROUP BY YEAR(p.post_date), MONTH(p.post_date), DAY(p.post_date)
ORDER BY p.post_date ASC
@adamcrampton
adamcrampton / getRedirectedUrl.php
Last active February 5, 2019 23:10
Return the forwarded URL using cURL - useful for link shorteners etc
<?php
function returnRedirectedUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
<style>
/* BASE RULES */
html, body {
width: 100%;
height: 100%;
background-color: #ccccff;
}
div {
@adamcrampton
adamcrampton / flattenPDOArray.php
Created January 8, 2019 00:07
Flatten a multidimensional PDO array into an indexed array when fetching a single column
/**
* Flatten array of PDO results into a single dimension.
* Note: This is only useful for a PDO query returning a single column (you probably don't want to do this otherwise).
*
* @param array $arrayToFlatten
* @return array
*/
public function flattenArray($arrayToFlatten, $key)
{
// Set up array.
@adamcrampton
adamcrampton / appendToArrayWithinLoop.js
Created December 4, 2018 00:55
Append multiple properties to array, using iteration counter as key within a for loop
// Pretend someObject is already defined and populated
const someData = {};
for (var i = 0; i < someObject.length; i++) {
  // Add object data to array.
someData[i] = {};
someData[i].itemName = someObject[i].itemName;
someData[i].anotherName = someObject[i].anotherName;
}
@adamcrampton
adamcrampton / headScriptTagLoad.js
Created December 3, 2018 23:58
Dynamically append a script tag to the head
var s = document.createElement('script');
var scriptPath = 'https://www.someurl.com/example.js';
s.setAttribute('src', scriptPath);
document.getElementsByTagName('head')[0].appendChild(s);