Get Distance Between Two Points

from math import sqrt

def get_distance(origin, destination):
    '''Distance Between Two Points'''
    (o_x, o_y) = origin
    (d_x, d_y) = destination
    return sqrt((d_x - o_x)**2.0 + (d_y - o_y)**2.0)

Synchronize $remote and $local Files

function sync_files( $remote, $local )
{
    if ( ! is_file( $local ) ) { return copy( $remote, $local ); }

    $handle = fopen( $remote, 'r' );
    if ( ! $handle ) { die( "Could not open {$remote}." ); }
    $meta = stream_get_meta_data( $handle );
    fclose( $handle );

    foreach( $meta['wrapper_data'] as $response )
    {
        // Redirection
        if ( substr( strtolower( $response ), 0, 10 ) === 'location: ' ) {
            return sync_files( substr( $response, 10 ), $local );
        }

        // Compare sizes
        if ( substr( strtolower( $response ), 0, 16 ) === 'content-length: ' )
        {
            if ( (int) filesize( $local ) !== (int) substr( $response, 16 ) ) {
                return copy( $remote, $local );
            }
            continue;
        }

        // Compare dates
        if ( substr( strtolower( $response ), 0, 15 ) === 'last-modified: ' )
        {
            if ( (int) filemtime( $local ) < (int) strtotime( substr( $response, 15 ) ) ) {
                return copy( $remote, $local );
            }
            continue;
        }
    }

    return false;
}

Remove Nodes (Comments, Script Tags) from $html String

function remove_nodes( $html, array $selectors = array( '//comment()', '//script' ) )
{
    $dom = new DOMDocument;
    $dom->loadHtml( strval( $html ) );
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $xpath = new DOMXPath( $dom );
    foreach( $selectors as $selector ) {
        while ( $node = $xpath->query( $selector )->item( 0 ) ) {
            // Remove selected tag from html string
            $node->parentNode->removeChild( $node );
        }
    }
    return $dom->saveHTML();
}

Sanitize Key : Lowercase Accentless $string, Special Characters $replacement

function sanitize_key( $string, $replacement = '_' )
{
    // Lowercase and remove accents
    $string = htmlentities( trim( strtolower( strval( $string ) ) ), ENT_NOQUOTES );
    $string = preg_replace( '/&([a-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);/', '$1', $string );
    $string = preg_replace( '/&([a-z]{2})(?:lig);/', '$1', $string );
    $string = preg_replace( '/&[^;]+;/', $replacement, $string );
    // Replace non-alphanumeric characters
    return preg_replace( '/[^a-z0-9]+/', $replacement, $string );
}

Sanitize $value According to its $type

function sanitize( $value, $type = null )
{
    // Recursive sanitation for arrays and objects
    if ( is_array( $value ) || is_object( $value ) ) {
        $output = array();
        foreach ( (array) $value as $k => $v ) {
            $k = sanitize_key( $k );
            $output[$k] = sanitize( $v, $type );
        }
        return $output;
    }

    // Default to value's actual type
    if ( ! $type ) { $type = gettype( $value ); }

    switch( strtolower( $type ) )
    {
        case 'boolean':
            return boolval( $value );
        case 'integer':
        case 'number':
            return intval( $value );
        case 'double':
        case 'float':
            return floatval( $value );
        case 'null':
            return null;
        case 'resource':
            return $value;
        case 'url':
            return filter_var( rtrim( $value, '/' ), FILTER_SANITIZE_URL );
        case 'email':
            return filter_var( substr( $value, 0, 254 ), FILTER_SANITIZE_EMAIL );
        default:
            // String
            return filter_var( $value, FILTER_SANITIZE_STRING );
    }
}

Open files or folders in Atom from OSX Finder

Mac computers come with the Automator application.

Open it.

Create a new “Service” : File > New > Service.

In the library of actions on the left sidebar, you will find one called Run Shell Script : add it to the workflow.

Set the service to receive selected files or folders, the shell as /bin/zsh, and the input to be passed as arguments.

Then enter /Applications/Atom.app/Contents/MacOS/Atom -n $@ in the text area.

Finally, save the workflow as “Open in Atom”.

And you are done !

Sass Compiler for WordPress

It uses the scssphp Compiler.

Installation

  1. Download the latest release
  2. Unzip it into your wp-content/plugins directory
  3. Activate the plugin in WordPress

Compiler

This first page allows you to write and compile Sass. The resulting stylesheet is automatically enqueued.

Compiler Page

Variables

This second page lists all registered variables, and allows you to edit their values upon compiling.

Variables Page after configuration

PHP Configuration

add_filter( 'sass_configuration', 'my_sass_config' );
function my_sass_config( $defaults ) {
  return array(
    'variables' => array( 'sass/_variables.scss' ),
    'imports'   => array( 'sass/bootstrap.scss', 'sass/_theme.scss' )
  );
}

Configuration of the plugin is optional, but you should at least register your variables if you are using a CSS framework.

Paths to Sass files are relative to the theme directory.

Use the filter 'sass_configuration' to return your configurations array.

  • variables(array)
    In order to list and edit your Sass variables on the plugin dedicated page, it is necessary to register their “definition” file(s). It is assumed that those files’ only role is to declare variables and their values.
  • imports(array)
    It will prepend the code to compile with an @import directive for each listed file. Useful to compile dependencies, and to “hide” them from the Compiler page. It is necessary to hit the “Compile” button once for these imports to be compiled.
  • cache(string)
    Allows you to define the path to your cache directory. This directory has to be writable (0755). The default cache directory path is wp-content/cache.
  • search(boolean)
    Wether or not to display the “Search Variables” filter box. It can come handy if you have a lot of variables. Default is true.

Once registered, you can access any variables with the function sass_get( 'my-variable' );, or override its value (upon what has been set on the Variables page) with sass_set( 'my-variable', 'my-value' ).

It is also possible to use @import directives, as well as any Sass language features, straight from the Compiler page.

Enqueuing external Sass

Out of the main stylesheet, simply use the WordPress wp_enqueue_style function to enqueue separated Sass files.

add_action( 'wp_enqueue_scripts', 'my_other_sass_enqueue' );
function my_other_sass_enqueue() {
  wp_enqueue_style( 'my-other-handle', get_template_directory_uri() . '/my-other-file.scss', array( 'wm-sass' ) );
}

It will be compared to the cached version, compiled if changes occurred, and the resulting stylesheet will be enqueued.
Don’t forget to set the main Sass stylesheet handle 'wm-sass' as a dependency (… if it is one).