Skip to content

Instantly share code, notes, and snippets.

View gh-o-st's full-sized avatar
💀
The same thing we do every day, Pinky...

gh0st gh-o-st

💀
The same thing we do every day, Pinky...
  • Fort Smith, AR
  • 04:57 (UTC -05:00)
View GitHub Profile
@gh-o-st
gh-o-st / findNextPrev.js
Created October 13, 2021 01:05
Find the next/prev matching sibling using vanilla JS
const getPreviousMatch = (el, target) => {
// Get the next matching element
let match = el.previousElementSibling;
// If there's no selector, return the first element
if (!target) return match;
// If the element matches our selector, use it
// If not, jump to the next element and continue the loop
@gh-o-st
gh-o-st / array_key_exists_r.php
Last active July 26, 2021 06:56
Just a quick n' dirty recursive version of array_key_exists()
<?php
function array_key_exists_r($array, $keySearch) {
if (is_array($array)) {
foreach ($array as $key => $item) {
return ($key == $keySearch) ? true : (array_key_exists_r($item, $keySearch)) ? true : false;
}
}
return false;
}
const testGeolocation = function() {
if ('permissions' in navigator) {
navigator.permissions.query({name:'geolocation'}).then(function(response) {
if (response.state == 'prompt') {
// do stuff for permission request
} else if (response.state == 'denied') {
// do stuff for permission denied
} else {
// do stuff for permission granted
}
const paramExists = function(name) {
let params = window.location.search;
if (params.indexOf('?'+name) > -1 || params.indexOf('&'+name) > -1) {
return true;
}
return false;
};
@gh-o-st
gh-o-st / cookies.js
Last active November 11, 2020 17:51
const setCookie = function(name,value,days) {
let expires = "";
if (days) {
let date = new Date();
date.setDate(date.getDate() + days);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
};
@gh-o-st
gh-o-st / woo-order.php
Created November 5, 2019 06:50
Create pseudo-random WooCommerce order numbers
<?php
// Create pseudo-random order numbers while retaining
// the uniqueness of Woo's order number system
function change_woocommerce_order_number($order_id) {
// The prefix is prepended to the actual order number
// It consists of the month number (without leading zeros) followed by a hyphen
// and then followed by the current minutes (minus leading zeros)
$prefix = date('n').'-'.ltrim(date('i'), 0); // This is prepended to the order number
@gh-o-st
gh-o-st / array_search_recursive.php
Last active November 11, 2020 17:24
Recursive replacement for in_array() function
<?php
# Recursive replacement for in_array() function
function array_search_recursive($needle, $haystack) {
# Validate data types
if (is_array($needle) || !is_array($haystack)) {
return null;
}
@gh-o-st
gh-o-st / Request.php
Created October 20, 2014 15:53 — forked from rattrap/Request.php
POST with cURL but fallback to fopen
<?php
class Request {
/**
* do_post
* POST request
*
* @access public
* @param string $url - url
@gh-o-st
gh-o-st / rgb2hex.js
Last active November 11, 2020 17:46
Turns an RGB color to hex
const rgb2hex = function(r, g, b) {
return hexify(r).hexify(g).hexify(b);
};
const hexify = function(num) {
let hex = num.toString(16);
return hex.length == 1 ? "0" + hex : hex;
};
@gh-o-st
gh-o-st / getNISTbits.php
Last active August 29, 2015 14:03
Function to calculate the bits of information entropy as defined in the NIST Special Publication 800-63 with a modification to penalize repeated characters.
<?php
function getNISTbits($password, $rules=array()) {
if (empty($rules)) {
$rules['diminishing'] = true;
$rules['upper'] = 2;
$rules['lower'] = 2;
$rules['number'] = 1;
$rules['special'] = 1;