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 / 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 / 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 / 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 / getAllPostMeta.php
Created April 8, 2019 06:21
WordPress - Return all post meta values in flat array
<?php
// Return all meta for a single post in a flat array.
function get_all_post_meta($post_id) {
// Get all the meta values.
$meta = get_post_meta($post_id, '');
// We only need the first item - return a flat array.
return array_map(function($item) {
return $item[0];
}, $meta);
@adamcrampton
adamcrampton / dd.php
Created April 9, 2019 01:20 — forked from james2doyle/dd.php
A implementation of "dump and die" (dd) for WordPress
<?php
if (!function_exists('dd')) {
function dd($data)
{
ini_set("highlight.comment", "#969896; font-style: italic");
ini_set("highlight.default", "#FFFFFF");
ini_set("highlight.html", "#D16568");
ini_set("highlight.keyword", "#7FA3BC; font-weight: bold");
ini_set("highlight.string", "#F2C47E");
@adamcrampton
adamcrampton / trimArrayKeys.php
Created April 9, 2019 01:24
Trims array keys specified in another array
function trimArrayKeys($originalArray) {
$strip_keys = ['banned_key'];
return array_diff_key($originalArray, array_flip($strip_keys));
}
@adamcrampton
adamcrampton / default
Created April 9, 2019 23:54
Nginx - sites-available default file - Ubuntu 18.04 + PHP 7.2
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
@adamcrampton
adamcrampton / laravel
Created April 9, 2019 23:56
Nginx - sites-available Laravel 5.x vhost example file - Ubuntu 18.04 + PHP 7.2
server {
listen 80;
listen [::]:80 ipv6only=on;
# Log files for Debugging
access_log /var/log/nginx/laravel-access.log;
error_log /var/log/nginx/laravel-error.log;
# Webroot Directory for Laravel project
root /var/www/laravel/public;
@adamcrampton
adamcrampton / lambdaApiBoilerplate.js
Created April 23, 2019 04:37
Boilerplate for Lambda function envoked from AWS API Gatewat
'use strict';
console.log('Getting JSON...');
exports.handler = async (event) => {
let templateId = 1;
let responseCode = 200;
console.log('request:' + JSON.stringify(event));
if (event.queryStringParameters && event.queryStringParameters.templateId) {
@adamcrampton
adamcrampton / sidebarBanner.php
Created May 3, 2019 00:55
WordPress widget - linked image for sidebar
<?php
/**
* Steps:
* 1. Pop this in your theme's functions.php file
* 2. See the other gist for JS instructions
* 3. Profit
*/
// Register widget area.
function sidebar_widgets_init() {