Intercept FORM submit

Nakaci se na ‘submit’ event, napravi nesto sa vrijednostima i dodaj novi input koji ce se submitati

$(document).on('submit', 'form', function (e) {
    e.preventDefault();

    var form = $(this),
        elements_to_submit = $('[name]', this),
        signed_values = nv.crypto.sign({
            exclude_elements: ['__RequestVerificationToken'],
            hash_fn: nv.crypto.hash.simpleHash,
            elements_to_sign: elements_to_submit
        });

    form.append('<input type="hidden" name="signedValues" value="' + signed_values + '">');

    this.submit();
});

 

PHP run process in background

This launches the command $cmd, redirects the command output to $outputfile, and writes the process id to $pidfile.

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

This lets you easily monitor what the process is doing and if it’s still running.

function isRunning($pid){
    try{
        $result = shell_exec(sprintf("ps %d", $pid));
        if( count(preg_split("/\n/", $result)) > 2){
            return true;
        }
    }catch(Exception $e){}

    return false;
}

 Source

Manual:

exec

proc_open

 

Setting hostname in ubuntu

Most people recommend setting up the hostname on a Linux box so that:

1) running ‘hostname‘ returns the short name (i.e. myhost)
2) running ‘hostname -f‘ returns the FQDN (i.e. myhost.prod.example.com)
3) running ‘hostname -d‘ returns the domain name (i.e prod.example.com)

After experimenting a bit and also finding this helpful Server Fault post, here’s what we did to achieve this (we did it via Chef recipes, but it amounts to the same thing):

  • make sure we have the short name in /etc/hostname:

myhost

(also run ‘hostname myhost‘ at the command line)

  • make sure we have the FQDN as the first entry associated with the IP of the server in /etc/hosts:

10.0.1.10 myhost.prod.example.com myhost myhost.prod

  • make sure we have the domain name set up as the search domain in /etc/resolv.conf:

search prod.example.com

Reboot the box when you’re done to make sure all of this survives reboots.
http://agiletesting.blogspot.com/2014/06/setting-up-hostname-in-ubuntu.html

Javscript client side error logging

<script type="text/javascript" src="/static/js/stacktrace.js">
</script>
<script type="text/javascript">

window.onerror = function () {
    var stackTrace, argData, docData, request;
    var _doc = {};

    _doc.URL = document.URL;
    docData = encodeURIComponent(
        JSON.stringify(_doc));
    argData = encodeURIComponent(
        JSON.stringify(arguments));
    stackTrace = JSON.stringify(printStackTrace());

    console.log(arguments);

    request = new XMLHttpRequest();
    request.open(
        'POST', 
        "https://www.yoursite.com/error-logging-url"
    );
    request.setRequestHeader(
        "Content-type",
        "application/x-www-form-urlencoded"
    );
    request.onload = function (e) {};
    request.send(
        "docData=" + docData + "&" + "argData="
       + argData + "&" + "stackTrace=" + stackTrace);

    return false;
}
</script>

 

Code quality

Answer yes or no to each.

  1. Could a new developer run the code on their machine within five minutes?
  2. Could you look at any section of code and understand its purpose?
  3. Could you locate the code for any feature within ten minutes?
  4. Could you explain all of the code in a way that a junior developer could understand?
  5. Could you re-factor the code without fear of breaking it?
  6. Could you test a component without testing its dependencies?
  7. If any library or technology becomes obsolete, could you replace it easily?
  8. Could you take modules and re-use them in other projects with no modification?
  9. Can you be certain that each line of code is actually being used?
  10. If the code became open-source, would you be happy to be associated with it?

The Joel Test

  1. Do you use source control?
  2. Can you make a build in one step?
  3. Do you make daily builds?
  4. Do you have a bug database?
  5. Do you fix bugs before writing new code?
  6. Do you have an up-to-date schedule?
  7. Do you have a spec?
  8. Do programmers have quiet working conditions?
  9. Do you use the best tools money can buy?
  10. Do you have testers?
  11. Do new candidates write code during their interview?
  12. Do you do hallway usability testing?

WordPress Check User Role Function

/**
 * Checks if a particular user has a role. 
 * Returns true if a match was found.
 *
 * @param string $role Role name.
 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
 * @return bool
 */
function appthemes_check_user_role( $role, $user_id = null ) {

    if ( is_numeric( $user_id ) )
	$user = get_userdata( $user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user ) )
	return false;

    return in_array( $role, (array) $user->roles );
}

// example use for the current user
if ( appthemes_check_user_role( 'customer' )
    _e( "You've got access dude!", 'appthemes' );
else
    _e( "Sorry man, no luck.", 'appthemes' );

// example use for a specific user
$user_id = 23;

if ( appthemes_check_user_role( 'customer', $user_id )
    _e( "You've got access dude!", 'appthemes' );
else
    _e( "Sorry man, no luck.", 'appthemes' );