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 / eloquentWhereHas.php
Created November 29, 2018 23:01
Laravel Eloquent whereHas query example
class TaxonomyEntity extends Model
{
/**
* Get a list of categories with optional limit and order parameters.
* @param integer $limit
* @param string $order
* @return @return\Illuminate\Database\Eloquent\Collection
*/
public function getCategories($limit = 5, $order = 'DESC')
{
@adamcrampton
adamcrampton / lambdaDeploy.sh
Created November 29, 2018 23:25
Shell script for deploying Lambda functions using aws-cli
if [ "$#" -ne 1 ]; then
echo "Usage : ./build.sh lambdaName";
exit 1;
fi
lambda=${1%/}; // # Removes trailing slashes
echo "Deploying $lambda";
cd $lambda;
if [ $? -eq 0 ]; then
echo "...."
@adamcrampton
adamcrampton / alexaNodeLambdaBoilerplate.js
Created December 3, 2018 04:30
Simple boilerplate for Lambda function using the Alexa SDK for Node JS
// Alexa Demo
// ==========
'use strict';
// Config Alexa SDK and required libraries.
const Alexa = require('alexa-sdk');
const request = require('request-promise');
// Define handlers.
const handlers = {
@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);
@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 / 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 / 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))
@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 / 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 / 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;
}