Skip to content

Instantly share code, notes, and snippets.

View Adam-Mould's full-sized avatar

Adam Mould Adam-Mould

View GitHub Profile
const Analytics = {};
// Pass n arguments using rest parameter
Analytics.trigger = (...params) => {
// console.log(typeof params); // object (Array)
// Spread parameter into CoreMetrics tag
cmCreateElementTag(...params);
};
@Adam-Mould
Adam-Mould / example.html
Created March 20, 2018 08:58
Basic Lazy Load Images
<img data-src="http://placehold.it/300x300" alt="Example image">
Array.from(document.querySelectorAll('[id]'))
.map(domNode => domNode.id)
.filter((id, index, self) => id.length > 0 && self.indexOf(id) !== index)
.forEach(id => console.warn(`Multiple IDs #${id}`));
const events = (function () {
const topics = {};
return {
subscribe(topic, listener) {
// Create topics object if not yet created
if (!topics.hasOwnProperty.call(topics, topic)) topics[topic] = [];
// Add listener to the queue
const index = topics[topic].push(listener) - 1;
@Adam-Mould
Adam-Mould / lazyLoad.js
Created July 30, 2018 08:00
Lazy Load Images using IntersectionObserver
function imageLazyLoad() {
const images = Array.from(document.querySelectorAll('img[data-src]'));
if (images.length) {
if ('IntersectionObserver' in window) {
setupIntersectionObserver(images);
} else {
loadImages(images);
}
}
function apply_global_coupon() {
global $woocommerce;
$all_coupons = get_all_store_coupons();
$coupon_code = 'Global';
if ( in_array( $coupon_code, $all_coupons) ) {
if ( WC()->cart->has_discount( $coupon_code ) ) return;
<?php
/* Developer Config */
// Environment
define( 'DEV_MODE', true );
// Debug (based upon dev mode)
define( 'WP_DEBUG', DEV_MODE );
define( 'WP_DEBUG_LOG', DEV_MODE );
@Adam-Mould
Adam-Mould / next-drupal-app-router.tsx
Created November 29, 2023 09:35
Using next-drupal in Next.js 14 app router
import { DrupalJsonApiParams } from "drupal-jsonapi-params";
import { drupal, getResourceByParams } from "@/lib/drupal";
const RESOURCE_TYPE = "node--landing_page";
export default async function Page({ params }: { params: { slug: string[] } }) {
// Fetch a resource by params
const resource = await getResourceByParams(params);