Playing around with PHP "Closures"
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

UA Matcher

For certain platforms we want to have a way to direct users on those platforms to the corresponding store that contains our native app. For instance, user goes to

And on that page is a button or link that tells the user to click on it to go to the Apple App Store to download WordPress for iOS.

This class provides a clean way to pair the logic for determining which platform the visitor is using with the URL and label we want to use for the link we will show them.


Instantiate and configure the instance:

$matcher = new WPCOM_Platform_Matcher();

Provide a single regex to compare against the User Agent (or whatever string is given):

$matcher->on( "/ios/i", '' );

Provide a closure to use to examine the UA to do some fancy comparing:

$matcher->on( function( $ua ){
  if ( strlen( $ua ) > 10 && preg_match( "/something/i", $ua ) ) {
    return true;
  return false;
}, '' );

Provide an instance of any object that has an __invoke method:

class TrueEveryOther {
  private $is_true = true;
  function __invoke(){
    $this->is_true = !$this->is_true;
    return $is_true;

$matcher->prepend( new TrueEveryOther, "" );

You can also chain the methods:

  ->on( "/[\d]+/", "" )
  ->on( "/wordpress/i", "" )
  ->on( "/(one|two|three)/i", "" );

When you want to find a match for a User Agent then you:

$match = $matcher->matching( 'Mozilla Whatever or something' );
echo $match;

Running Tests

First time PHPUnit user and this is how I ran the tests:

phpunit Tests/WPCOM_Platform_RedirectorTests