Skip to content

Instantly share code, notes, and snippets.

@krisatkinson
krisatkinson / gist:9b572c5f619fca1472d8
Last active August 29, 2015 14:07
round up to next accepted value
/** usage */
// list of values that we accept
$array = [2,4,8,16,32,64,128,256,512,1024,2048];
round_up(18,$array); // returns 32
round_up(2,$array); // returns 2
round_up(2049,$array); // returns false
// pass a number and an array, find the next highest number (kind of like the price is right, but backwards - "closest without going under")
function round_up($number,Array $array = []){
@krisatkinson
krisatkinson / gist:7814a29112a16b9649da
Last active March 12, 2024 19:45 — forked from lucasstark/gist:6594983
Example to add meta data to woocommerce cart items
<?php
/*
* Plugin Name: Example Modify Price
*/
class Example_Modify_Price {
private static $instance;
public static function register() {
if (self::$instance == null) {
var React = require('react');
var request = require('superagent');
var Header = require('../../common/components/header.jsx');
var Button = require('../../common/components/button.jsx');
var App = React.createClass({
getInitialState: function() {
return {
@krisatkinson
krisatkinson / LICENSE
Last active August 29, 2015 14:27 — forked from ourmaninamsterdam/LICENSE
Arrayzing - The JavaScript array cheatsheet
The MIT License (MIT)
Copyright (c) 2015 Justin Perry
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
function createRandomString(minLen, maxLen) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split(''),
stringLen = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen,
string = '';
for(var i = 0; i < stringLen; i++) {
string += alphabet[Math.floor(Math.random() * alphabet.length)];
}
return string;
}
@krisatkinson
krisatkinson / bettermysql-connect.php
Last active August 29, 2015 14:27 — forked from terrymun/bettermysql-connect.php
Connecting to the MySQL database using PDO
<?php
// Establish a new database connection
// Barebones edition
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
// Advanced edition
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8;port=3306', DB_USER, DB_PASS);
?>
@krisatkinson
krisatkinson / bettermysql-sanitize-input.php
Last active August 29, 2015 14:27 — forked from terrymun/bettermysql-sanitize-input.php
Sanitising user inputs for other variables
<?php
// Assuming that database connection is already open
// Let's say a user is allowed to provide a table name to query from
$tableName = trim($_GET['tableName']);
// This would NOT work (no good!)
$stmt = $db->prepare("SELECT user, id, email FROM :table WHERE id > 1000");
$stmt->bindParam(':table', $tableName);
$stmt->execute();
@krisatkinson
krisatkinson / ip_blacklist.lua
Created May 18, 2016 22:36 — forked from chrisboulton/ip_blacklist.lua
Redis based IP blacklist for Nginx (LUA)
-- a quick LUA access script for nginx to check IP addresses against an
-- `ip_blacklist` set in Redis, and if a match is found send a HTTP 403.
--
-- allows for a common blacklist to be shared between a bunch of nginx
-- web servers using a remote redis instance. lookups are cached for a
-- configurable period of time.
--
-- block an ip:
-- redis-cli SADD ip_blacklist 10.1.1.1
-- remove an ip:
@krisatkinson
krisatkinson / pasteboard.swift
Created September 26, 2016 23:17 — forked from kristopherjohnson/pasteboard.swift
Utility functions to copy/paste text
import Foundation
#if os(iOS)
import UIKit
#else
import AppKit
#endif
/// Return string value currently on clipboard
func getPasteboardContents() -> String? {
@krisatkinson
krisatkinson / Input.es5.js
Created November 21, 2018 14:51 — forked from royboy789/Input.es5.js
Learn Gutenberg es5 tutorial
var myComponents = myComponents || {};
var el = wp.element.createElement;
var __ = wp.i18n.__;
myComponents.inputComponent = function( id, attributes, changeCallback ) {
return el(
'input',
{
id: id + '-control',
value: attributes.who,