Skip to content

Instantly share code, notes, and snippets.

View attitude's full-sized avatar

Martin Adamko attitude

View GitHub Profile
@attitude
attitude / function-array_merge_for_sql.php
Last active December 19, 2015 10:58
Function to merge new SQL argument arrays (right) with defaults (left)
<?php
/**
* Merges new SQL argument arrays with defaults
*
* Differs from {@link array_merge()} and {@link array_merge_recursive()}:
*
* 1. Array of 'where', 'orderby', 'groupby' and 'join' keys should be
* appended and remain unique.
* 2. 'select', 'limit' should be overwritten, by new argument on right
@attitude
attitude / function-file_size_format.php
Last active December 19, 2015 13:49
Returns formatted file size
<?php
/**
* Returns formatted file size
*
* Inspired by {@link http://betterwp.net/wordpress-tips/size_format/}, modified
* to work outside of WordPress environment.
*
* @param int $bytes Size in bytes
* @param int $decimals Number of decimal places
@attitude
attitude / function-wp_get_attachment_meta.php
Last active December 19, 2015 13:49
Returns metadata for current WordPress attachment or array of attachments
<?php
/**
* Returns metadata for current WordPress attachment or array of attachments
*
* Reads all attachment metadata and expands it. URLs are rewritten with upload
* dir prepended, file size is calculated if function exists. By default url protocol
* is removed, use `add_filter()` to disable.
*
* @uses {@link apply_filters() http://codex.wordpress.org/Function_Reference/apply_filters}
@attitude
attitude / function-maybe_decode_json.php
Last active December 19, 2015 16:29
Decodes JSON string if needed
<?php
/**
* Decodes JSON string if needed
*
* @uses json_decode()
* @author Martin Adamko <@martin_adamko>
* @licence The MIT Licence <http://opensource.org/licenses/MIT>
*
* @param string $str String to decode if needed
@attitude
attitude / function-get_pagination_data.php
Created July 12, 2013 21:47
Generate {{mustache}} ready data for pagination
<?php
/**
* Returns {{mustache}} ready data for pagination
*
* Pagination array for `prev`, `next`, `start`, `end` and `jump`
* to direct pages
*
* @author Martin Adamko <@martin_adamko>
* @licence The MIT Licence <http://opensource.org/licenses/MIT>
@attitude
attitude / on-git-read-tree-and-dependencies.md
Created September 18, 2013 16:23
Reusing of single library component in your project with Git `read-tree`

Here is a quick tip on pulling dependencies in your project using git read-tree command.

The Problem

How to use a single component from some library (eg. Form from Symfony), but not the whole library (tried the git submodule ?).

  1. Init project:

    Create a new project and init Git:

@attitude
attitude / closing-connection-early-page-load-speed-tip.md
Created September 23, 2013 13:08
Faster page loading by closing connection early and deferring processes to finish only after that

Page Speed Tip: Defer time consuming processes after sending the page output

Recently I have written a PHP class to try server analytics. But using the UAMP, script has to wait for response from Google's servers in order to close the connection. Even if the response took less than 100ms, still it's unnecessary time for client to wait and show loading spinner and block page render.

There is a space for optimisation: closing connection early and present the client with the response before the script finishes it's run.

@attitude
attitude / platform.js
Created February 27, 2016 09:22
Platform CSS class on HTML tag
/**
* Platform CSS class on HTML tag inspired by https://github.com/driftyco/ionic/blob/67ef9ebde21ac64e1f9e3db43b16c2db7ed2256d/js/utils/platform.js
*/
(function(w, el) {
var platformName,
IOS = 'ios',
ANDROID = 'android',
WINDOWS_PHONE = 'windowsphone',
EDGE = 'edge',
CROSSWALK = 'crosswalk';
@attitude
attitude / hideGoogleSerp.js
Last active May 29, 2016 12:14
Script to dim some SERP in Chrome; requires Control Freak extension.
(function(domains) {
var to,
observer,
// Google SERP div#search
search,
// Array of links
links,
// Passed arg iterator
domainsI,
// Passed arg length
@attitude
attitude / hideGoogleSerps.js
Last active May 29, 2016 13:37
Tiny script to dim some Google SERPs in Chrome; requires Control Freak extension.
(function (w, domains) {
'use strict';
var to,
// Google SERP div#search
search,
// Array of links
links,
// Passed arg iterator
domainsI,
// Passed arg length