How to include a web page thumbnail in a WordPress post

In my article about My take on a Vending Machine Simulation, I wanted to include a link to my Gist, together with a nice thumbnail. Having installed Nzymes on my blog, that wasn’t too difficult.

I thought about using an injection like this:

´{[ =<URL of the file>= | 1.thumb(1) ]}´

so that I could easily reuse it just by changing the URL.

Here are the contents of the execution enzyme.

thumb

list( $url, $size, $target ) = array_pad( $arguments, 3, null );
if (! $size) $size = 'x';
if (! $target) $target = '_blank';

$src = "http://free.pagepeeker.com/v2/thumbs.php?size=$size&url=" . urlencode($url);
$img = "<img src='$src' />";
$result = "<a href='$url' target='$target'>$img</a>";
return $result;

The idea is to rely on the PagePeeker free service and return a properly built A IMG element.

How to include code from GitHub in a WordPress post

In my article about How to setup a Multi Page Application in AngularJS, I wanted to include code from GitHub. Having installed Nzymes on my blog, that wasn’t too difficult.

I thought about using an injection like this:

´{[ =<URL of the file>= | 1.get-url(1) | =javascript= | 1.hl(2) ]}´

so that I could easily reuse it just by changing the URL.

Here are the contents of the execution enzymes.

get-url

list($url) = $arguments;
$pieces = preg_split('@/@', $url, -1, PREG_SPLIT_NO_EMPTY);
$protocol = array_shift($pieces);
$file = implode('/', $pieces);

$upload_dir = wp_upload_dir();
$filename = "{$upload_dir['basedir']}/$file";
if (! file_exists($filename)) {
  $dirname = dirname($filename);
  if (! file_exists($dirname)) {
    wp_mkdir_p($dirname);
  }
  $data = file_get_contents($url);
  file_put_contents($filename, $data);
}
$data = file_get_contents($filename);
return $data;

The idea is to download the remote file once and store it on the server, in a directory structure like the url (except the protocol). Thus, at any later time, the local copy is returned.

hl

list( $code, $language, $numbers ) = array_pad( $arguments, 3, null );

$pre_class = '';
$numbers   = trim( $numbers );
if ( $numbers ) {
    $pre_class = ' class="' . $numbers . '"';
}

$code     = str_replace( '{' . '[', '{' . '-[', $code );  // escape Enzymes 3 injections just in case
$code     = htmlspecialchars( $code );

$code_class = '';
$language   = trim( $language );
if ( $language ) {
    $code_class = ' class="' . $language . ($language == 'ruby' ? ' hljs' : '') . '"';
    $template = '<pre%s><code%s>%s</code></pre>';
    $args = array($template, $pre_class, $code_class, $code);
} else {
    $template = '<pre%s>%s</pre>';
    $args = array($template, $pre_class, $code);
}

$result   = call_user_func_array( 'sprintf', $args );
return $result;

The idea is to wrap the escaped code into a PRE CODE element, prepared for Chili to later highlight it in the browser. Fun fact: above hl highlights itself:

´{[ 1.hl | =php= | 1.hl(2) ]}´.

PHPUnit and Debugging

Lately I’ve been using this simple setup to print stuff to the terminal during PHPUnit runs.

    /**
     * @var bool
     */
    public $debug_on = false;

    /**
     * @param mixed $something
     */
    public
    function debug_print( $something ) {
        if ( ! $this->debug_on ) {
            return;
        }
        fwrite( STDERR, "n" . print_r( $something, true ) . "n" );
    }

Use Case 1

Control debug printing by means of opening and closing “parentheses”:

$tested->debug_on = true;
//...
$tested->debug_print( $stuff );
//...
$tested->debug_on = false;

Notice that you can also control (everything or something) either from the testing or the tested code, the only difference being the use of ´$this´ instead of ´$tested´.

Use Case 2

Control debug printing by means of an automatic toggle like this one:

static $count = 0;
$this->debug_on = !$count++;
//...
$this->debug_print( $stuff );

Same considerations as above.