Recursive sass asset manager

in code

I have a stack of assets that needed a minimum-fucking-around solution to manage and load on demand, so I came up with this function: Call it with a mandatory asset and optional variants, and it returns the path to the asset wrapped in url($foo);.

@function asset($asset, $variants: null) {
    $asset: map-get($assets, $asset);

    @if ($variants) {
        @each $variant in $variants {
            $asset: map-get($asset, $variant);
        }
    }

    @return url($asset);
}
$assets: (
    brand: (
        tuairisc: (
            black: $brands + 'tuairisc-black.svg',
            white: $brands + 'tuairisc-white.svg'
        ),
        foras: (
            black: 'foras-black.svg',
            white: 'foras-white.svg'
        ),
        greann: $brands + 'greann.svg'
    )
);

For example (yes, the selector is cancer; it’s a theme engine tied to the <body> tag):

body[class*=section]:not(.section-greann) a#home {
     background: center/contain no-repeat asset(brand, tuairisc white);
}

The function looks for brand, and then recursively fetches tuairisc white. Compiled output is:

body[class*=section]:not(.section-greann) a#home {
     background: center/contain no-repeat url('assets/images/brands/tuairisc-white.svg');
}


^([Kk]il+|me|[no]w)$

in code

I spent today writing a two thousand word article on regular expressions. Symbols dance around when I close my eyes. Kill me now.


I updated Social Meta

in code

Some dude made a request for an Open Graph plugin on the /r/wordpress subreddit. I gave a pointless link to a library I knew he can’t use, because his question was lazy:

https://github.com/bhalash/social-meta

I realized the library can be fiddly to use when I looked back over my post, so I refactored the script as a class with a constructor.

include('/path/to/social-meta/social-meta.php');

$my_social_meta_instance = new Social_Meta(array(
    'twitter' => '@username',
    'facebook' => 'username',
    'fallback_image' => array(
        'url' => get_template_directory_uri() . '/path/to/image.jpg',
        'path' => get_template_directory() . '/path/to/image.jpg'
    )
));

Article Images has been likewise updated; it now has a function to set fallback image.


The accidental 15k walk

in ireland

Whoops.

Eadaoin and Sandymount Strand
Looking back at the Pigeon House
Poolbeg Lighthouse

Eadaoin and I went for an accidental fifteen kilometre walk on Saturday. In inappropriate gear, no less. “…wait, what?” I hear you sputter in consternation. Yes, it was dumb — I was dumb, to not grasp the length. So, in short, we decided to walk from Blackrock to Poolbeg Lighthouse by way of Strandybeach Mount (or whatever it is called) in total ignorance of the distance. Turns out the walk from the main road to Poolbeg Lighthouse alone is six kilometres, and our walk up the beach was a further eight. In shitty knockoff Converse shoes with no support.

My back came away all fucked.


x to y of z search results II

in code

The code snippet I posted last August hasn’t worked too well:

  • It only works for the index.php loop of all posts.
  • All other loops (search, taxonomy, archive, etc.) have weird counts.

I changed it to use the global wp_query variable to determine post count, with an addendum for private posts if I am logged in. get_loop_type is a shorthand function I wrote a while ago.

function archive_page_count($echo = false, $page_num = null, $total_results = null) {
    global $wp_query;

    if (is_null($total_results)) {
        $total_results = $wp_query->found_posts;
    }

    if (is_null($page_num)) {
        $page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
    }

    $posts_per_page = get_option('posts_per_page');
    $total_pages = ceil($total_results / $posts_per_page);
    $page_count = sprintf(__('Page %s of %s', TTD), $page_num, $total_pages);

    if (!$echo) {
        return $page_count;
    }

    printf($page_count);
}