Skip to content

Instantly share code, notes, and snippets.

View itzikbenh's full-sized avatar

Isaac Ben Hutta itzikbenh

View GitHub Profile
@itzikbenh
itzikbenh / .block
Created June 3, 2017 13:13
fresh block
license: mit
@itzikbenh
itzikbenh / infinite-scroll.js
Last active June 14, 2017 14:41
This is related to this gist https://gist.github.com/itzikbenh/5cc5fb05b393a1516af70bc0fde3fa18 I'm experimenting with solutions to back button. Not ready yet
(function($) {
//Handles the infinite-scroll part and adds new content to sessionStorage so we can return user to the same position
//when he hits the back button
$(window).scroll(function() {
var url = $('.nav-links .next').attr('href');
if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 150) {
//To avoid repeating calls we set the paginate container to hold only text and we remove the links.
//that way url variable would be empty, thus if statement would return false and not continure to the get call.
$('.navigation').text("Fetching more posts..."); //You can optionally load an icon-loader or something.
$(".loader").removeClass("hidden"); //Show the loader icon
@itzikbenh
itzikbenh / projectController.php
Last active June 18, 2017 00:47
Laravel - project update with revision
<?php
public function update(Request $request, $id)
{
$this->validate($request, [
'number' => 'required',
'client_id' => 'required',
'location_id' => 'required',
'priority_id' => 'required',
'status_id' => 'required',
@itzikbenh
itzikbenh / errors.php
Last active July 31, 2017 13:00
WordPress simple validator class. Still in the work
<?php if( isset( $_SESSION['errors'] ) ): ?>
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
<ul>
<?php foreach ($_SESSION["errors"] as $error): ?>
<li><?php echo $error; ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif; ?>
@itzikbenh
itzikbenh / functions.php
Created July 31, 2017 22:00
WordPress - show current user attachments
<?php
//Besides administrator show only the current user media files
function show_current_user_attachments( $query ) {
if( ! current_user_can( 'manage_options' ) ) {
$user_id = get_current_user_id();
if ( $user_id ) {
$query['author'] = $user_id;
}
return $query;
@itzikbenh
itzikbenh / app.js
Created August 1, 2017 18:09
WordPress - Insert file into tinymce in the frontend via the REST API
let $body = $('body');
let $error_field = $('.file-error');
function hasExtension(file_name, exts) {
return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$', "i")).test(file_name);
}
function fileSizeInMB(file_size) {
return (file_size / (1024*1024)).toFixed(2);
@itzikbenh
itzikbenh / example.md
Created August 2, 2017 01:11
Stripe basic class

NOTE

Always handle errors when you use stripe functions. You don't want to make mistakes when it comes to people's money.

Create a customer

$stripe_client = new Stripe_Client( $user, $stripe_secret, $token );

try {
  $customer = $stripe_client->create_customer();
} catch (Exception $e) {
@itzikbenh
itzikbenh / cron-commands
Created September 8, 2017 00:49
Backup cron
0 * * * * /usr/bin/env bash /root/backup/db-backup.sh &>> /root/backup/backup.log
0 0 * * * /usr/bin/env bash /root/backup/uploads-backup.sh &>> /root/backup/backup.log
@itzikbenh
itzikbenh / company.js
Last active October 11, 2017 12:08
WP repeater field
function generate_key_person_fields(newId) {
return `
<div class="key-person-container">
<div class="form-group-admin-inline">
<label>Name</label><br>
<input type="text" name="key_person[name][${newId}][]" value="" required>
</div>
<div class="form-group-admin-inline">
<label>Title</label><br>
@itzikbenh
itzikbenh / app.js
Created October 11, 2017 17:31
Picky class
class Picky {
constructor(element) {
let self = this;
this.$el = element;
this.select = this.$el.querySelector('select');
this.dropdown = this.$el.querySelector('ul');
this.input = this.$el.querySelector('input');
this._open = false;