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 / laravelCsvParseAndUpsert.php
Last active January 18, 2024 03:17
Laravel - Parse a CSV file, map to database columns and upsert
<?php
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
final class CsvService {
/**
* CSV data from file into an array using the provided mappings, then insert to database.
*
@adamcrampton
adamcrampton / Handler.php
Last active November 4, 2023 07:55
Custom error handling redirects for Laravel (App\Exceptions\Handler)
<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Throwable;
class Handler extends ExceptionHandler
@adamcrampton
adamcrampton / bootstrap-confirm-modal.vue
Created April 23, 2021 00:24
Bootstrap 4 confirm modal for VueJs 2
<template>
<div class="modal" id="confirmModal" tabindex="-1" aria-labelledby="confirmModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialogue-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirmModalLabel"><slot name="modal-header"></slot></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
@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 / loading.html
Created August 11, 2020 22:32
Loading overlay with spinner
<style>
#overlay {
background: #000;
color: #fff;
position: fixed;
height: 100%;
width: 100%;
z-index: 5000;
top: 0;
left: 0;
@adamcrampton
adamcrampton / youTubeDurationParser.php
Created June 22, 2020 23:01
Parse YouTube duration string to seconds integer
<?php
/**
* Match pattern PT#H#M#S (leading PT, hours, minutes, seconds) and return seconds value.
*
* @param string $string
* @return int
*/
private static function convertDuration($string)
{
// Comprehensive regex that should match any value.
@adamcrampton
adamcrampton / JsonCheck.php
Created May 26, 2020 06:50
Determine if PHP string is valid JSON
<?php
/**
* Checks if string is valid JSON.
*
* @param string $string
* @return bool
*/
public function jsonCheck($string)
{
@adamcrampton
adamcrampton / ajaxFullPageSpinner.html
Created May 7, 2020 23:59
Full page CSS spinner useful for pages with AJAX
<div class="loading hidden"><i class="fa fa-spin fa-circle-o-notch" style="font-size: 48px;"></i></div>
<script type="text/javascript">
const formData = new FormData();
formData.append('key', 'value');
$.ajax({
async: true,
contentType: false,
processData: false,
@adamcrampton
adamcrampton / jqueryAjaxPostBoilerplate.js
Last active March 13, 2020 05:10
Boilerplate for jQuery AJAX POST request
const endpoint = 'https://api.some.endpoint.test';
const formId = 'this-form-id-value';
const data = new FormData(document.getElementById('formId'));
$.ajax({
async: true,
contentType: false,
processData: false,
url: endpoint,
method: 'POST',
@adamcrampton
adamcrampton / ttlArray.php
Created September 30, 2019 02:28
Handy array useful for configuring cache TTLs etc
<?php
/*
|--------------------------------------------------------------------------
| Cache TTL
|--------------------------------------------------------------------------
|
| Set commonly used TTL values.
|
*/
return [