Skip to content

Instantly share code, notes, and snippets.

View baras's full-sized avatar

Yoav Baras baras

View GitHub Profile
@baras
baras / download.php
Last active October 20, 2021 07:00 — forked from brasofilo/download.php
Force File Download with PHP
<?php
/*
* Force File Download.
* Usage: http://example.com/download.php?file=./uploads/image.jpg
*
* There are a couple of *ninja* exit() as security guarantee, adapt as necessary.
*
*/
// Grab the requested file's name.
@baras
baras / set-input-direction.js
Last active July 27, 2021 12:26
Set input / textarea direction to rtl if the field contains RTL characters.
$(document).on('input', 'input, textarea', function () {
var $el = $(this),
val = $el.val();
if (val.length) {
$el.css({direction: isRTL(val) ? 'rtl' : 'ltr'});
} else {
$el.css({direction: ""});
}
});
@baras
baras / smallest-positive-int-solution.php
Last active September 23, 2020 01:28
Given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
/**
* Finds the smallest positive integer which does not exist in an array.
*
* @param array $a
* The array to check.
*
* @return int
* The smallest positive integer not found in $a.
*/
function solution2(array $a) {
@baras
baras / link
Created July 1, 2020 17:34
Scrolling to an anchor given fixed position navbar
@baras
baras / get_custom_post_types_and_page_templates
Created January 16, 2019 08:44
Get custom post types and page templates in php
function get_custom_post_types_and_page_templates() {
global $wp_post_types;
// Public custom post types.
$args = [
'public' => true,
'_builtin' => false,
];
// Get the lists and add a suffix which denotes the type, Page or Post.
@baras
baras / qstring.js
Last active December 24, 2018 06:59
Get the URL query string parameters in JavaScript.
var queryString = {};
location.search.substr(1).split("&").forEach(function (pair) {
if (pair.length) {
var parts = pair.split("=");
queryString[parts[0]] = decodeURIComponent(parts[1].replace(/\+/g, " "));
}
});
console.log(queryString);
@baras
baras / decode_url.js
Created December 16, 2018 14:32
Decode a URL in the browser address bar.
if (history.pushState) { //IE10+
var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + decodeURIComponent(window.location.search);
window.history.pushState({path:newurl},'',newurl);
}
((general).*?(override))
/**
* Wrap each letter of text in the given element in a span tag.
*/
$.fn.lettering = function () {
this.each(function (index) {
var elem = $(this),
characters = elem.text().split("");
elem.empty();
<div class="c-forms w50">
<div class="row no-gutters">
<div class="col-md-6 col-xl-4 pad-by-location">
<div>
<label> Full Name* [text* full_name placeholder "Full Name"] </label>
</div>
</div>
<div class="col-md-6 col-xl-4 pad-by-location">
<div>
<label> Company Name* [text* company_name placeholder "Company Name"] </label>