Skip to content

Instantly share code, notes, and snippets.

View tzkmx's full-sized avatar
🎉
estrenando repositorios privados

Jesus Franco tzkmx

🎉
estrenando repositorios privados
View GitHub Profile
@tzkmx
tzkmx / test.php
Last active November 27, 2015 00:55
Test Symfony Events, modify event with Reflection
<?php
require 'vendor/autoload.php';
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\EventDispatcher\Event;
class EventSubscriber implements EventSubscriberInterface
@tzkmx
tzkmx / app.php
Created December 16, 2015 19:20
Silex password MD5 encoding service
<?php
use Silex\Application;
use Silex\SecurityServiceProvider;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
$app = new Application();
$app->register(new SecurityServiceProvider(), array(
'security.encoder.digest' => $app->share(function () use ($app) {
return new MessageDigestPasswordEncoder('md5', false, 1);
})
@tzkmx
tzkmx / getFirstIPv4.bat
Last active May 20, 2016 22:30
Loops to get first IPv4 address in CMD && then reset ini file to result
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET _c=0
FOR /F "delims=: tokens=1,2" %%A IN ('ipconfig') DO (
FOR /F "tokens=2" %%C IN ("%%A") DO (
IF !_c! EQU 0 IF /I %%C==IPv4. ( echo %%B & SET /A _c+=1 )
)
)
@tzkmx
tzkmx / grommet_hello_world.html
Created May 31, 2016 23:05 — forked from grommetux/grommet_hello_world.html
Grommet Hello World app
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400italic,400,700' rel='stylesheet' type='text/css'>
<link href='http://grommet.io/assets/latest/css/grommet.min.css' rel='stylesheet' type='text/css'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.2/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
@tzkmx
tzkmx / ControllerResolver.php
Created June 7, 2016 14:31
Inject services from Pimple Container in Controller arguments
<?php
namespace Controller;
use Silex\ControllerResolver as SilexControllerResolver;
use Symfony\Component\HttpFoundation\Request;
use \Pimple;
/**
* Extension of Silex\ControllerResolver, as that injects Application class if
* its requested by Controller by Typehinting; this looks for, and injects,
* services with same name as parameter argument and validates is the right
@tzkmx
tzkmx / insert-posts.php
Created June 16, 2016 23:53
WordPress: Insert a post into WordPress from an external script
<?php
// Load WordPress
require_once 'path/to/www/wp-load.php';
require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
// Set the timezone so times are calculated correctly
date_default_timezone_set('Europe/London');
// Create post
@tzkmx
tzkmx / iframe_shortcode.php
Created June 23, 2016 22:45
Minimal shortcode to override WordPress virtual ban of iframes
<?php
function iframe_render_shortcode($atts) {
$keys = array_flip(array_keys($atts));
$params = shortcode_atts($keys, $atts);
$out = [];
foreach($params as $key => $val) {
$out[] = $key . '="' . $val . '"';
}
return '<iframe ' . implode(' ', $out) . '></iframe>';
}
@tzkmx
tzkmx / get_thumbnail_to_category_rest.php
Created July 21, 2016 00:15
Add thumbnail field to WordPress category via REST API
<?php
add_action('rest_api_init', 'register_my_rest_field');
/**
* It depends on term_meta key _thumbnail_id using the metadata API
* for terms. Only plugin I know uses this is Taxonomy Thumbnail:
* https://wordpress.org/plugins/sf-taxonomy-thumbnail/ instead of
* storing metadata for terms in wp_options or custom tables
*/
function register_my_rest_field()
{
@tzkmx
tzkmx / ThemeClass.php
Created July 25, 2016 18:06
Towards a more testable WordPress. The main idea is simple. Instead of piling everything into a theme's functions.php file, create a class to hold all the theme's functions. Then, using dependency injection, add the WordPress facade and inside the ThemeClass make all calls to the facade instead of the global WP functions. This allow us to use Mo…
<?php
/**
* This is a straightforward example of what a ThemeClass may look like. It contains all the
* expected initialization, and wp_enqueue_script calls.
*/
class ThemeClass
{
protected $wp;
protected $themeDirectory;
@tzkmx
tzkmx / demo.js
Created August 5, 2016 19:33
Demo of tail call optimization in javascript with metaret.js
function fact(i) {
return i === 1 ? 1 : i * fact(i-1);
}
console.time('fact recursive naive');
for(var i = 0; i<1000000 ; i++) {
var g=fact(170);
}
console.log(g);
console.timeEnd('fact recursive naive');
metaparse("metafun fact_m(self, i, acc) { " +